source: CIVL/examples/cuda/exitBarrier.cvl@ b231753

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

fix the return type syntax error of cuda/exitBarrier.cvl.

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

  • Property mode set to 100644
File size: 3.9 KB
RevLine 
[dd83b0d]1// Tyler Sorensen
2//
3// An attempt to create an "exit barrier"
4// as seen in NVIDIA PTX instructions
5// See "exit" and "bar.sync" instructions in PTX ISA
6//
7// The algorithm implemented uses the general CIVL barrier
8// module found in examples/concurrency/barrier2.cvl
9//
10// The exit semantics are a combination of barrier
11// resignation as seen in KRoC release of occam and
12// documented in the paper:
13// Higher Level of Process Synchronization by:
14// Peter H. Welch and David C. Wood.
15// and a return statement (to exit the thread)
16//
17// The resignation algorithm is given briefly in
18// the paper cited above which I attempted to
19// implement. However, this implementation assumes
20// that a resigned thread does not access the barrier
21// again! It is implemented with the exit barrier
22// semantics in mind, i.e. the thread will cease
23// execution right after resignation. No attempts
24// are made to guard against behavior deviating
25// from this.
[36b5ada]26// Commandline execution:
27// civl verify exitBarrier.cvl
[dd83b0d]28
29// TODO: Right now, the thread termination is simply
30// a "return" statement. Anyway for that to actually
31// kill the thread?
32
33#include <civlc.h>
34
35struct _CIVL_Barrier {
36 int numProcs; //how many are enrolled to begin with, cannot change
37 int numWaiting; //New variable for exit barriers, how many you need to wait for, can change
38 int *in_barrier;
39 int num_in_barrier;
40 int lock;
41};
42
43typedef struct _CIVL_Barrier CIVL_Barrier;
44
45void CIVL_Barrier_init(int numProcs, CIVL_Barrier *barrier, int *array) {
46 barrier->numProcs = numProcs;
47 barrier->numWaiting = numProcs;
48 barrier->num_in_barrier = 0;
49 barrier->lock = 0;
50 barrier->in_barrier = array;
51 for (int i=0; i<numProcs; i++)
52 barrier->in_barrier[i] = 0;
53}
54
55void CIVL_barrier(CIVL_Barrier *barrier, int tid) {
56 $when (barrier->lock==0) barrier->lock = 1;
57 barrier->in_barrier[tid] = 1;
58 barrier->num_in_barrier++;
59
60 //simply wait for how many need to be waited for, but
61 //free them all. No exceptions.
62 if (barrier->num_in_barrier == barrier->numWaiting) {
63 for (int i=0; i<barrier->numProcs; i++)
64 barrier->in_barrier[i] = 0;
65 barrier->num_in_barrier = 0;
66 }
67 barrier->lock = 0;
[a465feb]68 $when (barrier->in_barrier[tid] == 0);
[dd83b0d]69}
70
71//The resign function: Basically the same, but
72//decrement the number waiting for. If
73//A barrier is waiting on the resigning thread, then free
74//the barrier before leaving. No need to wait also.
75void CIVL_barrier_resign(CIVL_Barrier *barrier, int tid) {
76 $when (barrier->lock==0) barrier->lock = 1;
77 barrier->in_barrier[tid] = 1;
78 barrier->numWaiting--;
79 if (barrier->num_in_barrier == barrier->numWaiting) {
80 for (int i=0; i<barrier->numProcs; i++)
81 barrier->in_barrier[i] = 0;
82 barrier->num_in_barrier = 0;
83 }
[a465feb]84 barrier->lock = 0;
[dd83b0d]85}
86
[b968f34]87#define CIVL_barrier_exit(barrier, tid) CIVL_barrier_resign(barrier, tid); return
[dd83b0d]88
89void main() {
90 int N=4;
91 $proc threads[N];
92 int counter = 0;
93 CIVL_Barrier b;
94 int barrier_array[N];
95
[36b5ada]96 void run_proc0(int tid) {
[09b9231b]97 $assert(counter == 0);
[a465feb]98 CIVL_barrier_exit(&b, tid);
[dd83b0d]99 }
100
101 void run_proc1(int tid) {
[09b9231b]102 $assert(counter == 0);
[dd83b0d]103 CIVL_barrier(&b, tid);
104 counter++;
[a465feb]105 CIVL_barrier_exit(&b, tid);
[dd83b0d]106 }
107
[36b5ada]108 void run_proc2(int tid) {
[09b9231b]109 $assert(counter == 0);
[dd83b0d]110 CIVL_barrier(&b, tid);
111 counter++;
112 CIVL_barrier(&b, tid);
[09b9231b]113 $assert(counter == 3);
[a465feb]114 CIVL_barrier_exit(&b, tid);
[dd83b0d]115 }
116
117 void run_proc3(int tid) {
[09b9231b]118 $assert(counter == 0);
[dd83b0d]119 CIVL_barrier(&b, tid);
120 counter++;
121 CIVL_barrier(&b, tid);
[09b9231b]122 $assert(counter == 3);
[dd83b0d]123 CIVL_barrier(&b, tid);
124 counter--;
[a465feb]125 CIVL_barrier_exit(&b, tid);
[dd83b0d]126 }
[a465feb]127
128 CIVL_Barrier_init(N, &b, barrier_array);
[dd83b0d]129
130 //Spawn threads
131 $proc proc0 = $spawn run_proc0(0);
132 $proc proc1 = $spawn run_proc1(1);
133 $proc proc2 = $spawn run_proc2(2);
134 $proc proc3 = $spawn run_proc3(3);
135
136 //wait for threads
[a82987f]137 $wait(proc0);
138 $wait(proc1);
139 $wait(proc2);
140 $wait(proc3);
[dd83b0d]141
[09b9231b]142 $assert(counter == 2);
[dd83b0d]143}
Note: See TracBrowser for help on using the repository browser.