source: CIVL/examples/concurrency/threadprivate.cvl@ b2ddb2e

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since b2ddb2e was 82136fb, checked in by Stephen Siegel <siegel@…>, 12 years ago

Adding an example derived from an OpenMP example, threadprivate.

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

  • Property mode set to 100644
File size: 1.2 KB
Line 
1#include<civlc.h>
2#include<stdio.h>
3#define OWNS(numIters, threadId, index) ((index)%NTHREADS==threadId)
4$heap h;
5int NTHREADS = 3;
6$proc threads[NTHREADS];
7
8int 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
16void 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.