c - OMP 线程数多于内核,性能仍相同

标签 c multithreading openmp

我有一部分使用 OpenMP 的串行程序。当我用 8 个线程执行它时(我的计算机可以使用 8 个线程),当我用 16 或 32 或 64 等执行它时,时间相同。这正常吗?因为我认为当我创建的线程多于核心时,程序会变慢。如果您想检查的话,这是代码。它运行正确!在其他文件的主文件中,有设定的线程数。

       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)
            }
        }
    }
}

}

最佳答案

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

关于c - OMP 线程数多于内核,性能仍相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40808018/

相关文章:

c - 如何用C程序获取当前位置的字符?

c - 无法在 fork 进程中设置 OpenMP 线程关联

c - 使用多线程时低于预期的加速

pthreads - 将 openmp 编译成 pthreads C 代码

c - C中的空指针

c - 使用日期和时间函数

c - 无法理解此随机数生成器代码

c++ - VS2012 中的并发分析 - Reader Writer Locks

iphone - 核心文本: counting pages in background thread

ios - Alamofire 线程甚至可以在屏幕上的另一个 View Controller 上工作吗? ios