torch/luajit-rocks — pull request #6 by fidlej (2014-09-16 11:28:13+00:00)
Title: Increased the DEFAULT_GRANULARITY to  512MB.

On Mac, luajit often runs out of memory. The error is:

luajit: not enough memory

The cause:
Luajit is able to use only the first 2GB of memory.
Luajit is using the memory to hold tables, references and strings.
Tensors are not restricted to 2GB of memory.
But a tensor can stole a memory from the first 2GB.
This happens on Mac. On a Mac, the first available memory is given to the tensor.

I implemented the DEFAULT_GRANULARITY change mentioned at:
http://comments.gmane.org/gmane.comp.lang.lua.luajit/3288

The memory assigned to tensors is now above the first 512MB.
You can reproduce the out-of-memory problem by:

local ffi = require("ffi")

ffi.cdef[[
void *malloc(size_t size);
void free(void *ptr);
]]

local N = 2^31
local array = ffi.cast("char *", ffi.C.malloc(N))
assert(array ~= nil, "out of memory")

local address = ffi.cast("long", array)
print(string.format("got address: %s MB", address / (1024 * 1024)))
if address > 2^31 then
ffi.C.free(array)
print("got memory above 2GB")
return
end

-- If holding the array, the following loop
-- will run quickly out of memory.
local holder = {}
for i = 1, math.huge do
local msg = tostring(i) .. '\n'
table.insert(holder, msg)
if i % 1000000 == 0 then
io.stderr:write(msg)
end
end

ffi.C.free(array)