source: CIVL/examples/concurrency/fig4.98-threadprivate.cvl@ cf2a996

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since cf2a996 was 69bf2e6, checked in by Manchun Zheng <zmanchun@…>, 12 years ago

cleaned up examples and tests.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@611 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 1.6 KB
Line 
1/*
2 * Command line execution:
3 * civl verify -inputTHREADS_BOUND=10 -inputN_BOUND=40 fig4.98-threadprivate.cvl -enablePrintf=false
4 */
5#include<civlc.h>
6#include<stdio.h>
7$input int THREADS_BOUND; // upper bound of number of threads
8$input int NTHREADS;// number of threads
9$assume 0 < NTHREADS && NTHREADS <=THREADS_BOUND;
10$input int N_BOUND; // upper bound of N
11$input int N; // the value of the variable n
12$assume 0 < N && N <=N_BOUND;
13#define OWNS(numIters, threadId, index) ((index)%NTHREADS==threadId)
14$heap h;
15$proc threads[NTHREADS];
16
17int calculate_sum(int length, int *_pglobal) {
18 int sum = 0;
19
20 for (int j=0; j<length; j++) sum += _pglobal[j];
21 return(sum);
22}
23
24void main() {
25 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.
26 int length[n], check[n];
27
28 for (i=0; i<n; i++) {
29 length[i] = 10*(i+1);
30 check[i] = length[i]*(length[i]+1)/2;
31 }
32
33 void __par_1(int _TID) { // every thread runs this
34 int *_pglobal;
35
36 void __for_loop_1 (int _i) {
37 int _j, _sum;
38
39 _pglobal = (int*)$malloc(&h, length[_i]*sizeof(int));
40 _sum = 0;
41 for (_j=0; _j<length[_i]; _j++) _pglobal[_j]=_j+1;
42 _sum = calculate_sum(length[_i], _pglobal);
43 printf("TID %d: value of sum for i = %d is %8d (check = %8d)\n",
44 _TID, _i, _sum, check[_i]);
45 $assert(_sum == check[_i]);
46 $free(&h, _pglobal);
47 }
48
49 for (int _i=0; _i<n; _i++)
50 if (OWNS(n, _TID, _i)) __for_loop_1(_i);
51 }
52
53 for (int t=0; t<NTHREADS; t++) threads[t] = $spawn __par_1(t);
54 for (int t=0; t<NTHREADS; t++) $wait threads[t];
55}
Note: See TracBrowser for help on using the repository browser.