| 1 | #include <mem.cvh>
|
|---|
| 2 | #include <stdio.h>
|
|---|
| 3 |
|
|---|
| 4 | typedef int data_t;
|
|---|
| 5 |
|
|---|
| 6 | $mem ws[2];
|
|---|
| 7 | $mem rs[2];
|
|---|
| 8 |
|
|---|
| 9 | void check_data_race(int cur_tid) {
|
|---|
| 10 | $mem out_s0 = $mem_empty();
|
|---|
| 11 | $mem out_s1 = $mem_empty();
|
|---|
| 12 | $mem cur_mw = $write_set_pop();
|
|---|
| 13 | $mem cur_mr = $read_set_pop();
|
|---|
| 14 | ws[cur_tid] = cur_mw;
|
|---|
| 15 | rs[cur_tid] = cur_mr;
|
|---|
| 16 | {
|
|---|
| 17 | int tmp_tid = 0;
|
|---|
| 18 | for (; tmp_tid < 2; tmp_tid = tmp_tid + 1) {
|
|---|
| 19 | if (tmp_tid == cur_tid)
|
|---|
| 20 | continue;
|
|---|
| 21 | $mem tmp_mr = rs[tmp_tid];
|
|---|
| 22 | $mem tmp_mw = ws[tmp_tid];
|
|---|
| 23 | _Bool $sef$14 = $mem_no_intersect(cur_mr, tmp_mw, &(out_s0), &(out_s1));
|
|---|
| 24 | $assert($sef$14, "Data-race detected: %p read by thread %d intersects %p written by thread %d\n", out_s0, cur_tid, out_s1, tmp_tid);
|
|---|
| 25 | _Bool $sef$15 = $mem_no_intersect(cur_mw, tmp_mr, &(out_s0), &(out_s1));
|
|---|
| 26 | $assert($sef$15, "Data-race detected: %p read by thread %d intersects %p written by thread %d\n", out_s0, cur_tid, out_s1, tmp_tid);
|
|---|
| 27 | _Bool $sef$16 = $mem_no_intersect(cur_mw, tmp_mw, &(out_s0), &(out_s1));
|
|---|
| 28 | $assert($sef$16, "Data-race detected: %p written by thread %d intersects %p written by thread %d\n", out_s0, cur_tid, out_s1, tmp_tid);
|
|---|
| 29 | }
|
|---|
| 30 | }
|
|---|
| 31 | $read_set_push();
|
|---|
| 32 | $write_set_push();
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | void clear_mem_sets(int cur_tid) {
|
|---|
| 36 | ws[cur_tid] = $mem_empty();
|
|---|
| 37 | rs[cur_tid] = $mem_empty();
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | int assignVal(int* p, int v) {
|
|---|
| 41 | int ret = *p;
|
|---|
| 42 | *p = v;
|
|---|
| 43 | return ret;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | void simpleKernel(int tid, int* data1) {
|
|---|
| 47 | $local_start();
|
|---|
| 48 | $read_set_push();
|
|---|
| 49 | $write_set_push();
|
|---|
| 50 | data1[0] = 1;
|
|---|
| 51 | int* $_cuda_tmp2 = data1;
|
|---|
| 52 | check_data_race(tid);
|
|---|
| 53 | $yield();
|
|---|
| 54 | int $_cuda_tmp3 = assignVal($_cuda_tmp2, 5);
|
|---|
| 55 | check_data_race(tid);
|
|---|
| 56 | clear_mem_sets(tid);
|
|---|
| 57 | //printf("wow\n");
|
|---|
| 58 | check_data_race(tid);
|
|---|
| 59 | $read_set_pop();
|
|---|
| 60 | $write_set_pop();
|
|---|
| 61 | $local_end();
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | int main() {
|
|---|
| 65 | //fprintf(stdout, "wut");
|
|---|
| 66 | data_t data1 = 0;
|
|---|
| 67 | $parfor(int x : 0.. 1) {
|
|---|
| 68 | simpleKernel(x, &data1);
|
|---|
| 69 | }
|
|---|
| 70 | }
|
|---|