-
Notifications
You must be signed in to change notification settings - Fork 2
/
sum_parallel.c
47 lines (42 loc) · 1.29 KB
/
sum_parallel.c
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
#include<stdio.h>
#include<omp.h>
#define MAX 100000000
int array[MAX];
int main(){
int sum=0;
double t1,t2;
for(int i=0; i<MAX; i++){
array[i]=1;
}
t1=omp_get_wtime();
int tid, numt,i;
// In parallel, Sum is wrong – because of race condition
// More time – cache coherence
#pragma omp parallel default(shared) private(i,tid)
{
// from and to define the range of array on which a thread has to make operations.
int from, to, partial_sum=0;
tid=omp_get_thread_num();
numt=omp_get_num_threads();
// Distribute the work here.
from=(MAX/numt)*tid;
to=(MAX/numt)*(tid+1)-1;
if(tid==numt-1){
to=MAX-1;
}
printf("Thread ID %d is working on the array starting from %d till %d\n",tid,from,to);
for(i=from; i<=to; i++){
//#pragma omp critical
// this summing line is the section where only a single thread can enter the region.
// used to deal with cases of mutual exclusion. Used to safeguard operations on shared variables.
partial_sum+=array[i];
}
#pragma omp critical
// commenting inside the loop and adding all partial sums in critical region.
// in this way we can reduce synchronization.
sum+=partial_sum;
}
t2=omp_get_wtime();
printf("Sum is %d\nTime taken is %g\n",sum,t2-t1);
return 0;
}