OpenMP argmin reduction for multiple values

ε祈祈猫儿з 提交于 2019-12-06 11:43:05

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);
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!