forked from vlvovch/PHYS6350-ComputationalPhysics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel_sum_openmp.cpp
More file actions
42 lines (32 loc) · 1.3 KB
/
Copy pathparallel_sum_openmp.cpp
File metadata and controls
42 lines (32 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// To compile
// On Unix: g++ -fopenmp parallel_sum_openmp.cpp -o parallel_sum_openmp
// On Mac: clang++ -Xpreprocessor -std=c++11 -Xclang -fopenmp -L/opt/homebrew/opt/libomp/lib -I/opt/homebrew/opt/libomp/include -lomp parallel_sum_openmp.cpp -o parallel_sum_openmp
#include <iostream>
#include <omp.h>
#include <vector>
int main(int argc, char* argv[]) {
if (argc < 3) {
std::cerr << "Usage: " << argv[0] << " <repetitions_size> <num_threads>" << std::endl;
return 1;
}
// Parse command-line arguments for array size and number of threads
int repetitions_size = std::stoi(argv[1]);
int num_threads = std::stoi(argv[2]);
const long long N = 10000000;
// Initialize the array with sample data
std::vector<long long> arr(N, 5);
// Declare a variable to store the sum
long long sum = 0;
// Set the number of threads to use for the parallel region
omp_set_num_threads(num_threads);
for (int i = 0; i < repetitions_size; ++i) {
// Compute the sum in parallel using OpenMP
#pragma omp parallel for reduction(+:sum)
for (int i = 0; i < arr.size(); i++) {
sum += arr[i];
}
}
// Print the result
std::cout << "Sum of the array " << repetitions_size << " times: " << sum << std::endl;
return 0;
}