I am writing an N-body simulation in C using the Barnes-Hut algorithm which requires using big blocks of memory. I am going for speed and efficiency. Is there any way to guarantee that these blocks of memory will stay in RAM and not get paged to the hard drive?
Edit: I would like to allocate as many as 2GB, however it is conceivable that I may end up running some simulations with much more memory.
Edit: Solution should support Windows7 (maybe Windows8 when it comes out?) and Ubuntu
There are operating system primitives that do what you want:
mlockon Unix (of which Ubuntu is but one example¹), andVirtualLockon Windows. (Ignore the quibbling in the comments over the exact semantics of VirtualLock; they’re irrelevant for your use case.)The Unix primitive requires
rootprivilege in the calling process (some systems permit locking down a small amount of memory without privilege, but you want far more than that). The Windows primitive appears not to require special privileges.¹ “Linux is not UNIX” objection noted and ignored with prejudice.