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