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

main
Last change on this file was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 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<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$proc threads[NTHREADS];
15$scope root = $here;
16int 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
23void main() {
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.
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
38 _pglobal = (int*)$malloc(root, length[_i]*sizeof(int));
39 _sum = 0;
40 for (_j=0; _j<length[_i]; _j++) _pglobal[_j]=_j+1;
41 _sum = calculate_sum(length[_i], _pglobal);
42 printf("TID %d: value of sum for i = %d is %8d (check = %8d)\n",
43 _TID, _i, _sum, check[_i]);
44 $assert(_sum == check[_i]);
45 $free(_pglobal);
46 }
47
48 for (int _i=0; _i<n; _i++)
49 if (OWNS(n, _TID, _i)) __for_loop_1(_i);
50 }
51
52 for (int t=0; t<NTHREADS; t++) threads[t] = $spawn __par_1(t);
53 for (int t=0; t<NTHREADS; t++) $wait(threads[t]);
54}
Note: See TracBrowser for help on using the repository browser.