gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:22:46 25 4
gpt4 key购买 nike

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

例如,从一个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”应按线程数顺序收集“hugeList”中满足条件(<10)的元素。

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

最佳答案

您的代码中有几个问题。

1/omp pragma 应该是并行的 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;
}

关于c - 如何使用 OpenMP 将每个线程的输出返回到数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54287191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com