| [10670fd] | 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>
|
|---|
| [3cf40e5] | 7 | #include <assert.h>
|
|---|
| [10670fd] | 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 |
|
|---|
| 16 | int 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 | {
|
|---|
| [3cf40e5] | 25 | int _nthreads = 1+$choose_int(THREAD_MAX);
|
|---|
| 26 | $proc threads[_nthreads];
|
|---|
| 27 | $gbarrier gbarrier = $gbarrier_create($here, _nthreads);
|
|---|
| 28 | void thread(int _tid) {
|
|---|
| [10670fd] | 29 | int i, tid;
|
|---|
| 30 | double localsum;
|
|---|
| [3cf40e5] | 31 | $barrier barrier = $barrier_create($here, gbarrier, _tid);
|
|---|
| [10670fd] | 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 | }
|
|---|
| [3cf40e5] | 43 | $barrier_call(barrier);
|
|---|
| [10670fd] | 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;
|
|---|
| [3cf40e5] | 53 | $barrier_destroy(barrier);
|
|---|
| [10670fd] | 54 | }
|
|---|
| 55 |
|
|---|
| 56 | for (int i=0; i<_nthreads; i++)
|
|---|
| [3cf40e5] | 57 | threads[i] = $spawn thread(i);
|
|---|
| [10670fd] | 58 | for (int i=0; i<_nthreads; i++)
|
|---|
| 59 | $wait(threads[i]);
|
|---|
| [3cf40e5] | 60 | $gbarrier_destroy(gbarrier);
|
|---|
| [10670fd] | 61 | }
|
|---|
| 62 | printf(" Sum = %2.1f\n",sum);
|
|---|
| [3cf40e5] | 63 | assert(sum==285.0);
|
|---|
| [10670fd] | 64 | }
|
|---|