| [3ff27cf] | 1 | #include <civlc.cvh>
|
|---|
| [9c73065] | 2 | /*
|
|---|
| 3 | * Command line execution:
|
|---|
| [69bf2e6] | 4 | * civl verify -inputTHREADS_BOUND=10 -inputN_BOUND=40 fig4.98-threadprivate.cvl -enablePrintf=false
|
|---|
| [9c73065] | 5 | */
|
|---|
| [82136fb] | 6 | #include<stdio.h>
|
|---|
| [69bf2e6] | 7 | $input int THREADS_BOUND; // upper bound of number of threads
|
|---|
| 8 | $input int NTHREADS;// number of threads
|
|---|
| [3ff27cf] | 9 | $assume(0 < NTHREADS && NTHREADS <=THREADS_BOUND);
|
|---|
| [69bf2e6] | 10 | $input int N_BOUND; // upper bound of N
|
|---|
| 11 | $input int N; // the value of the variable n
|
|---|
| [3ff27cf] | 12 | $assume(0 < N && N <=N_BOUND);
|
|---|
| [82136fb] | 13 | #define OWNS(numIters, threadId, index) ((index)%NTHREADS==threadId)
|
|---|
| 14 | $proc threads[NTHREADS];
|
|---|
| [d66b03b] | 15 | $scope root = $here;
|
|---|
| [82136fb] | 16 | int calculate_sum(int length, int *_pglobal) {
|
|---|
| 17 | int sum = 0;
|
|---|
| 18 |
|
|---|
| 19 | for (int j=0; j<length; j++) sum += _pglobal[j];
|
|---|
| 20 | return(sum);
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | void main() {
|
|---|
| [69bf2e6] | 24 | int i, j, sum, TID, n= N; // the oringial program uses 5 for n, but we need to use a larger value of n for verification when there are more than 5 threads.
|
|---|
| [82136fb] | 25 | int length[n], check[n];
|
|---|
| 26 |
|
|---|
| 27 | for (i=0; i<n; i++) {
|
|---|
| 28 | length[i] = 10*(i+1);
|
|---|
| 29 | check[i] = length[i]*(length[i]+1)/2;
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | void __par_1(int _TID) { // every thread runs this
|
|---|
| 33 | int *_pglobal;
|
|---|
| 34 |
|
|---|
| 35 | void __for_loop_1 (int _i) {
|
|---|
| 36 | int _j, _sum;
|
|---|
| 37 |
|
|---|
| [d66b03b] | 38 | _pglobal = (int*)$malloc(root, length[_i]*sizeof(int));
|
|---|
| [82136fb] | 39 | _sum = 0;
|
|---|
| 40 | for (_j=0; _j<length[_i]; _j++) _pglobal[_j]=_j+1;
|
|---|
| 41 | _sum = calculate_sum(length[_i], _pglobal);
|
|---|
| [abb7a279] | 42 | printf("TID %d: value of sum for i = %d is %8d (check = %8d)\n",
|
|---|
| [82136fb] | 43 | _TID, _i, _sum, check[_i]);
|
|---|
| [d980649] | 44 | $assert(_sum == check[_i]);
|
|---|
| [0b9a80a] | 45 | $free(_pglobal);
|
|---|
| [82136fb] | 46 | }
|
|---|
| 47 |
|
|---|
| 48 | for (int _i=0; _i<n; _i++)
|
|---|
| 49 | if (OWNS(n, _TID, _i)) __for_loop_1(_i);
|
|---|
| 50 | }
|
|---|
| [551b7d9] | 51 |
|
|---|
| 52 | for (int t=0; t<NTHREADS; t++) threads[t] = $spawn __par_1(t);
|
|---|
| [a82987f] | 53 | for (int t=0; t<NTHREADS; t++) $wait(threads[t]);
|
|---|
| [82136fb] | 54 | }
|
|---|