| 1 | /* dotProduct_orphan.cvl: model of dotProduct_orphan.c.
|
|---|
| 2 | */
|
|---|
| 3 | #include <civlc.h>
|
|---|
| 4 | #include <stdio.h>
|
|---|
| 5 | #include <stdlib.h>
|
|---|
| 6 | #include <assert.h>
|
|---|
| 7 | #define VECLEN 10
|
|---|
| 8 | #define THREAD_MAX 4
|
|---|
| 9 | /* Does thread t own iteration i in loop with n iterations? */
|
|---|
| 10 | #define CIVL_owns(t, n, i) ((i)%(n)==(t))
|
|---|
| 11 |
|
|---|
| 12 | float a[VECLEN], b[VECLEN], sum;
|
|---|
| 13 |
|
|---|
| 14 | // note dotprod has been changed to accept
|
|---|
| 15 | // _tid and _nthreads, since it can be called
|
|---|
| 16 | // from a parallel construct.
|
|---|
| 17 | // also, return type has changed to void since it
|
|---|
| 18 | // does not return value.
|
|---|
| 19 | void dotprod (int _tid, int _nthreads) {
|
|---|
| 20 | int i, tid, _sum=0;
|
|---|
| 21 |
|
|---|
| 22 | tid = _tid;
|
|---|
| 23 | // #pragma omp for reduction(+:sum)
|
|---|
| 24 | for (i=0; i < VECLEN; i++) {
|
|---|
| 25 | if (CIVL_owns(_tid, _nthreads, i)) {
|
|---|
| 26 | _sum = _sum + (a[i]*b[i]);
|
|---|
| 27 | printf(" tid= %d i=%d\n",tid,i);
|
|---|
| 28 | }
|
|---|
| 29 | }
|
|---|
| 30 | sum += _sum;
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | void main () {
|
|---|
| 34 | int i;
|
|---|
| 35 |
|
|---|
| 36 | for (i=0; i < VECLEN; i++)
|
|---|
| 37 | a[i] = b[i] = 1.0 * i;
|
|---|
| 38 | sum = 0.0;
|
|---|
| 39 | { // #pragma omp parallel
|
|---|
| 40 | int _nthreads = 1+$choose_int(THREAD_MAX);
|
|---|
| 41 | $proc threads[_nthreads];
|
|---|
| 42 | void parallel_body(int _tid) {
|
|---|
| 43 | dotprod(_tid, _nthreads);
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | for (int tid=0; tid<_nthreads; tid++)
|
|---|
| 47 | threads[tid] = $spawn parallel_body(tid);
|
|---|
| 48 | for (int tid=0; tid<_nthreads; tid++)
|
|---|
| 49 | $wait(threads[tid]);
|
|---|
| 50 | } // end of parallel region
|
|---|
| 51 |
|
|---|
| 52 | printf("Sum = %f\n",sum);
|
|---|
| 53 | assert(sum==285);
|
|---|
| 54 | }
|
|---|