source: CIVL/examples/omp/dotProduct_critical.c

main
Last change on this file was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

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

  • Property mode set to 100644
File size: 1.2 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// permission obtained
8
9#include <omp.h>
10#include <stdio.h>
11#include <stdlib.h>
12
13#ifdef _CIVL
14#define N 10
15#else
16#define N 100
17#endif
18
19int main (int argc, char *argv[]) {
20
21 double a[N], b[N];
22 double localsum, sum = 0.0;
23 int i, tid, nthreads;
24
25#pragma omp parallel shared(a,b,sum) private(i, localsum, tid)
26 {
27 /* Get thread number */
28 tid = omp_get_thread_num();
29
30 /* Only master thread does this */
31#pragma omp master
32 {
33 nthreads = omp_get_num_threads();
34 printf("Number of threads = %d\n", nthreads);
35 }
36
37 /* Initialization */
38#pragma omp for
39 for (i=0; i < N; i++)
40 a[i] = b[i] = (double)i;
41
42 localsum = 0.0;
43
44 /* Compute the local sums of all products */
45#pragma omp for
46 for (i=0; i < N; i++)
47 localsum = localsum + (a[i] * b[i]);
48
49#pragma omp critical
50 sum = sum+localsum;
51
52 } /* End of parallel region */
53
54 printf(" Sum = %2.1f\n",sum);
55 exit(0);
56}
Note: See TracBrowser for help on using the repository browser.