source: CIVL/examples/translation/openmp/dotProduct.cvl@ bfebb46

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since bfebb46 was 58da37d, checked in by Manchun Zheng <zmanchun@…>, 12 years ago

cleaned up openmp examples.

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

  • Property mode set to 100644
File size: 2.3 KB
Line 
1/******************************************************************************
2* FILE: omp_reduction.c
3* DESCRIPTION:
4* OpenMP Example - Combined Parallel Loop Reduction - C/C++ Version
5* This example demonstrates a sum reduction within a combined parallel loop
6* construct. Notice that default data element scoping is assumed - there
7* are no clauses specifying shared or private variables. OpenMP will
8* automatically make loop index variables private within team threads, and
9* global variables shared.
10* AUTHOR: Blaise Barney 5/99
11* LAST REVISED: 04/06/05
12******************************************************************************/
13/**
14* This program computes the dot product of two vectors.
15* Online source:
16* https://computing.llnl.gov/tutorials/openMP/samples/C/omp_reduction.c
17**/
18/*
19 * civl verify -inputNTHREADS=2 -inputN=8 dotProduct.cvl
20 * */
21#include <civlc.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include "omp.cvh"
25$input int NTHREADS;
26$input int N;
27
28int main (int argc, char *argv[]) {
29 /* implicitly initializing the number of threads */
30 omp_set_num_threads(NTHREADS);
31
32 int i;
33 double a[N], b[N], sum;
34
35 /* Some initializations */
36 for (i=0; i < N; i++)
37 a[i] = b[i] = i * 1.0;
38 sum = 0.0;
39
40 /* The procedure of each thread for the for loop */
41 void __for_0(int __tid, int __start, int __end, int __extra){
42 double __sum = sum;
43
44 for(int __i = __start; __i < __end; __i++) {
45 __sum = __sum + (a[__i] * b[__i]);
46 }
47 if(__extra > 0) {
48 __sum = __sum + (a[__extra] * b[__extra]);
49 }
50 __barrier(__tid);
51 sum += __sum;
52 }
53
54 /* Helper variales introduced for the parallel for loop */
55 $proc __for_0_procs[OMP_NUM_THREADS];
56
57 __barrier_init();
58 /* Create processes to conduct the parallel for loop */
59 for(i = 0; i < OMP_NUM_THREADS; i++) {
60 int __start = __for_start(i, N);
61 int __end = __for_end(i, N);
62 int __extra = __for_extra(i, N);
63
64 __for_0_procs[i] = $spawn __for_0(i, __start, __end, __extra);
65 }
66
67 /** The orginal OpenMP parallel for loop
68 #pragma omp parallel for reduction(+:sum)
69 for (i=0; i < n; i++){
70 sum = sum + (a[i] * b[i]);
71 printf("loop %d\n", i);
72 }
73 **/
74
75 /* Join parallel for processes */
76 for(i = 0; i < OMP_NUM_THREADS; i++) {
77 $wait(__for_0_procs[i]);
78 }
79 /* Print result */
80 printf(" Sum = %f\n",sum);
81}
Note: See TracBrowser for help on using the repository browser.