Skip to content
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

Use thrust::inclusive_scan for 1D cumsum/cumprod #742

Merged
merged 1 commit into from
Apr 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/THC/THCTensorMathScan.cu
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "THCReduce.cuh"
#include "THCNumerics.cuh"
#include "THCTensorMathReduce.cuh"
#include <thrust/scan.h>
#include <thrust/execution_policy.h>

/* Perform an inclusive scan along an outer dimension of a tensor.
*
Expand All @@ -20,8 +22,8 @@
*/
template<typename T, class BinaryOp>
__global__ void THCTensor_kernel_scanOuterDim(T *tgt_, T *src_,
unsigned num_orows, unsigned num_irows, unsigned row_size,
T init, BinaryOp binary_op)
unsigned num_orows, unsigned num_irows, unsigned row_size,
T init, BinaryOp binary_op)
{
for (unsigned orow = blockIdx.x; orow < num_orows; orow += gridDim.x) {
for (unsigned irow = blockIdx.y * blockDim.x + threadIdx.x; irow < num_irows; irow += gridDim.y * blockDim.x) {
Expand Down Expand Up @@ -52,8 +54,8 @@ __global__ void THCTensor_kernel_scanOuterDim(T *tgt_, T *src_,
*/
template<typename T, int num_threads_x, int num_threads_y, class BinaryFunction>
__global__ void THCTensor_kernel_scanInnermostDim(T *tgt_, T *src_,
unsigned num_rows, unsigned row_size,
T init, BinaryFunction binary_op)
unsigned num_rows, unsigned row_size,
T init, BinaryFunction binary_op)
{
__shared__ T sbuf[num_threads_y][2 * num_threads_x];

Expand Down
35 changes: 33 additions & 2 deletions lib/THC/generic/THCTensorMathScan.cu
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@
#define THC_GENERIC_FILE "generic/THCTensorMathScan.cu"
#else

#ifndef THC_REAL_IS_HALF
template<class BinaryFunction>
__host__ void THCTensor_(scanThrust)(
THCState *state,
THCTensor *dst,
THCTensor *src,
BinaryFunction binary_op)
{
THCThrustAllocator thrustAlloc(state);
thrust::device_ptr<real> src_data(THCTensor_(data)(state, src));
thrust::device_ptr<real> dst_data(THCTensor_(data)(state, dst));
ptrdiff_t size = THCTensor_(nElement)(state, src);
thrust::inclusive_scan(
#if CUDA_VERSION >= 7000
thrust::cuda::par(thrustAlloc).on(THCState_getCurrentStream(state)),
#endif
src_data, src_data + size, dst_data,
binary_op);
}
#endif

template<class BinaryOp>
__host__ void THCTensor_(scanOuterDim)(THCState *state, THCTensor *tgt,
THCTensor *src, long dimension,
Expand Down Expand Up @@ -57,12 +78,22 @@ template<class BinaryFunction>
void THCTensor_(scanDim)(THCState *state, THCTensor *self_, THCTensor *src,
long dimension, real init, BinaryFunction binary_op)
{
THCTensor_(resizeAs)(state, self_, src);
// "init" must be the identity element for binary_op
int ndim = THCTensor_(nDimension)(state, src);
THArgCheck(dimension >= 0 && dimension < ndim, 3, "dimension %d out of range",
dimension + TH_INDEX_BASE);

THCTensor_(resizeAs)(state, self_, src);
THCTensor *self = THCTensor_(newContiguous)(state, self_);
src = THCTensor_(newContiguous)(state, src);

if (dimension == THCTensor_(nDimension)(state, src) - 1) {
#ifndef THC_REAL_IS_HALF
if (ndim == 1) {
// thrust does not take an "init"
THCTensor_(scanThrust)(state, self, src, binary_op);
} else
#endif
if (dimension == ndim - 1) {
THCTensor_(scanInnermostDim)(state, self, src, init, binary_op);
} else {
THCTensor_(scanOuterDim)(state, self, src, dimension, init, binary_op);
Expand Down