-
Notifications
You must be signed in to change notification settings - Fork 0
/
openmp3.c
51 lines (41 loc) · 1006 Bytes
/
openmp3.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
/*
* Copyright (c) 2014 UFSM, UNIPAMPA.
*
* Authors: Joao Lima (UFSM) and Claudio Schepke (Unipampa).
*/
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#define MAX_N 20000000
int main(void)
{
int N = MAX_N;
double t0, t1, tdelta;
int *a, *b;
int i;
a = (int*)calloc(MAX_N, sizeof(int));
b = (int*)calloc(MAX_N, sizeof(int));
for(i = 0; i < N; i++){
a[i] = b[i] = 1;
}
t0 = omp_get_wtime();
#pragma omp parallel
{
/* variaveis locais para cada thread */
int id, i, nthreads, istart, iend;
id = omp_get_thread_num();
nthreads = omp_get_num_threads();
if(id == 0)
printf("Soma de %d numeros com %d threads\n", N, nthreads);
istart = id * N / nthreads;
iend = (id + 1) * N / nthreads;
if( id == nthreads-1 )
iend = N;
for(i = istart; i < iend; i++)
a[i] = a[i] + b[i];
}
t1 = omp_get_wtime();
tdelta = t1 - t0;
printf("Soma de %d numeros em tempo: %.4f segundos.\n", N, tdelta);
return 0;
}