Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Torch-compatible GPU memory allocator
- Loading branch information
Showing
6 changed files
with
124 additions
and
14 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,53 @@ | ||
require 'cutorch' | ||
|
||
local cv = require 'cv' | ||
require 'cv.cudawarping' | ||
require 'cv.highgui' | ||
require 'cv.imgcodecs' | ||
require 'cv.cudawarping' -- cv.cuda.resize | ||
require 'cv.imgproc' -- cv.resize | ||
require 'cv.highgui' -- cv.imshow | ||
require 'cv.imgcodecs' -- cv.imread | ||
|
||
if not arg[1] then | ||
print('Usage: `th demo/cuda/resize.lua path-to-image`') | ||
print('Now using demo/data/lena.jpg') | ||
print('') | ||
end | ||
|
||
local img = cv.imread {arg[1] or 'demo/data/lena.jpg', cv.IMREAD_COLOR} | ||
local imgCUDA = img:float():cuda() / 255 | ||
local resized = cv.cuda.resize{imgCUDA, {1024, 768}} | ||
local img = cv.imread {arg[1] or 'demo/data/lena.jpg', cv.IMREAD_COLOR}:float() / 255 | ||
local imgCUDA = img:cuda() | ||
|
||
require 'xlua' | ||
local numIterations, dsize = 2000, {1024, 768} | ||
|
||
local resized = cv.resize{img, dsize} | ||
local resizedCUDA = torch.CudaTensor(resized:size()) | ||
|
||
print(('Doing `cv.resize{}` (CPU) %d times (OpenCV\'s number of threads is %d):') | ||
:format(numIterations, cv.getNumThreads{})) | ||
local timer = torch.Timer() | ||
|
||
for iter = 1,numIterations do | ||
cv.resize{img, dsize, dst=resized} | ||
if iter % 100 == 0 then xlua.progress(iter, numIterations) end | ||
end | ||
local timeCPU = timer:time().real | ||
|
||
print(('Doing `cv.cuda.resize{}` (GPU) %d times:'):format(numIterations)) | ||
timer:reset() | ||
|
||
for iter = 1,numIterations do | ||
cv.cuda.resize{imgCUDA, dsize, dst=resizedCUDA} | ||
if iter % 100 == 0 then xlua.progress(iter, numIterations) end | ||
end | ||
cutorch.synchronize() | ||
local timeGPU = timer:time().real | ||
|
||
-- a technical test to check if Tensor freeing works without errors | ||
for iter = 1,40 do | ||
local _ = cv.cuda.resize{imgCUDA, dsize} | ||
end | ||
collectgarbage() | ||
|
||
cv.imshow{"Resized to 1024x768", resized:float()} | ||
local title = | ||
("Lena resized to 1024x768 by your GPU (%.3f times faster than CPU)"):format(timeCPU / timeGPU) | ||
cv.imshow{title, resizedCUDA:float()} | ||
cv.waitKey{0} |
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 |
---|---|---|
|
@@ -31,6 +31,7 @@ struct TensorArray { | |
}; | ||
|
||
void initAllocator(); | ||
void initAllocatorCUDA(struct cutorchInfo info); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
shrubb
Author
Contributor
|
||
|
||
void *malloc(size_t size); | ||
void free(void *ptr); | ||
|
@@ -333,6 +334,10 @@ end | |
--- ***************** Tensor <=> Mat conversion ***************** | ||
|
||
C.initAllocator() | ||
if CUDACommon_C then | ||
cv.cuda = cv.cuda or require 'cv._env_cuda' | ||
CUDACommon_C.initAllocatorCUDA(cv.cuda._info()) | ||
end | ||
|
||
local tensor_CV_code_by_letter = { | ||
[ 66] = cv.CV_8U , -- B : Byte | ||
|
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
We have a problem when installing torch-opencv wrapper when using non GPU version of openCV.
I guess this is due to line 34 of init.lua that define a specific CUDA function while not being in a specific CUDA code part. (At runtime we get an error saying that luaJIT can't find initAllocatorCUDA symbol)