int search(struct arg_struct *arg) { // printf("# Thread working: %u\n", (int)pthread_self()); int n = arg->topic; int start = arg->startidx; int end = arg->endidx; heap* h = arg->h; heap_create(h,0,NULL); int i=0, j=0; int base = arg->base; float score; int t; float* min_key; int* min_val; for (i=start; i<end; i++) { if (tweetids[i] > topics2011_time[n]) { base += doclengths[i]; continue; } score = 0; int tf[topics2011[n][1]]; memset(tf,0,sizeof(tf)); for (j=0; j<doclengths[i]; j++) { for (t=2; t<2+topics2011[n][1]; t++) { if (collection_pos[base+j] == topics2011[n][t]) { tf[t - 2] ++; } } } for (t = 0; t < topics2011[n][1]; t++) { if (tf[t] > 0) { score += log(1 + tf[t]/(MU * (cf[topics2011[n][t+2]] + 1) / (TOTAL_TERMS + 1))) + log(MU / (doclengths[i] + MU)); } } if (score > 0) { int size = heap_size(h); if ( size < TOP_K ) { int *docid = malloc(sizeof(int)); *docid = i; float *scorez = malloc(sizeof(float)); *scorez = score; heap_insert(h, scorez, docid); } else { heap_min(h, (void**)&min_key, (void**)&min_val); if (score > *min_key) { heap_delmin(h, (void**)&min_key, (void**)&min_val); int *docid = malloc(sizeof(int)); *docid = i; float *scorez = malloc(sizeof(float)); *scorez = score; heap_insert(h, scorez, docid); } } } base += doclengths[i]; } return 0; }
int search(int n) { struct arg_struct *args = malloc(sizeof (struct arg_struct)); args->topic = n; args->startidx = 0; args->endidx = num_docs; args->base = 0; heap h; heap_create(&h,0,NULL); args->h = &h; scansearch(args); float* min_key; int* min_val; int rank = TOP_K; while (heap_delmin(&h, (void**)&min_key, (void**)&min_val)) { printf("MB%02d Q0 %ld %d %f " SCANNAME "\n", (n+1), tweetids[*min_val], rank, *min_key); rank--; } heap_destroy(&h); free(args); }
void *heap_delmin_value(heap* h) { void *value = NULL; heap_delmin(h, NULL, &value); return value; }
int main(int argc, const char* argv[]) { if (argc <= 1) { printf("PLEASE ENTER THREAD NUMBER!\n"); return 0; } int nthreads=atoi(argv[1]); printf("Number of threads: %d\n", nthreads); init_pos(); double total = 0; int N = 3; int count; for (count = 1; count <= N; count ++) { struct timeval begin, end; double time_spent; gettimeofday(&begin, NULL); int n; for (n=0; n<NUM_TOPICS; n++) { // printf("Processing topic %d...\n", topics2011[n][0]); heap h_array[nthreads]; memset(h_array,0,sizeof(h_array)); struct threadpool *pool; pool = threadpool_init(nthreads); int i = 0; for (i=0; i<nthreads; i++) { struct arg_struct *args = malloc(sizeof *args); args->topic = n; args->startidx = i*(int)(ceil((double)NUM_DOCS / nthreads)); if ((i+1)*(int)(ceil((double)NUM_DOCS / nthreads)) > NUM_DOCS) { args->endidx = NUM_DOCS; } else { args->endidx = (i+1)*(int)(ceil((double)NUM_DOCS / nthreads)); } args->base = termindexes[nthreads-1][i]; heap h; h_array[i] = h; args->h = &h_array[i]; threadpool_add_task(pool,search,args,0); } threadpool_free(pool,1); heap h_merge; heap_create(&h_merge,0,NULL); float* min_key_merge; int* min_val_merge; for (i=0; i<nthreads; i++) { float* min_key; int* min_val; while(heap_delmin(&h_array[i], (void**)&min_key, (void**)&min_val)) { int size = heap_size(&h_merge); if ( size < TOP_K ) { heap_insert(&h_merge, min_key, min_val); } else { heap_min(&h_merge, (void**)&min_key_merge, (void**)&min_val_merge); if (*min_key_merge < *min_key) { heap_delmin(&h_merge, (void**)&min_key_merge, (void**)&min_val_merge); heap_insert(&h_merge, min_key, min_val); } } } heap_destroy(&h_array[i]); } int rank = TOP_K; while (heap_delmin(&h_merge, (void**)&min_key_merge, (void**)&min_val_merge)) { printf("MB%02d Q0 %ld %d %f Scan1_pos_multithread_intraquery\n", (n+1), tweetids[*min_val_merge], rank, *min_key_merge); rank--; } heap_destroy(&h_merge); } gettimeofday(&end, NULL); time_spent = (double)((end.tv_sec * 1000000 + end.tv_usec) - (begin.tv_sec * 1000000 + begin.tv_usec)); total = total + time_spent / 1000.0; } printf("Total time = %f ms\n", total/N); printf("Time per query = %f ms\n", (total/N)/NUM_TOPICS); }
int main(int argc, char** argv) { // Create the heap heap h; heap_create(&h, 0, NULL); // Maximum int count = 10000000; // 10M if (argc > 1) count = atoi(argv[1]); // Get the count as an argument printf("Sorting array of %d random entries.\n", count); // Allocate a key and value int* key = (int*)malloc(count*sizeof(int)); char* value = "The meaning of life."; // Initialize the first key unsigned int val = 42; srand(val); printf("Seed %d\n", val); // Store that as the minimum int min = INT_MAX; // Use a pseudo-random generator for the other keys for (int i=0; i<count; i++) { *(key+i) = rand(); // Check for a new min if (*(key+i) < min) min = *(key+i); // Insert into the heap heap_insert(&h, key+i, value); } // Get the minimum int* min_key; char* min_val; int *prev_key = &min; // Show the real minimum printf("Real min: %d\n", min); // Try to get the minimum while (heap_delmin(&h, (void**)&min_key, (void**)&min_val)) { // Verify that the values are getting larger if (*prev_key > *min_key) { printf("Previous key is greater than current key!\n"); break; } prev_key = min_key; } if (heap_size(&h) == 0) printf("Verify success!\n"); // Clean up the heap heap_destroy(&h); }