source: CIVL/examples/concurrency/threadprivate.cvl@ 9c73065

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

improved the POR algorithm, now it scales well.

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

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