<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 DistArray
Step1:  Software Versions
Step2:  Set a RandomState
Set a RandomState so random numpy arrays don't change between runs.
Step3:  NumPy Arrays
DistArray is built on NumPy and provides a NumPy-array-like interface.  First, let's generate a NumPy array and examine some of its attributes.
Step4:  DistArrays
We'll make our first DistArray out of the NumPy array created above.
Step5:  Universal Functions (ufuncs)
Step6:  Reductions
Functions like sum, mean, min, and max are known as reductions, since they take an array and produce a smaller array or a scalar.  In NumPy and DistArray, some of these functions can be applied over a specific axis.
Step7:  Indexing and Slicing
DistArrays support standard NumPy Indexing and distributed slicing, including slices with a step.  Slicing is currently only supported for Block (and undistributed) DistArrays.
Step8:  Distributions
Above, when we created a DistArray out of a NumPy array, we didn't specify how the elements should be distributed among our engines.  Distributions give you control over this, if you want it.  In other words, Distributions control which processes own which (global) indices.
Step9:  The Distribution above was created for us by fromarray,
but DistArray lets us specify more complex distributions.
Here, we specify that the 0th dimension has a Block distribution ('b')
and the 1st dimension has a Cyclic distribution.
DistArray supports Block, Cyclic, Block-Cyclic, Unstructured,
and No-distribution dimensions.  See the
ScaLAPACK Documentation for more information about Distribution types.
Step10:  Redistribution
Since DistArrays are distributed, the equivalent to NumPy's reshape (distribute_as) can be a more complex and costly operation.  For convenience, you can supply either a shape or a full Distribution object.  Only Block distributions (and No-dist) are currently redistributable.
Step11:  Contexts
Context objects manage the setup of and communication to the worker processes for DistArray objects.  They also act as the namespace to which
DistArray creation functions are attached.
Step12:  Parallel IO
DistArray has support for reading NumPy .npy files in parallel, for reading and writing .dnpy files in parallel (our own flat-file format), and reading and writing HDF5 files in parallel (if you have a parallel build of h5py).
Step15:  Context.apply
Global view, local control.  The apply method on a Context allows you to write functions that are applied locally (that is, on the engines) to each section of a DistArray.  This allows you to push your computation close to your data, avoiding communication round-trips and possibly speeding up your computations.
Step17:  Context.register
Context.register is similar to Context.apply, but it allows you to register your function with a Context up front, and then call it repeatedly, with a nice syntax.
Step18:  MPI-only Execution
Instead of using an IPython client (which uses ZeroMQ to communicate to the engines), you can run your DistArray code in MPI-only mode (using an extra MPI process for the client).  This can be more performant.
Step19:  Distributed Array Protocol
Already have a library with its own distributed arrays?  Use the Distributed Array Protocol to work with DistArray.
The Distributed Array Protocol (DAP) is a process-local protocol that allows two subscribers, called the "producer" and the "consumer" or the "exporter" and the "importer", to communicate the essential data and metadata necessary to share a distributed-memory array between them. This allows two independently developed components to access, modify, and update a distributed array without copying. The protocol formalizes the metadata and buffers involved in the transfer, allowing several distributed array projects to collaborate, facilitating interoperability. By not copying the underlying array data, the protocol allows for efficient sharing of array data.
http
<ASSISTANT_TASK:>
Python Code:
# some utility imports
from __future__ import print_function
from pprint import pprint
from matplotlib import pyplot as plt
# main imports
import numpy
import distarray
# reduce precision on printed array values
numpy.set_printoptions(precision=2)
# display figures inline
%matplotlib inline
Explanation: DistArray: Distributed Arrays for Python
docs.enthought.com/distarray
Setup
Much of this notebook requires an IPython.parallel cluster to be running.
Outside the notebook, run
dacluster start -n4
End of explanation
print("numpy", numpy.__version__)
import matplotlib
print("matplotlib", matplotlib.__version__)
import h5py
print("h5py", h5py.__version__)
print("distarray", distarray.__version__)
Explanation: Software Versions
End of explanation
from numpy.random import RandomState
prng = RandomState(1234567890)
Explanation: Set a RandomState
Set a RandomState so random numpy arrays don't change between runs.
End of explanation
# a 4-row 5-column NumPy array with random contents
nparr = prng.rand(4, 5)
nparr
# NumPy array attributes
print("type:", type(nparr))
print("dtype:", nparr.dtype)
print("ndim:", nparr.ndim)
print("shape:", nparr.shape)
print("itemsize:", nparr.itemsize)
print("nbytes:", nparr.nbytes)
Explanation: NumPy Arrays
DistArray is built on NumPy and provides a NumPy-array-like interface.  First, let's generate a NumPy array and examine some of its attributes.
End of explanation
# First we need a `Context` object.  More on this later.
# For now, think of this object like the `NumPy` module.
# `Context`s manage the worker engines for us.
from distarray.globalapi import Context
context = Context()
# Make a DistArray from a NumPy array.
# This will push sections of the original NumPy array out
# to the engines.
darr = context.fromarray(nparr)
darr
# Print the array section stored on each engine
for i, a in enumerate(darr.get_localarrays()):
    print(i, a)
# DistArrays have similar attributes to NumPy arrays,
print("type:", type(darr))
print("dtype:", darr.dtype)
print("ndim:", darr.ndim)
print("shape:", darr.shape)
print("itemsize:", darr.itemsize)
print("nbytes:", darr.nbytes)
# and some additional attributes.
print("targets:", darr.targets)
print("context:", darr.context)
print("distribution:", darr.distribution)
Explanation: DistArrays
We'll make our first DistArray out of the NumPy array created above.
End of explanation
# NumPy provides `ufuncs`, or Universal Functions, that operate
# elementwise over NumPy arrays.
numpy.sin(nparr)
# DistArray provides ufuncs as well, for `DistArray`s.
import distarray.globalapi as da
da.sin(darr)
# `toarray` makes a NumPy array out of a DistArray, pulling all of the
# pieces back to the client.  We do this to display the contents of the
# DistArray.
da.sin(darr).toarray()
# A NumPy binary ufunc.
nparr + nparr
# The equivalent DistArray ufunc.
# Notice that a new DistArray is created without
# pulling data back to the client.
darr + darr
# Contents of the resulting DistArray.
(darr + darr).toarray()
Explanation: Universal Functions (ufuncs)
End of explanation
# NumPy sum
print("sum:", nparr.sum())
print("sum over an axis:", nparr.sum(axis=1))
# DistArray sum
print("sum:", darr.sum(), darr.sum().toarray())
print("sum over an axis:", darr.sum(axis=1), darr.sum(axis=1).toarray())
Explanation: Reductions
Functions like sum, mean, min, and max are known as reductions, since they take an array and produce a smaller array or a scalar.  In NumPy and DistArray, some of these functions can be applied over a specific axis.
End of explanation
# Our example array, as a reminder:
darr.toarray()
# The shapes of the local sections of our DistArray
darr.localshapes()
# Return the value of a single element
darr[0, 2]
# Take a column slice
darr_view = darr[:, 3]  # all rows, third column
print(darr_view)
print(darr_view.toarray())
# Slices return a new DistArray that is a view on the
# original, just like in NumPy.
# Changes in the view change the original array.
darr_view[3] = -0.99
print("view:")
print(darr_view.toarray())
print("original:")
print(darr.toarray())
# A more complex slice, with negative indices and a step.
print(darr[:, 2::2])
print(darr[:-1, 2::2].toarray())
# Incomplete indexing
# Grab the first row
darr[0]
Explanation: Indexing and Slicing
DistArrays support standard NumPy Indexing and distributed slicing, including slices with a step.  Slicing is currently only supported for Block (and undistributed) DistArrays.
End of explanation
# Let's look at the `Distribution` object that was created for us
# automatically by `fromarray`.
distribution = darr.distribution
# This is a 2D distribution: its 0th dimension is Block-distributed,
# and it's 1st dimension isn't distributed.
pprint(distribution.maps)
# Plot this Distribution, color-coding which process each global index
# belongs to.
from distarray.plotting import plot_array_distribution
process_coords = [(0, 0), (1, 0), (2, 0), (3, 0)]
plot_array_distribution(darr, process_coords, cell_label=False, legend=True)
# Check out which sections of this array's 0th dimension are on
# each process.
distribution.maps[0].bounds
Explanation: Distributions
Above, when we created a DistArray out of a NumPy array, we didn't specify how the elements should be distributed among our engines.  Distributions give you control over this, if you want it.  In other words, Distributions control which processes own which (global) indices.
End of explanation
from distarray.globalapi import Distribution
distribution = Distribution(context, shape=(64, 64), dist=('b', 'c'))
a = context.zeros(distribution, dtype='int32')
plot_array_distribution(a, process_coords, cell_label=False, legend=True)
Explanation: The Distribution above was created for us by fromarray,
but DistArray lets us specify more complex distributions.
Here, we specify that the 0th dimension has a Block distribution ('b')
and the 1st dimension has a Cyclic distribution.
DistArray supports Block, Cyclic, Block-Cyclic, Unstructured,
and No-distribution dimensions.  See the
ScaLAPACK Documentation for more information about Distribution types.
End of explanation
darr
darr.toarray()
# simple reshaping
reshaped = darr.distribute_as((10, 2))
reshaped
reshaped.toarray()
# A more complex resdistribution,
# changing shape, dist, and targets
dist = Distribution(context, shape=(5, 4),
                    dist=('b', 'b'), targets=(1, 3))
darr.distribute_as(dist)
Explanation: Redistribution
Since DistArrays are distributed, the equivalent to NumPy's reshape (distribute_as) can be a more complex and costly operation.  For convenience, you can supply either a shape or a full Distribution object.  Only Block distributions (and No-dist) are currently redistributable.
End of explanation
print("targets:", context.targets)
print("comm:", context.comm)
context.zeros((5, 3))
context.ones((20, 20))
Explanation: Contexts
Context objects manage the setup of and communication to the worker processes for DistArray objects.  They also act as the namespace to which
DistArray creation functions are attached.
End of explanation
# load .npy files in parallel
numpy.save("/tmp/outfile.npy", nparr)
distribution = Distribution(context, nparr.shape) 
new_darr = context.load_npy("/tmp/outfile.npy", distribution)
new_darr
# save to .dnpy (a built-in flat-file format based on .npy)
context.save_dnpy("/tmp/outfile", darr)
# load from .dnpy
context.load_dnpy("/tmp/outfile")
# save DistArrays to .hdf5 files in parallel
context.save_hdf5("/tmp/outfile.hdf5", darr, mode='w')
# load DistArrays from .hdf5 files in parallel (using h5py)
context.load_hdf5("/tmp/outfile.hdf5", distribution)
Explanation: Parallel IO
DistArray has support for reading NumPy .npy files in parallel, for reading and writing .dnpy files in parallel (our own flat-file format), and reading and writing HDF5 files in parallel (if you have a parallel build of h5py).
End of explanation
def get_local_random():
    Function to be applied locally.
    import numpy
    return numpy.random.randint(10)
context.apply(get_local_random)
def get_local_var(darr):
    Another local computation.
    return darr.ndarray.var()
context.apply(get_local_var, args=(darr.key,))
Explanation: Context.apply
Global view, local control.  The apply method on a Context allows you to write functions that are applied locally (that is, on the engines) to each section of a DistArray.  This allows you to push your computation close to your data, avoiding communication round-trips and possibly speeding up your computations.
End of explanation
def local_demean(la):
    Return the local array with the mean removed.
    return la.ndarray - la.ndarray.mean()
context.register(local_demean)
context.local_demean(darr)
Explanation: Context.register
Context.register is similar to Context.apply, but it allows you to register your function with a Context up front, and then call it repeatedly, with a nice syntax.
End of explanation
# an example script to run in MPI-only mode
%cd julia_set
!python benchmark_julia.py -h
# Compile kernel.pyx
!python setup.py build_ext --inplace
# Run the benchmarking script with 5 MPI processes:
# 4 worker processes and 1 client process
!mpiexec -np 5 python benchmark_julia.py --kernel=cython -r1 1024
Explanation: MPI-only Execution
Instead of using an IPython client (which uses ZeroMQ to communicate to the engines), you can run your DistArray code in MPI-only mode (using an extra MPI process for the client).  This can be more performant.
End of explanation
def return_protocol_structure(la):
    return la.__distarray__()
context.apply(return_protocol_structure, (darr.key,))
Explanation: Distributed Array Protocol
Already have a library with its own distributed arrays?  Use the Distributed Array Protocol to work with DistArray.
The Distributed Array Protocol (DAP) is a process-local protocol that allows two subscribers, called the "producer" and the "consumer" or the "exporter" and the "importer", to communicate the essential data and metadata necessary to share a distributed-memory array between them. This allows two independently developed components to access, modify, and update a distributed array without copying. The protocol formalizes the metadata and buffers involved in the transfer, allowing several distributed array projects to collaborate, facilitating interoperability. By not copying the underlying array data, the protocol allows for efficient sharing of array data.
http://distributed-array-protocol.readthedocs.org/en/rel-0.9.0/
End of explanation
<END_TASK>