| 1 | /* Manually transformed CIVL-C code for the openMP data race benchmakr
|
|---|
| 2 | 028 example.
|
|---|
| 3 | */
|
|---|
| 4 | /*
|
|---|
| 5 | tmp should be annotated as private to avoid race condition.
|
|---|
| 6 | Data race pairs: tmp@65:5 vs. tmp@66:12
|
|---|
| 7 | tmp@65:5 vs. tmp@65:5
|
|---|
| 8 | */
|
|---|
| 9 |
|
|---|
| 10 | /*
|
|---|
| 11 | ###############################
|
|---|
| 12 | ###### Origianl program ######
|
|---|
| 13 |
|
|---|
| 14 | #include <stdlib.h>
|
|---|
| 15 | #include <stdio.h>
|
|---|
| 16 | int main(int argc, char* argv[])
|
|---|
| 17 | {
|
|---|
| 18 | int i;
|
|---|
| 19 | int tmp;
|
|---|
| 20 | int len=100;
|
|---|
| 21 | int a[100];
|
|---|
| 22 | for (i=0;i<len;i++)
|
|---|
| 23 | a[i]=i;
|
|---|
| 24 |
|
|---|
| 25 | #pragma omp parallel for
|
|---|
| 26 | for (i=0;i<len;i++)
|
|---|
| 27 | {
|
|---|
| 28 | tmp =a[i]+i;
|
|---|
| 29 | a[i] = tmp;
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | printf("a[50]=%d\n", a[50]);
|
|---|
| 33 | return 0;
|
|---|
| 34 | }
|
|---|
| 35 | ###############################
|
|---|
| 36 | */
|
|---|
| 37 | #include<stdio.h>
|
|---|
| 38 | #include<mem.cvh>
|
|---|
| 39 | #include<civl-omp.cvh>
|
|---|
| 40 |
|
|---|
| 41 | #ifndef NTHREADS
|
|---|
| 42 | #define NTHREADS 10
|
|---|
| 43 | #endif
|
|---|
| 44 |
|
|---|
| 45 | /* Eventually these two sets shall be stored in $omp_team locally.
|
|---|
| 46 | */
|
|---|
| 47 | $mem reads[NTHREADS], writes[NTHREADS];
|
|---|
| 48 |
|
|---|
| 49 | /*@ executes_when \true;
|
|---|
| 50 | @ depends_on \nothing;
|
|---|
| 51 | */
|
|---|
| 52 | void capture_start(int tid);
|
|---|
| 53 |
|
|---|
| 54 | /*@ executes_when \true;
|
|---|
| 55 | @ depends_on \nothing;
|
|---|
| 56 | */
|
|---|
| 57 | void capture_end(int tid);
|
|---|
| 58 |
|
|---|
| 59 | /*@ executes_when \true;
|
|---|
| 60 | @ depends_on \nothing;
|
|---|
| 61 | */
|
|---|
| 62 | void check_data_race();
|
|---|
| 63 |
|
|---|
| 64 | int main() {
|
|---|
| 65 | int i;
|
|---|
| 66 | int tmp;
|
|---|
| 67 | int len=100;
|
|---|
| 68 | int a[100];
|
|---|
| 69 | for (i=0;i<len;i++)
|
|---|
| 70 | a[i]=i;
|
|---|
| 71 |
|
|---|
| 72 | $omp_gteam gteam = $omp_gteam_create($here, NTHREADS);
|
|---|
| 73 |
|
|---|
| 74 | // translation of "parallel"
|
|---|
| 75 | $parfor (int _tid : 0 .. NTHREADS-1) {
|
|---|
| 76 | $local_start();
|
|---|
| 77 | $omp_team team = $omp_team_create($here, gteam, _tid);
|
|---|
| 78 | // translation of "for":
|
|---|
| 79 | $domain loop_domain = ($domain(1)){0 .. len-1};
|
|---|
| 80 | $domain(1) my_iters = ($domain(1))$omp_arrive_loop(team, 0, loop_domain, ROUND_ROBIN);
|
|---|
| 81 |
|
|---|
| 82 | // original loop-body:
|
|---|
| 83 | capture_start(_tid); // start monitoring
|
|---|
| 84 | $for (int i : my_iters) {
|
|---|
| 85 | tmp = a[i]+i;
|
|---|
| 86 | a[i] = tmp;
|
|---|
| 87 | }
|
|---|
| 88 | capture_end(_tid); // end monitoring
|
|---|
| 89 | $barrier_call(team->barrier); // implicit at end of parallel region
|
|---|
| 90 | $local_end();
|
|---|
| 91 | if (_tid == 0)
|
|---|
| 92 | check_data_race();
|
|---|
| 93 | $omp_team_destroy(team);
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | printf("a[50]=%d\n", a[50]);
|
|---|
| 97 | $omp_gteam_destroy(gteam);
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 | void capture_start(int tid) {
|
|---|
| 102 | $read_set_push();$write_set_push();
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | void capture_end(int tid) {
|
|---|
| 106 | $mem mr = $read_set_pop();
|
|---|
| 107 | $mem mw = $write_set_pop();
|
|---|
| 108 |
|
|---|
| 109 | reads[tid] = mr;
|
|---|
| 110 | writes[tid] = mw;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | void check_data_race() {
|
|---|
| 114 | for (int i = 0; i < NTHREADS; i++)
|
|---|
| 115 | for (int j = i+1; j < NTHREADS; j++) {
|
|---|
| 116 | $mem mr_i = reads[i];
|
|---|
| 117 | $mem mw_j = writes[j];
|
|---|
| 118 | $mem out_i, out_j;
|
|---|
| 119 | _Bool no_race = $mem_no_intersect(mr_i, mw_j, &out_i, &out_j);
|
|---|
| 120 |
|
|---|
| 121 | if (no_race)
|
|---|
| 122 | continue;
|
|---|
| 123 | $assert(no_race, "%p read by thread %d intersects %p written by thread %d\n",
|
|---|
| 124 | out_i, i, out_j, j);
|
|---|
| 125 | }
|
|---|
| 126 | }
|
|---|