| 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 | $read_set_push();
|
|---|
| 44 | $write_set_push();
|
|---|
| 45 | $yield();
|
|---|
| 46 | $when(lock->state == OMP_LOCK_UNLOCKED) {
|
|---|
| 47 | lock->state = tid;
|
|---|
| 48 | }
|
|---|
| 49 | $read_set_pop();
|
|---|
| 50 | $write_set_pop();
|
|---|
| 51 | }
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | /* void omp_unset_lock (omp_lock_t *); */
|
|---|
| 55 | void $omp_unset_lock(omp_lock_t *lock, int tid) {
|
|---|
| 56 | $atomic {
|
|---|
| 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();
|
|---|
| 67 | }
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | /* int omp_test_lock (omp_lock_t *); */
|
|---|
| 71 | int $omp_test_lock(omp_lock_t *lock, int tid) {
|
|---|
| 72 | int rtn = 0;
|
|---|
| 73 | $atomic {
|
|---|
| 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 | }
|
|---|
| 86 | }
|
|---|
| 87 | return rtn;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | /**** **** **** **** OMP NESTED LOCK **** **** **** ****/
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 | /* void omp_init_nest_lock (omp_nest_lock_t *); */
|
|---|
| 94 | void 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;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | /* void omp_destroy_nest_lock (omp_nest_lock_t *); */
|
|---|
| 103 | void 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 *); */
|
|---|
| 112 | void $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.");
|
|---|
| 116 | $read_set_push();
|
|---|
| 117 | $write_set_push();
|
|---|
| 118 | $yield();
|
|---|
| 119 | $when( lock->state == OMP_LOCK_UNLOCKED
|
|---|
| 120 | || lock->state == tid) {
|
|---|
| 121 | lock->state = tid;
|
|---|
| 122 | lock->count = lock->count + 1;
|
|---|
| 123 | }
|
|---|
| 124 | $read_set_pop();
|
|---|
| 125 | $write_set_pop();
|
|---|
| 126 | }
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | /* void omp_unset_nest_lock (omp_nest_lock_t *); */
|
|---|
| 130 | void $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 *); */
|
|---|
| 148 | int $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 | }
|
|---|
| 163 | }
|
|---|
| 164 | return lock->count;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | $abstract double OMP_time(int time_count);
|
|---|
| 168 |
|
|---|
| 169 | /* double omp_get_wtime (void); */
|
|---|
| 170 | double omp_get_wtime() {
|
|---|
| 171 | int OMP_time_count = $next_time_count();
|
|---|
| 172 | double result = OMP_time(OMP_time_count);
|
|---|
| 173 |
|
|---|
| 174 | if (OMP_time_count > 0) {
|
|---|
| 175 | $assume(result > OMP_time(OMP_time_count-1));
|
|---|
| 176 | } else {
|
|---|
| 177 | $assume(result > 0);
|
|---|
| 178 | }
|
|---|
| 179 | return result;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 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 |
|
|---|
| 190 |
|
|---|
| 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);
|
|---|
| 198 | */
|
|---|