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