Skip to content

Instantly share code, notes, and snippets.

@killeent
Created December 22, 2016 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save killeent/bcd13c48d5b894ce109ae90100004c17 to your computer and use it in GitHub Desktop.
Save killeent/bcd13c48d5b894ce109ae90100004c17 to your computer and use it in GitHub Desktop.
require 'torch'
require 'cutorch'
local counts = {2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192}
local trials = 10
print('Averages of', trials, 'trials used to generate results')
print("----------------------------------------------------------------")
-- 1D Tensors
for _, ct in ipairs(counts) do
local sum = 0
local sum2 = 0
for i = 1, trials do
local tensors = {}
for i = 1, ct do
table.insert(tensors, torch.CudaTensor(torch.random(1, 25)):uniform())
end
local timer = torch.Timer()
local catted = torch.cat(tensors, 1)
sum = sum + timer:time().real
local timer2 = torch.Timer()
local cloned = catted:clone()
sum2 = sum2 + timer2:time().real
end
print("CatArray for", ct, "1D Tensors took", sum / trials, "seconds. Clone:", sum2 / trials)
end
print("----------------------------------------------------------------")
-- 2D Tensors
for _, ct in ipairs(counts) do
local sum = 0
local sum2 = 0
for i = 1, trials do
local tensors = {}
local dim2 = torch.random(1, 25)
for i = 1, ct do
table.insert(tensors, torch.CudaTensor(torch.random(1, 25), dim2):uniform())
end
local timer = torch.Timer()
local catted = torch.cat(tensors, 1)
sum = sum + timer:time().real
local timer2 = torch.Timer()
local cloned = catted:clone()
sum2 = sum2 + timer2:time().real
end
print("CatArray for", ct, "2D Tensors along dim=1 took", sum / trials, "seconds. Clone:", sum2 / trials)
sum = 0
sum2 = 0
for i = 1, trials do
local tensors = {}
local dim1 = torch.random(1, 25)
for i = 1, ct do
table.insert(tensors, torch.CudaTensor(dim1, torch.random(1, 25)):uniform())
end
local timer = torch.Timer()
local catted = torch.cat(tensors, 2)
sum = sum + timer:time().real
local timer2 = torch.Timer()
local cloned = catted:clone()
sum2 = sum2 + timer2:time().real
end
print("CatArray for", ct, "2D Tensors along dim=2 took", sum / trials, "seconds. Clone:", sum2 / trials)
end
print("----------------------------------------------------------------")
-- 3D Tensors
for _, ct in ipairs(counts) do
local sum = 0
local sum2 = 0
for i = 1, trials do
local tensors = {}
local dim2 = torch.random(1, 25)
local dim3 = torch.random(1, 25)
for i = 1, ct do
table.insert(tensors, torch.CudaTensor(torch.random(1, 25), dim2, dim3):uniform())
end
local timer = torch.Timer()
local catted = torch.cat(tensors, 1)
sum = sum + timer:time().real
local timer2 = torch.Timer()
local cloned = catted:clone()
sum2 = sum2 + timer2:time().real
end
print("CatArray for", ct, "3D Tensors along dim=1 took", sum / trials, "seconds. Clone:", sum2 / trials)
sum = 0
sum2 = 0
for i = 1, trials do
local tensors = {}
local dim1 = torch.random(1, 25)
local dim3 = torch.random(1, 25)
for i = 1, ct do
table.insert(tensors, torch.CudaTensor(dim1, torch.random(1, 25), dim3):uniform())
end
local timer = torch.Timer()
local catted = torch.cat(tensors, 2)
sum = sum + timer:time().real
local timer2 = torch.Timer()
local cloned = catted:clone()
sum2 = sum2 + timer2:time().real
end
print("CatArray for", ct, "3D Tensors along dim=2 took", sum / trials, "seconds. Clone:", sum2 / trials )
sum = 0
sum2 = 0
for i = 1, trials do
local tensors = {}
local dim1 = torch.random(1, 25)
local dim2 = torch.random(1, 25)
for i = 1, ct do
table.insert(tensors, torch.CudaTensor(dim1, dim2, torch.random(1, 25)):uniform())
end
local timer = torch.Timer()
local catted = torch.cat(tensors, 3)
sum = sum + timer:time().real
local timer2 = torch.Timer()
local cloned = catted:clone()
sum2 = sum2 + timer2:time().real
end
print("CatArray for", ct, "3D Tensors along dim=3 took", sum / trials, "seconds. Clone:", sum2 / trials)
end
print('done')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment