SHA256
add omp threaded reduction
This commit is contained in:
+23
-1
@@ -2,6 +2,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <omp.h>
|
#include <omp.h>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
unsigned sum(const unsigned* V, std::size_t n){
|
unsigned sum(const unsigned* V, std::size_t n){
|
||||||
unsigned acc=0;
|
unsigned acc=0;
|
||||||
@@ -20,6 +21,27 @@ unsigned sum_par(const unsigned* V, std::size_t n){
|
|||||||
return acc;
|
return acc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned sum_par_rr(const unsigned* V, std::size_t n){
|
||||||
|
unsigned acc=0;
|
||||||
|
unsigned* partial_sums = (unsigned*)calloc(omp_get_num_procs(),sizeof(unsigned));
|
||||||
|
|
||||||
|
#pragma omp parallel
|
||||||
|
{
|
||||||
|
int T = omp_get_num_threads();
|
||||||
|
int t = omp_get_thread_num();
|
||||||
|
|
||||||
|
for(size_t i =0;i <n; i++){
|
||||||
|
partial_sums[t]+=V[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = 0;i<omp_get_num_procs();i++){
|
||||||
|
acc+=partial_sums[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return acc;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv){
|
int main(int argc, char** argv){
|
||||||
std::size_t n = 1u<<28;
|
std::size_t n = 1u<<28;
|
||||||
@@ -38,7 +60,7 @@ int main(int argc, char** argv){
|
|||||||
|
|
||||||
{
|
{
|
||||||
auto t0 = omp_get_wtime();
|
auto t0 = omp_get_wtime();
|
||||||
auto r = sum(data.get(),n);
|
auto r = sum_par_rr(data.get(),n);
|
||||||
auto t1 = omp_get_wtime();
|
auto t1 = omp_get_wtime();
|
||||||
printf("res: :0x%x\n It took %g s \n",r,t1-t0);
|
printf("res: :0x%x\n It took %g s \n",r,t1-t0);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user