void* threadpool_excute_task(void *arg) { int index = 0; void *tmp_arg = NULL; threadpool_t *tpool; tpool = (threadpool_t*)arg; return_if_null(tpool); threadpool_task_func_t task_func; while(1) { pthread_mutex_lock(&tpool->mutex_queue); index = tpool->task_queue->head; if(index == tpool->task_queue->tail) { pthread_mutex_unlock(&tpool->mutex_queue); pthread_mutex_lock(&tpool->mutex_cond); pthread_cond_wait(&tpool->cond, &tpool->mutex_cond); //等待线程调度 pthread_mutex_unlock(&tpool->mutex_cond); } else { task_func = tpool->task_queue->task_func[index]; tmp_arg = tpool->task_queue->arg[index]; tpool->task_queue->head = (index+1) % tpool->max_thr_num; pthread_mutex_unlock(&tpool->mutex_queue); task_func(tmp_arg); printf("process end"); } } }
static void *sched_call_func(void *args) { ScheduleEntry *pEntry; void *func_args; TaskFunc task_func; int task_id; pEntry = (ScheduleEntry *)args; task_func = pEntry->task_func; func_args = pEntry->func_args; task_id = pEntry->id; logDebug("file: "__FILE__", line: %d, " \ "thread enter, task id: %d", __LINE__, task_id); pEntry->thread_running = true; task_func(func_args); logDebug("file: "__FILE__", line: %d, " \ "thread exit, task id: %d", __LINE__, task_id); pthread_detach(pthread_self()); return NULL; }
int main ( int argc, char* argv[] ) { int i, j, level; #pragma omp parallel { nthreads = omp_get_num_threads(); } if ( argc != 5 ) { fprintf(stderr, "Usage: %s length1 length2 tileWidth tileHeight\n", argv[0]); exit(1); } fprintf(stdout, "Running SmithWaterman (OpenMP) with %d threads\n", nthreads); unsigned long long length1 = strtoull(argv[1], NULL, 10); unsigned long long length2 = strtoull(argv[2], NULL, 10); tile_width = (int) atoi (argv[3]); tile_height = (int) atoi (argv[4]); unsigned long long n_char_in_file_1 = length1; unsigned long long n_char_in_file_2 = length2; string_1 = (signed char *)malloc(n_char_in_file_1); string_2 = (signed char *)malloc(n_char_in_file_2); random_init(string_1, n_char_in_file_1); random_init(string_2, n_char_in_file_2); fprintf(stdout, "Tile width %d\n", tile_width); fprintf(stdout, "Tile height %d\n", tile_height); if (n_char_in_file_1 % tile_width) { fprintf(stderr, "String1 length should be multiple of tile width\n"); exit(1); } if (n_char_in_file_2 % tile_height) { fprintf(stderr, "String2 length should be multiple of tile height\n"); exit(1); } n_tiles_x = n_char_in_file_1/tile_width; n_tiles_y = n_char_in_file_2/tile_height; fprintf(stdout, "Computing %d x %d intra-node tiles\n", n_tiles_x, n_tiles_y); strlen_1 = n_char_in_file_1; strlen_2 = n_char_in_file_2; // Allocate space for edge data and diagonals // Increase tile space to allocate for initial condition n_tiles_x++; n_tiles_y++; int num_tiles = n_tiles_x*n_tiles_y; put_counts = (int*)malloc(sizeof(int)*num_tiles); for (i=0; i<num_tiles; i++) { put_counts[i] = 0; } // Allocate space for diagonal puts int num_diags = n_tiles_x + n_tiles_y - 1; tile_diag = (int*)malloc(sizeof(int)*num_diags); tile_diag[DIAG_INDEX(0,0)] = 0; PUT_DIAG(0,0); for (i = 1; i < n_tiles_x; i++) { tile_diag[DIAG_INDEX(0,i)] = GAP_PENALTY*(i*tile_width); PUT_DIAG(0,i); } for (i = 1; i < n_tiles_y; i++) { tile_diag[DIAG_INDEX(i,0)] = GAP_PENALTY*(i*tile_height); PUT_DIAG(i,0); } // Allocate space for y puts tile_edges_y = (int**)malloc(sizeof(int*)*n_tiles_y); for (i = 1; i < n_tiles_y; i++) { tile_edges_y[i] = (int*)malloc(sizeof(int)*(tile_height)); for (j = 0; j < tile_height; j++) tile_edges_y[i][j] = GAP_PENALTY*((i-1)*tile_height+j); PUT_RCOL(i,0); } // Allocate space for x puts tile_edges_x = (int**)malloc(sizeof(int*)*n_tiles_x); for (i = 1; i < n_tiles_x; i++) { tile_edges_x[i] = (int*)malloc(sizeof(int)*(tile_width)); for (j = 0; j < tile_width; j++) tile_edges_x[i][j] = GAP_PENALTY*((i-1)*tile_width+j); PUT_BROW(0,i); } // Allocate working tile for each worker thread worker_tiles = (int***)malloc(sizeof(int**)*nthreads); for (i = 0; i < nthreads; i++) { worker_tiles[i] = (int**)malloc(sizeof(int*)*(tile_height+1)); for (j = 0; j < tile_height+1; j++) worker_tiles[i][j] = (int*)malloc(sizeof(int)*(tile_width+1)); } // Setup SIZE dsizes[DIAG_KIND] = 1; //diag dsizes[RCOL_KIND] = tile_height; //right column dsizes[BROW_KIND] = tile_width; //bottom row max_size = dsizes[0]; for (i = 1; i < NUM_DEPS; i++) if (max_size < dsizes[i]) max_size = dsizes[i]; max_buffer = (int*)malloc(sizeof(int)*max_size); temp_buffer = (int*)malloc(sizeof(int)*max_size); // Print 2-D matrix of tiles #if DEBUG fprintf(stdout, "\nTile Matrix:\n\n "); for ( j = 0; j < n_tiles_x; j++ ) fprintf(stdout," %3d ", j); fprintf(stdout,"\n "); for ( j = 0; j < n_tiles_x; j++ ) fprintf(stdout," ----------- ", j); fprintf(stdout,"\n\n"); for ( i = 0; i < n_tiles_y; i++ ) { fprintf(stdout,"%3d |", i); for ( j = 0; j < n_tiles_x; j++ ) { int diag_guid = GUID_DIAG(i,j); int rcol_guid = GUID_RCOL(i,j); fprintf(stdout," [%4d %4d] ", DIST_HOME(diag_guid),rcol_guid); } fprintf(stdout,"\n "); for ( j = 0; j < n_tiles_x; j++ ) { int diag_guid = GUID_DIAG(i,j); int brow_guid = GUID_BROW(i,j); fprintf(stdout," [%4d %4d] ", brow_guid,diag_guid); } fprintf(stdout,"\n\n"); } fflush(stdout); #endif int result_row = n_tiles_y - 1; int result_col = n_tiles_x - 1; done = 0; gettimeofday(&begin,0); tiles_to_run *curr = (tiles_to_run*)malloc(sizeof(tiles_to_run)); curr->base = (tile *)malloc(sizeof(tile) * n_tiles_y * n_tiles_x); curr->q = curr->base; curr->capacity = n_tiles_y * n_tiles_x; curr->length = 1; (curr->base)[0].row = 1; (curr->base)[0].col = 1; tiles_to_run *next = (tiles_to_run*)malloc(sizeof(tiles_to_run)); next->base = (tile *)malloc(sizeof(tile) * n_tiles_y * n_tiles_x); next->q = next->base; next->capacity = n_tiles_y * n_tiles_x; next->length = 0; while (curr->length > 0) { tile *mine = NULL; #pragma omp parallel firstprivate(mine) { bool done = false; while (!done) { #pragma omp critical { mine = pop_next_tile(curr); } done = (mine == NULL); if (!done) { task_func(mine->row, mine->col, next); } } } tiles_to_run *tmp = curr; curr = next; next = tmp; curr->q = curr->base; next->length = 0; #ifdef __CHIMES_SUPPORT checkpoint(); #endif } gettimeofday(&end,0); int score = tile_diag[DIAG_INDEX(result_row,result_col)]; fprintf(stdout, "score: %d\n", score); fprintf(stdout, "The computation took %f seconds\n",((end.tv_sec - begin.tv_sec)*1000000+(end.tv_usec - begin.tv_usec))*1.0/1000000); for (i = 0; i < nthreads; i++) { for (j = 0; j < tile_height+1; j++) free(worker_tiles[i][j]); free(worker_tiles[i]); } free(worker_tiles); for (i = 1; i < n_tiles_y; i++) free(tile_edges_y[i]); free(tile_edges_y); for (i = 1; i < n_tiles_x; i++) free(tile_edges_x[i]); free(tile_edges_x); free(tile_diag); free(string_1); free(string_2); return 0; }