diff --git a/.zed/debug.json b/.zed/debug.json index 0f93857..faa4064 100644 --- a/.zed/debug.json +++ b/.zed/debug.json @@ -3,5 +3,14 @@ // For more documentation on how to configure debug tasks, // see: https://zed.dev/docs/debugger [ - + { + "label": "Build Main", + "adapter": "CodeLLDB", + "program": "a.out", + "request": "launch", + "build": { + "command": "g++", + "args": ["-fopenmp", "./src/main.cpp", "-j 4"] + } + } ] diff --git a/src/main.cpp b/src/main.cpp index a89756d..760b887 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,106 +1,150 @@ +#include #include -#include -#include -#include #include +#include +#include +#include +#include +#include +#include #define CHACHE_LINE 64 -unsigned sum(const unsigned* V, std::size_t n){ - unsigned acc=0; - for(int i =0;i (T); + auto worker_proc = [V, n, T, &p](unsigned t) { + size_t s = n / T, b = n % T; + if (t < b) + b = ++s * t; + else + b += s * t; + unsigned e = b + s; + unsigned sum = 0; + for (unsigned i = b; i < e; i++) { + sum += V[i]; + // p[t].v+= V[i]; } - - for(int i = 0;i workers(T); + for(unsigned t=0;t(n); - #pragma omp parallel for - for(std::size_t i=0; i(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(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("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("sum_par_rr: :0x%x\n It took %g s \n", r, t1 - t0); + } + + { + auto t0 = omp_get_wtime(); + auto r = sum_par_rr_no_cache(data.get(), n); + auto t1 = omp_get_wtime(); + printf("sum_par_rr_no_cache: :0x%x\n It took %g s \n", r, t1 - t0); + } + { + auto t0 = omp_get_wtime(); + auto r = sum_loc_cpp(data.get(), n); + auto t1 = omp_get_wtime(); + printf("sum_loc_cpp: :0x%x\n It took %g s \n", r, t1 - t0); + } + + return 0; }