-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaximum.c
94 lines (76 loc) · 2.46 KB
/
maximum.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<stdio.h>
#include<stdlib.h>
#include <omp.h>
#include<time.h>
#define SIZE 1000000
int searchMax(int *a,int threads);
int main(void)
{ int i=0, my_max, a[SIZE], threads;
double start, end;
srand(time(0));
printf("Give me the number of threads you want to use:\n");
scanf("%d", &threads);
printf("\n");
// Check processors and threads
int processors = omp_get_num_procs (); // Available processors
printf ("Array size = %d\nProcesses = %d\nProcessors = %d\n",
SIZE, threads, processors);
if (threads > processors)
{
printf
("Warning: %d threads requested, will run_omp on %d processors available\n",
threads, processors);
omp_set_num_threads (threads);
}
int max_threads = omp_get_max_threads (); // Max available threads
if (threads > max_threads) // Requested threads are more than max available
{
printf ("Error: Cannot use %d threads, only %d threads available\n",
threads, max_threads);
return 1;
}
for (i = 0; i < SIZE; i++)
a[i]=rand();
//Find maximum value
start = omp_get_wtime();
printf("Maximum value in the matrix is : %d\n", searchMax(a , threads));
end = omp_get_wtime();
printf("Elapsed = %lf\n", end - start);
printf("Success !\n");
return 0;
}
int searchMax(int *a,int threads){
int max[threads], my_max, i;
omp_set_num_threads(threads);
#pragma omp parallel
{
int id, n, begin, stop;
int length = (sizeof(max)/sizeof(int));
n = SIZE/length; // step = SIZE/number of threads.
id = omp_get_thread_num(); // id is one of 0, 1, ..., (threads -1)
begin = id * n;
if( id != (threads - 1) )
{
stop = begin + n;
}
else
{
stop = SIZE;
}
my_max = a[begin];
for (i = begin+1; i < stop; i++ )
{
if(a[i] > my_max )
my_max = a[i];
}
max[id] = my_max; // Store result in max[id]
}
// ----------------------------------------
// Post processing: Find actual maximum
// ----------------------------------------
my_max = max[0];
for (i = 1; i < threads; i++)
if ( max[i] > my_max )
my_max = max[i];
return my_max;
}