Skip to content

Commit

Permalink
Merge pull request #120 from torch/goodies
Browse files Browse the repository at this point in the history
adds Sort, revamps TensorMath, adds masked* operations
  • Loading branch information
soumith committed Apr 1, 2015
2 parents 4fdbf0c + 41006b8 commit c9b2253
Show file tree
Hide file tree
Showing 30 changed files with 4,016 additions and 2,515 deletions.
1 change: 1 addition & 0 deletions FFI.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ typedef struct THCState
{
struct THCRNGState* rngState;
struct THCBlasState* blasState;
struct cudaDeviceProp* deviceProperties;
} THCState;
typedef struct THCudaStorage
Expand Down
15 changes: 0 additions & 15 deletions Tensor.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,6 @@

/* everything is as the generic Storage.c, except few things (see below) */

static void THCudaTensor_maskedFill(THCState *state, THCudaTensor *tensor, THByteTensor *mask, float value)
{
THError("not yet implemented for CUDA");
}

static void THCudaTensor_maskedCopy(THCState *state, THCudaTensor *tensor, THByteTensor *mask, THCudaTensor* src)
{
THError("not yet implemented for CUDA");
}

void THCudaTensor_maskedSelect(THCState *state, THCudaTensor *tensor, THCudaTensor* src, THByteTensor *mask)
{
THError("not yet implemented for CUDA");
}

#define real float
#define Real Cuda

Expand Down
30 changes: 30 additions & 0 deletions Tensor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,44 @@ local function Tensor__float(self,type)
return self:type('torch.FloatTensor')
end

local function Tensor__byte(self,type)
return self:type('torch.ByteTensor')
end

local function Tensor__char(self,type)
return self:type('torch.CharTensor')
end

local function Tensor__int(self,type)
return self:type('torch.IntTensor')
end

local function Tensor__short(self,type)
return self:type('torch.ShortTensor')
end

local function Tensor__Long(self,type)
return self:type('torch.LongTensor')
end

rawset(torch.getmetatable('torch.DoubleTensor'), 'cuda', Tensor__cuda)
rawset(torch.getmetatable('torch.FloatTensor'), 'cuda', Tensor__cuda)
rawset(torch.getmetatable('torch.ByteTensor'), 'cuda', Tensor__cuda)
rawset(torch.getmetatable('torch.CharTensor'), 'cuda', Tensor__cuda)
rawset(torch.getmetatable('torch.IntTensor'), 'cuda', Tensor__cuda)
rawset(torch.getmetatable('torch.ShortTensor'), 'cuda', Tensor__cuda)
rawset(torch.getmetatable('torch.LongTensor'), 'cuda', Tensor__cuda)
rawset(torch.getmetatable('torch.CudaTensor'), 'cuda', Tensor__cuda)

rawset(torch.getmetatable('torch.CudaTensor'), 'type', Tensor__type)
rawset(torch.getmetatable('torch.CudaTensor'), 'typeAs', Tensor__typeAs)
rawset(torch.getmetatable('torch.CudaTensor'), 'double', Tensor__double)
rawset(torch.getmetatable('torch.CudaTensor'), 'float', Tensor__float)
rawset(torch.getmetatable('torch.CudaTensor'), 'byte', Tensor__byte)
rawset(torch.getmetatable('torch.CudaTensor'), 'char', Tensor__char)
rawset(torch.getmetatable('torch.CudaTensor'), 'int', Tensor__int)
rawset(torch.getmetatable('torch.CudaTensor'), 'short', Tensor__short)
rawset(torch.getmetatable('torch.CudaTensor'), 'long', Tensor__long)

do
local metatable = torch.getmetatable('torch.CudaTensor')
Expand Down
20 changes: 19 additions & 1 deletion TensorMath.lua
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ end

local function lastdim(argn)
return function(arg)
return string.format("THCudaTensor_nDimension(%s)", arg.args[argn]:carg())
return string.format("THCudaTensor_nDimension(cutorch_getstate(L), %s)", arg.args[argn]:carg())
end
end

Expand Down Expand Up @@ -289,6 +289,24 @@ wrap("addcdiv",
{name=Tensor},
{name=Tensor}})

wrap("maskedFill",
cname("maskedFill"),
{{name=Tensor, returned=true, method={default='nil'}},
{name=Tensor},
{name=real}})

wrap("maskedCopy",
cname("maskedCopy"),
{{name=Tensor, returned=true, method={default='nil'}},
{name=Tensor},
{name=Tensor}})

wrap("maskedSelect",
cname("maskedSelect"),
{{name=Tensor, returned=true, default=true},
{name=Tensor},
{name=Tensor}})

do
local Tensor = Tensor
local real = real
Expand Down
10 changes: 10 additions & 0 deletions init.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ static int cutorch_getDeviceCount(lua_State *L)
return 1;
}

static int cutorch_getMemoryUsage(lua_State *L) {
size_t freeBytes = 0;
size_t totalBytes = 0;
THCudaCheck(cudaMemGetInfo(&freeBytes, &totalBytes));
lua_pushnumber(L, freeBytes);
lua_pushnumber(L, totalBytes);
return 2;
}

static int cutorch_setDevice(lua_State *L)
{
THCState *state = cutorch_getstate(L);
Expand Down Expand Up @@ -158,6 +167,7 @@ static const struct luaL_Reg cutorch_stuff__ [] = {
{"deviceReset", cutorch_deviceReset},
{"getDeviceCount", cutorch_getDeviceCount},
{"getDeviceProperties", cutorch_getDeviceProperties},
{"getMemoryUsage", cutorch_getMemoryUsage},
{"setDevice", cutorch_setDevice},
{"seed", cutorch_seed},
{"seedAll", cutorch_seedAll},
Expand Down
26 changes: 25 additions & 1 deletion lib/THC/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
SET(src
THCGeneral.c THCStorage.c THCStorageCopy.c THCTensor.c THCTensorCopy.c)

SET(src-cuda THC.cu)
SET(src-cuda
THCReduceApplyUtils.cu
THCBlas.cu
THCStorage.cu
THCStorageCopy.cu
THCTensor.cu
THCTensorCopy.cu
THCTensorMath2.cu
THCTensorMathBlas.cu
THCTensorMathCompare.cu
THCTensorMathCompareT.cu
THCTensorMath.cu
THCTensorMathPairwise.cu
THCTensorMathPointwise.cu
THCTensorMathScan.cu
THCTensorMathTransformReduce.cu
THCTensorMasked.cu
THCTensorIndex.cu
THCTensorConv.cu
THCTensorRandom.cu
THCApply.cu
)

CUDA_ADD_LIBRARY(THC SHARED ${src} ${src-cuda})
CUDA_ADD_CUBLAS_TO_TARGET(THC)
Expand All @@ -23,4 +44,7 @@ INSTALL(FILES
THCTensorRandom.h
THCTensorMath.h
THCTensorConv.h
THCApply.cuh
THCReduce.cuh
THCReduceApplyUtils.cuh
DESTINATION "${Torch_INSTALL_INCLUDE_SUBDIR}/THC")
11 changes: 0 additions & 11 deletions lib/THC/THC.cu

This file was deleted.

10 changes: 10 additions & 0 deletions lib/THC/THCApply.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "THCApply.cuh"

// Implementation of copyIgnoringOverlaps, defined after pointwiseApply2.
void THCudaTensor_copyIgnoringOverlaps(THCState* state,
THCudaTensor* dst,
THCudaTensor* src) {
THCudaTensor_pointwiseApply2(state, dst, src, CopyOp<float>(),
ReadOnly, // ignore overwrites
ReadOnly);
}
Loading

0 comments on commit c9b2253

Please sign in to comment.