shmem and mmap

603 views
Skip to first unread message

Amit Murthy

unread,
Apr 28, 2013, 1:30:49 PM4/28/13
to julia...@googlegroups.com
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. 



Viral Shah

unread,
Apr 29, 2013, 2:49:15 AM4/29/13
to julia...@googlegroups.com
This is pretty cool, and could be used to share large data structures for coarse-grained parallelism. This issue talks about shared memory parallelism:


-viral

Jeff Bezanson

unread,
Apr 29, 2013, 3:13:46 AM4/29/13
to julia...@googlegroups.com
Nice! We should pursue this. One next step is to create a Broadcast
type, to be able to to share objects like this without using global
variables. We can also do openmp-style loops right away.
Reply all
Reply to author
Forward
0 new messages