main
| Line | |
|---|
| 1 | /******************************************************************************
|
|---|
| 2 | * FILE: omp_orphan.c
|
|---|
| 3 | * DESCRIPTION:
|
|---|
| 4 | * OpenMP Example - Parallel region with an orphaned directive - C/C++ Version
|
|---|
| 5 | * This example demonstrates a dot product being performed by an orphaned
|
|---|
| 6 | * loop reduction construct. Scoping of the reduction variable is critical.
|
|---|
| 7 | * AUTHOR: Blaise Barney 5/99
|
|---|
| 8 | * LAST REVISED: 06/30/05
|
|---|
| 9 | ******************************************************************************/
|
|---|
| 10 | /**
|
|---|
| 11 | * This program computes the dot product of two vectors. The parallel loop
|
|---|
| 12 | * construct is orphaned - it is contained in a subroutine outside the lexical
|
|---|
| 13 | * extent of the main program's parallel region.
|
|---|
| 14 | * Online source:
|
|---|
| 15 | * https://computing.llnl.gov/tutorials/openMP/samples/C/omp_orphan.c
|
|---|
| 16 | **/
|
|---|
| 17 | #include <omp.h>
|
|---|
| 18 | #include <stdio.h>
|
|---|
| 19 | #include <stdlib.h>
|
|---|
| 20 | #define VECLEN 100
|
|---|
| 21 |
|
|---|
| 22 | float a[VECLEN], b[VECLEN], sum;
|
|---|
| 23 |
|
|---|
| 24 | float dotprod ()
|
|---|
| 25 | {
|
|---|
| 26 | int i,tid;
|
|---|
| 27 |
|
|---|
| 28 | tid = omp_get_thread_num();
|
|---|
| 29 | #pragma omp for reduction(+:sum)
|
|---|
| 30 | for (i=0; i < VECLEN; i++)
|
|---|
| 31 | {
|
|---|
| 32 | sum = sum + (a[i]*b[i]);
|
|---|
| 33 | printf(" tid= %d i=%d\n",tid,i);
|
|---|
| 34 | }
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 | int main (int argc, char *argv[]) {
|
|---|
| 39 | int i;
|
|---|
| 40 |
|
|---|
| 41 | for (i=0; i < VECLEN; i++)
|
|---|
| 42 | a[i] = b[i] = 1.0 * i;
|
|---|
| 43 | sum = 0.0;
|
|---|
| 44 |
|
|---|
| 45 | #pragma omp parallel
|
|---|
| 46 | dotprod();
|
|---|
| 47 |
|
|---|
| 48 | printf("Sum = %f\n",sum);
|
|---|
| 49 |
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.