Reputation: 499
I want to use OpenMP to collect some data. For that, I check many candidates and collect only those that satisfy some conditions. The simplified example would be the following.
#include <bits/stdc++.h>
#include "omp.h"
using namespace std;
class DataPoint {};
DataPoint random_data_point() {
// generate random data point
}
bool test(DataPoint r) {
// do something
}
int main() {
constexpr int num_iterations = 10000;
set<DataPoint> good_points;
#pragma omp parallel for reduction(???)
for (int iter = 0; iter < num_iterations; iter++) {
DataPoint r = random_data_point();
if (test(DataPoint))
good_points.insert(r);
}
// ...
return 0;
}
The question is how to use OpenMP efficiently. My idea is that each thread collects its own data (in the example above - its own set) and then, after all of the thread finished, their sets are merged.
Note 1: my iterations are very independent (as in the example above), so OpenMP should be helpful. Note 2: in the real programme, I use more complicated data structure than set. (In fact, it is a convex hull of the data points.)
Upvotes: 0
Views: 210
Reputation: 2859
Modern OpenMP allows you to define your own reduction functions, so you should be able to achieve this that way should you want to, though, realistically, for something this simple it might be easier just to write code along these lines (untested, typed into this answer, not compiled :-))
#pragma omp parallel
{
set<Data_Point> thread_good_points;
#pragma omp for nowait
for (int iter = 0; iter < num_iterations; iter++) {
DataPoint r = random_data_point();
if (test(DataPoint))
thread_good_points.insert(r);
}
#pragma omp critical
good_points.merge(thread_good_points);
}
Upvotes: 2