-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaxelementreduction.c
63 lines (47 loc) · 1.49 KB
/
maxelementreduction.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* Find maximum element among K matrices
Author: R Mukesh (CED15I002)
*/
#define M 1000 // the number of rows in matrices
#define N 1000 // the number of columns in matrices
#define K 100 // the number of matrices to find max among
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <time.h>
int const NUM_THREADS[] = {1, 2, 4, 6, 8, 10, 12, 16, 20, 24, 28, 32};
int main(void)
{
int k, i, j, num_threads_index;
clock_t start_clock, end_clock;
// initialise K matrices
int ***matrices;
matrices = (int ***)malloc(sizeof(int **)*K);
for(k=0; k<K; ++k)
{
*(matrices+k) = (int **)malloc(sizeof(int)*M);
for(i=0; i<M; ++i)
{
*(*(matrices+k) + i) = (int *)malloc(sizeof(int)*N);
for(j=0; j<N; ++j)
*(*(*(matrices+k) + i)+j) = rand();
}
}
// Finding max among K matrices using reduction directive
int maximum = INT_MIN;
for(num_threads_index=0; num_threads_index<sizeof(NUM_THREADS)/sizeof(int); ++num_threads_index)
{
start_clock = clock();
#pragma omp parallel for default(none) private(k, i, j) shared(matrices) reduction(max:maximum) num_threads(NUM_THREADS[num_threads_index])
for(k=0; k<K; ++k)
{
for(i=0; i<M; ++i)
for(j=0; j<N; ++j)
if( *(*(*(matrices+k) + i)+j) > maximum)
maximum = *(*(*(matrices+k) + i)+j);
}
end_clock = clock();
printf("Num Threads = %d, Time taken = %lf\n", NUM_THREADS[num_threads_index], (end_clock - start_clock)*1.00/CLOCKS_PER_SEC);
}
printf("Maximum element = %d\n", maximum);
return 0;
}