Skip to content

Commit ee6fb9d

Browse files
committed
benchmarks
1 parent 55b0de6 commit ee6fb9d

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

R/utils.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,13 @@ formula_vars = function(f, x) { # .formula2varlist is not API and seems to have
227227
attr(terms, "term.labels")
228228
)
229229
}
230+
231+
omp_flags = function(variant, len, halt, th) {
232+
th = as.integer(th)
233+
halt = as.integer(halt)
234+
len = as.integer(len)
235+
variant = as.integer(variant)
236+
stopifnot(is.integer(th))
237+
stopifnot(th <= parallel::detectCores(), th > 0L)
238+
.Call("Cbenchmark_omp_flagR", variant, len, halt, th)
239+
}

src/data.table.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,3 +410,5 @@ SEXP dt_has_zlib(void);
410410
SEXP startsWithAny(SEXP, SEXP, SEXP);
411411
SEXP convertDate(SEXP, SEXP);
412412
SEXP fastmean(SEXP);
413+
414+
SEXP benchmark_omp_flagR(SEXP, SEXP, SEXP, SEXP);

src/init.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ R_CallMethodDef callMethods[] = {
160160
{"CmemcpyDTadaptive", (DL_FUNC)&memcpyDTadaptive, -1},
161161
{"Csetgrowable", (DL_FUNC)&setgrowable, -1},
162162
{"Cfrolladapt", (DL_FUNC)&frolladapt, -1},
163+
{"Cbenchmark_omp_flagR", (DL_FUNC)&benchmark_omp_flagR, -1},
163164
{NULL, NULL, 0}
164165
};
165166

src/omp-flags.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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

Comments
 (0)