1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <mcheck.h>
#include <pthread.h>
#include <sys/time.h>
#ifndef PARALLELIZE
#define PARALLELIZE 1
#endif
#define N_CHILD 8
int G_MAX_DEPTH = 7;
int G_ITERATION = 11;
int G_TRACEITER = -1;
int G_ITER = 0;
typedef struct node {
int dep; /* depth in tree */
int off; /* offset at the same depth */
int idx; /* index of memory array, -1 indicates using pointer */
int nchild; /* number of children */
} node_t;
node_t *G_NODES_ARR;
inline int curr_time_micro(void)
{
struct timeval tp[1];
struct timezone tzp[1];
gettimeofday (tp, tzp);
return tp->tv_sec * 1000000 + tp->tv_usec;
}
inline void * xmalloc(size_t size)
{
void *p = malloc(size);
if (p == NULL) {
perror("malloc failed");
abort();
}
return p;
}
inline int quick_base(int depth)
{
switch (depth) {
case 0: return 0;
case 1: return 1;
case 2: return 9;
case 3: return 73;
case 4: return 585;
case 5: return 4681;
case 6: return 37449;
case 7: return 299593;
case 8: return 2396745;
case 9: return 19173961;
case 10: return 153391689;
default: return (pow(N_CHILD, depth) - 1) / (N_CHILD - 1);
}
}
void tree_build_serial(node_t *node)
{
int child_dep, base, offset, off, idx, i;
node_t *child;
if (node->dep >= G_MAX_DEPTH) return;
child_dep = node->dep + 1;
base = quick_base(child_dep);
offset = node->off * N_CHILD;
node->nchild = N_CHILD;
for (i = 0; i < N_CHILD; i++) {
off = offset + i;
idx = base + off;
child = G_NODES_ARR + idx;
child->dep = child_dep;
child->off = off;
child->idx = idx;
child->nchild = 0;
if (child_dep < G_MAX_DEPTH) {
tree_build_serial(child);
}
}
}
void * tree_build_parallel(void *args)
{
pthread_t ths[N_CHILD];
node_t *node = (node_t *) args;
int child_dep, base, offset, off, idx, i;
node_t *child;
if (node->dep >= G_MAX_DEPTH) return (void *) 0;
child_dep = node->dep + 1;
base = quick_base(child_dep);
offset = node->off * N_CHILD;
node->nchild = N_CHILD;
for (i = 0; i < N_CHILD; i++) {
off = offset + i;
idx = base + off;
child = G_NODES_ARR + idx;
child->dep = child_dep;
child->off = off;
child->idx = idx;
child->nchild = 0;
if (child_dep < G_MAX_DEPTH) {
if (i < N_CHILD - 1)
pthread_create(&ths[i], NULL, tree_build_parallel, child);
else
tree_build_serial(child);
}
}
if (child_dep < G_MAX_DEPTH) {
for (i = 0; i < N_CHILD - 1; i++)
pthread_join(ths[i], NULL);
}
}
void tree_build(node_t *root)
{
#if PARALLELIZE
tree_build_parallel(root);
#else
tree_build_serial(root);
#endif
}
void tree_traversal_serial(node_t *node, int *n)
{
int offset, i;
(*n)++;
offset = quick_base(node->dep + 1) + node->off * N_CHILD;
for (i = 0; i < node->nchild; i++)
tree_traversal_serial(G_NODES_ARR + offset + i, n);
}
struct count_nodes {
node_t *node;
int n;
};
void * tree_traversal_parallel(void *args)
{
pthread_t ths[N_CHILD];
struct count_nodes dat[N_CHILD];
struct count_nodes * p = (struct count_nodes *) args;
int offset, i;
p->n = 1;
offset = quick_base(p->node->dep + 1) + p->node->off * N_CHILD;
for (i = 0; i < p->node->nchild; i++) {
dat[i].node = G_NODES_ARR + offset + i;
if (i < p->node->nchild - 1) {
pthread_create(&ths[i], NULL, tree_traversal_parallel, &dat[i]);
} else
tree_traversal_parallel(&dat[i]);
}
for (i = 0; i < p->node->nchild; i++) {
if (i < p->node->nchild - 1)
pthread_join(ths[i], NULL);
p->n += dat[i].n;
}
}
void tree_traversal(node_t *root, int *n)
{
#if PARALLELIZE
struct count_nodes dat;
dat.node = root;
dat.n = 0;
tree_traversal_parallel(&dat);
*n = dat.n;
#else
tree_traversal_serial(root, n);
#endif
}
void tree_free(void)
{
int total = (pow(N_CHILD, G_MAX_DEPTH + 1) - 1) / (N_CHILD - 1);
memset(G_NODES_ARR, 0x0, sizeof(node_t) * total);
}
int main(int argc, char *argv[])
{
int total;
int n, i, t0, t1, t2, t3;
node_t root;
if (argc < 2) {
printf("Arguments not sufficient, use default values\n");
} else {
G_MAX_DEPTH = atoi(argv[1]);
if (argc >= 3)
G_ITERATION = atoi(argv[2]);
if (argc >= 4)
G_TRACEITER = atoi(argv[3]);
else
G_TRACEITER = 0.8 * G_ITERATION;
}
total = (pow(N_CHILD, G_MAX_DEPTH + 1) - 1) / (N_CHILD - 1);
G_NODES_ARR = xmalloc(sizeof(node_t) * total);
memset(G_NODES_ARR, 0x0, sizeof(node_t) * total);
printf("Depth: %d, Iter: %d (mtrace the %dth), Nodes: %d\n",
G_MAX_DEPTH, G_ITERATION, G_TRACEITER, total);
root.dep = 0;
root.off = 0;
root.idx = 0;
root.nchild = 0;
for (G_ITER = 0; G_ITER < G_ITERATION; G_ITER++) {
t0 = curr_time_micro();
if (G_ITER == G_TRACEITER) mtrace();
tree_build(&root);
if (G_ITER == G_TRACEITER) muntrace();
t1 = curr_time_micro();
/*
for (i = 0; i < total; i++) {
node_t *p = G_NODES_ARR + i;
printf("%d: %d %d %d %d\n", i, p->dep, p->off, p->idx, p->nchild);
}
*/
n = 0;
tree_traversal(&root, &n);
if (n != total)
printf("Tree check failed! (%d vs. %d)\n", total, n);
t2 = curr_time_micro();
tree_free();
t3 = curr_time_micro();
printf("[%02d] Build: %d Traversal: %d Free: %d\n", G_ITER, t1-t0, t2-t1, t3-t2);
}
return 0;
}
|