source: CIVL/include/impls/omp.cvl@ 1aaefd4

main test-branch
Last change on this file since 1aaefd4 was b76d962, checked in by Wenhao Wu <wuwenhao@…>, 3 years ago

Merge changes made for the verification of the OpenMP programs

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

  • Property mode set to 100644
File size: 5.2 KB
RevLine 
[4209e90]1#include <civlc.cvh>
[3af26ac]2#include <civl-omp.cvh>
[e6bb2ae]3#include <stdbool.h>
[86ee0b6]4#include <mem.cvh>
5#include <omp.h>
6
7#define OMP_LOCK_UNINITIALIZED -2
8#define OMP_LOCK_UNLOCKED -1
[469fa91]9
[2f8145f]10// implementations of functions in omp.h go here...
[d21ed28]11
[86ee0b6]12struct omp_lock_t {
13 int state; // owner thread id
14};
15
16struct omp_nest_lock_t {
17 int state; // owner thread id
18 int count; // lock counter
[e6bb2ae]19};
20
21/************************** OMP LIB Implementations *******************************/
22
[86ee0b6]23/**** **** **** **** OMP SIMPLE LOCK **** **** **** ****/
24
25/* void omp_init_lock (omp_lock_t *); */
26void omp_init_lock(omp_lock_t *lock){
27 lock->state = OMP_LOCK_UNLOCKED;
28}
29
30/* void omp_destroy_lock (omp_lock_t *); */
31void omp_destroy_lock(omp_lock_t *lock){
32 $assert(lock->state == OMP_LOCK_UNLOCKED,
33 "Destroy a simple lock not in 'unlocked' state.");
34
35 lock->state = OMP_LOCK_UNINITIALIZED;
[e6bb2ae]36}
37
[86ee0b6]38/* void omp_set_lock (omp_lock_t *); */
39void $omp_set_lock(omp_lock_t *lock, int tid) {
40 $atomic {
41 $assert(lock->state != OMP_LOCK_UNINITIALIZED,
42 "Set a simple lock in 'uninitialized' state.");
[b76d962]43 $read_set_push();
44 $write_set_push();
45 $yield();
[86ee0b6]46 $when(lock->state == OMP_LOCK_UNLOCKED) {
47 lock->state = tid;
48 }
[b76d962]49 $read_set_pop();
50 $write_set_pop();
[86ee0b6]51 }
[e6bb2ae]52}
53
[86ee0b6]54/* void omp_unset_lock (omp_lock_t *); */
55void $omp_unset_lock(omp_lock_t *lock, int tid) {
[f3274f8]56 $atomic {
[86ee0b6]57 $assert(lock->state == tid,
58 "Unset a simple lock not owned: caller_tid: %d, owner_tid: %d.",
59 tid, lock->state);
60 $assert(lock->state > OMP_LOCK_UNLOCKED,
61 "Unset a simple lock not in 'locked' state.");
62 $read_set_push();
63 $write_set_push();
64 lock->state = OMP_LOCK_UNLOCKED;
65 $read_set_pop();
66 $write_set_pop();
[f3274f8]67 }
68}
69
[86ee0b6]70/* int omp_test_lock (omp_lock_t *); */
71int $omp_test_lock(omp_lock_t *lock, int tid) {
72 int rtn = 0;
[f3274f8]73 $atomic {
[86ee0b6]74 $assert(lock->state != OMP_LOCK_UNINITIALIZED,
75 "Set a simple lock in 'uninitialized' state.");
76 $assert(lock->state != tid,
77 "Test a simple lock owned by the caller: tid: %d.", tid);
78 if (lock->state == OMP_LOCK_UNLOCKED) {
79 $read_set_push();
80 $write_set_push();
81 lock->state = tid;
82 $read_set_pop();
83 $write_set_pop();
84 rtn = 1;
85 }
[f3274f8]86 }
[86ee0b6]87 return rtn;
88}
89
90/**** **** **** **** OMP NESTED LOCK **** **** **** ****/
91
92
93/* void omp_init_nest_lock (omp_nest_lock_t *); */
94void omp_init_nest_lock(omp_nest_lock_t *lock){
95 $assert(lock->state == OMP_LOCK_UNINITIALIZED,
96 "Init a simple lock not in 'uninitialized' state.");
97
98 lock->state = OMP_LOCK_UNLOCKED;
99 lock->count = 0;
[f3274f8]100}
101
[86ee0b6]102/* void omp_destroy_nest_lock (omp_nest_lock_t *); */
103void omp_destroy_nest_lock(omp_nest_lock_t *lock){
104 $assert(lock->state == OMP_LOCK_UNLOCKED,
105 "Destroy a simple lock not in 'unlocked' state.");
106
107 lock->state = OMP_LOCK_UNINITIALIZED;
108 lock->count = 0;
109}
110
111/* void omp_set_nest_lock (omp_nest_lock_t *); */
112void $omp_set_nest_lock(omp_nest_lock_t *lock, int tid) {
113 $atomic {
114 $assert(lock->state != OMP_LOCK_UNINITIALIZED,
115 "Set a simple lock in 'uninitialized' state.");
[b76d962]116 $read_set_push();
117 $write_set_push();
118 $yield();
[86ee0b6]119 $when( lock->state == OMP_LOCK_UNLOCKED
120 || lock->state == tid) {
121 lock->state = tid;
122 lock->count = lock->count + 1;
[e6bb2ae]123 }
[b76d962]124 $read_set_pop();
125 $write_set_pop();
[e6bb2ae]126 }
127}
128
[86ee0b6]129/* void omp_unset_nest_lock (omp_nest_lock_t *); */
130void $omp_unset_nest_lock(omp_nest_lock_t *lock, int tid) {
131 $atomic {
132 $assert(lock->state == tid,
133 "Unset a simple lock not owned: caller_tid: %d, owner_tid: %d.",
134 tid, lock->state);
135 $assert(lock->state > OMP_LOCK_UNLOCKED,
136 "Unset a simple lock not in 'locked' state.");
137 $read_set_push();
138 $write_set_push();
139 lock->count = lock->count - 1;
140 if (lock->count == 0)
141 lock->state = OMP_LOCK_UNLOCKED;
142 $read_set_pop();
143 $write_set_pop();
144 }
145}
146
147/* int omp_test_nest_lock (omp_nest_lock_t *); */
148int $omp_test_nest_lock(omp_nest_lock_t *lock, int tid) {
149 $atomic {
150 $assert(lock->state != OMP_LOCK_UNINITIALIZED,
151 "Set a simple lock in 'uninitialized' state.");
152 $assert(lock->state != tid,
153 "Test a simple lock owned by the caller: tid: %d.", tid);
154 if ( lock->state == OMP_LOCK_UNLOCKED
155 || lock->state == tid) {
156 $read_set_push();
157 $write_set_push();
158 lock->state = tid;
159 lock->count = lock->count + 1;
160 $read_set_pop();
161 $write_set_pop();
162 }
[e6bb2ae]163 }
[86ee0b6]164 return lock->count;
[e6bb2ae]165}
166
[4209e90]167$abstract double OMP_time(int time_count);
[e6bb2ae]168
[86ee0b6]169/* double omp_get_wtime (void); */
[e6bb2ae]170double omp_get_wtime() {
[4209e90]171 int OMP_time_count = $next_time_count();
172 double result = OMP_time(OMP_time_count);
173
174 if (OMP_time_count > 0) {
[3ff27cf]175 $assume(result > OMP_time(OMP_time_count-1));
[4209e90]176 } else {
[3ff27cf]177 $assume(result > 0);
[4209e90]178 }
[e6bb2ae]179 return result;
180}
181
[86ee0b6]182/* OpenMP Library Routines that transformed by CIVL:
183 * void omp_set_num_threads (int);
184 * int omp_get_num_threads (void);
185 * int omp_get_max_threads (void);
186 * int omp_get_thread_num (void);
187 * int omp_get_num_procs (void);
188 */
189
[e6bb2ae]190
[86ee0b6]191/* Unsupported Functions:
192 * int omp_in_parallel (void);
193 * void omp_set_dynamic (int);
194 * int omp_get_dynamic (void);
195 * void omp_set_nested (int);
196 * int omp_get_nested (void);
197 * double omp_get_wtick (void);
[2f8145f]198*/
Note: See TracBrowser for help on using the repository browser.