improvemts

This commit is contained in:
2026-10-15 13:12:22 +10:00
parent 14b15c7b3b
commit a2623ba755
3 changed files with 16 additions and 7 deletions
+15 -7
View File
@@ -3,6 +3,7 @@
#include <omp.h>
#include <memory>
#include <cstdlib>
#define CHACHE_LINE 64
unsigned sum(const unsigned* V, std::size_t n){
unsigned acc=0;
@@ -23,20 +24,20 @@ unsigned sum_par(const unsigned* V, std::size_t n){
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));
unsigned* partial_sums = (unsigned*)malloc(omp_get_num_procs()*CHACHE_LINE);
#pragma omp parallel
{
int T = omp_get_num_threads();
int t = omp_get_thread_num();
partial_sums[t*CHACHE_LINE/sizeof(unsigned)]=0;
for(size_t i =0;i <n; i++){
partial_sums[t]+=V[i];
for(size_t i = t;i <n; i+=T){
partial_sums[t*CHACHE_LINE/sizeof(unsigned)]+=V[i];
}
}
for(int i = 0;i<omp_get_num_procs();i++){
acc+=partial_sums[i];
acc+=partial_sums[i*CHACHE_LINE/sizeof(unsigned)];
}
return acc;
@@ -51,18 +52,25 @@ int main(int argc, char** argv){
data[i]=i;
}
{
auto t0 = omp_get_wtime();
auto r = sum(data.get(),n);
auto t1 = omp_get_wtime();
printf("sum: :0x%x\n It took %g s \n",r,t1-t0);
}
{
auto t0 = omp_get_wtime();
auto r = sum_par(data.get(),n);
auto t1 = omp_get_wtime();
printf("par res: :0x%x\n It took %g s \n",r,t1-t0);
printf("sum_par: :0x%x\n It took %g s \n",r,t1-t0);
}
{
auto t0 = omp_get_wtime();
auto r = sum_par_rr(data.get(),n);
auto t1 = omp_get_wtime();
printf("res: :0x%x\n It took %g s \n",r,t1-t0);
printf("sum_par_rr: :0x%x\n It took %g s \n",r,t1-t0);
}
return 0;