main
test-branch
|
Last change
on this file since bb03188 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 | #define N 100
|
|---|
| 14 |
|
|---|
| 15 | int main (int argc, char *argv[]) {
|
|---|
| 16 |
|
|---|
| 17 | double a[N], b[N];
|
|---|
| 18 | double localsum, sum = 0.0;
|
|---|
| 19 | int i, tid, nthreads;
|
|---|
| 20 |
|
|---|
| 21 | #pragma omp parallel shared(a,b,sum) private(i, localsum)
|
|---|
| 22 | {
|
|---|
| 23 | /* Get thread number */
|
|---|
| 24 | tid = omp_get_thread_num();
|
|---|
| 25 |
|
|---|
| 26 | /* Only master thread does this */
|
|---|
| 27 | #pragma omp master
|
|---|
| 28 | {
|
|---|
| 29 | nthreads = omp_get_num_threads();
|
|---|
| 30 | printf("Number of threads = %d\n", nthreads);
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | /* Initialization */
|
|---|
| 34 | #pragma omp single
|
|---|
| 35 | for (i=0; i < N; i++)
|
|---|
| 36 | a[i] = b[i] = (double)i;
|
|---|
| 37 |
|
|---|
| 38 | localsum = 0.0;
|
|---|
| 39 |
|
|---|
| 40 | /* Compute the local sums of all products */
|
|---|
| 41 | #pragma omp single
|
|---|
| 42 | for (i=0; i < N; i++)
|
|---|
| 43 | localsum = localsum + (a[i] * b[i]);
|
|---|
| 44 |
|
|---|
| 45 | #pragma omp critical
|
|---|
| 46 | sum = sum+localsum;
|
|---|
| 47 |
|
|---|
| 48 | } /* End of parallel region */
|
|---|
| 49 |
|
|---|
| 50 | printf(" Sum = %2.1f\n",sum);
|
|---|
| 51 | exit(0);
|
|---|
| 52 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.