source: CIVL/examples/omp/simple/omp_reduce.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.5 KB
Line 
1#include <assert.h>
2#include <omp.h>
3#include <stdio.h>
4
5int main () {
6 int n = 5;
7 int arr[5] = {5,3,9,1,7};
8 // Reduction combiners
9 int max = -10;
10 int min = 10;
11 int sum = 0;
12 int sub = 30;
13 int prod = 1;
14 _Bool land = 1; /* TRUE */
15 int band = ~(arr[0] & 0);
16 _Bool lor = 0; /* FALSE */
17 int bor = (arr[0] & 0);
18 int bxor = (arr[0] & 0);
19
20#pragma omp parallel for reduction(max:max)
21 for (int i=0; i<n; i++)
22 max = max < arr[i] ? arr[i] : max;
23 assert(max == 9);
24
25#pragma omp parallel for reduction(min:min)
26 for (int i=0; i<n; i++)
27 min = min > arr[i] ? arr[i] : min;
28 assert(min == 1);
29
30#pragma omp parallel for reduction(+:sum)
31 for (int i=0; i<n; i++)
32 sum = sum + arr[i];
33 assert(sum == 25);
34
35#pragma omp parallel for reduction(-:sub)
36 for (int i=0; i<n; i++)
37 sub = sub - arr[i];
38 assert(sub == 5);
39
40#pragma omp parallel for reduction(*:prod)
41 for (int i=0; i<n; i++)
42 prod = prod * arr[i];
43 assert(prod == 945);
44
45#pragma omp parallel for reduction(&&:land)
46 for (int i=0; i<n; i++)
47 land = land && arr[i];
48 assert(land);
49
50#pragma omp parallel for reduction(&:band)
51 for (int i=0; i<n; i++)
52 band = band & arr[i];
53 assert(band == 1);
54
55#pragma omp parallel for reduction(||:lor)
56 for (int i=0; i<n; i++)
57 lor = lor || arr[i];
58 assert(lor);
59
60#pragma omp parallel for reduction(|:bor)
61 for (int i=0; i<n; i++)
62 bor = bor | arr[i];
63 assert(bor == 15);
64
65#pragma omp parallel for reduction(^:bxor)
66 for (int i=0; i<n; i++)
67 bxor = bxor ^ arr[i];
68 assert(bxor == 9);
69}
Note: See TracBrowser for help on using the repository browser.