Package: py-dsm
PydsmWhile the multi-thread parallel performance is limited in Python because of
the Global Interpreter Lock, Pydsm provided parallel computation that
truly shares memory between processes in a distributed way.RequirementsSharedArraynumpyPOSIX (Linux, variants of Unix including macOS, etc.)Python 2 is supported, but Python 3 is recommendedYou can install py-dsm via thepipcommand:pip install py-dsmAuthorsZhiyuan GuoNorm MatloffUsageImport PydsmTo import pydsm in your program, sayfoo.py, simply do# foo.py
import pydsmStart the processesTo create a cluster instance of 4 worker processes, one will do:with pydsm.Cluster(4) as cluster:Global arrayIn pydsm, all global arrays in shared memory are numpy arrays.
To create a global array, one needs to first create a cluster instance
and then callcreateShared().a = cluster.createShared(name = "A", shape = 10, dataType = int)The user needs to provide the global array with an arbitrary but unique name,
the shape (i.e., dimension) of the array, and the datatype (default is int).
The shape and datatype arguments will follow the same format as those typical
numpy functions such asnumpy.zeros(). The returned array is an array with
all elements initialized to zeros. All names of the global arrays should be
unique.Run the processesThen,runProcesses(func, paras)is invoked
to run the user's function in parallel.funcis the name of the
user-defined parallel function.paras, defaulted to an empty tuple,
is a tuple consisting of parameters passed in to the user's function.
Each worker process will get a copy of those parameters inparas.cluster.runProcesses(foo)Parallelized functionThe functionfooimplemented by the user will be executed
by 4 processes simultaneously.The first argument of a user's parallel function is mendatory.This mendatory parameter is a dictionary that containsresourceyou may need for your parallel function.
The resource has references to the id of each process,
the global array(s), and a lock.def foo(res, ...):
	# res is the resource
	# ... means any extra parameters you may need
	# CodeLocks and barriersTo use a lock in the parallelized function,
the lock first needs to be created in the main process.cluster.createdLock("foo_lock")Then retrieve the lock from 'resource' (the first parameter)
in the parallel function.lock = res['foo_lock']Then, the user can apply the lock anywhere
they want in the function.lock.apply()
# Critical section
lock.release()The barrier is invoked in the following way.pydsm.Cluster.barrier()Split the tasksThe user can invokesplitIndices(n, id, random=True)inside the user's
parallel function to distribute the tasks.splitIndiceswill return to each worker process a list of indices (numbers).Say we want to compute Y = AX in parallel by splitting the rows of A into
chunks.  We have 100 rows in A and 10 processors. We can let each
process take 10 rows.myid = resource['id']
myidxs = pydsm.Cluster.splitIndices(100, myid, random=True)
Y[myidxs, ] = np.matmul(A[myidxs,], X)In this scenario,nwill be 100.
It is recommended to setrandomto True to
split the rows randomly and thus achieve better load balancing.
Otherwise, process 0 will take the first ten rows, process 1 will take the
second ten rows, and so on, which may result in inefficient load balancing.ExampleThis is an embarrassingly parallel example that illustrates the usage
of the pydsm. Two vectors are added in parallel.
The idea is to break A and B into chunks, and have each worker process
work on one chunk.
The complete source code is indoc/examples/vecAdd.py.A and B are the two vectors to be added.
The result will be stored in C, as the computation is carried out.
Thus, all of them need to be shared variables.
They are set up in the following way.with pydsm.Cluster(4) as cluster:
	A = cluster.createShared(name = "A", shape = 10, dataType = int)
	B = cluster.createShared("B", 10, int)
	C = cluster.createShared("C", 10, int)Now A and B are shared arrays backed by numpy.
We can use and treat them the same way we do to numpy arrays.A[:] = np.arange(10) 
	# A is now "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
	B[:] = np.arange(10)
	B += 1 
	# B is now "array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])"We then callrunProcesses(), with the first parameter being
the name of the parallel function, which we will define later.
The second parameter is a tuple of parameters used by parallel function.
Here we pass in 10 as the length of each vector.cluster.runProcesses(add, paras=(10,))Next, we define the parallel functionadd.def add(resource, n):
    myid = resource['id']
    A = pydsm.Cluster.getShared("A")
    B = pydsm.Cluster.getShared("B")
    C = pydsm.Cluster.getShared("C")

    myidxs = pydsm.Cluster.splitIndices(n, myid, random=False)
    C[myidxs] = A[myidxs] + B[myidxs]Each worker process first obtains an unique id, and then
attaches A, B, and C to the corresponding shared variable.splitIndices()is then called to give each worker a list of indices.
Each worker will then compute elements in the array corresponding to those
indices.
Note that C is a shared variable, soadddoes not need to return C.For illustrative purpose of the usage of barriers,
we can have the following code at the end ofadd.
The process 1 will print out C after the computation is done.# Below is just for illustrative purpose
pydsm.Cluster.barrier()
if myid == 1:
	# In some versions of python, printing C directly may cause issues.
	# It is better to first convert the SharedArray into an numpy array
	# and then print it. So do np.array(C) before printing
	print("Check out vector C in processes: ", np.array(C))
	# C will be '[ 1  3  5  7  9 11 13 15 17 19]'More examplesFor more sample applications using pydsm such as finding prime numbers and
matrix multiplication, they are under the directoryinst/.NotesIf your program is terminated abnormally, py-dsm may not compeletely delete
your shared variables. When you run your program next time, you may encounter
the following error message because your program is trying to create a
shared variable that is still alive.# You are trying to create a shared variable named 'A', but there is
# already a shared var named 'A'. 
FileExistsError: [Errno 17] File exists: 'shm://A'In this scenario, you need to delete the shared variable yourself in the Python
interpreter.>>> import SharedArray as sa
>>> sa.delete("A")