source: CIVL/examples/omp/fig4.98-threadprivate.cvl@ b784507

1.23 2.0 main test-branch
Last change on this file since b784507 was 3ff27cf, checked in by Manchun Zheng <zmanchun@…>, 11 years ago

updated examples since $assert/$assume has been changed to functions; fixed the model builder for the new side-effect remover.

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

  • Property mode set to 100644
File size: 1.7 KB
Line 
1#include <civlc.cvh>
2/*
3 * Command line execution:
4 * civl verify -inputTHREADS_BOUND=10 -inputN_BOUND=40 fig4.98-threadprivate.cvl -enablePrintf=false
5 */
6#include<civlc.h>
7#include<stdio.h>
8$input int THREADS_BOUND; // upper bound of number of threads
9$input int NTHREADS;// number of threads
10$assume(0 < NTHREADS && NTHREADS <=THREADS_BOUND);
11$input int N_BOUND; // upper bound of N
12$input int N; // the value of the variable n
13$assume(0 < N && N <=N_BOUND);
14#define OWNS(numIters, threadId, index) ((index)%NTHREADS==threadId)
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($root, 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(_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.