Skip to content

Commit

Permalink
Added openmp code for prefix_sum with pretty print
Browse files Browse the repository at this point in the history
  • Loading branch information
pesricha committed Nov 8, 2024
1 parent bb96950 commit 85ef4d9
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 7 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
"streambuf": "cpp",
"typeinfo": "cpp",
"iomanip": "cpp",
"sstream": "cpp"
"sstream": "cpp",
"unordered_set": "cpp",
"fstream": "cpp"
}
}
12 changes: 12 additions & 0 deletions openmp/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CXX = g++
CXXFLAGS = -fopenmp
TARGET = main
SRCS = main.cpp

all: $(TARGET)

$(TARGET): $(SRCS)
$(CXX) $(CXXFLAGS) -o $(TARGET) $(SRCS)

clean:
rm -f $(TARGET)
98 changes: 98 additions & 0 deletions openmp/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include<iostream>
#include<vector>
#include<chrono>
#include<iomanip>
#include<omp.h>

typedef std::vector<int> vec;

const int N_VALUES[] = {10000, 20000, 30000};
const int THREAD_VALUE[] = {2, 4, 8, 16, 32, 64};

void printVector1D(vec& A)
{
for (auto elem: A) std::cout << elem << " ";
std::cout << std::endl;
}

void prefixSumOMP(vec& A, vec &B, int n, int thread_count)
{
vec offsets(thread_count, 0);

#pragma omp parallel num_threads(thread_count)
{
int threadId = omp_get_thread_num();
int start = threadId * (n / thread_count);
int end = (threadId + 1) * (n / thread_count);

B[start] = A[start];

for (int i = start + 1; i < end; i++)
{
B[i] = A[i] + B[i-1];
}

#pragma omp barrier

offsets[threadId] = (threadId > 0) ? B[start - 1] : 0;

#pragma omp barrier

#pragma omp single
{
for (int i = 1; i < thread_count; i++)
{
offsets[i] = offsets[i] + offsets[i-1];
}
}

#pragma omp barrier

#pragma omp simd
for(int i = start; i < end ; i++)
{
B[i] = B[i] + offsets[threadId];
}

#pragma omp barrier

}

}

int main(int argc, char** argv)
{
std::cout << "=============================================================================" << std::endl;

for(auto thread_count: THREAD_VALUE)
{
for (const auto N: N_VALUES){

std::chrono::duration<double, std::milli> total_time = std::chrono::duration<double>(0.0);
vec A(N);
vec B(N, 0);

// Initializing A to avoid overflow.
for (int i = 0 ; i < N; i++) A[i] = std::rand() % (INT32_MAX/N);

for (int i = 0; i < 5; i++)
{
auto start = std::chrono::high_resolution_clock::now();
prefixSumOMP(A, B, N, thread_count);
auto stop = std::chrono::high_resolution_clock::now();

std::chrono::duration<double, std::milli> time = stop - start;
total_time += time;
}

total_time = total_time / 5;

// std::cout << std::scientific;
std::cout << "Threads: " << thread_count << " |" << "Value: " << B[N-1] <<" | Average time: " << total_time.count() <<" ms" << std::endl;
}

std::cout << "=============================================================================" << std::endl;
}

return 0;
}
16 changes: 10 additions & 6 deletions prefix_sum/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,28 @@ int main()
{
for (const auto N: N_VALUES){

std::chrono::duration<double> total_time = std::chrono::duration<double>(0.0);
vec A(N, 1);
vec B(N);
std::chrono::duration<double,std::milli> total_time = std::chrono::duration<double>(0.0);
vec A(N);
vec B(N, 0);

// Initializing A to avoid overflow.
for (int i = 0 ; i < N; i++) A[i] = std::rand() % (INT32_MAX/N);


for (int i = 0; i < 5; i++)
{
auto start = std::chrono::high_resolution_clock::now();
prefixSum(A, B);
auto stop = std::chrono::high_resolution_clock::now();

std::chrono::duration<double> time = stop - start;
std::chrono::duration<double, std::milli> time = stop - start;
total_time += time;
}

total_time = total_time / 5;

std::cout << std::scientific;
std::cout << "Value: " << B[N-1] <<" | Average time: " << total_time.count() <<" seconds" << std::endl;
// std::cout << std::scientific;
std::cout << "Value: " << B[N-1] <<" | Average time: " << total_time.count() <<" ms" << std::endl;
}

return 0;
Expand Down

0 comments on commit 85ef4d9

Please sign in to comment.