OMP更多线程比核心仍然具有相同的性能

时间:2016-11-25 15:16:29

标签: c multithreading openmp

我有一部分与OpenMP的串行程序。当我用8个线程(我的计算机可以使用8个线程)执行它时,同时我用16或32或64等表示它是正常的吗?这是正常的吗? Couse我认为当我创建更多的线程而不是核心时,程序将会缓慢。如果要检查它,这是代码。它运行正常!在main中,在其他文件中,有一组线程。

       void truncated_radix_sort(unsigned long int *morton_codes,
          unsigned long int *sorted_morton_codes,
          unsigned int *permutation_vector,
          unsigned int *index,
          int *level_record,
          int N,
          int population_threshold,
          int sft, int lv){

int BinSizes[MAXBINS] = {0};
unsigned int *tmp_ptr;
unsigned long int *tmp_code;

//thread management
extern int NUM_THREADS;
extern int activeThreads;
int startNewThreads = 0;
//if there's space for new threads, set flag to 1 and add the new threads to the count
//once calling is over, decrement count

level_record[0] = lv; // record the level of the node

if(N<=population_threshold || sft < 0) { // Base case. The node is a leaf
    memcpy(permutation_vector, index, N*sizeof(unsigned int)); // Copy the pernutation vector
    memcpy(sorted_morton_codes, morton_codes, N*sizeof(unsigned long int)); // Copy the Morton codes

    return;
}
else{

    // Find which child each point belongs to
    int j = 0;
    for(j=0; j<N; j++){
        unsigned int ii = (morton_codes[j]>>sft) & 0x07;
        BinSizes[ii]++;
    }


    // scan prefix (must change this code)
    int offset = 0, i = 0;
    for(i=0; i<MAXBINS; i++){
        int ss = BinSizes[i];
        BinSizes[i] = offset;
        offset += ss;
    }

    for(j=0; j<N; j++){
        unsigned int ii = (morton_codes[j]>>sft) & 0x07;
        permutation_vector[BinSizes[ii]] = index[j];
        sorted_morton_codes[BinSizes[ii]] = morton_codes[j];
        BinSizes[ii]++;
    }

    //swap the index pointers
    swap(&index, &permutation_vector);

    //swap the code pointers
    swap_long(&morton_codes, &sorted_morton_codes);
    int offsets[MAXBINS];
    offset = 0;
    offsets[0] = 0;
    for(i = 0; i<MAXBINS-1; i++) {
        int size = BinSizes[i] - offset;
        offset +=size;
        offsets[i+1] = offset;
    }

    #pragma omp flush(activeThreads)
    //Allow creation of new threads? Only if the number has not been exceeded
    if (activeThreads < NUM_THREADS && 0 == startNewThreads){
        startNewThreads = 1; //allow creation of more threads
    }
    if (activeThreads > NUM_THREADS && 1 == startNewThreads){
        startNewThreads = 0; //stop creating more threads
    }


    #pragma omp flush(startNewThreads)
    omp_set_nested(startNewThreads);
    /* Call the function recursively to split the lower levels */
    #pragma omp parallel num_threads(NUM_THREADS)
    {
        #pragma omp for private(i) nowait\
        schedule(static)
        for(i=0; i<MAXBINS; i++){
            if (omp_get_nested()){
                #pragma omp atomic
                activeThreads ++; //account for new thread
                #pragma omp flush(activeThreads)
            }
            truncated_radix_sort(&morton_codes[offsets[i]],
                    &sorted_morton_codes[offsets[i]],
                    &permutation_vector[offsets[i]],
                    &index[offsets[i]], &level_record[offsets[i]],
                    sizes[i],
                    population_threshold,
                    sft-3, lv+1);
            if(omp_get_nested()){
                #pragma omp atomic
                activeThreads--;  //thread about to terminate
                #pragma omp flush(activeThreads)
            }
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

你的实验与理论相吻合。您可能想了解Amdahl's law。基本上,根据这个定律,您将获得与较低线程数大致相同的性能。在现实生活中,它会在某个时刻开始减少(你有太多的线程)。如果你有数以千计的线程,你可能会发现它。