首页 > 解决方案 > 如何使用 OpenMP 将每个线程的输出返回到数组中?

问题描述

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

例如,从一个 int 数组中选择所有 < 10 的数字并将它们放入一个新数组中。

伪代码(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”应该按顺序和线程数收集满足条件(<10)的“hugeList”中的元素。

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

标签: cmultithreadingopenmp

解决方案


您的代码中有几个问题。

1/ omp pragma 应该是并行的,如果你想并行化 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;
}

推荐阅读