OpenMP argmin减少多个值 [英] OpenMP argmin reduction for multiple values

查看:94
本文介绍了OpenMP argmin减少多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个例程,该例程使用循环来计算给定下方的粒子表面的粒子的最小高度.此例程尝试随机位置并计算最小高度,然后返回x, y, z值,其中z是找到的最小高度.

I have a routine that uses a loop to compute the minimum height of a particle given a surface of particles beneath. This routine tries random positions and compute the minimum height and then returns the x, y, z values, where z is the minimum height found.

此例程可以与omp parallel for并行化.但是我在寻找如何获取三元组(x, y, z)时遇到困难,而不仅仅是最小的z(因为最小的z当然对应于给定的x, y坐标).实际上,我可以通过如下所示的归约运算来获得最小的z

This routine can be parallelized with omp parallel for. But I am having problems figuring out how to get the triplet (x, y, z), not just the minimum z (because the minimum z of course corresponds to a given x, y coordinates). I can actually get the smallest z by using a reduction operation as follows

double x = 0, y = 0, z = 1.0e300; // initially z is large
#pragma omp parallel for reduction(min:z)
for(int trial = 0; trial < NTRIALS; ++trial) {
    // long routine that, at the end, computes x, y, z 
    // and selects only the x, y, z corresponding to the 
    // smallest z
}

但是我无法获得相应的xy.最后,我只是得到了其中一个线程编写的随机xy.

But I cannot get the corresponding x and y. At the end I just end up with a random x and y written by one of the threads.

是否也可以获取这些值?如何?我正在考虑使用一个数组,其中每个线程存储其值x, y, zmin,然后在归约操作之后,将每个线程zmin与已归约的全局值进行比较,然后获得与选择的全局值相对应的那些值.从某种意义上说,OpenMP是否有更好的方法,所以我不需要定义此动态数组并比较浮点数?

Is it possible to get also those values? How? I was thinking on having an array where each thread stores their value of x, y, zmin and then, after the reduction operation, compare each thread zmin with the reduced global value and then get those that correspond to the choosing one. Is there a better way in the sense that OpenMP does it so I don't need to define this dynamic array and compare floats?

推荐答案


您可以使用用户定义的归约(从OpenMP 4.0开始提供)为多个值实现 argmin .为此,您必须将三元组放在一种类型中.定义便利功能会很有帮助.


You can implement an argmin for multiple values using user defined reduction (available since OpenMP 4.0). For that you have to put the triple in one type. It is helpful to define a convenience function.

struct xyz {
    double x; double y; double z;
}

struct xyz xyz_min2(struct xyz a, struct xyz b) {
    return a.z < b.z ? a : b; 
}

#pragma omp declare reduction(xyz_min: struct xyz: omp_out=xyz_min2(omp_out, omp_in))\
    initializer(omp_priv={0, 0, DBL_MAX})

struct xyz value = {0, 0, DBL_MAX};
#pragma omp parallel for reduction(xyz_min:value)
for (int trial = 0; trial < NTRIALS; ++trial) {
    struct xyz new_value = ...;
    value = xyz_min2(value, new_value);
}

这篇关于OpenMP argmin减少多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆