如何使用 OpenMP 将每个线程的输出返回到数组中?

Posted

技术标签:

【中文标题】如何使用 OpenMP 将每个线程的输出返回到数组中?【英文标题】:How to return each thread's output into an array using OpenMP? 【发布时间】:2019-01-21 09:45:51 【问题描述】:

我想进行一个多线程程序,其中每个线程输出一个未知数量的元素数组。

例如,从一个 int 数组中选择所有

伪代码(8个线程):

  int *hugeList = malloc(10000000);
  for (long i = 0; i < 1000000; ++i)
  
      hugeList[i] = (rand() % 100);//random integers from 0 to 99
  
  long *subList[8];//to fill each thread's result
  #pragma omp parallel
  for (long i = 0; i < 1000000; ++i)
  
     long n = 0;
     if(hugeList[i] < 10)
     
        //do something to fill "subList" properly
        subList[threadNo][n] = hugeList[i];
        n++;
     
  

数组“subList”应该按照线程号顺序收集满足条件(

我应该如何编写代码?如果有更好的方法使用 OpenMP 就可以了。

【问题讨论】:

【参考方案1】:

你的代码有几个问题。

1/ omp pragma 应该是 parallel for,如果您希望 for 循环被并行化。否则,代码将在每个线程中重复。

2/ 代码与注释不一致

  //do something to fill "subList" properly
   hugeList[i] = subList[threadNo][n];

3/ 你如何知道子列表中的元素数量?它必须返回到主线程。您可以使用数组,但要注意虚假共享。最好使用本地变量并将其写在并行部分的末尾。

4/ 未分配子列表。困难在于您不知道线程数。您可以询问 omp 最大线程数(get_omp_max_thread),并进行动态分配。如果你想要一些静态分配,也许最好是分配一个大表并计算每个线程中的实际地址。

5/ omp 代码也必须在没有 openmp 编译器的情况下工作。为此使用#ifdef _OPENMP。

这是一种(未经测试的)代码编写方式

#define HUGE 10000000
int *hugeList = (int *) malloc(HUGE);
#ifdef _OPENMP
int thread_nbr=omp_get_max_threads();
#else
int thread_nbr=1; // to ensure proper behavior in a sequential context
#endif

struct thread_results  // to hold per thread results
  int nbr; // nbr of generated results
  int *results; // actual filtered numbers. Will write in subList table
;

// could be parallelized, but rand is not thread safe. drand48 should be
for (long i = 0; i < 1000000; ++i)
  
    hugeList[i] = (rand() % 100);//random integers from 0 to 99
  

int *subList=(int *)malloc(HUGE*sizeof(int)); // table to hold thread results
   // this is more complex to have a 2D array here as max_thread and actual number of thread
   // are not known at compile time. VLA cannot be used (and array dim can be very large).
   // Concerning its size, it is possible to have ALL elements in hugeList selected and the array must be
   // dimensionned accordingly to avoid bugs.
struct thread_results* threadres=(struct thread_results *)malloc(thread_nbr*sizeof(struct thread_results));

#pragma omp parallel

// first declare and initialize thread vars
#ifdef _OPENMP
  int thread_id = omp_get_thread_num() ; // hold thread id
  int thread_nbr = omp_get_num_threads() ; // hold actual nbr of threads
#else
  // to ensure proper serial behavior
  int thread_id = 0;
  int thread_nbr = 1;
#endif

  struct thread_results *res=threadres+thread_id;
  res->nbr=0;
  // compute address in subList table
  res->results=subList+(HUGE/thread_nbr)*thread_id;
  int * res_ptr=res->results; // local pointer. Each thread points to independent part of subList table

  int n=0; // number of results. We want one per thread to only have local updates.
  #pragma omp for
  for (long i = 0; i < 1000000; ++i)
  

     if(hugeList[i] < 10)
     
      //do something to fill "subList" properly
       res_ptr[n]=hugeList[i];
       n++;
     
  
  res->nbr=n;

【讨论】:

感谢您的快速回复。 1. 从“#pragma...”,我不知道怎么写。 2. 更正。 3.“subList”是一个二维数组。一维的长度是maxThreadNo。在这个节目中,没有。较大尺寸的元素约为。 1,000,000/10/maxThreadNo.4。 4.好的 5.好的。在您的代码中, res_ptr[n]=hugeList[i];导致“分段错误(核心转储)”。可能 res_ptr 没有正确分配? 更多问题: 1.在“...omp for”部分,n在每个线程中都有不同的副本,并且res_ptr的任何元素都可以被任何线程写入。它会给出正确的结果吗? 2. 我想要二维数组作为输出,或者 maxThreadNo 个一维数组。这似乎很容易,但我不知道它的功能。 *results 将此二维数组保存为长组合的一维数组。有没有更好的办法? 我已经更新了答案并添加了解释性的 cmets。 Segfault 问题出现在 subList 中线程指针的指针计算中。使用 2D 数组存储结果要复杂得多,因为您在编译时不知道线程数。但它可以通过分配来实现。 我把“#pragma omp parallel”改成了“#pragma omp for reduction(+:n)”,否则subList和n都不能复现。 (我不知道“reduction”如何影响 subList...如果没有“reduction”参数,则 subList 元素大多为 0。)我将上传一个新版本的代码,该版本可根据您的重现。【参考方案2】:

根据@Alain Merigot 的回答更新了完整代码 我测试了以下代码;它是可重现的(包括存在和不存在#pragma 参数)。 但是,只有 subList 的前面元素是正确的,其余的都是空的。 (文件名.c)

#include <stdio.h>
#include <time.h>
#include <omp.h>
#include <stdlib.h>
#include <math.h>
#define HUGE 10000000
#define DELAY 1000 //depends on your CPU power

//use global variables to store desired results, otherwise can't be obtain outside "pragma"
int n = 0;// number of results. We want one per thread to only have local updates.
double *subList;// table to hold thread results

int main()

    double *hugeList = (double *)malloc(HUGE);
#ifdef _OPENMP
    int thread_nbr = omp_get_max_threads();
#else
    int thread_nbr = 1; // to ensure proper behavior in a sequential context
#endif

    struct thread_results
                        // to hold per thread results
        int nbr;         // nbr of generated results
        double *results; // actual filtered numbers. Will write in subList table
    ;

    // could be parallelized, but rand is not thread safe. drand48 should be
    for (long i = 0; i < 1000000; ++i)
    
        hugeList[i] = sin(i); //fixed array content to test reproducibility
    

    subList = (double *)malloc(HUGE * sizeof(double)); // table to hold thread results
                                                        // this is more complex to have a 2D array here as max_thread and actual number of thread
                                                        // are not known at compile time. VLA cannot be used (and array dim can be very large).
                                                        // Concerning its size, it is possible to have ALL elements in hugeList selected and the array must be
                                                        // dimensionned accordingly to avoid bugs.
    struct thread_results *threadres = (struct thread_results *)malloc(thread_nbr * sizeof(struct thread_results));

#pragma omp parallel
    
// first declare and initialize thread vars
#ifdef _OPENMP
        int thread_id = omp_get_thread_num();   // hold thread id
        int thread_nbr = omp_get_num_threads(); // hold actual nbr of threads
#else
        // to ensure proper serial behavior
        int thread_id = 0;
        int thread_nbr = 1;
#endif

        struct thread_results *res = threadres + thread_id;
        res->nbr = 0;
        // compute address in subList table
        res->results = subList + (HUGE / thread_nbr) * thread_id;
        double *res_ptr = res->results; // local pointer. Each thread points to independent part of subList table

#pragma omp for reduction(+ \
                          : n)
        for (long i = 0; i < 1000000; ++i)
        
            for (int i = 0; i < DELAY; ++i)//do nothing, just waste time

            if (hugeList[i] < 0)
            
                //do something to fill "subList" properly
                res_ptr[n] = hugeList[i];
                n++;
            
        
        res->nbr = n;
    
    for (int i = 0; i < 10; ++i)
    
        printf("sublist %d: %lf\n", i, subList[i]);//show some elements of subList to check reproducibility
    
    printf("n = %d\n", n);

Linux编译:gcc -o filename filename.c -fopenmp -lm

希望能有更多关于这段代码的机制的讨论。

【讨论】:

以上是关于如何使用 OpenMP 将每个线程的输出返回到数组中?的主要内容,如果未能解决你的问题,请参考以下文章

使用openmp进行合并排序

OpenMP 子句共享与关键

C ++中OpenMP中的有序线程ID

OpenMP:如何在每个线程中利用递归函数?

OpenMP、VTune、空闲线程

我可以将多个线程分配给 OpenMP 中的代码段吗?