I was just experimenting with shared memory and mmap and thought of sharing the code.
It is basically an mmap of shared memory instead of a file.
The code is here -
https://gist.github.com/amitmurthy/5477462 It allows all Julia processes to map the same shared memory segment to global variables of a specific Array type.
- ShmemVar is a type specifying the Symbol, Type and Dimensions of the array to be mapped in shared memory.
- map_shmem takes in a ShmemVar or an Array of ShmemVars and maps them- each Julia process will have the Symbols mapped in the global namespace
- on linux, /dev/shm will have a shared memory segment with the name julia_<prefix>_<symbol_name>
free_shmem should be called to unlink the shared memory segment with the same ShmemVar(s) / Symbol names
For example (julia started with "-p 2"):
julia> require("mapshmem.jl")
# map tvar on all process to an Array of type Int32 . julia> map_shmem(ShmemVar(:tvar, Int32, 100000000), "pfx")
julia> tvar
100000000-element Int32 Array:
0
0
0
.
.
.
# The 'global tvar;' statement is important to be able to access the shared memory segment.
julia> @parallel for i=1:100000000
global tvar; tvar[i] = i
end
julia> remotecall_fetch(2, () -> tvar[67000000])
67000000
julia> remotecall_fetch(1, () -> tvar[67000000])
67000000
# This will unlink the created shared memory segment
julia> free_shmem(:tvar, "pfx")
NOTE:
- The mmap constants defined in the gist are valid for Ubuntu. They have to be verified for OSX.
- In Linux you can also manually unlink the shared memory segments visible at /dev/shm . I don't know the equivalent for OSX.