title: How to get past 1gb memory limit of 64 bit LuaJIT on Linux?
body: <p>The overview is I am prototyping code to understand my problem space, and I am running into 'PANIC: unprotected error in call to Lua API (not enough memory)' errors.  I am looking for ways to get around this limit.</p>
  
  <p>The environment bottom line is Torch, a scientific computing framework that runs on LuaJIT, and LuaJIT runs on Lua.  I need Torch because I eventually want to hammer on my problem with neural nets on a GPU, but to get there I need a good representation of the problem to feed to the nets.  I am (stuck) on Centos Linux, and I suspect that trying to rebuild all the pieces from source in 32bit mode (this is reported to extend the LuaJIT memory limit to 4gb) will be a nightmare if it works at all for all of the libraries.</p>
  
  <p>The problem space itself is probably not particularly relevant, but in overview I have datafiles of points that I calculate distances between and then bin (i.e. make histograms of) these distances to try and work out the most useful ranges.  Conveniently I can create complicated Lua tables with various sets of bins and torch.save() the mess of counts out, then pick it up later and inspect with different normalisations etc. -- so after one month of playing I am finding this to be really easy and powerful.  </p>
  
  <p>I can make it work looking at up to 3 distances with 15 bins each (15x15x15 plus overhead), but this only by adding explicit garbagecollection() calls and using fork()/wait() for each datafile so that the outer loop will keep running if one datafile (of several thousand) still blows the memory limit and crashes the child.  This gets extra painful as each successful child process now has to read, modify and write the current set of bin counts -- and my largest files for this are currently 36mb.  I would like to go larger (more bins), and would really prefer to just hold the counts in the 15 gigs of RAM I can't seem to access.</p>
  
  <p>So, here are some paths I have thought of; please do comment if you can confirm/deny that any of them will/won't get me outside of the 1gb boundary, or will just improve my efficiency within it.  Please do comment if you can suggest another approach that I have not thought of.</p>
  
  <ul>
  <li><p>am I missing a way to fire off a Lua process that I can read an arbitrary table back in from?  No doubt I can break my problem into smaller pieces, but parsing a return table from stdio (as from a system call to another Lua script) seems error prone, and writing/reading small intermediate files will be a lot of disk i/o.</p></li>
  <li><p>am I missing a stash-and-access-table-in-high-memory module ?  This seems like what I really want, but not found it yet</p></li>
  <li><p>can FFI C data structures be put outside the 1gb?  Doesn't seem like that would be the case but certainly I lack a full understanding of what is causing the limit in the first place.  I suspect that this will just get me an efficiency improvement over generic Lua tables for the few pieces that have moved beyond prototyping?  (unless I do a bunch of coding for each change)</p></li>
  <li><p>Surely I can get out by writing an extension in C (Torch appears to support nets that should go outside of the limit), but my brief investigation there turns up references to 'lightuserdata' pointers -- does this mean that a more normal extension won't get outside 1gb either?  This also seems like it has the heavy development cost for what should be a prototyping exercise.</p></li>
  </ul>
  
  <p>I know C well so going the FFI or extension route doesn't bother me - but I know from experience that encapsulating algorithms in this way can be both really elegant and really painful with two places to hide bugs.  Working through data structures containing tables within tables on the stack doesn't seem great either.  Before I make this effort I would like to be certain that the end result really will solve my problem.</p>
  
  <p>Thanks for reading the long post.</p>