source: CIVL/examples/openMP/dotProduct_critical.c@ fbc5eb8

1.23 2.0 main test-branch
Last change on this file since fbc5eb8 was c0d806d, checked in by Manchun Zheng <zmanchun@…>, 12 years ago

added the translation of two different implementations of dot product in OpenMP.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@717 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 1.1 KB
Line 
1/*
2 OpenMP example program which computes the dot product of two arrays a and b
3 (that is sum(a[i]*b[i]) ) using explicit synchronization with a critical region.
4 Compile with gcc -O3 -fopenmp omp_critical.c -o omp_critical
5*/
6// Online source: http://users.abo.fi/mats/PP2012/examples/OpenMP/omp_critical.c
7
8#include <omp.h>
9#include <stdio.h>
10#include <stdlib.h>
11
12#define N 8
13
14int main (int argc, char *argv[]) {
15
16 double a[N], b[N];
17 double localsum, sum = 0.0;
18 int i, tid, nthreads;
19
20#pragma omp parallel shared(a,b,sum) private(i, localsum)
21 {
22 /* Get thread number */
23 tid = omp_get_thread_num();
24
25 /* Only master thread does this */
26#pragma omp master
27 {
28 nthreads = omp_get_num_threads();
29 printf("Number of threads = %d\n", nthreads);
30 }
31
32 /* Initialization */
33#pragma omp for
34 for (i=0; i < N; i++)
35 a[i] = b[i] = (double)i;
36
37 localsum = 0.0;
38
39 /* Compute the local sums of all products */
40#pragma omp for
41 for (i=0; i < N; i++)
42 localsum = localsum + (a[i] * b[i]);
43
44#pragma omp critical
45 sum = sum+localsum;
46
47 } /* End of parallel region */
48
49 printf(" Sum = %2.1f\n",sum);
50 exit(0);
51}
Note: See TracBrowser for help on using the repository browser.