-
Notifications
You must be signed in to change notification settings - Fork 995
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
introduce fexplode function #4156
Draft
MichaelChirico
wants to merge
20
commits into
master
Choose a base branch
from
unnest
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 8 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
f88e69b
prod_int function (probably not safe enough to use
ade2a94
initial working version of unnest
6777a4a
make note of incorrect cartesian unnesting
2b34cac
initial progress migrating to simpler signature
fa93bee
extract CJ workhorse to power unnesting
e1af1ef
move unnest into cj script (similar logic)
71f72c1
fix some bugs, but giving up on this approach for now
eaf56c6
use rbindlist() and cj() internally
8933477
no need for splitting out cj logic anymore
488f679
reduce diff slightly
b2214fe
validate input, add comments
c263976
conditional parallelism
c3d9096
fix rownames issue, add man page
99a0487
bad copypasta
f876968
typo in man
4cfc5e6
rename -> funnest, add first batch of tests
565051b
Merge branch 'master' into unnest
0df5db8
tests numbers
fbbccd2
missed rename in man
d161dfd
coverage; fix test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,235 @@ | ||
#include "data.table.h" | ||
|
||
// workhorse for cycling a vector in the correct pattern, | ||
// essentially rep(x, m, each = n) for appropriate m,n at each j | ||
void cycle_vector(SEXP source, SEXP target, int j, int eachrep, int nrow) { | ||
int thislen = LENGTH(source); | ||
int blocklen = thislen*eachrep; | ||
int ncopy = nrow/blocklen; | ||
switch(TYPEOF(source)) { | ||
case LGLSXP: | ||
case INTSXP: { | ||
const int *restrict sourceP = INTEGER(source); | ||
int *restrict targetP = INTEGER(target); | ||
#pragma omp parallel for num_threads(getDTthreads()) if (nrow >= OMP_MIN_VALUE) | ||
// default static schedule so two threads won't write to same cache line in last column | ||
// if they did write to same cache line (and will when last column's thislen is small) there's no correctness issue | ||
for (int i=0; i<thislen; ++i) { | ||
const int item = sourceP[i]; | ||
const int end = (i+1)*eachrep; | ||
for (int j=i*eachrep; j<end; ++j) | ||
targetP[j] = item; // no div, mod or read ops inside loop; just rep a const contiguous write | ||
} | ||
#pragma omp parallel for num_threads(getDTthreads()) if (nrow >= OMP_MIN_VALUE) | ||
for (int i=1; i<ncopy; ++i) { | ||
memcpy(targetP + i*blocklen, targetP, blocklen*sizeof(int)); | ||
} | ||
} break; | ||
case REALSXP: { | ||
const double *restrict sourceP = REAL(source); | ||
double *restrict targetP = REAL(target); | ||
#pragma omp parallel for num_threads(getDTthreads()) if (nrow >= OMP_MIN_VALUE) | ||
for (int i=0; i<thislen; ++i) { | ||
const double item = sourceP[i]; | ||
const int end=(i+1)*eachrep; | ||
for (int j=i*eachrep; j<end; ++j) targetP[j] = item; | ||
} | ||
#pragma omp parallel for num_threads(getDTthreads()) if (nrow >= OMP_MIN_VALUE) | ||
for (int i=1; i<ncopy; ++i) { | ||
memcpy(targetP + i*blocklen, targetP, blocklen*sizeof(double)); | ||
} | ||
} break; | ||
case CPLXSXP: { | ||
const Rcomplex *restrict sourceP = COMPLEX(source); | ||
Rcomplex *restrict targetP = COMPLEX(target); | ||
#pragma omp parallel for num_threads(getDTthreads()) if (nrow >= OMP_MIN_VALUE) | ||
for (int i=0; i<thislen; ++i) { | ||
const Rcomplex item = sourceP[i]; | ||
const int end=(i+1)*eachrep; | ||
for (int j=i*eachrep; j<end; ++j) targetP[j] = item; | ||
} | ||
#pragma omp parallel for num_threads(getDTthreads()) if (nrow >= OMP_MIN_VALUE) | ||
for (int i=1; i<ncopy; ++i) { | ||
memcpy(targetP + i*blocklen, targetP, blocklen*sizeof(Rcomplex)); | ||
} | ||
} break; | ||
case STRSXP: { | ||
const SEXP *sourceP = STRING_PTR(source); | ||
int start = 0; | ||
for (int i=0; i<ncopy; ++i) { | ||
for (int j=0; j<thislen; ++j) { | ||
const SEXP item = sourceP[j]; | ||
const int end = start+eachrep; | ||
for (int k=start; k<end; ++k) SET_STRING_ELT(target, k, item); // no div, mod, or read-API call to STRING_ELT | ||
start = end; | ||
} | ||
} | ||
} break; | ||
case VECSXP: { | ||
const SEXP *sourceP = VECTOR_PTR(source); | ||
int start = 0; | ||
for (int i=0; i<ncopy; ++i) { | ||
for (int j=0; j<thislen; ++j) { | ||
const SEXP item = sourceP[j]; | ||
const int end = start+eachrep; | ||
for (int k=start; k<end; ++k) SET_VECTOR_ELT(target, k, item); | ||
start = end; | ||
} | ||
} | ||
} break; | ||
default: | ||
error(_("Type '%s' not supported by for cross-join."), type2char(TYPEOF(source))); | ||
} | ||
} | ||
|
||
SEXP cj(SEXP base_list) { | ||
int ncol = LENGTH(base_list); | ||
SEXP out = PROTECT(allocVector(VECSXP, ncol)); | ||
int nrow = 1; | ||
// already confirmed to be less than .Machine$integer.max at R level | ||
for (int j=0; j<ncol; ++j) nrow *= length(VECTOR_ELT(base_list, j)); | ||
// start with double to allow overflow | ||
double nrow_dbl=1; | ||
int nrow; | ||
for (int j=0; j<ncol; ++j) nrow_dbl *= length(VECTOR_ELT(base_list, j)); | ||
if (nrow_dbl > INT_MAX) { | ||
error(_("Cross product of elements provided to CJ() would result in %.0f rows which exceeds .Machine$integer.max == %d"), nrow_dbl, INT_MAX); | ||
} else { | ||
nrow = (int) nrow_dbl; | ||
} | ||
int eachrep = 1; | ||
for (int j=ncol-1; j>=0; --j) { | ||
SEXP source = VECTOR_ELT(base_list, j), target; | ||
SET_VECTOR_ELT(out, j, target=allocVector(TYPEOF(source), nrow)); | ||
copyMostAttrib(source, target); // includes levels of factors, integer64, custom classes, etc | ||
if (nrow==0) continue; // one or more columns are empty so the result will be empty, #2511 | ||
int thislen = LENGTH(source); | ||
int blocklen = thislen*eachrep; | ||
int ncopy = nrow/blocklen; | ||
switch(TYPEOF(source)) { | ||
case LGLSXP: | ||
case INTSXP: { | ||
const int *restrict sourceP = INTEGER(source); | ||
int *restrict targetP = INTEGER(target); | ||
#pragma omp parallel for num_threads(getDTthreads()) | ||
// default static schedule so two threads won't write to same cache line in last column | ||
// if they did write to same cache line (and will when last column's thislen is small) there's no correctness issue | ||
for (int i=0; i<thislen; ++i) { | ||
const int item = sourceP[i]; | ||
const int end = (i+1)*eachrep; | ||
for (int j=i*eachrep; j<end; ++j) targetP[j] = item; // no div, mod or read ops inside loop; just rep a const contiguous write | ||
} | ||
#pragma omp parallel for num_threads(getDTthreads()) | ||
for (int i=1; i<ncopy; ++i) { | ||
memcpy(targetP + i*blocklen, targetP, blocklen*sizeof(int)); | ||
} | ||
} break; | ||
case REALSXP: { | ||
const double *restrict sourceP = REAL(source); | ||
double *restrict targetP = REAL(target); | ||
#pragma omp parallel for num_threads(getDTthreads()) | ||
for (int i=0; i<thislen; ++i) { | ||
const double item = sourceP[i]; | ||
const int end=(i+1)*eachrep; | ||
for (int j=i*eachrep; j<end; ++j) targetP[j] = item; | ||
} | ||
#pragma omp parallel for num_threads(getDTthreads()) | ||
for (int i=1; i<ncopy; ++i) { | ||
memcpy(targetP + i*blocklen, targetP, blocklen*sizeof(double)); | ||
} | ||
} break; | ||
case CPLXSXP: { | ||
const Rcomplex *restrict sourceP = COMPLEX(source); | ||
Rcomplex *restrict targetP = COMPLEX(target); | ||
#pragma omp parallel for num_threads(getDTthreads()) | ||
for (int i=0; i<thislen; ++i) { | ||
const Rcomplex item = sourceP[i]; | ||
const int end=(i+1)*eachrep; | ||
for (int j=i*eachrep; j<end; ++j) targetP[j] = item; | ||
} | ||
#pragma omp parallel for num_threads(getDTthreads()) | ||
for (int i=1; i<ncopy; ++i) { | ||
memcpy(targetP + i*blocklen, targetP, blocklen*sizeof(Rcomplex)); | ||
} | ||
cycle_vector(source, target, j, eachrep, nrow); | ||
eachrep *= LENGTH(source); | ||
} | ||
UNPROTECT(1); | ||
return out; | ||
} | ||
|
||
SEXP unnest(SEXP x, SEXP cols) { | ||
int k = LENGTH(cols); | ||
// nothing to unnest -- need to go further, just be sure to copy | ||
if (k == 0) | ||
return (duplicate(x)); | ||
int n = LENGTH(VECTOR_ELT(x, 0)); | ||
int p = LENGTH(x); | ||
|
||
if (TYPEOF(cols) != INTSXP) | ||
error(_("cols must be an integer vector, got %s"), type2char(TYPEOF(cols))); | ||
int *colp = INTEGER(cols); | ||
|
||
// ignore non-VECSXP elements of cols; use lcols and lk (list-only versions) | ||
int lcols[k], j; | ||
int lk=0; | ||
for (int i=0; i<k; i++) { | ||
j = colp[i]; | ||
if (j < 1 || j > p) | ||
error(_("cols to unnest must be in [1, ncol(x)=%d], but cols[%d]=%d"), p, i, j); | ||
switch(TYPEOF(VECTOR_ELT(x, j-1))) { | ||
case RAWSXP : | ||
case LGLSXP : | ||
case INTSXP : | ||
case REALSXP : | ||
case CPLXSXP : | ||
case STRSXP : break; | ||
case VECSXP : { | ||
lcols[lk++] = j-1; // move to 0-based | ||
} break; | ||
case STRSXP: { | ||
const SEXP *sourceP = STRING_PTR(source); | ||
int start = 0; | ||
for (int i=0; i<ncopy; ++i) { | ||
for (int j=0; j<thislen; ++j) { | ||
const SEXP item = sourceP[j]; | ||
const int end = start+eachrep; | ||
for (int k=start; k<end; ++k) SET_STRING_ELT(target, k, item); // no div, mod, or read-API call to STRING_ELT | ||
start = end; | ||
default: | ||
error(_("Unsupported type for unnesting: %s"), type2char(TYPEOF(VECTOR_ELT(x, j)))); | ||
} | ||
} | ||
if (lk == 0) | ||
return (duplicate(x)); | ||
|
||
int row_counts[n]; | ||
|
||
SEXP cj_rowwise = PROTECT(allocVector(VECSXP, n)); | ||
for (int i=0; i<n; i++) { | ||
SEXP nest_vals = PROTECT(allocVector(VECSXP, lk)); | ||
for (int j=0; j<lk; j++) | ||
SET_VECTOR_ELT(nest_vals, j, VECTOR_ELT(VECTOR_ELT(x, lcols[j]), i)); | ||
SET_VECTOR_ELT(cj_rowwise, i, cj(nest_vals)); | ||
row_counts[i] = LENGTH(VECTOR_ELT(VECTOR_ELT(cj_rowwise, i), 0)); | ||
UNPROTECT(1); | ||
} | ||
SEXP unnest_lcols = rbindlist(cj_rowwise, ScalarLogical(FALSE), ScalarLogical(FALSE), R_NilValue); | ||
UNPROTECT(1); | ||
int out_rows = LENGTH(VECTOR_ELT(unnest_lcols, 0)); | ||
|
||
SEXP ans = PROTECT(allocVector(VECSXP, p)); | ||
copyMostAttrib(x, ans); | ||
for (int j=0, lj=0; j<p; j++) { | ||
if (lj < lk && j == lcols[lj]) { // vec col: apply unnesting | ||
SET_VECTOR_ELT(ans, j, VECTOR_ELT(unnest_lcols, lj++)); | ||
} else { // non-vec col: simply copy | ||
int outi=0; | ||
SEXP xj = VECTOR_ELT(x, j), ansj; | ||
SET_VECTOR_ELT(ans, j, ansj=allocVector(TYPEOF(xj), out_rows)); | ||
switch(TYPEOF(xj)) { | ||
case RAWSXP: { | ||
const Rbyte *source = RAW(xj); | ||
Rbyte *target = RAW(ansj); | ||
for (int i=0; i<n; i++) { | ||
for(int repi=0; repi<row_counts[i]; repi++) { | ||
memcpy(target + outi + repi, &source[i], sizeof(Rbyte)); | ||
} | ||
outi += row_counts[i]; | ||
} | ||
} | ||
} break; | ||
case VECSXP: { | ||
const SEXP *sourceP = VECTOR_PTR(source); | ||
int start = 0; | ||
for (int i=0; i<ncopy; ++i) { | ||
for (int j=0; j<thislen; ++j) { | ||
const SEXP item = sourceP[j]; | ||
const int end = start+eachrep; | ||
for (int k=start; k<end; ++k) SET_VECTOR_ELT(target, k, item); | ||
start = end; | ||
} break; | ||
case LGLSXP: | ||
case INTSXP: { | ||
const int *source = INTEGER(xj); | ||
int *target = INTEGER(ansj); | ||
for (int i=0; i<n; i++) { | ||
for(int repi=0; repi<row_counts[i]; repi++) { | ||
memcpy(target + outi + repi, &source[i], sizeof(int)); | ||
} | ||
outi += row_counts[i]; | ||
} | ||
} break; | ||
case REALSXP: { | ||
const double *source = REAL(xj); | ||
double *target = REAL(ansj); | ||
for (int i=0; i<n; i++) { | ||
for(int repi=0; repi<row_counts[i]; repi++) { | ||
memcpy(target + outi + repi, &source[i], sizeof(double)); | ||
} | ||
outi += row_counts[i]; | ||
} | ||
} break; | ||
case CPLXSXP: { | ||
const Rcomplex *source = COMPLEX(xj); | ||
Rcomplex *target = COMPLEX(ansj); | ||
for (int i=0; i<n; i++) { | ||
for(int repi=0; repi<row_counts[i]; repi++) { | ||
memcpy(target + outi + repi, &source[i], sizeof(Rcomplex)); | ||
} | ||
outi += row_counts[i]; | ||
} | ||
} break; | ||
case STRSXP: { | ||
for (int i=0; i<n; i++) { | ||
for(int repi=0; repi<row_counts[i]; repi++) { | ||
SET_STRING_ELT(ansj, outi + repi, STRING_ELT(xj, i)); | ||
} | ||
outi += row_counts[i]; | ||
} | ||
} break; | ||
default: error(_("Unsupported column type %s"), type2char(TYPEOF(xj))); | ||
} | ||
} break; | ||
default: | ||
error(_("Type '%s' not supported by CJ."), type2char(TYPEOF(source))); | ||
copyMostAttrib(xj, ansj); | ||
} | ||
eachrep *= thislen; | ||
} | ||
UNPROTECT(1); | ||
return out; | ||
} | ||
|
||
// copy names | ||
SEXP ansNames; | ||
SEXP xNames = PROTECT(getAttrib(x, R_NamesSymbol)); | ||
setAttrib(ans, R_NamesSymbol, ansNames=allocVector(STRSXP, p)); | ||
for (int j=0; j<p; j++) { | ||
SET_STRING_ELT(ansNames, j, STRING_ELT(xNames, j)); | ||
} | ||
UNPROTECT(2); | ||
return ans; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ | |
#define omp_get_wtime() 0 | ||
#endif | ||
|
||
#define OMP_MIN_VALUE 1024 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
migrated this to C, epsilon more efficient but anyway cleaner to have more logic there; will also make it easier to switch to long vector support