From 9cc25297f8e64503345765f1247af98628d70631 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Thu, 11 Jul 2019 01:06:39 +0800 Subject: [PATCH 1/5] Add support for complex vectors to CJ, part of #3690 --- NEWS.md | 2 +- inst/tests/tests.Rraw | 22 +++++++++++++--------- man/J.Rd | 3 +++ src/cj.c | 15 +++++++++++++++ 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/NEWS.md b/NEWS.md index 10e3d5d5ea..4e901d3a1f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -94,7 +94,7 @@ 16. `as.data.table` now unpacks columns in a `data.frame` which are themselves a `data.frame`. This need arises when parsing JSON, a corollary in [#3369](https://github.com/Rdatatable/data.table/issues/3369#issuecomment-462662752). `data.table` does not allow columns to be objects which themselves have columns (such as `matrix` and `data.frame`), unlike `data.frame` which does. Bug fix 19 in v1.12.2 (see below) added a helpful error (rather than segfault) to detect such invalid `data.table`, and promised that `as.data.table()` would unpack these columns in the next release (i.e. this release) so that the invalid `data.table` is not created in the first place. -17. `CJ` has been ported to C and parallelized, thanks to a PR by Michael Chirico, [#3596](https://github.com/Rdatatable/data.table/pull/3596). All types benefit, and as in many `data.table` operations, factors benefit more than character. +17. `CJ` has been ported to C and parallelized, thanks to a PR by Michael Chirico, [#3596](https://github.com/Rdatatable/data.table/pull/3596). All types benefit (including newly supported complex, part of [#3690](https://github.com/Rdatatable/data.table/issues/3690)), and as in many `data.table` operations, factors benefit more than character. ```R # default 4 threads on a laptop with 16GB RAM and 8 logical CPU diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 1c720d4de9..17fe523193 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -2809,23 +2809,27 @@ xx <- factor(letters[1:2], ordered=TRUE) yy <- sample(2L) yy_sort = base::sort.int(yy) old = options(datatable.CJ.names=FALSE) -test(996.1, CJ(xx, yy), setkey(data.table(rep(xx, each=2L), rep(yy_sort, 2L)))) -test(996.2, CJ(a = xx, yy), setkey(data.table(a = rep(xx, each=2L), V2 = rep(yy_sort, 2L)))) +test(996.01, CJ(xx, yy), setkey(data.table(rep(xx, each=2L), rep(yy_sort, 2L)))) +test(996.02, CJ(a = xx, yy), setkey(data.table(a = rep(xx, each=2L), V2 = rep(yy_sort, 2L)))) options(datatable.CJ.names=TRUE) -test(996.3, CJ(xx, yy), setkey(data.table(xx = rep(xx, each=2L), yy = rep(yy_sort, 2L)))) +test(996.03, CJ(xx, yy), setkey(data.table(xx = rep(xx, each=2L), yy = rep(yy_sort, 2L)))) options(old) # #3597 -- CJ properly informs sorted can't apply to list input -test(996.4, CJ(list(1:2, 3L)), error = "non-atomic, which can't be sorted") -test(996.5, CJ(list(1:2, 3), 4:6, sorted = FALSE), +test(996.04, CJ(list(1:2, 3L)), error = "non-atomic, which can't be sorted") +test(996.05, CJ(list(1:2, 3), 4:6, sorted = FALSE), data.table(V1 = list(1:2, 1:2, 1:2, 3, 3, 3), V2 = rep(4:6, 2L))) -test(996.6, CJ(4:6, list(1:2, 3), sorted = FALSE), +test(996.06, CJ(4:6, list(1:2, 3), sorted = FALSE), data.table(V1 = rep(4:6, each = 2L), V2 = rep(list(1:2, 3), 3L))) -test(996.7, CJ(1:2, list(1:2, 3), 4:5, sorted = FALSE), +test(996.07, CJ(1:2, list(1:2, 3), 4:5, sorted = FALSE), data.table(V1 = rep(1:2, each = 4L), V2 = rep(rep(list(1:2, 3), each = 2L), 2L), V3 = rep(4:5, 4L))) -test(996.8, CJ(expression(1)), error = "element 1 is non-atomic") -test(996.9, CJ(expression(2), 3, sorted = FALSE), error = "Type 'expression' not supported") +test(996.08, CJ(expression(1)), error = "element 1 is non-atomic") +test(996.09, CJ(expression(2), 3, sorted = FALSE), error = "Type 'expression' not supported") +## complex input support (can't handle sorted yet) +test(996.10, CJ(z = 0:1 + (0:1)*1i, b = 1:3, sorted = FALSE), + data.table(z = rep(0:1, each=3L) + rep(0:1, each=3L)*1i, b = rep(1:3, 2))) + # That CJ orders NA consistently with setkey and historically, now it doesn't use setkey. # NA must always come first in data.table throughout, since binary search relies on that internally. diff --git a/man/J.Rd b/man/J.Rd index 1d10b2976f..914ca07f2f 100644 --- a/man/J.Rd +++ b/man/J.Rd @@ -56,6 +56,9 @@ x = c(1, 1, 2) y = c(4, 6, 4) CJ(x, y) # output columns are automatically named 'x' and 'y' CJ(x, y, unique=TRUE) # unique(x) and unique(y) are computed automatically + +z = 0:1 + (0:1)*1i +CJ(x, z, sorted = FALSE) # support for sorting complex is not yet implemented } \keyword{ data } diff --git a/src/cj.c b/src/cj.c index 93a8294fab..3cf2d5feb4 100644 --- a/src/cj.c +++ b/src/cj.c @@ -1,4 +1,5 @@ #include "data.table.h" +#include SEXP cj(SEXP base_list) { int ncol = LENGTH(base_list); @@ -47,6 +48,20 @@ SEXP cj(SEXP base_list) { memcpy(targetP + i*blocklen, targetP, blocklen*sizeof(double)); } } break; + case CPLXSXP: { + const double complex *restrict sourceP = (double complex *)COMPLEX(source); + double complex *restrict targetP = (double complex *)COMPLEX(target); + #pragma omp parallel for num_threads(getDTthreads()) + for (int i=0; i Date: Thu, 11 Jul 2019 07:41:41 +0800 Subject: [PATCH 2/5] add test for coverage? --- inst/tests/tests.Rraw | 2 ++ 1 file changed, 2 insertions(+) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 17fe523193..28c30264ce 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -2829,6 +2829,8 @@ test(996.09, CJ(expression(2), 3, sorted = FALSE), error = "Type 'expression' no ## complex input support (can't handle sorted yet) test(996.10, CJ(z = 0:1 + (0:1)*1i, b = 1:3, sorted = FALSE), data.table(z = rep(0:1, each=3L) + rep(0:1, each=3L)*1i, b = rep(1:3, 2))) +test(996.11, CJ(b = 1:3, z = 0:1 + (0:1)*1i, sorted = FALSE), + data.table(b = rep(1:3, each = 2L), z = rep(0:1, 3) + rep(0:1, 3)*1i)) # That CJ orders NA consistently with setkey and historically, now it doesn't use setkey. From f06cc58d80810cfed5063ad2681112831b3eaa79 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Sun, 14 Jul 2019 04:08:49 +0800 Subject: [PATCH 3/5] move to Rcomplex API --- src/cj.c | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/src/cj.c b/src/cj.c index 3cf2d5feb4..93a8294fab 100644 --- a/src/cj.c +++ b/src/cj.c @@ -1,5 +1,4 @@ #include "data.table.h" -#include SEXP cj(SEXP base_list) { int ncol = LENGTH(base_list); @@ -48,20 +47,6 @@ SEXP cj(SEXP base_list) { memcpy(targetP + i*blocklen, targetP, blocklen*sizeof(double)); } } break; - case CPLXSXP: { - const double complex *restrict sourceP = (double complex *)COMPLEX(source); - double complex *restrict targetP = (double complex *)COMPLEX(target); - #pragma omp parallel for num_threads(getDTthreads()) - for (int i=0; i Date: Sun, 14 Jul 2019 04:10:56 +0800 Subject: [PATCH 4/5] move to Rcomplex API (seriously though) --- src/cj.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/cj.c b/src/cj.c index 93a8294fab..88ea78d7e4 100644 --- a/src/cj.c +++ b/src/cj.c @@ -46,6 +46,19 @@ SEXP cj(SEXP base_list) { for (int i=1; i Date: Sun, 14 Jul 2019 04:34:11 +0800 Subject: [PATCH 5/5] bad copypasta --- src/cj.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cj.c b/src/cj.c index 88ea78d7e4..ab1576b29a 100644 --- a/src/cj.c +++ b/src/cj.c @@ -46,6 +46,7 @@ SEXP cj(SEXP base_list) { for (int i=1; i