项目作者: arthurdouillard

项目描述 :
Distributed memory with MPI in Python, features also map/reduce/filter!
高级语言: Python
项目地址: git://github.com/arthurdouillard/distributed_memory_mpi.git
创建时间: 2017-11-13T08:39:47Z
项目社区:https://github.com/arthurdouillard/distributed_memory_mpi

开源协议:

下载


Distributed memory with MPI in Python

This project’s goal is to produce a distributed memory. This project is made in
Python and will use mpi4py, the Python bindings for MPI for message passing
between the different machines hosting the distributed memory.

At least two hosts are needed.

To launch your python script using the distributed memory (-n is the number
of emulated hosts):

  1. mpiexec -hostfile hostfile -n 5 demo.py

The distributed memory only works for storing int and list[int].

Dependencies

  • Install MPI
  • Install the Python requirements:
  1. pip3 install -r requirements.txt

The API

  1. import distributed_memory as dm

Initialization:

  1. mem = dm.init_memory(max_per_slave=10)
  2. # max_per_slave is available memory per hosts

Note: Your application MUST call mem.quit() method at the end in order to exit
gracefully.

Creating a variable:

  1. var_int = mem.add(42)
  2. var_list = mem.add([1, 2, 3])

Reading a variable:

  1. value_int = mem.read(var_int)
  2. value_list = mem.read(var_list)

Modifying a variable:

Note: A boolean is returned to inform whether the modification has taken place.

  1. bool_int = mem.modify(var_int, 1337)
  2. bool_list = mem.modify(var_list, 42, index=0)

Reducing a list:

  1. sum_list = mem.reduce(var_list, lambda x, y: x + y, 0)

Mapping a list:

Note: The mapping may not be finished when the mem.map method returns.

  1. mem.map(var_list, lambda x: x ** 2)

Filtering a list:

  1. mem.filter(var_list, lambda x: x % == 0)

Freeing a variable:

  1. mem.free(var_int)
  2. mem.free(var_list)

Examples

  1. import distributed_memory as dm
  2. mem = mem.init_memory(max_per_slave=10)
  3. var = mem.add([1, 2, 3])
  4. print(mem.read(var))
  5. mem.map(var, lambda x: x + 1)
  6. print(mem.read(var))
  7. sum_result = mem.reduce(var, lambda x, y: x + y, 0)
  8. print(sum_result)
  9. mem.filter(var, lambda x: x % 2 == 0)
  10. print(mem.read(var))

Tests

To launch the tests:

  1. mpiexec -hostfile hostfile -n <nb_hosts> tests.py

Demo

To start the demo:

  1. mpiexec -hostfile -n <nb_hosts> demo.py --size <list_size> --verbose