source: CIVL/examples/omp/dotProduct_critical.cvl

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.6 KB
Line 
1/* CIVL model of dotProduct_critical.c. To verify:
2 * civl verify dotProduct_critical.cvl
3 */
4#include <civlc.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <assert.h>
8
9#define N 10
10#define THREAD_MAX 4
11/* Does thread t own iteration i in loop with n iterations? */
12#define CIVL_owns(t, n, i) ((i)%(n)==(t))
13
14_Bool critical_lock = $false;
15
16int main (int argc, char *argv[]) {
17 double a[N], b[N];
18 double sum = 0.0;
19 int nthreads;
20
21 // translation of parallel construct
22 // #pragma omp parallel shared(a,b,sum) private(i, localsum)
23 // note: tid should be private. corrected here.
24 {
25 int _nthreads = 1+$choose_int(THREAD_MAX);
26 $proc threads[_nthreads];
27 $gbarrier gbarrier = $gbarrier_create($here, _nthreads);
28 void thread(int _tid) {
29 int i, tid;
30 double localsum;
31 $barrier barrier = $barrier_create($here, gbarrier, _tid);
32
33 tid = _tid;
34 if (_tid == 0) {
35 nthreads =_nthreads;
36 printf("Number of threads = %d\n", nthreads);
37 }
38 for (i=0; i < N; i++) {
39 if (CIVL_owns(_tid, _nthreads, i)) {
40 a[i] = b[i] = (double)i;
41 }
42 }
43 $barrier_call(barrier);
44 localsum = 0;
45 for (i=0; i < N; i++) {
46 if (CIVL_owns(_tid, _nthreads, i)) {
47 localsum = localsum + (a[i] * b[i]);
48 }
49 }
50 $when (!critical_lock) critical_lock = $true;
51 sum = sum + localsum;
52 critical_lock = $false;
53 $barrier_destroy(barrier);
54 }
55
56 for (int i=0; i<_nthreads; i++)
57 threads[i] = $spawn thread(i);
58 for (int i=0; i<_nthreads; i++)
59 $wait(threads[i]);
60 $gbarrier_destroy(gbarrier);
61 }
62 printf(" Sum = %2.1f\n",sum);
63 assert(sum==285.0);
64}
Note: See TracBrowser for help on using the repository browser.