1.23
2.0
acw/focus-triggers
main
test-branch
| Line | |
|---|
| 1 | #include<civlc.h>
|
|---|
| 2 | #include<stdio.h>
|
|---|
| 3 | #define OWNS(numIters, threadId, index) ((index)%NTHREADS==threadId)
|
|---|
| 4 | $heap h;
|
|---|
| 5 | int NTHREADS = 3;
|
|---|
| 6 | $proc threads[NTHREADS];
|
|---|
| 7 |
|
|---|
| 8 | int calculate_sum(int length, int *_pglobal) {
|
|---|
| 9 | int sum = 0;
|
|---|
| 10 |
|
|---|
| 11 | for (int j=0; j<length; j++) sum += _pglobal[j];
|
|---|
| 12 | return(sum);
|
|---|
| 13 | }
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 | void main() {
|
|---|
| 17 | int i, j, sum, TID, n=5;
|
|---|
| 18 | int length[n], check[n];
|
|---|
| 19 |
|
|---|
| 20 | for (i=0; i<n; i++) {
|
|---|
| 21 | length[i] = 10*(i+1);
|
|---|
| 22 | check[i] = length[i]*(length[i]+1)/2;
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | void __par_1(int _TID) { // every thread runs this
|
|---|
| 26 | int *_pglobal;
|
|---|
| 27 |
|
|---|
| 28 | void __for_loop_1 (int _i) {
|
|---|
| 29 | int _j, _sum;
|
|---|
| 30 |
|
|---|
| 31 | // this $atom really shouldn't be there...
|
|---|
| 32 | $atom {
|
|---|
| 33 | _pglobal = (int*)$malloc(&h, length[_i]*sizeof(int));
|
|---|
| 34 | _sum = 0;
|
|---|
| 35 | for (_j=0; _j<length[_i]; _j++) _pglobal[_j]=_j+1;
|
|---|
| 36 | _sum = calculate_sum(length[_i], _pglobal);
|
|---|
| 37 | printf("TID %d: value of sum for i = %d is %8d (check = %8d)\n",
|
|---|
| 38 | _TID, _i, _sum, check[_i]);
|
|---|
| 39 | $assert(_sum == check[_i]);
|
|---|
| 40 | }
|
|---|
| 41 | $free(&h, _pglobal);
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | for (int _i=0; _i<n; _i++)
|
|---|
| 45 | if (OWNS(n, _TID, _i)) __for_loop_1(_i);
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | $atomic {
|
|---|
| 49 | for (int t=0; t<NTHREADS; t++) threads[t] = $spawn __par_1(t);
|
|---|
| 50 | for (int t=0; t<NTHREADS; t++) $wait threads[t];
|
|---|
| 51 | }
|
|---|
| 52 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.