first lab done

This commit is contained in:
2026-10-15 11:54:26 +10:00
commit a4f383238e
3 changed files with 47 additions and 0 deletions
Executable
BIN
View File
Binary file not shown.
Executable
BIN
View File
Binary file not shown.
+47
View File
@@ -0,0 +1,47 @@
#include <cstdio>
#include <stdio.h>
#include <omp.h>
#include <memory>
unsigned sum(const unsigned* V, std::size_t n){
unsigned acc=0;
for(int i =0;i <n; i++){
acc+=V[i];
}
return acc;
}
unsigned sum_par(const unsigned* V, std::size_t n){
unsigned acc=0;
#pragma omp parallel for reduction(+ : acc)
for(size_t i =0;i <n; i++){
acc+=V[i];
}
return acc;
}
int main(int argc, char** argv){
std::size_t n = 1u<<28;
auto data = std::make_unique<unsigned[]>(n);
#pragma omp parallel for
for(std::size_t i=0; i<n;++i){
data[i]=i;
}
{
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);
}
{
auto t0 = omp_get_wtime();
auto r = sum(data.get(),n);
auto t1 = omp_get_wtime();
printf("res: :0x%x\n It took %g s \n",r,t1-t0);
}
return 0;
}