SHA256
add threaded sum
This commit is contained in:
+10
-1
@@ -3,5 +3,14 @@
|
|||||||
// For more documentation on how to configure debug tasks,
|
// For more documentation on how to configure debug tasks,
|
||||||
// see: https://zed.dev/docs/debugger
|
// 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"]
|
||||||
|
}
|
||||||
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
+89
-45
@@ -1,105 +1,149 @@
|
|||||||
|
#include <cstddef>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <stdio.h>
|
|
||||||
#include <omp.h>
|
|
||||||
#include <memory>
|
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <memory>
|
||||||
|
#include <new>
|
||||||
|
#include <omp.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <thread>
|
||||||
|
#include <vector>
|
||||||
#define CHACHE_LINE 64
|
#define CHACHE_LINE 64
|
||||||
|
|
||||||
unsigned sum(const unsigned* V, std::size_t n){
|
unsigned sum(const unsigned *V, std::size_t n) {
|
||||||
unsigned acc=0;
|
unsigned acc = 0;
|
||||||
for(int i =0;i <n; i++){
|
for (int i = 0; i < n; i++) {
|
||||||
acc+=V[i];
|
acc += V[i];
|
||||||
}
|
}
|
||||||
return acc;
|
return acc;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned sum_par(const unsigned* V, std::size_t n){
|
unsigned sum_par(const unsigned *V, std::size_t n) {
|
||||||
unsigned acc=0;
|
unsigned acc = 0;
|
||||||
#pragma omp parallel for reduction(+ : acc)
|
#pragma omp parallel for reduction(+ : acc)
|
||||||
for(size_t i =0;i <n; i++){
|
for (size_t i = 0; i < n; i++) {
|
||||||
acc+=V[i];
|
acc += V[i];
|
||||||
}
|
}
|
||||||
return acc;
|
return acc;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned sum_par_rr(const unsigned* V, std::size_t n){
|
unsigned sum_par_rr(const unsigned *V, std::size_t n) {
|
||||||
unsigned acc=0;
|
unsigned acc = 0;
|
||||||
unsigned* partial_sums = (unsigned*)malloc(omp_get_num_procs()*CHACHE_LINE);
|
unsigned *partial_sums =
|
||||||
#pragma omp parallel
|
(unsigned *)malloc(omp_get_num_procs() * CHACHE_LINE);
|
||||||
|
#pragma omp parallel
|
||||||
{
|
{
|
||||||
int T = omp_get_num_threads();
|
int T = omp_get_num_threads();
|
||||||
int t = omp_get_thread_num();
|
int t = omp_get_thread_num();
|
||||||
//partial_sums[t*CHACHE_LINE/sizeof(unsigned)]=0;
|
// partial_sums[t*CHACHE_LINE/sizeof(unsigned)]=0;
|
||||||
|
|
||||||
for(size_t i = t;i <n; i+=T){
|
for (size_t i = t; i < n; i += T) {
|
||||||
partial_sums[t*CHACHE_LINE/sizeof(unsigned)]+=V[i];
|
partial_sums[t * CHACHE_LINE / sizeof(unsigned)] += V[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for(int i = 0;i<omp_get_num_procs();i++){
|
for (int i = 0; i < omp_get_num_procs(); i++) {
|
||||||
acc+=partial_sums[i*CHACHE_LINE/sizeof(unsigned)];
|
acc += partial_sums[i * CHACHE_LINE / sizeof(unsigned)];
|
||||||
}
|
}
|
||||||
|
|
||||||
return acc;
|
return acc;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned sum_par_rr_no_cache(const unsigned* V, std::size_t n){
|
unsigned sum_loc_cpp(const unsigned *V, std::size_t n) {
|
||||||
unsigned acc=0;
|
unsigned T = std::thread::hardware_concurrency();
|
||||||
unsigned* partial_sums = (unsigned*)calloc(omp_get_num_procs(),sizeof(unsigned));
|
struct alignas(std::hardware_destructive_interference_size) p_t {
|
||||||
#pragma omp parallel
|
unsigned v;
|
||||||
|
};
|
||||||
|
auto p = std::make_unique<p_t[]>(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];
|
||||||
|
}
|
||||||
|
p[t].v = sum;
|
||||||
|
};
|
||||||
|
std::vector <std::thread> workers(T);
|
||||||
|
for(unsigned t=0;t<T; t++){
|
||||||
|
workers[t] = std::thread(worker_proc,t);
|
||||||
|
}
|
||||||
|
for(auto & w : workers){
|
||||||
|
w.join();
|
||||||
|
}
|
||||||
|
unsigned result =0;
|
||||||
|
for (size_t i =0;i<T;i++){
|
||||||
|
result += p[i].v;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned sum_par_rr_no_cache(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_num_threads();
|
||||||
int t = omp_get_thread_num();
|
int t = omp_get_thread_num();
|
||||||
partial_sums[t*CHACHE_LINE/sizeof(unsigned)]=0;
|
partial_sums[t * CHACHE_LINE / sizeof(unsigned)] = 0;
|
||||||
|
|
||||||
for(size_t i = t;i <n; i+=T){
|
for (size_t i = t; i < n; i += T) {
|
||||||
partial_sums[t]+=V[i];
|
partial_sums[t] += V[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for(int i = 0;i<omp_get_num_procs();i++){
|
for (int i = 0; i < omp_get_num_procs(); i++) {
|
||||||
acc+=partial_sums[i];
|
acc += partial_sums[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
return acc;
|
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 << 29;
|
||||||
auto data = std::make_unique<unsigned[]>(n);
|
auto data = std::make_unique<unsigned[]>(n);
|
||||||
#pragma omp parallel for
|
#pragma omp parallel for
|
||||||
for(std::size_t i=0; i<n;++i){
|
for (std::size_t i = 0; i < n; ++i) {
|
||||||
data[i]=i;
|
data[i] = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
auto t0 = omp_get_wtime();
|
auto t0 = omp_get_wtime();
|
||||||
auto r = sum(data.get(),n);
|
auto r = sum(data.get(), n);
|
||||||
auto t1 = omp_get_wtime();
|
auto t1 = omp_get_wtime();
|
||||||
printf("sum: :0x%x\n It took %g s \n",r,t1-t0);
|
printf("sum: :0x%x\n It took %g s \n", r, t1 - t0);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
auto t0 = omp_get_wtime();
|
auto t0 = omp_get_wtime();
|
||||||
auto r = sum_par(data.get(),n);
|
auto r = sum_par(data.get(), n);
|
||||||
auto t1 = omp_get_wtime();
|
auto t1 = omp_get_wtime();
|
||||||
printf("sum_par: :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 t0 = omp_get_wtime();
|
||||||
auto r = sum_par_rr(data.get(),n);
|
auto r = sum_par_rr(data.get(), n);
|
||||||
auto t1 = omp_get_wtime();
|
auto t1 = omp_get_wtime();
|
||||||
printf("sum_par_rr: :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);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
auto t0 = omp_get_wtime();
|
auto t0 = omp_get_wtime();
|
||||||
auto r = sum_par_rr_no_cache(data.get(),n);
|
auto r = sum_par_rr_no_cache(data.get(), n);
|
||||||
auto t1 = omp_get_wtime();
|
auto t1 = omp_get_wtime();
|
||||||
printf("sum_par_rr_no_cache: :0x%x\n It took %g s \n",r,t1-t0);
|
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;
|
return 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user