|
| 1 | +#include "data.table.h" |
| 2 | + |
| 3 | +void benchmark_omp_flag(const int variant, int len, int halt, int th, int *cnt) { |
| 4 | + if (variant == 1) { |
| 5 | + // plain as is now |
| 6 | + bool skip = false; |
| 7 | + #pragma omp parallel for num_threads(th) |
| 8 | + for (int i=0; i<len; i++) { |
| 9 | + if (skip) |
| 10 | + continue; |
| 11 | + int tid = omp_get_thread_num(); |
| 12 | + cnt[tid]++; |
| 13 | + if (i == halt) |
| 14 | + skip = true; |
| 15 | + } |
| 16 | + } else if (variant == 2) { |
| 17 | + // volatile |
| 18 | + volatile bool skip = false; |
| 19 | + #pragma omp parallel for num_threads(th) |
| 20 | + for (int i=0; i<len; i++) { |
| 21 | + if (skip) |
| 22 | + continue; |
| 23 | + int tid = omp_get_thread_num(); |
| 24 | + cnt[tid]++; |
| 25 | + if (i == halt) |
| 26 | + skip = true; |
| 27 | + } |
| 28 | + } else if (variant == 3) { |
| 29 | + // volatile and shared |
| 30 | + volatile bool skip = false; |
| 31 | + #pragma omp parallel for num_threads(th) shared(skip) |
| 32 | + for (int i=0; i<len; i++) { |
| 33 | + if (skip) |
| 34 | + continue; |
| 35 | + int tid = omp_get_thread_num(); |
| 36 | + cnt[tid]++; |
| 37 | + if (i == halt) |
| 38 | + skip = true; |
| 39 | + } |
| 40 | + } else if (variant == 4) { |
| 41 | + // atomic write |
| 42 | + bool skip = false; |
| 43 | + #pragma omp parallel for num_threads(th) shared(skip) |
| 44 | + for (int i=0; i<len; i++) { |
| 45 | + if (skip) |
| 46 | + continue; |
| 47 | + int tid = omp_get_thread_num(); |
| 48 | + cnt[tid]++; |
| 49 | + if (i == halt) { |
| 50 | + #pragma omp atomic write |
| 51 | + skip = true; |
| 52 | + } |
| 53 | + } |
| 54 | + } else if (variant == 5) { |
| 55 | + // atomic read and atomic write |
| 56 | + bool skip = false; |
| 57 | + #pragma omp parallel for num_threads(th) shared(skip) |
| 58 | + for (int i=0; i<len; i++) { |
| 59 | + bool local_skip; |
| 60 | + #pragma omp atomic read |
| 61 | + local_skip = skip; |
| 62 | + if (local_skip) |
| 63 | + continue; |
| 64 | + int tid = omp_get_thread_num(); |
| 65 | + cnt[tid]++; |
| 66 | + if (i == halt) { |
| 67 | + #pragma omp atomic write |
| 68 | + skip = true; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +SEXP benchmark_omp_flagR(SEXP variant, SEXP len, SEXP halt, SEXP th) { |
| 75 | + SEXP ans = PROTECT(allocVector(INTSXP, INTEGER_RO(th)[0])); |
| 76 | + for (int i=0; i<INTEGER_RO(th)[0]; i++) INTEGER(ans)[i] = 0; |
| 77 | + benchmark_omp_flag(INTEGER_RO(variant)[0], INTEGER_RO(len)[0], INTEGER_RO(halt)[0]-1, INTEGER_RO(th)[0], INTEGER(ans)); |
| 78 | + UNPROTECT(1); |
| 79 | + return(ans); |
| 80 | +} |
0 commit comments