forked from wncc/Hello-Foss-PyThread.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrong_sum.cpp
59 lines (43 loc) · 1.44 KB
/
wrong_sum.cpp
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "parallel_sum.h"
#include <omp.h>
#include <iostream>
int parallel_sum(const int arr[], int size, int step_size) {
int sum = 0;
#pragma omp parallel
{
for (int i = 0; i < size; i += step_size) {
int start = i, end = i + step_size - 1;
// Ensure end index is within bounds
if (end >= size) end = size - 1;
std::cout << "Thread computing Sum(" << start << "," << end << ") from "
<< omp_get_thread_num() << " of " << omp_get_num_threads() << std::endl;
#pragma omp task
{
int psum = 0;
std::cout << "Task computing Sum(" << start << "," << end << ") from "
<< omp_get_thread_num() << " of " << omp_get_num_threads() << std::endl;
for (int j = start; j <= end; j++) {
psum += arr[j];
}
#pragma omp critical
{
sum += psum;
}
}
}
}
return sum;
}
int main(int argc, char* argv[]) {
#define ARR_SIZE 600
#define STEP_SIZE 100
int a[ARR_SIZE];
// Initialize array with values
for (int i = 0; i < ARR_SIZE; i++) {
a[i] = 1;
}
// Call the parallel sum function
int sum = parallel_sum(a, ARR_SIZE, STEP_SIZE);
std::cout << "Sum = " << sum << std::endl;
return 0;
}