Last active
July 11, 2020 05:23
-
-
Save crcrpar/223e304762b04a38f9b6be7a76b77273 to your computer and use it in GitHub Desktop.
OpenMP reduction with custom types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ref: https://stackoverflow.com/a/39990387 | |
#include <iostream> | |
#include <vector> | |
#include <omp.h> | |
struct Value { | |
Value(void) : x(-1) {} | |
Value(float x) : x(x) {} | |
float x; | |
}; | |
const Value &max(const Value &a, const Value &b) { return a.x > b.x ? a : b; } | |
auto prepare(void) -> std::vector<std::vector<Value>> { | |
std::vector<std::vector<Value>> x; | |
for (size_t i = 0; i < 10; i++) { | |
x.push_back(std::vector<Value>(10, Value(0.0))); | |
} | |
return x; | |
} | |
auto main() -> int { | |
auto x = prepare(); | |
Value max_value(-1); | |
#pragma omp declare reduction(maxVal:Value : omp_out = max(omp_out, omp_in)) | |
#pragma omp parallel for collapse(2) reduction(maxVal : max_value) | |
for (size_t i = 0; i < 10; i++) { | |
for (size_t j = 0; j < 100; j++) { | |
max_value.x = i * 100 + j; | |
} | |
} | |
std::cout << max_value.x << std::endl; | |
} | |
#if 0 | |
~/Documents/misc/omp_example | |
❯ /usr/local/bin/g++-9 baka_para.cpp -std=c++14 -fopenmp -lpthread -O0 -g | |
~/Documents/misc/omp_example | |
❯ ./a.out | |
999 | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// If your OpenMP <= 3.1 | |
// https://stackoverflow.com/a/39993717 | |
#include <iostream> | |
#include <vector> | |
#include <omp.h> | |
struct Value { | |
Value(void) : x(-1) {} | |
Value(float x) : x(x) {} | |
float x; | |
}; | |
const Value &max(const Value &a, const Value &b) { return a.x > b.x ? a : b; } | |
auto prepare(void) -> std::vector<std::vector<Value>> { | |
std::vector<std::vector<Value>> x; | |
for (size_t i = 0; i < 10; i++) { | |
x.push_back(std::vector<Value>(10, Value(0.0))); | |
} | |
return x; | |
} | |
auto main() -> int { | |
auto x = prepare(); | |
Value max_value(-1); | |
#pragma omp parallel | |
{ | |
Value thread_max_value; | |
#pragma omp for collapse(2) | |
for (size_t i = 0; i < 10; i++) { | |
for (size_t j = 0; j < 100; j++) { | |
if (i * 100 + j > thread_max_value.x) { | |
thread_max_value.x = i * 100 + j; | |
} | |
} | |
} | |
#pragma omp critical | |
{ | |
if (thread_max_value.x > max_value.x) { | |
max_value.x = thread_max_value.x; | |
} | |
} | |
} | |
std::cout << max_value.x << std::endl; | |
} | |
#if 0 | |
~/Documents/misc/omp_example | |
❯ /usr/local/bin/g++-9 baka_para_oldOMP.cpp -std=c++14 -fopenmp -lpthread -O0 -g | |
~/Documents/misc/omp_example | |
❯ ./a.out | |
999 | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On mac, GCC9