| 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 |
|
|---|
| 8 | #define N 10
|
|---|
| 9 | #define THREAD_MAX 4
|
|---|
| 10 | /* Does thread t own iteration i in loop with n iterations? */
|
|---|
| 11 | #define CIVL_owns(t, n, i) ((i)%(n)==(t))
|
|---|
| 12 |
|
|---|
| 13 | _Bool critical_lock = $false;
|
|---|
| 14 |
|
|---|
| 15 | int main (int argc, char *argv[]) {
|
|---|
| 16 | double a[N], b[N];
|
|---|
| 17 | double sum = 0.0;
|
|---|
| 18 | int nthreads;
|
|---|
| 19 |
|
|---|
| 20 | // translation of parallel construct
|
|---|
| 21 | // #pragma omp parallel shared(a,b,sum) private(i, localsum)
|
|---|
| 22 | // note: tid should be private. corrected here.
|
|---|
| 23 | {
|
|---|
| 24 | int barrier = 0;
|
|---|
| 25 | void thread(int _tid, int _nthreads) {
|
|---|
| 26 | int i, tid;
|
|---|
| 27 | double localsum;
|
|---|
| 28 |
|
|---|
| 29 | tid = _tid;
|
|---|
| 30 | if (_tid == 0) {
|
|---|
| 31 | nthreads =_nthreads;
|
|---|
| 32 | printf("Number of threads = %d\n", nthreads);
|
|---|
| 33 | }
|
|---|
| 34 | for (i=0; i < N; i++) {
|
|---|
| 35 | if (CIVL_owns(_tid, _nthreads, i)) {
|
|---|
| 36 | a[i] = b[i] = (double)i;
|
|---|
| 37 | }
|
|---|
| 38 | }
|
|---|
| 39 | barrier++;
|
|---|
| 40 | $when (barrier==_nthreads);
|
|---|
| 41 | localsum = 0;
|
|---|
| 42 | for (i=0; i < N; i++) {
|
|---|
| 43 | if (CIVL_owns(_tid, _nthreads, i)) {
|
|---|
| 44 | localsum = localsum + (a[i] * b[i]);
|
|---|
| 45 | }
|
|---|
| 46 | }
|
|---|
| 47 | $when (!critical_lock) critical_lock = $true;
|
|---|
| 48 | sum = sum + localsum;
|
|---|
| 49 | critical_lock = $false;
|
|---|
| 50 | }
|
|---|
| 51 | int _nthreads = 1+$choose_int(THREAD_MAX);
|
|---|
| 52 | $proc threads[_nthreads];
|
|---|
| 53 |
|
|---|
| 54 | for (int i=0; i<_nthreads; i++)
|
|---|
| 55 | threads[i] = $spawn thread(i, _nthreads);
|
|---|
| 56 | for (int i=0; i<_nthreads; i++)
|
|---|
| 57 | $wait(threads[i]);
|
|---|
| 58 | }
|
|---|
| 59 | printf(" Sum = %2.1f\n",sum);
|
|---|
| 60 | }
|
|---|