SHA256
add omp parrallel sum with critical section
This commit is contained in:
+19
-1
@@ -4,7 +4,6 @@
|
|||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <iostream>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <new>
|
#include <new>
|
||||||
#include <omp.h>
|
#include <omp.h>
|
||||||
@@ -179,6 +178,25 @@ unsigned sum_cpp_mtx(const unsigned *V, size_t n) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned sum_omp_mtx(const unsigned *V, size_t n) {
|
||||||
|
unsigned T = get_num_threads();
|
||||||
|
unsigned result = 0;
|
||||||
|
#pragma omp parallel shared(result)
|
||||||
|
{
|
||||||
|
unsigned t = omp_get_thread_num();
|
||||||
|
auto [beg, end] = get_range(n, t, T);
|
||||||
|
unsigned v = 0;
|
||||||
|
for (size_t i = beg; i < end; i++) {
|
||||||
|
v += V[i];
|
||||||
|
}
|
||||||
|
#pragma omp critical
|
||||||
|
{
|
||||||
|
result = ++v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
std::size_t n = 1u << 28;
|
std::size_t n = 1u << 28;
|
||||||
auto data = std::make_unique<unsigned[]>(n);
|
auto data = std::make_unique<unsigned[]>(n);
|
||||||
|
|||||||
Reference in New Issue
Block a user