struct bnode* bst_put(struct bnode* x, int val) { int cmp; if (x == NULL) { struct bnode* n = malloc(sizeof(struct bnode)); memset(n, 0, sizeof(struct bnode)); n->v = val; n->n = 1; return n; } cmp = val - x->v; if (cmp < 0) x->left = bst_put(x->left, val); else if (cmp > 0) x->right = bst_put(x->right, val); else x->v = val; x->n = 1 + bst_size(x->left) + bst_size(x->right); return x; }
void *test(void *data) { fprintf(stderr, "Starting test\n"); //get the per-thread data thread_data_t *d = (thread_data_t *)data; //place the thread on the apropriate cpu set_cpu(d->id); int op_count = 10000; ssalloc_init(); /* Wait on barrier */ barrier_cross(d->barrier); int i; bst_value_t* val = (bst_value_t*)malloc(sizeof(bst_value_t)); bst_value_t* added; for ( i = 1; i <= op_count; i++){ *val = d->id*op_count+i; // bst_value_t val = d->id*op_count+i; // fprintf(stderr, "[%d] before add\n", pthread_self()); added = bst_put(i, val, root); // fprintf(stderr, "[%d] Added %d\n", pthread_self(), i); // fprintf(stderr, "[%d] Added %d? %d\n", d->id, i, added==TRUE); if (added == NULL) { d->num_insert++; FAI_U8(&v[i]); } } // printf("Root right node: %d", root->right->key); for (i = 1; i <= op_count; i++){ bool_t found = (bst_get(i, root) != NULL); // printf("Contains %d? %d\n", i, found==FOUND); if (found) { d->num_search ++; } } // fprintf(stderr, "After insertions, found %d\n", d->num_search); // d->num_search = 0; for ( i = 1; i <= op_count; i++){ bool_t removed = (bst_remove(i, root) != NULL); // printf("Removed %d? %d\n", i, removed==TRUE); if (removed == TRUE) { d->num_remove ++; FAI_U8(&v[i]); } } // for ( i = 1; i <= op_count; i++){ // bool_t found = (bst_get(i) != NULL); // // printf("Contains %d? %d\n", i, found==FOUND); // if (found) { // d->num_search ++; // } // } // fprintf(stderr, "After deletions, found %d\n", d->num_search); return NULL; }