mpi4py-3.0.3/ 0000775 0001750 0001750 00000000000 13560002767 014005 5 ustar dalcinl dalcinl 0000000 0000000 mpi4py-3.0.3/conf/ 0000775 0001750 0001750 00000000000 13560002767 014732 5 ustar dalcinl dalcinl 0000000 0000000 mpi4py-3.0.3/conf/epydoc.cfg 0000644 0001750 0001750 00000010550 13200562156 016667 0 ustar dalcinl dalcinl 0000000 0000000 [epydoc] # Epydoc section marker (required by ConfigParser)
# The list of objects to document. Objects can be named using
# dotted names, module filenames, or package directory names.
# Alases for this option include "objects" and "values".
modules: mpi4py
# The type of output that should be generated. Should be one
# of: html, text, latex, dvi, ps, pdf.
#output: html
# The path to the output directory. May be relative or absolute.
#target: docs/html/
# An integer indicating how verbose epydoc should be. The default
# value is 0; negative values will supress warnings and errors;
# positive values will give more verbose output.
verbosity: 0
# A boolean value indicating that Epydoc should show a tracaback
# in case of unexpected error. By default don't show tracebacks
#debug: 0
# If True, don't try to use colors or cursor control when doing
# textual output. The default False assumes a rich text prompt
#simple-term: 0
### Generation options
# The default markup language for docstrings, for modules that do
# not define __docformat__. Defaults to epytext.
docformat: reStructuredText
# Whether or not parsing should be used to examine objects.
parse: yes
# Whether or not introspection should be used to examine objects.
introspect: yes
# Don't examine in any way the modules whose dotted name match this
# regular expression pattern.
exclude: mpi4py\.(?!MPI)
# Don't perform introspection on the modules whose dotted name match this
# regular expression pattern.
#exclude-introspect
# Don't perform parsing on the modules whose dotted name match this
# regular expression pattern.
#exclude-parse
# The format for showing inheritance objects.
# It should be one of: 'grouped', 'listed', 'included'.
inheritance: listed
# Whether or not to inclue private variables. (Even if included,
# private variables will be hidden by default.)
private: yes
# Whether or not to list each module's imports.
imports: no
# Whether or not to include syntax highlighted source code in
# the output (HTML only).
sourcecode: yes
# Whether or not to include a a page with Epydoc log, containing
# effective option at the time of generation and the reported logs.
include-log: no
### Output options
# The documented project's name.
name: MPI for Python
# The documented project's URL.
url: https://bitbucket.org/mpi4py/mpi4py
# The CSS stylesheet for HTML output. Can be the name of a builtin
# stylesheet, or the name of a file.
css: white
# HTML code for the project link in the navigation bar. If left
# unspecified, the project link will be generated based on the
# project's name and URL.
#link: My Cool Project
# The "top" page for the documentation. Can be a URL, the name
# of a module or class, or one of the special names "trees.html",
# "indices.html", or "help.html"
#top: os.path
# An alternative help file. The named file should contain the
# body of an HTML file; navigation bars will be added to it.
#help: my_helpfile.html
# Whether or not to include a frames-based table of contents.
frames: yes
# Whether each class should be listed in its own section when
# generating LaTeX or PDF output.
separate-classes: no
### API linking options
# Define a new API document. A new interpreted text role
# will be created
#external-api: epydoc
# Use the records in this file to resolve objects in the API named NAME.
#external-api-file: epydoc:api-objects.txt
# Use this URL prefix to configure the string returned for external API.
#external-api-root: epydoc:http://epydoc.sourceforge.net/api
### Graph options
# The list of graph types that should be automatically included
# in the output. Graphs are generated using the Graphviz "dot"
# executable. Graph types include: "classtree", "callgraph",
# "umlclasstree". Use "all" to include all graph types
graph: classtree
# The path to the Graphviz "dot" executable, used to generate
# graphs.
#dotpath: /usr/local/bin/dot
# The name of one or more pstat files (generated by the profile
# or hotshot module). These are used to generate call graphs.
#pstat: profile.out
# Specify the font used to generate Graphviz graphs.
# (e.g., helvetica or times).
graph-font: Helvetica
# Specify the font size used to generate Graphviz graphs.
graph-font-size: 10
### Return value options
# The condition upon which Epydoc should exit with a non-zero
# exit status. Possible values are error, warning, docstring_warning
#fail-on: error
mpi4py-3.0.3/conf/epydocify.py 0000755 0001750 0001750 00000006524 13200540202 017265 0 ustar dalcinl dalcinl 0000000 0000000 #!/usr/bin/env python
# --------------------------------------------------------------------
from mpi4py import MPI
try:
from signal import signal, SIGPIPE, SIG_IGN
signal(SIGPIPE, SIG_IGN)
except ImportError:
pass
# --------------------------------------------------------------------
try:
from docutils.nodes import NodeVisitor
NodeVisitor.unknown_visit = lambda self, node: None
NodeVisitor.unknown_departure = lambda self, node: None
except ImportError:
pass
try: # epydoc 3.0.1 + docutils 0.6
from docutils.nodes import Text
try:
from collections import UserString
except ImportError:
from UserString import UserString
if not isinstance(Text, UserString):
def Text_get_data(s):
try:
return s._data
except AttributeError:
return s.astext()
def Text_set_data(s, d):
s.astext = lambda: d
s._data = d
Text.data = property(Text_get_data, Text_set_data)
except ImportError:
pass
# --------------------------------------------------------------------
from epydoc.docwriter import dotgraph
import re
dotgraph._DOT_VERSION_RE = \
re.compile(r'dot (?:- Graphviz )version ([\d\.]+)')
try:
dotgraph.DotGraph.DEFAULT_HTML_IMAGE_FORMAT
dotgraph.DotGraph.DEFAULT_HTML_IMAGE_FORMAT = 'png'
except AttributeError:
DotGraph_to_html = dotgraph.DotGraph.to_html
DotGraph_run_dot = dotgraph.DotGraph._run_dot
def to_html(self, image_file, image_url, center=True):
if image_file[-4:] == '.gif':
image_file = image_file[:-4] + '.png'
if image_url[-4:] == '.gif':
image_url = image_url[:-4] + '.png'
return DotGraph_to_html(self, image_file, image_url)
def _run_dot(self, *options):
if '-Tgif' in options:
opts = list(options)
for i, o in enumerate(opts):
if o == '-Tgif': opts[i] = '-Tpng'
options = type(options)(opts)
return DotGraph_run_dot(self, *options)
dotgraph.DotGraph.to_html = to_html
dotgraph.DotGraph._run_dot = _run_dot
# --------------------------------------------------------------------
from epydoc import docstringparser as dp
dp.STANDARD_FIELDS += [
dp.DocstringField(['credits'], 'Credits', multivalue=0,
varnames=['__credits__']),
]
from epydoc import docintrospecter as di
di.UNDOCUMENTED_MODULE_VARS += ('__package__',)
# --------------------------------------------------------------------
import re
_SIGNATURE_RE = re.compile(
# Class name (for builtin methods)
r'^\s*((?P\w+)\.)?' +
# The function name
r'(?P\w+)' +
# The parameters
r'\(((?P(?:self|cls|mcs)),?)?(?P.*)\)' +
# The return value (optional)
r'(\s*(->)\s*(?P\S.*?))?'+
# The end marker
r'\s*(\n|\s+(--|<=+>)\s+|$|\.\s+|\.\n)')
from epydoc import docstringparser as dsp
dsp._SIGNATURE_RE = _SIGNATURE_RE
# --------------------------------------------------------------------
import sys, os
import epydoc.cli
def epydocify():
dirname = os.path.dirname(__file__)
config = os.path.join(dirname, 'epydoc.cfg')
sys.argv.append('--config=' + config)
epydoc.cli.cli()
if __name__ == '__main__':
epydocify()
# --------------------------------------------------------------------
mpi4py-3.0.3/conf/mpiconfig.py 0000644 0001750 0001750 00000040312 13200562156 017247 0 ustar dalcinl dalcinl 0000000 0000000 import sys, os, platform
from distutils.util import split_quoted
from distutils.spawn import find_executable
from distutils import log as dulog
try:
from collections import OrderedDict
except ImportError:
OrderedDict = dict
try:
from configparser import ConfigParser
from configparser import Error as ConfigParserError
except ImportError:
from ConfigParser import ConfigParser
from ConfigParser import Error as ConfigParserError
class Config(object):
def __init__(self, logger=None):
self.log = logger or dulog
self.section = None
self.filename = None
self.compiler_info = OrderedDict((
('mpicc' , None),
('mpicxx' , None),
('mpifort', None),
('mpif90' , None),
('mpif77' , None),
('mpild' , None),
))
self.library_info = OrderedDict((
('define_macros' , []),
('undef_macros' , []),
('include_dirs' , []),
('libraries' , []),
('library_dirs' , []),
('runtime_library_dirs' , []),
('extra_compile_args' , []),
('extra_link_args' , []),
('extra_objects' , []),
))
def __bool__(self):
for v in self.compiler_info.values():
if v:
return True
for v in self.library_info.values():
if v:
return True
return False
__nonzero__ = __bool__
def get(self, k, d=None):
if k in self.compiler_info:
return self.compiler_info[k]
if k in self.library_info:
return self.library_info[k]
return d
def info(self, log=None):
if log is None: log = self.log
mpicc = self.compiler_info.get('mpicc')
mpicxx = self.compiler_info.get('mpicxx')
mpifort = self.compiler_info.get('mpifort')
mpif90 = self.compiler_info.get('mpif90')
mpif77 = self.compiler_info.get('mpif77')
mpild = self.compiler_info.get('mpild')
if mpicc:
log.info("MPI C compiler: %s", mpicc)
if mpicxx:
log.info("MPI C++ compiler: %s", mpicxx)
if mpifort:
log.info("MPI F compiler: %s", mpifort)
if mpif90:
log.info("MPI F90 compiler: %s", mpif90)
if mpif77:
log.info("MPI F77 compiler: %s", mpif77)
if mpild:
log.info("MPI linker: %s", mpild)
def update(self, config, **more):
if hasattr(config, 'keys'):
config = config.items()
for option, value in config:
if option in self.compiler_info:
self.compiler_info[option] = value
if option in self.library_info:
self.library_info[option] = value
if more:
self.update(more)
def setup(self, options, environ=None):
if environ is None: environ = os.environ
self.setup_library_info(options, environ)
self.setup_compiler_info(options, environ)
def setup_library_info(self, options, environ):
filename = section = None
mpiopt = getattr(options, 'mpi', None)
mpiopt = environ.get('MPICFG', mpiopt)
if mpiopt:
if ',' in mpiopt:
section, filename = mpiopt.split(',', 1)
elif os.path.isfile(mpiopt):
filename = mpiopt
else:
section = mpiopt
if not filename: filename = "mpi.cfg"
if not section: section = "mpi"
mach = platform.machine()
arch = platform.architecture()[0]
plat = sys.platform
osnm = os.name
if 'linux' == plat[:5]: plat = 'linux'
elif 'sunos' == plat[:5]: plat = 'solaris'
elif 'win' == plat[:3]: plat = 'windows'
suffixes = []
suffixes.append(plat+'-'+mach)
suffixes.append(plat+'-'+arch)
suffixes.append(plat)
suffixes.append(osnm+'-'+mach)
suffixes.append(osnm+'-'+arch)
suffixes.append(osnm)
suffixes.append(mach)
suffixes.append(arch)
sections = [section+"-"+s for s in suffixes]
sections += [section]
self.load(filename, sections)
if not self:
if os.name == 'posix':
self._setup_posix()
if sys.platform == 'win32':
self._setup_windows()
def _setup_posix(self):
pass
def _setup_windows(self):
# Microsoft MPI (v7, v6, v5, v4)
def msmpi_ver():
try:
try:
import winreg
except ImportError:
import _winreg as winreg
HKLM = winreg.HKEY_LOCAL_MACHINE
subkey = "SOFTWARE\Microsoft\MPI"
with winreg.OpenKey(HKLM, subkey) as key:
for i in range(winreg.QueryInfoKey(key)[1]):
name, value, type = winreg.EnumValue(key, i)
if name != "Version": continue
major, minor = value.split('.')[:2]
return (int(major), int(minor))
except: pass
return (1, 0)
def setup_msmpi(MSMPI_INC, MSMPI_LIB):
from os.path import join, isfile
ok = (MSMPI_INC and isfile(join(MSMPI_INC, 'mpi.h')) and
MSMPI_LIB and isfile(join(MSMPI_LIB, 'msmpi.lib')))
if not ok: return False
MSMPI_VER = '0x%d%02d' % msmpi_ver()
MSMPI_INC = os.path.normpath(MSMPI_INC)
MSMPI_LIB = os.path.normpath(MSMPI_LIB)
self.library_info.update(
define_macros=[('MSMPI_VER', MSMPI_VER)],
include_dirs=[MSMPI_INC],
library_dirs=[MSMPI_LIB],
libraries=['msmpi'])
self.section = 'msmpi'
self.filename = [os.path.dirname(MSMPI_INC)]
return True
arch = platform.architecture()[0][:2]
# Look for Microsoft MPI in the environment
MSMPI_INC = os.environ.get('MSMPI_INC')
MSMPI_LIB = os.environ.get('MSMPI_LIB'+arch)
if setup_msmpi(MSMPI_INC, MSMPI_LIB): return
# Look for Microsoft MPI v7/v6/v5 in default install path
for ProgramFiles in ('ProgramFiles', 'ProgramFiles(x86)'):
ProgramFiles = os.environ.get(ProgramFiles, '')
archdir = {'32':'x86', '64':'x64'}[arch]
MSMPI_DIR = os.path.join(ProgramFiles, 'Microsoft SDKs', 'MPI')
MSMPI_INC = os.path.join(MSMPI_DIR, 'Include')
MSMPI_LIB = os.path.join(MSMPI_DIR, 'Lib', archdir)
if setup_msmpi(MSMPI_INC, MSMPI_LIB): return
# Look for Microsoft HPC Pack 2012 R2 in default install path
for ProgramFiles in ('ProgramFiles', 'ProgramFiles(x86)'):
ProgramFiles = os.environ.get(ProgramFiles, '')
archdir = {'32':'i386', '64':'amd64'}[arch]
MSMPI_DIR = os.path.join(ProgramFiles, 'Microsoft MPI')
MSMPI_INC = os.path.join(MSMPI_DIR, 'Inc')
MSMPI_LIB = os.path.join(MSMPI_DIR, 'Lib', archdir)
if setup_msmpi(MSMPI_INC, MSMPI_LIB): return
# Microsoft MPI (legacy) and others
from glob import glob
ProgramFiles = os.environ.get('ProgramFiles', '')
CCP_HOME = os.environ.get('CCP_HOME', '')
for (name, prefix, suffix) in (
('msmpi', CCP_HOME, ''),
('msmpi', ProgramFiles, 'Microsoft HPC Pack 2012 R2'),
('msmpi', ProgramFiles, 'Microsoft HPC Pack 2012'),
('msmpi', ProgramFiles, 'Microsoft HPC Pack 2012 SDK'),
('msmpi', ProgramFiles, 'Microsoft HPC Pack 2008 R2'),
('msmpi', ProgramFiles, 'Microsoft HPC Pack 2008'),
('msmpi', ProgramFiles, 'Microsoft HPC Pack 2008 SDK'),
):
mpi_dir = os.path.join(prefix, suffix)
if not mpi_dir or not os.path.isdir(mpi_dir): continue
define_macros = []
include_dir = os.path.join(mpi_dir, 'include')
library = 'mpi'
library_dir = os.path.join(mpi_dir, 'lib')
if name == 'msmpi':
include_dir = os.path.join(mpi_dir, 'inc')
library = 'msmpi'
arch = platform.architecture()[0]
if arch == '32bit':
library_dir = os.path.join(library_dir, 'i386')
if arch == '64bit':
library_dir = os.path.join(library_dir, 'amd64')
if not os.path.isdir(include_dir):
include_dir = os.path.join(mpi_dir, 'include')
self.library_info.update(
define_macros=define_macros,
include_dirs=[include_dir],
libraries=[library],
library_dirs=[library_dir],
)
self.section = name
self.filename = [mpi_dir]
break
def setup_compiler_info(self, options, environ):
def find_exe(cmd, path=None):
if not cmd: return None
parts = split_quoted(cmd)
exe, args = parts[0], parts[1:]
if not os.path.isabs(exe) and path:
exe = os.path.basename(exe)
exe = find_executable(exe, path)
if not exe: return None
return ' '.join([exe]+args)
COMPILERS = (
('mpicc', ['mpicc', 'mpcc_r']),
('mpicxx', ['mpicxx', 'mpic++', 'mpiCC', 'mpCC_r']),
('mpifort', ['mpifort', 'mpif90', 'mpif77', 'mpfort_r']),
('mpif90', ['mpif90', 'mpf90_r']),
('mpif77', ['mpif77', 'mpf77_r']),
('mpild', []),
)
#
compiler_info = {}
PATH = environ.get('PATH', '')
for name, _ in COMPILERS:
cmd = (environ.get(name.upper()) or
getattr(options, name, None) or
self.compiler_info.get(name) or
None)
if cmd:
exe = find_exe(cmd, path=PATH)
if exe:
path = os.path.dirname(exe)
PATH = path + os.path.pathsep + PATH
compiler_info[name] = exe
else:
self.log.error("error: '%s' not found", cmd)
#
if not self and not compiler_info:
for name, candidates in COMPILERS:
for cmd in candidates:
cmd = find_exe(cmd)
if cmd:
compiler_info[name] = cmd
break
#
self.compiler_info.update(compiler_info)
def load(self, filename="mpi.cfg", section='mpi'):
if isinstance(filename, str):
filenames = filename.split(os.path.pathsep)
else:
filenames = list(filename)
if isinstance(section, str):
sections = section.split(',')
else:
sections = list(section)
#
try:
parser = ConfigParser(dict_type=OrderedDict)
except TypeError:
parser = ConfigParser()
try:
read_ok = parser.read(filenames)
except ConfigParserError:
self.log.error(
"error: parsing configuration file/s '%s'",
os.path.pathsep.join(filenames))
return None
for section in sections:
if parser.has_section(section):
break
section = None
if not section:
self.log.error(
"error: section/s '%s' not found in file/s '%s'",
','.join(sections), os.path.pathsep.join(filenames))
return None
parser_items = list(parser.items(section, vars=None))
#
compiler_info = type(self.compiler_info)()
for option, value in parser_items:
if option in self.compiler_info:
compiler_info[option] = value
#
pathsep = os.path.pathsep
expanduser = os.path.expanduser
expandvars = os.path.expandvars
library_info = type(self.library_info)()
for k, v in parser_items:
if k in ('define_macros',
'undef_macros',
):
macros = [e.strip() for e in v.split(',')]
if k == 'define_macros':
for i, m in enumerate(macros):
try: # -DFOO=bar
idx = m.index('=')
macro = (m[:idx], m[idx+1:] or None)
except ValueError: # -DFOO
macro = (m, None)
macros[i] = macro
library_info[k] = macros
elif k in ('include_dirs',
'library_dirs',
'runtime_dirs',
'runtime_library_dirs',
):
if k == 'runtime_dirs': k = 'runtime_library_dirs'
pathlist = [p.strip() for p in v.split(pathsep)]
library_info[k] = [expanduser(expandvars(p))
for p in pathlist if p]
elif k == 'libraries':
library_info[k] = [e.strip() for e in split_quoted(v)]
elif k in ('extra_compile_args',
'extra_link_args',
):
library_info[k] = split_quoted(v)
elif k == 'extra_objects':
library_info[k] = [expanduser(expandvars(e))
for e in split_quoted(v)]
elif hasattr(self, k):
library_info[k] = v.strip()
else:
pass
#
self.section = section
self.filename = read_ok
self.compiler_info.update(compiler_info)
self.library_info.update(library_info)
return compiler_info, library_info, section, read_ok
def dump(self, filename=None, section='mpi'):
# prepare configuration values
compiler_info = self.compiler_info.copy()
library_info = self.library_info.copy()
for k in library_info:
if k in ('define_macros',
'undef_macros',
):
macros = library_info[k]
if k == 'define_macros':
for i, (m, v) in enumerate(macros):
if v is None:
macros[i] = m
else:
macros[i] = '%s=%s' % (m, v)
library_info[k] = ','.join(macros)
elif k in ('include_dirs',
'library_dirs',
'runtime_library_dirs',
):
library_info[k] = os.path.pathsep.join(library_info[k])
elif isinstance(library_info[k], list):
library_info[k] = ' '.join(library_info[k])
# fill configuration parser
try:
parser = ConfigParser(dict_type=OrderedDict)
except TypeError:
parser = ConfigParser()
parser.add_section(section)
for option, value in compiler_info.items():
if not value: continue
parser.set(section, option, value)
for option, value in library_info.items():
if not value: continue
parser.set(section, option, value)
# save configuration file
if filename is None:
parser.write(sys.stdout)
elif hasattr(filename, 'write'):
parser.write(filename)
elif isinstance(filename, str):
with open(filename, 'w') as f:
parser.write(f)
return parser
if __name__ == '__main__':
import optparse
parser = optparse.OptionParser()
parser.add_option("--mpi", type="string")
parser.add_option("--mpicc", type="string")
parser.add_option("--mpicxx", type="string")
parser.add_option("--mpifort", type="string")
parser.add_option("--mpif90", type="string")
parser.add_option("--mpif77", type="string")
parser.add_option("--mpild", type="string")
(opts, args) = parser.parse_args()
log = dulog.Log(dulog.INFO)
cfg = Config(log)
cfg.setup(opts)
cfg.dump()
mpi4py-3.0.3/conf/mpiregexes.py 0000644 0001750 0001750 00000005257 12750576064 017471 0 ustar dalcinl dalcinl 0000000 0000000 import re
def anyof(*args):
return r'(?:%s)' % '|'.join(args)
def join(*args):
tokens = []
for tok in args:
if isinstance(tok, (list, tuple)):
tok = '(%s)' % r'\s*'.join(tok)
tokens.append(tok)
return r'\s*'.join(tokens)
lparen = r'\('
rparen = r'\)'
colon = r'\:'
asterisk = r'\*'
ws = r'\s*'
sol = r'^'
eol = r'$'
enum = join('enum', colon)
typedef = 'ctypedef'
pointer = asterisk
struct = join(typedef, 'struct')
basic_type = r'(?:void|int|char\s*\*{1,3})'
integral_type = r'MPI_(?:Aint|Offset|Count|Fint)'
struct_type = r'MPI_(?:Status)'
opaque_type = r'MPI_(?:Datatype|Request|Message|Op|Info|Group|Errhandler|Comm|Win|File)'
any_mpi_type = r'(?:%s|%s|%s)' % (struct_type, integral_type, opaque_type)
upper_name = r'MPI_[A-Z0-9_]+'
camel_name = r'MPI_[A-Z][a-z0-9_]+'
usrfun_name = camel_name + r'_(?:function|fn)'
arg_list = r'.*'
ret_type = r'void|int|double|MPI_Aint'
canyint = anyof(r'int', r'long(?:\s+long)?')
canyptr = join(r'\w+', pointer+'?')
annotation = r'\#\:\='
fallback_value = r'\(?[A-Za-z0-9_\+\-\(\)\*]+\)?'
fallback = r'(?:%s)?' % join (annotation, [fallback_value])
INTEGRAL_TYPE = join( typedef, [canyint], [integral_type], fallback, eol)
STRUCT_TYPE = join( struct, [struct_type], colon, eol)
OPAQUE_TYPE = join( typedef, canyptr, [opaque_type], eol)
FUNCTION_TYPE = join( typedef, [ret_type], [camel_name], lparen, [arg_list], rparen, fallback, eol)
ENUM_VALUE = join(sol, enum, [upper_name], fallback, eol)
HANDLE_VALUE = join(sol, [opaque_type], [upper_name], fallback, eol)
BASIC_PTRVAL = join(sol, [basic_type, pointer], [upper_name], fallback, eol)
INTEGRAL_PTRVAL = join(sol, [integral_type, pointer], [upper_name], fallback, eol)
STRUCT_PTRVAL = join(sol, [struct_type, pointer], [upper_name], fallback, eol)
FUNCT_PTRVAL = join(sol, [usrfun_name, pointer], [upper_name], fallback, eol)
FUNCTION_PROTO = join(sol, [ret_type], [camel_name], lparen, [arg_list], rparen, fallback, eol)
fint_type = r'MPI_Fint'
fmpi_type = opaque_type.replace('Datatype', 'Type')
c2f_name = fmpi_type+'_c2f'
f2c_name = fmpi_type+'_f2c'
FUNCTION_C2F = join(sol, [fint_type], [c2f_name], lparen, [opaque_type], rparen, fallback, eol)
FUNCTION_F2C = join(sol, [opaque_type], [f2c_name], lparen, [fint_type], rparen, fallback, eol)
IGNORE = anyof(join(sol, r'cdef.*', eol),
join(sol, struct, r'_mpi_\w+_t', eol),
join(sol, 'int', r'MPI_(?:SOURCE|TAG|ERROR)', eol),
join(sol, r'#.*', eol),
join(sol, eol))
# compile the RE's
glb = globals()
all = [key for key in dict(glb) if key.isupper()]
for key in all: glb[key] = re.compile(glb[key])
mpi4py-3.0.3/conf/CMakeLists.txt 0000644 0001750 0001750 00000014634 13200562156 017472 0 ustar dalcinl dalcinl 0000000 0000000 # Author: Lisandro Dalcin
# Contact: dalcinl@gmail.com
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(mpi4py)
SET(PythonInterp_FIND_VERSION ${PYTHON_VERSION})
SET(PythonLibs_FIND_VERSION ${PYTHON_VERSION_STRING})
FIND_PACKAGE(PythonInterp)
FIND_PACKAGE(PythonLibs)
FIND_PACKAGE(MPI)
SET(mpi4py_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src")
SET(mpi4py_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/mpi4py")
FILE(GLOB mpi4py_PYTHON_FILES
RELATIVE ${mpi4py_SOURCE_DIR}
${mpi4py_SOURCE_DIR}/mpi4py/*.py
${mpi4py_SOURCE_DIR}/mpi4py/futures/*.py
)
FILE(GLOB mpi4py_HEADER_FILES
RELATIVE ${mpi4py_SOURCE_DIR}
${mpi4py_SOURCE_DIR}/mpi4py/*.pxd
${mpi4py_SOURCE_DIR}/mpi4py/include/mpi4py/*.[hi]
${mpi4py_SOURCE_DIR}/mpi4py/include/mpi4py/*.px[di]
)
FOREACH(file
${mpi4py_PYTHON_FILES}
${mpi4py_HEADER_FILES}
)
SET(src "${mpi4py_SOURCE_DIR}/${file}")
SET(tgt "${mpi4py_BINARY_DIR}/${file}")
ADD_CUSTOM_COMMAND(
DEPENDS ${src}
OUTPUT ${tgt}
COMMAND ${CMAKE_COMMAND} ARGS -E copy ${src} ${tgt}
COMMENT "copy: ${file}"
)
SET(mpi4py_OUTPUT_FILES ${mpi4py_OUTPUT_FILES} ${tgt})
ENDFOREACH(file)
##FOREACH(file ${mpi4py_PYTHON_FILES})
## SET(mpi4py_py ${mpi4py_py} "${mpi4py_BINARY_DIR}/${file}")
## SET(mpi4py_pyc ${mpi4py_pyc} "${mpi4py_BINARY_DIR}/${file}c")
## SET(mpi4py_pyo ${mpi4py_pyo} "${mpi4py_BINARY_DIR}/${file}o")
##ENDFOREACH(file)
##ADD_CUSTOM_COMMAND(
## COMMAND ${CMAKE_COMMAND} ARGS -E echo 'from compileall import compile_dir' > compile_py
## COMMAND ${CMAKE_COMMAND} ARGS -E echo 'compile_dir(\"${mpi4py_BINARY_DIR}\")' >> compile_py
## COMMAND ${PYTHON_EXECUTABLE} ARGS compile_py
## COMMAND ${PYTHON_EXECUTABLE} ARGS -O compile_py
## COMMAND ${CMAKE_COMMAND} ARGS -E remove compile_py
## DEPENDS ${mpi4py_py}
## OUTPUT ${mpi4py_pyc} ${mpi4py_pyo}
## )
##SET(mpi4py_OUTPUT_FILES ${mpi4py_OUTPUT_FILES} ${mpi4py_pyc} ${mpi4py_pyo})
FIND_PROGRAM(MPI_COMPILER_CC
NAMES mpicc
HINTS "${MPI_BASE_DIR}"
PATH_SUFFIXES bin
DOC "MPI C compiler wrapper")
MARK_AS_ADVANCED(MPI_COMPILER_CC)
FIND_PROGRAM(MPI_COMPILER_CXX
NAMES mpicxx mpic++ mpiCC
HINTS "${MPI_BASE_DIR}"
PATH_SUFFIXES bin
DOC "MPI C++ compiler wrapper")
MARK_AS_ADVANCED(MPI_COMPILER_CXX)
find_program(MPI_COMPILER_FC
NAMES mpifort mpif90 mpif77
HINTS "${MPI_BASE_DIR}"
PATH_SUFFIXES bin
DOC "MPI Fortran compiler wrapper")
MARK_AS_ADVANCED(MPI_COMPILER_FC)
FIND_PROGRAM(MPI_COMPILER_F90
NAMES mpif90
HINTS "${MPI_BASE_DIR}"
PATH_SUFFIXES bin
DOC "MPI Fortran 90 compiler wrapper")
MARK_AS_ADVANCED(MPI_COMPILER_F90)
find_program(MPI_COMPILER_F77
NAMES mpif77
HINTS "${MPI_BASE_DIR}"
PATH_SUFFIXES bin
DOC "MPI Fortran 77 compiler wrapper")
MARK_AS_ADVANCED(MPI_COMPILER_F77)
FOREACH(file "mpi.cfg")
SET(tgt "${mpi4py_BINARY_DIR}/${file}")
ADD_CUSTOM_COMMAND(
OUTPUT ${tgt}
COMMAND ${CMAKE_COMMAND} ARGS -E echo '[mpi]' > "${tgt}"
COMMAND ${CMAKE_COMMAND} ARGS -E echo 'mpicc = ${MPI_COMPILER_CC}' >> ${tgt}
COMMAND ${CMAKE_COMMAND} ARGS -E echo 'mpicxx = ${MPI_COMPILER_CXX}' >> ${tgt}
COMMAND ${CMAKE_COMMAND} ARGS -E echo 'mpifort = ${MPI_COMPILER_FC}' >> ${tgt}
COMMAND ${CMAKE_COMMAND} ARGS -E echo 'mpif90 = ${MPI_COMPILER_F90}' >> ${tgt}
COMMAND ${CMAKE_COMMAND} ARGS -E echo 'mpif77 = ${MPI_COMPILER_F77}' >> ${tgt}
COMMENT "write: ${file}"
)
SET(mpi4py_OUTPUT_FILES ${mpi4py_OUTPUT_FILES} ${tgt})
ENDFOREACH(file)
ADD_CUSTOM_TARGET(mpi4py ALL DEPENDS ${mpi4py_OUTPUT_FILES})
INCLUDE_DIRECTORIES(
${MPI_INCLUDE_PATH}
${PYTHON_INCLUDE_PATH}
"${mpi4py_SOURCE_DIR}"
)
# --- mpi4py.MPI ---
PYTHON_ADD_MODULE(mpi4py.MPI MODULE "${mpi4py_SOURCE_DIR}/MPI.c")
SET_TARGET_PROPERTIES(
mpi4py.MPI PROPERTIES
OUTPUT_NAME "MPI" PREFIX ""
COMPILE_FLAGS "${MPI_COMPILE_FLAGS}"
LINK_FLAGS "${MPI_LINK_FLAGS}"
LIBRARY_OUTPUT_DIRECTORY "${mpi4py_BINARY_DIR}"
RUNTIME_OUTPUT_DIRECTORY "${mpi4py_BINARY_DIR}"
LINKER_LANGUAGE C
)
TARGET_LINK_LIBRARIES(mpi4py.MPI ${PYTHON_LIBRARY})
TARGET_LINK_LIBRARIES(mpi4py.MPI ${MPI_LIBRARIES})
# --- mpi4py.dl ---
PYTHON_ADD_MODULE(mpi4py.dl MODULE "${mpi4py_SOURCE_DIR}/dynload.c")
SET_TARGET_PROPERTIES(
mpi4py.dl PROPERTIES
OUTPUT_NAME "dl" PREFIX ""
LIBRARY_OUTPUT_DIRECTORY "${mpi4py_BINARY_DIR}"
RUNTIME_OUTPUT_DIRECTORY "${mpi4py_BINARY_DIR}"
LINKER_LANGUAGE C
)
TARGET_LINK_LIBRARIES(mpi4py.dl ${PYTHON_LIBRARY})
TARGET_LINK_LIBRARIES(mpi4py.dl ${CMAKE_DL_LIBS})
# --- mpi4py/bin/python-mpi ---
ADD_EXECUTABLE(python-mpi "${mpi4py_SOURCE_DIR}/python.c")
SET_TARGET_PROPERTIES(
python-mpi PROPERTIES
COMPILE_FLAGS "${MPI_COMPILE_FLAGS}"
LINK_FLAGS "${MPI_LINK_FLAGS}"
RUNTIME_OUTPUT_DIRECTORY "${mpi4py_BINARY_DIR}/bin"
LINKER_LANGUAGE C
)
TARGET_LINK_LIBRARIES(python-mpi ${PYTHON_LIBRARY})
TARGET_LINK_LIBRARIES(python-mpi ${MPI_LIBRARIES})
# --- mpi4py/lib-pmpi/libmpe.so ---
ADD_LIBRARY(pmpi-mpe MODULE "${mpi4py_SOURCE_DIR}/lib-pmpi/mpe.c")
SET_TARGET_PROPERTIES(
pmpi-mpe PROPERTIES
OUTPUT_NAME "mpe"
LIBRARY_OUTPUT_DIRECTORY "${mpi4py_BINARY_DIR}/lib-pmpi"
RUNTIME_OUTPUT_DIRECTORY "${mpi4py_BINARY_DIR}/lib-pmpi"
LINKER_LANGUAGE C
)
TARGET_LINK_LIBRARIES(pmpi-mpe ${MPE_LIBRARIES})
TARGET_LINK_LIBRARIES(pmpi-mpe ${MPI_LIBRARIES})
# --- mpi4py/lib-pmpi/libvt.so ---
ADD_LIBRARY(pmpi-vt MODULE "${mpi4py_SOURCE_DIR}/lib-pmpi/vt.c")
SET_TARGET_PROPERTIES(
pmpi-vt PROPERTIES
OUTPUT_NAME "vt"
LIBRARY_OUTPUT_DIRECTORY "${mpi4py_BINARY_DIR}/lib-pmpi"
RUNTIME_OUTPUT_DIRECTORY "${mpi4py_BINARY_DIR}/lib-pmpi"
LINKER_LANGUAGE C
)
TARGET_LINK_LIBRARIES(pmpi-vt ${VT_LIBRARIES})
TARGET_LINK_LIBRARIES(pmpi-vt ${MPI_LIBRARIES})
# --- mpi4py/lib-pmpi/libvt-mpi.so ---
ADD_LIBRARY(pmpi-vt-mpi MODULE "${mpi4py_SOURCE_DIR}/lib-pmpi/vt-mpi.c")
SET_TARGET_PROPERTIES(
pmpi-vt-mpi PROPERTIES
OUTPUT_NAME "vt-mpi"
LIBRARY_OUTPUT_DIRECTORY "${mpi4py_BINARY_DIR}/lib-pmpi"
RUNTIME_OUTPUT_DIRECTORY "${mpi4py_BINARY_DIR}/lib-pmpi"
LINKER_LANGUAGE C
)
TARGET_LINK_LIBRARIES(pmpi-vt-mpi ${VT_MPI_LIBRARIES})
TARGET_LINK_LIBRARIES(pmpi-vt-mpi ${MPI_LIBRARIES})
# --- mpi4py/lib-pmpi/libvt-hyb.so ---
ADD_LIBRARY(pmpi-vt-hyb MODULE "${mpi4py_SOURCE_DIR}/lib-pmpi/vt-hyb.c")
SET_TARGET_PROPERTIES(
pmpi-vt-hyb PROPERTIES
OUTPUT_NAME "vt-hyb"
LIBRARY_OUTPUT_DIRECTORY "${mpi4py_BINARY_DIR}/lib-pmpi"
RUNTIME_OUTPUT_DIRECTORY "${mpi4py_BINARY_DIR}/lib-pmpi"
LINKER_LANGUAGE C
)
TARGET_LINK_LIBRARIES(pmpi-vt-hyb ${VT_HYB_LIBRARIES})
TARGET_LINK_LIBRARIES(pmpi-vt-hyb ${MPI_LIBRARIES})
mpi4py-3.0.3/conf/cythonize.py 0000644 0001750 0001750 00000003744 13200562156 017320 0 ustar dalcinl dalcinl 0000000 0000000 #!/usr/bin/env python
import sys, os
def cythonize(source,
includes=(),
destdir_c=None,
destdir_h=None,
wdir=None):
from Cython.Compiler.Main import \
CompilationOptions, default_options, \
compile, \
PyrexError
from Cython.Compiler import Options
cwd = os.getcwd()
try:
name, ext = os.path.splitext(source)
outputs_c = [name+'.c']
outputs_h = [name+'.h', name+'_api.h']
# change working directory
if wdir:
os.chdir(wdir)
# run Cython on source
options = CompilationOptions(default_options)
options.output_file = outputs_c[0]
options.include_path = list(includes)
Options.generate_cleanup_code = 3
any_failures = 0
try:
result = compile(source, options)
if result.num_errors > 0:
any_failures = 1
except (EnvironmentError, PyrexError):
e = sys.exc_info()[1]
sys.stderr.write(str(e) + '\n')
any_failures = 1
if any_failures:
for output in outputs_c + outputs_h:
try:
os.remove(output)
except OSError:
pass
return 1
# move ouputs
for destdir, outputs in (
(destdir_c, outputs_c),
(destdir_h, outputs_h)):
if destdir is None: continue
for output in outputs:
dest = os.path.join(
destdir, os.path.basename(output))
try:
os.remove(dest)
except OSError:
pass
os.rename(output, dest)
#
return 0
#
finally:
os.chdir(cwd)
if __name__ == "__main__":
sys.exit(
cythonize('mpi4py.MPI.pyx',
destdir_h=os.path.join('mpi4py', 'include', 'mpi4py'),
wdir='src')
)
mpi4py-3.0.3/conf/mpiscanner.py 0000644 0001750 0001750 00000025756 13200562156 017452 0 ustar dalcinl dalcinl 0000000 0000000 # Very, very naive RE-based way for collecting declarations inside
# 'cdef extern from *' Cython blocks in in source files, and next
# generate compatibility headers for MPI-2 partially implemented or
# built, or MPI-1 implementations, perhaps providing a subset of MPI-2
from textwrap import dedent
from warnings import warn
import mpiregexes as Re
class Node(object):
REGEX = None
def match(self, line):
m = self.REGEX.search(line)
if m: return m.groups()
match = classmethod(match)
HEADER = None
CONFIG = None
MISSING = None
MISSING_HEAD = """\
#ifndef PyMPI_HAVE_%(name)s
#undef %(cname)s
"""
MISSING_TAIL = """
#endif
"""
def init(self, name, **kargs):
assert name is not None
self.name = name
self.__dict__.update(kargs)
def header(self):
line = dedent(self.HEADER) % vars(self)
line = line.replace('\n', '')
line = line.replace(' ', ' ')
return line + '\n'
def config(self):
return dedent(self.CONFIG) % vars(self)
def missing(self, guard=True):
if guard:
head = dedent(self.MISSING_HEAD)
tail = dedent(self.MISSING_TAIL)
else:
head = '#undef %(cname)s\n'
tail = '\n\n'
body = dedent(self.MISSING)
return (head+body+tail) % vars(self)
class NodeType(Node):
CONFIG = """\
%(ctype)s v; %(ctype)s* p; (void)v; (void)p;"""
def __init__(self, ctype):
self.init(name=ctype,
cname=ctype,
ctype=ctype,)
class NodeStructType(NodeType):
HEADER = """\
typedef struct {%(cfields)s ...; } %(ctype)s;"""
MISSING = """\
typedef struct PyMPI_%(ctype)s {
%(cfields)s
} PyMPI_%(ctype)s;
#define %(ctype)s PyMPI_%(ctype)s"""
def __init__(self, ctype, cfields):
super(NodeStructType, self).__init__(ctype)
self.cfields = '\n'.join([' %s %s;' % field
for field in cfields])
class NodeFuncType(NodeType):
HEADER = """\
typedef %(crett)s (%(cname)s)(%(cargs)s);"""
MISSING = """\
typedef %(crett)s (MPIAPI PyMPI_%(cname)s)(%(cargs)s);
#define %(cname)s PyMPI_%(cname)s"""
def __init__(self, crett, cname, cargs, calias=None):
self.init(name=cname,
cname=cname,
ctype=cname+'*',)
self.crett = crett
self.cargs = cargs or 'void'
if calias is not None:
self.MISSING = '#define %(cname)s %(calias)s'
self.calias = calias
class NodeValue(Node):
HEADER = """\
const %(ctype)s %(cname)s;"""
CONFIG = """\
%(ctype)s v; v = %(cname)s; (void)v;"""
MISSING = '#define %(cname)s (%(calias)s)'
def __init__(self, ctype, cname, calias):
self.init(name=cname,
cname=cname,
ctype=ctype,
calias=calias)
if ctype.endswith('*'):
ctype = ctype + ' const'
self.HEADER = ctype + ' %(cname)s;'
def ctypefix(ct):
ct = ct.strip()
ct = ct.replace('[][3]',' (*)[3]')
ct = ct.replace('[]','*')
return ct
class NodeFuncProto(Node):
HEADER = """\
%(crett)s %(cname)s(%(cargs)s);"""
CONFIG = """\
%(crett)s v; v = %(cname)s(%(cargscall)s); (void)v;"""
MISSING = ' '. join(['#define %(cname)s(%(cargsnamed)s)',
'PyMPI_UNAVAILABLE("%(name)s"%(comma)s%(cargsnamed)s)'])
def __init__(self, crett, cname, cargs, calias=None):
self.init(name=cname,
cname=cname)
self.crett = crett
self.cargs = cargs or 'void'
if cargs == 'void': cargs = ''
if cargs:
cargs = cargs.split(',')
if cargs[-1].strip() == '...':
del cargs[-1]
else:
cargs = []
self.cargstype = cargs
nargs = len(cargs)
if nargs: self.comma = ','
else: self.comma = ''
cargscall = ['(%s)0' % ctypefix(a) for a in cargs]
self.cargscall = ','.join(cargscall)
cargsnamed = ['a%d' % (a+1) for a in range(nargs)]
self.cargsnamed = ','.join(cargsnamed)
if calias is not None:
self.MISSING = '#define %(cname)s %(calias)s'
self.calias = calias
class IntegralType(NodeType):
REGEX = Re.INTEGRAL_TYPE
HEADER = """\
typedef %(cbase)s... %(ctype)s;"""
MISSING = """\
typedef %(ctdef)s PyMPI_%(ctype)s;
#define %(ctype)s PyMPI_%(ctype)s"""
def __init__(self, cbase, ctype, calias=None):
super(IntegralType, self).__init__(ctype)
self.cbase = cbase
if calias is not None:
self.ctdef = calias
else:
self.ctdef = cbase
class StructType(NodeStructType):
REGEX = Re.STRUCT_TYPE
def __init__(self, ctype):
cnames = ['MPI_SOURCE', 'MPI_TAG', 'MPI_ERROR']
cfields = list(zip(['int']*3, cnames))
super(StructType, self).__init__(ctype, cfields)
class OpaqueType(NodeType):
REGEX = Re.OPAQUE_TYPE
HEADER = """\
typedef struct{...;} %(ctype)s;"""
MISSING = """\
typedef void *PyMPI_%(ctype)s;
#define %(ctype)s PyMPI_%(ctype)s"""
class FunctionType(NodeFuncType):
REGEX = Re.FUNCTION_TYPE
class EnumValue(NodeValue):
REGEX = Re.ENUM_VALUE
def __init__(self, cname, calias):
self.init(name=cname,
cname=cname,
ctype='int',
calias=calias)
class HandleValue(NodeValue):
REGEX = Re.HANDLE_VALUE
MISSING = '#define %(cname)s ((%(ctype)s)%(calias)s)'
class BasicPtrVal(NodeValue):
REGEX = Re.BASIC_PTRVAL
MISSING = '#define %(cname)s ((%(ctype)s)%(calias)s)'
class IntegralPtrVal(NodeValue):
REGEX = Re.INTEGRAL_PTRVAL
MISSING = '#define %(cname)s ((%(ctype)s)%(calias)s)'
class StructPtrVal(NodeValue):
REGEX = Re.STRUCT_PTRVAL
class FunctionPtrVal(NodeValue):
REGEX = Re.FUNCT_PTRVAL
class FunctionProto(NodeFuncProto):
REGEX = Re.FUNCTION_PROTO
class FunctionC2F(NodeFuncProto):
REGEX = Re.FUNCTION_C2F
MISSING = ' '.join(['#define %(cname)s(%(cargsnamed)s)',
'((%(crett)s)0)'])
class FunctionF2C(NodeFuncProto):
REGEX = Re.FUNCTION_F2C
MISSING = ' '.join(['#define %(cname)s(%(cargsnamed)s)',
'%(cretv)s'])
def __init__(self, *a, **k):
NodeFuncProto.__init__(self, *a, **k)
self.cretv = self.crett.upper() + '_NULL'
class Scanner(object):
NODE_TYPES = [
IntegralType,
StructType, OpaqueType,
HandleValue, EnumValue,
BasicPtrVal,
IntegralPtrVal, StructPtrVal,
FunctionType, FunctionPtrVal,
FunctionProto, FunctionC2F, FunctionF2C,
]
def __init__(self):
self.nodes = []
self.nodemap = {}
def parse_file(self, filename):
with open(filename) as f:
self.parse_lines(f)
def parse_lines(self, lines):
for line in lines:
self.parse_line(line)
def parse_line(self, line):
if Re.IGNORE.match(line): return
nodemap = self.nodemap
nodelist = self.nodes
for nodetype in self.NODE_TYPES:
args = nodetype.match(line)
if args:
node = nodetype(*args)
assert node.name not in nodemap, node.name
nodemap[node.name] = len(nodelist)
nodelist.append(node)
break
if not args:
warn('unmatched line:\n%s' % line)
def __iter__(self):
return iter(self.nodes)
def __getitem__(self, name):
return self.nodes[self.nodemap[name]]
def dump_header_h(self, fileobj):
if isinstance(fileobj, str):
with open(fileobj, 'w') as f:
self.dump_header_h(f)
return
for node in self:
fileobj.write(node.header())
CONFIG_HEAD = """\
#ifndef PyMPI_CONFIG_H
#define PyMPI_CONFIG_H
"""
CONFIG_MACRO = 'PyMPI_HAVE_%s'
CONFIG_TAIL = """\
#endif /* !PyMPI_CONFIG_H */
"""
def dump_config_h(self, fileobj, suite):
if isinstance(fileobj, str):
with open(fileobj, 'w') as f:
self.dump_config_h(f, suite)
return
head = dedent(self.CONFIG_HEAD)
macro = dedent(self.CONFIG_MACRO)
tail = dedent(self.CONFIG_TAIL)
fileobj.write(head)
if suite is None:
for node in self:
line = '#undef %s\n' % ((macro % node.name))
fileobj.write(line)
else:
for name, result in suite:
assert name in self.nodemap
if result:
line = '#define %s 1\n' % ((macro % name))
else:
line = '#undef %s\n' % ((macro % name))
fileobj.write(line)
fileobj.write(tail)
MISSING_HEAD = """\
#ifndef PyMPI_MISSING_H
#define PyMPI_MISSING_H
#ifndef PyMPI_UNUSED
# if defined(__GNUC__)
# if !defined(__cplusplus) || (__GNUC__>3||(__GNUC__==3&&__GNUC_MINOR__>=4))
# define PyMPI_UNUSED __attribute__ ((__unused__))
# else
# define PyMPI_UNUSED
# endif
# elif defined(__INTEL_COMPILER) || defined(__ICC)
# define PyMPI_UNUSED __attribute__ ((__unused__))
# else
# define PyMPI_UNUSED
# endif
#endif
#define PyMPI_ERR_UNAVAILABLE (-1431655766) /*0xaaaaaaaa*/
static PyMPI_UNUSED
int PyMPI_UNAVAILABLE(const char *name,...)
{ (void)name; return PyMPI_ERR_UNAVAILABLE; }
"""
MISSING_TAIL = """\
#endif /* !PyMPI_MISSING_H */
"""
def dump_missing_h(self, fileobj, suite):
if isinstance(fileobj, str):
with open(fileobj, 'w') as f:
self.dump_missing_h(f, suite)
return
head = dedent(self.MISSING_HEAD)
tail = dedent(self.MISSING_TAIL)
#
fileobj.write(head)
if suite is None:
for node in self:
fileobj.write(node.missing())
else:
for name, result in suite:
node = self[name]
if not result:
fileobj.write(node.missing())
fileobj.write(tail)
# -----------------------------------------
if __name__ == '__main__':
import sys, os
sources = [os.path.join('src', 'mpi4py', 'libmpi.pxd')]
log = lambda msg: sys.stderr.write(msg + '\n')
scanner = Scanner()
for filename in sources:
log('parsing file %s' % filename)
scanner.parse_file(filename)
log('processed %d definitions' % len(scanner.nodes))
config_h = os.path.join('src', 'lib-mpi', 'config', 'config.h')
log('writing file %s' % config_h)
scanner.dump_config_h(config_h, None)
missing_h = os.path.join('src', 'lib-mpi', 'missing.h')
log('writing file %s' % missing_h)
scanner.dump_missing_h(missing_h, None)
#libmpi_h = os.path.join('.', 'libmpi.h')
#log('writing file %s' % libmpi_h)
#scanner.dump_header_h(libmpi_h)
# -----------------------------------------
mpi4py-3.0.3/conf/cythonize.sh 0000755 0001750 0001750 00000000164 13200562156 017276 0 ustar dalcinl dalcinl 0000000 0000000 #!/bin/sh
python -m cython --cleanup 3 -w src $@ mpi4py.MPI.pyx && \
mv src/mpi4py.MPI*.h src/mpi4py/include/mpi4py
mpi4py-3.0.3/conf/coverage.sh 0000775 0001750 0001750 00000016323 13426006675 017074 0 ustar dalcinl dalcinl 0000000 0000000 #!/bin/bash
MPIEXEC=${MPIEXEC-mpiexec}
PYTHON=${1-${PYTHON-python}}
export PYTHONDONTWRITEBYTECODE=1
$PYTHON -m coverage erase
$MPIEXEC -n 1 $PYTHON -m coverage run "$(dirname "$0")/coverage-helper.py" > /dev/null || true
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py.bench --help > /dev/null
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py.bench --threads helloworld -q
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py.bench --no-threads helloworld -q
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py.bench --thread-level=single helloworld -q
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py.bench helloworld > /dev/null
$MPIEXEC -n 2 $PYTHON -m coverage run -m mpi4py.bench helloworld > /dev/null
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py.bench helloworld > /dev/null
$MPIEXEC -n 2 $PYTHON -m coverage run -m mpi4py.bench helloworld -q
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py.bench ringtest > /dev/null
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py.bench ringtest -q -l 2 -s 1
$MPIEXEC -n 2 $PYTHON -m coverage run -m mpi4py.bench ringtest -q -l 2 -s 1
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py.bench > /dev/null 2>&1 || true
$MPIEXEC -n 2 $PYTHON -m coverage run -m mpi4py.bench > /dev/null 2>&1 || true
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py.bench qwerty > /dev/null 2>&1 || true
$MPIEXEC -n 2 $PYTHON -m coverage run -m mpi4py.bench qwerty > /dev/null 2>&1 || true
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py.bench --mpe qwerty > /dev/null 2>&1 || true
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py.bench --vt qwerty > /dev/null 2>&1 || true
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py.run --help > /dev/null
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py --version > /dev/null
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py --help > /dev/null
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py - < /dev/null
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py -c "42" > /dev/null
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py -m this > /dev/null
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py "$(dirname "$0")/coverage-helper.py" > /dev/null || true
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py -rc threads=0 --rc=thread_level=single -c "" > /dev/null
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py -p mpe -profile mpe -c "" > /dev/null 2>&1 || true
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py --profile mpe --profile=mpe -c "" > /dev/null 2>&1 || true
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py -vt --vt -mpe --mpe -c "" > /dev/null 2>&1 || true
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py > /dev/null 2>&1 || true
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py -m > /dev/null 2>&1 || true
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py -c > /dev/null 2>&1 || true
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py -p > /dev/null 2>&1 || true
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py -bad > /dev/null 2>&1 || true
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py --bad=a > /dev/null 2>&1 || true
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py -rc= > /dev/null 2>&1 || true
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py --rc=a > /dev/null 2>&1 || true
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py --rc=a= > /dev/null 2>&1 || true
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py --rc==a > /dev/null 2>&1 || true
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py -c "import sys; sys.exit()" > /dev/null 2>&1 || true
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py -c "import sys; sys.exit(0)" > /dev/null 2>&1 || true
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py -c "import sys; sys.exit(1)" > /dev/null 2>&1 || true
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py -c "import sys; sys.exit('error')" > /dev/null 2>&1 || true
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py -c "from mpi4py import MPI; 1/0;" > /dev/null 2>&1 || true
$MPIEXEC -n 1 $PYTHON -m coverage run demo/futures/test_futures.py -q 2> /dev/null
$MPIEXEC -n 2 $PYTHON -m coverage run demo/futures/test_futures.py -q 2> /dev/null
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py.futures demo/futures/test_futures.py -q 2> /dev/null
$MPIEXEC -n 2 $PYTHON -m coverage run -m mpi4py.futures demo/futures/test_futures.py -q ProcessPoolPickleTest 2> /dev/null
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py.futures -h > /dev/null
$MPIEXEC -n 2 $PYTHON -m coverage run -m mpi4py.futures -h > /dev/null
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py.futures -m this > /dev/null
$MPIEXEC -n 2 $PYTHON -m coverage run -m mpi4py.futures -m this > /dev/null
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py.futures -c "42"
$MPIEXEC -n 2 $PYTHON -m coverage run -m mpi4py.futures -c "42"
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py.futures - /dev/null 2>&1 || true
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py.futures xy > /dev/null 2>&1 || true
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py.futures -c > /dev/null 2>&1 || true
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py.futures -m > /dev/null 2>&1 || true
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py.futures -x > /dev/null 2>&1 || true
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py.futures -c "1/0" > /dev/null 2>&1 || true
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py.futures -c "raise SystemExit(11)" > /dev/null 2>&1 || true
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py.futures -c "raise SystemExit('')" > /dev/null 2>&1 || true
if [ $(command -v mpichversion) ]; then
testdir=demo/futures
$MPIEXEC -n 1 $PYTHON -m coverage run -m mpi4py.futures.server --xyz > /dev/null 2>&1 || true
$MPIEXEC -n 2 $PYTHON -m coverage run -m mpi4py.futures.server --bind localhost &
mpi4pyserver=$!; sleep 1;
$MPIEXEC -n 1 $PYTHON -m coverage run $testdir/test_service.py --host localhost
wait $mpi4pyserver
$MPIEXEC -n 2 $PYTHON -m coverage run -m mpi4py.futures.server --port 31414 --info "a=x,b=y" &
mpi4pyserver=$!; sleep 1;
$MPIEXEC -n 1 $PYTHON -m coverage run $testdir/test_service.py --port 31414 --info "a=x,b=y"
wait $mpi4pyserver
fi
if [ $(command -v mpichversion) ] && [ $(command -v hydra_nameserver) ]; then
testdir=demo/futures
hydra_nameserver &
nameserver=$!; sleep 1;
$MPIEXEC -nameserver localhost -n 2 $PYTHON -m coverage run -m mpi4py.futures.server &
mpi4pyserver=$!; sleep 1;
$MPIEXEC -nameserver localhost -n 1 $PYTHON -m coverage run $testdir/test_service.py
wait $mpi4pyserver
kill -TERM $nameserver
wait $nameserver 2>/dev/null || true
fi
$PYTHON -m coverage combine
mpi4py-3.0.3/conf/coverage-helper.py 0000644 0001750 0001750 00000002721 13200562156 020346 0 ustar dalcinl dalcinl 0000000 0000000 # ---
import mpi4py
try: mpi4py.get_include()
except: pass
try: mpi4py.get_config()
except: pass
# ---
def test_mpi4py_rc():
import mpi4py.rc
mpi4py.rc(
initialize = True,
threads = True,
thread_level = 'multiple',
finalize = None,
fast_reduce = True,
recv_mprobe = True,
errors = 'exception',
)
try: mpi4py.rc(qwerty=False)
except TypeError: pass
else: raise RuntimeError
test_mpi4py_rc()
# ---
def test_mpi4py_profile():
import mpi4py
def mpi4py_profile(*args, **kargs):
try: mpi4py.profile(*args, **kargs)
except ValueError: pass
import warnings
warnings.simplefilter('ignore')
mpi4py_profile('mpe')
mpi4py_profile('mpe', path="/usr/lib")
mpi4py_profile('mpe', path=["/usr/lib"])
mpi4py_profile('mpe', logfile="mpi4py")
mpi4py_profile('mpe', logfile="mpi4py")
mpi4py_profile('vt')
mpi4py_profile('vt', path="/usr/lib")
mpi4py_profile('vt', path=["/usr/lib"])
mpi4py_profile('vt', logfile="mpi4py")
mpi4py_profile('vt', logfile="mpi4py")
mpi4py_profile('@querty')
mpi4py_profile('c', path=["/usr/lib", "/usr/lib64"])
mpi4py_profile('m', path=["/usr/lib", "/usr/lib64"])
mpi4py_profile('dl', path=["/usr/lib", "/usr/lib64"])
mpi4py_profile('hosts', path=["/etc"])
test_mpi4py_profile()
# ---
import mpi4py.__main__
import mpi4py.bench
import mpi4py.futures
import mpi4py.futures.__main__
import mpi4py.futures.server
import mpi4py.run
mpi4py-3.0.3/conf/cythonize.bat 0000644 0001750 0001750 00000000162 13200562156 017425 0 ustar dalcinl dalcinl 0000000 0000000 @echo off
python -m cython --cleanup 3 -w src %* mpi4py.MPI.pyx
move src\mpi4py.MPI*.h src\mpi4py\include\mpi4py mpi4py-3.0.3/conf/mpidistutils.py 0000664 0001750 0001750 00000153227 13477641753 020063 0 ustar dalcinl dalcinl 0000000 0000000 # Author: Lisandro Dalcin
# Contact: dalcinl@gmail.com
"""
Support for building mpi4py with distutils/setuptools.
"""
# -----------------------------------------------------------------------------
import sys, os, platform
from distutils import sysconfig
from distutils.util import convert_path
from distutils.util import split_quoted
from distutils import log
# Fix missing variables PyPy's distutils.sysconfig
if hasattr(sys, 'pypy_version_info'):
config_vars = sysconfig.get_config_vars()
for name in ('prefix', 'exec_prefix'):
if name not in config_vars:
config_vars[name] = os.path.normpath(getattr(sys, name))
if sys.platform == 'darwin' and 'LDSHARED' in config_vars:
if '-undefined' not in config_vars['LDSHARED']:
config_vars['LDSHARED'] += ' -undefined dynamic_lookup'
# Workaround distutils.cygwinccompiler.get_versions()
# failing when the compiler path contains spaces
from distutils import cygwinccompiler as cygcc
cygcc_get_versions = cygcc.get_versions
def get_versions():
import distutils.spawn
find_executable_orig = distutils.spawn.find_executable
def find_executable(exe):
exe = find_executable_orig(exe)
if exe and ' ' in exe: exe = '"' + exe + '"'
return exe
distutils.spawn.find_executable = find_executable
versions = cygcc_get_versions()
distutils.spawn.find_executable = find_executable_orig
return versions
cygcc.get_versions = get_versions
# Normalize linker flags for runtime library dirs
from distutils.unixccompiler import UnixCCompiler
rpath_option_orig = UnixCCompiler.runtime_library_dir_option
def rpath_option(compiler, dir):
option = rpath_option_orig(compiler, dir)
if sys.platform.startswith('linux'):
if option.startswith('-R'):
option = option.replace('-R', '-Wl,-rpath,', 1)
elif option.startswith('-Wl,-R,'):
option = option.replace('-Wl,-R,', '-Wl,-rpath,', 1)
return option
UnixCCompiler.runtime_library_dir_option = rpath_option
def fix_compiler_cmd(cc, mpicc):
if not mpicc: return
i = 0
while os.path.basename(cc[i]) == 'env':
i = i + 1
while '=' in cc[i]:
i = i + 1
while os.path.basename(cc[i]) == 'ccache':
i = i + 1
cc[i:i+1] = split_quoted(mpicc)
def fix_linker_cmd(ld, mpild):
if not mpild: return
i = 0
if (sys.platform.startswith('aix') and
os.path.basename(ld[i]) == 'ld_so_aix'):
i = 1
while os.path.basename(ld[i]) == 'env':
i = i + 1
while '=' in ld[i]:
i = i + 1
while os.path.basename(ld[i]) == 'ccache':
del ld[i]
ld[i:i+1] = split_quoted(mpild)
def customize_compiler(compiler, lang=None,
mpicc=None, mpicxx=None, mpild=None,
):
sysconfig.customize_compiler(compiler)
if compiler.compiler_type == 'unix':
ld = compiler.linker_exe
for envvar in ('LDFLAGS', 'CFLAGS', 'CPPFLAGS'):
if envvar in os.environ:
ld += split_quoted(os.environ[envvar])
if sys.platform == 'darwin':
badcflags = ['-mno-fused-madd']
for attr in ('preprocessor',
'compiler', 'compiler_cxx', 'compiler_so',
'linker_so', 'linker_exe'):
compiler_cmd = getattr(compiler, attr, None)
if compiler_cmd is None: continue
for flag in badcflags:
while flag in compiler_cmd:
compiler_cmd.remove(flag)
if compiler.compiler_type == 'unix':
# Compiler command overriding
if mpicc:
fix_compiler_cmd(compiler.compiler, mpicc)
if lang in ('c', None):
fix_compiler_cmd(compiler.compiler_so, mpicc)
if mpicxx:
fix_compiler_cmd(compiler.compiler_cxx, mpicxx)
if lang == 'c++':
fix_compiler_cmd(compiler.compiler_so, mpicxx)
if mpild:
for ld in [compiler.linker_so, compiler.linker_exe]:
fix_linker_cmd(ld, mpild)
if compiler.compiler_type == 'cygwin':
compiler.set_executables(
preprocessor = 'gcc -mcygwin -E',
)
if compiler.compiler_type == 'mingw32':
compiler.set_executables(
preprocessor = 'gcc -mno-cygwin -E',
)
if compiler.compiler_type in ('unix', 'cygwin', 'mingw32'):
badcxxflags = [ '-Wimplicit', '-Wstrict-prototypes']
for flag in badcxxflags:
while flag in compiler.compiler_cxx:
compiler.compiler_cxx.remove(flag)
if lang == 'c++':
while flag in compiler.compiler_so:
compiler.compiler_so.remove(flag)
if compiler.compiler_type == 'mingw32':
# Remove msvcrXX.dll
del compiler.dll_libraries[:]
# http://bugs.python.org/issue12641
if compiler.gcc_version >= '4.4':
for attr in (
'preprocessor',
'compiler', 'compiler_cxx', 'compiler_so',
'linker_so', 'linker_exe'):
try: getattr(compiler, attr).remove('-mno-cygwin')
except: pass
# Add required define and compiler flags for AMD64
if platform.architecture()[0] == '64bit':
for attr in (
'preprocessor',
'compiler', 'compiler_cxx', 'compiler_so',
'linker_so', 'linker_exe'):
getattr(compiler, attr).insert(1, '-DMS_WIN64')
getattr(compiler, attr).insert(1, '-m64')
if compiler.compiler_type == 'msvc':
if not compiler.initialized: compiler.initialize()
compiler.ldflags_shared.append('/MANIFEST')
compiler.ldflags_shared_debug.append('/MANIFEST')
compile_options = (compiler.compile_options,
compiler.compile_options_debug)
from distutils.msvc9compiler import VERSION
if VERSION < 10.0:
for options in compile_options:
options.append('/D_USE_DECLSPECS_FOR_SAL=0')
options.append('/D_USE_ATTRIBUTES_FOR_SAL=0')
options.append('/DMSMPI_NO_SAL')
if VERSION <= 10.0:
topdir = os.path.dirname(os.path.dirname(__file__))
srcdir = os.path.abspath(os.path.join(topdir, 'src'))
header = os.path.join(srcdir, 'msvcfix.h')
if os.path.exists(header):
for options in compile_options:
options.append('/FI%s' % header)
# -----------------------------------------------------------------------------
from mpiconfig import Config
def configuration(command_obj, verbose=True):
config = Config()
config.setup(command_obj)
if verbose:
if config.section and config.filename:
log.info("MPI configuration: [%s] from '%s'",
config.section, ','.join(config.filename))
config.info(log)
return config
def configure_compiler(compiler, config, lang=None):
#
mpicc = config.get('mpicc')
mpicxx = config.get('mpicxx')
mpild = config.get('mpild')
if not mpild and (mpicc or mpicxx):
if lang == 'c': mpild = mpicc
if lang == 'c++': mpild = mpicxx
if not mpild: mpild = mpicc or mpicxx
#
customize_compiler(compiler, lang,
mpicc=mpicc, mpicxx=mpicxx, mpild=mpild)
#
for k, v in config.get('define_macros', []):
compiler.define_macro(k, v)
for v in config.get('undef_macros', []):
compiler.undefine_macro(v)
for v in config.get('include_dirs', []):
compiler.add_include_dir(v)
for v in config.get('libraries', []):
compiler.add_library(v)
for v in config.get('library_dirs', []):
compiler.add_library_dir(v)
for v in config.get('runtime_library_dirs', []):
compiler.add_runtime_library_dir(v)
for v in config.get('extra_objects', []):
compiler.add_link_object(v)
if compiler.compiler_type in \
('unix', 'intel', 'cygwin', 'mingw32'):
cc_args = config.get('extra_compile_args', [])
ld_args = config.get('extra_link_args', [])
compiler.compiler += cc_args
compiler.compiler_so += cc_args
compiler.compiler_cxx += cc_args
compiler.linker_so += ld_args
compiler.linker_exe += ld_args
return compiler
# -----------------------------------------------------------------------------
try:
from mpiscanner import Scanner
except ImportError:
class Scanner(object):
def parse_file(self, *args):
raise NotImplementedError(
"You forgot to grab 'mpiscanner.py'")
class ConfigureMPI(object):
SRCDIR = 'src'
SOURCES = [os.path.join('mpi4py', 'libmpi.pxd')]
DESTDIR = os.path.join('src', 'lib-mpi')
CONFIG_H = os.path.join('config', 'config.h')
MISSING_H = 'missing.h'
CONFIGTEST_H = """\
/* _configtest.h */
#if !defined(MPIAPI)
# define MPIAPI
#endif
"""
def __init__(self, config_cmd):
self.scanner = Scanner()
for filename in self.SOURCES:
fullname = os.path.join(self.SRCDIR, filename)
self.scanner.parse_file(fullname)
self.config_cmd = config_cmd
def run(self):
results = []
with open('_configtest.h', 'w') as f:
f.write(self.CONFIGTEST_H)
for node in self.scanner:
name = node.name
testcode = node.config()
confcode = node.missing(guard=False)
log.info("checking for '%s' ..." % name)
ok = self.run_test(testcode)
if not ok:
log.info("**** failed check for '%s'" % name)
with open('_configtest.h', 'a') as f:
f.write(confcode)
results.append((name, ok))
try: os.remove('_configtest.h')
except OSError: pass
return results
def gen_test(self, code):
body = ['#include "_configtest.h"',
'int main(int argc, char **argv) {',
'\n'.join([' ' + line for line in code.split('\n')]),
' (void)argc; (void)argv;',
' return 0;',
'}']
body = '\n'.join(body) + '\n'
return body
def run_test(self, code, lang='c'):
body = self.gen_test(code)
headers = ['stdlib.h', 'mpi.h']
ok = self.config_cmd.try_link(body, headers=headers, lang=lang)
return ok
def dump(self, results):
destdir = self.DESTDIR
config_h = os.path.join(destdir, self.CONFIG_H)
missing_h = os.path.join(destdir, self.MISSING_H)
log.info("writing '%s'", config_h)
self.scanner.dump_config_h(config_h, results)
log.info("writing '%s'", missing_h)
self.scanner.dump_missing_h(missing_h, None)
# -----------------------------------------------------------------------------
cmd_mpi_opts = [
('mpild=', None,
"MPI linker command, "
"overridden by environment variable 'MPILD' "
"(defaults to 'mpicc' or 'mpicxx' if any is available)"),
('mpif77=', None,
"MPI F77 compiler command, "
"overridden by environment variable 'MPIF77' "
"(defaults to 'mpif77' if available)"),
('mpif90=', None,
"MPI F90 compiler command, "
"overridden by environment variable 'MPIF90' "
"(defaults to 'mpif90' if available)"),
('mpifort=', None,
"MPI Fortran compiler command, "
"overridden by environment variable 'MPIFORT' "
"(defaults to 'mpifort' if available)"),
('mpicxx=', None,
"MPI C++ compiler command, "
"overridden by environment variable 'MPICXX' "
"(defaults to 'mpicxx', 'mpiCC', or 'mpic++' if any is available)"),
('mpicc=', None,
"MPI C compiler command, "
"overridden by environment variables 'MPICC' "
"(defaults to 'mpicc' if available)"),
('mpi=', None,
"specify a configuration section, "
"and an optional list of configuration files "
+ "(e.g. --mpi=section,file1" + os.path.pathsep + "file2), " +
"to look for MPI includes/libraries, "
"overridden by environment variable 'MPICFG' "
"(defaults to section 'mpi' in configuration file 'mpi.cfg')"),
('configure', None,
"exhaustive test for checking missing MPI constants/types/functions"),
]
def cmd_get_mpi_options(cmd_opts):
optlist = []
for (option, _, _) in cmd_opts:
if option[-1] == '=':
option = option[:-1]
option = option.replace('-','_')
optlist.append(option)
return optlist
def cmd_initialize_mpi_options(cmd):
mpiopts = cmd_get_mpi_options(cmd_mpi_opts)
for op in mpiopts:
setattr(cmd, op, None)
def cmd_set_undefined_mpi_options(cmd, basecmd):
mpiopts = cmd_get_mpi_options(cmd_mpi_opts)
optlist = tuple(zip(mpiopts, mpiopts))
cmd.set_undefined_options(basecmd, *optlist)
# -----------------------------------------------------------------------------
try:
import setuptools
except ImportError:
setuptools = None
def import_command(cmd):
try:
from importlib import import_module
except ImportError:
import_module = lambda n: __import__(n, fromlist=[None])
try:
if not setuptools: raise ImportError
return import_module('setuptools.command.' + cmd)
except ImportError:
return import_module('distutils.command.' + cmd)
if setuptools:
from setuptools import Distribution as cls_Distribution
from setuptools import Extension as cls_Extension
from setuptools import Command
else:
from distutils.core import Distribution as cls_Distribution
from distutils.core import Extension as cls_Extension
from distutils.core import Command
cmd_config = import_command('config')
cmd_build = import_command('build')
cmd_install = import_command('install')
cmd_sdist = import_command('sdist')
cmd_clean = import_command('clean')
cmd_build_clib = import_command('build_clib')
cmd_build_ext = import_command('build_ext')
cmd_install_lib = import_command('install_lib')
cmd_install_data = import_command('install_data')
from distutils.errors import DistutilsError
from distutils.errors import DistutilsSetupError
from distutils.errors import DistutilsPlatformError
from distutils.errors import DistutilsOptionError
from distutils.errors import CCompilerError
# -----------------------------------------------------------------------------
# Distribution class supporting a 'executables' keyword
class Distribution(cls_Distribution):
def __init__ (self, attrs=None):
# support for pkg data
self.package_data = {}
# PEP 314
self.provides = None
self.requires = None
self.obsoletes = None
# supports 'executables' keyword
self.executables = None
cls_Distribution.__init__(self, attrs)
def has_executables(self):
return self.executables and len(self.executables) > 0
def is_pure (self):
return (cls_Distribution.is_pure(self) and
not self.has_executables())
# Extension class
class Extension(cls_Extension):
def __init__ (self, **kw):
optional = kw.pop('optional', None)
configure = kw.pop('configure', None)
cls_Extension.__init__(self, **kw)
self.optional = optional
self.configure = configure
# Library class
class Library(Extension):
def __init__ (self, **kw):
kind = kw.pop('kind', "static")
package = kw.pop('package', None)
dest_dir = kw.pop('dest_dir', None)
Extension.__init__(self, **kw)
self.kind = kind
self.package = package
self.dest_dir = dest_dir
# Executable class
class Executable(Extension):
def __init__ (self, **kw):
package = kw.pop('package', None)
dest_dir = kw.pop('dest_dir', None)
Extension.__init__(self, **kw)
self.package = package
self.dest_dir = dest_dir
# setup function
def setup(**attrs):
if setuptools:
from setuptools import setup as fcn_setup
else:
from distutils.core import setup as fcn_setup
if 'distclass' not in attrs:
attrs['distclass'] = Distribution
if 'cmdclass' not in attrs:
attrs['cmdclass'] = {}
cmdclass = attrs['cmdclass']
for cmd in (config, build, install,
test, clean, sdist,
build_src, build_clib, build_ext, build_exe,
install_lib, install_data, install_exe,
):
if cmd.__name__ not in cmdclass:
cmdclass[cmd.__name__] = cmd
return fcn_setup(**attrs)
# -----------------------------------------------------------------------------
# A minimalistic MPI program :-)
ConfigTest = """\
int main(int argc, char **argv)
{
int ierr;
(void)argc; (void)argv;
ierr = MPI_Init(&argc, &argv);
if (ierr) return -1;
ierr = MPI_Finalize();
if (ierr) return -1;
return 0;
}
"""
class config(cmd_config.config):
user_options = cmd_config.config.user_options + cmd_mpi_opts
def initialize_options (self):
cmd_config.config.initialize_options(self)
cmd_initialize_mpi_options(self)
self.noisy = 0
def finalize_options (self):
cmd_config.config.finalize_options(self)
if not self.noisy:
self.dump_source = 0
def _clean(self, *a, **kw):
if sys.platform.startswith('win'):
for fn in ('_configtest.exe.manifest', ):
if os.path.exists(fn):
self.temp_files.append(fn)
cmd_config.config._clean(self, *a, **kw)
def check_header (self, header, headers=None, include_dirs=None):
if headers is None: headers = []
log.info("checking for header '%s' ..." % header)
body = "int main(int n, char**v) { (void)n; (void)v; return 0; }"
ok = self.try_compile(body, list(headers) + [header], include_dirs)
log.info(ok and 'success!' or 'failure.')
return ok
def check_macro (self, macro, headers=None, include_dirs=None):
log.info("checking for macro '%s' ..." % macro)
body = ("#ifndef %s\n"
"#error macro '%s' not defined\n"
"#endif\n") % (macro, macro)
body += "int main(int n, char**v) { (void)n; (void)v; return 0; }"
ok = self.try_compile(body, headers, include_dirs)
return ok
def check_library (self, library, library_dirs=None,
headers=None, include_dirs=None,
other_libraries=[], lang="c"):
if sys.platform == "darwin":
self.compiler.linker_exe.append('-flat_namespace')
self.compiler.linker_exe.append('-undefined')
self.compiler.linker_exe.append('suppress')
log.info("checking for library '%s' ..." % library)
body = "int main(int n, char**v) { (void)n; (void)v; return 0; }"
ok = self.try_link(body, headers, include_dirs,
[library]+other_libraries, library_dirs,
lang=lang)
if sys.platform == "darwin":
self.compiler.linker_exe.remove('-flat_namespace')
self.compiler.linker_exe.remove('-undefined')
self.compiler.linker_exe.remove('suppress')
return ok
def check_function (self, function,
headers=None, include_dirs=None,
libraries=None, library_dirs=None,
decl=0, call=0, lang="c"):
log.info("checking for function '%s' ..." % function)
body = []
if decl:
if call: proto = "int %s (void);"
else: proto = "int %s;"
if lang == "c":
proto = "\n".join([
"#ifdef __cplusplus",
"extern \"C\"",
"#endif", proto])
body.append(proto % function)
body.append( "int main (int n, char**v) {")
if call:
body.append(" (void)%s();" % function)
else:
body.append(" %s;" % function)
body.append( " (void)n; (void)v;")
body.append( " return 0;")
body.append( "}")
body = "\n".join(body) + "\n"
ok = self.try_link(body, headers, include_dirs,
libraries, library_dirs, lang=lang)
return ok
def check_symbol (self, symbol, type="int",
headers=None, include_dirs=None,
libraries=None, library_dirs=None,
decl=0, lang="c"):
log.info("checking for symbol '%s' ..." % symbol)
body = []
if decl:
body.append("%s %s;" % (type, symbol))
body.append("int main (int n, char**v) {")
body.append(" %s s; s = %s; (void)s;" % (type, symbol))
body.append(" (void)n; (void)v;")
body.append(" return 0;")
body.append("}")
body = "\n".join(body) + "\n"
ok = self.try_link(body, headers, include_dirs,
libraries, library_dirs, lang=lang)
return ok
def check_function_call (self, function, args='',
headers=None, include_dirs=None,
libraries=None, library_dirs=None,
lang="c"):
log.info("checking for function '%s' ..." % function)
body = []
body.append("int main (int n, char**v) {")
body.append(" (void)%s(%s);" % (function, args))
body.append(" (void)n; (void)v;")
body.append(" return 0;")
body.append("}")
body = "\n".join(body) + "\n"
ok = self.try_link(body, headers, include_dirs,
libraries, library_dirs, lang=lang)
return ok
check_hdr = check_header
check_lib = check_library
check_func = check_function
check_sym = check_symbol
def run (self):
#
config = configuration(self, verbose=True)
# test MPI C compiler
self.compiler = getattr(
self.compiler, 'compiler_type', self.compiler)
self._check_compiler()
configure_compiler(self.compiler, config, lang='c')
self.try_link(ConfigTest, headers=['mpi.h'], lang='c')
# test MPI C++ compiler
self.compiler = getattr(
self.compiler, 'compiler_type', self.compiler)
self._check_compiler()
configure_compiler(self.compiler, config, lang='c++')
self.try_link(ConfigTest, headers=['mpi.h'], lang='c++')
class build(cmd_build.build):
user_options = cmd_build.build.user_options + cmd_mpi_opts
def initialize_options(self):
cmd_build.build.initialize_options(self)
cmd_initialize_mpi_options(self)
def finalize_options(self):
cmd_build.build.finalize_options(self)
config_cmd = self.get_finalized_command('config')
if isinstance(config_cmd, config):
cmd_set_undefined_mpi_options(self, 'config')
def has_executables (self):
return self.distribution.has_executables()
sub_commands = \
[('build_src', lambda *args: True)] + \
cmd_build.build.sub_commands + \
[('build_exe', has_executables)]
# XXX disable build_exe subcommand !!!
del sub_commands[-1]
class build_src(Command):
description = "build C sources from Cython files"
user_options = [
('force', 'f',
"forcibly build everything (ignore file timestamps)"),
]
boolean_options = ['force']
def initialize_options(self):
self.force = False
def finalize_options(self):
self.set_undefined_options('build',
('force', 'force'),
)
def run(self):
pass
# Command class to build libraries
class build_clib(cmd_build_clib.build_clib):
user_options = [
('build-clib-a=', 's',
"directory to build C/C++ static libraries to"),
('build-clib-so=', 's',
"directory to build C/C++ shared libraries to"),
]
user_options += cmd_build_clib.build_clib.user_options + cmd_mpi_opts
def initialize_options (self):
self.libraries = None
self.libraries_a = []
self.libraries_so = []
self.library_dirs = None
self.rpath = None
self.link_objects = None
self.build_lib = None
self.build_clib_a = None
self.build_clib_so = None
cmd_build_clib.build_clib.initialize_options(self)
cmd_initialize_mpi_options(self)
def finalize_options (self):
cmd_build_clib.build_clib.finalize_options(self)
build_cmd = self.get_finalized_command('build')
if isinstance(build_cmd, build):
cmd_set_undefined_mpi_options(self, 'build')
#
self.set_undefined_options('build',
('build_lib', 'build_lib'),
('build_lib', 'build_clib_a'),
('build_lib', 'build_clib_so'))
#
if self.libraries:
libraries = self.libraries[:]
self.libraries = []
self.check_library_list (libraries)
for i, lib in enumerate(libraries):
if isinstance(lib, Library):
if lib.kind == "static":
self.libraries_a.append(lib)
else:
self.libraries_so.append(lib)
else:
self.libraries.append(lib)
def check_library_list (self, libraries):
ListType, TupleType = type([]), type(())
if not isinstance(libraries, ListType):
raise DistutilsSetupError(
"'libraries' option must be a list of "
"Library instances or 2-tuples")
for lib in libraries:
#
if isinstance(lib, Library):
lib_name = lib.name
build_info = lib.__dict__
elif isinstance(lib, TupleType) and len(lib) == 2:
lib_name, build_info = lib
else:
raise DistutilsSetupError(
"each element of 'libraries' option must be an "
"Library instance or 2-tuple")
#
if not isinstance(lib_name, str):
raise DistutilsSetupError(
"first element of each tuple in 'libraries' "
"must be a string (the library name)")
if '/' in lib_name or (os.sep != '/' and os.sep in lib_name):
raise DistutilsSetupError(
"bad library name '%s': "
"may not contain directory separators" % lib[0])
if not isinstance(build_info, dict):
raise DistutilsSetupError(
"second element of each tuple in 'libraries' "
"must be a dictionary (build info)")
lib_type = build_info.get('kind', 'static')
if lib_type not in ('static', 'shared', 'dylib'):
raise DistutilsSetupError(
"in 'kind' option (library '%s'), "
"'kind' must be one of "
" \"static\", \"shared\", \"dylib\"" % lib_name)
sources = build_info.get('sources')
if (sources is None or
type(sources) not in (ListType, TupleType)):
raise DistutilsSetupError(
"in 'libraries' option (library '%s'), "
"'sources' must be present and must be "
"a list of source filenames" % lib_name)
depends = build_info.get('depends')
if (depends is not None and
type(depends) not in (ListType, TupleType)):
raise DistutilsSetupError(
"in 'libraries' option (library '%s'), "
"'depends' must be a list "
"of source filenames" % lib_name)
def run (self):
cmd_build_clib.build_clib.run(self)
if (not self.libraries_a and
not self.libraries_so):
return
#
from distutils.ccompiler import new_compiler
self.compiler = new_compiler(compiler=self.compiler,
dry_run=self.dry_run,
force=self.force)
#
if self.define is not None:
for (name, value) in self.define:
self.compiler.define_macro(name, value)
if self.undef is not None:
for macro in self.undef:
self.compiler.undefine_macro(macro)
if self.include_dirs is not None:
self.compiler.set_include_dirs(self.include_dirs)
if self.library_dirs is not None:
self.compiler.set_library_dirs(self.library_dirs)
if self.rpath is not None:
self.compiler.set_runtime_library_dirs(self.rpath)
if self.link_objects is not None:
self.compiler.set_link_objects(self.link_objects)
#
config = configuration(self, verbose=True)
configure_compiler(self.compiler, config)
if self.compiler.compiler_type == "unix":
try: del self.compiler.shared_lib_extension
except: pass
#
self.build_libraries(self.libraries)
self.build_libraries(self.libraries_a)
self.build_libraries(self.libraries_so)
def build_libraries (self, libraries):
for lib in libraries:
# old-style
if not isinstance(lib, Library):
cmd_build_clib.build_clib.build_libraries(self, [lib])
continue
# new-style
try:
self.build_library(lib)
except (DistutilsError, CCompilerError):
if not lib.optional: raise
e = sys.exc_info()[1]
self.warn('%s' % e)
self.warn('building optional library "%s" failed' % lib.name)
def config_library (self, lib):
if lib.configure:
config_cmd = self.get_finalized_command('config')
config_cmd.compiler = self.compiler # fix compiler
return lib.configure(lib, config_cmd)
def build_library(self, lib):
from distutils.dep_util import newer_group
sources = [convert_path(p) for p in lib.sources]
depends = [convert_path(p) for p in lib.depends]
depends = sources + depends
if lib.kind == "static":
build_dir = self.build_clib_a
else:
build_dir = self.build_clib_so
lib_fullpath = self.get_lib_fullpath(lib, build_dir)
if not (self.force or newer_group(depends, lib_fullpath, 'newer')):
log.debug("skipping '%s' %s library (up-to-date)",
lib.name, lib.kind)
return
ok = self.config_library(lib)
log.info("building '%s' %s library", lib.name, lib.kind)
# First, compile the source code to object files in the library
# directory. (This should probably change to putting object
# files in a temporary build directory.)
macros = lib.define_macros[:]
for undef in lib.undef_macros:
macros.append((undef,))
objects = self.compiler.compile(
sources,
depends=lib.depends,
output_dir=self.build_temp,
macros=macros,
include_dirs=lib.include_dirs,
extra_preargs=None,
extra_postargs=lib.extra_compile_args,
debug=self.debug,
)
if lib.kind == "static":
# Now "link" the object files together
# into a static library.
self.compiler.create_static_lib(
objects,
lib.name,
output_dir=os.path.dirname(lib_fullpath),
debug=self.debug,
)
else:
extra_objects = lib.extra_objects[:]
export_symbols = lib.export_symbols[:]
extra_link_args = lib.extra_link_args[:]
extra_preargs = None
objects.extend(extra_objects)
if (self.compiler.compiler_type == 'msvc' and
export_symbols is not None):
output_dir = os.path.dirname(lib_fullpath)
implib_filename = self.compiler.library_filename(lib.name)
implib_file = os.path.join(output_dir, lib_fullpath)
extra_link_args.append ('/IMPLIB:' + implib_file)
# Detect target language, if not provided
src_language = self.compiler.detect_language(sources)
language = (lib.language or src_language)
# Now "link" the object files together
# into a shared library.
if sys.platform == 'darwin':
linker_so = self.compiler.linker_so[:]
while '-bundle' in self.compiler.linker_so:
pos = self.compiler.linker_so.index('-bundle')
self.compiler.linker_so[pos] = '-shared'
install_name = os.path.basename(lib_fullpath)
extra_preargs = ['-install_name', install_name]
if sys.platform.startswith('linux'):
extra_preargs = ['-Wl,--no-as-needed']
self.compiler.link(
self.compiler.SHARED_LIBRARY,
objects, lib_fullpath,
#
libraries=lib.libraries,
library_dirs=lib.library_dirs,
runtime_library_dirs=lib.runtime_library_dirs,
export_symbols=export_symbols,
extra_preargs=extra_preargs,
extra_postargs=extra_link_args,
debug=self.debug,
target_lang=language,
)
if sys.platform == 'darwin':
self.compiler.linker_so = linker_so
return
def get_lib_fullpath (self, lib, build_dir):
package_dir = (lib.package or '').split('.')
dest_dir = convert_path(lib.dest_dir or '')
output_dir = os.path.join(build_dir, *package_dir+[dest_dir])
lib_type = lib.kind
if sys.platform != 'darwin':
if lib_type == 'dylib':
lib_type = 'shared'
lib_fullpath = self.compiler.library_filename(
lib.name, lib_type=lib_type, output_dir=output_dir)
return lib_fullpath
def get_source_files (self):
filenames = cmd_build_clib.build_clib.get_source_files(self)
self.check_library_list(self.libraries)
self.check_library_list(self.libraries_a)
self.check_library_list(self.libraries_so)
for (lib_name, build_info) in self.libraries:
filenames.extend(build_info.get(sources, []))
for lib in self.libraries_so + self.libraries_a:
filenames.extend(lib.sources)
return filenames
def get_outputs (self):
outputs = []
for lib in self.libraries_a:
lib_fullpath = self.get_lib_fullpath(lib, self.build_clib_a)
outputs.append(lib_fullpath)
for lib in self.libraries_so:
lib_fullpath = self.get_lib_fullpath(lib, self.build_clib_so)
outputs.append(lib_fullpath)
return outputs
# Command class to build extension modules
class build_ext(cmd_build_ext.build_ext):
user_options = cmd_build_ext.build_ext.user_options + cmd_mpi_opts
def initialize_options(self):
cmd_build_ext.build_ext.initialize_options(self)
cmd_initialize_mpi_options(self)
def finalize_options(self):
cmd_build_ext.build_ext.finalize_options(self)
build_cmd = self.get_finalized_command('build')
if isinstance(build_cmd, build):
cmd_set_undefined_mpi_options(self, 'build')
#
if ((sys.platform.startswith('linux') or
sys.platform.startswith('gnu') or
sys.platform.startswith('sunos')) and
sysconfig.get_config_var('Py_ENABLE_SHARED')):
# Remove /lib[64]/pythonX.Y/config
libdir = os.path.dirname(sysconfig.get_makefile_filename())
if libdir in self.library_dirs:
self.library_dirs.remove(bad_libdir)
# Add /lib[64]
libdir = sysconfig.get_config_var("LIBDIR")
if libdir not in self.library_dirs:
self.library_dirs.append(libdir)
if libdir not in self.rpath:
self.rpath.append(libdir)
# Special-case
if sys.exec_prefix == '/usr':
self.library_dirs.remove(libdir)
self.rpath.remove(libdir)
def run (self):
if self.distribution.has_c_libraries():
build_clib = self.get_finalized_command('build_clib')
if build_clib.libraries:
build_clib.run()
cmd_build_ext.build_ext.run(self)
def build_extensions(self):
from copy import deepcopy
# First, sanity-check the 'extensions' list
self.check_extensions_list(self.extensions)
# customize compiler
self.compiler_sys = deepcopy(self.compiler)
customize_compiler(self.compiler_sys)
# parse configuration file and configure compiler
self.compiler_mpi = self.compiler
self.config = configuration(self, verbose=True)
configure_compiler(self.compiler, self.config)
# extra configuration, check for all MPI symbols
if self.configure:
log.info('testing for missing MPI symbols')
config_cmd = self.get_finalized_command('config')
config_cmd.compiler = self.compiler # fix compiler
configure = ConfigureMPI(config_cmd)
results = configure.run()
configure.dump(results)
#
macro = 'HAVE_CONFIG_H'
log.info("defining preprocessor macro '%s'" % macro)
self.compiler.define_macro(macro, 1)
# build extensions
for ext in self.extensions:
try:
self.build_extension(ext)
except (DistutilsError, CCompilerError):
if not ext.optional: raise
e = sys.exc_info()[1]
self.warn('%s' % e)
exe = isinstance(ext, Executable)
knd = 'executable' if exe else 'extension'
self.warn('building optional %s "%s" failed' % (knd, ext.name))
def config_extension (self, ext):
configure = getattr(ext, 'configure', None)
if configure:
config_cmd = self.get_finalized_command('config')
config_cmd.compiler = self.compiler # fix compiler
configure(ext, config_cmd)
def build_extension (self, ext):
from distutils.dep_util import newer_group
fullname = self.get_ext_fullname(ext.name)
filename = os.path.join(
self.build_lib, self.get_ext_filename(fullname))
depends = ext.sources + ext.depends
if not (self.force or newer_group(depends, filename, 'newer')):
log.debug("skipping '%s' extension (up-to-date)", ext.name)
return
#
# XXX -- this is a Vile HACK!
self.compiler = self.compiler_mpi
if ext.name == 'mpi4py.dl':
self.compiler = self.compiler_sys
#
self.config_extension(ext)
cmd_build_ext.build_ext.build_extension(self, ext)
#
# XXX -- this is a Vile HACK!
if ext.name == 'mpi4py.MPI':
dest_dir = os.path.dirname(filename)
self.mkpath(dest_dir)
mpi_cfg = os.path.join(dest_dir, 'mpi.cfg')
log.info("writing %s" % mpi_cfg)
if not self.dry_run:
self.config.dump(filename=mpi_cfg)
def get_outputs(self):
outputs = cmd_build_ext.build_ext.get_outputs(self)
for ext in self.extensions:
# XXX -- this is a Vile HACK!
if ext.name == 'mpi4py.MPI':
fullname = self.get_ext_fullname(ext.name)
filename = os.path.join(
self.build_lib,
self.get_ext_filename(fullname))
dest_dir = os.path.dirname(filename)
mpi_cfg = os.path.join(dest_dir, 'mpi.cfg')
outputs.append(mpi_cfg)
return outputs
# Command class to build executables
class build_exe(build_ext):
description = "build binary executable components"
user_options = [
('build-exe=', None,
"build directory for executable components"),
] + build_ext.user_options
def initialize_options (self):
build_ext.initialize_options(self)
self.build_base = None
self.build_exe = None
def finalize_options (self):
build_ext.finalize_options(self)
self.configure = None
self.set_undefined_options('build',
('build_base','build_base'),
('build_lib', 'build_exe'))
self.executables = self.distribution.executables
# XXX This is a hack
self.extensions = self.distribution.executables
self.check_extensions_list = self.check_executables_list
self.build_extension = self.build_executable
self.get_ext_filename = self.get_exe_filename
self.build_lib = self.build_exe
def check_executables_list (self, executables):
ListType, TupleType = type([]), type(())
if type(executables) is not ListType:
raise DistutilsSetupError(
"'executables' option must be a list of Executable instances")
for exe in executables:
if not isinstance(exe, Executable):
raise DistutilsSetupError(
"'executables' items must be Executable instances")
if (exe.sources is None or
type(exe.sources) not in (ListType, TupleType)):
raise DistutilsSetupError(
("in 'executables' option (executable '%s'), " +
"'sources' must be present and must be " +
"a list of source filenames") % exe.name)
def get_exe_filename(self, exe_name):
exe_ext = sysconfig.get_config_var('EXE') or ''
return exe_name + exe_ext
def get_exe_fullpath(self, exe, build_dir=None):
build_dir = build_dir or self.build_exe
package_dir = (exe.package or '').split('.')
dest_dir = convert_path(exe.dest_dir or '')
output_dir = os.path.join(build_dir, *package_dir+[dest_dir])
exe_filename = self.get_exe_filename(exe.name)
return os.path.join(output_dir, exe_filename)
def config_executable (self, exe):
build_ext.config_extension(self, exe)
def build_executable (self, exe):
from distutils.dep_util import newer_group
sources = list(exe.sources)
depends = list(exe.depends)
exe_fullpath = self.get_exe_fullpath(exe)
depends = sources + depends
if not (self.force or newer_group(depends, exe_fullpath, 'newer')):
log.debug("skipping '%s' executable (up-to-date)", exe.name)
return
self.config_executable(exe)
log.info("building '%s' executable", exe.name)
# Next, compile the source code to object files.
# XXX not honouring 'define_macros' or 'undef_macros' -- the
# CCompiler API needs to change to accommodate this, and I
# want to do one thing at a time!
macros = exe.define_macros[:]
for undef in exe.undef_macros:
macros.append((undef,))
# Two possible sources for extra compiler arguments:
# - 'extra_compile_args' in Extension object
# - CFLAGS environment variable (not particularly
# elegant, but people seem to expect it and I
# guess it's useful)
# The environment variable should take precedence, and
# any sensible compiler will give precedence to later
# command line args. Hence we combine them in order:
extra_args = exe.extra_compile_args[:]
objects = self.compiler.compile(
sources,
output_dir=self.build_temp,
macros=macros,
include_dirs=exe.include_dirs,
debug=self.debug,
extra_postargs=extra_args,
depends=exe.depends)
self._built_objects = objects[:]
# Now link the object files together into a "shared object" --
# of course, first we have to figure out all the other things
# that go into the mix.
if exe.extra_objects:
objects.extend(exe.extra_objects)
extra_args = exe.extra_link_args[:]
# Get special linker flags for building a executable with
# bundled Python library, also fix location of needed
# python.exp file on AIX
ldshflag = sysconfig.get_config_var('LINKFORSHARED') or ''
ldshflag = ldshflag.replace('-Xlinker ', '-Wl,')
if sys.platform == 'darwin': # fix wrong framework paths
fwkprefix = sysconfig.get_config_var('PYTHONFRAMEWORKPREFIX')
fwkdir = sysconfig.get_config_var('PYTHONFRAMEWORKDIR')
if fwkprefix and fwkdir and fwkdir != 'no-framework':
for flag in split_quoted(ldshflag):
if flag.startswith(fwkdir):
fwkpath = os.path.join(fwkprefix, flag)
ldshflag = ldshflag.replace(flag, fwkpath)
if sys.platform.startswith('aix'):
python_lib = sysconfig.get_python_lib(standard_lib=1)
python_exp = os.path.join(python_lib, 'config', 'python.exp')
ldshflag = ldshflag.replace('Modules/python.exp', python_exp)
# Detect target language, if not provided
language = exe.language or self.compiler.detect_language(sources)
self.compiler.link(
self.compiler.EXECUTABLE,
objects, exe_fullpath,
output_dir=None,
libraries=self.get_libraries(exe),
library_dirs=exe.library_dirs,
runtime_library_dirs=exe.runtime_library_dirs,
extra_preargs=split_quoted(ldshflag),
extra_postargs=extra_args,
debug=self.debug,
target_lang=language)
def get_outputs (self):
outputs = []
for exe in self.executables:
outputs.append(self.get_exe_fullpath(exe))
return outputs
class install(cmd_install.install):
def run(self):
cmd_install.install.run(self)
def has_lib (self):
return (cmd_install.install.has_lib(self) and
self.has_exe())
def has_exe (self):
return self.distribution.has_executables()
sub_commands = \
cmd_install.install.sub_commands[:] + \
[('install_exe', has_exe)]
# XXX disable install_exe subcommand !!!
del sub_commands[-1]
class install_lib(cmd_install_lib.install_lib):
def get_outputs(self):
outputs = cmd_install_lib.install_lib.get_outputs(self)
for (build_cmd, build_dir) in (('build_clib', 'build_lib'),
('build_exe', 'build_exe')):
outs = self._mutate_outputs(1, build_cmd, build_dir,
self.install_dir)
build_cmd = self.get_finalized_command(build_cmd)
build_files = build_cmd.get_outputs()
for out in outs:
if os.path.exists(out):
outputs.append(out)
return outputs
class install_data (cmd_install_data.install_data):
def finalize_options (self):
self.set_undefined_options('install',
('install_lib', 'install_dir'),
('root', 'root'),
('force', 'force'),
)
class install_exe(cmd_install_lib.install_lib):
description = "install binary executable components"
user_options = [
('install-dir=', 'd', "directory to install to"),
('build-dir=','b', "build directory (where to install from)"),
('force', 'f', "force installation (overwrite existing files)"),
('skip-build', None, "skip the build steps"),
]
boolean_options = ['force', 'skip-build']
negative_opt = { }
def initialize_options (self):
self.install_dir = None
self.build_dir = None
self.force = 0
self.skip_build = None
def finalize_options (self):
self.set_undefined_options('build_exe',
('build_exe', 'build_dir'))
self.set_undefined_options('install',
('force', 'force'),
('skip_build', 'skip_build'),
('install_scripts', 'install_dir'))
def run (self):
self.build()
self.install()
def build (self):
if not self.skip_build:
if self.distribution.has_executables():
self.run_command('build_exe')
def install (self):
self.outfiles = []
if self.distribution.has_executables():
build_exe = self.get_finalized_command('build_exe')
for exe in build_exe.executables:
exe_fullpath = build_exe.get_exe_fullpath(exe)
exe_filename = os.path.basename(exe_fullpath)
if (os.name == "posix" and
exe_filename.startswith("python-")):
install_name = exe_filename.replace(
"python-","python%s-" % sys.version[:3])
link = None
else:
install_name = exe_fullpath
link = None
source = exe_fullpath
target = os.path.join(self.install_dir, install_name)
self.mkpath(self.install_dir)
out, done = self.copy_file(source, target, link=link)
self.outfiles.append(out)
def get_outputs (self):
return self.outfiles
def get_inputs (self):
inputs = []
if self.distribution.has_executables():
build_exe = self.get_finalized_command('build_exe')
inputs.extend(build_exe.get_outputs())
return inputs
class test(Command):
description = "run the test suite"
user_options = [
('args=', 'a', "options"),
]
def initialize_options(self):
self.args = None
def finalize_options(self):
if self.args:
self.args = split_quoted(self.args)
else:
self.args = []
def run(self):
pass
class sdist(cmd_sdist.sdist):
def run (self):
build_src = self.get_finalized_command('build_src')
build_src.run()
cmd_sdist.sdist.run(self)
class clean(cmd_clean.clean):
description = "clean up temporary files from 'build' command"
user_options = \
cmd_clean.clean.user_options[:2] + [
('build-exe=', None,
"build directory for executable components "
"(default: 'build_exe.build-exe')"),
] + cmd_clean.clean.user_options[2:]
def initialize_options(self):
cmd_clean.clean.initialize_options(self)
self.build_exe = None
def finalize_options(self):
cmd_clean.clean.finalize_options(self)
self.set_undefined_options('build_exe',
('build_exe', 'build_exe'))
def run(self):
from distutils.dir_util import remove_tree
# remove the build/temp. directory
# (unless it's already gone)
if os.path.exists(self.build_temp):
remove_tree(self.build_temp, dry_run=self.dry_run)
else:
log.debug("'%s' does not exist -- can't clean it",
self.build_temp)
if self.all:
# remove build directories
for directory in (self.build_lib,
self.build_exe,
self.build_scripts,
self.bdist_base,
):
if os.path.exists(directory):
remove_tree(directory, dry_run=self.dry_run)
else:
log.debug("'%s' does not exist -- can't clean it",
directory)
# just for the heck of it, try to remove the base build directory:
# we might have emptied it right now, but if not we don't care
if not self.dry_run:
try:
os.rmdir(self.build_base)
log.info("removing '%s'", self.build_base)
except OSError:
pass
if self.all:
# remove the .egg_info directory
try:
egg_info = self.get_finalized_command('egg_info').egg_info
if os.path.exists(egg_info):
remove_tree(egg_info, dry_run=self.dry_run)
else:
log.debug("'%s' does not exist -- can't clean it",
egg_info)
except DistutilsError:
pass
# -----------------------------------------------------------------------------
if setuptools:
try:
from setuptools.command import egg_info as mod_egg_info
_FileList = mod_egg_info.FileList
class FileList(_FileList):
def process_template_line(self, line):
level = log.set_threshold(log.ERROR)
try:
_FileList.process_template_line(self, line)
finally:
log.set_threshold(level)
mod_egg_info.FileList = FileList
except:
pass
# -----------------------------------------------------------------------------
try:
import msilib
if not hasattr(msilib, 'Win64'):
if hasattr(msilib, 'AMD64'):
msilib.Win64 = msilib.AMD64
Directory_make_short = msilib.Directory.make_short
def make_short(self, file):
parts = file.split('.')
if len(parts) > 1:
file = '_'.join(parts[:-1])+'.'+parts[-1]
return Directory_make_short(self, file)
msilib.Directory.make_short = make_short
except:
pass
# -----------------------------------------------------------------------------
mpi4py-3.0.3/test/ 0000775 0001750 0001750 00000000000 13560002767 014764 5 ustar dalcinl dalcinl 0000000 0000000 mpi4py-3.0.3/test/test_cco_obj_inter.py 0000644 0001750 0001750 00000017012 13200562156 021165 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
from functools import reduce
cumsum = lambda seq: reduce(lambda x, y: x+y, seq, 0)
cumprod = lambda seq: reduce(lambda x, y: x*y, seq, 1)
_basic = [None,
True, False,
-7, 0, 7, 2**31,
-2**63+1, 2**63-1,
-2.17, 0.0, 3.14,
1+2j, 2-3j,
'mpi4py',
]
messages = _basic
messages += [ list(_basic),
tuple(_basic),
dict([('k%d' % key, val)
for key, val in enumerate(_basic)])
]
@unittest.skipMPI('openmpi(<1.6.0)')
@unittest.skipMPI('MPICH1')
@unittest.skipIf(MPI.ROOT == MPI.PROC_NULL, 'mpi-root')
@unittest.skipIf(MPI.COMM_WORLD.Get_size() < 2, 'mpi-world-size<2')
class BaseTestCCOObjInter(object):
BASECOMM = MPI.COMM_NULL
INTRACOMM = MPI.COMM_NULL
INTERCOMM = MPI.COMM_NULL
def setUp(self):
size = self.BASECOMM.Get_size()
rank = self.BASECOMM.Get_rank()
if rank < size // 2 :
self.COLOR = 0
self.LOCAL_LEADER = 0
self.REMOTE_LEADER = size // 2
else:
self.COLOR = 1
self.LOCAL_LEADER = 0
self.REMOTE_LEADER = 0
self.INTRACOMM = self.BASECOMM.Split(self.COLOR, key=0)
Create_intercomm = MPI.Intracomm.Create_intercomm
self.INTERCOMM = Create_intercomm(self.INTRACOMM,
self.LOCAL_LEADER,
self.BASECOMM,
self.REMOTE_LEADER)
def tearDown(self):
self.INTRACOMM.Free()
self.INTERCOMM.Free()
@unittest.skipMPI('MPICH2(<1.0.8)')
def testBarrier(self):
self.INTERCOMM.Barrier()
def testBcast(self):
rank = self.INTERCOMM.Get_rank()
size = self.INTERCOMM.Get_size()
rsize = self.INTERCOMM.Get_remote_size()
for smess in messages + [messages]:
for color in [0, 1]:
if self.COLOR == color:
for root in range(size):
if root == rank:
rmess = self.INTERCOMM.bcast(smess, root=MPI.ROOT)
else:
rmess = self.INTERCOMM.bcast(None, root=MPI.PROC_NULL)
self.assertEqual(rmess, None)
else:
for root in range(rsize):
rmess = self.INTERCOMM.bcast(None, root=root)
self.assertEqual(rmess, smess)
def testGather(self):
rank = self.INTERCOMM.Get_rank()
size = self.INTERCOMM.Get_size()
rsize = self.INTERCOMM.Get_remote_size()
for smess in messages + [messages]:
for color in [0, 1]:
if self.COLOR == color:
for root in range(size):
if root == rank:
rmess = self.INTERCOMM.gather(smess, root=MPI.ROOT)
self.assertEqual(rmess, [smess] * rsize)
else:
rmess = self.INTERCOMM.gather(None, root=MPI.PROC_NULL)
self.assertEqual(rmess, None)
else:
for root in range(rsize):
rmess = self.INTERCOMM.gather(smess, root=root)
self.assertEqual(rmess, None)
def testScatter(self):
rank = self.INTERCOMM.Get_rank()
size = self.INTERCOMM.Get_size()
rsize = self.INTERCOMM.Get_remote_size()
for smess in messages + [messages]:
for color in [0, 1]:
if self.COLOR == color:
for root in range(size):
if root == rank:
rmess = self.INTERCOMM.scatter([smess] * rsize, root=MPI.ROOT)
else:
rmess = self.INTERCOMM.scatter(None, root=MPI.PROC_NULL)
self.assertEqual(rmess, None)
else:
for root in range(rsize):
rmess = self.INTERCOMM.scatter(None, root=root)
self.assertEqual(rmess, smess)
@unittest.skipMPI('MPICH2(<1.0.8)')
def testAllgather(self):
rank = self.INTERCOMM.Get_rank()
size = self.INTERCOMM.Get_size()
rsize = self.INTERCOMM.Get_remote_size()
for smess in messages + [messages]:
rmess = self.INTERCOMM.allgather(smess)
self.assertEqual(rmess, [smess] * rsize)
def testAlltoall(self):
rank = self.INTERCOMM.Get_rank()
size = self.INTERCOMM.Get_size()
rsize = self.INTERCOMM.Get_remote_size()
for smess in messages + [messages]:
rmess = self.INTERCOMM.alltoall([smess] * rsize)
self.assertEqual(rmess, [smess] * rsize)
def testReduce(self):
rank = self.INTERCOMM.Get_rank()
size = self.INTERCOMM.Get_size()
rsize = self.INTERCOMM.Get_remote_size()
for op in (MPI.SUM, MPI.MAX, MPI.MIN, MPI.PROD):
for color in [0, 1]:
if self.COLOR == color:
for root in range(size):
if root == rank:
value = self.INTERCOMM.reduce(None, op=op, root=MPI.ROOT)
if op == MPI.SUM:
self.assertEqual(value, cumsum(range(rsize)))
elif op == MPI.PROD:
self.assertEqual(value, cumprod(range(rsize)))
elif op == MPI.MAX:
self.assertEqual(value, rsize-1)
elif op == MPI.MIN:
self.assertEqual(value, 0)
else:
value = self.INTERCOMM.reduce(None, op=op, root=MPI.PROC_NULL)
self.assertEqual(value, None)
else:
for root in range(rsize):
value = self.INTERCOMM.reduce(rank, op=op, root=root)
self.assertEqual(value, None)
@unittest.skipMPI('MPICH2(<1.0.8)')
def testAllreduce(self):
rank = self.INTERCOMM.Get_rank()
size = self.INTERCOMM.Get_size()
rsize = self.INTERCOMM.Get_remote_size()
for op in (MPI.SUM, MPI.MAX, MPI.MIN, MPI.PROD):
value = self.INTERCOMM.allreduce(rank, op)
if op == MPI.SUM:
self.assertEqual(value, cumsum(range(rsize)))
elif op == MPI.PROD:
self.assertEqual(value, cumprod(range(rsize)))
elif op == MPI.MAX:
self.assertEqual(value, rsize-1)
elif op == MPI.MIN:
self.assertEqual(value, 0)
class TestCCOObjInter(BaseTestCCOObjInter, unittest.TestCase):
BASECOMM = MPI.COMM_WORLD
class TestCCOObjInterDup(TestCCOObjInter):
def setUp(self):
self.BASECOMM = self.BASECOMM.Dup()
super(TestCCOObjInterDup, self).setUp()
def tearDown(self):
self.BASECOMM.Free()
super(TestCCOObjInterDup, self).tearDown()
class TestCCOObjInterDupDup(TestCCOObjInterDup):
BASECOMM = MPI.COMM_WORLD
INTERCOMM_ORIG = MPI.COMM_NULL
def setUp(self):
super(TestCCOObjInterDupDup, self).setUp()
self.INTERCOMM_ORIG = self.INTERCOMM
self.INTERCOMM = self.INTERCOMM.Dup()
def tearDown(self):
super(TestCCOObjInterDupDup, self).tearDown()
self.INTERCOMM_ORIG.Free()
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_comm_inter.py 0000644 0001750 0001750 00000005611 13200562156 020524 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
@unittest.skipIf(MPI.COMM_WORLD.Get_size() < 2, 'mpi-world-size<2')
class BaseTestIntercomm(object):
BASECOMM = MPI.COMM_NULL
INTRACOMM = MPI.COMM_NULL
INTERCOMM = MPI.COMM_NULL
def setUp(self):
size = self.BASECOMM.Get_size()
rank = self.BASECOMM.Get_rank()
if rank < size // 2 :
self.COLOR = 0
self.LOCAL_LEADER = 0
self.REMOTE_LEADER = size // 2
else:
self.COLOR = 1
self.LOCAL_LEADER = 0
self.REMOTE_LEADER = 0
self.INTRACOMM = self.BASECOMM.Split(self.COLOR, key=0)
Create_intercomm = MPI.Intracomm.Create_intercomm
self.INTERCOMM = Create_intercomm(self.INTRACOMM,
self.LOCAL_LEADER,
self.BASECOMM,
self.REMOTE_LEADER)
def tearDown(self):
self.INTRACOMM.Free()
self.INTERCOMM.Free()
def testFortran(self):
intercomm = self.INTERCOMM
fint = intercomm.py2f()
newcomm = MPI.Comm.f2py(fint)
self.assertEqual(newcomm, intercomm)
self.assertTrue(type(newcomm) is MPI.Intercomm)
def testLocalGroupSizeRank(self):
intercomm = self.INTERCOMM
local_group = intercomm.Get_group()
self.assertEqual(local_group.size, intercomm.Get_size())
self.assertEqual(local_group.size, intercomm.size)
self.assertEqual(local_group.rank, intercomm.Get_rank())
self.assertEqual(local_group.rank, intercomm.rank)
local_group.Free()
def testRemoteGroupSize(self):
intercomm = self.INTERCOMM
remote_group = intercomm.Get_remote_group()
self.assertEqual(remote_group.size, intercomm.Get_remote_size())
self.assertEqual(remote_group.size, intercomm.remote_size)
remote_group.Free()
def testMerge(self):
basecomm = self.BASECOMM
intercomm = self.INTERCOMM
if basecomm.rank < basecomm.size // 2:
high = False
else:
high = True
intracomm = intercomm.Merge(high)
self.assertEqual(intracomm.size, basecomm.size)
self.assertEqual(intracomm.rank, basecomm.rank)
intracomm.Free()
class TestIntercomm(BaseTestIntercomm, unittest.TestCase):
BASECOMM = MPI.COMM_WORLD
class TestIntercommDup(TestIntercomm):
def setUp(self):
self.BASECOMM = self.BASECOMM.Dup()
super(TestIntercommDup, self).setUp()
def tearDown(self):
self.BASECOMM.Free()
super(TestIntercommDup, self).tearDown()
class TestIntercommDupDup(TestIntercomm):
def setUp(self):
super(TestIntercommDupDup, self).setUp()
INTERCOMM = self.INTERCOMM
self.INTERCOMM = self.INTERCOMM.Dup()
INTERCOMM.Free()
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_spawn.py 0000664 0001750 0001750 00000020664 13426010662 017527 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
import sys, os, mpi4py
MPI4PYPATH = os.path.abspath(os.path.dirname(mpi4py.__path__[0]))
CHILDSCRIPT = os.path.abspath(
os.path.join(os.path.dirname(__file__), 'spawn_child.py')
)
HAVE_MPE = 'MPE_LOGFILE_PREFIX' in os.environ
HAVE_VT = 'VT_FILE_PREFIX' in os.environ
def childscript():
from tempfile import mkstemp
from textwrap import dedent
fd, script = mkstemp(suffix='.py', prefix="mpi4py-")
os.close(fd)
with open(script, "w") as f:
f.write(dedent("""\
#!%(python)s
import sys; sys.path.insert(0, "%(path)s")
import mpi4py
if %(mpe)s: mpi4py.profile('mpe', logfile="%(logfile)s")
if %(vt)s: mpi4py.profile('vt', logfile="%(logfile)s")
from mpi4py import MPI
parent = MPI.Comm.Get_parent()
parent.Barrier()
parent.Disconnect()
assert parent == MPI.COMM_NULL
parent = MPI.Comm.Get_parent()
assert parent == MPI.COMM_NULL
""" % dict(python=sys.executable, path=MPI4PYPATH,
mpe=HAVE_MPE, vt=HAVE_VT,
logfile="runtests-mpi4py-child")))
os.chmod(script, int("770", 8))
return script
def appnum():
if MPI.APPNUM == MPI.KEYVAL_INVALID: return None
return MPI.COMM_WORLD.Get_attr(MPI.APPNUM)
@unittest.skipMPI('MPI(<2.0)')
@unittest.skipMPI('openmpi(<3.0.0)')
@unittest.skipMPI('openmpi(==4.0.0)')
@unittest.skipMPI('mpich', appnum() is None)
@unittest.skipMPI('msmpi(<8.1.0)')
@unittest.skipMPI('msmpi', appnum() is None)
@unittest.skipMPI('MVAPICH2')
@unittest.skipMPI('MPICH2')
@unittest.skipMPI('MPICH1')
@unittest.skipMPI('PlatformMPI')
class BaseTestSpawn(object):
COMM = MPI.COMM_NULL
COMMAND = sys.executable
ARGS = [CHILDSCRIPT, MPI4PYPATH,
"mpe" if HAVE_MPE else
"vt" if HAVE_VT else ""]
MAXPROCS = 1
INFO = MPI.INFO_NULL
ROOT = 0
def testCommSpawn(self):
self.COMM.Barrier()
child = self.COMM.Spawn(self.COMMAND, self.ARGS, self.MAXPROCS,
info=self.INFO, root=self.ROOT)
local_size = child.Get_size()
remote_size = child.Get_remote_size()
child.Barrier()
child.Disconnect()
self.COMM.Barrier()
self.assertEqual(local_size, self.COMM.Get_size())
self.assertEqual(remote_size, self.MAXPROCS)
@unittest.skipMPI('msmpi')
def testErrcodes(self):
self.COMM.Barrier()
errcodes = []
child = self.COMM.Spawn(self.COMMAND, self.ARGS, self.MAXPROCS,
info=self.INFO, root=self.ROOT,
errcodes=errcodes)
child.Barrier()
child.Disconnect()
self.COMM.Barrier()
self.assertEqual(len(errcodes), self.MAXPROCS)
for errcode in errcodes:
self.assertEqual(errcode, MPI.SUCCESS)
@unittest.skipMPI('msmpi')
def testArgsOnlyAtRoot(self):
self.COMM.Barrier()
if self.COMM.Get_rank() == self.ROOT:
child = self.COMM.Spawn(self.COMMAND, self.ARGS, self.MAXPROCS,
info=self.INFO, root=self.ROOT)
else:
child = self.COMM.Spawn(None, None, -1,
info=MPI.INFO_NULL, root=self.ROOT)
child.Barrier()
child.Disconnect()
self.COMM.Barrier()
@unittest.skipIf(os.name != 'posix', 'posix')
def testNoArgs(self):
self.COMM.Barrier()
script = None
if self.COMM.Get_rank() == self.ROOT:
script = childscript()
self.COMM.Barrier()
script = self.COMM.bcast(script, root=self.ROOT)
child = self.COMM.Spawn(script, None, self.MAXPROCS,
info=self.INFO, root=self.ROOT)
child.Barrier()
child.Disconnect()
self.COMM.Barrier()
if self.COMM.Get_rank() == self.ROOT:
os.remove(script)
self.COMM.Barrier()
def testCommSpawnMultiple(self):
self.COMM.Barrier()
count = 2 + (self.COMM.Get_size() == 0)
COMMAND = [self.COMMAND] * count
ARGS = [self.ARGS] * len(COMMAND)
MAXPROCS = [self.MAXPROCS] * len(COMMAND)
INFO = [self.INFO] * len(COMMAND)
child = self.COMM.Spawn_multiple(
COMMAND, ARGS, MAXPROCS,
info=INFO, root=self.ROOT)
local_size = child.Get_size()
remote_size = child.Get_remote_size()
child.Barrier()
child.Disconnect()
self.COMM.Barrier()
self.assertEqual(local_size, self.COMM.Get_size())
self.assertEqual(remote_size, sum(MAXPROCS))
def testCommSpawnMultipleDefaults1(self):
self.COMM.Barrier()
count = 2 + (self.COMM.Get_size() == 0)
COMMAND = [self.COMMAND] * count
ARGS = [self.ARGS] * len(COMMAND)
child = self.COMM.Spawn_multiple(COMMAND, ARGS)
local_size = child.Get_size()
remote_size = child.Get_remote_size()
child.Barrier()
child.Disconnect()
self.COMM.Barrier()
self.assertEqual(local_size, self.COMM.Get_size())
self.assertEqual(remote_size, len(COMMAND))
def testCommSpawnMultipleDefaults2(self):
self.COMM.Barrier()
count = 2 + (self.COMM.Get_size() == 0)
COMMAND = [self.COMMAND] * count
ARGS = [self.ARGS] * len(COMMAND)
child = self.COMM.Spawn_multiple(COMMAND, ARGS, 1, MPI.INFO_NULL)
local_size = child.Get_size()
remote_size = child.Get_remote_size()
child.Barrier()
child.Disconnect()
self.COMM.Barrier()
self.assertEqual(local_size, self.COMM.Get_size())
self.assertEqual(remote_size, len(COMMAND))
@unittest.skipMPI('msmpi')
def testErrcodesMultiple(self):
self.COMM.Barrier()
count = 2 + (self.COMM.Get_size() == 0)
COMMAND = [self.COMMAND] * count
ARGS = [self.ARGS]*len(COMMAND)
MAXPROCS = list(range(1, len(COMMAND)+1))
INFO = MPI.INFO_NULL
errcodelist = []
child = self.COMM.Spawn_multiple(
COMMAND, ARGS, MAXPROCS,
info=INFO, root=self.ROOT,
errcodes=errcodelist)
child.Barrier()
child.Disconnect()
self.COMM.Barrier()
self.assertEqual(len(errcodelist), len(COMMAND))
for i, errcodes in enumerate(errcodelist):
self.assertEqual(len(errcodes), MAXPROCS[i])
for errcode in errcodes:
self.assertEqual(errcode, MPI.SUCCESS)
@unittest.skipMPI('msmpi')
def testArgsOnlyAtRootMultiple(self):
self.COMM.Barrier()
if self.COMM.Get_rank() == self.ROOT:
count = 2 + (self.COMM.Get_size() == 0)
COMMAND = [self.COMMAND] * count
ARGS = [self.ARGS] * len(COMMAND)
MAXPROCS = list(range(1, len(COMMAND)+1))
INFO = [MPI.INFO_NULL] * len(COMMAND)
child = self.COMM.Spawn_multiple(
COMMAND, ARGS, MAXPROCS,
info=INFO, root=self.ROOT)
else:
child = self.COMM.Spawn_multiple(
None, None, -1,
info=MPI.INFO_NULL, root=self.ROOT)
child.Barrier()
child.Disconnect()
self.COMM.Barrier()
@unittest.skipIf(os.name != 'posix', 'posix')
def testNoArgsMultiple(self):
self.COMM.Barrier()
script = None
if self.COMM.Get_rank() == self.ROOT:
script = childscript()
self.COMM.Barrier()
script = self.COMM.bcast(script, root=self.ROOT)
count = 2 + (self.COMM.Get_size() == 0)
COMMAND = [script] * count
MAXPROCS = list(range(1, len(COMMAND)+1))
INFO = [self.INFO] * len(COMMAND)
child = self.COMM.Spawn_multiple(COMMAND, None, MAXPROCS,
info=INFO, root=self.ROOT)
child.Barrier()
child.Disconnect()
self.COMM.Barrier()
if self.COMM.Get_rank() == self.ROOT:
os.remove(script)
self.COMM.Barrier()
class TestSpawnSelf(BaseTestSpawn, unittest.TestCase):
COMM = MPI.COMM_SELF
class TestSpawnWorld(BaseTestSpawn, unittest.TestCase):
COMM = MPI.COMM_WORLD
class TestSpawnSelfMany(BaseTestSpawn, unittest.TestCase):
COMM = MPI.COMM_SELF
MAXPROCS = MPI.COMM_WORLD.Get_size()
class TestSpawnWorldMany(BaseTestSpawn, unittest.TestCase):
COMM = MPI.COMM_WORLD
MAXPROCS = MPI.COMM_WORLD.Get_size()
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_p2p_obj_matched.py 0000644 0001750 0001750 00000012607 13200562156 021413 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
_basic = [None,
True, False,
-7, 0, 7,
-2**63+1, 2**63-1,
-2.17, 0.0, 3.14,
1+2j, 2-3j,
'mpi4py',
]
messages = list(_basic)
messages += [ list(_basic),
tuple(_basic),
dict([('k%d' % key, val)
for key, val in enumerate(_basic)])
]
@unittest.skipIf(MPI.MESSAGE_NULL == MPI.MESSAGE_NO_PROC, 'mpi-message')
class TestMessage(unittest.TestCase):
def testMessageNull(self):
null = MPI.MESSAGE_NULL
self.assertFalse(null)
null2 = MPI.Message()
self.assertEqual(null, null2)
null3 = MPI.Message(null)
self.assertEqual(null, null3)
def testMessageNoProc(self):
#
noproc = MPI.MESSAGE_NO_PROC
self.assertTrue(noproc)
noproc.recv()
self.assertTrue(noproc)
noproc.irecv().wait()
self.assertTrue(noproc)
#
noproc2 = MPI.Message(MPI.MESSAGE_NO_PROC)
self.assertTrue(noproc2)
self.assertEqual(noproc2, noproc)
self.assertNotEqual(noproc, MPI.MESSAGE_NULL)
#
message = MPI.Message(MPI.MESSAGE_NO_PROC)
message.recv()
self.assertEqual(message, MPI.MESSAGE_NULL)
#
message = MPI.Message(MPI.MESSAGE_NO_PROC)
request = message.irecv()
self.assertEqual(message, MPI.MESSAGE_NULL)
self.assertNotEqual(request, MPI.REQUEST_NULL)
request.wait()
self.assertEqual(request, MPI.REQUEST_NULL)
#
comm = MPI.COMM_SELF
message = comm.mprobe(MPI.PROC_NULL)
self.assertNotEqual(message, MPI.MESSAGE_NULL)
self.assertEqual(message, MPI.MESSAGE_NO_PROC)
noproc = comm.improbe(MPI.PROC_NULL)
self.assertNotEqual(message, MPI.MESSAGE_NULL)
self.assertEqual(message, MPI.MESSAGE_NO_PROC)
@unittest.skipIf(MPI.MESSAGE_NULL == MPI.MESSAGE_NO_PROC, 'mpi-message')
class BaseTestP2PMatched(object):
COMM = MPI.COMM_NULL
def testIMProbe(self):
comm = self.COMM.Dup()
try:
m = comm.improbe()
self.assertEqual(m, None)
m = comm.improbe(MPI.ANY_SOURCE)
self.assertEqual(m, None)
m = comm.improbe(MPI.ANY_SOURCE, MPI.ANY_TAG)
self.assertEqual(m, None)
status = MPI.Status()
m = comm.improbe(MPI.ANY_SOURCE, MPI.ANY_TAG, status)
self.assertEqual(m, None)
self.assertEqual(status.source, MPI.ANY_SOURCE)
self.assertEqual(status.tag, MPI.ANY_TAG)
self.assertEqual(status.error, MPI.SUCCESS)
m = MPI.Message.iprobe(comm)
self.assertEqual(m, None)
# Open MPI <= 1.8.4
s = comm.isend(None, comm.rank, 0)
r = comm.mprobe(comm.rank, 0).irecv()
MPI.Request.waitall([s,r])
finally:
comm.Free()
def testProbeRecv(self):
comm = self.COMM
size = comm.Get_size()
rank = comm.Get_rank()
for smsg in messages:
if size == 1:
sr = comm.isend(smsg, 0, 0)
m = comm.mprobe(0, 0)
self.assertTrue(isinstance(m, MPI.Message))
self.assertTrue(m)
rr = m.irecv()
self.assertFalse(m)
self.assertTrue(sr)
self.assertTrue(rr)
MPI.Request.Waitall([sr,rr])
self.assertFalse(sr)
self.assertFalse(rr)
#
r = comm.isend(smsg, 0, 0)
m = MPI.Message.probe(comm, 0, 0)
self.assertTrue(isinstance(m, MPI.Message))
self.assertTrue(m)
rmsg = m.recv()
self.assertFalse(m)
r.wait()
elif rank == 0:
comm.send(smsg, 1, 0)
m = comm.mprobe(1, 0)
self.assertTrue(m)
rmsg = m.recv()
self.assertFalse(m)
#
comm.send(smsg, 1, 1)
m = None
while not m:
m = MPI.Message.iprobe(comm, 1, 1)
rmsg = m.irecv().wait()
elif rank == 1:
m = comm.mprobe(0, 0)
self.assertTrue(m)
rmsg = m.recv()
self.assertFalse(m)
comm.send(rmsg, 0, 0)
#
m = None
while not m:
m = MPI.Message.iprobe(comm, 0, 1)
rmsg = m.irecv().wait()
comm.send(smsg, 0, 1)
else:
rmsg = smsg
self.assertEqual(smsg, rmsg)
class TestP2PMatchedSelf(BaseTestP2PMatched, unittest.TestCase):
COMM = MPI.COMM_SELF
class TestP2PMatchedWorld(BaseTestP2PMatched, unittest.TestCase):
COMM = MPI.COMM_WORLD
@unittest.skipMPI('openmpi(<1.8.5)', MPI.COMM_WORLD.Get_size() > 1)
class TestP2PMatchedSelfDup(TestP2PMatchedSelf):
def setUp(self):
self.COMM = MPI.COMM_SELF.Dup()
def tearDown(self):
self.COMM.Free()
@unittest.skipMPI('openmpi(<1.8.5)', MPI.COMM_WORLD.Get_size() > 1)
class TestP2PMatchedWorldDup(TestP2PMatchedWorld):
def setUp(self):
self.COMM = MPI.COMM_WORLD.Dup()
def tearDown(self):
self.COMM.Free()
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_ctypes.py 0000664 0001750 0001750 00000003511 13426006675 017707 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
import sys
pypy_lt_510 = (hasattr(sys, 'pypy_version_info') and
sys.pypy_version_info < (5, 10))
try:
if pypy_lt_510:
ctypes = None
else:
import ctypes
except ImportError:
ctypes = None
@unittest.skipIf(ctypes is None, 'ctypes')
class TestCTYPES(unittest.TestCase):
objects = [
MPI.DATATYPE_NULL,
MPI.INT,
MPI.DOUBLE,
MPI.REQUEST_NULL,
MPI.INFO_NULL,
MPI.INFO_ENV,
MPI.ERRHANDLER_NULL,
MPI.ERRORS_RETURN,
MPI.ERRORS_ARE_FATAL,
MPI.GROUP_NULL,
MPI.GROUP_EMPTY,
MPI.WIN_NULL,
MPI.OP_NULL,
MPI.SUM,
MPI.MIN,
MPI.MAX,
MPI.FILE_NULL,
MPI.MESSAGE_NULL,
MPI.MESSAGE_NO_PROC,
MPI.COMM_NULL,
MPI.COMM_SELF,
MPI.COMM_WORLD,
]
def testHandleAdress(self):
typemap = {ctypes.sizeof(ctypes.c_int): ctypes.c_int,
ctypes.sizeof(ctypes.c_void_p): ctypes.c_void_p}
for obj in self.objects:
handle_t = typemap[MPI._sizeof(obj)]
oldobj = obj
newobj = type(obj)()
handle_old = handle_t.from_address(MPI._addressof(oldobj))
handle_new = handle_t.from_address(MPI._addressof(newobj))
handle_new.value = handle_old.value
self.assertEqual(obj, newobj)
def testHandleValue(self):
typemap = {ctypes.sizeof(ctypes.c_uint32): ctypes.c_uint32,
ctypes.sizeof(ctypes.c_uint64): ctypes.c_uint64}
for obj in self.objects:
uintptr_t = typemap[MPI._sizeof(obj)]
handle = uintptr_t.from_address(MPI._addressof(obj))
self.assertEqual(handle.value, MPI._handleof(obj))
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_dl.py 0000664 0001750 0001750 00000004545 13426006675 017007 0 ustar dalcinl dalcinl 0000000 0000000 import mpiunittest as unittest
import sys, os
try:
from mpi4py import dl
except ImportError:
dl = None
pypy_lt_510 = (hasattr(sys, 'pypy_version_info') and
sys.pypy_version_info < (5, 10))
@unittest.skipIf(dl is None, 'mpi4py-dl')
class TestDL(unittest.TestCase):
def testDL1(self):
if sys.platform == 'darwin':
libm = 'libm.dylib'
else:
libm = 'libm.so'
handle = dl.dlopen(libm, dl.RTLD_LOCAL|dl.RTLD_LAZY)
if handle == 0 and sys.platform.startswith('linux'):
self.assertTrue(dl.dlerror() is not None)
handle = dl.dlopen('libm.so.6', dl.RTLD_LOCAL|dl.RTLD_LAZY)
self.assertTrue(handle != 0)
self.assertTrue(dl.dlerror() is None)
symbol = dl.dlsym(handle, 'sqrt')
self.assertTrue(symbol != 0)
self.assertTrue(dl.dlerror() is None)
symbol = dl.dlsym(handle, 'xxxxx')
self.assertTrue(symbol == 0)
self.assertTrue(dl.dlerror() is not None)
ierr = dl.dlclose(handle)
self.assertTrue(ierr == 0)
self.assertTrue(dl.dlerror() is None)
@unittest.skipIf(pypy_lt_510 and sys.platform == 'darwin',
'pypy(<5.10)|darwin')
def testDL2(self):
handle = dl.dlopen(None, dl.RTLD_GLOBAL|dl.RTLD_NOW)
self.assertTrue(handle != 0)
self.assertTrue(dl.dlerror() is None)
symbol = dl.dlsym(handle, 'malloc')
self.assertTrue(symbol != 0)
self.assertTrue(dl.dlerror() is None)
symbol = dl.dlsym(handle, '!@#$%^&*()')
self.assertTrue(symbol == 0)
self.assertTrue(dl.dlerror() is not None)
ierr = dl.dlclose(handle)
self.assertTrue(ierr == 0)
self.assertTrue(dl.dlerror() is None)
def testDL3(self):
symbol = dl.dlsym(None, 'malloc')
self.assertTrue(symbol != 0)
self.assertTrue(dl.dlerror() is None)
symbol = dl.dlsym(None, '!@#$%^&*()')
self.assertTrue(symbol == 0)
self.assertTrue(dl.dlerror() is not None)
ierr = dl.dlclose(None)
self.assertTrue(ierr == 0)
self.assertTrue(dl.dlerror() is None)
def testDL4(self):
handle = dl.dlopen('xxxxx', dl.RTLD_LOCAL|dl.RTLD_LAZY)
self.assertTrue(handle == 0)
self.assertTrue(dl.dlerror() is not None)
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_msgspec.py 0000664 0001750 0001750 00000053270 13557770163 020055 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
from arrayimpl import allclose
import sys
typemap = MPI._typedict
try:
import array
except ImportError:
array = None
try:
import numpy
except ImportError:
numpy = None
pypy2 = (hasattr(sys, 'pypy_version_info') and
sys.version_info[0] == 2)
pypy_lt_53 = (hasattr(sys, 'pypy_version_info') and
sys.pypy_version_info < (5, 3))
def Sendrecv(smsg, rmsg):
MPI.COMM_SELF.Sendrecv(sendbuf=smsg, dest=0, sendtag=0,
recvbuf=rmsg, source=0, recvtag=0,
status=MPI.Status())
class TestMessageSimple(unittest.TestCase):
TYPECODES = "hil"+"HIL"+"fd"
def check1(self, equal, zero, s, r, t):
r[:] = zero
Sendrecv(s, r)
self.assertTrue(equal(s, r))
def check21(self, equal, zero, s, r, typecode):
datatype = typemap[typecode]
for type in (None, typecode, datatype):
r[:] = zero
Sendrecv([s, type],
[r, type])
self.assertTrue(equal(s, r))
def check22(self, equal, zero, s, r, typecode):
size = len(r)
for count in range(size):
r[:] = zero
Sendrecv([s, count],
[r, count])
for i in range(count):
self.assertTrue(equal(r[i], s[i]))
for i in range(count, size):
self.assertTrue(equal(r[i], zero[0]))
for count in range(size):
r[:] = zero
Sendrecv([s, (count, None)],
[r, (count, None)])
for i in range(count):
self.assertTrue(equal(r[i], s[i]))
for i in range(count, size):
self.assertTrue(equal(r[i], zero[0]))
for disp in range(size):
r[:] = zero
Sendrecv([s, (None, disp)],
[r, (None, disp)])
for i in range(disp):
self.assertTrue(equal(r[i], zero[0]))
for i in range(disp, size):
self.assertTrue(equal(r[i], s[i]))
for disp in range(size):
for count in range(size-disp):
r[:] = zero
Sendrecv([s, (count, disp)],
[r, (count, disp)])
for i in range(0, disp):
self.assertTrue(equal(r[i], zero[0]))
for i in range(disp, disp+count):
self.assertTrue(equal(r[i], s[i]))
for i in range(disp+count, size):
self.assertTrue(equal(r[i], zero[0]))
def check31(self, equal, z, s, r, typecode):
datatype = typemap[typecode]
for type in (None, typecode, datatype):
for count in (None, len(s)):
r[:] = z
Sendrecv([s, count, type],
[r, count, type])
self.assertTrue(equal(s, r))
def check32(self, equal, z, s, r, typecode):
datatype = typemap[typecode]
for type in (None, typecode, datatype):
for p in range(0, len(s)):
r[:] = z
Sendrecv([s, (p, None), type],
[r, (p, None), type])
self.assertTrue(equal(s[:p], r[:p]))
for q in range(p, len(s)):
count, displ = q-p, p
r[:] = z
Sendrecv([s, (count, displ), type],
[r, (count, displ), type])
self.assertTrue(equal(s[p:q], r[p:q]))
self.assertTrue(equal(z[:p], r[:p]))
self.assertTrue(equal(z[q:], r[q:]))
def check4(self, equal, z, s, r, typecode):
datatype = typemap[typecode]
for type in (None, typecode, datatype):
for p in range(0, len(s)):
r[:] = z
Sendrecv([s, p, None, type],
[r, p, None, type])
self.assertTrue(equal(s[:p], r[:p]))
for q in range(p, len(s)):
count, displ = q-p, p
r[:] = z
Sendrecv([s, count, displ, type],
[r, count, displ, type])
self.assertTrue(equal(s[p:q], r[p:q]))
self.assertTrue(equal(z[:p], r[:p]))
self.assertTrue(equal(z[q:], r[q:]))
def testMessageBad(self):
buf = MPI.Alloc_mem(5)
empty = [None, 0, "B"]
def f(): Sendrecv([buf, 0, 0, "i", None], empty)
self.assertRaises(ValueError, f)
def f(): Sendrecv([buf, 0, "\0"], empty)
self.assertRaises(KeyError, f)
def f(): Sendrecv([buf, -1, "i"], empty)
self.assertRaises(ValueError, f)
def f(): Sendrecv([buf, 0, -1, "i"], empty)
self.assertRaises(ValueError, f)
def f(): Sendrecv([buf, 0, +2, "i"], empty)
self.assertRaises(ValueError, f)
def f(): Sendrecv([None, 1, 0, "i"], empty)
self.assertRaises(ValueError, f)
def f(): Sendrecv([buf, None, 0, "i"], empty)
self.assertRaises(ValueError, f)
def f(): Sendrecv([buf, 0, 1, MPI.DATATYPE_NULL], empty)
self.assertRaises(ValueError, f)
def f(): Sendrecv([buf, None, 0, MPI.DATATYPE_NULL], empty)
self.assertRaises(ValueError, f)
try:
t = MPI.INT.Create_resized(0, -4).Commit()
def f(): Sendrecv([buf, None, t], empty)
self.assertRaises(ValueError, f)
def f(): Sendrecv([buf, 0, 1, t], empty)
self.assertRaises(ValueError, f)
t.Free()
except NotImplementedError:
pass
MPI.Free_mem(buf)
buf = [1,2,3,4]
def f(): Sendrecv([buf, 4, 0, "i"], empty)
self.assertRaises(TypeError, f)
buf = {1:2,3:4}
def f(): Sendrecv([buf, 4, 0, "i"], empty)
self.assertRaises(TypeError, f)
def f(): Sendrecv(b"abc", b"abc")
self.assertRaises((BufferError, TypeError, ValueError), f)
def testMessageNone(self):
empty = [None, 0, "B"]
Sendrecv(empty, empty)
empty = [None, "B"]
Sendrecv(empty, empty)
def testMessageBottom(self):
empty = [MPI.BOTTOM, 0, "B"]
Sendrecv(empty, empty)
empty = [MPI.BOTTOM, "B"]
Sendrecv(empty, empty)
@unittest.skipIf(pypy_lt_53, 'pypy(<5.3)')
def testMessageBytes(self):
sbuf = b"abc"
rbuf = bytearray(3)
Sendrecv([sbuf, "c"], [rbuf, MPI.CHAR])
self.assertEqual(sbuf, rbuf)
@unittest.skipIf(pypy_lt_53, 'pypy(<5.3)')
def testMessageBytearray(self):
sbuf = bytearray(b"abc")
rbuf = bytearray(3)
Sendrecv([sbuf, "c"], [rbuf, MPI.CHAR])
self.assertEqual(sbuf, rbuf)
@unittest.skipIf(pypy2, 'pypy2')
@unittest.skipIf(sys.version_info[0] >= 3, 'python3')
@unittest.skipIf(hasattr(MPI, 'ffi'), 'mpi4py-cffi')
def testMessageUnicode(self): # Test for Issue #120
sbuf = unicode("abc")
rbuf = bytearray(len(buffer(sbuf)))
Sendrecv([sbuf, MPI.BYTE], [rbuf, MPI.BYTE])
@unittest.skipIf(pypy_lt_53, 'pypy(<5.3)')
def testMessageBuffer(self):
if sys.version_info[0] != 2: return
sbuf = buffer(b"abc")
rbuf = bytearray(3)
Sendrecv([sbuf, "c"], [rbuf, MPI.CHAR])
self.assertEqual(sbuf, rbuf)
self.assertRaises((BufferError, TypeError, ValueError),
Sendrecv, [rbuf, "c"], [sbuf, "c"])
@unittest.skipIf(pypy2, 'pypy2')
@unittest.skipIf(pypy_lt_53, 'pypy(<5.3)')
def testMessageMemoryView(self):
if sys.version_info[:2] < (2, 7): return
sbuf = memoryview(b"abc")
rbuf = bytearray(3)
Sendrecv([sbuf, "c"], [rbuf, MPI.CHAR])
self.assertEqual(sbuf, rbuf)
self.assertRaises((BufferError, TypeError, ValueError),
Sendrecv, [rbuf, "c"], [sbuf, "c"])
@unittest.skipIf(array is None, 'array')
def checkArray(self, test):
from operator import eq as equal
for t in tuple(self.TYPECODES):
for n in range(1, 10):
z = array.array(t, [0]*n)
s = array.array(t, list(range(n)))
r = array.array(t, [0]*n)
test(equal, z, s, r, t)
def testArray1(self):
self.checkArray(self.check1)
def testArray21(self):
self.checkArray(self.check21)
def testArray22(self):
self.checkArray(self.check22)
def testArray31(self):
self.checkArray(self.check31)
def testArray32(self):
self.checkArray(self.check32)
def testArray4(self):
self.checkArray(self.check4)
@unittest.skipIf(numpy is None, 'numpy')
def checkNumPy(self, test):
from numpy import zeros, arange, empty
for t in tuple(self.TYPECODES):
for n in range(10):
z = zeros (n, dtype=t)
s = arange(n, dtype=t)
r = empty (n, dtype=t)
test(allclose, z, s, r, t)
def testNumPy1(self):
self.checkNumPy(self.check1)
def testNumPy21(self):
self.checkNumPy(self.check21)
def testNumPy22(self):
self.checkNumPy(self.check22)
def testNumPy31(self):
self.checkNumPy(self.check31)
def testNumPy32(self):
self.checkNumPy(self.check32)
def testNumPy4(self):
self.checkNumPy(self.check4)
@unittest.skipIf(numpy is None, 'numpy')
def testNumPyBad(self):
from numpy import zeros
wbuf = zeros([1])
rbuf = zeros([1])
rbuf.flags.writeable = False
self.assertRaises((BufferError, ValueError),
Sendrecv, wbuf, rbuf)
wbuf = zeros([3,2])[:,0]
rbuf = zeros([3])
rbuf.flags.writeable = False
self.assertRaises((BufferError, ValueError),
Sendrecv, rbuf, wbuf)
@unittest.skipMPI('msmpi(<8.0.0)')
class TestMessageBlock(unittest.TestCase):
@unittest.skipIf(MPI.COMM_WORLD.Get_size() < 2, 'mpi-world-size<2')
def testMessageBad(self):
comm = MPI.COMM_WORLD
buf = MPI.Alloc_mem(4)
empty = [None, 0, "B"]
def f(): comm.Alltoall([buf, None, "i"], empty)
self.assertRaises(ValueError, f)
MPI.Free_mem(buf)
def Alltoallv(smsg, rmsg):
comm = MPI.COMM_SELF
comm.Alltoallv(smsg, rmsg)
@unittest.skipMPI('msmpi(<8.0.0)')
class TestMessageVector(unittest.TestCase):
TYPECODES = "hil"+"HIL"+"fd"
def check1(self, equal, zero, s, r, t):
r[:] = zero
Alltoallv(s, r)
self.assertTrue(equal(s, r))
def check21(self, equal, zero, s, r, typecode):
datatype = typemap[typecode]
for type in (None, typecode, datatype):
r[:] = zero
Alltoallv([s, type],
[r, type])
self.assertTrue(equal(s, r))
def check22(self, equal, zero, s, r, typecode):
size = len(r)
for count in range(size):
r[:] = zero
Alltoallv([s, count],
[r, count])
for i in range(count):
self.assertTrue(equal(r[i], s[i]))
for i in range(count, size):
self.assertTrue(equal(r[i], zero[0]))
for count in range(size):
r[:] = zero
Alltoallv([s, (count, None)],
[r, (count, None)])
for i in range(count):
self.assertTrue(equal(r[i], s[i]))
for i in range(count, size):
self.assertTrue(equal(r[i], zero[0]))
for disp in range(size):
for count in range(size-disp):
r[:] = zero
Alltoallv([s, ([count], [disp])],
[r, ([count], [disp])])
for i in range(0, disp):
self.assertTrue(equal(r[i], zero[0]))
for i in range(disp, disp+count):
self.assertTrue(equal(r[i], s[i]))
for i in range(disp+count, size):
self.assertTrue(equal(r[i], zero[0]))
def check31(self, equal, z, s, r, typecode):
datatype = typemap[typecode]
for type in (None, typecode, datatype):
for count in (None, len(s)):
r[:] = z
Alltoallv([s, count, type],
[r, count, type])
self.assertTrue(equal(s, r))
def check32(self, equal, z, s, r, typecode):
datatype = typemap[typecode]
for type in (None, typecode, datatype):
for p in range(len(s)):
r[:] = z
Alltoallv([s, (p, None), type],
[r, (p, None), type])
self.assertTrue(equal(s[:p], r[:p]))
for q in range(p, len(s)):
count, displ = q-p, p
r[:] = z
Alltoallv([s, (count, [displ]), type],
[r, (count, [displ]), type])
self.assertTrue(equal(s[p:q], r[p:q]))
self.assertTrue(equal(z[:p], r[:p]))
self.assertTrue(equal(z[q:], r[q:]))
def check4(self, equal, z, s, r, typecode):
datatype = typemap[typecode]
for type in (None, typecode, datatype):
for p in range(0, len(s)):
r[:] = z
Alltoallv([s, p, None, type],
[r, p, None, type])
self.assertTrue(equal(s[:p], r[:p]))
for q in range(p, len(s)):
count, displ = q-p, p
r[:] = z
Alltoallv([s, count, [displ], type],
[r, count, [displ], type])
self.assertTrue(equal(s[p:q], r[p:q]))
self.assertTrue(equal(z[:p], r[:p]))
self.assertTrue(equal(z[q:], r[q:]))
def testMessageBad(self):
buf = MPI.Alloc_mem(5)
empty = [None, 0, [0], "B"]
def f(): Alltoallv([buf, 0, [0], "i", None], empty)
self.assertRaises(ValueError, f)
def f(): Alltoallv([buf, 0, [0], "\0"], empty)
self.assertRaises(KeyError, f)
def f(): Alltoallv([buf, None, [0], MPI.DATATYPE_NULL], empty)
self.assertRaises(ValueError, f)
def f(): Alltoallv([buf, None, [0], "i"], empty)
self.assertRaises(ValueError, f)
try:
t = MPI.INT.Create_resized(0, -4).Commit()
def f(): Alltoallv([buf, None, [0], t], empty)
self.assertRaises(ValueError, f)
t.Free()
except NotImplementedError:
pass
MPI.Free_mem(buf)
buf = [1,2,3,4]
def f(): Alltoallv([buf, 0, 0, "i"], empty)
self.assertRaises(TypeError, f)
buf = {1:2,3:4}
def f(): Alltoallv([buf, 0, 0, "i"], empty)
self.assertRaises(TypeError, f)
def testMessageNone(self):
empty = [None, 0, "B"]
Alltoallv(empty, empty)
empty = [None, "B"]
Alltoallv(empty, empty)
def testMessageBottom(self):
empty = [MPI.BOTTOM, 0, [0], "B"]
Alltoallv(empty, empty)
empty = [MPI.BOTTOM, 0, "B"]
Alltoallv(empty, empty)
empty = [MPI.BOTTOM, "B"]
Alltoallv(empty, empty)
@unittest.skipIf(pypy_lt_53, 'pypy(<5.3)')
def testMessageBytes(self):
sbuf = b"abc"
rbuf = bytearray(3)
Alltoallv([sbuf, "c"], [rbuf, MPI.CHAR])
self.assertEqual(sbuf, rbuf)
@unittest.skipIf(pypy_lt_53, 'pypy(<5.3)')
def testMessageBytearray(self):
sbuf = bytearray(b"abc")
rbuf = bytearray(3)
Alltoallv([sbuf, "c"], [rbuf, MPI.CHAR])
self.assertEqual(sbuf, rbuf)
@unittest.skipIf(array is None, 'array')
def checkArray(self, test):
from operator import eq as equal
for t in tuple(self.TYPECODES):
for n in range(1, 10):
z = array.array(t, [0]*n)
s = array.array(t, list(range(n)))
r = array.array(t, [0]*n)
test(equal, z, s, r, t)
def testArray1(self):
self.checkArray(self.check1)
def testArray21(self):
self.checkArray(self.check21)
def testArray22(self):
self.checkArray(self.check22)
def testArray31(self):
self.checkArray(self.check31)
def testArray32(self):
self.checkArray(self.check32)
def testArray4(self):
self.checkArray(self.check4)
@unittest.skipIf(numpy is None, 'numpy')
def checkNumPy(self, test):
from numpy import zeros, arange, empty
for t in tuple(self.TYPECODES):
for n in range(10):
z = zeros (n, dtype=t)
s = arange(n, dtype=t)
r = empty (n, dtype=t)
test(allclose, z, s, r, t)
def testNumPy1(self):
self.checkNumPy(self.check1)
def testNumPy21(self):
self.checkNumPy(self.check21)
def testNumPy22(self):
self.checkNumPy(self.check22)
def testNumPy31(self):
self.checkNumPy(self.check31)
def testNumPy32(self):
self.checkNumPy(self.check32)
def testNumPy4(self):
self.checkNumPy(self.check4)
def Alltoallw(smsg, rmsg):
try:
MPI.COMM_SELF.Alltoallw(smsg, rmsg)
except NotImplementedError:
if isinstance(smsg, (list, tuple)): smsg = smsg[0]
if isinstance(rmsg, (list, tuple)): rmsg = rmsg[0]
try: rmsg[:] = smsg
except: pass
class TestMessageVectorW(unittest.TestCase):
def testMessageBad(self):
sbuf = MPI.Alloc_mem(4)
rbuf = MPI.Alloc_mem(4)
def f(): Alltoallw([sbuf],[rbuf])
self.assertRaises(ValueError, f)
def f(): Alltoallw([sbuf, [0], [0], [MPI.BYTE], None],
[rbuf, [0], [0], [MPI.BYTE]])
self.assertRaises(ValueError, f)
def f(): Alltoallw([sbuf, [0], [0], [MPI.BYTE]],
[rbuf, [0], [0], [MPI.BYTE], None])
self.assertRaises(ValueError, f)
MPI.Free_mem(sbuf)
MPI.Free_mem(rbuf)
@unittest.skipIf(pypy_lt_53, 'pypy(<5.3)')
def testMessageBytes(self):
sbuf = b"abc"
rbuf = bytearray(3)
smsg = [sbuf, [3], [0], [MPI.CHAR]]
rmsg = [rbuf, ([3], [0]), [MPI.CHAR]]
Alltoallw(smsg, rmsg)
self.assertEqual(sbuf, rbuf)
@unittest.skipIf(pypy_lt_53, 'pypy(<5.3)')
def testMessageBytearray(self):
sbuf = bytearray(b"abc")
rbuf = bytearray(3)
smsg = [sbuf, [3], [0], [MPI.CHAR]]
rmsg = [rbuf, ([3], [0]), [MPI.CHAR]]
Alltoallw(smsg, rmsg)
self.assertEqual(sbuf, rbuf)
sbuf = bytearray(b"abc")
rbuf = bytearray(3)
smsg = [sbuf, None, None, [MPI.CHAR]]
rmsg = [rbuf, [MPI.CHAR]]
Alltoallw(smsg, rmsg)
self.assertEqual(sbuf[0], rbuf[0])
self.assertEqual(bytearray(2), rbuf[1:])
def PutGet(smsg, rmsg, target):
try: win = MPI.Win.Allocate(256, 1, MPI.INFO_NULL, MPI.COMM_SELF)
except NotImplementedError: win = MPI.WIN_NULL
try:
try: win.Fence()
except NotImplementedError: pass
try: win.Put(smsg, 0, target)
except NotImplementedError: pass
try: win.Fence()
except NotImplementedError: pass
try: win.Get(rmsg, 0, target)
except NotImplementedError:
if isinstance(smsg, (list, tuple)): smsg = smsg[0]
if isinstance(rmsg, (list, tuple)): rmsg = rmsg[0]
try: rmsg[:] = smsg
except: pass
try: win.Fence()
except NotImplementedError: pass
finally:
if win != MPI.WIN_NULL: win.Free()
class TestMessageRMA(unittest.TestCase):
def testMessageBad(self):
sbuf = [None, 0, 0, "B", None]
rbuf = [None, 0, 0, "B"]
target = (0, 0, MPI.BYTE)
def f(): PutGet(sbuf, rbuf, target)
self.assertRaises(ValueError, f)
sbuf = [None, 0, 0, "B"]
rbuf = [None, 0, 0, "B", None]
target = (0, 0, MPI.BYTE)
def f(): PutGet(sbuf, rbuf, target)
self.assertRaises(ValueError, f)
sbuf = [None, 0, "B"]
rbuf = [None, 0, "B"]
target = (0, 0, MPI.BYTE, None)
def f(): PutGet(sbuf, rbuf, target)
self.assertRaises(ValueError, f)
sbuf = [None, 0, "B"]
rbuf = [None, 0, "B"]
target = {1:2,3:4}
def f(): PutGet(sbuf, rbuf, target)
self.assertRaises(ValueError, f)
def testMessageNone(self):
for empty in ([None, 0, 0, MPI.BYTE],
[None, 0, MPI.BYTE],
[None, MPI.BYTE]):
for target in (None, 0, [0, 0, MPI.BYTE]):
PutGet(empty, empty, target)
def testMessageBottom(self):
for empty in ([MPI.BOTTOM, 0, 0, MPI.BYTE],
[MPI.BOTTOM, 0, MPI.BYTE],
[MPI.BOTTOM, MPI.BYTE]):
for target in (None, 0, [0, 0, MPI.BYTE]):
PutGet(empty, empty, target)
@unittest.skipIf(pypy_lt_53, 'pypy(<5.3)')
def testMessageBytes(self):
for target in (None, 0, [0, 3, MPI.BYTE]):
sbuf = b"abc"
rbuf = bytearray(3)
PutGet(sbuf, rbuf, target)
self.assertEqual(sbuf, rbuf)
@unittest.skipIf(pypy_lt_53, 'pypy(<5.3)')
def testMessageBytearray(self):
for target in (None, 0, [0, 3, MPI.BYTE]):
sbuf = bytearray(b"abc")
rbuf = bytearray(3)
PutGet(sbuf, rbuf, target)
self.assertEqual(sbuf, rbuf)
@unittest.skipIf(pypy2, 'pypy2')
@unittest.skipIf(sys.version_info[0] >= 3, 'python3')
@unittest.skipIf(hasattr(MPI, 'ffi'), 'mpi4py-cffi')
def testMessageUnicode(self): # Test for Issue #120
sbuf = unicode("abc")
rbuf = bytearray(len(buffer(sbuf)))
PutGet([sbuf, MPI.BYTE], [rbuf, MPI.BYTE], None)
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_fortran.py 0000644 0001750 0001750 00000004721 13200562156 020044 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
class BaseTestFortran(object):
HANDLES = []
def testFortran(self):
for handle1 in self.HANDLES:
try:
fint = handle1.py2f()
except NotImplementedError:
continue
handle2 = type(handle1).f2py(fint)
self.assertEqual(handle1, handle2)
class TestFortranStatus(BaseTestFortran, unittest.TestCase):
def setUp(self):
s1 = MPI.Status()
s2 = MPI.Status()
s2.source = 1
s2.tag = 2
s2.error = MPI.ERR_OTHER
s3 = MPI.Status()
s3.source = 0
s3.tag = 0
s3.error = MPI.SUCCESS
self.HANDLES = [s1, s2, s3]
@unittest.skipMPI('MPICH1')
def testFortran(self):
super(TestFortranStatus, self).testFortran()
class TestFortranDatatype(BaseTestFortran, unittest.TestCase):
HANDLES = [MPI.DATATYPE_NULL,
MPI.CHAR, MPI.SHORT,
MPI.INT, MPI.LONG,
MPI.FLOAT, MPI.DOUBLE,
]
class TestFortranOp(BaseTestFortran, unittest.TestCase):
HANDLES = [MPI.OP_NULL,
MPI.MAX, MPI.MIN,
MPI.SUM, MPI.PROD,
MPI.LAND, MPI.BAND,
MPI.LOR, MPI.BOR,
MPI.LXOR, MPI.BXOR,
MPI.MAXLOC, MPI.MINLOC,
]
class TestFortranRequest(BaseTestFortran, unittest.TestCase):
HANDLES = [MPI.REQUEST_NULL,
]
class TestFortranMessage(BaseTestFortran, unittest.TestCase):
HANDLES = [MPI.MESSAGE_NULL,
MPI.MESSAGE_NO_PROC,
]
class TestFortranErrhandler(BaseTestFortran, unittest.TestCase):
HANDLES = [MPI.ERRHANDLER_NULL,
MPI.ERRORS_RETURN,
MPI.ERRORS_ARE_FATAL,
]
class TestFortranInfo(BaseTestFortran, unittest.TestCase):
HANDLES = [MPI.INFO_NULL,
]
class TestFortranGroup(BaseTestFortran, unittest.TestCase):
HANDLES = [MPI.GROUP_NULL,
MPI.GROUP_EMPTY,
]
class TestFortranComm(BaseTestFortran, unittest.TestCase):
HANDLES = [MPI.COMM_NULL,
MPI.COMM_SELF,
MPI.COMM_WORLD,
]
class TestFortranWin(BaseTestFortran, unittest.TestCase):
HANDLES = [MPI.WIN_NULL,
]
class TestFortranFile(BaseTestFortran, unittest.TestCase):
HANDLES = [MPI.FILE_NULL,
]
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_errorcode.py 0000644 0001750 0001750 00000007565 13200562156 020366 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
class TestErrorCode(unittest.TestCase):
errorclasses = [item[1] for item in vars(MPI).items()
if item[0].startswith('ERR_')]
errorclasses.insert(0, MPI.SUCCESS)
errorclasses.remove(MPI.ERR_LASTCODE)
def testGetErrorClass(self):
self.assertEqual(self.errorclasses[0], 0)
for ierr in self.errorclasses:
errcls = MPI.Get_error_class(ierr)
self.assertTrue(errcls >= MPI.SUCCESS)
self.assertTrue(errcls <= MPI.ERR_LASTCODE)
self.assertEqual(errcls, ierr)
def testGetErrorStrings(self):
for ierr in self.errorclasses:
errstr = MPI.Get_error_string(ierr)
def testException(self):
from sys import version_info as py_version
success = MPI.Exception(MPI.SUCCESS)
lasterr = MPI.Exception(MPI.ERR_LASTCODE)
for ierr in self.errorclasses:
errstr = MPI.Get_error_string(ierr)
errcls = MPI.Get_error_class(ierr)
errexc = MPI.Exception(ierr)
if py_version >= (2,5):
self.assertEqual(errexc.error_code, ierr)
self.assertEqual(errexc.error_class, ierr)
self.assertEqual(errexc.error_string, errstr)
self.assertEqual(repr(errexc), "MPI.Exception(%d)" % ierr)
self.assertEqual(str(errexc), errstr)
self.assertEqual(int(errexc), ierr)
self.assertEqual(hash(errexc), hash(errexc.error_code))
self.assertTrue(errexc == ierr)
self.assertTrue(errexc == errexc)
self.assertFalse(errexc != ierr)
self.assertFalse(errexc != errexc)
self.assertTrue(success <= ierr <= lasterr)
self.assertTrue(success <= errexc <= lasterr)
self.assertTrue(errexc >= ierr)
self.assertTrue(errexc >= success)
self.assertTrue(lasterr >= ierr)
self.assertTrue(lasterr >= errexc)
if errexc == success:
self.assertFalse(errexc)
else:
self.assertTrue(errexc)
self.assertTrue(errexc > success)
self.assertTrue(success < errexc)
exc = MPI.Exception(MPI.SUCCESS-1)
self.assertTrue(exc, MPI.ERR_UNKNOWN)
exc = MPI.Exception(MPI.ERR_LASTCODE+1)
self.assertTrue(exc, MPI.ERR_UNKNOWN)
@unittest.skipMPI('openmpi(<1.10.0)')
def testAddErrorClass(self):
try:
errclass = MPI.Add_error_class()
except NotImplementedError:
self.skipTest('mpi-add_error_class')
self.assertTrue(errclass >= MPI.ERR_LASTCODE)
@unittest.skipMPI('openmpi(<1.10.0)')
def testAddErrorClassCodeString(self):
try:
errclass = MPI.Add_error_class()
except NotImplementedError:
self.skipTest('mpi-add_error_class')
lastused = MPI.COMM_WORLD.Get_attr(MPI.LASTUSEDCODE)
self.assertTrue(errclass == lastused)
errstr = MPI.Get_error_string(errclass)
self.assertEqual(errstr, "")
MPI.Add_error_string(errclass, "error class")
self.assertEqual(MPI.Get_error_string(errclass), "error class")
errcode1 = MPI.Add_error_code(errclass)
errstr = MPI.Get_error_string(errcode1)
self.assertEqual(errstr, "")
MPI.Add_error_string(errcode1, "error code 1")
self.assertEqual(MPI.Get_error_class(errcode1), errclass)
self.assertEqual(MPI.Get_error_string(errcode1), "error code 1")
errcode2 = MPI.Add_error_code(errclass)
errstr = MPI.Get_error_string(errcode2)
self.assertEqual(errstr, "")
MPI.Add_error_string(errcode2, "error code 2")
self.assertEqual(MPI.Get_error_class(errcode2), errclass)
self.assertEqual(MPI.Get_error_string(errcode2), "error code 2")
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_file.py 0000644 0001750 0001750 00000014307 13200562156 017311 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
import sys, os, tempfile
class BaseTestFile(object):
COMM = MPI.COMM_NULL
FILE = MPI.FILE_NULL
prefix = 'mpi4py'
def setUp(self):
fd, self.fname = tempfile.mkstemp(prefix=self.prefix)
os.close(fd)
self.amode = MPI.MODE_RDWR | MPI.MODE_CREATE
#self.amode |= MPI.MODE_DELETE_ON_CLOSE
try:
self.FILE = MPI.File.Open(self.COMM,
self.fname, self.amode,
MPI.INFO_NULL)
#self.fname=None
except Exception:
os.remove(self.fname)
raise
def tearDown(self):
if self.FILE == MPI.FILE_NULL: return
amode = self.FILE.amode
self.FILE.Close()
if not (amode & MPI.MODE_DELETE_ON_CLOSE):
MPI.File.Delete(self.fname, MPI.INFO_NULL)
@unittest.skipMPI('openmpi(==2.0.0)')
@unittest.skipMPI('MPICH2(<1.1.0)')
def testPreallocate(self):
size = self.FILE.Get_size()
self.assertEqual(size, 0)
self.FILE.Preallocate(1)
size = self.FILE.Get_size()
self.assertEqual(size, 1)
self.FILE.Preallocate(100)
size = self.FILE.Get_size()
self.assertEqual(size, 100)
self.FILE.Preallocate(10)
size = self.FILE.Get_size()
self.assertEqual(size, 100)
self.FILE.Preallocate(200)
size = self.FILE.Get_size()
self.assertEqual(size, 200)
def testGetSetSize(self):
size = self.FILE.Get_size()
self.assertEqual(size, 0)
size = self.FILE.size
self.assertEqual(size, 0)
self.FILE.Set_size(100)
size = self.FILE.Get_size()
self.assertEqual(size, 100)
size = self.FILE.size
self.assertEqual(size, 100)
def testGetGroup(self):
fgroup = self.FILE.Get_group()
cgroup = self.COMM.Get_group()
gcomp = MPI.Group.Compare(fgroup, cgroup)
self.assertEqual(gcomp, MPI.IDENT)
fgroup.Free()
cgroup.Free()
def testGetAmode(self):
amode = self.FILE.Get_amode()
self.assertEqual(self.amode, amode)
self.assertEqual(self.FILE.amode, self.amode)
def testGetSetInfo(self):
#info = MPI.INFO_NULL
#self.FILE.Set_info(info)
info = MPI.Info.Create()
self.FILE.Set_info(info)
info.Free()
info = self.FILE.Get_info()
self.FILE.Set_info(info)
info.Free()
def testGetSetView(self):
fsize = 100 * MPI.DOUBLE.size
self.FILE.Set_size(fsize)
displacements = range(100)
datatypes = [MPI.SHORT, MPI.INT, MPI.LONG, MPI.FLOAT, MPI.DOUBLE]
datareps = ['native'] #['native', 'internal', 'external32']
for disp in displacements:
for dtype in datatypes:
for datarep in datareps:
etype, ftype = dtype, dtype
self.FILE.Set_view(disp, etype, ftype,
datarep, MPI.INFO_NULL)
of, et, ft, dr = self.FILE.Get_view()
self.assertEqual(disp, of)
self.assertEqual(etype.Get_extent(), et.Get_extent())
self.assertEqual(ftype.Get_extent(), ft.Get_extent())
self.assertEqual(datarep, dr)
try:
if not et.is_predefined: et.Free()
except NotImplementedError:
if et != etype: et.Free()
try:
if not ft.is_predefined: ft.Free()
except NotImplementedError:
if ft != ftype: ft.Free()
def testGetSetAtomicity(self):
atom = self.FILE.Get_atomicity()
self.assertFalse(atom)
for atomicity in [True, False] * 4:
self.FILE.Set_atomicity(atomicity)
atom = self.FILE.Get_atomicity()
self.assertEqual(atom, atomicity)
def testSync(self):
self.FILE.Sync()
def testSeekGetPosition(self):
offset = 0
self.FILE.Seek(offset, MPI.SEEK_END)
self.FILE.Seek(offset, MPI.SEEK_CUR)
self.FILE.Seek(offset, MPI.SEEK_SET)
pos = self.FILE.Get_position()
self.assertEqual(pos, offset)
def testSeekGetPositionShared(self):
offset = 0
self.FILE.Seek_shared(offset, MPI.SEEK_END)
self.FILE.Seek_shared(offset, MPI.SEEK_CUR)
self.FILE.Seek_shared(offset, MPI.SEEK_SET)
pos = self.FILE.Get_position_shared()
self.assertEqual(pos, offset)
@unittest.skipMPI('openmpi(==2.0.0)')
def testGetByteOffset(self):
for offset in range(10):
disp = self.FILE.Get_byte_offset(offset)
self.assertEqual(disp, offset)
def testGetTypeExtent(self):
extent = self.FILE.Get_type_extent(MPI.BYTE)
self.assertEqual(extent, 1)
def testGetErrhandler(self):
eh = self.FILE.Get_errhandler()
self.assertEqual(eh, MPI.ERRORS_RETURN)
eh.Free()
class TestFileNull(unittest.TestCase):
def setUp(self):
self.eh_save = MPI.FILE_NULL.Get_errhandler()
def tearDown(self):
MPI.FILE_NULL.Set_errhandler(self.eh_save)
self.eh_save.Free()
def testGetSetErrhandler(self):
eh = MPI.FILE_NULL.Get_errhandler()
self.assertEqual(eh, MPI.ERRORS_RETURN)
eh.Free()
MPI.FILE_NULL.Set_errhandler(MPI.ERRORS_ARE_FATAL)
eh = MPI.FILE_NULL.Get_errhandler()
self.assertEqual(eh, MPI.ERRORS_ARE_FATAL)
eh.Free()
MPI.FILE_NULL.Set_errhandler(MPI.ERRORS_RETURN)
eh = MPI.FILE_NULL.Get_errhandler()
self.assertEqual(eh, MPI.ERRORS_RETURN)
eh.Free()
class TestFileSelf(BaseTestFile, unittest.TestCase):
COMM = MPI.COMM_SELF
prefix = BaseTestFile.prefix + ('-%d' % MPI.COMM_WORLD.Get_rank())
def have_feature():
case = BaseTestFile()
case.COMM = TestFileSelf.COMM
case.prefix = TestFileSelf.prefix
case.setUp()
case.tearDown()
try:
have_feature()
except NotImplementedError:
unittest.disable(BaseTestFile, 'mpi-file')
unittest.disable(TestFileNull, 'mpi-file')
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_p2p_obj.py 0000664 0001750 0001750 00000052513 13426006675 017741 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
import sys
pypy_lt_53 = (hasattr(sys, 'pypy_version_info') and
sys.pypy_version_info < (5, 3))
def allocate(n):
if pypy_lt_53:
try:
import array
return array.array('B', [0]) * n
except ImportError:
return None
return bytearray(n)
_basic = [None,
True, False,
-7, 0, 7,
-2**63+1, 2**63-1,
-2.17, 0.0, 3.14,
1+2j, 2-3j,
'mpi4py',
]
messages = list(_basic)
messages += [ list(_basic),
tuple(_basic),
set(_basic),
frozenset(_basic),
dict([('k%d' % key, val)
for key, val in enumerate(_basic)])
]
class BaseTestP2PObj(object):
COMM = MPI.COMM_NULL
def testSendAndRecv(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for smess in messages:
self.COMM.send(smess, MPI.PROC_NULL)
rmess = self.COMM.recv(None, MPI.PROC_NULL, 0)
self.assertEqual(rmess, None)
if size == 1: return
for smess in messages:
if rank == 0:
self.COMM.send(smess, rank+1, 0)
rmess = smess
elif rank == size - 1:
rmess = self.COMM.recv(None, rank-1, 0)
else:
rmess = self.COMM.recv(None, rank-1, 0)
self.COMM.send(rmess, rank+1, 0)
self.assertEqual(rmess, smess)
def testISendAndRecv(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
buf = None
for smess in messages:
req = self.COMM.isend(smess, MPI.PROC_NULL)
self.assertTrue(req)
req.Wait()
self.assertFalse(req)
rmess = self.COMM.recv(buf, MPI.PROC_NULL, 0)
self.assertEqual(rmess, None)
for smess in messages:
req = self.COMM.isend(smess, rank, 0)
self.assertTrue(req)
rmess = self.COMM.recv(buf, rank, 0)
self.assertTrue(req)
flag = req.Test()
self.assertTrue(flag)
self.assertFalse(req)
self.assertEqual(rmess, smess)
for smess in messages:
dst = (rank+1)%size
src = (rank-1)%size
req = self.COMM.isend(smess, dst, 0)
self.assertTrue(req)
rmess = self.COMM.recv(buf, src, 0)
req.Wait()
self.assertFalse(req)
self.assertEqual(rmess, smess)
def testIRecvAndSend(self):
comm = self.COMM
size = comm.Get_size()
rank = comm.Get_rank()
for smess in messages:
req = comm.irecv(0, MPI.PROC_NULL)
self.assertTrue(req)
comm.send(smess, MPI.PROC_NULL)
rmess = req.wait()
self.assertFalse(req)
self.assertEqual(rmess, None)
for smess in messages:
buf = allocate(512)
req = comm.irecv(buf, rank, 0)
self.assertTrue(req)
flag, rmess = req.test()
self.assertTrue(req)
self.assertFalse(flag)
self.assertEqual(rmess, None)
comm.send(smess, rank, 0)
self.assertTrue(req)
flag, rmess = req.test()
while not flag: flag, rmess = req.test()
self.assertTrue(flag)
self.assertFalse(req)
self.assertEqual(rmess, smess)
tmp = allocate(1024)
for buf in (None, 1024, tmp):
for smess in messages + [messages]:
dst = (rank+1)%size
src = (rank-1)%size
req = comm.irecv(buf, src, 0)
self.assertTrue(req)
comm.send(smess, dst, 0)
rmess = req.wait()
self.assertFalse(req)
self.assertEqual(rmess, smess)
for smess in messages:
src = dst = rank
rreq1 = comm.irecv(None, src, 1)
rreq2 = comm.irecv(None, src, 2)
rreq3 = comm.irecv(None, src, 3)
rreqs = [rreq1, rreq2, rreq3]
for i in range(len(rreqs)):
self.assertTrue(rreqs[i])
comm.send(smess, dst, i+1)
index, obj = MPI.Request.waitany(rreqs)
self.assertEqual(index, i)
self.assertEqual(obj, smess)
self.assertFalse(rreqs[index])
index, obj = MPI.Request.waitany(rreqs)
self.assertEqual(index, MPI.UNDEFINED)
self.assertEqual(obj, None)
for smess in messages:
src = dst = rank
rreq1 = comm.irecv(None, src, 1)
rreq2 = comm.irecv(None, src, 2)
rreq3 = comm.irecv(None, src, 3)
rreqs = [rreq1, rreq2, rreq3]
index, flag, obj = MPI.Request.testany(rreqs)
self.assertEqual(index, MPI.UNDEFINED)
self.assertEqual(flag, False)
self.assertEqual(obj, None)
for i in range(len(rreqs)):
self.assertTrue(rreqs[i])
comm.send(smess, dst, i+1)
index, flag, obj = MPI.Request.testany(rreqs)
while not flag:
index, flag, obj = MPI.Request.testany(rreqs)
self.assertEqual(index, i)
self.assertEqual(flag, True)
self.assertEqual(obj, smess)
self.assertFalse(rreqs[i])
index, flag, obj = MPI.Request.testany(rreqs)
self.assertEqual(index, MPI.UNDEFINED)
self.assertEqual(flag, True)
self.assertEqual(obj, None)
def testIRecvAndISend(self):
comm = self.COMM
size = comm.Get_size()
rank = comm.Get_rank()
tmp = allocate(512)
for smess in messages:
dst = (rank+1)%size
src = (rank-1)%size
rreq = comm.irecv(None, src, 0)
self.assertTrue(rreq)
sreq = comm.isend(smess, dst, 0)
self.assertTrue(sreq)
index1, mess1 = MPI.Request.waitany([sreq,rreq])
self.assertTrue(index1 in (0, 1))
if index1 == 0:
self.assertFalse(sreq)
self.assertTrue (rreq)
self.assertEqual(mess1, None)
else:
self.assertTrue (sreq)
self.assertFalse(rreq)
self.assertEqual(mess1, smess)
index2, mess2 = MPI.Request.waitany([sreq,rreq])
self.assertTrue(index2 in (0, 1))
self.assertNotEqual(index2, index1)
self.assertFalse(sreq)
self.assertFalse(rreq)
if index2 == 0:
self.assertEqual(mess2, None)
else:
self.assertEqual(mess2, smess)
for smess in messages:
dst = (rank+1)%size
src = (rank-1)%size
rreq = comm.irecv(None, src, 0)
self.assertTrue(rreq)
sreq = comm.isend(smess, dst, 0)
self.assertTrue(sreq)
index1, flag1, mess1 = MPI.Request.testany([sreq,rreq])
while not flag1:
index1, flag1, mess1 = MPI.Request.testany([sreq,rreq])
self.assertTrue(index1 in (0, 1))
if index1 == 0:
self.assertFalse(sreq)
self.assertTrue (rreq)
self.assertEqual(mess1, None)
else:
self.assertTrue (sreq)
self.assertFalse(rreq)
self.assertEqual(mess1, smess)
index2, flag2, mess2 = MPI.Request.testany([sreq,rreq])
while not flag2:
index2, flag2, mess2 = MPI.Request.testany([sreq,rreq])
self.assertTrue(index2 in (0, 1))
self.assertNotEqual(index2, index1)
self.assertFalse(sreq)
self.assertFalse(rreq)
if index2 == 0:
self.assertEqual(mess2, None)
else:
self.assertEqual(mess2, smess)
for buf in (None, 512, tmp):
for smess in messages:
dst = (rank+1)%size
src = (rank-1)%size
rreq = comm.irecv(buf, src, 0)
self.assertTrue(rreq)
sreq = comm.isend(smess, dst, 0)
self.assertTrue(sreq)
dummy, rmess = MPI.Request.waitall([sreq,rreq], [])
self.assertFalse(sreq)
self.assertFalse(rreq)
self.assertEqual(dummy, None)
self.assertEqual(rmess, smess)
for buf in (None, 512, tmp):
for smess in messages:
src = dst = rank
rreq = comm.irecv(buf, src, 1)
flag, msg = MPI.Request.testall([rreq])
self.assertEqual(flag, False)
self.assertEqual(msg, None)
sreq = comm.isend(smess, dst, 1)
while True:
flag, msg = MPI.Request.testall([sreq,rreq], [])
if not flag:
self.assertEqual(msg, None)
continue
(dummy, rmess) = msg
self.assertEqual(dummy, None)
self.assertEqual(rmess, smess)
break
def testManyISendAndRecv(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for smess in messages:
reqs = []
for k in range(6):
r = self.COMM.isend(smess, rank, 0)
reqs.append(r)
flag = MPI.Request.Testall(reqs)
if not flag:
index, flag = MPI.Request.Testany(reqs)
indices = MPI.Request.Testsome(reqs)
if indices is None:
count = MPI.UNDEFINED
indices = []
else:
count = len(indices)
self.assertTrue(count in [0, MPI.UNDEFINED])
for k in range(3):
rmess = self.COMM.recv(None, rank, 0)
self.assertEqual(rmess, smess)
flag = MPI.Request.Testall(reqs)
if not flag:
index, flag = MPI.Request.Testany(reqs)
self.assertEqual(index, 0)
self.assertTrue(flag)
indices = MPI.Request.Testsome(reqs)
if indices is None:
count = MPI.UNDEFINED
indices = []
else:
count = len(indices)
self.assertTrue(count >= 2)
indices = list(indices)
indices.sort()
self.assertTrue(indices[:2] == [1, 2])
for k in range(3):
rmess = self.COMM.recv(None, rank, 0)
self.assertEqual(rmess, smess)
flag = MPI.Request.Testall(reqs)
self.assertTrue(flag)
for smess in messages:
reqs = []
for k in range(6):
r = self.COMM.isend(smess, rank, 0)
reqs.append(r)
for k in range(3):
rmess = self.COMM.recv(None, rank, 0)
self.assertEqual(rmess, smess)
index = MPI.Request.Waitany(reqs)
self.assertTrue(index == 0)
self.assertTrue(flag)
indices1 = MPI.Request.Waitsome(reqs)
if indices1 is None:
count1 = MPI.UNDEFINED
indices1 = []
else:
count1 = len(indices1)
for k in range(3):
rmess = self.COMM.recv(None, rank, 0)
self.assertEqual(rmess, smess)
indices2 = MPI.Request.Waitsome(reqs)
if indices2 is None:
count2 = MPI.UNDEFINED
indices2 = []
else:
count2 = len(indices2)
if count1 == MPI.UNDEFINED: count1 = 0
if count2 == MPI.UNDEFINED: count2 = 0
self.assertEqual(6, 1+count1+count2)
indices = [0]+list(indices1)+list(indices2)
indices.sort()
self.assertEqual(indices, list(range(6)))
MPI.Request.Waitall(reqs)
def testSSendAndRecv(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for smess in messages:
self.COMM.ssend(smess, MPI.PROC_NULL)
rmess = self.COMM.recv(None, MPI.PROC_NULL, 0)
self.assertEqual(rmess, None)
if size == 1: return
for smess in messages:
if rank == 0:
self.COMM.ssend(smess, rank+1, 0)
rmess = smess
elif rank == size - 1:
rmess = self.COMM.recv(None, rank-1, 0)
else:
rmess = self.COMM.recv(None, rank-1, 0)
self.COMM.ssend(rmess, rank+1, 0)
self.assertEqual(rmess, smess)
def testISSendAndRecv(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for smess in messages:
req = self.COMM.issend(smess, MPI.PROC_NULL)
self.assertTrue(req)
req.Wait()
self.assertFalse(req)
rmess = self.COMM.recv(None, MPI.PROC_NULL, 0)
self.assertEqual(rmess, None)
for smess in messages:
req = self.COMM.issend(smess, rank, 0)
self.assertTrue(req)
flag = req.Test()
self.assertFalse(flag)
self.assertTrue(req)
rmess = self.COMM.recv(None, rank, 0)
self.assertTrue(req)
flag = req.Test()
self.assertTrue(flag)
self.assertFalse(req)
self.assertEqual(rmess, smess)
for smess in messages:
dst = (rank+1)%size
src = (rank-1)%size
req = self.COMM.issend(smess, dst, 0)
self.assertTrue(req)
rmess = self.COMM.recv(None, src, 0)
req.Wait()
self.assertFalse(req)
self.assertEqual(rmess, smess)
def testIRecvAndBSend(self):
comm = self.COMM
rank = comm.Get_rank()
buf = MPI.Alloc_mem((1<<16)+MPI.BSEND_OVERHEAD)
MPI.Attach_buffer(buf)
try:
for smess in messages:
src = dst = rank
req1 = comm.irecv(None, src, 1)
req2 = comm.irecv(None, src, 2)
req3 = comm.irecv(None, src, 3)
comm.bsend(smess, dst, 3)
comm.bsend(smess, dst, 2)
comm.bsend(smess, dst, 1)
self.assertEqual(smess, req3.wait())
self.assertEqual(smess, req2.wait())
self.assertEqual(smess, req1.wait())
comm.bsend(smess, MPI.PROC_NULL, 3)
finally:
MPI.Detach_buffer()
MPI.Free_mem(buf)
def testIRecvAndIBSend(self):
comm = self.COMM
rank = comm.Get_rank()
buf = MPI.Alloc_mem((1<<16)+MPI.BSEND_OVERHEAD)
MPI.Attach_buffer(buf)
try:
for smess in messages:
src = dst = rank
req1 = comm.irecv(None, src, 1)
req2 = comm.irecv(None, src, 2)
req3 = comm.irecv(None, src, 3)
req4 = comm.ibsend(smess, dst, 3)
req5 = comm.ibsend(smess, dst, 2)
req6 = comm.ibsend(smess, dst, 1)
MPI.Request.waitall([req4, req5, req6])
self.assertEqual(smess, req3.wait())
self.assertEqual(smess, req2.wait())
self.assertEqual(smess, req1.wait())
comm.ibsend(smess, MPI.PROC_NULL, 3).wait()
finally:
MPI.Detach_buffer()
MPI.Free_mem(buf)
def testIRecvAndSSend(self):
comm = self.COMM
rank = comm.Get_rank()
for smess in messages:
src = dst = rank
req1 = comm.irecv(None, src, 1)
req2 = comm.irecv(None, src, 2)
req3 = comm.irecv(None, src, 3)
comm.ssend(smess, dst, 3)
comm.ssend(smess, dst, 2)
comm.ssend(smess, dst, 1)
self.assertEqual(smess, req3.wait())
self.assertEqual(smess, req2.wait())
self.assertEqual(smess, req1.wait())
comm.ssend(smess, MPI.PROC_NULL, 3)
def testIRecvAndISSend(self):
comm = self.COMM
rank = comm.Get_rank()
for smess in messages:
src = dst = rank
req1 = comm.irecv(None, src, 1)
req2 = comm.irecv(None, src, 2)
req3 = comm.irecv(None, src, 3)
req4 = comm.issend(smess, dst, 3)
req5 = comm.issend(smess, dst, 2)
req6 = comm.issend(smess, dst, 1)
MPI.Request.waitall([req4, req5, req6])
self.assertEqual(smess, req3.wait())
self.assertEqual(smess, req2.wait())
self.assertEqual(smess, req1.wait())
comm.issend(smess, MPI.PROC_NULL, 3).wait()
def testSendrecv(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for smess in messages:
dest = (rank + 1) % size
source = (rank - 1) % size
rmess = self.COMM.sendrecv(smess, dest, 0,
None, source, 0)
continue
self.assertEqual(rmess, smess)
rmess = self.COMM.sendrecv(None, dest, 0,
None, source, 0)
self.assertEqual(rmess, None)
rmess = self.COMM.sendrecv(smess, MPI.PROC_NULL, 0,
None, MPI.PROC_NULL, 0)
self.assertEqual(rmess, None)
def testMixed(self):
comm = self.COMM
rank = comm.Get_rank()
#
sreq = comm.Isend([None, 0, 'B'], rank)
obj = comm.recv(None, rank)
sreq.Wait()
self.assertTrue(obj is None)
for smess in messages:
buf = MPI.pickle.dumps(smess)
sreq = comm.Isend([buf, 'B'], rank)
rmess = comm.recv(None, rank)
sreq.Wait()
self.assertTrue(rmess == smess)
#
sreq = comm.Isend([None, 0, 'B'], rank)
rreq = comm.irecv(None, rank)
sreq.Wait()
obj = rreq.wait()
self.assertTrue(obj is None)
for smess in messages:
buf = MPI.pickle.dumps(smess)
sreq = comm.Isend([buf, 'B'], rank)
rreq = comm.irecv(None, rank)
sreq.Wait()
rmess = rreq.wait()
self.assertTrue(rmess == smess)
def testPingPong01(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for smess in messages:
self.COMM.send(smess, MPI.PROC_NULL)
rmess = self.COMM.recv(None, MPI.PROC_NULL, 0)
self.assertEqual(rmess, None)
if size == 1: return
smess = None
if rank == 0:
self.COMM.send(smess, rank+1, 0)
rmess = self.COMM.recv(None, rank+1, 0)
elif rank == 1:
rmess = self.COMM.recv(None, rank-1, 0)
self.COMM.send(smess, rank-1, 0)
else:
rmess = smess
self.assertEqual(rmess, smess)
for smess in messages:
if rank == 0:
self.COMM.send(smess, rank+1, 0)
rmess = self.COMM.recv(None, rank+1, 0)
elif rank == 1:
rmess = self.COMM.recv(None, rank-1, 0)
self.COMM.send(smess, rank-1, 0)
else:
rmess = smess
self.assertEqual(rmess, smess)
@unittest.skipMPI('MPICH1')
def testProbe(self):
comm = self.COMM.Dup()
try:
status = MPI.Status()
flag = comm.iprobe(MPI.ANY_SOURCE, MPI.ANY_TAG, status)
self.assertFalse(flag)
for smess in messages:
request = comm.issend(smess, comm.rank, 123)
self.assertTrue(request)
flag = comm.iprobe(MPI.ANY_SOURCE, MPI.ANY_TAG, status)
self.assertEqual(status.source, comm.rank)
self.assertEqual(status.tag, 123)
self.assertTrue(flag)
comm.probe(MPI.ANY_SOURCE, MPI.ANY_TAG, status)
self.assertEqual(status.source, comm.rank)
self.assertEqual(status.tag, 123)
self.assertTrue(request)
flag, obj = request.test()
self.assertTrue(request)
self.assertFalse(flag)
self.assertEqual(obj, None)
obj = comm.recv(None, comm.rank, 123)
self.assertEqual(obj, smess)
self.assertTrue(request)
flag, obj = request.test()
self.assertFalse(request)
self.assertTrue(flag)
self.assertEqual(obj, None)
finally:
comm.Free()
class TestP2PObjSelf(BaseTestP2PObj, unittest.TestCase):
COMM = MPI.COMM_SELF
class TestP2PObjWorld(BaseTestP2PObj, unittest.TestCase):
COMM = MPI.COMM_WORLD
class TestP2PObjSelfDup(TestP2PObjSelf):
def setUp(self):
self.COMM = MPI.COMM_SELF.Dup()
def tearDown(self):
self.COMM.Free()
@unittest.skipMPI('openmpi(<1.4.0)', MPI.Query_thread() > MPI.THREAD_SINGLE)
class TestP2PObjWorldDup(TestP2PObjWorld):
def setUp(self):
self.COMM = MPI.COMM_WORLD.Dup()
def tearDown(self):
self.COMM.Free()
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_request.py 0000644 0001750 0001750 00000011262 13200562156 020057 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
class TestRequest(unittest.TestCase):
def setUp(self):
self.REQUEST = MPI.Request()
self.STATUS = MPI.Status()
def testWait(self):
self.REQUEST.Wait()
self.REQUEST.Wait(None)
self.REQUEST.Wait(self.STATUS)
self.assertTrue(self.REQUEST.Wait() is True)
self.REQUEST.wait()
self.REQUEST.wait(None)
self.REQUEST.wait(self.STATUS)
self.assertTrue(self.REQUEST.wait() is None)
def testTest(self):
self.REQUEST.Test()
self.REQUEST.Test(None)
self.REQUEST.Test(self.STATUS)
self.assertTrue(self.REQUEST.Test() is True)
self.REQUEST.test()
self.REQUEST.test(None)
self.REQUEST.test(self.STATUS)
self.assertTrue(self.REQUEST.test() == (True, None))
@unittest.skipMPI('MPICH1')
@unittest.skipMPI('LAM/MPI')
def testGetStatus(self):
try:
flag = self.REQUEST.Get_status()
except NotImplementedError:
self.skipTest('mpi-request-get_status')
self.assertTrue(flag)
flag = self.REQUEST.Get_status(self.STATUS)
self.assertTrue(flag)
self.assertEqual(self.STATUS.Get_source(), MPI.ANY_SOURCE)
self.assertEqual(self.STATUS.Get_tag(), MPI.ANY_TAG)
self.assertEqual(self.STATUS.Get_error(), MPI.SUCCESS)
self.assertEqual(self.STATUS.Get_count(MPI.BYTE), 0)
self.assertEqual(self.STATUS.Get_elements(MPI.BYTE), 0)
try:
self.assertFalse(self.STATUS.Is_cancelled())
except NotImplementedError:
self.skipTest('mpi-status-is_cancelled')
class TestRequestArray(unittest.TestCase):
def setUp(self):
self.REQUESTS = [MPI.Request() for i in range(5)]
self.STATUSES = [MPI.Status() for i in range(5)]
def testWaitany(self):
MPI.Request.Waitany(self.REQUESTS)
MPI.Request.Waitany(self.REQUESTS, None)
MPI.Request.Waitany(self.REQUESTS, self.STATUSES[0])
MPI.Request.waitany(self.REQUESTS)
MPI.Request.waitany(self.REQUESTS, None)
MPI.Request.waitany(self.REQUESTS, self.STATUSES[0])
def testTestany(self):
MPI.Request.Testany(self.REQUESTS)
MPI.Request.Testany(self.REQUESTS, None)
MPI.Request.Testany(self.REQUESTS, self.STATUSES[0])
MPI.Request.testany(self.REQUESTS)
MPI.Request.testany(self.REQUESTS, None)
MPI.Request.testany(self.REQUESTS, self.STATUSES[0])
def testWaitall(self):
MPI.Request.Waitall(self.REQUESTS)
MPI.Request.Waitall(self.REQUESTS, None)
self.assertTrue(MPI.Request.Waitall(self.REQUESTS) is True)
for statuses in (tuple(self.STATUSES), (self.STATUSES[0],), ()):
MPI.Request.Waitall(self.REQUESTS, statuses)
for statuses in (self.STATUSES, []):
MPI.Request.Waitall(self.REQUESTS, statuses)
self.assertEqual(len(statuses), len(self.REQUESTS))
MPI.Request.waitall(self.REQUESTS)
MPI.Request.waitall(self.REQUESTS, None)
for statuses in (self.STATUSES, []):
MPI.Request.waitall(self.REQUESTS, statuses)
self.assertEqual(len(statuses), len(self.REQUESTS))
def testTestall(self):
MPI.Request.Testall(self.REQUESTS)
MPI.Request.Testall(self.REQUESTS, None)
self.assertTrue(MPI.Request.Testall(self.REQUESTS) is True)
for statuses in (self.STATUSES, []):
MPI.Request.Testall(self.REQUESTS, statuses)
self.assertEqual(len(statuses), len(self.REQUESTS))
MPI.Request.testall(self.REQUESTS)
MPI.Request.testall(self.REQUESTS, None)
for statuses in (self.STATUSES, []):
MPI.Request.testall(self.REQUESTS, statuses)
self.assertEqual(len(statuses), len(self.REQUESTS))
def testWaitsome(self):
ret = MPI.Request.Waitsome(self.REQUESTS)
self.assertEqual(ret, None)
ret = MPI.Request.Waitsome(self.REQUESTS, None)
self.assertEqual(ret, None)
for statuses in (self.STATUSES, []):
ret = MPI.Request.Waitsome(self.REQUESTS, statuses)
self.assertEqual(ret, None)
self.assertEqual(len(statuses), len(self.REQUESTS))
def testTestsome(self):
ret = MPI.Request.Testsome(self.REQUESTS)
self.assertEqual(ret, None)
ret = MPI.Request.Testsome(self.REQUESTS, None)
self.assertEqual(ret, None)
for statuses in (self.STATUSES, []):
ret = MPI.Request.Testsome(self.REQUESTS, statuses)
self.assertEqual(ret, None)
self.assertEqual(len(statuses), len(self.REQUESTS))
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_status.py 0000644 0001750 0001750 00000006217 13200562156 017716 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
class TestStatus(unittest.TestCase):
def setUp(self):
self.STATUS = MPI.Status()
def tearDown(self):
self.STATUS = None
def testDefaultFieldValues(self):
self.assertEqual(self.STATUS.Get_source(), MPI.ANY_SOURCE)
self.assertEqual(self.STATUS.Get_tag(), MPI.ANY_TAG)
self.assertEqual(self.STATUS.Get_error(), MPI.SUCCESS)
def testGetCount(self):
count = self.STATUS.Get_count(MPI.BYTE)
self.assertEqual(count, 0)
def testGetElements(self):
elements = self.STATUS.Get_elements(MPI.BYTE)
self.assertEqual(elements, 0)
def testSetElements(self):
try:
self.STATUS.Set_elements(MPI.BYTE, 7)
count = self.STATUS.Get_count(MPI.BYTE)
self.assertEqual(count, 7)
elements = self.STATUS.Get_elements(MPI.BYTE)
self.assertEqual(elements, 7)
except NotImplementedError:
if MPI.Get_version() >= (2,0): raise
self.skipTest('mpi-status-set_elements')
def testIsCancelled(self):
flag = self.STATUS.Is_cancelled()
self.assertTrue(type(flag) is bool)
self.assertFalse(flag)
def testSetCancelled(self):
try:
self.STATUS.Set_cancelled(True)
flag = self.STATUS.Is_cancelled()
self.assertTrue(flag)
except NotImplementedError:
if MPI.Get_version() >= (2,0): raise
self.skipTest('mpi-status-set_cancelled')
def testPyProps(self):
self.assertEqual(self.STATUS.Get_source(), self.STATUS.source)
self.assertEqual(self.STATUS.Get_tag(), self.STATUS.tag)
self.assertEqual(self.STATUS.Get_error(), self.STATUS.error)
self.STATUS.source = 1
self.STATUS.tag = 2
self.STATUS.error = MPI.ERR_ARG
self.assertEqual(self.STATUS.source, 1)
self.assertEqual(self.STATUS.tag, 2)
self.assertEqual(self.STATUS.error, MPI.ERR_ARG)
def testConstructor(self):
self.assertRaises(TypeError, MPI.Status, 123)
self.assertRaises(TypeError, MPI.Status, "abc")
def testCopyConstructor(self):
self.STATUS.source = 1
self.STATUS.tag = 2
self.STATUS.error = MPI.ERR_ARG
status = MPI.Status(self.STATUS)
self.assertEqual(status.source, 1)
self.assertEqual(status.tag, 2)
self.assertEqual(status.error, MPI.ERR_ARG)
try:
self.STATUS.Set_elements(MPI.BYTE, 7)
except NotImplementedError:
pass
try:
self.STATUS.Set_cancelled(True)
except NotImplementedError:
pass
status = MPI.Status(self.STATUS)
try:
count = status.Get_count(MPI.BYTE)
elems = status.Get_elements(MPI.BYTE)
self.assertEqual(count, 7)
self.assertEqual(elems, 7)
except NotImplementedError:
pass
try:
flag = status.Is_cancelled()
self.assertTrue(flag)
except NotImplementedError:
pass
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_errhandler.py 0000664 0001750 0001750 00000005532 13426006675 020533 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
class TestErrhandler(unittest.TestCase):
def testPredefined(self):
self.assertFalse(MPI.ERRHANDLER_NULL)
self.assertTrue(MPI.ERRORS_ARE_FATAL)
self.assertTrue(MPI.ERRORS_RETURN)
def testCommGetSetErrhandler(self):
for COMM in [MPI.COMM_SELF, MPI.COMM_WORLD]:
for ERRHANDLER in [MPI.ERRORS_ARE_FATAL, MPI.ERRORS_RETURN,
MPI.ERRORS_ARE_FATAL, MPI.ERRORS_RETURN, ]:
errhdl_1 = COMM.Get_errhandler()
self.assertNotEqual(errhdl_1, MPI.ERRHANDLER_NULL)
COMM.Set_errhandler(ERRHANDLER)
errhdl_2 = COMM.Get_errhandler()
self.assertEqual(errhdl_2, ERRHANDLER)
errhdl_2.Free()
self.assertEqual(errhdl_2, MPI.ERRHANDLER_NULL)
COMM.Set_errhandler(errhdl_1)
errhdl_1.Free()
self.assertEqual(errhdl_1, MPI.ERRHANDLER_NULL)
def testGetErrhandler(self):
errhdls = []
for i in range(100):
e = MPI.COMM_WORLD.Get_errhandler()
errhdls.append(e)
for e in errhdls:
e.Free()
for e in errhdls:
self.assertEqual(e, MPI.ERRHANDLER_NULL)
@unittest.skipMPI('MPI(<2.0)')
def testCommCallErrhandler(self):
errhdl = MPI.COMM_SELF.Get_errhandler()
comm = MPI.COMM_SELF.Dup()
comm.Set_errhandler(MPI.ERRORS_RETURN)
comm.Call_errhandler(MPI.ERR_OTHER)
comm.Free()
@unittest.skipMPI('MPI(<2.0)')
@unittest.skipMPI('SpectrumMPI')
def testWinCallErrhandler(self):
try:
win = MPI.Win.Create(MPI.BOTTOM, 1, MPI.INFO_NULL, MPI.COMM_SELF)
except NotImplementedError:
self.skipTest('mpi-win')
win.Set_errhandler(MPI.ERRORS_RETURN)
win.Call_errhandler(MPI.ERR_OTHER)
win.Free()
@unittest.skipMPI('MPI(<2.0)')
@unittest.skipMPI('msmpi')
def testFileCallErrhandler(self):
import os, tempfile
rank = MPI.COMM_WORLD.Get_rank()
fd, filename = tempfile.mkstemp(prefix='mpi4py-', suffix="-%d"%rank)
os.close(fd)
amode = MPI.MODE_WRONLY | MPI.MODE_CREATE | MPI.MODE_DELETE_ON_CLOSE
try:
file = MPI.File.Open(MPI.COMM_SELF, filename, amode, MPI.INFO_NULL)
except NotImplementedError:
self.skipTest('mpi-file')
file.Set_errhandler(MPI.ERRORS_RETURN)
#file.Call_errhandler(MPI.ERR_OTHER)
file.Call_errhandler(MPI.SUCCESS)
file.Close()
try:
MPI.Win.Create(MPI.BOTTOM, 1, MPI.INFO_NULL, MPI.COMM_SELF).Free()
except (NotImplementedError, MPI.Exception):
TestErrhandler.testWinCallErrhandler = \
unittest.disable(TestErrhandler.testWinCallErrhandler, 'mpi-win')
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_cco_ngh_obj.py 0000644 0001750 0001750 00000006672 13200562156 020632 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
_basic = [None,
True, False,
-7, 0, 7, 2**31,
-2**63+1, 2**63-1,
-2.17, 0.0, 3.14,
1+2j, 2-3j,
'mpi4py',
]
messages = _basic
messages += [ list(_basic),
tuple(_basic),
dict([('k%d' % key, val)
for key, val in enumerate(_basic)])
]
messages = messages + [messages]
def create_topo_comms(comm):
size = comm.Get_size()
rank = comm.Get_rank()
# Cartesian
n = int(size**1/2.0)
m = int(size**1/3.0)
if m*m*m == size:
dims = [m, m, m]
elif n*n == size:
dims = [n, n]
else:
dims = [size]
periods = [True] * len(dims)
yield comm.Create_cart(dims, periods=periods)
# Graph
index, edges = [0], []
for i in range(size):
pos = index[-1]
index.append(pos+2)
edges.append((i-1)%size)
edges.append((i+1)%size)
yield comm.Create_graph(index, edges)
# Dist Graph
sources = [(rank-2)%size, (rank-1)%size]
destinations = [(rank+1)%size, (rank+2)%size]
yield comm.Create_dist_graph_adjacent(sources, destinations)
def get_neighbors_count(comm):
topo = comm.Get_topology()
if topo == MPI.CART:
ndim = comm.Get_dim()
return 2*ndim, 2*ndim
if topo == MPI.GRAPH:
rank = comm.Get_rank()
nneighbors = comm.Get_neighbors_count(rank)
return nneighbors, nneighbors
if topo == MPI.DIST_GRAPH:
indeg, outdeg, w = comm.Get_dist_neighbors_count()
return indeg, outdeg
return 0, 0
def have_feature():
cartcomm = MPI.COMM_SELF.Create_cart([1], periods=[0])
try:
cartcomm.neighbor_allgather(None)
return True
except NotImplementedError:
return False
finally:
cartcomm.Free()
@unittest.skipIf(not have_feature(), 'mpi-neighbor')
class BaseTestCCONghObj(object):
COMM = MPI.COMM_NULL
@unittest.skipMPI('openmpi(<2.2.0)')
def testNeighborAllgather(self):
for comm in create_topo_comms(self.COMM):
rsize, ssize = get_neighbors_count(comm)
for smess in messages:
rmess = comm.neighbor_allgather(smess)
self.assertEqual(rmess, [smess] * rsize)
comm.Free()
def testNeighborAlltoall(self):
for comm in create_topo_comms(self.COMM):
rsize, ssize = get_neighbors_count(comm)
for smess in messages:
rmess = comm.neighbor_alltoall([smess] * ssize)
self.assertEqual(rmess, [smess] * rsize)
comm.Free()
class TestCCONghObjSelf(BaseTestCCONghObj, unittest.TestCase):
COMM = MPI.COMM_SELF
class TestCCONghObjWorld(BaseTestCCONghObj, unittest.TestCase):
COMM = MPI.COMM_WORLD
class TestCCONghObjSelfDup(TestCCONghObjSelf):
def setUp(self):
self.COMM = MPI.COMM_SELF.Dup()
def tearDown(self):
self.COMM.Free()
class TestCCONghObjWorldDup(TestCCONghObjWorld):
def setUp(self):
self.COMM = MPI.COMM_WORLD.Dup()
def tearDown(self):
self.COMM.Free()
name, version = MPI.get_vendor()
if name == 'Open MPI' and version < (1,8,4):
_create_topo_comms = create_topo_comms
def create_topo_comms(comm):
for c in _create_topo_comms(comm):
if c.size * 2 < sum(c.degrees):
c.Free(); continue
yield c
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_pickle.py 0000644 0001750 0001750 00000012146 13200562156 017640 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
import sys
try:
import cPickle
except ImportError:
cPickle = None
try:
import pickle as pyPickle
except ImportError:
pyPickle = None
try:
import dill
except ImportError:
dill = None
try:
import marshal
except ImportError:
marshal = None
try:
import simplejson
except ImportError:
simplejson = None
try:
import json
except ImportError:
json = None
try:
import yaml
yaml.dump(None)
except ImportError:
yaml = None
OBJS = [
None,
True,
False,
7,
1<<32,
3.14,
1+2j,
'qwerty',
(0, 1, 2),
[0, 1, 2],
{'a':0, 'b':1},
]
try:
memoryview
tobytes = lambda s: memoryview(s).tobytes()
except NameError:
tobytes = lambda s: buffer(s)[:]
class TestPickle(unittest.TestCase):
def setUp(self):
self.pickle = MPI.pickle
def tearDown(self):
self.pickle.__init__()
def do_pickle(self, obj, pickle):
comm = MPI.COMM_SELF
o = comm.sendrecv(obj, 0, 0, None, 0, 0)
self.assertEqual(obj, o)
s = pickle.dumps(obj)
o = pickle.loads(s)
self.assertEqual(obj, o)
def testDefault(self):
pickle = self.pickle
protocols = [0, 1, 2]
if sys.version_info[:2] >= (3, 0):
protocols.append(3)
if sys.version_info[:2] >= (3, 4):
protocols.append(4)
protocols.append(-1)
protocols.append(None)
for proto in protocols:
pickle.__init__(protocol=proto)
for obj in OBJS:
self.do_pickle(obj, pickle)
self.do_pickle(OBJS, pickle)
def testCPickle(self):
if cPickle is None: return
pickle = self.pickle
dumps = cPickle.dumps
loads = cPickle.loads
protocols = [0, 1, 2]
if sys.version_info[:2] >= (3, 0):
protocols.append(3)
if sys.version_info[:2] >= (3, 4):
protocols.append(4)
protocols.append(-1)
protocols.append(None)
for proto in protocols:
pickle.__init__(dumps, loads, proto)
for obj in OBJS:
self.do_pickle(obj, pickle)
self.do_pickle(OBJS, pickle)
def testPyPickle(self):
pickle = self.pickle
dumps = pyPickle.dumps
loads = pyPickle.loads
protocols = [0, 1, 2]
if sys.version_info[:2] >= (3, 0):
protocols.append(3)
if sys.version_info[:2] >= (3, 4):
protocols.append(4)
protocols.append(-1)
protocols.append(None)
for proto in protocols:
pickle.__init__(dumps, loads, proto)
for obj in OBJS:
self.do_pickle(obj, pickle)
self.do_pickle(OBJS, pickle)
@unittest.skipIf(dill is None, 'dill')
def testDill(self):
pickle = self.pickle
dumps = dill.dumps
loads = dill.loads
protocols = list(range(dill.HIGHEST_PROTOCOL+1))
protocols.append(-1)
protocols.append(None)
for proto in protocols:
pickle.__init__(dumps, loads, proto)
for obj in OBJS:
self.do_pickle(obj, pickle)
self.do_pickle(OBJS, pickle)
@unittest.skipIf(marshal is None, 'marshal')
def testMarshal(self):
pickle = self.pickle
dumps = marshal.dumps
loads = marshal.loads
protocols = [0, 1, 2]
if sys.version_info[:2] >= (3, 4):
protocols.append(3)
protocols.append(4)
protocols.append(None)
for protocol in protocols:
pickle.__init__(dumps, loads, protocol)
for obj in OBJS:
self.do_pickle(obj, pickle)
self.do_pickle(OBJS, pickle)
@unittest.skipIf(simplejson is None, 'simplejson')
def testSimpleJson(self):
pickle = self.pickle
dumps = lambda o: simplejson.dumps(o).encode()
loads = lambda s: simplejson.loads(tobytes(s).decode())
pickle.__init__(dumps, loads)
OBJS2 = [o for o in OBJS
if not isinstance(o, (float, complex, tuple))]
for obj in OBJS2:
self.do_pickle(obj, pickle)
self.do_pickle(OBJS2, pickle)
@unittest.skipIf(json is None, 'json')
def testJson(self):
pickle = self.pickle
dumps = lambda o: json.dumps(o).encode()
loads = lambda s: json.loads(tobytes(s).decode())
pickle.__init__(dumps, loads)
OBJS2 = [o for o in OBJS
if not isinstance(o, (float, complex, tuple))]
for obj in OBJS2:
self.do_pickle(obj, pickle)
self.do_pickle(OBJS2, pickle)
@unittest.skipIf(yaml is None, 'yaml')
def testYAML(self):
pickle = self.pickle
dumps = lambda o: yaml.dump(o).encode()
loads = lambda s: yaml.load(tobytes(s).decode())
pickle.__init__(dumps, loads)
OBJS2 = [o for o in OBJS
if not isinstance(o, (complex, tuple))]
for obj in OBJS2:
self.do_pickle(obj, pickle)
self.do_pickle(OBJS2, pickle)
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_environ.py 0000644 0001750 0001750 00000006652 13200562156 020056 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
def appnum():
if MPI.APPNUM == MPI.KEYVAL_INVALID: return None
return MPI.COMM_WORLD.Get_attr(MPI.APPNUM)
class TestEnviron(unittest.TestCase):
def testIsInitialized(self):
flag = MPI.Is_initialized()
self.assertTrue(type(flag) is bool)
self.assertTrue(flag)
def testIsFinalized(self):
flag = MPI.Is_finalized()
self.assertTrue(type(flag) is bool)
self.assertFalse(flag)
def testGetVersion(self):
version = MPI.Get_version()
self.assertEqual(len(version), 2)
major, minor = version
self.assertTrue(type(major) is int)
self.assertTrue(major >= 1)
self.assertTrue(type(minor) is int)
self.assertTrue(minor >= 0)
def testGetLibraryVersion(self):
version = MPI.Get_library_version()
self.assertTrue(isinstance(version, str))
self.assertTrue(len(version) > 0)
def testGetProcessorName(self):
procname = MPI.Get_processor_name()
self.assertTrue(isinstance(procname, str))
def testWTime(self):
time1 = MPI.Wtime()
self.assertTrue(type(time1) is float)
time2 = MPI.Wtime()
self.assertTrue(type(time2) is float)
self.assertTrue(time2 >= time1)
def testWTick(self):
tick = MPI.Wtick()
self.assertTrue(type(tick) is float)
self.assertTrue(tick > 0.0)
def testPControl(self):
for level in (2, 1, 0):
MPI.Pcontrol(level)
MPI.Pcontrol(1)
class TestWorldAttrs(unittest.TestCase):
def testWTimeIsGlobal(self):
wtg = MPI.COMM_WORLD.Get_attr(MPI.WTIME_IS_GLOBAL)
if wtg is not None:
self.assertTrue(wtg in (True, False))
def testWTimeIsGlobal(self):
wtg = MPI.COMM_WORLD.Get_attr(MPI.WTIME_IS_GLOBAL)
if wtg is not None:
self.assertTrue(wtg in (True, False))
def testHostPorcessor(self):
size = MPI.COMM_WORLD.Get_size()
vals = list(range(size)) + [MPI.PROC_NULL]
hostproc = MPI.COMM_WORLD.Get_attr(MPI.HOST)
if hostproc is not None:
self.assertTrue(hostproc in vals)
def testIOProcessor(self):
size = MPI.COMM_WORLD.Get_size()
vals = list(range(size)) + [MPI.UNDEFINED,
MPI.ANY_SOURCE,
MPI.PROC_NULL]
ioproc = MPI.COMM_WORLD.Get_attr(MPI.IO)
if ioproc is not None:
self.assertTrue(ioproc in vals)
@unittest.skipIf(MPI.APPNUM == MPI.KEYVAL_INVALID, 'mpi-appnum')
def testAppNum(self):
appnum = MPI.COMM_WORLD.Get_attr(MPI.APPNUM)
if appnum is not None:
self.assertTrue(appnum == MPI.UNDEFINED or appnum >= 0)
@unittest.skipMPI('MPICH(>1.2.0)', appnum() is None)
@unittest.skipMPI('MVAPICH2', appnum() is None)
@unittest.skipMPI('MPICH2', appnum() is None)
@unittest.skipIf(MPI.UNIVERSE_SIZE == MPI.KEYVAL_INVALID, 'mpi-universe-size')
def testUniverseSize(self):
univsz = MPI.COMM_WORLD.Get_attr(MPI.UNIVERSE_SIZE)
if univsz is not None:
self.assertTrue(univsz == MPI.UNDEFINED or univsz >= 0)
@unittest.skipIf(MPI.LASTUSEDCODE == MPI.KEYVAL_INVALID, 'mpi-lastusedcode')
def testLastUsedCode(self):
lastuc = MPI.COMM_WORLD.Get_attr(MPI.LASTUSEDCODE)
self.assertTrue(lastuc >= 0)
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_rma_nb.py 0000664 0001750 0001750 00000015730 13426006675 017644 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
import arrayimpl
import sys
pypy_lt_53 = (hasattr(sys, 'pypy_version_info') and
sys.pypy_version_info < (5, 3))
def mkzeros(n):
if pypy_lt_53:
return b'\0' * n
return bytearray(n)
def memzero(m):
try:
m[:] = 0
except IndexError: # cffi buffer
m[0:len(m)] = b'\0'*len(m)
class BaseTestRMA(object):
COMM = MPI.COMM_NULL
INFO = MPI.INFO_NULL
COUNT_MIN = 0
def setUp(self):
nbytes = 100*MPI.DOUBLE.size
try:
self.mpi_memory = MPI.Alloc_mem(nbytes)
self.memory = self.mpi_memory
memzero(self.memory)
except MPI.Exception:
import array
self.mpi_memory = None
self.memory = array.array('B',[0]*nbytes)
self.WIN = MPI.Win.Create(self.memory, 1, self.INFO, self.COMM)
def tearDown(self):
self.WIN.Free()
if self.mpi_memory:
MPI.Free_mem(self.mpi_memory)
def testPutGet(self):
group = self.WIN.Get_group()
size = group.Get_size()
group.Free()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for count in range(self.COUNT_MIN, 10):
for rank in range(size):
sbuf = array([rank]*count, typecode)
rbuf = array(-1, typecode, count+1)
self.WIN.Fence()
self.WIN.Lock(rank)
r = self.WIN.Rput(sbuf.as_mpi(), rank)
r.Wait()
self.WIN.Flush(rank)
r = self.WIN.Rget(rbuf.as_mpi_c(count), rank)
r.Wait()
self.WIN.Unlock(rank)
for i in range(count):
self.assertEqual(sbuf[i], rank)
self.assertEqual(rbuf[i], rank)
self.assertEqual(rbuf[-1], -1)
@unittest.skipMPI('openmpi(>=1.10.0,<1.11.0)')
def testAccumulate(self):
group = self.WIN.Get_group()
size = group.Get_size()
group.Free()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for count in range(self.COUNT_MIN, 10):
for rank in range(size):
ones = array([1]*count, typecode)
sbuf = array(range(count), typecode)
rbuf = array(-1, typecode, count+1)
for op in (MPI.SUM, MPI.PROD,
MPI.MAX, MPI.MIN,
MPI.REPLACE):
self.WIN.Lock(rank)
self.WIN.Put(ones.as_mpi(), rank)
self.WIN.Flush(rank)
r = self.WIN.Raccumulate(sbuf.as_mpi(),
rank, op=op)
r.Wait()
self.WIN.Flush(rank)
r = self.WIN.Rget(rbuf.as_mpi_c(count), rank)
r.Wait()
self.WIN.Unlock(rank)
#
for i in range(count):
self.assertEqual(sbuf[i], i)
self.assertEqual(rbuf[i], op(1, i))
self.assertEqual(rbuf[-1], -1)
@unittest.skipMPI('openmpi(>=1.10,<1.11)')
def testGetAccumulate(self):
group = self.WIN.Get_group()
size = group.Get_size()
group.Free()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for count in range(self.COUNT_MIN, 10):
for rank in range(size):
ones = array([1]*count, typecode)
sbuf = array(range(count), typecode)
rbuf = array(-1, typecode, count+1)
gbuf = array(-1, typecode, count+1)
for op in (MPI.SUM, MPI.PROD,
MPI.MAX, MPI.MIN,
MPI.REPLACE, MPI.NO_OP):
self.WIN.Lock(rank)
self.WIN.Put(ones.as_mpi(), rank)
self.WIN.Flush(rank)
r = self.WIN.Rget_accumulate(sbuf.as_mpi(),
rbuf.as_mpi_c(count),
rank, op=op)
r.Wait()
self.WIN.Flush(rank)
r = self.WIN.Rget(gbuf.as_mpi_c(count), rank)
r.Wait()
self.WIN.Unlock(rank)
#
for i in range(count):
self.assertEqual(sbuf[i], i)
self.assertEqual(rbuf[i], 1)
self.assertEqual(gbuf[i], op(1, i))
self.assertEqual(rbuf[-1], -1)
self.assertEqual(gbuf[-1], -1)
def testPutProcNull(self):
rank = self.COMM.Get_rank()
self.WIN.Lock(rank)
r = self.WIN.Rput(None, MPI.PROC_NULL, None)
r.Wait()
self.WIN.Unlock(rank)
def testGetProcNull(self):
rank = self.COMM.Get_rank()
self.WIN.Lock(rank)
r = self.WIN.Rget(None, MPI.PROC_NULL, None)
r.Wait()
self.WIN.Unlock(rank)
def testAccumulateProcNullReplace(self):
rank = self.COMM.Get_rank()
zeros = mkzeros(8)
self.WIN.Lock(rank)
r = self.WIN.Raccumulate([zeros, MPI.INT], MPI.PROC_NULL, None, MPI.REPLACE)
r.Wait()
r = self.WIN.Raccumulate([zeros, MPI.INT], MPI.PROC_NULL, None, MPI.REPLACE)
r.Wait()
self.WIN.Unlock(rank)
def testAccumulateProcNullSum(self):
rank = self.COMM.Get_rank()
zeros = mkzeros(8)
self.WIN.Lock(rank)
r = self.WIN.Raccumulate([zeros, MPI.INT], MPI.PROC_NULL, None, MPI.SUM)
r.Wait()
r = self.WIN.Raccumulate([None, MPI.INT], MPI.PROC_NULL, None, MPI.SUM)
r.Wait()
self.WIN.Unlock(rank)
@unittest.skipMPI('MPI(<3.0)')
@unittest.skipMPI('openmpi(<1.8.1)')
@unittest.skipMPI('MPICH2(<1.5.0)')
class TestRMASelf(BaseTestRMA, unittest.TestCase):
COMM = MPI.COMM_SELF
@unittest.skipMPI('MPI(<3.0)')
@unittest.skipMPI('openmpi(<1.8.1)')
@unittest.skipMPI('MPICH2(<1.5.0)')
class TestRMAWorld(BaseTestRMA, unittest.TestCase):
COMM = MPI.COMM_WORLD
SpectrumMPI = MPI.get_vendor()[0] == 'Spectrum MPI'
try:
if SpectrumMPI: raise NotImplementedError
MPI.Win.Create(None, 1, MPI.INFO_NULL, MPI.COMM_SELF).Free()
except (NotImplementedError, MPI.Exception):
unittest.disable(BaseTestRMA, 'mpi-rma-nb')
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/runtests.py 0000644 0001750 0001750 00000022371 13200562156 017222 0 ustar dalcinl dalcinl 0000000 0000000 import sys, os
import optparse
import unittest
def getoptionparser():
parser = optparse.OptionParser()
parser.add_option("-q", "--quiet",
action="store_const", const=0, dest="verbose", default=1,
help="minimal output")
parser.add_option("-v", "--verbose",
action="store_const", const=2, dest="verbose", default=1,
help="verbose output")
parser.add_option("-i", "--include", type="string",
action="append", dest="include", default=[],
help="include tests matching PATTERN", metavar="PATTERN")
parser.add_option("-e", "--exclude", type="string",
action="append", dest="exclude", default=[],
help="exclude tests matching PATTERN", metavar="PATTERN")
parser.add_option("-f", "--failfast",
action="store_true", dest="failfast", default=False,
help="stop on first failure")
parser.add_option("-c", "--catch",
action="store_true", dest="catchbreak", default=False,
help="catch Control-C and display results")
parser.add_option("--no-builddir",
action="store_false", dest="builddir", default=True,
help="disable testing from build directory")
parser.add_option("--path", type="string",
action="append", dest="path", default=[],
help="prepend PATH to sys.path", metavar="PATH")
parser.add_option("--refleaks", type="int",
action="store", dest="repeats", default=3,
help="run tests REPEAT times in a loop to catch leaks",
metavar="REPEAT")
parser.add_option("--threads",
action="store_true", dest="threads", default=None,
help="initialize MPI with thread support")
parser.add_option("--no-threads",
action="store_false", dest="threads", default=None,
help="initialize MPI without thread support")
parser.add_option("--thread-level", type="choice",
choices=["single", "funneled", "serialized", "multiple"],
action="store", dest="thread_level", default=None,
help="initialize MPI with required thread support")
parser.add_option("--mpe",
action="store_true", dest="mpe", default=False,
help="use MPE for MPI profiling")
parser.add_option("--vt",
action="store_true", dest="vt", default=False,
help="use VampirTrace for MPI profiling")
parser.add_option("--no-numpy",
action="store_false", dest="numpy", default=True,
help="disable testing with NumPy arrays")
parser.add_option("--no-array",
action="store_false", dest="array", default=True,
help="disable testing with builtin array module")
parser.add_option("--no-skip-mpi",
action="store_false", dest="skip_mpi", default=True,
help="disable known failures with backend MPI")
return parser
def getbuilddir():
from distutils.util import get_platform
s = os.path.join("build", "lib.%s-%.3s" % (get_platform(), sys.version))
if hasattr(sys, 'gettotalrefcount'): s += '-pydebug'
return s
def setup_python(options):
rootdir = os.path.dirname(os.path.dirname(__file__))
builddir = os.path.join(rootdir, getbuilddir())
if options.builddir and os.path.exists(builddir):
sys.path.insert(0, builddir)
if options.path:
path = options.path[:]
path.reverse()
for p in path:
sys.path.insert(0, p)
def setup_unittest(options):
from unittest import TestSuite
try:
from unittest.runner import _WritelnDecorator
except ImportError:
from unittest import _WritelnDecorator
#
writeln_orig = _WritelnDecorator.writeln
def writeln(self, message=''):
try: self.stream.flush()
except: pass
writeln_orig(self, message)
try: self.stream.flush()
except: pass
_WritelnDecorator.writeln = writeln
def import_package(options, pkgname):
#
if not options.numpy:
sys.modules['numpy'] = None
if not options.array:
sys.modules['array'] = None
#
package = __import__(pkgname)
#
import mpi4py.rc
if options.threads is not None:
mpi4py.rc.threads = options.threads
if options.thread_level is not None:
mpi4py.rc.thread_level = options.thread_level
if options.mpe:
mpi4py.profile('mpe', logfile='runtests-mpi4py')
if options.vt:
mpi4py.profile('vt', logfile='runtests-mpi4py')
import mpi4py.MPI
#
return package
def getprocessorinfo():
from mpi4py import MPI
rank = MPI.COMM_WORLD.Get_rank()
name = MPI.Get_processor_name()
return (rank, name)
def getlibraryinfo():
from mpi4py import MPI
info = "MPI %d.%d" % MPI.Get_version()
name, version = MPI.get_vendor()
if name != "unknown":
info += (" (%s %s)" % (name, '%d.%d.%d' % version))
return info
def getpythoninfo():
x, y = sys.version_info[:2]
return ("Python %d.%d (%s)" % (x, y, sys.executable))
def getpackageinfo(pkg):
return ("%s %s (%s)" % (pkg.__name__,
pkg.__version__,
pkg.__path__[0]))
def writeln(message='', endl='\n'):
sys.stderr.flush()
sys.stderr.write(message+endl)
sys.stderr.flush()
def print_banner(options, package):
r, n = getprocessorinfo()
fmt = "[%d@%s] %s"
if options.verbose:
writeln(fmt % (r, n, getpythoninfo()))
writeln(fmt % (r, n, getlibraryinfo()))
writeln(fmt % (r, n, getpackageinfo(package)))
def load_tests(options, args):
# Find tests
import re, glob
testsuitedir = os.path.dirname(__file__)
sys.path.insert(0, testsuitedir)
pattern = 'test_*.py'
wildcard = os.path.join(testsuitedir, pattern)
testfiles = glob.glob(wildcard)
include = exclude = None
if options.include:
include = re.compile('|'.join(options.include)).search
if options.exclude:
exclude = re.compile('|'.join(options.exclude)).search
testnames = []
for testfile in testfiles:
filename = os.path.basename(testfile)
testname = os.path.splitext(filename)[0]
if ((exclude and exclude(testname)) or
(include and not include(testname))):
continue
testnames.append(testname)
testnames.sort()
# Handle options
if not options.numpy:
sys.modules['numpy'] = None
if not options.array:
sys.modules['array'] = None
if not options.skip_mpi:
import mpiunittest
mpiunittest.skipMPI = lambda p, *c: lambda f: f
# Load tests and populate suite
testloader = unittest.TestLoader()
testsuite = unittest.TestSuite()
for testname in testnames:
module = __import__(testname)
for arg in args:
try:
cases = testloader.loadTestsFromNames((arg,), module)
except AttributeError:
continue
testsuite.addTests(cases)
if not args:
cases = testloader.loadTestsFromModule(module)
testsuite.addTests(cases)
return testsuite
def run_tests(options, testsuite, runner=None):
if runner is None:
runner = unittest.TextTestRunner()
runner.verbosity = options.verbose
runner.failfast = options.failfast
if options.catchbreak:
unittest.installHandler()
result = runner.run(testsuite)
return result.wasSuccessful()
def test_refleaks(options, args):
from sys import gettotalrefcount
from gc import collect
testsuite = load_tests(options, args)
testsuite._cleanup = False
for case in testsuite:
case._cleanup = False
class EmptyIO(object):
def write(self, *args):
pass
runner = unittest.TextTestRunner(stream=EmptyIO(), verbosity=0)
rank, name = getprocessorinfo()
r1 = r2 = 0
repeats = options.repeats
while repeats:
collect()
r1 = gettotalrefcount()
run_tests(options, testsuite, runner)
collect()
r2 = gettotalrefcount()
leaks = r2-r1
if leaks and repeats < options.repeats:
writeln('[%d@%s] refleaks: (%d - %d) --> %d'
% (rank, name, r2, r1, leaks))
repeats -= 1
def abort(code=1):
from mpi4py import MPI
MPI.COMM_WORLD.Abort(code)
def shutdown(success):
from mpi4py import MPI
def main(args=None):
pkgname = 'mpi4py'
parser = getoptionparser()
(options, args) = parser.parse_args(args)
setup_python(options)
setup_unittest(options)
package = import_package(options, pkgname)
print_banner(options, package)
testsuite = load_tests(options, args)
success = run_tests(options, testsuite)
if not success and options.failfast: abort()
if success and hasattr(sys, 'gettotalrefcount'):
test_refleaks(options, args)
shutdown(success)
return not success
if __name__ == '__main__':
import sys
sys.dont_write_bytecode = True
sys.exit(main())
mpi4py-3.0.3/test/test_threads.py 0000644 0001750 0001750 00000004075 13200562156 020025 0 ustar dalcinl dalcinl 0000000 0000000 import sys
try:
import threading
HAVE_THREADING = True
except ImportError:
import dummy_threading as threading
HAVE_THREADING = False
VERBOSE = False
#VERBOSE = True
import mpi4py.rc
mpi4py.rc.thread_level = 'multiple'
from mpi4py import MPI
import mpiunittest as unittest
pypy3_lt_50 = (hasattr(sys, 'pypy_version_info') and
sys.version_info[0] == 3 and
sys.pypy_version_info < (5, 0))
class TestMPIThreads(unittest.TestCase):
def testThreadLevels(self):
levels = [MPI.THREAD_SINGLE,
MPI.THREAD_FUNNELED,
MPI.THREAD_SERIALIZED,
MPI.THREAD_MULTIPLE]
for i in range(len(levels)-1):
self.assertTrue(levels[i] < levels[i+1])
try:
provided = MPI.Query_thread()
self.assertTrue(provided in levels)
except NotImplementedError:
self.skipTest('mpi-query_thread')
def testIsThreadMain(self):
try:
flag = MPI.Is_thread_main()
except NotImplementedError:
self.skipTest('mpi-is_thread_main')
name = threading.current_thread().name
main = (name == 'MainThread') or not HAVE_THREADING
self.assertEqual(flag, main)
if VERBOSE:
log = lambda m: sys.stderr.write(m+'\n')
log("%s: MPI.Is_thread_main() -> %s" % (name, flag))
@unittest.skipIf(pypy3_lt_50, 'pypy3(<5.0)')
def testIsThreadMainInThread(self):
try:
provided = MPI.Query_thread()
except NotImplementedError:
self.skipTest('mpi-query_thread')
self.testIsThreadMain()
T = [threading.Thread(target=self.testIsThreadMain) for _ in range(5)]
if provided == MPI.THREAD_MULTIPLE:
for t in T:
t.start()
for t in T:
t.join()
elif provided == MPI.THREAD_SERIALIZED:
for t in T:
t.start()
t.join()
else:
self.skipTest('mpi-thread_level')
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_subclass.py 0000664 0001750 0001750 00000017531 13426006675 020226 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
import sys
# ---
class MyBaseComm(object):
def free(self):
if self != MPI.COMM_NULL:
MPI.Comm.Free(self)
class BaseTestBaseComm(object):
def setUp(self):
self.comm = self.CommType(self.COMM_BASE)
def testSubType(self):
self.assertTrue(type(self.comm) not in [
MPI.Comm,
MPI.Intracomm,
MPI.Cartcomm,
MPI.Graphcomm,
MPI.Distgraphcomm,
MPI.Intercomm])
self.assertTrue(isinstance(self.comm, self.CommType))
def testCloneFree(self):
if self.COMM_BASE != MPI.COMM_NULL:
comm = self.comm.Clone()
else:
comm = self.CommType()
self.assertTrue(isinstance(comm, MPI.Comm))
self.assertTrue(isinstance(comm, self.CommType))
comm.free()
def tearDown(self):
self.comm.free()
# ---
class MyComm(MPI.Comm, MyBaseComm):
def __new__(cls, comm=None):
if comm is not None:
if comm != MPI.COMM_NULL:
comm = comm.Clone()
return super(MyComm, cls).__new__(cls, comm)
class BaseTestMyComm(BaseTestBaseComm):
CommType = MyComm
class TestMyCommNULL(BaseTestMyComm, unittest.TestCase):
COMM_BASE = MPI.COMM_NULL
class TestMyCommSELF(BaseTestMyComm, unittest.TestCase):
COMM_BASE = MPI.COMM_SELF
class TestMyCommWORLD(BaseTestMyComm, unittest.TestCase):
COMM_BASE = MPI.COMM_WORLD
# ---
class MyIntracomm(MPI.Intracomm, MyBaseComm):
def __new__(cls, comm=None):
if comm is not None:
if comm != MPI.COMM_NULL:
comm = comm.Dup()
return super(MyIntracomm, cls).__new__(cls, comm)
class BaseTestMyIntracomm(BaseTestBaseComm):
CommType = MyIntracomm
class TestMyIntracommNULL(BaseTestMyIntracomm, unittest.TestCase):
COMM_BASE = MPI.COMM_NULL
class TestMyIntracommSELF(BaseTestMyIntracomm, unittest.TestCase):
COMM_BASE = MPI.COMM_SELF
class TestMyIntracommWORLD(BaseTestMyIntracomm, unittest.TestCase):
COMM_BASE = MPI.COMM_WORLD
# ---
class MyCartcomm(MPI.Cartcomm, MyBaseComm):
def __new__(cls, comm=None):
if comm is not None:
if comm != MPI.COMM_NULL:
dims = [comm.size]
comm = comm.Create_cart(dims)
return super(MyCartcomm, cls).__new__(cls, comm)
class BaseTestMyCartcomm(BaseTestBaseComm):
CommType = MyCartcomm
class TestMyCartcommNULL(BaseTestMyCartcomm, unittest.TestCase):
COMM_BASE = MPI.COMM_NULL
class TestMyCartcommSELF(BaseTestMyCartcomm, unittest.TestCase):
COMM_BASE = MPI.COMM_SELF
class TestMyCartcommWORLD(BaseTestMyCartcomm, unittest.TestCase):
COMM_BASE = MPI.COMM_WORLD
# ---
class MyGraphcomm(MPI.Graphcomm, MyBaseComm):
def __new__(cls, comm=None):
if comm is not None:
if comm != MPI.COMM_NULL:
index = list(range(0, comm.size+1))
edges = list(range(0, comm.size))
comm = comm.Create_graph(index, edges)
return super(MyGraphcomm, cls).__new__(cls, comm)
class BaseTestMyGraphcomm(BaseTestBaseComm):
CommType = MyGraphcomm
class TestMyGraphcommNULL(BaseTestMyGraphcomm, unittest.TestCase):
COMM_BASE = MPI.COMM_NULL
class TestMyGraphcommSELF(BaseTestMyGraphcomm, unittest.TestCase):
COMM_BASE = MPI.COMM_SELF
class TestMyGraphcommWORLD(BaseTestMyGraphcomm, unittest.TestCase):
COMM_BASE = MPI.COMM_WORLD
# ---
class MyRequest(MPI.Request):
def __new__(cls, request=None):
return super(MyRequest, cls).__new__(cls, request)
def test(self):
return super(type(self), self).Test()
def wait(self):
return super(type(self), self).Wait()
class MyPrequest(MPI.Prequest):
def __new__(cls, request=None):
return super(MyPrequest, cls).__new__(cls, request)
def test(self):
return super(type(self), self).Test()
def wait(self):
return super(type(self), self).Wait()
def start(self):
return super(type(self), self).Start()
class MyGrequest(MPI.Grequest):
def __new__(cls, request=None):
return super(MyGrequest, cls).__new__(cls, request)
def test(self):
return super(type(self), self).Test()
def wait(self):
return super(type(self), self).Wait()
class BaseTestMyRequest(object):
def setUp(self):
self.req = self.MyRequestType(MPI.REQUEST_NULL)
def testSubType(self):
self.assertTrue(type(self.req) is not self.MPIRequestType)
self.assertTrue(isinstance(self.req, self.MPIRequestType))
self.assertTrue(isinstance(self.req, self.MyRequestType))
self.req.test()
class TestMyRequest(BaseTestMyRequest, unittest.TestCase):
MPIRequestType = MPI.Request
MyRequestType = MyRequest
class TestMyPrequest(BaseTestMyRequest, unittest.TestCase):
MPIRequestType = MPI.Prequest
MyRequestType = MyPrequest
class TestMyGrequest(BaseTestMyRequest, unittest.TestCase):
MPIRequestType = MPI.Grequest
MyRequestType = MyGrequest
class TestMyRequest2(TestMyRequest):
def setUp(self):
req = MPI.COMM_SELF.Isend(
[MPI.BOTTOM, 0, MPI.BYTE],
dest=MPI.PROC_NULL, tag=0)
self.req = MyRequest(req)
class TestMyPrequest2(TestMyPrequest):
def setUp(self):
req = MPI.COMM_SELF.Send_init(
[MPI.BOTTOM, 0, MPI.BYTE],
dest=MPI.PROC_NULL, tag=0)
self.req = MyPrequest(req)
def tearDown(self):
self.req.Free()
def testStart(self):
for i in range(5):
self.req.start()
self.req.test()
self.req.start()
self.req.wait()
# ---
class MyWin(MPI.Win):
def __new__(cls, win=None):
return MPI.Win.__new__(cls, win)
def free(self):
if self != MPI.WIN_NULL:
MPI.Win.Free(self)
class BaseTestMyWin(object):
def setUp(self):
w = MPI.Win.Create(MPI.BOTTOM)
self.win = MyWin(w)
def tearDown(self):
self.win.free()
def testSubType(self):
self.assertTrue(type(self.win) is not MPI.Win)
self.assertTrue(isinstance(self.win, MPI.Win))
self.assertTrue(isinstance(self.win, MyWin))
def testFree(self):
self.assertTrue(self.win)
self.win.free()
self.assertFalse(self.win)
class TestMyWin(BaseTestMyWin, unittest.TestCase):
pass
SpectrumMPI = MPI.get_vendor()[0] == 'Spectrum MPI'
try:
if SpectrumMPI: raise NotImplementedError
MPI.Win.Create(MPI.BOTTOM).Free()
except (NotImplementedError, MPI.Exception):
unittest.disable(BaseTestMyWin, 'mpi-win')
# ---
import os, tempfile
class MyFile(MPI.File):
def __new__(cls, file=None):
return MPI.File.__new__(cls, file)
def close(self):
if self != MPI.FILE_NULL:
MPI.File.Close(self)
class BaseTestMyFile(object):
def openfile(self):
fd, fname = tempfile.mkstemp(prefix='mpi4py')
os.close(fd)
amode = MPI.MODE_RDWR | MPI.MODE_CREATE | MPI.MODE_DELETE_ON_CLOSE
try:
self.file = MPI.File.Open(MPI.COMM_SELF, fname, amode, MPI.INFO_NULL)
return self.file
except Exception:
os.remove(fname)
raise
def setUp(self):
f = self.openfile()
self.file = MyFile(f)
def tearDown(self):
self.file.close()
def testSubType(self):
self.assertTrue(type(self.file) is not MPI.File)
self.assertTrue(isinstance(self.file, MPI.File))
self.assertTrue(isinstance(self.file, MyFile))
def testFree(self):
self.assertTrue(self.file)
self.file.close()
self.assertFalse(self.file)
class TestMyFile(BaseTestMyFile, unittest.TestCase):
pass
try:
BaseTestMyFile().openfile().Close()
except NotImplementedError:
unittest.disable(BaseTestMyFile, 'mpi-file')
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_p2p_buf_matched.py 0000644 0001750 0001750 00000015563 13200562156 021421 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
import arrayimpl
@unittest.skipIf(MPI.MESSAGE_NULL == MPI.MESSAGE_NO_PROC, 'mpi-message')
class TestMessage(unittest.TestCase):
def testMessageNull(self):
null = MPI.MESSAGE_NULL
self.assertFalse(null)
null2 = MPI.Message()
self.assertEqual(null, null2)
null3 = MPI.Message(null)
self.assertEqual(null, null3)
def testMessageNoProc(self):
#
noproc = MPI.MESSAGE_NO_PROC
self.assertTrue(noproc)
noproc.Recv(None)
self.assertTrue(noproc)
noproc.Irecv(None).Wait()
self.assertTrue(noproc)
#
noproc2 = MPI.Message(MPI.MESSAGE_NO_PROC)
self.assertTrue(noproc2)
self.assertEqual(noproc2, noproc)
self.assertNotEqual(noproc, MPI.MESSAGE_NULL)
#
message = MPI.Message(MPI.MESSAGE_NO_PROC)
message.Recv(None)
self.assertEqual(message, MPI.MESSAGE_NULL)
#
message = MPI.Message(MPI.MESSAGE_NO_PROC)
request = message.Irecv(None)
self.assertEqual(message, MPI.MESSAGE_NULL)
self.assertNotEqual(request, MPI.REQUEST_NULL)
request.Wait()
self.assertEqual(request, MPI.REQUEST_NULL)
@unittest.skipIf(MPI.MESSAGE_NULL == MPI.MESSAGE_NO_PROC, 'mpi-message')
class BaseTestP2PMatched(object):
COMM = MPI.COMM_NULL
def testIMProbe(self):
comm = self.COMM.Dup()
try:
m = comm.Improbe()
self.assertEqual(m, None)
m = comm.Improbe(MPI.ANY_SOURCE)
self.assertEqual(m, None)
m = comm.Improbe(MPI.ANY_SOURCE, MPI.ANY_TAG)
self.assertEqual(m, None)
status = MPI.Status()
m = comm.Improbe(MPI.ANY_SOURCE, MPI.ANY_TAG, status)
self.assertEqual(m, None)
self.assertEqual(status.source, MPI.ANY_SOURCE)
self.assertEqual(status.tag, MPI.ANY_TAG)
self.assertEqual(status.error, MPI.SUCCESS)
m = MPI.Message.Iprobe(comm)
self.assertEqual(m, None)
# Open MPI <= 1.8.4
buf = [None, 0, MPI.BYTE]
s = comm.Isend(buf, comm.rank, 0)
r = comm.Mprobe(comm.rank, 0).Irecv(buf)
MPI.Request.Waitall([s,r])
finally:
comm.Free()
def testProbeRecv(self):
comm = self.COMM
size = comm.Get_size()
rank = comm.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for s in range(0, size+1):
sbuf = array( s, typecode, s)
rbuf = array(-1, typecode, s)
if size == 1:
n = comm.Improbe(0, 0)
self.assertEqual(n, None)
sr = comm.Isend(sbuf.as_mpi(), 0, 0)
m = comm.Mprobe(0, 0)
self.assertTrue(isinstance(m, MPI.Message))
self.assertTrue(m)
rr = m.Irecv(rbuf.as_raw())
self.assertFalse(m)
self.assertTrue(sr)
self.assertTrue(rr)
MPI.Request.Waitall([sr,rr])
self.assertFalse(sr)
self.assertFalse(rr)
#
n = comm.Improbe(0, 0)
self.assertEqual(n, None)
r = comm.Isend(sbuf.as_mpi(), 0, 0)
m = MPI.Message.Probe(comm, 0, 0)
self.assertTrue(isinstance(m, MPI.Message))
self.assertTrue(m)
m.Recv(rbuf.as_raw())
self.assertFalse(m)
r.Wait()
#
n = MPI.Message.Iprobe(comm, 0, 0)
self.assertEqual(n, None)
r = comm.Isend(sbuf.as_mpi(), 0, 0)
m = MPI.Message.Iprobe(comm, 0, 0)
self.assertTrue(isinstance(m, MPI.Message))
self.assertTrue(m)
m.Recv(rbuf.as_raw())
self.assertFalse(m)
r.Wait()
#
n = MPI.Message.Iprobe(comm, 0, 0)
self.assertEqual(n, None)
r = comm.Isend(sbuf.as_mpi(), 0, 0)
m = comm.Mprobe(0, 0)
self.assertTrue(isinstance(m, MPI.Message))
self.assertTrue(m)
m.Recv(rbuf.as_raw())
self.assertFalse(m)
r.Wait()
elif rank == 0:
n = comm.Improbe(0, 0)
self.assertEqual(n, None)
#
comm.Send(sbuf.as_mpi(), 1, 0)
m = comm.Mprobe(1, 0)
self.assertTrue(m)
m.Recv(rbuf.as_raw())
self.assertFalse(m)
#
n = comm.Improbe(0, 0)
self.assertEqual(n, None)
comm.Send(sbuf.as_mpi(), 1, 1)
m = None
while not m:
m = comm.Improbe(1, 1)
m.Irecv(rbuf.as_raw()).Wait()
elif rank == 1:
n = comm.Improbe(1, 0)
self.assertEqual(n, None)
#
m = comm.Mprobe(0, 0)
self.assertTrue(m)
m.Recv(rbuf.as_raw())
self.assertFalse(m)
#
n = comm.Improbe(1, 0)
self.assertEqual(n, None)
comm.Send(sbuf.as_mpi(), 0, 0)
m = None
while not m:
m = comm.Improbe(0, 1)
m.Irecv(rbuf.as_mpi()).Wait()
comm.Send(sbuf.as_mpi(), 0, 1)
else:
rbuf = sbuf
for value in rbuf:
self.assertEqual(value, s)
class TestP2PMatchedSelf(BaseTestP2PMatched, unittest.TestCase):
COMM = MPI.COMM_SELF
class TestP2PMatchedWorld(BaseTestP2PMatched, unittest.TestCase):
COMM = MPI.COMM_WORLD
class TestP2PMatchedSelfDup(TestP2PMatchedSelf):
def setUp(self):
self.COMM = MPI.COMM_SELF.Dup()
def tearDown(self):
self.COMM.Free()
class TestP2PMatchedWorldDup(TestP2PMatchedWorld):
def setUp(self):
self.COMM = MPI.COMM_WORLD.Dup()
def tearDown(self):
self.COMM.Free()
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_cco_nb_vec.py 0000644 0001750 0001750 00000040144 13200562156 020450 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
import arrayimpl
def maxvalue(a):
try:
typecode = a.typecode
except AttributeError:
typecode = a.dtype.char
if typecode == ('f'):
return 1e30
elif typecode == ('d'):
return 1e300
else:
return 2 ** (a.itemsize * 7) - 1
@unittest.skipMPI('msmpi(<8.1.0)')
class BaseTestCCOVec(object):
COMM = MPI.COMM_NULL
skip = []
@unittest.skipMPI('openmpi(==1.10.1)')
def testGatherv(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
for count in range(size):
if (count, '*') in self.skip: continue
if (count, typecode) in self.skip: continue
sbuf = array(root, typecode, count)
rbuf = array( -1, typecode, size*size)
counts = [count] * size
displs = list(range(0, size*size, size))
recvbuf = rbuf.as_mpi_v(counts, displs)
if rank != root: recvbuf=None
self.COMM.Igatherv(sbuf.as_mpi(), recvbuf, root).Wait()
if recvbuf is not None:
for i in range(size):
row = rbuf[i*size:(i+1)*size]
a, b = row[:count], row[count:]
for va in a:
self.assertEqual(va, root)
for vb in b:
self.assertEqual(vb, -1)
@unittest.skipMPI('openmpi(==1.10.1)')
def testGatherv2(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
for count in range(size):
if (count, '*') in self.skip: continue
if (count, typecode) in self.skip: continue
sbuf = array(root, typecode, size)
rbuf = array( -1, typecode, size*size)
sendbuf = sbuf.as_mpi_c(count)
recvbuf = rbuf.as_mpi_v(count, size)
if rank != root: recvbuf=None
self.COMM.Igatherv(sendbuf, recvbuf, root).Wait()
if recvbuf is not None:
for i in range(size):
row = rbuf[i*size:(i+1)*size]
a, b = row[:count], row[count:]
for va in a:
self.assertEqual(va, root)
for vb in b:
self.assertEqual(vb, -1)
@unittest.skipMPI('openmpi(==1.10.1)')
def testGatherv3(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
for count in range(size+1):
if (count, '*') in self.skip: continue
if (count, typecode) in self.skip: continue
#
sbuf = array(root, typecode, count).as_raw()
rbuf = array( -1, typecode, count*size).as_raw()
sendbuf = sbuf
recvbuf = [rbuf, count]
if rank != root: recvbuf=None
self.COMM.Igatherv(sendbuf, recvbuf, root).Wait()
if recvbuf is not None:
for v in rbuf:
self.assertEqual(v, root)
#
sbuf = array(root, typecode, count).as_raw()
if rank == root:
rbuf = array( -1, typecode, count*size).as_raw()
else:
rbuf = None
self.COMM.Gatherv(sbuf, rbuf, root)
self.COMM.Barrier()
if rank == root:
for v in rbuf:
self.assertEqual(v, root)
@unittest.skipMPI('openmpi(==1.10.1)')
def testScatterv(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
for count in range(size):
if (count, '*') in self.skip: continue
if (count, typecode) in self.skip: continue
sbuf = array(root, typecode, size*size)
rbuf = array( -1, typecode, count)
counts = [count] * size
displs = list(range(0, size*size, size))
sendbuf = sbuf.as_mpi_v(counts, displs)
if rank != root: sendbuf = None
self.COMM.Iscatterv(sendbuf, rbuf.as_mpi(), root).Wait()
for vr in rbuf:
self.assertEqual(vr, root)
@unittest.skipMPI('openmpi(==1.10.1)')
def testScatterv2(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
for count in range(size):
if (count, '*') in self.skip: continue
if (count, typecode) in self.skip: continue
sbuf = array(root, typecode, size*size)
rbuf = array( -1, typecode, size)
sendbuf = sbuf.as_mpi_v(count, size)
recvbuf = rbuf.as_mpi_c(count)
if rank != root: sendbuf = None
self.COMM.Iscatterv(sendbuf, recvbuf, root).Wait()
a, b = rbuf[:count], rbuf[count:]
for va in a:
self.assertEqual(va, root)
for vb in b:
self.assertEqual(vb, -1)
@unittest.skipMPI('openmpi(==1.10.1)')
def testScatterv3(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
for count in range(size+1):
if (count, '*') in self.skip: continue
if (count, typecode) in self.skip: continue
#
sbuf = array(root, typecode, count*size).as_raw()
rbuf = array( -1, typecode, count).as_raw()
sendbuf = [sbuf, count]
recvbuf = rbuf
if rank != root: sendbuf = None
self.COMM.Iscatterv(sendbuf, recvbuf, root).Wait()
for v in rbuf:
self.assertEqual(v, root)
#
if rank == root:
sbuf = array(root, typecode, count*size).as_raw()
else:
sbuf = None
rbuf = array( -1, typecode, count).as_raw()
self.COMM.Scatterv(sbuf, rbuf, root)
for v in rbuf:
self.assertEqual(v, root)
@unittest.skipMPI('openmpi(==1.10.1)')
def testAllgatherv(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
for count in range(size):
if (count, '*') in self.skip: continue
if (count, typecode) in self.skip: continue
sbuf = array(root, typecode, count)
rbuf = array( -1, typecode, size*size)
counts = [count] * size
displs = list(range(0, size*size, size))
sendbuf = sbuf.as_mpi()
recvbuf = rbuf.as_mpi_v(counts, displs)
self.COMM.Iallgatherv(sendbuf, recvbuf).Wait()
for i in range(size):
row = rbuf[i*size:(i+1)*size]
a, b = row[:count], row[count:]
for va in a:
self.assertEqual(va, root)
for vb in b:
self.assertEqual(vb, -1)
@unittest.skipMPI('openmpi(==1.10.1)')
def testAllgatherv2(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
for count in range(size):
if (count, '*') in self.skip: continue
if (count, typecode) in self.skip: continue
sbuf = array(root, typecode, size)
rbuf = array( -1, typecode, size*size)
sendbuf = sbuf.as_mpi_c(count)
recvbuf = rbuf.as_mpi_v(count, size)
self.COMM.Iallgatherv(sendbuf, recvbuf).Wait()
for i in range(size):
row = rbuf[i*size:(i+1)*size]
a, b = row[:count], row[count:]
for va in a:
self.assertEqual(va, root)
for vb in b:
self.assertEqual(vb, -1)
@unittest.skipMPI('openmpi(==1.10.1)')
def testAllgatherv3(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
for count in range(size+1):
if (count, '*') in self.skip: continue
if (count, typecode) in self.skip: continue
#
sbuf = array(root, typecode, count).as_raw()
rbuf = array( -1, typecode, count*size).as_raw()
sendbuf = sbuf
recvbuf = [rbuf, count]
self.COMM.Iallgatherv(sendbuf, recvbuf).Wait()
for v in rbuf:
self.assertEqual(v, root)
#
sbuf = array(root, typecode, count).as_raw()
rbuf = array( -1, typecode, count*size).as_raw()
self.COMM.Iallgatherv(sbuf, rbuf).Wait()
for v in rbuf:
self.assertEqual(v, root)
def testAlltoallv(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
for count in range(size):
sbuf = array(root, typecode, size*size)
rbuf = array( -1, typecode, size*size)
counts = [count] * size
displs = list(range(0, size*size, size))
sendbuf = sbuf.as_mpi_v(counts, displs)
recvbuf = rbuf.as_mpi_v(counts, displs)
self.COMM.Ialltoallv(sendbuf, recvbuf).Wait()
for i in range(size):
row = rbuf[i*size:(i+1)*size]
a, b = row[:count], row[count:]
for va in a:
self.assertEqual(va, root)
for vb in b:
self.assertEqual(vb, -1)
def testAlltoallv2(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
for count in range(size):
sbuf = array(root, typecode, size*size)
rbuf = array( -1, typecode, size*size)
sendbuf = sbuf.as_mpi_v(count, size)
recvbuf = rbuf.as_mpi_v(count, size)
self.COMM.Ialltoallv(sendbuf, recvbuf).Wait()
for i in range(size):
row = rbuf[i*size:(i+1)*size]
a, b = row[:count], row[count:]
for va in a:
self.assertEqual(va, root)
for vb in b:
self.assertEqual(vb, -1)
def testAlltoallv3(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
for count in range(size+1):
#
sbuf = array(root, typecode, count*size).as_raw()
rbuf = array( -1, typecode, count*size).as_raw()
sendbuf = [sbuf, count]
recvbuf = [rbuf, count]
self.COMM.Ialltoallv(sendbuf, recvbuf).Wait()
for v in rbuf:
self.assertEqual(v, root)
#
sbuf = array(root, typecode, count*size).as_raw()
rbuf = array( -1, typecode, count*size).as_raw()
self.COMM.Ialltoallv(sbuf, rbuf).Wait()
for v in rbuf:
self.assertEqual(v, root)
@unittest.skipMPI('openmpi(<=1.8.0)')
def testAlltoallw(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for n in range(1, size+1):
sbuf = array( n, typecode, (size, n))
rbuf = array(-1, typecode, (size, n))
sdt, rdt = sbuf.mpidtype, rbuf.mpidtype
sdsp = list(range(0, size*n*sdt.extent, n*sdt.extent))
rdsp = list(range(0, size*n*rdt.extent, n*rdt.extent))
smsg = (sbuf.as_raw(), ([n]*size, sdsp), [sdt]*size)
rmsg = (rbuf.as_raw(), ([n]*size, rdsp), [rdt]*size)
try:
self.COMM.Ialltoallw(smsg, rmsg).Wait()
except NotImplementedError:
self.skipTest('mpi-ialltoallw')
for v in rbuf.flat:
self.assertEqual(v, n)
class TestCCOVecSelf(BaseTestCCOVec, unittest.TestCase):
COMM = MPI.COMM_SELF
class TestCCOVecWorld(BaseTestCCOVec, unittest.TestCase):
COMM = MPI.COMM_WORLD
class TestCCOVecSelfDup(TestCCOVecSelf):
def setUp(self):
self.COMM = MPI.COMM_SELF.Dup()
def tearDown(self):
self.COMM.Free()
class TestCCOVecWorldDup(TestCCOVecWorld):
def setUp(self):
self.COMM = MPI.COMM_WORLD.Dup()
def tearDown(self):
self.COMM.Free()
name, version = MPI.get_vendor()
if name == 'Open MPI':
if version == (1,10,0):
BaseTestCCOVec.skip += [(0, '*')]
if version == (1,8,6):
BaseTestCCOVec.skip += [(0, 'b')]
try:
MPI.COMM_SELF.Ibarrier().Wait()
except NotImplementedError:
unittest.disable(BaseTestCCOVec, 'mpi-nbc')
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_msgzero.py 0000644 0001750 0001750 00000003423 13200562156 020055 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
class BaseTestMessageZero(object):
null_b = [None, MPI.INT]
null_v = [None, (0, None), MPI.INT]
def testPointToPoint(self):
comm = self.COMM
comm.Sendrecv(sendbuf=self.null_b, dest=comm.rank,
recvbuf=self.null_b, source=comm.rank)
r2 = comm.Irecv(self.null_b, comm.rank)
r1 = comm.Isend(self.null_b, comm.rank)
MPI.Request.Waitall([r1, r2])
def testCollectivesBlock(self):
comm = self.COMM
comm.Bcast(self.null_b)
comm.Gather(self.null_b, self.null_b)
comm.Scatter(self.null_b, self.null_b)
comm.Allgather(self.null_b, self.null_b)
comm.Alltoall(self.null_b, self.null_b)
def testCollectivesVector(self):
comm = self.COMM
comm.Gatherv(self.null_b, self.null_v)
comm.Scatterv(self.null_v, self.null_b)
comm.Allgatherv(self.null_b, self.null_v)
comm.Alltoallv(self.null_v, self.null_v)
@unittest.skipMPI('openmpi')
@unittest.skipMPI('SpectrumMPI')
def testReductions(self):
comm = self.COMM
comm.Reduce(self.null_b, self.null_b)
comm.Allreduce(self.null_b, self.null_b)
comm.Reduce_scatter_block(self.null_b, self.null_b)
rcnt = [0]*comm.Get_size()
comm.Reduce_scatter(self.null_b, self.null_b, rcnt)
try: comm.Scan(self.null_b, self.null_b)
except NotImplementedError: pass
try: comm.Exscan(self.null_b, self.null_b)
except NotImplementedError: pass
class TestMessageZeroSelf(BaseTestMessageZero, unittest.TestCase):
COMM = MPI.COMM_SELF
class TestMessageZeroWorld(BaseTestMessageZero, unittest.TestCase):
COMM = MPI.COMM_WORLD
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_comm.py 0000644 0001750 0001750 00000014720 13200562156 017324 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
class TestCommNull(unittest.TestCase):
def testContructor(self):
comm = MPI.Comm()
self.assertEqual(comm, MPI.COMM_NULL)
self.assertFalse(comm is MPI.COMM_NULL)
def construct(): MPI.Comm((1,2,3))
self.assertRaises(TypeError, construct)
def testContructorIntra(self):
comm_null = MPI.Intracomm()
self.assertFalse(comm_null is MPI.COMM_NULL)
self.assertEqual(comm_null, MPI.COMM_NULL)
def testContructorInter(self):
comm_null = MPI.Intercomm()
self.assertFalse(comm_null is MPI.COMM_NULL)
self.assertEqual(comm_null, MPI.COMM_NULL)
class BaseTestComm(object):
def testContructor(self):
comm = MPI.Comm(self.COMM)
self.assertEqual(comm, self.COMM)
self.assertFalse(comm is self.COMM)
def testPyProps(self):
comm = self.COMM
self.assertEqual(comm.Get_size(), comm.size)
self.assertEqual(comm.Get_rank(), comm.rank)
self.assertEqual(comm.Is_intra(), comm.is_intra)
self.assertEqual(comm.Is_inter(), comm.is_inter)
self.assertEqual(comm.Get_topology(), comm.topology)
def testGroup(self):
comm = self.COMM
group = self.COMM.Get_group()
self.assertEqual(comm.Get_size(), group.Get_size())
self.assertEqual(comm.Get_rank(), group.Get_rank())
group.Free()
self.assertEqual(group, MPI.GROUP_NULL)
def testCloneFree(self):
comm = self.COMM.Clone()
comm.Free()
self.assertEqual(comm, MPI.COMM_NULL)
def testCompare(self):
results = (MPI.IDENT, MPI.CONGRUENT, MPI.SIMILAR, MPI.UNEQUAL)
ccmp = MPI.Comm.Compare(self.COMM, MPI.COMM_WORLD)
self.assertTrue(ccmp in results)
ccmp = MPI.Comm.Compare(self.COMM, self.COMM)
self.assertEqual(ccmp, MPI.IDENT)
comm = self.COMM.Dup()
ccmp = MPI.Comm.Compare(self.COMM, comm)
comm.Free()
self.assertEqual(ccmp, MPI.CONGRUENT)
def testIsInter(self):
is_inter = self.COMM.Is_inter()
self.assertTrue(type(is_inter) is bool)
def testGetSetName(self):
try:
name = self.COMM.Get_name()
self.COMM.Set_name('comm')
self.assertEqual(self.COMM.Get_name(), 'comm')
self.COMM.Set_name(name)
self.assertEqual(self.COMM.Get_name(), name)
except NotImplementedError:
self.skipTest('mpi-comm-name')
def testGetParent(self):
try:
parent = MPI.Comm.Get_parent()
except NotImplementedError:
self.skipTest('mpi-comm-get_parent')
def testDupWithInfo(self):
info = None
self.COMM.Dup(info).Free()
info = MPI.INFO_NULL
self.COMM.Dup(info).Free()
self.COMM.Dup_with_info(info).Free()
info = MPI.Info.Create()
self.COMM.Dup(info).Free()
self.COMM.Dup_with_info(info).Free()
info.Free()
@unittest.skipMPI('mpich(<=3.1.0)', MPI.Query_thread() > MPI.THREAD_SINGLE)
def testIDup(self):
try:
comm, request = self.COMM.Idup()
except NotImplementedError:
self.skipTest('mpi-comm-idup')
request.Wait()
ccmp = MPI.Comm.Compare(self.COMM, comm)
comm.Free()
self.assertEqual(ccmp, MPI.CONGRUENT)
def testGetSetInfo(self):
#info = MPI.INFO_NULL
#self.COMM.Set_info(info)
info = MPI.Info.Create()
self.COMM.Set_info(info)
info.Free()
info = self.COMM.Get_info()
self.COMM.Set_info(info)
info.Free()
def testCreate(self):
group = self.COMM.Get_group()
comm = self.COMM.Create(group)
ccmp = MPI.Comm.Compare(self.COMM, comm)
self.assertEqual(ccmp, MPI.CONGRUENT)
comm.Free()
group.Free()
@unittest.skipMPI('openmpi(<=1.8.1)')
def testCreateGroup(self):
group = self.COMM.Get_group()
try:
try:
comm = self.COMM.Create_group(group)
ccmp = MPI.Comm.Compare(self.COMM, comm)
self.assertEqual(ccmp, MPI.CONGRUENT)
comm.Free()
finally:
group.Free()
except NotImplementedError:
self.skipTest('mpi-comm-create_group')
@unittest.skipMPI('openmpi(==2.0.0)')
def testSplitType(self):
try:
MPI.COMM_SELF.Split_type(MPI.COMM_TYPE_SHARED).Free()
except NotImplementedError:
self.skipTest('mpi-comm-split_type')
#comm = self.COMM.Split_type(MPI.UNDEFINED)
#self.assertEqual(comm, MPI.COMM_NULL)
comm = self.COMM.Split_type(MPI.COMM_TYPE_SHARED)
self.assertNotEqual(comm, MPI.COMM_NULL)
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
if size == 1:
self.assertEqual(comm.size, 1)
self.assertEqual(comm.rank, 0)
comm.Free()
for root in range(size):
if rank == root:
split_type = MPI.COMM_TYPE_SHARED
else:
split_type = MPI.UNDEFINED
comm = self.COMM.Split_type(split_type)
if rank == root:
self.assertNotEqual(comm, MPI.COMM_NULL)
self.assertEqual(comm.size, 1)
self.assertEqual(comm.rank, 0)
comm.Free()
else:
self.assertEqual(comm, MPI.COMM_NULL)
class TestCommSelf(BaseTestComm, unittest.TestCase):
def setUp(self):
self.COMM = MPI.COMM_SELF
def testSize(self):
size = self.COMM.Get_size()
self.assertEqual(size, 1)
def testRank(self):
rank = self.COMM.Get_rank()
self.assertEqual(rank, 0)
class TestCommWorld(BaseTestComm, unittest.TestCase):
def setUp(self):
self.COMM = MPI.COMM_WORLD
def testSize(self):
size = self.COMM.Get_size()
self.assertTrue(size >= 1)
def testRank(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
self.assertTrue(rank >= 0 and rank < size)
class TestCommSelfDup(TestCommSelf):
def setUp(self):
self.COMM = MPI.COMM_SELF.Dup()
def tearDown(self):
self.COMM.Free()
@unittest.skipMPI('openmpi(<1.4.0)', MPI.Query_thread() > MPI.THREAD_SINGLE)
class TestCommWorldDup(TestCommWorld):
def setUp(self):
self.COMM = MPI.COMM_WORLD.Dup()
def tearDown(self):
self.COMM.Free()
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_cco_obj.py 0000644 0001750 0001750 00000017130 13200562156 017765 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
from functools import reduce
cumsum = lambda seq: reduce(lambda x, y: x+y, seq, 0)
cumprod = lambda seq: reduce(lambda x, y: x*y, seq, 1)
_basic = [None,
True, False,
-7, 0, 7, 2**31,
-2**63+1, 2**63-1,
-2.17, 0.0, 3.14,
1+2j, 2-3j,
'mpi4py',
]
messages = _basic
messages += [ list(_basic),
tuple(_basic),
dict([('k%d' % key, val)
for key, val in enumerate(_basic)])
]
class BaseTestCCOObj(object):
COMM = MPI.COMM_NULL
def testBarrier(self):
self.COMM.barrier()
def testBcast(self):
for smess in messages:
for root in range(self.COMM.Get_size()):
rmess = self.COMM.bcast(smess, root=root)
self.assertEqual(smess, rmess)
def testGather(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for smess in messages + [messages]:
for root in range(size):
rmess = self.COMM.gather(smess, root=root)
if rank == root:
self.assertEqual(rmess, [smess] * size)
else:
self.assertEqual(rmess, None)
def testScatter(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for smess in messages + [messages]:
for root in range(size):
if rank == root:
rmess = self.COMM.scatter([smess] * size, root=root)
else:
rmess = self.COMM.scatter(None, root=root)
self.assertEqual(rmess, smess)
def testAllgather(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for smess in messages + [messages]:
rmess = self.COMM.allgather(smess)
self.assertEqual(rmess, [smess] * size)
def testAlltoall(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for smess in messages + [messages]:
rmess = self.COMM.alltoall([smess] * size)
self.assertEqual(rmess, [smess] * size)
def testReduce(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for root in range(size):
for op in (MPI.SUM, MPI.PROD,
MPI.MAX, MPI.MIN,
MPI.MAXLOC, MPI.MINLOC,
MPI.REPLACE, MPI.NO_OP):
if op == MPI.OP_NULL: continue
if op in (MPI.MAXLOC, MPI.MINLOC):
sendobj = (rank, rank)
else:
sendobj = rank
value = self.COMM.reduce(sendobj, op=op, root=root)
if rank != root:
self.assertTrue(value is None)
else:
if op == MPI.SUM:
self.assertEqual(value, cumsum(range(size)))
elif op == MPI.PROD:
self.assertEqual(value, cumprod(range(size)))
elif op == MPI.MAX:
self.assertEqual(value, size-1)
elif op == MPI.MIN:
self.assertEqual(value, 0)
elif op == MPI.MAXLOC:
self.assertEqual(value[0], size-1)
self.assertEqual(value[1], size-1)
elif op == MPI.MINLOC:
self.assertEqual(value[0], 0)
self.assertEqual(value[1], 0)
elif op == MPI.REPLACE:
self.assertEqual(value, size-1)
elif op == MPI.NO_OP:
self.assertEqual(value, 0)
def testAllreduce(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for op in (MPI.SUM, MPI.PROD,
MPI.MAX, MPI.MIN,
MPI.MAXLOC, MPI.MINLOC,
MPI.REPLACE, MPI.NO_OP):
if op == MPI.OP_NULL: continue
if op in (MPI.MAXLOC, MPI.MINLOC):
sendobj = (rank, rank)
else:
sendobj = rank
value = self.COMM.allreduce(sendobj, op)
if op == MPI.SUM:
self.assertEqual(value, cumsum(range(size)))
elif op == MPI.PROD:
self.assertEqual(value, cumprod(range(size)))
elif op == MPI.MAX:
self.assertEqual(value, size-1)
elif op == MPI.MIN:
self.assertEqual(value, 0)
elif op == MPI.MAXLOC:
self.assertEqual(value[1], size-1)
elif op == MPI.MINLOC:
self.assertEqual(value[1], 0)
elif op == MPI.REPLACE:
self.assertEqual(value, size-1)
elif op == MPI.NO_OP:
self.assertEqual(value, 0)
def testScan(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
# --
sscan = self.COMM.scan(size, op=MPI.SUM)
self.assertEqual(sscan, cumsum([size]*(rank+1)))
# --
rscan = self.COMM.scan(rank, op=MPI.SUM)
self.assertEqual(rscan, cumsum(range(rank+1)))
# --
minloc = self.COMM.scan((rank, rank), op=MPI.MINLOC)
maxloc = self.COMM.scan((rank, rank), op=MPI.MAXLOC)
self.assertEqual(minloc, (0, 0))
self.assertEqual(maxloc, (rank, rank))
# --
if MPI.REPLACE != MPI.OP_NULL:
rscan = self.COMM.scan(rank, op=MPI.REPLACE)
self.assertEqual(rscan, rank)
# --
if MPI.NO_OP != MPI.OP_NULL:
rscan = self.COMM.scan(rank, op=MPI.NO_OP)
self.assertEqual(rscan, 0)
def testExscan(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
# --
sscan = self.COMM.exscan(size, op=MPI.SUM)
if rank == 0:
self.assertTrue(sscan is None)
else:
self.assertEqual(sscan, cumsum([size]*(rank)))
# --
rscan = self.COMM.exscan(rank, op=MPI.SUM)
if rank == 0:
self.assertTrue(rscan is None)
else:
self.assertEqual(rscan, cumsum(range(rank)))
# --
minloc = self.COMM.exscan((rank, rank), op=MPI.MINLOC)
maxloc = self.COMM.exscan((rank, rank), op=MPI.MAXLOC)
if rank == 0:
self.assertEqual(minloc, None)
self.assertEqual(maxloc, None)
else:
self.assertEqual(minloc, (0, 0))
self.assertEqual(maxloc, (rank-1, rank-1))
# --
if MPI.REPLACE != MPI.OP_NULL:
rscan = self.COMM.exscan(rank, op=MPI.REPLACE)
if rank == 0:
self.assertTrue(rscan is None)
else:
self.assertEqual(rscan, rank-1)
# --
if MPI.NO_OP != MPI.OP_NULL:
rscan = self.COMM.exscan(rank, op=MPI.NO_OP)
if rank == 0:
self.assertTrue(rscan is None)
else:
self.assertEqual(rscan, 0)
class TestCCOObjSelf(BaseTestCCOObj, unittest.TestCase):
COMM = MPI.COMM_SELF
class TestCCOObjWorld(BaseTestCCOObj, unittest.TestCase):
COMM = MPI.COMM_WORLD
class TestCCOObjSelfDup(TestCCOObjSelf):
def setUp(self):
self.COMM = MPI.COMM_SELF.Dup()
def tearDown(self):
self.COMM.Free()
@unittest.skipMPI('openmpi(<1.4.0)', MPI.Query_thread() > MPI.THREAD_SINGLE)
class TestCCOObjWorldDup(TestCCOObjWorld):
def setUp(self):
self.COMM = MPI.COMM_WORLD.Dup()
def tearDown(self):
self.COMM.Free()
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/arrayimpl.py 0000644 0001750 0001750 00000007500 13200562156 017330 0 ustar dalcinl dalcinl 0000000 0000000 import sys
from mpi4py import MPI
try:
from collections import OrderedDict
except ImportError:
OrderedDict = dict
try:
import array
except ImportError:
array = None
try:
import numpy
except ImportError:
numpy = None
__all__ = ['TypeMap', 'ArrayTypes', 'allclose']
TypeMap = OrderedDict([
('b', MPI.SIGNED_CHAR),
('h', MPI.SHORT),
('i', MPI.INT),
('l', MPI.LONG),
('q', MPI.LONG_LONG),
('f', MPI.FLOAT),
('d', MPI.DOUBLE),
])
if MPI.SIGNED_CHAR == MPI.DATATYPE_NULL:
del TypeMap['b']
if sys.version_info[:2] < (3, 3):
del TypeMap['q']
ArrayTypes = []
def allclose(a, b, rtol=1.e-5, atol=1.e-8):
try: iter(a)
except TypeError: a = [a]
try: iter(b)
except TypeError: b = [b]
for x, y in zip(a, b):
if abs(x-y) > (atol + rtol * abs(y)):
return False
return True
if array is not None:
def product(seq):
res = 1
for s in seq:
res = res * s
return res
def mkshape(seq):
return tuple([int(s) for s in seq])
class Array(array.array):
TypeMap = TypeMap.copy()
def __new__(cls, arg, typecode, shape=None):
if isinstance(arg, (int, float)):
if shape is None:
shape = ()
else:
try:
shape = mkshape(shape)
except TypeError:
shape = (int(shape),)
size = product(shape)
arg = [arg] * size
else:
size = len(arg)
if shape is None: shape = (size,)
else: shape = mkshape(shape)
assert size == product(shape)
ary = array.array.__new__(cls, typecode, arg)
ary.shape = shape
ary.size = size
try:
ary.mpidtype = Array.TypeMap[typecode]
except KeyError:
ary.mpidtype = MPI.DATATYPE_NULL
return ary
def flat(self): return self
flat = property(flat)
def as_raw(self):
return self
def as_mpi(self):
return (self, self.mpidtype)
def as_mpi_c(self, count):
return (self, count, self.mpidtype)
def as_mpi_v(self, cnt, dsp):
return (self, (cnt, dsp), self.mpidtype)
ArrayTypes.append(Array)
__all__.append('Array')
if numpy is not None:
class NumPy(object):
TypeMap = TypeMap.copy()
def __init__(self, arg, typecode, shape=None):
if isinstance(arg, (int, float, complex)):
if shape is None: shape = ()
else:
if shape is None: shape = len(arg)
self.array = ary = numpy.zeros(shape, typecode)
if isinstance(arg, (int, float, complex)):
ary.fill(arg)
else:
ary[:] = arg
try:
self.mpidtype = NumPy.TypeMap[typecode]
except KeyError:
self.mpidtype = MPI.DATATYPE_NULL
def __len__(self): return len(self.array)
def __getitem__(self, i): return self.array[i]
def __setitem__(self, i, v): self.array[i] = v
def typecode(self): return self.array.dtype.char
typecode = property(typecode)
def itemsize(self): return self.array.itemsize
itemsize = property(itemsize)
def flat(self): return self.array.flat
flat = property(flat)
def as_raw(self):
return self.array
def as_mpi(self):
return (self.array, self.mpidtype)
def as_mpi_c(self, count):
return (self.array, count, self.mpidtype)
def as_mpi_v(self, cnt, dsp):
return (self.array, (cnt, dsp), self.mpidtype)
ArrayTypes.append(NumPy)
__all__.append('NumPy')
mpi4py-3.0.3/test/test_info.py 0000644 0001750 0001750 00000013210 13200562156 017315 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
class TestInfoNull(unittest.TestCase):
def testTruth(self):
self.assertFalse(bool(MPI.INFO_NULL))
def testPyMethods(self):
inull = MPI.INFO_NULL
def getitem(): return inull['k']
def setitem(): inull['k'] = 'v'
def delitem(): del inull['k']
def update(): inull.update([])
self.assertEqual(len(inull), 0)
self.assertFalse('key' in inull)
self.assertRaises(KeyError, getitem)
self.assertRaises(KeyError, setitem)
self.assertRaises(KeyError, delitem)
self.assertRaises(KeyError, update)
self.assertEqual(inull.get('key', None), None)
self.assertEqual(inull.keys(), [])
self.assertEqual(inull.values(), [])
self.assertEqual(inull.items(), [])
self.assertEqual(inull.copy(), inull)
inull.clear()
class TestInfoEnv(unittest.TestCase):
def testTruth(self):
self.assertTrue(bool(MPI.INFO_ENV))
def testPyMethods(self):
env = MPI.INFO_ENV
if env == MPI.INFO_NULL: return
for key in ("command", "argv",
"maxprocs", "soft",
"host", "arch",
"wdir", "file",
"thread_level"):
v = env.Get(key)
class TestInfo(unittest.TestCase):
def setUp(self):
self.INFO = MPI.Info.Create()
def tearDown(self):
self.INFO.Free()
self.assertEqual(self.INFO, MPI.INFO_NULL)
self.INFO = None
def testTruth(self):
self.assertTrue(bool(self.INFO))
def testDup(self):
info = self.INFO.Dup()
self.assertNotEqual(self.INFO, info)
self.assertEqual(info.Get_nkeys(), 0)
info.Free()
self.assertFalse(info)
def testGet(self):
value = self.INFO.Get('key')
self.assertEqual(value, None)
def testGetNKeys(self):
self.assertEqual(self.INFO.Get_nkeys(), 0)
def testGetSetDelete(self):
INFO = self.INFO
self.assertEqual(INFO.Get_nkeys(), 0)
INFO.Set('key', 'value')
nkeys = INFO.Get_nkeys()
self.assertEqual(nkeys, 1)
key = INFO.Get_nthkey(0)
self.assertEqual(key, 'key')
value = INFO.Get('key')
self.assertEqual(value, 'value')
INFO.Delete('key')
nkeys = INFO.Get_nkeys()
self.assertEqual(nkeys, 0)
value = INFO.Get('key')
self.assertEqual(value, None)
def testPyMethods(self):
INFO = self.INFO
self.assertEqual(len(INFO), 0)
self.assertTrue('key' not in INFO)
self.assertEqual(INFO.keys(), [])
self.assertEqual(INFO.values(), [])
self.assertEqual(INFO.items(), [])
INFO['key'] = 'value'
self.assertEqual(len(INFO), 1)
self.assertTrue('key' in INFO)
self.assertEqual(INFO['key'], 'value')
for key in INFO:
self.assertEqual(key, 'key')
self.assertEqual(INFO.keys(), ['key'])
self.assertEqual(INFO.values(), ['value'])
self.assertEqual(INFO.items(), [('key', 'value')])
self.assertEqual(key, 'key')
del INFO['key']
self.assertEqual(len(INFO), 0)
self.assertTrue('key' not in INFO)
self.assertEqual(INFO.keys(), [])
self.assertEqual(INFO.values(), [])
self.assertEqual(INFO.items(), [])
def getitem(): INFO['key']
self.assertRaises(KeyError, getitem)
def delitem(): del INFO['key']
self.assertRaises(KeyError, delitem)
INFO.clear()
INFO.update([('key1','value1')])
self.assertEqual(len(INFO), 1)
self.assertEqual(INFO['key1'], 'value1')
self.assertEqual(INFO.get('key1'), 'value1')
self.assertEqual(INFO.get('key2'), None)
self.assertEqual(INFO.get('key2', 'value2'), 'value2')
INFO.update(key2='value2')
self.assertEqual(len(INFO), 2)
self.assertEqual(INFO['key1'], 'value1')
self.assertEqual(INFO['key2'], 'value2')
self.assertEqual(INFO.get('key1'), 'value1')
self.assertEqual(INFO.get('key2'), 'value2')
self.assertEqual(INFO.get('key3'), None)
self.assertEqual(INFO.get('key3', 'value3'), 'value3')
INFO.update([('key1', 'newval1')], key2='newval2')
self.assertEqual(len(INFO), 2)
self.assertEqual(INFO['key1'], 'newval1')
self.assertEqual(INFO['key2'], 'newval2')
self.assertEqual(INFO.get('key1'), 'newval1')
self.assertEqual(INFO.get('key2'), 'newval2')
self.assertEqual(INFO.get('key3'), None)
self.assertEqual(INFO.get('key3', 'newval3'), 'newval3')
INFO.update(dict(key1='val1', key2='val2', key3='val3'))
self.assertEqual(len(INFO), 3)
self.assertEqual(INFO['key1'], 'val1')
self.assertEqual(INFO['key2'], 'val2')
self.assertEqual(INFO['key3'], 'val3')
dupe = INFO.copy()
self.assertEqual(INFO.items(), dupe.items())
dupe.Free()
INFO.clear()
self.assertEqual(len(INFO), 0)
self.assertEqual(INFO.get('key1'), None)
self.assertEqual(INFO.get('key2'), None)
self.assertEqual(INFO.get('key3'), None)
self.assertEqual(INFO.get('key1', 'value1'), 'value1')
self.assertEqual(INFO.get('key2', 'value2'), 'value2')
self.assertEqual(INFO.get('key3', 'value3'), 'value3')
try:
MPI.Info.Create().Free()
except NotImplementedError:
unittest.disable(TestInfo, 'mpi-info')
unittest.disable(TestInfoNull, 'mpi-info')
if (MPI.VERSION < 3 and MPI.INFO_ENV == MPI.INFO_NULL):
unittest.disable(TestInfoEnv, 'mpi-info-env')
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_dynproc.py 0000644 0001750 0001750 00000015743 13200562156 020055 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
try:
import socket
except ImportError:
socket = None
def appnum():
if MPI.APPNUM == MPI.KEYVAL_INVALID: return None
return MPI.COMM_WORLD.Get_attr(MPI.APPNUM)
@unittest.skipMPI('openmpi(<2.0.0)')
@unittest.skipMPI('MVAPICH2')
@unittest.skipMPI('msmpi(<8.1.0)')
class TestDPM(unittest.TestCase):
message = [
None,
True, False,
-7, 0, 7,
-2**63+1, 2**63-1,
-2.17, 0.0, 3.14,
1+2j, 2-3j,
'mpi4py',
(1, 2, 3),
[1, 2, 3],
{1:2},
]
@unittest.skipMPI('mpich', appnum() is None)
@unittest.skipMPI('MPICH2', appnum() is None)
@unittest.skipMPI('MPICH1', appnum() is None)
@unittest.skipMPI('msmpi(<8.1.0)', appnum() is None)
@unittest.skipMPI('PlatformMPI')
def testNamePublishing(self):
rank = MPI.COMM_WORLD.Get_rank()
service = "mpi4py-%d" % rank
port = MPI.Open_port()
MPI.Publish_name(service, port)
found = MPI.Lookup_name(service)
self.assertEqual(port, found)
MPI.Unpublish_name(service, port)
MPI.Close_port(port)
@unittest.skipIf(MPI.COMM_WORLD.Get_size() < 2, 'mpi-world-size<2')
def testAcceptConnect(self):
comm_self = MPI.COMM_SELF
comm_world = MPI.COMM_WORLD
wsize = comm_world.Get_size()
wrank = comm_world.Get_rank()
group_world = comm_world.Get_group()
group = group_world.Excl([0])
group_world.Free()
comm = comm_world.Create(group)
group.Free()
if wrank == 0:
self.assertEqual(comm, MPI.COMM_NULL)
else:
self.assertNotEqual(comm, MPI.COMM_NULL)
self.assertEqual(comm.size, comm_world.size-1)
self.assertEqual(comm.rank, comm_world.rank-1)
if wrank == 0:
port = MPI.Open_port()
comm_world.send(port, dest=1)
intercomm = comm_self.Accept(port)
self.assertEqual(intercomm.remote_size, comm_world.size-1)
self.assertEqual(intercomm.size, 1)
self.assertEqual(intercomm.rank, 0)
MPI.Close_port(port)
else:
if wrank == 1:
port = comm_world.recv(source=0)
else:
port = None
intercomm = comm.Connect(port, root=0)
self.assertEqual(intercomm.remote_size, 1)
self.assertEqual(intercomm.size, comm_world.size-1)
self.assertEqual(intercomm.rank, comm.rank)
comm.Free()
if wrank == 0:
message = TestDPM.message
root = MPI.ROOT
else:
message = None
root = 0
message = intercomm.bcast(message, root)
if wrank == 0:
self.assertEqual(message, None)
else:
self.assertEqual(message, TestDPM.message)
intercomm.Free()
@unittest.skipIf(MPI.COMM_WORLD.Get_size() < 2, 'mpi-world-size<2')
def testConnectAccept(self):
comm_self = MPI.COMM_SELF
comm_world = MPI.COMM_WORLD
wsize = comm_world.Get_size()
wrank = comm_world.Get_rank()
group_world = comm_world.Get_group()
group = group_world.Excl([0])
group_world.Free()
comm = comm_world.Create(group)
group.Free()
if wrank == 0:
self.assertEqual(comm, MPI.COMM_NULL)
else:
self.assertNotEqual(comm, MPI.COMM_NULL)
self.assertEqual(comm.size, comm_world.size-1)
self.assertEqual(comm.rank, comm_world.rank-1)
if wrank == 0:
port = comm_world.recv(source=1)
intercomm = comm_self.Connect(port)
self.assertEqual(intercomm.remote_size, comm_world.size-1)
self.assertEqual(intercomm.size, 1)
self.assertEqual(intercomm.rank, 0)
else:
if wrank == 1:
port = MPI.Open_port()
comm_world.send(port, dest=0)
else:
port = None
intercomm = comm.Accept(port, root=0)
if wrank == 1:
MPI.Close_port(port)
self.assertEqual(intercomm.remote_size, 1)
self.assertEqual(intercomm.size, comm_world.size-1)
self.assertEqual(intercomm.rank, comm.rank)
comm.Free()
if wrank == 0:
message = TestDPM.message
root = MPI.ROOT
else:
message = None
root = 0
message = intercomm.bcast(message, root)
if wrank == 0:
self.assertEqual(message, None)
else:
self.assertEqual(message, TestDPM.message)
intercomm.Free()
@unittest.skipIf(MPI.COMM_WORLD.Get_size() < 2, 'mpi-world-size<2')
@unittest.skipIf(socket is None, 'socket')
def testJoin(self):
size = MPI.COMM_WORLD.Get_size()
rank = MPI.COMM_WORLD.Get_rank()
server = client = address = None
# crate server/client sockets
if rank == 0: # server
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
server.bind((host, 0))
server.listen(0)
if rank == 1: # client
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# communicate address
if rank == 0:
address = server.getsockname()
MPI.COMM_WORLD.ssend(address, 1)
if rank == 1:
address = MPI.COMM_WORLD.recv(None, 0)
MPI.COMM_WORLD.Barrier()
# stablish client/server connection
connected = False
if rank == 0: # server
client = server.accept()[0]
server.close()
if rank == 1: # client
try:
client.connect(address)
connected = True
except socket.error:
raise
connected = MPI.COMM_WORLD.bcast(connected, root=1)
# test Comm.Join()
MPI.COMM_WORLD.Barrier()
if client:
fd = client.fileno()
intercomm = MPI.Comm.Join(fd)
client.close()
if intercomm != MPI.COMM_NULL:
self.assertEqual(intercomm.remote_size, 1)
self.assertEqual(intercomm.size, 1)
self.assertEqual(intercomm.rank, 0)
if rank == 0:
message = TestDPM.message
root = MPI.ROOT
else:
message = None
root = 0
message = intercomm.bcast(message, root)
if rank == 0:
self.assertEqual(message, None)
else:
self.assertEqual(message, TestDPM.message)
intercomm.Free()
MPI.COMM_WORLD.Barrier()
MVAPICH2 = MPI.get_vendor()[0] == 'MVAPICH2'
try:
if MVAPICH2: raise NotImplementedError
MPI.Close_port(MPI.Open_port())
except NotImplementedError:
unittest.disable(TestDPM, 'mpi-dpm')
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_p2p_buf.py 0000644 0001750 0001750 00000033720 13200562156 017727 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
import arrayimpl
class BaseTestP2PBuf(object):
COMM = MPI.COMM_NULL
def testSendrecv(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
dest = (rank + 1) % size
source = (rank - 1) % size
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for s in range(0, size):
sbuf = array( s, typecode, s)
rbuf = array(-1, typecode, s+1)
self.COMM.Sendrecv(sbuf.as_mpi(), dest, 0,
rbuf.as_mpi(), source, 0)
for value in rbuf[:-1]:
self.assertEqual(value, s)
self.assertEqual(rbuf[-1], -1)
def testSendrecvReplace(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
dest = (rank + 1) % size
source = (rank - 1) % size
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for s in range(0, size):
buf = array(rank, typecode, s);
self.COMM.Sendrecv_replace(buf.as_mpi(), dest, 0, source, 0)
for value in buf:
self.assertEqual(value, source)
def testSendRecv(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for s in range(0, size):
#
sbuf = array( s, typecode, s)
rbuf = array(-1, typecode, s)
mem = array( 0, typecode, 2*(s+MPI.BSEND_OVERHEAD)).as_raw()
if size == 1:
MPI.Attach_buffer(mem)
rbuf = sbuf
MPI.Detach_buffer()
elif rank == 0:
MPI.Attach_buffer(mem)
self.COMM.Ibsend(sbuf.as_mpi(), 1, 0).Wait()
self.COMM.Bsend(sbuf.as_mpi(), 1, 0)
MPI.Detach_buffer()
self.COMM.Send(sbuf.as_mpi(), 1, 0)
self.COMM.Ssend(sbuf.as_mpi(), 1, 0)
self.COMM.Recv(rbuf.as_mpi(), 1, 0)
self.COMM.Recv(rbuf.as_mpi(), 1, 0)
self.COMM.Recv(rbuf.as_mpi(), 1, 0)
self.COMM.Recv(rbuf.as_mpi(), 1, 0)
elif rank == 1:
self.COMM.Recv(rbuf.as_mpi(), 0, 0)
self.COMM.Recv(rbuf.as_mpi(), 0, 0)
self.COMM.Recv(rbuf.as_mpi(), 0, 0)
self.COMM.Recv(rbuf.as_mpi(), 0, 0)
MPI.Attach_buffer(mem)
self.COMM.Ibsend(sbuf.as_mpi(), 0, 0).Wait()
self.COMM.Bsend(sbuf.as_mpi(), 0, 0)
MPI.Detach_buffer()
self.COMM.Send(sbuf.as_mpi(), 0, 0)
self.COMM.Ssend(sbuf.as_mpi(), 0, 0)
else:
rbuf = sbuf
for value in rbuf:
self.assertEqual(value, s)
#
rank = self.COMM.Get_rank()
sbuf = array( s, typecode, s)
rbuf = array(-1, typecode, s)
rreq = self.COMM.Irecv(rbuf.as_mpi(), rank, 0)
self.COMM.Rsend(sbuf.as_mpi(), rank, 0)
rreq.Wait()
for value in rbuf:
self.assertEqual(value, s)
rbuf = array(-1, typecode, s)
rreq = self.COMM.Irecv(rbuf.as_mpi(), rank, 0)
self.COMM.Irsend(sbuf.as_mpi(), rank, 0).Wait()
rreq.Wait()
for value in rbuf:
self.assertEqual(value, s)
def testProcNull(self):
comm = self.COMM
#
comm.Sendrecv(None, MPI.PROC_NULL, 0,
None, MPI.PROC_NULL, 0)
comm.Sendrecv_replace(None,
MPI.PROC_NULL, 0,
MPI.PROC_NULL, 0)
#
comm.Send (None, MPI.PROC_NULL)
comm.Isend (None, MPI.PROC_NULL).Wait()
req = comm.Send_init(None, MPI.PROC_NULL)
req.Start(); req.Wait(); req.Free()
#
comm.Ssend(None, MPI.PROC_NULL)
comm.Issend(None, MPI.PROC_NULL).Wait()
req = comm.Ssend_init(None, MPI.PROC_NULL)
req.Start(); req.Wait(); req.Free()
#
buf = MPI.Alloc_mem(MPI.BSEND_OVERHEAD)
MPI.Attach_buffer(buf)
comm.Bsend(None, MPI.PROC_NULL)
comm.Ibsend(None, MPI.PROC_NULL).Wait()
req = comm.Bsend_init(None, MPI.PROC_NULL)
req.Start(); req.Wait(); req.Free()
MPI.Detach_buffer()
MPI.Free_mem(buf)
#
comm.Rsend(None, MPI.PROC_NULL)
comm.Irsend(None, MPI.PROC_NULL).Wait()
req = comm.Rsend_init(None, MPI.PROC_NULL)
req.Start(); req.Wait(); req.Free()
#
comm.Recv (None, MPI.PROC_NULL)
comm.Irecv(None, MPI.PROC_NULL).Wait()
req = comm.Recv_init(None, MPI.PROC_NULL)
req.Start(); req.Wait(); req.Free()
def testPersistent(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
dest = (rank + 1) % size
source = (rank - 1) % size
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for s in range(size):
for xs in range(3):
#
sbuf = array( s, typecode, s)
rbuf = array(-1, typecode, s+xs)
sendreq = self.COMM.Send_init(sbuf.as_mpi(), dest, 0)
recvreq = self.COMM.Recv_init(rbuf.as_mpi(), source, 0)
sendreq.Start()
recvreq.Start()
sendreq.Wait()
recvreq.Wait()
self.assertNotEqual(sendreq, MPI.REQUEST_NULL)
self.assertNotEqual(recvreq, MPI.REQUEST_NULL)
sendreq.Free()
recvreq.Free()
self.assertEqual(sendreq, MPI.REQUEST_NULL)
self.assertEqual(recvreq, MPI.REQUEST_NULL)
for value in rbuf[:s]:
self.assertEqual(value, s)
for value in rbuf[s:]:
self.assertEqual(value, -1)
#
sbuf = array(s, typecode, s)
rbuf = array(-1, typecode, s+xs)
sendreq = self.COMM.Send_init(sbuf.as_mpi(), dest, 0)
recvreq = self.COMM.Recv_init(rbuf.as_mpi(), source, 0)
reqlist = [sendreq, recvreq]
MPI.Prequest.Startall(reqlist)
index1 = MPI.Prequest.Waitany(reqlist)
self.assertTrue(index1 in [0, 1])
self.assertNotEqual(reqlist[index1], MPI.REQUEST_NULL)
index2 = MPI.Prequest.Waitany(reqlist)
self.assertTrue(index2 in [0, 1])
self.assertNotEqual(reqlist[index2], MPI.REQUEST_NULL)
self.assertTrue(index1 != index2)
index3 = MPI.Prequest.Waitany(reqlist)
self.assertEqual(index3, MPI.UNDEFINED)
for preq in reqlist:
self.assertNotEqual(preq, MPI.REQUEST_NULL)
preq.Free()
self.assertEqual(preq, MPI.REQUEST_NULL)
for value in rbuf[:s]:
self.assertEqual(value, s)
for value in rbuf[s:]:
self.assertEqual(value, -1)
#
sbuf = array( s, typecode, s)
rbuf = array(-1, typecode, s+xs)
sendreq = self.COMM.Ssend_init(sbuf.as_mpi(), dest, 0)
recvreq = self.COMM.Recv_init(rbuf.as_mpi(), source, 0)
sendreq.Start()
recvreq.Start()
sendreq.Wait()
recvreq.Wait()
self.assertNotEqual(sendreq, MPI.REQUEST_NULL)
self.assertNotEqual(recvreq, MPI.REQUEST_NULL)
sendreq.Free()
recvreq.Free()
self.assertEqual(sendreq, MPI.REQUEST_NULL)
self.assertEqual(recvreq, MPI.REQUEST_NULL)
for value in rbuf[:s]:
self.assertEqual(value, s)
for value in rbuf[s:]:
self.assertEqual(value, -1)
#
mem = array( 0, typecode, s+MPI.BSEND_OVERHEAD).as_raw()
sbuf = array( s, typecode, s)
rbuf = array(-1, typecode, s+xs)
MPI.Attach_buffer(mem)
sendreq = self.COMM.Bsend_init(sbuf.as_mpi(), dest, 0)
recvreq = self.COMM.Recv_init(rbuf.as_mpi(), source, 0)
sendreq.Start()
recvreq.Start()
sendreq.Wait()
recvreq.Wait()
MPI.Detach_buffer()
self.assertNotEqual(sendreq, MPI.REQUEST_NULL)
self.assertNotEqual(recvreq, MPI.REQUEST_NULL)
sendreq.Free()
recvreq.Free()
self.assertEqual(sendreq, MPI.REQUEST_NULL)
self.assertEqual(recvreq, MPI.REQUEST_NULL)
for value in rbuf[:s]:
self.assertEqual(value, s)
for value in rbuf[s:]:
self.assertEqual(value, -1)
#
rank = self.COMM.Get_rank()
sbuf = array( s, typecode, s)
rbuf = array(-1, typecode, s+xs)
recvreq = self.COMM.Recv_init (rbuf.as_mpi(), rank, 0)
sendreq = self.COMM.Rsend_init(sbuf.as_mpi(), rank, 0)
recvreq.Start()
sendreq.Start()
recvreq.Wait()
sendreq.Wait()
self.assertNotEqual(sendreq, MPI.REQUEST_NULL)
self.assertNotEqual(recvreq, MPI.REQUEST_NULL)
sendreq.Free()
recvreq.Free()
self.assertEqual(sendreq, MPI.REQUEST_NULL)
self.assertEqual(recvreq, MPI.REQUEST_NULL)
for value in rbuf[:s]:
self.assertEqual(value, s)
for value in rbuf[s:]:
self.assertEqual(value, -1)
def testProbe(self):
comm = self.COMM.Dup()
try:
request = comm.Issend([None, 0, MPI.BYTE], comm.rank, 123)
self.assertTrue(request)
status = MPI.Status()
comm.Probe(MPI.ANY_SOURCE, MPI.ANY_TAG, status)
self.assertEqual(status.source, comm.rank)
self.assertEqual(status.tag, 123)
self.assertTrue(request)
flag = request.Test()
self.assertTrue(request)
self.assertFalse(flag)
comm.Recv([None, 0, MPI.BYTE], comm.rank, 123)
self.assertTrue(request)
flag = request.Test()
self.assertFalse(request)
self.assertTrue(flag)
finally:
comm.Free()
@unittest.skipMPI('MPICH1')
@unittest.skipMPI('LAM/MPI')
def testProbeCancel(self):
comm = self.COMM.Dup()
try:
request = comm.Issend([None, 0, MPI.BYTE], comm.rank, 123)
status = MPI.Status()
comm.Probe(MPI.ANY_SOURCE, MPI.ANY_TAG, status)
self.assertEqual(status.source, comm.rank)
self.assertEqual(status.tag, 123)
request.Cancel()
self.assertTrue(request)
status = MPI.Status()
request.Get_status(status)
cancelled = status.Is_cancelled()
if not cancelled:
comm.Recv([None, 0, MPI.BYTE], comm.rank, 123)
request.Wait()
else:
request.Free()
finally:
comm.Free()
def testIProbe(self):
comm = self.COMM.Dup()
try:
f = comm.Iprobe()
self.assertFalse(f)
f = comm.Iprobe(MPI.ANY_SOURCE)
self.assertFalse(f)
f = comm.Iprobe(MPI.ANY_SOURCE, MPI.ANY_TAG)
self.assertFalse(f)
status = MPI.Status()
f = comm.Iprobe(MPI.ANY_SOURCE, MPI.ANY_TAG, status)
self.assertFalse(f)
self.assertEqual(status.source, MPI.ANY_SOURCE)
self.assertEqual(status.tag, MPI.ANY_TAG)
self.assertEqual(status.error, MPI.SUCCESS)
finally:
comm.Free()
class TestP2PBufSelf(BaseTestP2PBuf, unittest.TestCase):
COMM = MPI.COMM_SELF
class TestP2PBufWorld(BaseTestP2PBuf, unittest.TestCase):
COMM = MPI.COMM_WORLD
class TestP2PBufSelfDup(TestP2PBufSelf):
def setUp(self):
self.COMM = MPI.COMM_SELF.Dup()
def tearDown(self):
self.COMM.Free()
@unittest.skipMPI('openmpi(<1.4.0)', MPI.Query_thread() > MPI.THREAD_SINGLE)
class TestP2PBufWorldDup(TestP2PBufWorld):
def setUp(self):
self.COMM = MPI.COMM_WORLD.Dup()
def tearDown(self):
self.COMM.Free()
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/mpiunittest.py 0000644 0001750 0001750 00000005637 13200562156 017726 0 ustar dalcinl dalcinl 0000000 0000000 import os
import sys
import glob
import unittest
from distutils.versionpredicate import VersionPredicate
class TestCase(unittest.TestCase):
def assertRaisesMPI(self, IErrClass, callableObj, *args, **kwargs):
from mpi4py.MPI import Exception as excClass, Get_version
try:
callableObj(*args, **kwargs)
except NotImplementedError:
if Get_version() >= (2, 0):
raise self.failureException("raised NotImplementedError")
except excClass:
excValue = sys.exc_info()[1]
error_class = excValue.Get_error_class()
if isinstance(IErrClass, (list, tuple)):
match = (error_class in IErrClass)
else:
match = (error_class == IErrClass)
if not match:
if isinstance(IErrClass, (list, tuple)):
IErrClassName = [ErrClsName(e) for e in IErrClass]
IErrClassName = type(IErrClass)(IErrClassName)
else:
IErrClassName = ErrClsName(IErrClass)
raise self.failureException(
"generated error class is '%s' (%d), "
"but expected '%s' (%s)" % \
(ErrClsName(error_class), error_class,
IErrClassName, IErrClass,)
)
else:
if hasattr(excClass,'__name__'): excName = excClass.__name__
else: excName = str(excClass)
raise self.failureException("%s not raised" % excName)
failUnlessRaisesMPI = assertRaisesMPI
ErrClsMap = None
def ErrClsName(ierr):
global ErrClsMap
if ErrClsMap is None:
from mpi4py import MPI
ErrClsMap = {}
ErrClsMap[MPI.SUCCESS] = 'SUCCESS'
for entry in dir(MPI):
if entry.startswith('ERR_'):
errcls = getattr(MPI, entry)
ErrClsMap[errcls] = entry
try:
return ErrClsMap[ierr]
except KeyError:
return ''
SkipTest = unittest.SkipTest
skip = unittest.skip
skipIf = unittest.skipIf
skipUnless = unittest.skipUnless
def skipMPI(predicate, *conditions):
from mpi4py import MPI
def key(s):
s = s.replace(' ', '')
s = s.replace('/', '')
s = s.replace('-', '')
s = s.replace('Microsoft', 'MS')
return s.lower()
vp = VersionPredicate(key(predicate))
if vp.name == 'mpi':
name, version = 'mpi', MPI.Get_version()
version = version + (0,)
else:
name, version = MPI.get_vendor()
if vp.name == key(name):
if vp.satisfied_by('%d.%d.%d' % version):
if not conditions or any(conditions):
return unittest.skip(str(vp))
return unittest.skipIf(False, '')
def disable(what, reason):
return unittest.skip(reason)(what)
def main(*args, **kargs):
try:
unittest.main(*args, **kargs)
except SystemExit:
pass
mpi4py-3.0.3/test/test_attributes.py 0000664 0001750 0001750 00000016256 13426006675 020600 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
try:
import array
except ImportError:
array = None
class BaseTestAttr(object):
keyval = MPI.KEYVAL_INVALID
def tearDown(self):
if self.obj:
self.obj.Free()
if self.keyval != MPI.KEYVAL_INVALID:
self.keyval = type(self.obj).Free_keyval(self.keyval)
self.assertEqual(self.keyval, MPI.KEYVAL_INVALID)
def testAttr(self, copy_fn=None, delete_fn=None):
cls, obj = type(self.obj), self.obj
self.keyval = cls.Create_keyval(copy_fn, delete_fn)
self.assertNotEqual(self.keyval, MPI.KEYVAL_INVALID)
attr = obj.Get_attr(self.keyval)
self.assertEqual(attr, None)
attrval = [1,2,3]
obj.Set_attr(self.keyval, attrval)
attr = obj.Get_attr(self.keyval)
self.assertTrue(attr is attrval)
if hasattr(obj, 'Dup'):
dup = obj.Dup()
attr = dup.Get_attr(self.keyval)
if copy_fn is True:
self.assertTrue(attr is attrval)
elif not copy_fn:
self.assertTrue(attr is None)
dup.Free()
obj.Delete_attr(self.keyval)
attr = obj.Get_attr(self.keyval)
self.assertTrue(attr is None)
def testAttrCopyFalse(self):
self.testAttr(False)
def testAttrCopyTrue(self):
self.testAttr(True)
def testAttrNoCopy(self):
cls, obj = type(self.obj), self.obj
def copy_fn(o, k, v):
assert k == self.keyval
assert v is attrval
return NotImplemented
self.keyval = cls.Create_keyval(copy_fn, None)
self.assertNotEqual(self.keyval, MPI.KEYVAL_INVALID)
attr = obj.Get_attr(self.keyval)
self.assertEqual(attr, None)
attrval = [1,2,3]
obj.Set_attr(self.keyval, attrval)
attr = obj.Get_attr(self.keyval)
self.assertTrue(attr is attrval)
if hasattr(obj, 'Dup'):
dup = obj.Dup()
attr = dup.Get_attr(self.keyval)
self.assertTrue(attr is None)
dup.Free()
obj.Delete_attr(self.keyval)
attr = obj.Get_attr(self.keyval)
self.assertTrue(attr is None)
def testAttrNoPython(self, intval=123456789):
cls, obj = type(self.obj), self.obj
def copy_fn(o, k, v):
assert k == self.keyval
assert v == intval
return v
def del_fn(o, k, v):
assert k == self.keyval
assert v == intval
self.keyval = cls.Create_keyval(copy_fn, del_fn, nopython=True)
self.assertNotEqual(self.keyval, MPI.KEYVAL_INVALID)
attr = obj.Get_attr(self.keyval)
self.assertEqual(attr, None)
obj.Set_attr(self.keyval, intval)
attr = obj.Get_attr(self.keyval)
self.assertEqual(attr, intval)
if hasattr(obj, 'Dup'):
dup = obj.Dup()
attr = dup.Get_attr(self.keyval)
self.assertEqual(attr, intval)
dup.Free()
obj.Delete_attr(self.keyval)
attr = obj.Get_attr(self.keyval)
self.assertTrue(attr is None)
@unittest.skipMPI('openmpi(<=1.10.2)')
def testAttrNoPythonZero(self):
self.testAttrNoPython(0)
@unittest.skipIf(array is None, 'array')
def testAttrNoPythonArray(self):
cls, obj = type(self.obj), self.obj
self.keyval = cls.Create_keyval(nopython=True)
#
ary = array.array('i', [42])
addr, _ = ary.buffer_info()
obj.Set_attr(self.keyval, addr)
#
attr = obj.Get_attr(self.keyval)
self.assertEqual(attr, addr)
class BaseTestCommAttr(BaseTestAttr):
NULL = MPI.COMM_NULL
@unittest.skipMPI('openmpi(<=1.5.1)')
def testAttrCopyDelete(self):
cls, obj, null = type(self.obj), self.obj, self.NULL
#
self.keyval = cls.Create_keyval(
copy_fn=lambda o, k, v: cls.Dup(v),
delete_fn=lambda o, k, v: cls.Free(v))
self.assertNotEqual(self.keyval, MPI.KEYVAL_INVALID)
#
obj1 = obj
dup1 = obj1.Dup()
obj1.Set_attr(self.keyval, dup1)
self.assertTrue(dup1 != null)
obj2 = obj1.Dup()
dup2 = obj2.Get_attr(self.keyval)
self.assertTrue(dup1 != dup2)
obj2.Free()
self.assertTrue(dup2 == null)
self.obj.Delete_attr(self.keyval)
self.assertTrue(dup1 == null)
class TestCommAttrWorld(BaseTestCommAttr, unittest.TestCase):
def setUp(self):
self.obj = MPI.COMM_WORLD.Dup()
class TestCommAttrSelf(BaseTestCommAttr, unittest.TestCase):
def setUp(self):
self.obj = MPI.COMM_SELF.Dup()
class BaseTestDatatypeAttr(BaseTestAttr):
NULL = MPI.DATATYPE_NULL
def testAttrCopyDelete(self):
cls, obj, null = type(self.obj), self.obj, self.NULL
#
self.keyval = cls.Create_keyval(
copy_fn=lambda o, k, v: cls.Dup(v),
delete_fn=lambda o, k, v: cls.Free(v))
self.assertNotEqual(self.keyval, MPI.KEYVAL_INVALID)
#
obj1 = obj
dup1 = obj1.Dup()
obj1.Set_attr(self.keyval, dup1)
self.assertTrue(dup1 != null)
obj2 = obj1.Dup()
dup2 = obj2.Get_attr(self.keyval)
self.assertTrue(dup1 != dup2)
obj2.Free()
self.assertTrue(dup2 == null)
self.obj.Delete_attr(self.keyval)
self.assertTrue(dup1 == null)
class TestDatatypeAttrBYTE(BaseTestDatatypeAttr, unittest.TestCase):
def setUp(self):
self.obj = MPI.BYTE.Dup()
class TestDatatypeAttrINT(BaseTestDatatypeAttr, unittest.TestCase):
def setUp(self):
self.obj = MPI.INT.Dup()
class TestDatatypeAttrFLOAT(BaseTestDatatypeAttr, unittest.TestCase):
def setUp(self):
self.obj = MPI.FLOAT.Dup()
class TestWinAttr(BaseTestAttr, unittest.TestCase):
NULL = MPI.WIN_NULL
def setUp(self):
win = MPI.Win.Create(MPI.BOTTOM, 1,
MPI.INFO_NULL, MPI.COMM_SELF)
self.obj = self.win = win
@unittest.skipMPI('openmpi(<=1.5.1)')
@unittest.skipMPI('PlatformMPI')
def testAttrCopyDelete(self):
#
null = self.NULL
def delete_fn(o, k, v):
assert isinstance(o, MPI.Win)
assert k == self.keyval
assert v is win
MPI.Win.Free(v)
self.keyval = MPI.Win.Create_keyval(delete_fn=delete_fn)
self.assertNotEqual(self.keyval, MPI.KEYVAL_INVALID)
#
win = MPI.Win.Create(MPI.BOTTOM, 1,
MPI.INFO_NULL, MPI.COMM_SELF)
self.obj.Set_attr(self.keyval, win)
self.assertTrue(win != null)
self.obj.Delete_attr(self.keyval)
self.assertTrue(win == null)
try:
k = MPI.Datatype.Create_keyval()
k = MPI.Datatype.Free_keyval(k)
except NotImplementedError:
unittest.disable(BaseTestDatatypeAttr, 'mpi-type-attr')
SpectrumMPI = MPI.get_vendor()[0] == 'Spectrum MPI'
try:
if SpectrumMPI: raise NotImplementedError
MPI.Win.Create(MPI.BOTTOM, 1, MPI.INFO_NULL, MPI.COMM_SELF).Free()
k = MPI.Win.Create_keyval()
k = MPI.Win.Free_keyval(k)
except (NotImplementedError, MPI.Exception):
unittest.disable(TestWinAttr, 'mpi-win-attr')
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_grequest.py 0000644 0001750 0001750 00000004414 13200562156 020227 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
class GReqCtx(object):
source = 3
tag = 7
completed = False
cancel_called = False
free_called = False
def query(self, status):
status.Set_source(self.source)
status.Set_tag(self.tag)
def free(self):
self.free_called = True
def cancel(self, completed):
self.cancel_called = True
if completed is not self.completed:
raise MPI.Exception(MPI.ERR_PENDING)
@unittest.skipMPI('MPI(<2.0)')
class TestGrequest(unittest.TestCase):
def testAll(self):
ctx = GReqCtx()
greq = MPI.Grequest.Start(ctx.query, ctx.free, ctx.cancel)
self.assertFalse(greq.Test())
self.assertFalse(ctx.free_called)
greq.Cancel()
self.assertTrue(ctx.cancel_called)
ctx.cancel_called = False
greq.Complete()
ctx.completed = True
greq.Cancel()
self.assertTrue(ctx.cancel_called)
status = MPI.Status()
self.assertTrue(greq.Test(status))
self.assertEqual(status.Get_source(), ctx.source)
self.assertEqual(status.Get_tag(), ctx.tag)
self.assertEqual(status.Get_error(), MPI.SUCCESS)
greq.Wait()
self.assertTrue(ctx.free_called)
def testAll1(self):
ctx = GReqCtx()
greq = MPI.Grequest.Start(ctx.query, None, None)
self.assertFalse(greq.Test())
greq.Cancel()
greq.Complete()
status = MPI.Status()
self.assertTrue(greq.Test(status))
self.assertEqual(status.Get_source(), ctx.source)
self.assertEqual(status.Get_tag(), ctx.tag)
self.assertEqual(status.Get_error(), MPI.SUCCESS)
self.assertFalse(status.Is_cancelled())
greq.Wait()
def testAll2(self):
greq = MPI.Grequest.Start(None, None, None)
self.assertFalse(greq.Test())
greq.Cancel()
greq.Complete()
status = MPI.Status()
self.assertTrue(greq.Test(status))
self.assertEqual(status.Get_source(), MPI.ANY_SOURCE)
self.assertEqual(status.Get_tag(), MPI.ANY_TAG)
self.assertEqual(status.Get_error(), MPI.SUCCESS)
self.assertFalse(status.Is_cancelled())
greq.Wait()
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_exceptions.py 0000664 0001750 0001750 00000030217 13426006675 020564 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
import sys, os
HAVE_MPE = 'MPE_LOGFILE_PREFIX' in os.environ
HAVE_VT = 'VT_FILE_PREFIX' in os.environ
# --------------------------------------------------------------------
@unittest.skipMPI('PlatformMPI')
@unittest.skipMPI('MPICH2')
@unittest.skipIf(HAVE_MPE or HAVE_VT, 'mpe|vt')
class BaseTestCase(unittest.TestCase):
def setUp(self):
self.errhdl_world = MPI.COMM_WORLD.Get_errhandler()
MPI.COMM_WORLD.Set_errhandler(MPI.ERRORS_RETURN)
self.errhdl_self = MPI.COMM_SELF.Get_errhandler()
MPI.COMM_SELF.Set_errhandler(MPI.ERRORS_RETURN)
def tearDown(self):
MPI.COMM_WORLD.Set_errhandler(self.errhdl_world)
self.errhdl_world.Free()
MPI.COMM_SELF.Set_errhandler(self.errhdl_self)
self.errhdl_self.Free()
# --------------------------------------------------------------------
class TestExcDatatypeNull(BaseTestCase):
def testDup(self):
self.assertRaisesMPI(MPI.ERR_TYPE, MPI.DATATYPE_NULL.Dup)
def testCommit(self):
self.assertRaisesMPI(MPI.ERR_TYPE, MPI.DATATYPE_NULL.Commit)
def testFree(self):
self.assertRaisesMPI(MPI.ERR_TYPE, MPI.DATATYPE_NULL.Free)
class TestExcDatatype(BaseTestCase):
DATATYPES = (MPI.BYTE, MPI.PACKED,
MPI.CHAR, MPI.WCHAR,
MPI.SIGNED_CHAR, MPI.UNSIGNED_CHAR,
MPI.SHORT, MPI.UNSIGNED_SHORT,
MPI.INT, MPI.UNSIGNED, MPI.UNSIGNED_INT,
MPI.LONG, MPI.UNSIGNED_LONG,
MPI.LONG_LONG, MPI.UNSIGNED_LONG_LONG,
MPI.FLOAT, MPI.DOUBLE, MPI.LONG_DOUBLE,
MPI.SHORT_INT, MPI.TWOINT, MPI.INT_INT, MPI.LONG_INT,
MPI.FLOAT_INT, MPI.DOUBLE_INT, MPI.LONG_DOUBLE_INT,
MPI.UB, MPI.LB,)
ERR_TYPE = MPI.ERR_TYPE
@unittest.skipMPI('msmpi')
def testFreePredefined(self):
for dtype in self.DATATYPES:
if dtype != MPI.DATATYPE_NULL:
self.assertRaisesMPI(self.ERR_TYPE, dtype.Free)
self.assertTrue(dtype != MPI.DATATYPE_NULL)
def testKeyvalInvalid(self):
for dtype in self.DATATYPES:
if dtype != MPI.DATATYPE_NULL:
try:
self.assertRaisesMPI(
[MPI.ERR_KEYVAL, MPI.ERR_OTHER],
dtype.Get_attr, MPI.KEYVAL_INVALID)
except NotImplementedError:
self.skipTest('mpi-type-get_attr')
name, version = MPI.get_vendor()
if name == 'Open MPI':
if version < (1,4,3):
TestExcDatatype.DATATYPES = TestExcDatatype.DATATYPES[1:]
TestExcDatatype.ERR_TYPE = MPI.ERR_INTERN
# --------------------------------------------------------------------
@unittest.skipMPI('msmpi(<=4.2.0)')
class TestExcStatus(BaseTestCase):
def testGetCount(self):
status = MPI.Status()
self.assertRaisesMPI(
MPI.ERR_TYPE, status.Get_count, MPI.DATATYPE_NULL)
def testGetElements(self):
status = MPI.Status()
self.assertRaisesMPI(
MPI.ERR_TYPE, status.Get_elements, MPI.DATATYPE_NULL)
@unittest.skipMPI('MPICH1')
def testSetElements(self):
status = MPI.Status()
self.assertRaisesMPI(
MPI.ERR_TYPE, status.Set_elements, MPI.DATATYPE_NULL, 0)
# --------------------------------------------------------------------
class TestExcRequestNull(BaseTestCase):
def testFree(self):
self.assertRaisesMPI(MPI.ERR_REQUEST, MPI.REQUEST_NULL.Free)
def testCancel(self):
self.assertRaisesMPI(MPI.ERR_REQUEST, MPI.REQUEST_NULL.Cancel)
# --------------------------------------------------------------------
class TestExcOpNull(BaseTestCase):
def testFree(self):
self.assertRaisesMPI([MPI.ERR_OP, MPI.ERR_ARG], MPI.OP_NULL.Free)
class TestExcOp(BaseTestCase):
def testFreePredefined(self):
for op in (MPI.MAX, MPI.MIN,
MPI.SUM, MPI.PROD,
MPI.LAND, MPI.BAND,
MPI.LOR, MPI.BOR,
MPI.LXOR, MPI.BXOR,
MPI.MAXLOC, MPI.MINLOC):
self.assertRaisesMPI([MPI.ERR_OP, MPI.ERR_ARG], op.Free)
if MPI.REPLACE != MPI.OP_NULL:
self.assertRaisesMPI([MPI.ERR_OP, MPI.ERR_ARG], op.Free)
# --------------------------------------------------------------------
class TestExcInfoNull(BaseTestCase):
def testTruth(self):
self.assertFalse(bool(MPI.INFO_NULL))
@unittest.skipMPI('msmpi(<8.1.0)')
def testDup(self):
self.assertRaisesMPI(
[MPI.ERR_INFO, MPI.ERR_ARG], MPI.INFO_NULL.Dup)
def testFree(self):
self.assertRaisesMPI(
[MPI.ERR_INFO, MPI.ERR_ARG], MPI.INFO_NULL.Free)
def testGet(self):
self.assertRaisesMPI(
[MPI.ERR_INFO, MPI.ERR_ARG], MPI.INFO_NULL.Get, 'key')
def testSet(self):
self.assertRaisesMPI(
[MPI.ERR_INFO, MPI.ERR_ARG], MPI.INFO_NULL.Set, 'key', 'value')
def testDelete(self):
self.assertRaisesMPI(
[MPI.ERR_INFO, MPI.ERR_ARG], MPI.INFO_NULL.Delete, 'key')
def testGetNKeys(self):
self.assertRaisesMPI(
[MPI.ERR_INFO, MPI.ERR_ARG], MPI.INFO_NULL.Get_nkeys)
def testGetNthKey(self):
self.assertRaisesMPI(
[MPI.ERR_INFO, MPI.ERR_ARG], MPI.INFO_NULL.Get_nthkey, 0)
class TestExcInfo(BaseTestCase):
def setUp(self):
super(TestExcInfo, self).setUp()
self.INFO = MPI.Info.Create()
def tearDown(self):
self.INFO.Free()
self.INFO = None
super(TestExcInfo, self).tearDown()
def testDelete(self):
self.assertRaisesMPI(
MPI.ERR_INFO_NOKEY, self.INFO.Delete, 'key')
def testGetNthKey(self):
self.assertRaisesMPI(
[MPI.ERR_INFO_KEY, MPI.ERR_ARG], self.INFO.Get_nthkey, 0)
try:
MPI.Info.Create().Free()
except NotImplementedError:
unittest.disable(TestExcInfo, 'mpi-info')
unittest.disable(TestExcInfoNull, 'mpi-info')
# --------------------------------------------------------------------
class TestExcGroupNull(BaseTestCase):
def testCompare(self):
self.assertRaisesMPI(
MPI.ERR_GROUP, MPI.Group.Compare, MPI.GROUP_NULL, MPI.GROUP_NULL)
self.assertRaisesMPI(
MPI.ERR_GROUP, MPI.Group.Compare, MPI.GROUP_NULL, MPI.GROUP_EMPTY)
self.assertRaisesMPI(
MPI.ERR_GROUP, MPI.Group.Compare, MPI.GROUP_EMPTY, MPI.GROUP_NULL)
def testAccessors(self):
for method in ('Get_size', 'Get_rank'):
self.assertRaisesMPI(
MPI.ERR_GROUP, getattr(MPI.GROUP_NULL, method))
class TestExcGroup(BaseTestCase):
pass
# --------------------------------------------------------------------
class TestExcCommNull(BaseTestCase):
ERR_COMM = MPI.ERR_COMM
def testCompare(self):
self.assertRaisesMPI(
self.ERR_COMM, MPI.Comm.Compare, MPI.COMM_NULL, MPI.COMM_NULL)
self.assertRaisesMPI(
self.ERR_COMM, MPI.Comm.Compare, MPI.COMM_SELF, MPI.COMM_NULL)
self.assertRaisesMPI(
self.ERR_COMM, MPI.Comm.Compare, MPI.COMM_WORLD, MPI.COMM_NULL)
self.assertRaisesMPI(
self.ERR_COMM, MPI.Comm.Compare, MPI.COMM_NULL, MPI.COMM_SELF)
self.assertRaisesMPI(
self.ERR_COMM, MPI.Comm.Compare, MPI.COMM_NULL, MPI.COMM_WORLD)
def testAccessors(self):
for method in ('Get_size', 'Get_rank',
'Is_inter', 'Is_intra',
'Get_group', 'Get_topology'):
self.assertRaisesMPI(MPI.ERR_COMM, getattr(MPI.COMM_NULL, method))
def testFree(self):
self.assertRaisesMPI(MPI.ERR_COMM, MPI.COMM_NULL.Free)
def testDisconnect(self):
try:
self.assertRaisesMPI(MPI.ERR_COMM, MPI.COMM_NULL.Disconnect)
except NotImplementedError:
self.skipTest('mpi-comm-disconnect')
@unittest.skipMPI('openmpi(<1.4.2)')
def testGetAttr(self):
self.assertRaisesMPI(
MPI.ERR_COMM, MPI.COMM_NULL.Get_attr, MPI.TAG_UB)
@unittest.skipMPI('openmpi(<1.4.1)')
def testGetErrhandler(self):
self.assertRaisesMPI(
[MPI.ERR_COMM, MPI.ERR_ARG], MPI.COMM_NULL.Get_errhandler)
def testSetErrhandler(self):
self.assertRaisesMPI(
MPI.ERR_COMM, MPI.COMM_NULL.Set_errhandler, MPI.ERRORS_RETURN)
def testIntraNull(self):
comm_null = MPI.Intracomm()
self.assertRaisesMPI(MPI.ERR_COMM, comm_null.Dup)
self.assertRaisesMPI(MPI.ERR_COMM, comm_null.Create, MPI.GROUP_EMPTY)
self.assertRaisesMPI(MPI.ERR_COMM, comm_null.Split, color=0, key=0)
def testInterNull(self):
comm_null = MPI.Intercomm()
self.assertRaisesMPI(MPI.ERR_COMM, comm_null.Get_remote_group)
self.assertRaisesMPI(MPI.ERR_COMM, comm_null.Get_remote_size)
self.assertRaisesMPI(MPI.ERR_COMM, comm_null.Dup)
self.assertRaisesMPI(MPI.ERR_COMM, comm_null.Create, MPI.GROUP_EMPTY)
self.assertRaisesMPI(MPI.ERR_COMM, comm_null.Split, color=0, key=0)
self.assertRaisesMPI(MPI.ERR_COMM, comm_null.Merge, high=True)
class TestExcComm(BaseTestCase):
@unittest.skipMPI('MPICH1')
def testFreeSelf(self):
errhdl = MPI.COMM_SELF.Get_errhandler()
try:
MPI.COMM_SELF.Set_errhandler(MPI.ERRORS_RETURN)
self.assertRaisesMPI(
[MPI.ERR_COMM, MPI.ERR_ARG], MPI.COMM_SELF.Free)
finally:
MPI.COMM_SELF.Set_errhandler(errhdl)
errhdl.Free()
@unittest.skipMPI('MPICH1')
def testFreeWorld(self):
self.assertRaisesMPI(
[MPI.ERR_COMM, MPI.ERR_ARG], MPI.COMM_WORLD.Free)
def testKeyvalInvalid(self):
self.assertRaisesMPI(
[MPI.ERR_KEYVAL, MPI.ERR_OTHER],
MPI.COMM_WORLD.Get_attr, MPI.KEYVAL_INVALID)
# --------------------------------------------------------------------
class TestExcWinNull(BaseTestCase):
def testFree(self):
self.assertRaisesMPI(
[MPI.ERR_WIN, MPI.ERR_ARG], MPI.WIN_NULL.Free)
def testGetErrhandler(self):
self.assertRaisesMPI(
[MPI.ERR_WIN, MPI.ERR_ARG], MPI.WIN_NULL.Get_errhandler)
def testSetErrhandler(self):
self.assertRaisesMPI(
[MPI.ERR_WIN, MPI.ERR_ARG],
MPI.WIN_NULL.Set_errhandler, MPI.ERRORS_RETURN)
def testCallErrhandler(self):
self.assertRaisesMPI([MPI.ERR_WIN, MPI.ERR_ARG],
MPI.WIN_NULL.Call_errhandler, 0)
class TestExcWin(BaseTestCase):
def setUp(self):
super(TestExcWin, self).setUp()
self.WIN = MPI.Win.Create(None, 1, MPI.INFO_NULL, MPI.COMM_SELF)
self.WIN.Set_errhandler(MPI.ERRORS_RETURN)
def tearDown(self):
self.WIN.Free()
self.WIN = None
super(TestExcWin, self).tearDown()
def testKeyvalInvalid(self):
self.assertRaisesMPI(
[MPI.ERR_KEYVAL, MPI.ERR_OTHER],
self.WIN.Get_attr, MPI.KEYVAL_INVALID)
SpectrumMPI = MPI.get_vendor()[0] == 'Spectrum MPI'
try:
if SpectrumMPI: raise NotImplementedError
MPI.Win.Create(None, 1, MPI.INFO_NULL, MPI.COMM_SELF).Free()
except (NotImplementedError, MPI.Exception):
unittest.disable(TestExcWin, 'mpi-win')
unittest.disable(TestExcWinNull, 'mpi-win')
# --------------------------------------------------------------------
class TestExcErrhandlerNull(BaseTestCase):
def testFree(self):
self.assertRaisesMPI(MPI.ERR_ARG, MPI.ERRHANDLER_NULL.Free)
def testCommSelfSetErrhandler(self):
self.assertRaisesMPI(
MPI.ERR_ARG, MPI.COMM_SELF.Set_errhandler, MPI.ERRHANDLER_NULL)
def testCommWorldSetErrhandler(self):
self.assertRaisesMPI(
MPI.ERR_ARG, MPI.COMM_WORLD.Set_errhandler, MPI.ERRHANDLER_NULL)
# class TestExcErrhandler(BaseTestCase):
#
# def testFreePredefined(self):
# self.assertRaisesMPI(MPI.ERR_ARG, MPI.ERRORS_ARE_FATAL.Free)
# self.assertRaisesMPI(MPI.ERR_ARG, MPI.ERRORS_RETURN.Free)
# pass
# --------------------------------------------------------------------
if __name__ == '__main__':
unittest.main()
# --------------------------------------------------------------------
mpi4py-3.0.3/test/test_objmodel.py 0000644 0001750 0001750 00000007255 13200562156 020171 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
import sys
class TestObjModel(unittest.TestCase):
objects = [
MPI.Status(),
MPI.DATATYPE_NULL,
MPI.REQUEST_NULL,
MPI.INFO_NULL,
MPI.ERRHANDLER_NULL,
MPI.GROUP_NULL,
MPI.WIN_NULL,
MPI.OP_NULL,
MPI.FILE_NULL,
MPI.MESSAGE_NULL,
MPI.COMM_NULL,
]
def testEq(self):
for i, obj1 in enumerate(self.objects):
objects = self.objects[:]
obj2 = objects[i]
self.assertTrue(obj1 == obj2)
self.assertFalse(obj1 != obj2)
del objects[i]
for obj2 in objects:
self.assertTrue(obj1 != obj2)
self.assertTrue(obj2 != obj1)
self.assertFalse(obj1 == obj2)
self.assertFalse(obj2 == obj1)
self.assertFalse(None == obj1 )
self.assertFalse(obj1 == None )
self.assertFalse(obj1 == True )
self.assertFalse(obj1 == False)
self.assertFalse(obj1 == 12345)
self.assertFalse(obj1 == "abc")
self.assertFalse(obj1 == [123])
self.assertFalse(obj1 == (1,2))
self.assertFalse(obj1 == {0:0})
self.assertFalse(obj1 == set())
def testNe(self):
for i, obj1 in enumerate(self.objects):
objects = self.objects[:]
obj2 = objects[i]
self.assertFalse(obj1 != obj2)
del objects[i]
for obj2 in objects:
self.assertTrue (obj1 != obj2)
self.assertTrue(None != obj1 )
self.assertTrue(obj1 != None )
self.assertTrue(obj1 != True )
self.assertTrue(obj1 != False)
self.assertTrue(obj1 != 12345)
self.assertTrue(obj1 != "abc")
self.assertTrue(obj1 != [123])
self.assertTrue(obj1 != (1,2))
self.assertTrue(obj1 != {0:0})
self.assertTrue(obj1 != set())
def testBool(self):
for obj in self.objects[1:]:
self.assertFalse(not not obj)
self.assertTrue(not obj)
self.assertFalse(obj)
def testHash(self):
try:
hash(MPI.COMM_NULL)
except TypeError:
pass
else:
if hasattr(sys, 'pypy_version_info'):
self.skipTest('pypy')
for obj in self.objects:
ob_hash = lambda: hash(obj)
self.assertRaises(TypeError, ob_hash)
def testInit(self):
for i, obj in enumerate(self.objects):
klass = type(obj)
new = klass()
self.assertEqual(new, obj)
new = klass(obj)
self.assertEqual(new, obj)
objects = self.objects[:]
del objects[i]
for other in objects:
ob_init = lambda: klass(other)
self.assertRaises(TypeError, ob_init)
ob_init = lambda: klass(1234)
self.assertRaises(TypeError, ob_init)
ob_init = lambda: klass("abc")
self.assertRaises(TypeError, ob_init)
def testSizeOf(self):
for obj in self.objects:
n1 = MPI._sizeof(obj)
n2 = MPI._sizeof(type(obj))
self.assertEqual(n1, n2)
def testAddressOf(self):
for obj in self.objects:
addr = MPI._addressof(obj)
def testAHandleOf(self):
for obj in self.objects:
if isinstance(obj, MPI.Status):
hdl = lambda: MPI._handleof(obj)
self.assertRaises(NotImplementedError, hdl)
continue
hdl = MPI._handleof(obj)
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_pack.py 0000644 0001750 0001750 00000012562 13200562156 017311 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
import arrayimpl
import struct
class BaseTestPack(object):
COMM = MPI.COMM_NULL
def testPackSize(self):
for array in arrayimpl.ArrayTypes:
for typecode, datatype in arrayimpl.TypeMap.items():
itemsize = struct.calcsize(typecode)
overhead = datatype.Pack_size(0, self.COMM)
for count in range(10):
pack_size = datatype.Pack_size(count, self.COMM)
self.assertEqual(pack_size - overhead, count*itemsize)
def testPackUnpack(self):
for array in arrayimpl.ArrayTypes:
for typecode1, datatype1 in arrayimpl.TypeMap.items():
for typecode2, datatype2 in arrayimpl.TypeMap.items():
for items in range(10):
# input and output arrays
iarray1 = array(range(items), typecode1).as_raw()
iarray2 = array(range(items), typecode2).as_raw()
oarray1 = array(items, typecode1, items).as_raw()
oarray2 = array(items, typecode2, items).as_raw()
# temp array for packing
size1 = datatype1.Pack_size(len(iarray1), self.COMM)
size2 = datatype2.Pack_size(len(iarray2), self.COMM)
tmpbuf = array(0, 'b', size1 + size2 + 1).as_raw()
# pack input arrays
position = 0
position = datatype1.Pack(iarray1, tmpbuf, position, self.COMM)
position = datatype2.Pack(iarray2, tmpbuf, position, self.COMM)
# unpack output arrays
position = 0
position = datatype1.Unpack(tmpbuf, position, oarray1, self.COMM)
position = datatype2.Unpack(tmpbuf, position, oarray2, self.COMM)
# test
equal = arrayimpl.allclose
self.assertTrue(equal(iarray1, oarray1))
self.assertTrue(equal(iarray2, oarray2))
EXT32 = 'external32'
class BaseTestPackExternal(object):
skipdtype = []
def testPackSize(self):
for array in arrayimpl.ArrayTypes:
for typecode, datatype in arrayimpl.TypeMap.items():
itemsize = struct.calcsize(typecode)
overhead = datatype.Pack_external_size(EXT32, 0)
for count in range(10):
pack_size = datatype.Pack_external_size(EXT32, count)
real_size = pack_size - overhead
def testPackUnpackExternal(self):
for array in arrayimpl.ArrayTypes:
for typecode1, datatype1 in arrayimpl.TypeMap.items():
for typecode2, datatype2 in arrayimpl.TypeMap.items():
for items in range(1, 10):
if typecode1 in self.skipdtype: continue
if typecode2 in self.skipdtype: continue
# input and output arrays
if typecode1 == 'b':
iarray1 = array(127, typecode1, items).as_raw()
else:
iarray1 = array(255, typecode1, items).as_raw()
iarray2 = array(range(items), typecode2).as_raw()
oarray1 = array(-1, typecode1, items).as_raw()
oarray2 = array(-1, typecode2, items).as_raw()
# temp array for packing
size1 = datatype1.Pack_external_size(EXT32, iarray1.size)
size2 = datatype2.Pack_external_size(EXT32, iarray2.size)
tmpbuf = array(0, 'b', size1 + size2 + 1).as_raw()
# pack input arrays
position = 0
position = datatype1.Pack_external(EXT32, iarray1, tmpbuf, position)
position = datatype2.Pack_external(EXT32, iarray2, tmpbuf, position)
# unpack output arrays
position = 0
position = datatype1.Unpack_external(EXT32, tmpbuf, position, oarray1)
position = datatype2.Unpack_external(EXT32, tmpbuf, position, oarray2)
# test result
equal = arrayimpl.allclose
self.assertTrue(equal(iarray1, oarray1))
self.assertTrue(equal(iarray2, oarray2))
class TestPackSelf(BaseTestPack, unittest.TestCase):
COMM = MPI.COMM_SELF
class TestPackWorld(BaseTestPack, unittest.TestCase):
COMM = MPI.COMM_SELF
@unittest.skipMPI('openmpi(<3.0.0)')
class TestPackExternal(BaseTestPackExternal, unittest.TestCase):
pass
name, version = MPI.get_vendor()
if name =='MPICH' or name == 'MPICH2':
BaseTestPackExternal.skipdtype += ['l']
BaseTestPackExternal.skipdtype += ['d']
elif name == 'Intel MPI':
BaseTestPackExternal.skipdtype += ['l']
BaseTestPackExternal.skipdtype += ['d']
elif name == 'MVAPICH2':
BaseTestPackExternal.skipdtype += ['l']
BaseTestPackExternal.skipdtype += ['d']
else:
try:
MPI.BYTE.Pack_external_size(EXT32, 0)
except NotImplementedError:
unittest.disable(BaseTestPackExternal, 'mpi-ext32')
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/spawn_child.py 0000644 0001750 0001750 00000000705 12750576064 017637 0 ustar dalcinl dalcinl 0000000 0000000 import sys; sys.path.insert(0, sys.argv[1])
import mpi4py
if len(sys.argv) > 2:
lfn = "runtests-mpi4py-child"
mpe = sys.argv[2] == 'mpe'
vt = sys.argv[2] == 'vt'
if mpe: mpi4py.profile('mpe', logfile=lfn)
if vt: mpi4py.profile('vt', logfile=lfn)
from mpi4py import MPI
parent = MPI.Comm.Get_parent()
parent.Barrier()
parent.Disconnect()
assert parent == MPI.COMM_NULL
parent = MPI.Comm.Get_parent()
assert parent == MPI.COMM_NULL
mpi4py-3.0.3/test/test_win.py 0000664 0001750 0001750 00000020620 13426006675 017175 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
import sys
try:
sys.getrefcount
except AttributeError:
class getrefcount(object):
def __init__(self, arg):
pass
def __eq__(self, other):
return True
def __add__(self, other):
return self
def __sub__(self, other):
return self
def memzero(m):
try:
m[:] = 0
except IndexError: # cffi buffer
m[0:len(m)] = b'\0'*len(m)
class BaseTestWin(object):
COMM = MPI.COMM_NULL
INFO = MPI.INFO_NULL
CREATE_FLAVOR = MPI.UNDEFINED
def testGetAttr(self):
base = MPI.Get_address(self.memory)
size = len(self.memory)
unit = 1
self.assertEqual(size, self.WIN.Get_attr(MPI.WIN_SIZE))
self.assertEqual(unit, self.WIN.Get_attr(MPI.WIN_DISP_UNIT))
self.assertEqual(base, self.WIN.Get_attr(MPI.WIN_BASE))
def testMemory(self):
memory = self.WIN.tomemory()
pointer = MPI.Get_address(memory)
length = len(memory)
base, size, dunit = self.WIN.attrs
self.assertEqual(size, length)
self.assertEqual(dunit, 1)
self.assertEqual(base, pointer)
def testAttributes(self):
base, size, unit = self.WIN.attrs
self.assertEqual(base, MPI.Get_address(self.memory))
self.assertEqual(size, len(self.memory))
self.assertEqual(unit, 1)
def testGetGroup(self):
cgroup = self.COMM.Get_group()
wgroup = self.WIN.Get_group()
grpcmp = MPI.Group.Compare(cgroup, wgroup)
cgroup.Free()
wgroup.Free()
self.assertEqual(grpcmp, MPI.IDENT)
def testGetSetInfo(self):
#info = MPI.INFO_NULL
#self.WIN.Set_info(info)
info = MPI.Info.Create()
self.WIN.Set_info(info)
info.Free()
info = self.WIN.Get_info()
self.WIN.Set_info(info)
info.Free()
def testGetSetErrhandler(self):
for ERRHANDLER in [MPI.ERRORS_ARE_FATAL, MPI.ERRORS_RETURN,
MPI.ERRORS_ARE_FATAL, MPI.ERRORS_RETURN,]:
errhdl_1 = self.WIN.Get_errhandler()
self.assertNotEqual(errhdl_1, MPI.ERRHANDLER_NULL)
self.WIN.Set_errhandler(ERRHANDLER)
errhdl_2 = self.WIN.Get_errhandler()
self.assertEqual(errhdl_2, ERRHANDLER)
errhdl_2.Free()
self.assertEqual(errhdl_2, MPI.ERRHANDLER_NULL)
self.WIN.Set_errhandler(errhdl_1)
errhdl_1.Free()
self.assertEqual(errhdl_1, MPI.ERRHANDLER_NULL)
def testGetSetName(self):
try:
name = self.WIN.Get_name()
self.WIN.Set_name('mywin')
self.assertEqual(self.WIN.Get_name(), 'mywin')
self.WIN.Set_name(name)
self.assertEqual(self.WIN.Get_name(), name)
except NotImplementedError:
self.skipTest('mpi-win-name')
@unittest.skipIf(MPI.WIN_CREATE_FLAVOR == MPI.KEYVAL_INVALID, 'mpi-win-flavor')
def testCreateFlavor(self):
flavors = (MPI.WIN_FLAVOR_CREATE,
MPI.WIN_FLAVOR_ALLOCATE,
MPI.WIN_FLAVOR_DYNAMIC,
MPI.WIN_FLAVOR_SHARED,)
flavor = self.WIN.Get_attr(MPI.WIN_CREATE_FLAVOR)
self.assertTrue (flavor in flavors)
self.assertEqual(flavor, self.WIN.flavor)
self.assertEqual(flavor, self.CREATE_FLAVOR)
@unittest.skipIf(MPI.WIN_MODEL == MPI.KEYVAL_INVALID, 'mpi-win-model')
def testMemoryModel(self):
models = (MPI.WIN_SEPARATE, MPI.WIN_UNIFIED)
model = self.WIN.Get_attr(MPI.WIN_MODEL)
self.assertTrue(model in models)
self.assertEqual(model, self.WIN.model)
class BaseTestWinCreate(BaseTestWin):
CREATE_FLAVOR = MPI.WIN_FLAVOR_CREATE
def setUp(self):
self.memory = MPI.Alloc_mem(10)
memzero(self.memory)
self.WIN = MPI.Win.Create(self.memory, 1, self.INFO, self.COMM)
def tearDown(self):
self.WIN.Free()
MPI.Free_mem(self.memory)
class BaseTestWinAllocate(BaseTestWin):
CREATE_FLAVOR = MPI.WIN_FLAVOR_ALLOCATE
def setUp(self):
self.WIN = MPI.Win.Allocate(10, 1, self.INFO, self.COMM)
self.memory = self.WIN.tomemory()
memzero(self.memory)
def tearDown(self):
self.WIN.Free()
class BaseTestWinAllocateShared(BaseTestWin):
CREATE_FLAVOR = MPI.WIN_FLAVOR_SHARED
def setUp(self):
self.WIN = MPI.Win.Allocate_shared(10, 1, self.INFO, self.COMM)
self.memory = self.WIN.tomemory()
memzero(self.memory)
def tearDown(self):
self.WIN.Free()
def testSharedQuery(self):
memory = self.WIN.tomemory()
address = MPI.Get_address(memory)
length = len(memory)
memories = self.COMM.allgather((address, length))
rank = self.COMM.Get_rank()
size = self.COMM.Get_size()
for i in range(size):
mem, disp = self.WIN.Shared_query(rank)
base = MPI.Get_address(mem)
size = len(mem)
if i == rank:
self.assertEqual(base, memories[i][0])
self.assertEqual(size, memories[i][1])
self.assertEqual(disp, 1)
class BaseTestWinCreateDynamic(BaseTestWin):
CREATE_FLAVOR = MPI.WIN_FLAVOR_DYNAMIC
def setUp(self):
self.WIN = MPI.Win.Create_dynamic(self.INFO, self.COMM)
def tearDown(self):
self.WIN.Free()
def testGetAttr(self):
base = self.WIN.Get_attr(MPI.WIN_BASE)
size = self.WIN.Get_attr(MPI.WIN_SIZE)
self.assertEqual(base, 0)
self.assertEqual(size, 0)
def testMemory(self):
memory = self.WIN.tomemory()
base = memory.address
size = memory.nbytes
self.assertEqual(base, 0)
self.assertEqual(size, 0)
def testAttributes(self):
base, size, _ = self.WIN.attrs
self.assertEqual(base, 0)
self.assertEqual(size, 0)
@unittest.skipMPI('msmpi(<9.1.0)')
def testAttachDetach(self):
mem1 = MPI.Alloc_mem(8)
mem2 = MPI.Alloc_mem(16)
mem3 = MPI.Alloc_mem(32)
for mem in (mem1, mem2, mem3):
self.WIN.Attach(mem)
self.testMemory()
self.WIN.Detach(mem)
for mem in (mem1, mem2, mem3):
self.WIN.Attach(mem)
self.testMemory()
for mem in (mem1, mem2, mem3):
self.WIN.Detach(mem)
for mem in (mem1, mem2, mem3):
self.WIN.Attach(mem)
self.testMemory()
for mem in (mem3, mem2, mem1):
self.WIN.Detach(mem)
MPI.Free_mem(mem1)
MPI.Free_mem(mem2)
MPI.Free_mem(mem3)
class TestWinCreateSelf(BaseTestWinCreate, unittest.TestCase):
COMM = MPI.COMM_SELF
@unittest.skipMPI('openmpi(<1.4.0)')
class TestWinCreateWorld(BaseTestWinCreate, unittest.TestCase):
COMM = MPI.COMM_WORLD
class TestWinAllocateSelf(BaseTestWinAllocate, unittest.TestCase):
COMM = MPI.COMM_SELF
@unittest.skipMPI('openmpi(<1.4.0)')
class TestWinAllocateWorld(BaseTestWinAllocate, unittest.TestCase):
COMM = MPI.COMM_WORLD
class TestWinAllocateSharedSelf(BaseTestWinAllocateShared, unittest.TestCase):
COMM = MPI.COMM_SELF
class TestWinAllocateSharedWorld(BaseTestWinAllocateShared, unittest.TestCase):
COMM = MPI.COMM_WORLD
class TestWinCreateDynamicSelf(BaseTestWinCreateDynamic, unittest.TestCase):
COMM = MPI.COMM_SELF
class TestWinCreateDynamicWorld(BaseTestWinCreateDynamic, unittest.TestCase):
COMM = MPI.COMM_WORLD
SpectrumMPI = MPI.get_vendor()[0] == 'Spectrum MPI'
try:
if SpectrumMPI: raise NotImplementedError
MPI.Win.Create(MPI.BOTTOM, 1, MPI.INFO_NULL, MPI.COMM_SELF).Free()
except (NotImplementedError, MPI.Exception):
unittest.disable(BaseTestWinCreate, 'mpi-win-create')
try:
if SpectrumMPI: raise NotImplementedError
MPI.Win.Allocate(1, 1, MPI.INFO_NULL, MPI.COMM_SELF).Free()
except (NotImplementedError, MPI.Exception):
unittest.disable(BaseTestWinAllocate, 'mpi-win-allocate')
try:
if SpectrumMPI: raise NotImplementedError
MPI.Win.Allocate_shared(1, 1, MPI.INFO_NULL, MPI.COMM_SELF).Free()
except (NotImplementedError, MPI.Exception):
unittest.disable(BaseTestWinAllocateShared, 'mpi-win-shared')
try:
if SpectrumMPI: raise NotImplementedError
MPI.Win.Create_dynamic(MPI.INFO_NULL, MPI.COMM_SELF).Free()
except (NotImplementedError, MPI.Exception):
unittest.disable(BaseTestWinCreateDynamic, 'mpi-win-dynamic')
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_memory.py 0000664 0001750 0001750 00000020653 13426006675 017716 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
import sys
try:
from array import array
except ImportError:
array = None
pypy_lt_58 = (hasattr(sys, 'pypy_version_info') and
sys.pypy_version_info < (5, 8))
class TestMemory(unittest.TestCase):
def testNewEmpty(self):
memory = MPI.memory
mem = memory()
self.assertEqual(mem.address, 0)
self.assertEqual(mem.nbytes, 0)
self.assertFalse(mem.readonly)
self.assertEqual(len(mem), 0)
mem[:] = 0
mem[:] = memory()
if sys.version_info < (3,0):
b = buffer(mem)
self.assertEqual(len(b), 0)
if sys.version_info >= (2,7):
m = memoryview(mem)
self.assertEqual(m.format, 'B')
self.assertEqual(m.itemsize, 1)
self.assertEqual(m.ndim, 1)
if not pypy_lt_58:
self.assertEqual(m.readonly, False)
self.assertEqual(m.shape, (0,))
self.assertEqual(m.strides, (1,))
self.assertEqual(m.tobytes(), b"")
self.assertEqual(m.tolist(), [])
mem.release()
self.assertEqual(mem.address, 0)
self.assertEqual(mem.nbytes, 0)
self.assertFalse(mem.readonly)
def testFromBufferBytes(self):
memory = MPI.memory
mem = memory.frombuffer(b"abc", readonly=True)
self.assertNotEqual(mem.address, 0)
self.assertEqual(mem.nbytes, 3)
self.assertTrue(mem.readonly)
self.assertEqual(len(mem), 3)
if sys.version_info < (3,0):
b = buffer(mem)
self.assertEqual(len(b), 3)
if sys.version_info >= (2,7):
m = memoryview(mem)
self.assertEqual(m.format, 'B')
self.assertEqual(m.itemsize, 1)
self.assertEqual(m.ndim, 1)
if not pypy_lt_58:
self.assertEqual(m.readonly, True)
self.assertEqual(m.shape, (3,))
self.assertEqual(m.strides, (1,))
self.assertEqual(m.tobytes(), b"abc")
self.assertEqual(m.tolist(), [ord(c) for c in "abc"])
mem.release()
self.assertEqual(mem.address, 0)
self.assertEqual(mem.nbytes, 0)
self.assertFalse(mem.readonly)
@unittest.skipIf(array is None, 'array')
def testFromBufferArrayRO(self):
memory = MPI.memory
obj = array('B', [1,2,3])
mem = memory.frombuffer(obj, readonly=True)
self.assertNotEqual(mem.address, 0)
self.assertEqual(mem.nbytes, 3)
self.assertTrue(mem.readonly)
self.assertEqual(len(mem), 3)
if sys.version_info < (3,0):
b = buffer(mem)
self.assertEqual(len(b), 3)
if sys.version_info >= (2,7):
m = memoryview(mem)
self.assertEqual(m.format, 'B')
self.assertEqual(m.itemsize, 1)
self.assertEqual(m.ndim, 1)
if not pypy_lt_58:
self.assertEqual(m.readonly, True)
self.assertEqual(m.shape, (3,))
self.assertEqual(m.strides, (1,))
self.assertEqual(m.tobytes(), b"\1\2\3")
self.assertEqual(m.tolist(), [1,2,3])
mem.release()
self.assertEqual(mem.address, 0)
self.assertEqual(mem.nbytes, 0)
self.assertFalse(mem.readonly)
@unittest.skipIf(array is None, 'array')
def testFromBufferArrayRW(self):
memory = MPI.memory
obj = array('B', [1,2,3])
mem = memory.frombuffer(obj, readonly=False)
self.assertNotEqual(mem.address, 0)
self.assertEqual(mem.nbytes, 3)
self.assertFalse(mem.readonly)
self.assertEqual(len(mem), 3)
if sys.version_info < (3,0):
b = buffer(mem)
self.assertEqual(len(b), 3)
if sys.version_info >= (2,7):
m = memoryview(mem)
self.assertEqual(m.format, 'B')
self.assertEqual(m.itemsize, 1)
self.assertEqual(m.ndim, 1)
if not pypy_lt_58:
self.assertEqual(m.readonly, False)
self.assertEqual(m.shape, (3,))
self.assertEqual(m.strides, (1,))
self.assertEqual(m.tobytes(), b"\1\2\3")
self.assertEqual(m.tolist(), [1,2,3])
mem[:] = 1
self.assertEqual(obj, array('B', [1]*3))
mem[1:] = array('B', [7]*2)
self.assertEqual(obj, array('B', [1,7,7]))
mem[1:2] = array('B', [8]*1)
self.assertEqual(obj, array('B', [1,8,7]))
mem.release()
self.assertEqual(mem.address, 0)
self.assertEqual(mem.nbytes, 0)
self.assertFalse(mem.readonly)
@unittest.skipIf(array is None, 'array')
def testFromAddress(self):
memory = MPI.memory
obj = array('B', [1,2,3])
addr, size = obj.buffer_info()
nbytes = size * obj.itemsize
mem = memory.fromaddress(addr, nbytes, readonly=False)
self.assertNotEqual(mem.address, 0)
self.assertEqual(mem.nbytes, 3)
self.assertFalse(mem.readonly)
self.assertEqual(len(mem), 3)
if sys.version_info < (3,0):
b = buffer(mem)
self.assertEqual(len(b), 3)
if sys.version_info >= (2,7):
m = memoryview(mem)
self.assertEqual(m.format, 'B')
self.assertEqual(m.itemsize, 1)
self.assertEqual(m.ndim, 1)
if not pypy_lt_58:
self.assertEqual(m.readonly, False)
self.assertEqual(m.shape, (3,))
self.assertEqual(m.strides, (1,))
self.assertEqual(m.tobytes(), b"\1\2\3")
self.assertEqual(m.tolist(), [1,2,3])
mem[:] = 1
self.assertEqual(obj, array('B', [1]*3))
mem[1:] = array('B', [7]*2)
self.assertEqual(obj, array('B', [1,7,7]))
mem[1:2] = array('B', [8]*1)
self.assertEqual(obj, array('B', [1,8,7]))
mem.release()
self.assertEqual(mem.address, 0)
self.assertEqual(mem.nbytes, 0)
self.assertFalse(mem.readonly)
def testSequence(self):
n = 16
try:
mem = MPI.Alloc_mem(n, MPI.INFO_NULL)
except NotImplementedError:
self.skipTest('mpi-alloc_mem')
try:
self.assertTrue(type(mem) is MPI.memory)
self.assertTrue(mem.address != 0)
self.assertEqual(mem.nbytes, n)
self.assertFalse(mem.readonly)
self.assertEqual(len(mem), n)
def delitem(): del mem[n]
def getitem1(): return mem[n]
def getitem2(): return mem[::2]
def getitem3(): return mem[None]
def setitem1(): mem[n] = 0
def setitem2(): mem[::2] = 0
def setitem3(): mem[None] = 0
self.assertRaises(Exception, delitem)
self.assertRaises(IndexError, getitem1)
self.assertRaises(IndexError, getitem2)
self.assertRaises(TypeError, getitem3)
self.assertRaises(IndexError, setitem1)
self.assertRaises(IndexError, setitem2)
self.assertRaises(TypeError, setitem3)
for i in range(n):
mem[i] = i
for i in range(n):
self.assertEqual(mem[i], i)
mem[:] = 0
for i in range(n):
self.assertEqual(mem[i], 0)
mem[:] = 255
for i in range(n):
self.assertEqual(mem[i], 255)
mem[:n//2] = 1
mem[n//2:] = 0
for i in range(n//2):
self.assertEqual(mem[i], 1)
for i in range(n//2, n):
self.assertEqual(mem[i], 0)
mem[:] = 0
mem[1:5] = b"abcd"
mem[10:13] = b"xyz"
self.assertEqual(mem[0], 0)
for i, c in enumerate("abcd"):
self.assertEqual(mem[1+i], ord(c))
for i in range(5, 10):
self.assertEqual(mem[i], 0)
for i, c in enumerate("xyz"):
self.assertEqual(mem[10+i], ord(c))
for i in range(13, n):
self.assertEqual(mem[i], 0)
self.assertEqual(mem[1:5].tobytes(), b"abcd")
self.assertEqual(mem[10:13].tobytes(), b"xyz")
finally:
MPI.Free_mem(mem)
self.assertEqual(mem.address, 0)
self.assertEqual(mem.nbytes, 0)
self.assertFalse(mem.readonly)
try:
MPI.memory
except AttributeError:
unittest.disable(TestMemory, 'mpi4py-memory')
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_datatype.py 0000644 0001750 0001750 00000036267 13200562156 020216 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
import sys
datatypes_c = [
MPI.CHAR, MPI.WCHAR,
MPI.SIGNED_CHAR, MPI.SHORT, MPI.INT, MPI.LONG,
MPI.UNSIGNED_CHAR, MPI.UNSIGNED_SHORT, MPI.UNSIGNED, MPI.UNSIGNED_LONG,
MPI.LONG_LONG, MPI.UNSIGNED_LONG_LONG,
MPI.FLOAT, MPI.DOUBLE, MPI.LONG_DOUBLE,
]
datatypes_c99 = [
MPI.C_BOOL,
MPI.INT8_T, MPI.INT16_T, MPI.INT32_T, MPI.INT64_T,
MPI.UINT8_T, MPI.UINT16_T, MPI.UINT32_T, MPI.UINT64_T,
MPI.C_COMPLEX, MPI.C_FLOAT_COMPLEX,
MPI.C_DOUBLE_COMPLEX, MPI.C_LONG_DOUBLE_COMPLEX,
]
datatypes_f = [
MPI.CHARACTER, MPI.LOGICAL, MPI.INTEGER,
MPI.REAL, MPI.DOUBLE_PRECISION,
MPI.COMPLEX, MPI.DOUBLE_COMPLEX,
]
datatypes_f90 = [
MPI.LOGICAL1, MPI.LOGICAL2, MPI.LOGICAL4, MPI.LOGICAL8,
MPI.INTEGER1, MPI.INTEGER2, MPI.INTEGER4, MPI.INTEGER8, MPI.INTEGER16,
MPI.REAL2, MPI.REAL4, MPI.REAL8, MPI.REAL16,
MPI.COMPLEX4, MPI.COMPLEX8, MPI.COMPLEX16, MPI.COMPLEX32,
]
datatypes_mpi = [
MPI.PACKED, MPI.BYTE, MPI.AINT, MPI.OFFSET,
]
datatypes = []
datatypes += datatypes_c
datatypes += datatypes_c99
datatypes += datatypes_f
datatypes += datatypes_f90
datatypes += datatypes_mpi
datatypes = [t for t in datatypes if t != MPI.DATATYPE_NULL]
combiner_map = {}
class TestDatatype(unittest.TestCase):
def testBoolEqNe(self):
for dtype in datatypes:
self.assertTrue (not not dtype)
self.assertTrue (dtype == MPI.Datatype(dtype))
self.assertFalse(dtype != MPI.Datatype(dtype))
def testGetExtent(self):
for dtype in datatypes:
lb, ext = dtype.Get_extent()
self.assertEqual(dtype.lb, lb)
self.assertEqual(dtype.ub, lb+ext)
self.assertEqual(dtype.extent, ext)
def testGetSize(self):
for dtype in datatypes:
size = dtype.Get_size()
self.assertTrue(dtype.size, size)
def testGetTrueExtent(self):
for dtype in datatypes:
try:
lb, ext = dtype.Get_true_extent()
self.assertEqual(dtype.true_lb, lb)
self.assertEqual(dtype.true_ub, lb+ext)
self.assertEqual(dtype.true_extent, ext)
except NotImplementedError:
self.skipTest('mpi-type-get_true_extent')
def testGetEnvelope(self):
for dtype in datatypes:
try:
envelope = dtype.Get_envelope()
except NotImplementedError:
self.skipTest('mpi-type-get_envelope')
if ('LAM/MPI' == MPI.get_vendor()[0] and
"COMPLEX" in dtype.name): continue
ni, na, nd, combiner = envelope
self.assertEqual(combiner, MPI.COMBINER_NAMED)
self.assertEqual(ni, 0)
self.assertEqual(na, 0)
self.assertEqual(nd, 0)
self.assertEqual(dtype.envelope, envelope)
self.assertEqual(dtype.combiner, combiner)
self.assertTrue(dtype.is_named)
self.assertTrue(dtype.is_predefined)
otype = dtype.decode()
self.assertTrue(dtype is otype)
def check_datatype_contents(self, oldtype, factory, newtype):
try:
envelope = newtype.Get_envelope()
contents = newtype.Get_contents()
except NotImplementedError:
self.skipTest('mpi-type-get_envelope')
ni, na, nd, combiner = envelope
i, a, d = contents
self.assertEqual(ni, len(i))
self.assertEqual(na, len(a))
self.assertEqual(nd, len(d))
self.assertTrue(combiner != MPI.COMBINER_NAMED)
self.assertEqual(newtype.envelope, envelope)
self.assertEqual(newtype.contents, contents)
self.assertEqual(newtype.combiner, combiner)
self.assertFalse(newtype.is_named)
if combiner in (MPI.COMBINER_F90_INTEGER,
MPI.COMBINER_F90_REAL,
MPI.COMBINER_F90_COMPLEX,):
self.assertTrue(newtype.is_predefined)
else:
self.assertFalse(newtype.is_predefined)
name = factory.__name__
NAME = name.replace('Create_', '').upper()
symbol = getattr(MPI, 'COMBINER_' + NAME)
if symbol == MPI.UNDEFINED: return
if combiner_map is None: return
symbol = combiner_map.get(symbol, symbol)
if symbol is None: return
self.assertEqual(symbol, combiner)
decoded = newtype.decode()
oldtype, constructor, kargs = decoded
constructor = 'Create_' + constructor.lower()
newtype2 = getattr(oldtype, constructor)(**kargs)
decoded2 = newtype2.decode()
self.assertEqual(decoded[1], decoded2[1])
self.assertEqual(decoded[2], decoded2[2])
if combiner not in (MPI.COMBINER_F90_INTEGER,
MPI.COMBINER_F90_REAL,
MPI.COMBINER_F90_COMPLEX,):
self.assertFalse(newtype2.is_predefined)
newtype2.Free()
else:
self.assertTrue(newtype2.is_predefined)
def check_datatype(self, oldtype, factory, *args):
try:
if isinstance(oldtype, MPI.Datatype):
newtype = factory(oldtype, *args)
else:
newtype = factory(*args)
except NotImplementedError:
self.skipTest('mpi-type-constructor')
self.check_datatype_contents(oldtype, factory, newtype)
newtype.Commit()
self.check_datatype_contents(oldtype, factory, newtype)
combiner = newtype.Get_envelope()[-1]
if combiner not in (MPI.COMBINER_F90_INTEGER,
MPI.COMBINER_F90_REAL,
MPI.COMBINER_F90_COMPLEX,):
newtype.Free()
def testDup(self):
for dtype in datatypes:
factory = MPI.Datatype.Dup
self.check_datatype(dtype, factory)
def testCreateContiguous(self):
for dtype in datatypes:
for count in range(5):
factory = MPI.Datatype.Create_contiguous
args = (count, )
self.check_datatype(dtype, factory, *args)
def testCreateVector(self):
for dtype in datatypes:
for count in range(5):
for blocklength in range(5):
for stride in range(5):
factory = MPI.Datatype.Create_vector
args = (count, blocklength, stride)
self.check_datatype(dtype, factory, *args)
def testCreateHvector(self):
for dtype in datatypes:
for count in range(5):
for blocklength in range(5):
for stride in range(5):
factory = MPI.Datatype.Create_hvector
args = (count, blocklength, stride)
self.check_datatype(dtype, factory, *args)
def testCreateIndexed(self):
for dtype in datatypes:
for block in range(5):
blocklengths = list(range(block, block+5))
displacements = [0]
for b in blocklengths[:-1]:
stride = displacements[-1] + b * dtype.extent + 1
displacements.append(stride)
factory = MPI.Datatype.Create_indexed
args = (blocklengths, displacements)
self.check_datatype(dtype, factory, *args)
#args = (block, displacements) XXX
#self.check_datatype(dtype, factory, *args) XXX
def testCreateIndexedBlock(self):
for dtype in datatypes:
for block in range(5):
blocklengths = list(range(block, block+5))
displacements = [0]
for b in blocklengths[:-1]:
stride = displacements[-1] + b * dtype.extent + 1
displacements.append(stride)
factory = MPI.Datatype.Create_indexed_block
args = (block, displacements)
self.check_datatype(dtype, factory, *args)
def testCreateHindexed(self):
for dtype in datatypes:
for block in range(5):
blocklengths = list(range(block, block+5))
displacements = [0]
for b in blocklengths[:-1]:
stride = displacements[-1] + b * dtype.extent + 1
displacements.append(stride)
factory = MPI.Datatype.Create_hindexed
args = (blocklengths, displacements)
self.check_datatype(dtype, factory, *args)
#args = (block, displacements) XXX
#self.check_datatype(dtype, factory, *args) XXX
@unittest.skipMPI('openmpi(<=1.8.1)', MPI.VERSION == 3)
def testCreateHindexedBlock(self):
for dtype in datatypes:
for block in range(5):
displacements = [0]
for i in range(5):
stride = displacements[-1] + block * dtype.extent + 1
displacements.append(stride)
factory = MPI.Datatype.Create_hindexed_block
args = (block, displacements)
self.check_datatype(dtype, factory, *args)
def testCreateStruct(self):
for dtype1 in datatypes:
for dtype2 in datatypes:
dtypes = (dtype1, dtype2)
blocklengths = (2, 3)
displacements = [0]
for dtype in dtypes[:-1]:
stride = displacements[-1] + dtype.extent
displacements.append(stride)
factory = MPI.Datatype.Create_struct
args = (blocklengths, displacements, dtypes)
self.check_datatype(dtypes, factory, *args)
def testCreateSubarray(self):
for dtype in datatypes:
for ndim in range(1, 5):
for size in range(1, 5):
for subsize in range(1, size):
for start in range(size-subsize):
for order in [MPI.ORDER_C,
MPI.ORDER_FORTRAN,
MPI.ORDER_F,
]:
sizes = [size] * ndim
subsizes = [subsize] * ndim
starts = [start] * ndim
factory = MPI.Datatype.Create_subarray
args = sizes, subsizes, starts, order
self.check_datatype(dtype, factory, *args)
def testCreateDarray(self):
for dtype in datatypes:
for ndim in range(1, 3+1):
for size in (4, 8, 9, 27):
for rank in (0, size-1):
for dist in [MPI.DISTRIBUTE_BLOCK, MPI.DISTRIBUTE_CYCLIC]:
for order in [MPI.ORDER_C, MPI.ORDER_F]:
gsizes = [size]*ndim
distribs = [dist]*ndim
dargs = [MPI.DISTRIBUTE_DFLT_DARG]*ndim
psizes = MPI.Compute_dims(size, [0]*ndim)
factory = MPI.Datatype.Create_darray
args = size, rank, gsizes, distribs, dargs, psizes, order
self.check_datatype(dtype, factory, *args)
def testCreateF90Integer(self):
for r in (1, 2, 4):
factory = MPI.Datatype.Create_f90_integer
args = (r,)
self.check_datatype(None, factory, *args)
@unittest.skipMPI('openmpi(<3.0.0)')
@unittest.skipMPI('msmpi')
@unittest.skipMPI('SpectrumMPI')
def testCreateF90RealSingle(self):
(p, r) = (6, 30)
factory = MPI.Datatype.Create_f90_real
args = (p, r)
self.check_datatype(None, factory, *args)
@unittest.skipMPI('openmpi(<3.0.0)')
@unittest.skipMPI('msmpi')
@unittest.skipMPI('SpectrumMPI')
def testCreateF90RealDouble(self):
(p, r) = (15, 300)
factory = MPI.Datatype.Create_f90_real
args = (p, r)
self.check_datatype(None, factory, *args)
@unittest.skipMPI('openmpi(<3.0.0)')
@unittest.skipMPI('msmpi')
@unittest.skipMPI('SpectrumMPI')
def testCreatef90ComplexSingle(self):
(p, r) = (6, 30)
factory = MPI.Datatype.Create_f90_complex
args = (p, r)
self.check_datatype(None, factory, *args)
@unittest.skipMPI('openmpi(<3.0.0)')
@unittest.skipMPI('msmpi')
@unittest.skipMPI('SpectrumMPI')
def testCreateF90ComplexDouble(self):
(p, r) = (15, 300)
factory = MPI.Datatype.Create_f90_complex
args = (p, r)
self.check_datatype(None, factory, *args)
match_size_integer = [1, 2, 4, 8]
match_size_real = [4, 8]
match_size_complex = [8, 16]
@unittest.skipMPI('MPI(<2.0)')
@unittest.skipMPI('openmpi', MPI.CHARACTER.Get_size() == 0)
def testMatchSize(self):
typeclass = MPI.TYPECLASS_INTEGER
for size in self.match_size_integer:
datatype = MPI.Datatype.Match_size(typeclass, size)
self.assertEqual(size, datatype.size)
typeclass = MPI.TYPECLASS_REAL
for size in self.match_size_real:
datatype = MPI.Datatype.Match_size(typeclass, size)
self.assertEqual(size, datatype.size)
typeclass = MPI.TYPECLASS_COMPLEX
for size in self.match_size_complex:
datatype = MPI.Datatype.Match_size(typeclass, size)
self.assertEqual(size, datatype.size)
def testCreateResized(self):
for dtype in datatypes:
for lb in range(-10, 10):
for extent in range(1, 10):
factory = MPI.Datatype.Create_resized
args = lb, extent
self.check_datatype(dtype, factory, *args)
def testGetSetName(self):
for dtype in datatypes:
try:
name = dtype.Get_name()
self.assertTrue(name)
dtype.Set_name(name)
self.assertEqual(name, dtype.Get_name())
except NotImplementedError:
self.skipTest('mpi-type-name')
def testCommit(self):
for dtype in datatypes:
dtype.Commit()
name, version = MPI.get_vendor()
if name == 'LAM/MPI':
combiner_map[MPI.COMBINER_INDEXED_BLOCK] = MPI.COMBINER_INDEXED
elif name == 'MPICH1':
combiner_map[MPI.COMBINER_VECTOR] = None
combiner_map[MPI.COMBINER_HVECTOR] = None
combiner_map[MPI.COMBINER_INDEXED] = None
combiner_map[MPI.COMBINER_HINDEXED_BLOCK] = None
for t in datatypes_f: datatypes.remove(t)
elif MPI.Get_version() < (2,0):
combiner_map = None
if name == 'Open MPI':
if MPI.CHARACTER.Get_size() == 0:
for dtype in datatypes_f + datatypes_f90:
if dtype and dtype in datatypes:
datatypes.remove(dtype)
if (1,6,0) < version < (1,7,0):
TestDatatype.match_size_complex[:] = []
if version < (1,5,2):
for t in datatypes_f90[-4:]:
if t != MPI.DATATYPE_NULL:
datatypes.remove(t)
if name == 'Platform MPI':
combiner_map[MPI.COMBINER_INDEXED_BLOCK] = MPI.COMBINER_INDEXED
combiner_map[MPI.COMBINER_DARRAY] = MPI.COMBINER_STRUCT
combiner_map[MPI.COMBINER_SUBARRAY] = MPI.COMBINER_STRUCT
TestDatatype.match_size_complex[:] = []
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_group.py 0000644 0001750 0001750 00000014147 13200562156 017530 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
class BaseTestGroup(object):
def testProperties(self):
group = self.GROUP
self.assertEqual(group.Get_size(), group.size)
self.assertEqual(group.Get_rank(), group.rank)
def testCompare(self):
results = (MPI.IDENT, MPI.SIMILAR, MPI.UNEQUAL)
group = MPI.COMM_WORLD.Get_group()
gcmp = MPI.Group.Compare(self.GROUP, group)
group.Free()
self.assertTrue(gcmp in results)
gcmp = MPI.Group.Compare(self.GROUP, self.GROUP)
self.assertEqual(gcmp, MPI.IDENT)
def testDup(self):
group = self.GROUP.Dup()
self.assertEqual(MPI.Group.Compare(group, self.GROUP), MPI.IDENT)
group.Free()
def testUnion(self):
group = MPI.Group.Union(MPI.GROUP_EMPTY, self.GROUP)
self.assertEqual(MPI.Group.Compare(group, self.GROUP), MPI.IDENT)
group.Free()
group = MPI.Group.Union(self.GROUP, MPI.GROUP_EMPTY)
self.assertEqual(MPI.Group.Compare(group, self.GROUP), MPI.IDENT)
group.Free()
group = MPI.Group.Union(self.GROUP, self.GROUP)
self.assertEqual(MPI.Group.Compare(group, self.GROUP), MPI.IDENT)
group.Free()
def testDifference(self):
group = MPI.Group.Difference(MPI.GROUP_EMPTY, self.GROUP)
self.assertEqual(MPI.Group.Compare(group, MPI.GROUP_EMPTY), MPI.IDENT)
group.Free()
group = MPI.Group.Difference(self.GROUP, MPI.GROUP_EMPTY)
self.assertEqual(MPI.Group.Compare(group, self.GROUP), MPI.IDENT)
group.Free()
group = MPI.Group.Difference(self.GROUP, self.GROUP)
self.assertEqual(MPI.Group.Compare(group, MPI.GROUP_EMPTY), MPI.IDENT)
group.Free()
def testIntersection(self):
group = MPI.Group.Intersection(MPI.GROUP_EMPTY, self.GROUP)
self.assertEqual(MPI.Group.Compare(group, MPI.GROUP_EMPTY), MPI.IDENT)
group.Free()
group = MPI.Group.Intersection(self.GROUP, MPI.GROUP_EMPTY)
self.assertEqual(MPI.Group.Compare(group, MPI.GROUP_EMPTY), MPI.IDENT)
group.Free()
group = MPI.Group.Intersection(self.GROUP, self.GROUP)
self.assertEqual(MPI.Group.Compare(group, self.GROUP), MPI.IDENT)
group.Free()
def testIncl(self):
group = self.GROUP.Incl([])
self.assertEqual(MPI.Group.Compare(group, MPI.GROUP_EMPTY), MPI.IDENT)
group.Free()
def testExcl(self):
group = self.GROUP.Excl([])
self.assertEqual(MPI.Group.Compare(group, self.GROUP), MPI.IDENT)
group.Free()
def testRangeIncl(self):
if self.GROUP == MPI.GROUP_EMPTY: return
group = self.GROUP.Range_incl([])
self.assertEqual(MPI.Group.Compare(group, MPI.GROUP_EMPTY), MPI.IDENT)
group.Free()
ranges = [ (0, self.GROUP.Get_size()-1, 1), ]
group = self.GROUP.Range_incl(ranges)
self.assertEqual(MPI.Group.Compare(group, self.GROUP), MPI.IDENT)
group.Free()
def testRangeExcl(self):
if self.GROUP == MPI.GROUP_EMPTY: return
group = self.GROUP.Range_excl([])
self.assertEqual(MPI.Group.Compare(group, self.GROUP), MPI.IDENT)
group.Free()
ranges = [ (0, self.GROUP.Get_size()-1, 1), ]
group = self.GROUP.Range_excl(ranges)
self.assertEqual(MPI.Group.Compare(group, MPI.GROUP_EMPTY), MPI.IDENT)
group.Free()
def testTranslRanks(self):
group1 = self.GROUP
group2 = self.GROUP
ranks1 = list(range(group1.Get_size())) * 3
ranks2 = MPI.Group.Translate_ranks(group1, ranks1)
ranks2 = MPI.Group.Translate_ranks(group1, ranks1, group2)
self.assertEqual(list(ranks1), list(ranks2))
@unittest.skipMPI('PlatformMPI')
@unittest.skipMPI('MPICH1')
@unittest.skipMPI('LAM/MPI')
def testTranslRanksProcNull(self):
if self.GROUP == MPI.GROUP_EMPTY: return
group1 = self.GROUP
group2 = self.GROUP
ranks1 = [MPI.PROC_NULL] * 10
ranks2 = MPI.Group.Translate_ranks(group1, ranks1, group2)
self.assertEqual(list(ranks1), list(ranks2))
def testTranslRanksGroupEmpty(self):
if self.GROUP == MPI.GROUP_EMPTY: return
group1 = self.GROUP
group2 = MPI.GROUP_EMPTY
ranks1 = list(range(group1.Get_size())) * 2
ranks2 = MPI.Group.Translate_ranks(group1, ranks1, group2)
for rank in ranks2:
self.assertEqual(rank, MPI.UNDEFINED)
class TestGroupNull(unittest.TestCase):
def testContructor(self):
group = MPI.Group()
self.assertFalse(group is MPI.GROUP_NULL)
self.assertEqual(group, MPI.GROUP_NULL)
def testNull(self):
GROUP_NULL = MPI.GROUP_NULL
group_null = MPI.Group()
self.assertFalse(GROUP_NULL)
self.assertFalse(group_null)
self.assertEqual(group_null, GROUP_NULL)
class TestGroupEmpty(BaseTestGroup, unittest.TestCase):
def setUp(self):
self.GROUP = MPI.GROUP_EMPTY
def testEmpty(self):
self.assertTrue(self.GROUP)
def testSize(self):
size = self.GROUP.Get_size()
self.assertEqual(size, 0)
def testRank(self):
rank = self.GROUP.Get_rank()
self.assertEqual(rank, MPI.UNDEFINED)
@unittest.skipMPI('MPICH1')
def testTranslRanks(self):
super(TestGroupEmpty, self).testTranslRanks()
class TestGroupSelf(BaseTestGroup, unittest.TestCase):
def setUp(self):
self.GROUP = MPI.COMM_SELF.Get_group()
def tearDown(self):
self.GROUP.Free()
def testSize(self):
size = self.GROUP.Get_size()
self.assertEqual(size, 1)
def testRank(self):
rank = self.GROUP.Get_rank()
self.assertEqual(rank, 0)
class TestGroupWorld(BaseTestGroup, unittest.TestCase):
def setUp(self):
self.GROUP = MPI.COMM_WORLD.Get_group()
def tearDown(self):
self.GROUP.Free()
def testSize(self):
size = self.GROUP.Get_size()
self.assertTrue(size >= 1)
def testRank(self):
size = self.GROUP.Get_size()
rank = self.GROUP.Get_rank()
self.assertTrue(rank >= 0 and rank < size)
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_cco_ngh_buf.py 0000644 0001750 0001750 00000016562 13200562156 020633 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
import arrayimpl
def create_topo_comms(comm):
size = comm.Get_size()
rank = comm.Get_rank()
# Cartesian
n = int(size**1/2.0)
m = int(size**1/3.0)
if m*m*m == size:
dims = [m, m, m]
elif n*n == size:
dims = [n, n]
else:
dims = [size]
periods = [True] * len(dims)
yield comm.Create_cart(dims, periods=periods)
# Graph
index, edges = [0], []
for i in range(size):
pos = index[-1]
index.append(pos+2)
edges.append((i-1)%size)
edges.append((i+1)%size)
yield comm.Create_graph(index, edges)
# Dist Graph
sources = [(rank-2)%size, (rank-1)%size]
destinations = [(rank+1)%size, (rank+2)%size]
yield comm.Create_dist_graph_adjacent(sources, destinations)
def get_neighbors_count(comm):
topo = comm.Get_topology()
if topo == MPI.CART:
ndim = comm.Get_dim()
return 2*ndim, 2*ndim
if topo == MPI.GRAPH:
rank = comm.Get_rank()
nneighbors = comm.Get_neighbors_count(rank)
return nneighbors, nneighbors
if topo == MPI.DIST_GRAPH:
indeg, outdeg, w = comm.Get_dist_neighbors_count()
return indeg, outdeg
return 0, 0
def have_feature():
cartcomm = MPI.COMM_SELF.Create_cart([1], periods=[0])
try:
cartcomm.neighbor_allgather(None)
return True
except NotImplementedError:
return False
finally:
cartcomm.Free()
@unittest.skipIf(not have_feature(), 'mpi-neighbor')
class BaseTestCCONghBuf(object):
COMM = MPI.COMM_NULL
def testNeighborAllgather(self):
for comm in create_topo_comms(self.COMM):
rsize, ssize = get_neighbors_count(comm)
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for v in range(3):
sbuf = array( v, typecode, 3)
rbuf = array(-1, typecode, (rsize, 3))
comm.Neighbor_allgather(sbuf.as_mpi(), rbuf.as_mpi())
for value in rbuf.flat:
self.assertEqual(value, v)
sbuf = array( v, typecode, 3)
rbuf = array(-1, typecode, (rsize, 3))
comm.Neighbor_allgatherv(sbuf.as_mpi_c(3), rbuf.as_mpi_c(3))
for value in rbuf.flat:
self.assertEqual(value, v)
sbuf = array( v, typecode, 3)
rbuf = array(-1, typecode, (rsize, 3))
comm.Ineighbor_allgather(sbuf.as_mpi(), rbuf.as_mpi()).Wait()
for value in rbuf.flat:
self.assertEqual(value, v)
sbuf = array( v, typecode, 3)
rbuf = array(-1, typecode, (rsize, 3))
comm.Ineighbor_allgatherv(sbuf.as_mpi_c(3), rbuf.as_mpi_c(3)).Wait()
for value in rbuf.flat:
self.assertEqual(value, v)
comm.Free()
def testNeighborAlltoall(self):
for comm in create_topo_comms(self.COMM):
rsize, ssize = get_neighbors_count(comm)
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for v in range(3):
sbuf = array( v, typecode, (ssize, 3))
rbuf = array(-1, typecode, (rsize, 3))
comm.Neighbor_alltoall(sbuf.as_mpi(), rbuf.as_mpi_c(3))
for value in rbuf.flat:
self.assertEqual(value, v)
sbuf = array( v, typecode, (ssize, 3))
rbuf = array(-1, typecode, (rsize, 3))
comm.Neighbor_alltoall(sbuf.as_mpi(), rbuf.as_mpi())
for value in rbuf.flat:
self.assertEqual(value, v)
sbuf = array( v, typecode, (ssize, 3))
rbuf = array(-1, typecode, (rsize, 3))
comm.Neighbor_alltoallv(sbuf.as_mpi_c(3), rbuf.as_mpi_c(3))
for value in rbuf.flat:
self.assertEqual(value, v)
sbuf = array( v, typecode, (ssize, 3))
rbuf = array(-1, typecode, (rsize, 3))
comm.Ineighbor_alltoall(sbuf.as_mpi(), rbuf.as_mpi()).Wait()
for value in rbuf.flat:
self.assertEqual(value, v)
sbuf = array( v, typecode, (ssize, 3))
rbuf = array(-1, typecode, (rsize, 3))
comm.Ineighbor_alltoallv(sbuf.as_mpi_c(3), rbuf.as_mpi_c(3)).Wait()
for value in rbuf.flat:
self.assertEqual(value, v)
comm.Free()
def testNeighborAlltoallw(self):
size = self.COMM.Get_size()
for comm in create_topo_comms(self.COMM):
rsize, ssize = get_neighbors_count(comm)
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for n in range(1,4):
for v in range(3):
sbuf = array( v, typecode, (ssize, n))
rbuf = array(-1, typecode, (rsize, n))
sdt, rdt = sbuf.mpidtype, rbuf.mpidtype
sdsp = list(range(0, ssize*n*sdt.extent, n*sdt.extent))
rdsp = list(range(0, rsize*n*rdt.extent, n*rdt.extent))
smsg = [sbuf.as_raw(), ([n]*ssize, sdsp), [sdt]*ssize]
rmsg = (rbuf.as_raw(), ([n]*rsize, rdsp), [rdt]*rsize)
try:
comm.Neighbor_alltoallw(smsg, rmsg)
except NotImplementedError:
self.skipTest('mpi-neighbor_alltoallw')
for value in rbuf.flat:
self.assertEqual(value, v)
smsg[0] = array(v+1, typecode, (ssize, n)).as_raw()
try:
comm.Ineighbor_alltoallw(smsg, rmsg).Wait()
except NotImplementedError:
self.skipTest('mpi-ineighbor_alltoallw')
for value in rbuf.flat:
self.assertEqual(value, v+1)
comm.Free()
class TestCCONghBufSelf(BaseTestCCONghBuf, unittest.TestCase):
COMM = MPI.COMM_SELF
class TestCCONghBufWorld(BaseTestCCONghBuf, unittest.TestCase):
COMM = MPI.COMM_WORLD
class TestCCONghBufSelfDup(TestCCONghBufSelf):
def setUp(self):
self.COMM = MPI.COMM_SELF.Dup()
def tearDown(self):
self.COMM.Free()
class TestCCONghBufWorldDup(TestCCONghBufWorld):
def setUp(self):
self.COMM = MPI.COMM_WORLD.Dup()
def tearDown(self):
self.COMM.Free()
name, version = MPI.get_vendor()
if name == 'Open MPI' and version < (1,8,4):
_create_topo_comms = create_topo_comms
def create_topo_comms(comm):
for c in _create_topo_comms(comm):
if c.size * 2 < sum(c.degrees):
c.Free(); continue
yield c
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_rma.py 0000664 0001750 0001750 00000036253 13426006675 017170 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
import arrayimpl
import sys
pypy_lt_53 = (hasattr(sys, 'pypy_version_info') and
sys.pypy_version_info < (5, 3))
def mkzeros(n):
if pypy_lt_53:
return b'\0' * n
return bytearray(n)
def memzero(m):
try:
m[:] = 0
except IndexError: # cffi buffer
m[0:len(m)] = b'\0'*len(m)
class BaseTestRMA(object):
COMM = MPI.COMM_NULL
INFO = MPI.INFO_NULL
def setUp(self):
nbytes = 100*MPI.DOUBLE.size
try:
self.mpi_memory = MPI.Alloc_mem(nbytes)
self.memory = self.mpi_memory
memzero(self.memory)
except MPI.Exception:
import array
self.mpi_memory = None
self.memory = array.array('B',[0]*nbytes)
self.WIN = MPI.Win.Create(self.memory, 1, self.INFO, self.COMM)
def tearDown(self):
self.WIN.Free()
if self.mpi_memory:
MPI.Free_mem(self.mpi_memory)
def testPutGet(self):
typemap = MPI._typedict
group = self.WIN.Get_group()
size = group.Get_size()
group.Free()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for count in range(10):
for rank in range(size):
sbuf = array(range(count), typecode)
rbuf = array(-1, typecode, count+1)
#
self.WIN.Fence()
self.WIN.Put(sbuf.as_mpi(), rank)
self.WIN.Fence()
self.WIN.Get(rbuf.as_mpi_c(count), rank)
self.WIN.Fence()
for i in range(count):
self.assertEqual(sbuf[i], i)
self.assertNotEqual(rbuf[i], -1)
self.assertEqual(rbuf[-1], -1)
#
sbuf = array(range(count), typecode)
rbuf = array(-1, typecode, count+1)
target = sbuf.itemsize
self.WIN.Fence()
self.WIN.Put(sbuf.as_mpi(), rank, target)
self.WIN.Fence()
self.WIN.Get(rbuf.as_mpi_c(count), rank, target)
self.WIN.Fence()
for i in range(count):
self.assertEqual(sbuf[i], i)
self.assertNotEqual(rbuf[i], -1)
self.assertEqual(rbuf[-1], -1)
#
sbuf = array(range(count), typecode)
rbuf = array(-1, typecode, count+1)
datatype = typemap[typecode]
target = (sbuf.itemsize, count, datatype)
self.WIN.Fence()
self.WIN.Put(sbuf.as_mpi(), rank, target)
self.WIN.Fence()
self.WIN.Get(rbuf.as_mpi_c(count), rank, target)
self.WIN.Fence()
for i in range(count):
self.assertEqual(sbuf[i], i)
self.assertNotEqual(rbuf[i], -1)
self.assertEqual(rbuf[-1], -1)
def testAccumulate(self):
group = self.WIN.Get_group()
size = group.Get_size()
group.Free()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for count in range(10):
for rank in range(size):
sbuf = array(range(count), typecode)
rbuf = array(-1, typecode, count+1)
for op in (MPI.SUM, MPI.PROD, MPI.MAX, MPI.MIN):
self.WIN.Fence()
self.WIN.Accumulate(sbuf.as_mpi(), rank, op=op)
self.WIN.Fence()
self.WIN.Get(rbuf.as_mpi_c(count), rank)
self.WIN.Fence()
for i in range(count):
self.assertEqual(sbuf[i], i)
self.assertNotEqual(rbuf[i], -1)
self.assertEqual(rbuf[-1], -1)
@unittest.skipMPI('openmpi(>=1.10,<1.11)')
def testGetAccumulate(self):
group = self.WIN.Get_group()
size = group.Get_size()
rank = group.Get_rank()
group.Free()
self.WIN.Fence()
obuf = MPI.Alloc_mem(1); memzero(obuf)
rbuf = MPI.Alloc_mem(1); memzero(rbuf)
try:
try:
self.WIN.Get_accumulate([obuf, 0, MPI.BYTE], [rbuf, 0, MPI.BYTE], rank)
finally:
MPI.Free_mem(obuf)
MPI.Free_mem(rbuf)
except NotImplementedError:
self.skipTest('mpi-win-get_accumulate')
self.WIN.Fence()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for count in range(10):
for rank in range(size):
ones = array([1]*count, typecode)
sbuf = array(range(count), typecode)
rbuf = array(-1, typecode, count+1)
gbuf = array(-1, typecode, count+1)
for op in (MPI.SUM, MPI.PROD,
MPI.MAX, MPI.MIN,
MPI.REPLACE, MPI.NO_OP):
self.WIN.Lock(rank)
self.WIN.Put(ones.as_mpi(), rank)
self.WIN.Flush(rank)
self.WIN.Get_accumulate(sbuf.as_mpi(),
rbuf.as_mpi_c(count),
rank, op=op)
self.WIN.Flush(rank)
self.WIN.Get(gbuf.as_mpi_c(count), rank)
self.WIN.Flush(rank)
self.WIN.Unlock(rank)
#
for i in range(count):
self.assertEqual(sbuf[i], i)
self.assertEqual(rbuf[i], 1)
self.assertEqual(gbuf[i], op(1, i))
self.assertEqual(rbuf[-1], -1)
self.assertEqual(gbuf[-1], -1)
def testFetchAndOp(self):
group = self.WIN.Get_group()
size = group.Get_size()
rank = group.Get_rank()
group.Free()
self.WIN.Fence()
obuf = MPI.Alloc_mem(1); memzero(obuf)
rbuf = MPI.Alloc_mem(1); memzero(rbuf)
try:
try:
self.WIN.Fetch_and_op([obuf, 1, MPI.BYTE], [rbuf, 1, MPI.BYTE], rank)
finally:
MPI.Free_mem(obuf)
MPI.Free_mem(rbuf)
except NotImplementedError:
self.skipTest('mpi-win-fetch_and_op')
self.WIN.Fence()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
obuf = array(+1, typecode)
rbuf = array(-1, typecode, 2)
for op in (MPI.SUM, MPI.PROD,
MPI.MAX, MPI.MIN,
MPI.REPLACE, MPI.NO_OP):
for rank in range(size):
for disp in range(3):
self.WIN.Lock(rank)
self.WIN.Fetch_and_op(obuf.as_mpi(),
rbuf.as_mpi_c(1),
rank, disp, op=op)
self.WIN.Unlock(rank)
self.assertEqual(rbuf[1], -1)
def testCompareAndSwap(self):
group = self.WIN.Get_group()
size = group.Get_size()
rank = group.Get_rank()
group.Free()
self.WIN.Fence()
obuf = MPI.Alloc_mem(1); memzero(obuf)
cbuf = MPI.Alloc_mem(1); memzero(cbuf)
rbuf = MPI.Alloc_mem(1); memzero(rbuf)
try:
try:
self.WIN.Compare_and_swap([obuf, 1, MPI.BYTE],
[cbuf, 1, MPI.BYTE],
[rbuf, 1, MPI.BYTE],
rank, 0)
finally:
MPI.Free_mem(obuf)
MPI.Free_mem(cbuf)
MPI.Free_mem(rbuf)
except NotImplementedError:
self.skipTest('mpi-win-compare_and_swap')
self.WIN.Fence()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
if typecode in 'fdg': continue
obuf = array(+1, typecode)
cbuf = array( 0, typecode)
rbuf = array(-1, typecode, 2)
for rank in range(size):
for disp in range(3):
self.WIN.Lock(rank)
self.WIN.Compare_and_swap(obuf.as_mpi(),
cbuf.as_mpi(),
rbuf.as_mpi_c(1),
rank, disp)
self.WIN.Unlock(rank)
self.assertEqual(rbuf[1], -1)
def testPutProcNull(self):
self.WIN.Fence()
self.WIN.Put(None, MPI.PROC_NULL, None)
self.WIN.Fence()
def testGetProcNull(self):
self.WIN.Fence()
self.WIN.Get(None, MPI.PROC_NULL, None)
self.WIN.Fence()
def testAccumulateProcNullReplace(self):
self.WIN.Fence()
zeros = mkzeros(8)
self.WIN.Fence()
self.WIN.Accumulate([zeros, MPI.INT], MPI.PROC_NULL, None, MPI.REPLACE)
self.WIN.Fence()
self.WIN.Accumulate([zeros, MPI.INT], MPI.PROC_NULL, None, MPI.REPLACE)
self.WIN.Fence()
def testAccumulateProcNullSum(self):
self.WIN.Fence()
zeros = mkzeros(8)
self.WIN.Fence()
self.WIN.Accumulate([zeros, MPI.INT], MPI.PROC_NULL, None, MPI.SUM)
self.WIN.Fence()
self.WIN.Accumulate([None, MPI.INT], MPI.PROC_NULL, None, MPI.SUM)
self.WIN.Fence()
def testGetAccumulateProcNull(self):
obuf = [mkzeros(8), 0, MPI.INT]
rbuf = [mkzeros(8), 0, MPI.INT]
self.WIN.Fence()
try:
self.WIN.Get_accumulate(obuf, rbuf, MPI.PROC_NULL)
except NotImplementedError:
self.skipTest('mpi-win-get_accumulate')
self.WIN.Fence()
##def testFetchAndOpProcNull(self):
## obuf = cbuf = rbuf = None
## self.WIN.Fence()
## try:
## self.WIN.Fetch_and_op(obuf, rbuf, MPI.PROC_NULL, 0)
## except NotImplementedError:
## self.skipTest('mpi-win-fetch_and_op')
## self.WIN.Fence()
##def testCompareAndSwapProcNull(self):
## obuf = cbuf = rbuf = None
## self.WIN.Fence()
## try:
## self.WIN.Compare_and_swap(obuf, cbuf, rbuf, MPI.PROC_NULL, 0)
## except NotImplementedError:
## self.skipTest('mpi-win-compare_and_swap')
## self.WIN.Fence()
def testFence(self):
win = self.WIN
LMODE = [0, MPI.MODE_NOSTORE, MPI.MODE_NOPUT,
MPI.MODE_NOSTORE|MPI.MODE_NOPUT]
GMODE = [0, MPI.MODE_NOPRECEDE, MPI.MODE_NOSUCCEED]
win.Fence()
for lmode in LMODE:
for gmode in GMODE:
assertion = lmode | gmode
win.Fence(assertion)
win.Fence()
@unittest.skipMPI('openmpi(==1.8.1)')
def testFenceAll(self):
win = self.WIN
assertion = 0
modes = [0,
MPI.MODE_NOSTORE,
MPI.MODE_NOPUT,
MPI.MODE_NOPRECEDE,
MPI.MODE_NOSUCCEED]
win.Fence()
for mode in modes:
win.Fence(mode)
assertion |= mode
win.Fence(assertion)
win.Fence()
@unittest.skipMPI('openmpi(==1.8.6)')
def testStartComplete(self):
self.WIN.Start(MPI.GROUP_EMPTY)
self.WIN.Complete()
@unittest.skipMPI('openmpi(==1.8.6)')
def testPostWait(self):
self.WIN.Post(MPI.GROUP_EMPTY)
self.WIN.Wait()
@unittest.skipMPI('openmpi(==1.8.7)')
@unittest.skipMPI('openmpi(==1.8.6)')
def testStartCompletePostWait(self):
win = self.WIN
wingroup = win.Get_group()
size = wingroup.Get_size()
rank = wingroup.Get_rank()
if size < 2: return wingroup.Free()
if rank == 0:
group = wingroup.Excl([0])
win.Start(group)
win.Complete()
win.Post(group)
win.Wait()
group.Free()
else:
group = wingroup.Incl([0])
win.Post(group)
win.Wait()
win.Start(group)
win.Complete()
group.Free()
wingroup.Free()
@unittest.skipMPI('openmpi(==1.8.7)')
@unittest.skipMPI('openmpi(==1.8.6)')
def testStartCompletePostTest(self):
comm = self.COMM
win = self.WIN
wingroup = win.Get_group()
size = wingroup.Get_size()
rank = wingroup.Get_rank()
if size < 2: return wingroup.Free()
if rank == 0:
group = wingroup.Excl([0])
win.Start(group)
comm.Barrier()
win.Complete()
comm.Barrier()
group.Free()
else:
group = wingroup.Incl([0])
win.Post(group)
flag = win.Test()
self.assertFalse(flag)
comm.Barrier()
comm.Barrier()
flag = win.Test()
self.assertTrue(flag)
group.Free()
wingroup.Free()
@unittest.skipMPI('MPI(<3.0)')
def testSync(self):
win = self.WIN
comm = self.COMM
rank = comm.Get_rank()
win.Lock(rank)
win.Sync()
win.Unlock(rank)
comm.Barrier()
@unittest.skipMPI('MPI(<3.0)')
def testFlush(self):
win = self.WIN
comm = self.COMM
size = comm.Get_size()
rank = comm.Get_rank()
#
for i in range(size):
win.Lock(i)
win.Flush(i)
win.Unlock(i)
comm.Barrier()
for i in range(size):
if i == rank:
win.Lock_all()
win.Flush_all()
win.Unlock_all()
comm.Barrier()
#
for i in range(size):
win.Lock(i)
win.Flush_local(i)
win.Unlock(i)
comm.Barrier()
for i in range(size):
if i == rank:
win.Lock_all()
win.Flush_local_all()
win.Unlock_all()
comm.Barrier()
class TestRMASelf(BaseTestRMA, unittest.TestCase):
COMM = MPI.COMM_SELF
class TestRMAWorld(BaseTestRMA, unittest.TestCase):
COMM = MPI.COMM_WORLD
SpectrumMPI = MPI.get_vendor()[0] == 'Spectrum MPI'
try:
if SpectrumMPI: raise NotImplementedError
MPI.Win.Create(None, 1, MPI.INFO_NULL, MPI.COMM_SELF).Free()
except (NotImplementedError, MPI.Exception):
unittest.disable(BaseTestRMA, 'mpi-rma')
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_io.py 0000644 0001750 0001750 00000036537 13200562156 017012 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
import arrayimpl
import sys, os, tempfile
class BaseTestIO(object):
COMM = MPI.COMM_NULL
FILE = MPI.FILE_NULL
prefix = 'mpi4py-'
def setUp(self):
comm = self.COMM
fname = None
if comm.Get_rank() == 0:
fd, fname = tempfile.mkstemp(prefix=self.prefix)
os.close(fd)
fname = comm.bcast(fname, 0)
amode = MPI.MODE_RDWR | MPI.MODE_CREATE
amode |= MPI.MODE_DELETE_ON_CLOSE
amode |= MPI.MODE_UNIQUE_OPEN
info = MPI.INFO_NULL
try:
self.FILE = MPI.File.Open(comm, fname, amode, info)
except Exception:
if comm.Get_rank() == 0:
os.remove(fname)
raise
def tearDown(self):
if self.FILE:
self.FILE.Close()
self.COMM.Barrier()
# non-collective
def testReadWriteAt(self):
comm = self.COMM
size = comm.Get_size()
rank = comm.Get_rank()
fh = self.FILE
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
etype = arrayimpl.TypeMap[typecode]
fh.Set_size(0)
fh.Set_view(0, etype)
count = 13
wbuf = array(42, typecode, count)
fh.Write_at(count*rank, wbuf.as_raw())
fh.Sync()
comm.Barrier()
fh.Sync()
rbuf = array(-1, typecode, count+1)
fh.Read_at(count*rank, rbuf.as_mpi_c(count))
for value in rbuf[:-1]:
self.assertEqual(value, 42)
self.assertEqual(rbuf[-1], -1)
comm.Barrier()
def testIReadIWriteAt(self):
comm = self.COMM
size = comm.Get_size()
rank = comm.Get_rank()
fh = self.FILE
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
etype = arrayimpl.TypeMap[typecode]
fh.Set_size(0)
fh.Set_view(0, etype)
count = 13
wbuf = array(42, typecode, count)
fh.Iwrite_at(count*rank, wbuf.as_raw()).Wait()
fh.Sync()
comm.Barrier()
fh.Sync()
rbuf = array(-1, typecode, count+1)
fh.Iread_at(count*rank, rbuf.as_mpi_c(count)).Wait()
for value in rbuf[:-1]:
self.assertEqual(value, 42)
self.assertEqual(rbuf[-1], -1)
comm.Barrier()
def testReadWrite(self):
comm = self.COMM
size = comm.Get_size()
rank = comm.Get_rank()
fh = self.FILE
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
etype = arrayimpl.TypeMap[typecode]
fh.Set_size(0)
fh.Set_view(0, etype)
count = 13
wbuf = array(42, typecode, count)
for r in range(size):
if r == rank:
fh.Seek(0, MPI.SEEK_SET)
fh.Write(wbuf.as_raw())
fh.Sync()
comm.Barrier()
fh.Sync()
for n in range(0, len(wbuf)):
rbuf = array(-1, typecode, n+1)
fh.Seek(0, MPI.SEEK_SET)
fh.Read(rbuf.as_mpi_c(n))
for value in rbuf[:-1]:
self.assertEqual(value, 42)
self.assertEqual(rbuf[-1], -1)
comm.Barrier()
def testIReadIWrite(self):
comm = self.COMM
size = comm.Get_size()
rank = comm.Get_rank()
fh = self.FILE
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
etype = arrayimpl.TypeMap[typecode]
fh.Set_size(0)
fh.Set_view(0, etype)
count = 13
wbuf = array(42, typecode, count)
for r in range(size):
if r == rank:
fh.Seek(0, MPI.SEEK_SET)
fh.Iwrite(wbuf.as_raw()).Wait()
fh.Sync()
comm.Barrier()
fh.Sync()
for n in range(0, len(wbuf)):
rbuf = array(-1, typecode, n+1)
fh.Seek(0, MPI.SEEK_SET)
fh.Iread(rbuf.as_mpi_c(n)).Wait()
for value in rbuf[:-1]:
self.assertEqual(value, 42)
self.assertEqual(rbuf[-1], -1)
comm.Barrier()
def testReadWriteShared(self):
comm = self.COMM
size = comm.Get_size()
rank = comm.Get_rank()
fh = self.FILE
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
etype = arrayimpl.TypeMap[typecode]
fh.Set_size(0)
fh.Set_view(0, etype)
count = 13
wbuf = array(rank%42, typecode, count)
fh.Seek_shared(0, MPI.SEEK_SET)
fh.Write_shared(wbuf.as_raw())
fh.Sync()
comm.Barrier()
fh.Sync()
rbuf = array(-1, typecode, count+1)
fh.Seek_shared(0, MPI.SEEK_SET)
fh.Read_shared(rbuf.as_mpi_c(count))
for value in rbuf[:-1]:
self.assertTrue(0<=value<42)
self.assertEqual(value, rbuf[0])
self.assertEqual(rbuf[-1], -1)
comm.Barrier()
def testIReadIWriteShared(self):
comm = self.COMM
size = comm.Get_size()
rank = comm.Get_rank()
fh = self.FILE
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
etype = arrayimpl.TypeMap[typecode]
fh.Set_size(0)
fh.Set_view(0, etype)
count = 13
wbuf = array(rank%42, typecode, count)
fh.Seek_shared(0, MPI.SEEK_SET)
fh.Iwrite_shared(wbuf.as_raw()).Wait()
fh.Sync()
comm.Barrier()
fh.Sync()
rbuf = array(-1, typecode, count+1)
fh.Seek_shared(0, MPI.SEEK_SET)
fh.Iread_shared(rbuf.as_mpi_c(count)).Wait()
for value in rbuf[:-1]:
self.assertTrue(0<=value<42)
self.assertEqual(value, rbuf[0])
self.assertEqual(rbuf[-1], -1)
comm.Barrier()
# collective
def testReadWriteAtAll(self):
comm = self.COMM
size = comm.Get_size()
rank = comm.Get_rank()
fh = self.FILE
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
etype = arrayimpl.TypeMap[typecode]
fh.Set_size(0)
fh.Set_view(0, etype)
count = 13
wbuf = array(42, typecode, count)
fh.Write_at_all(count*rank, wbuf.as_raw())
fh.Sync()
comm.Barrier()
fh.Sync()
rbuf = array(-1, typecode, count+1)
fh.Read_at_all(count*rank, rbuf.as_mpi_c(count))
for value in rbuf[:-1]:
self.assertEqual(value, 42)
self.assertEqual(rbuf[-1], -1)
comm.Barrier()
@unittest.skipMPI('SpectrumMPI')
def testIReadIWriteAtAll(self):
comm = self.COMM
size = comm.Get_size()
rank = comm.Get_rank()
fh = self.FILE
try: # MPI 3.1
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
etype = arrayimpl.TypeMap[typecode]
fh.Set_size(0)
fh.Set_view(0, etype)
count = 13
wbuf = array(42, typecode, count)
fh.Iwrite_at_all(count*rank, wbuf.as_raw()).Wait()
fh.Sync()
comm.Barrier()
fh.Sync()
rbuf = array(-1, typecode, count+1)
fh.Iread_at_all(count*rank, rbuf.as_mpi_c(count)).Wait()
for value in rbuf[:-1]:
self.assertEqual(value, 42)
self.assertEqual(rbuf[-1], -1)
comm.Barrier()
except NotImplementedError:
if MPI.Get_version() >= (3, 1): raise
self.skipTest('mpi-iwrite_at_all')
def testReadWriteAtAllBeginEnd(self):
comm = self.COMM
size = comm.Get_size()
rank = comm.Get_rank()
fh = self.FILE
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
etype = arrayimpl.TypeMap[typecode]
fh.Set_size(0)
fh.Set_view(0, etype)
count = 13
wbuf = array(42, typecode, count)
fh.Write_at_all_begin(count*rank, wbuf.as_raw())
fh.Write_at_all_end(wbuf.as_raw())
fh.Sync()
comm.Barrier()
fh.Sync()
rbuf = array(-1, typecode, count+1)
fh.Read_at_all_begin(count*rank, rbuf.as_mpi_c(count))
fh.Read_at_all_end(rbuf.as_raw())
for value in rbuf[:-1]:
self.assertEqual(value, 42)
self.assertEqual(rbuf[-1], -1)
comm.Barrier()
def testReadWriteAll(self):
comm = self.COMM
size = comm.Get_size()
rank = comm.Get_rank()
fh = self.FILE
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
etype = arrayimpl.TypeMap[typecode]
fh.Set_size(0)
fh.Set_view(0, etype)
count = 13
wbuf = array(42, typecode, count)
fh.Seek(count*rank, MPI.SEEK_SET)
fh.Write_all(wbuf.as_raw())
fh.Sync()
comm.Barrier()
fh.Sync()
rbuf = array(-1, typecode, count+1)
fh.Seek(count*rank, MPI.SEEK_SET)
fh.Read_all(rbuf.as_mpi_c(count))
for value in rbuf[:-1]:
self.assertEqual(value, 42)
self.assertEqual(rbuf[-1], -1)
comm.Barrier()
@unittest.skipMPI('SpectrumMPI')
def testIReadIWriteAll(self):
comm = self.COMM
size = comm.Get_size()
rank = comm.Get_rank()
fh = self.FILE
try: # MPI 3.1
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
etype = arrayimpl.TypeMap[typecode]
fh.Set_size(0)
fh.Set_view(0, etype)
count = 13
wbuf = array(42, typecode, count)
fh.Seek(count*rank, MPI.SEEK_SET)
fh.Iwrite_all(wbuf.as_raw()).Wait()
fh.Sync()
comm.Barrier()
fh.Sync()
rbuf = array(-1, typecode, count+1)
fh.Seek(count*rank, MPI.SEEK_SET)
fh.Iread_all(rbuf.as_mpi_c(count)).Wait()
for value in rbuf[:-1]:
self.assertEqual(value, 42)
self.assertEqual(rbuf[-1], -1)
comm.Barrier()
except NotImplementedError:
if MPI.Get_version() >= (3, 1): raise
self.skipTest('mpi-iwrite_all')
def testReadWriteAllBeginEnd(self):
comm = self.COMM
size = comm.Get_size()
rank = comm.Get_rank()
fh = self.FILE
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
etype = arrayimpl.TypeMap[typecode]
fh.Set_size(0)
fh.Set_view(0, etype)
count = 13
wbuf = array(42, typecode, count)
fh.Seek(count*rank, MPI.SEEK_SET)
fh.Write_all_begin(wbuf.as_raw())
fh.Write_all_end(wbuf.as_raw())
fh.Sync()
comm.Barrier()
fh.Sync()
rbuf = array(-1, typecode, count+1)
fh.Seek(count*rank, MPI.SEEK_SET)
fh.Read_all_begin(rbuf.as_mpi_c(count))
fh.Read_all_end(rbuf.as_raw())
for value in rbuf[:-1]:
self.assertEqual(value, 42)
self.assertEqual(rbuf[-1], -1)
comm.Barrier()
def testReadWriteOrdered(self):
comm = self.COMM
size = comm.Get_size()
rank = comm.Get_rank()
fh = self.FILE
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
etype = arrayimpl.TypeMap[typecode]
fh.Set_size(0)
fh.Set_view(0, etype)
count = 13
wbuf = array(rank%42, typecode, count)
fh.Seek_shared(0, MPI.SEEK_SET)
fh.Write_ordered(wbuf.as_raw())
fh.Sync()
comm.Barrier()
fh.Sync()
rbuf = array(-1, typecode, count+1)
fh.Seek_shared(0, MPI.SEEK_SET)
fh.Read_ordered(rbuf.as_mpi_c(count))
for value in rbuf[:-1]:
self.assertEqual(value, rank%42)
self.assertEqual(rbuf[-1], -1)
comm.Barrier()
def testReadWriteOrderedBeginEnd(self):
comm = self.COMM
size = comm.Get_size()
rank = comm.Get_rank()
fh = self.FILE
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
etype = arrayimpl.TypeMap[typecode]
fh.Set_size(0)
fh.Set_view(0, etype)
count = 13
wbuf = array(rank%42, typecode, count)
fh.Seek_shared(0, MPI.SEEK_SET)
fh.Write_ordered_begin(wbuf.as_raw())
fh.Write_ordered_end(wbuf.as_raw())
fh.Sync()
comm.Barrier()
fh.Sync()
rbuf = array(-1, typecode, count+1)
fh.Seek_shared(0, MPI.SEEK_SET)
fh.Read_ordered_begin(rbuf.as_mpi_c(count))
fh.Read_ordered_end(rbuf.as_raw())
for value in rbuf[:-1]:
self.assertEqual(value, rank%42)
self.assertEqual(rbuf[-1], -1)
comm.Barrier()
@unittest.skipMPI('MPICH1')
@unittest.skipMPI('LAM/MPI')
class TestIOSelf(BaseTestIO, unittest.TestCase):
COMM = MPI.COMM_SELF
prefix = BaseTestIO.prefix + ('%d-' % MPI.COMM_WORLD.Get_rank())
@unittest.skipMPI('openmpi(<2.2.0)')
@unittest.skipMPI('msmpi')
@unittest.skipMPI('MPICH2')
@unittest.skipMPI('MPICH1')
@unittest.skipMPI('LAM/MPI')
class TestIOWorld(BaseTestIO, unittest.TestCase):
COMM = MPI.COMM_WORLD
def have_feature():
case = BaseTestIO()
case.COMM = TestIOSelf.COMM
case.prefix = TestIOSelf.prefix
case.setUp()
case.tearDown()
try:
have_feature()
except NotImplementedError:
unittest.disable(BaseTestIO, 'mpi-io')
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_op.py 0000644 0001750 0001750 00000016246 13200562156 017014 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
import sys
MPI_ERR_OP = MPI.ERR_OP
try:
import array
except ImportError:
array = None
def asarray(typecode, data):
try:
memoryview
_tobytes = lambda s: memoryview(s).tobytes()
except NameError:
_tobytes = lambda s: buffer(s)[:]
try:
_frombytes = array.array.frombytes
except AttributeError:
_frombytes = array.array.fromstring
a = array.array(typecode, [])
_frombytes(a, _tobytes(data))
return a
def mysum_obj(a, b):
for i in range(len(a)):
b[i] = a[i] + b[i]
return b
def mysum_buf(a, b, dt):
assert dt == MPI.INT
assert len(a) == len(b)
b[:] = mysum_obj(asarray('i', a), asarray('i', b))
def mysum(ba, bb, dt):
if dt is None:
return mysum_obj(ba, bb)
else:
return mysum_buf(ba, bb, dt)
class TestOp(unittest.TestCase):
def testConstructor(self):
op = MPI.Op()
self.assertFalse(op)
self.assertEqual(op, MPI.OP_NULL)
@unittest.skipIf(array is None, 'array')
def testCreate(self):
for comm in [MPI.COMM_SELF, MPI.COMM_WORLD]:
for commute in [True, False]:
for N in range(4):
myop = MPI.Op.Create(mysum, commute)
self.assertFalse(myop.is_predefined)
if (hasattr(sys, 'pypy_version_info') and
comm.size > 1):
myop.Free()
continue
try:
# buffer(empty_array) returns
# the same non-NULL pointer !!!
if N == 0: continue
size = comm.Get_size()
rank = comm.Get_rank()
a = array.array('i', [i*(rank+1) for i in range(N)])
b = array.array('i', [0]*len(a))
comm.Allreduce([a, MPI.INT], [b, MPI.INT], myop)
scale = sum(range(1,size+1))
for i in range(N):
self.assertEqual(b[i], scale*i)
ret = myop(a, b)
self.assertTrue(ret is b)
for i in range(N):
self.assertEqual(b[i], a[i]+scale*i)
myop2 = MPI.Op(myop)
a = array.array('i', [1]*N)
b = array.array('i', [2]*N)
ret = myop2(a, b)
self.assertTrue(ret is b)
for i in range(N):
self.assertEqual(b[i], 3)
myop2 = None
finally:
myop.Free()
def testCreateMany(self):
N = 32 # max user-defined operations
#
ops = []
for i in range(N):
o = MPI.Op.Create(mysum)
ops.append(o)
for o in ops: o.Free() # cleanup
# another round
ops = []
for i in range(N):
o = MPI.Op.Create(mysum)
ops.append(o)
for o in ops: o.Free() # cleanup
def _test_call(self, op, args, res):
self.assertEqual(op(*args), res)
def testCall(self):
self._test_call(MPI.MIN, (2,3), 2)
self._test_call(MPI.MAX, (2,3), 3)
self._test_call(MPI.SUM, (2,3), 5)
self._test_call(MPI.PROD, (2,3), 6)
def xor(x,y): return bool(x) ^ bool(y)
for x, y in ((0, 0),
(0, 1),
(1, 0),
(1, 1)):
self._test_call(MPI.LAND, (x,y), x and y)
self._test_call(MPI.LOR, (x,y), x or y)
self._test_call(MPI.LXOR, (x,y), xor(x, y))
self._test_call(MPI.BAND, (x,y), x & y)
self._test_call(MPI.BOR, (x,y), x | y)
self._test_call(MPI.BXOR, (x,y), x ^ y)
if MPI.REPLACE:
self._test_call(MPI.REPLACE, (2,3), 3)
self._test_call(MPI.REPLACE, (3,2), 2)
if MPI.NO_OP:
self._test_call(MPI.NO_OP, (2,3), 2)
self._test_call(MPI.NO_OP, (3,2), 3)
def testMinMax(self):
x = [1]; y = [1]
res = MPI.MIN(x, y)
self.assertTrue(res is x)
res = MPI.MAX(x, y)
self.assertTrue(res is x)
def testMinMaxLoc(self):
x = [1]; i = [2]; u = [x, i]
y = [2]; j = [1]; v = [y, j]
res = MPI.MINLOC(u, v)
self.assertTrue(res[0] is x)
self.assertTrue(res[1] is i)
res = MPI.MINLOC(v, u)
self.assertTrue(res[0] is x)
self.assertTrue(res[1] is i)
res = MPI.MAXLOC(u, v)
self.assertTrue(res[0] is y)
self.assertTrue(res[1] is j)
res = MPI.MAXLOC(v, u)
self.assertTrue(res[0] is y)
self.assertTrue(res[1] is j)
#
x = [1]; i = 0; u = [x, i]
y = [1]; j = 1; v = [y, j]
res = MPI.MINLOC(u, v)
self.assertTrue(res[0] is x)
self.assertTrue(res[1] is i)
res = MPI.MAXLOC(u, v)
self.assertTrue(res[0] is x)
self.assertTrue(res[1] is i)
#
x = [1]; i = 1; u = [x, i]
y = [1]; j = 0; v = [y, j]
res = MPI.MINLOC(u, v)
self.assertTrue(res[0] is y)
self.assertTrue(res[1] is j)
res = MPI.MAXLOC(u, v)
self.assertTrue(res[0] is y)
self.assertTrue(res[1] is j)
#
x = [1]; i = [0]; u = [x, i]
y = [1]; j = [1]; v = [y, j]
res = MPI.MINLOC(u, v)
self.assertTrue(res[0] is x)
self.assertTrue(res[1] is i)
res = MPI.MAXLOC(u, v)
self.assertTrue(res[0] is x)
self.assertTrue(res[1] is i)
#
x = [1]; i = [1]; u = [x, i]
y = [1]; j = [0]; v = [y, j]
res = MPI.MINLOC(u, v)
self.assertTrue(res[0] is y)
self.assertTrue(res[1] is j)
res = MPI.MAXLOC(u, v)
self.assertTrue(res[0] is y)
self.assertTrue(res[1] is j)
@unittest.skipMPI('openmpi(<=1.8.1)')
def testIsCommutative(self):
try:
MPI.SUM.Is_commutative()
except NotImplementedError:
self.skipTest('mpi-op-is_commutative')
ops = [MPI.MAX, MPI.MIN,
MPI.SUM, MPI.PROD,
MPI.LAND, MPI.BAND,
MPI.LOR, MPI.BOR,
MPI.LXOR, MPI.BXOR,
MPI.MAXLOC, MPI.MINLOC,]
for op in ops:
flag = op.Is_commutative()
self.assertEqual(flag, op.is_commutative)
self.assertTrue(flag)
ops = [MPI.REPLACE, MPI.NO_OP]
for op in ops:
if not op: continue
flag = op.Is_commutative()
self.assertEqual(flag, op.is_commutative)
#self.assertFalse(flag)
def testIsPredefined(self):
self.assertTrue(MPI.OP_NULL.is_predefined)
ops = [MPI.MAX, MPI.MIN,
MPI.SUM, MPI.PROD,
MPI.LAND, MPI.BAND,
MPI.LOR, MPI.BOR,
MPI.LXOR, MPI.BXOR,
MPI.MAXLOC, MPI.MINLOC,]
for op in ops:
self.assertTrue(op.is_predefined)
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_rc.py 0000644 0001750 0001750 00000001125 13200562156 016770 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import rc
import mpiunittest as unittest
class TestRC(unittest.TestCase):
def testRC1(self):
rc(initialize = rc.initialize)
rc(threads = rc.threads)
rc(thread_level = rc.thread_level)
rc(finalize = rc.finalize)
rc(fast_reduce = rc.fast_reduce)
rc(recv_mprobe = rc.recv_mprobe)
def testRC2(self):
kwargs = rc.__dict__.copy()
rc(**kwargs)
def testRC3(self):
error = lambda: rc(ABCXYZ=123456)
self.assertRaises(TypeError, error)
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_cffi.py 0000644 0001750 0001750 00000004257 13200562156 017304 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
try:
import cffi
except ImportError:
cffi = None
@unittest.skipIf(cffi is None, 'cffi')
class TestCFFI(unittest.TestCase):
mpitypes = [
MPI.Datatype,
MPI.Request,
MPI.Info,
MPI.Errhandler,
MPI.Group,
MPI.Win,
MPI.Op,
MPI.File,
MPI.Message,
MPI.Comm,
]
objects = [
MPI.DATATYPE_NULL,
MPI.INT,
MPI.DOUBLE,
MPI.REQUEST_NULL,
MPI.INFO_NULL,
MPI.INFO_ENV,
MPI.ERRHANDLER_NULL,
MPI.ERRORS_RETURN,
MPI.ERRORS_ARE_FATAL,
MPI.GROUP_NULL,
MPI.GROUP_EMPTY,
MPI.WIN_NULL,
MPI.OP_NULL,
MPI.SUM,
MPI.MIN,
MPI.MAX,
MPI.FILE_NULL,
MPI.MESSAGE_NULL,
MPI.MESSAGE_NO_PROC,
MPI.COMM_NULL,
MPI.COMM_SELF,
MPI.COMM_WORLD,
]
def testHandleAddress(self):
ffi = cffi.FFI()
typemap = {ffi.sizeof('int'): 'int',
ffi.sizeof('void*'): 'void*'}
typename = lambda t: t.__name__.rsplit('.', 1)[-1]
for tp in self.mpitypes:
handle_t = typemap[MPI._sizeof(tp)]
mpi_t = 'MPI_' + typename(tp)
ffi.cdef("typedef %s %s;" % (handle_t, mpi_t))
for obj in self.objects:
if isinstance(obj, MPI.Comm):
mpi_t = 'MPI_Comm'
else:
mpi_t = 'MPI_' + typename(type(obj))
oldobj = obj
newobj = type(obj)()
handle_old = ffi.cast(mpi_t+'*', MPI._addressof(oldobj))
handle_new = ffi.cast(mpi_t+'*', MPI._addressof(newobj))
handle_new[0] = handle_old[0]
self.assertEqual(oldobj, newobj)
def testHandleValue(self):
ffi = cffi.FFI()
typemap = {ffi.sizeof('uint32_t'): 'uint32_t',
ffi.sizeof('uint64_t'): 'uint64_t',}
for obj in self.objects:
uintptr_t = typemap[MPI._sizeof(obj)]
handle = ffi.cast(uintptr_t+'*', MPI._addressof(obj))[0]
self.assertEqual(handle, MPI._handleof(obj))
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_cco_nb_buf.py 0000664 0001750 0001750 00000061500 13426006675 020461 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
import arrayimpl
from functools import reduce
prod = lambda sequence,start=1: reduce(lambda x, y: x*y, sequence, start)
def maxvalue(a):
try:
typecode = a.typecode
except AttributeError:
typecode = a.dtype.char
if typecode == ('f'):
return 1e30
elif typecode == ('d'):
return 1e300
else:
return 2 ** (a.itemsize * 7) - 1
@unittest.skipMPI('msmpi(<8.1.0)')
class BaseTestCCOBuf(object):
COMM = MPI.COMM_NULL
def testBarrier(self):
self.COMM.Ibarrier().Wait()
def testBcast(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
if rank == root:
buf = array(root, typecode, root)
else:
buf = array( -1, typecode, root)
self.COMM.Ibcast(buf.as_mpi(), root=root).Wait()
for value in buf:
self.assertEqual(value, root)
def testGather(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
sbuf = array(root, typecode, root+1)
if rank == root:
rbuf = array(-1, typecode, (size,root+1))
else:
rbuf = array([], typecode)
self.COMM.Igather(sbuf.as_mpi(), rbuf.as_mpi(),
root=root).Wait()
if rank == root:
for value in rbuf.flat:
self.assertEqual(value, root)
def testScatter(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
rbuf = array(-1, typecode, size)
if rank == root:
sbuf = array(root, typecode, (size, size))
else:
sbuf = array([], typecode)
self.COMM.Iscatter(sbuf.as_mpi(), rbuf.as_mpi(),
root=root).Wait()
for value in rbuf:
self.assertEqual(value, root)
def testAllgather(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
sbuf = array(root, typecode, root+1)
rbuf = array( -1, typecode, (size, root+1))
self.COMM.Iallgather(sbuf.as_mpi(), rbuf.as_mpi()).Wait()
for value in rbuf.flat:
self.assertEqual(value, root)
def testAlltoall(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
sbuf = array(root, typecode, (size, root+1))
rbuf = array( -1, typecode, (size, root+1))
self.COMM.Ialltoall(sbuf.as_mpi(), rbuf.as_mpi_c(root+1)).Wait()
for value in rbuf.flat:
self.assertEqual(value, root)
def assertAlmostEqual(self, first, second):
num = float(float(second-first))
den = float(second+first)/2 or 1.0
if (abs(num/den) > 1e-2):
raise self.failureException('%r != %r' % (first, second))
def testReduce(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
for op in (MPI.SUM, MPI.PROD, MPI.MAX, MPI.MIN):
sbuf = array(range(size), typecode)
rbuf = array(-1, typecode, size)
self.COMM.Ireduce(sbuf.as_mpi(),
rbuf.as_mpi(),
op, root).Wait()
max_val = maxvalue(rbuf)
for i, value in enumerate(rbuf):
if rank != root:
self.assertEqual(value, -1)
continue
if op == MPI.SUM:
if (i * size) < max_val:
self.assertAlmostEqual(value, i*size)
elif op == MPI.PROD:
if (i ** size) < max_val:
self.assertAlmostEqual(value, i**size)
elif op == MPI.MAX:
self.assertEqual(value, i)
elif op == MPI.MIN:
self.assertEqual(value, i)
def testAllreduce(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for op in (MPI.SUM, MPI.MAX, MPI.MIN, MPI.PROD):
sbuf = array(range(size), typecode)
rbuf = array(0, typecode, size)
self.COMM.Iallreduce(sbuf.as_mpi(),
rbuf.as_mpi(),
op).Wait()
max_val = maxvalue(rbuf)
for i, value in enumerate(rbuf):
if op == MPI.SUM:
if (i * size) < max_val:
self.assertAlmostEqual(value, i*size)
elif op == MPI.PROD:
if (i ** size) < max_val:
self.assertAlmostEqual(value, i**size)
elif op == MPI.MAX:
self.assertEqual(value, i)
elif op == MPI.MIN:
self.assertEqual(value, i)
@unittest.skipMPI('openmpi(<=1.8.3)')
def testReduceScatter(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for op in (MPI.SUM, MPI.MAX, MPI.MIN, MPI.PROD):
rcnt = list(range(1,size+1))
sbuf = array([rank+1]*sum(rcnt), typecode)
rbuf = array(-1, typecode, rank+1)
self.COMM.Ireduce_scatter(sbuf.as_mpi(),
rbuf.as_mpi(),
None, op).Wait()
max_val = maxvalue(rbuf)
for i, value in enumerate(rbuf):
if op == MPI.SUM:
redval = sum(range(size))+size
if redval < max_val:
self.assertAlmostEqual(value, redval)
elif op == MPI.PROD:
redval = prod(range(1,size+1))
if redval < max_val:
self.assertAlmostEqual(value, redval)
elif op == MPI.MAX:
self.assertEqual(value, size)
elif op == MPI.MIN:
self.assertEqual(value, 1)
rbuf = array(-1, typecode, rank+1)
self.COMM.Ireduce_scatter(sbuf.as_mpi(),
rbuf.as_mpi(),
rcnt, op).Wait()
max_val = maxvalue(rbuf)
for i, value in enumerate(rbuf):
if op == MPI.SUM:
redval = sum(range(size))+size
if redval < max_val:
self.assertAlmostEqual(value, redval)
elif op == MPI.PROD:
redval = prod(range(1,size+1))
if redval < max_val:
self.assertAlmostEqual(value, redval)
elif op == MPI.MAX:
self.assertEqual(value, size)
elif op == MPI.MIN:
self.assertEqual(value, 1)
def testReduceScatterBlock(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for op in (MPI.SUM, MPI.MAX, MPI.MIN, MPI.PROD):
for rcnt in range(1, size+1):
sbuf = array([rank]*rcnt*size, typecode)
rbuf = array(-1, typecode, rcnt)
if op == MPI.PROD:
sbuf = array([rank+1]*rcnt*size, typecode)
self.COMM.Ireduce_scatter_block(sbuf.as_mpi(),
rbuf.as_mpi(),
op).Wait()
max_val = maxvalue(rbuf)
v_sum = (size*(size-1))/2
v_prod = 1
for i in range(1,size+1): v_prod *= i
v_max = size-1
v_min = 0
for i, value in enumerate(rbuf):
if op == MPI.SUM:
if v_sum <= max_val:
self.assertAlmostEqual(value, v_sum)
elif op == MPI.PROD:
if v_prod <= max_val:
self.assertAlmostEqual(value, v_prod)
elif op == MPI.MAX:
self.assertEqual(value, v_max)
elif op == MPI.MIN:
self.assertEqual(value, v_min)
def testScan(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
# --
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for op in (MPI.SUM, MPI.PROD, MPI.MAX, MPI.MIN):
sbuf = array(range(size), typecode)
rbuf = array(0, typecode, size)
self.COMM.Iscan(sbuf.as_mpi(),
rbuf.as_mpi(),
op).Wait()
max_val = maxvalue(rbuf)
for i, value in enumerate(rbuf):
if op == MPI.SUM:
if (i * (rank + 1)) < max_val:
self.assertAlmostEqual(value, i * (rank + 1))
elif op == MPI.PROD:
if (i ** (rank + 1)) < max_val:
self.assertAlmostEqual(value, i ** (rank + 1))
elif op == MPI.MAX:
self.assertEqual(value, i)
elif op == MPI.MIN:
self.assertEqual(value, i)
@unittest.skipMPI('openmpi(<=1.8.1)')
def testExscan(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for op in (MPI.SUM, MPI.PROD, MPI.MAX, MPI.MIN):
sbuf = array(range(size), typecode)
rbuf = array(0, typecode, size)
self.COMM.Iexscan(sbuf.as_mpi(),
rbuf.as_mpi(),
op).Wait()
if rank == 1:
for i, value in enumerate(rbuf):
self.assertEqual(value, i)
elif rank > 1:
max_val = maxvalue(rbuf)
for i, value in enumerate(rbuf):
if op == MPI.SUM:
if (i * rank) < max_val:
self.assertAlmostEqual(value, i * rank)
elif op == MPI.PROD:
if (i ** rank) < max_val:
self.assertAlmostEqual(value, i ** rank)
elif op == MPI.MAX:
self.assertEqual(value, i)
elif op == MPI.MIN:
self.assertEqual(value, i)
def testBcastTypeIndexed(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode, datatype in arrayimpl.TypeMap.items():
for root in range(size):
#
if rank == root:
buf = array(range(10), typecode).as_raw()
else:
buf = array(-1, typecode, 10).as_raw()
indices = list(range(0, len(buf), 2))
newtype = datatype.Create_indexed_block(1, indices)
newtype.Commit()
newbuf = (buf, 1, newtype)
self.COMM.Ibcast(newbuf, root=root).Wait()
newtype.Free()
if rank != root:
for i, value in enumerate(buf):
if (i % 2):
self.assertEqual(value, -1)
else:
self.assertEqual(value, i)
#
if rank == root:
buf = array(range(10), typecode).as_raw()
else:
buf = array(-1, typecode, 10).as_raw()
indices = list(range(1, len(buf), 2))
newtype = datatype.Create_indexed_block(1, indices)
newtype.Commit()
newbuf = (buf, 1, newtype)
self.COMM.Ibcast(newbuf, root).Wait()
newtype.Free()
if rank != root:
for i, value in enumerate(buf):
if not (i % 2):
self.assertEqual(value, -1)
else:
self.assertEqual(value, i)
@unittest.skipMPI('MVAPICH2')
class BaseTestCCOBufInplace(object):
def testGather(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
count = root+3
if rank == root:
sbuf = MPI.IN_PLACE
buf = array(-1, typecode, (size, count))
#buf.flat[(rank*count):((rank+1)*count)] = \
# array(root, typecode, count)
s, e = rank*count, (rank+1)*count
for i in range(s, e): buf.flat[i] = root
rbuf = buf.as_mpi()
else:
buf = array(root, typecode, count)
sbuf = buf.as_mpi()
rbuf = None
self.COMM.Igather(sbuf, rbuf, root=root).Wait()
for value in buf.flat:
self.assertEqual(value, root)
@unittest.skipMPI('msmpi(==10.0.0)')
def testScatter(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
for count in range(1, 10):
if rank == root:
buf = array(root, typecode, (size, count))
sbuf = buf.as_mpi()
rbuf = MPI.IN_PLACE
else:
buf = array(-1, typecode, count)
sbuf = None
rbuf = buf.as_mpi()
self.COMM.Iscatter(sbuf, rbuf, root=root).Wait()
for value in buf.flat:
self.assertEqual(value, root)
def testAllgather(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for count in range(1, 10):
buf = array(-1, typecode, (size, count))
#buf.flat[(rank*count):((rank+1)*count)] = \
# array(count, typecode, count)
s, e = rank*count, (rank+1)*count
for i in range(s, e): buf.flat[i] = count
self.COMM.Iallgather(MPI.IN_PLACE, buf.as_mpi()).Wait()
for value in buf.flat:
self.assertEqual(value, count)
def assertAlmostEqual(self, first, second):
num = float(second-first)
den = float(second+first)/2 or 1.0
if (abs(num/den) > 1e-2):
raise self.failureException('%r != %r' % (first, second))
def testReduce(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
for op in (MPI.SUM, MPI.PROD, MPI.MAX, MPI.MIN):
count = size
if rank == root:
buf = array(range(size), typecode)
sbuf = MPI.IN_PLACE
rbuf = buf.as_mpi()
else:
buf = array(range(size), typecode)
buf2 = array(range(size), typecode)
sbuf = buf.as_mpi()
rbuf = buf2.as_mpi()
self.COMM.Ireduce(sbuf, rbuf, op, root).Wait()
if rank == root:
max_val = maxvalue(buf)
for i, value in enumerate(buf):
if op == MPI.SUM:
if (i * size) < max_val:
self.assertAlmostEqual(value, i*size)
elif op == MPI.PROD:
if (i ** size) < max_val:
self.assertAlmostEqual(value, i**size)
elif op == MPI.MAX:
self.assertEqual(value, i)
elif op == MPI.MIN:
self.assertEqual(value, i)
def testAllreduce(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for op in (MPI.SUM, MPI.MAX, MPI.MIN, MPI.PROD):
buf = array(range(size), typecode)
sbuf = MPI.IN_PLACE
rbuf = buf.as_mpi()
self.COMM.Iallreduce(sbuf, rbuf, op).Wait()
max_val = maxvalue(buf)
for i, value in enumerate(buf):
if op == MPI.SUM:
if (i * size) < max_val:
self.assertAlmostEqual(value, i*size)
elif op == MPI.PROD:
if (i ** size) < max_val:
self.assertAlmostEqual(value, i**size)
elif op == MPI.MAX:
self.assertEqual(value, i)
elif op == MPI.MIN:
self.assertEqual(value, i)
@unittest.skipMPI('openmpi(<=1.8.6)')
def testReduceScatterBlock(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for op in (MPI.SUM, MPI.MAX, MPI.MIN, MPI.PROD):
for rcnt in range(size):
if op == MPI.PROD:
rbuf = array([rank+1]*rcnt*size, typecode)
else:
rbuf = array([rank]*rcnt*size, typecode)
self.COMM.Ireduce_scatter_block(MPI.IN_PLACE,
rbuf.as_mpi(),
op).Wait()
max_val = maxvalue(rbuf)
for i, value in enumerate(rbuf):
if i >= rcnt:
if op == MPI.PROD:
self.assertEqual(value, rank+1)
else:
self.assertEqual(value, rank)
else:
if op == MPI.SUM:
redval = sum(range(size))
if redval < max_val:
self.assertAlmostEqual(value, redval)
elif op == MPI.PROD:
redval = prod(range(1,size+1))
if redval < max_val:
self.assertAlmostEqual(value, redval)
elif op == MPI.MAX:
self.assertEqual(value, size-1)
elif op == MPI.MIN:
self.assertEqual(value, 0)
@unittest.skipMPI('openmpi(<=1.8.6)')
def testReduceScatter(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for op in (MPI.SUM, MPI.MAX, MPI.MIN, MPI.PROD):
rcnt = list(range(1, size+1))
if op == MPI.PROD:
rbuf = array([rank+1]*sum(rcnt), typecode)
else:
rbuf = array([rank]*sum(rcnt), typecode)
self.COMM.Ireduce_scatter(MPI.IN_PLACE,
rbuf.as_mpi(),
rcnt, op).Wait()
max_val = maxvalue(rbuf)
for i, value in enumerate(rbuf):
if i >= rcnt[rank]:
if op == MPI.PROD:
self.assertEqual(value, rank+1)
else:
self.assertEqual(value, rank)
else:
if op == MPI.SUM:
redval = sum(range(size))
if redval < max_val:
self.assertAlmostEqual(value, redval)
elif op == MPI.PROD:
redval = prod(range(1,size+1))
if redval < max_val:
self.assertAlmostEqual(value, redval)
elif op == MPI.MAX:
self.assertEqual(value, size-1)
elif op == MPI.MIN:
self.assertEqual(value, 0)
class TestCCOBufSelf(BaseTestCCOBuf, unittest.TestCase):
COMM = MPI.COMM_SELF
class TestCCOBufWorld(BaseTestCCOBuf, unittest.TestCase):
COMM = MPI.COMM_WORLD
class TestCCOBufInplaceSelf(BaseTestCCOBufInplace, unittest.TestCase):
COMM = MPI.COMM_SELF
class TestCCOBufInplaceWorld(BaseTestCCOBufInplace, unittest.TestCase):
COMM = MPI.COMM_WORLD
@unittest.skipMPI('IntelMPI', MPI.COMM_WORLD.Get_size() > 1)
def testReduceScatter(self):
super(TestCCOBufInplaceWorld, self).testReduceScatter()
class TestCCOBufSelfDup(TestCCOBufSelf):
def setUp(self):
self.COMM = MPI.COMM_SELF.Dup()
def tearDown(self):
self.COMM.Free()
class TestCCOBufWorldDup(TestCCOBufWorld):
def setUp(self):
self.COMM = MPI.COMM_WORLD.Dup()
def tearDown(self):
self.COMM.Free()
try:
MPI.COMM_SELF.Ibarrier().Wait()
except NotImplementedError:
unittest.disable(BaseTestCCOBuf, 'mpi-nbc')
unittest.disable(BaseTestCCOBufInplace, 'mpi-nbc')
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_cco_buf.py 0000664 0001750 0001750 00000071712 13426006675 020010 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
import arrayimpl
from functools import reduce
prod = lambda sequence,start=1: reduce(lambda x, y: x*y, sequence, start)
def maxvalue(a):
try:
typecode = a.typecode
except AttributeError:
typecode = a.dtype.char
if typecode == ('f'):
return 1e30
elif typecode == ('d'):
return 1e300
else:
return 2 ** (a.itemsize * 7) - 1
class BaseTestCCOBuf(object):
COMM = MPI.COMM_NULL
def testBarrier(self):
self.COMM.Barrier()
def testBcast(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
if rank == root:
buf = array(root, typecode, root)
else:
buf = array( -1, typecode, root)
self.COMM.Bcast(buf.as_mpi(), root=root)
for value in buf:
self.assertEqual(value, root)
def testGather(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
sbuf = array(root, typecode, root+1)
if rank == root:
rbuf = array(-1, typecode, (size,root+1))
else:
rbuf = array([], typecode)
self.COMM.Gather(sbuf.as_mpi(), rbuf.as_mpi(),
root=root)
if rank == root:
for value in rbuf.flat:
self.assertEqual(value, root)
def testScatter(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
rbuf = array(-1, typecode, size)
if rank == root:
sbuf = array(root, typecode, (size, size))
else:
sbuf = array([], typecode)
self.COMM.Scatter(sbuf.as_mpi(), rbuf.as_mpi(),
root=root)
for value in rbuf:
self.assertEqual(value, root)
def testAllgather(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
sbuf = array(root, typecode, root+1)
rbuf = array( -1, typecode, (size, root+1))
self.COMM.Allgather(sbuf.as_mpi(), rbuf.as_mpi())
for value in rbuf.flat:
self.assertEqual(value, root)
def testAlltoall(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
sbuf = array(root, typecode, (size, root+1))
rbuf = array( -1, typecode, (size, root+1))
self.COMM.Alltoall(sbuf.as_mpi(), rbuf.as_mpi_c(root+1))
for value in rbuf.flat:
self.assertEqual(value, root)
def assertAlmostEqual(self, first, second):
num = float(float(second-first))
den = float(second+first)/2 or 1.0
if (abs(num/den) > 1e-2):
raise self.failureException('%r != %r' % (first, second))
def testReduce(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
for op in (MPI.SUM, MPI.PROD, MPI.MAX, MPI.MIN):
sbuf = array(range(size), typecode)
rbuf = array(-1, typecode, size)
self.COMM.Reduce(sbuf.as_mpi(),
rbuf.as_mpi(),
op, root)
max_val = maxvalue(rbuf)
for i, value in enumerate(rbuf):
if rank != root:
self.assertEqual(value, -1)
continue
if op == MPI.SUM:
if (i * size) < max_val:
self.assertAlmostEqual(value, i*size)
elif op == MPI.PROD:
if (i ** size) < max_val:
self.assertAlmostEqual(value, i**size)
elif op == MPI.MAX:
self.assertEqual(value, i)
elif op == MPI.MIN:
self.assertEqual(value, i)
def testAllreduce(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for op in (MPI.SUM, MPI.MAX, MPI.MIN, MPI.PROD):
sbuf = array(range(size), typecode)
rbuf = array(0, typecode, size)
self.COMM.Allreduce(sbuf.as_mpi(),
rbuf.as_mpi(),
op)
max_val = maxvalue(rbuf)
for i, value in enumerate(rbuf):
if op == MPI.SUM:
if (i * size) < max_val:
self.assertAlmostEqual(value, i*size)
elif op == MPI.PROD:
if (i ** size) < max_val:
self.assertAlmostEqual(value, i**size)
elif op == MPI.MAX:
self.assertEqual(value, i)
elif op == MPI.MIN:
self.assertEqual(value, i)
def testReduceScatter(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for op in (MPI.SUM, MPI.MAX, MPI.MIN, MPI.PROD):
rcnt = list(range(1,size+1))
sbuf = array([rank+1]*sum(rcnt), typecode)
rbuf = array(-1, typecode, rank+1)
self.COMM.Reduce_scatter(sbuf.as_mpi(),
rbuf.as_mpi(),
None, op)
max_val = maxvalue(rbuf)
for i, value in enumerate(rbuf):
if op == MPI.SUM:
redval = sum(range(size))+size
if redval < max_val:
self.assertAlmostEqual(value, redval)
elif op == MPI.PROD:
redval = prod(range(1,size+1))
if redval < max_val:
self.assertAlmostEqual(value, redval)
elif op == MPI.MAX:
self.assertEqual(value, size)
elif op == MPI.MIN:
self.assertEqual(value, 1)
rbuf = array(-1, typecode, rank+1)
self.COMM.Reduce_scatter(sbuf.as_mpi(),
rbuf.as_mpi(),
rcnt, op)
max_val = maxvalue(rbuf)
for i, value in enumerate(rbuf):
if op == MPI.SUM:
redval = sum(range(size))+size
if redval < max_val:
self.assertAlmostEqual(value, redval)
elif op == MPI.PROD:
redval = prod(range(1,size+1))
if redval < max_val:
self.assertAlmostEqual(value, redval)
elif op == MPI.MAX:
self.assertEqual(value, size)
elif op == MPI.MIN:
self.assertEqual(value, 1)
def testReduceScatterBlock(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for op in (MPI.SUM, MPI.MAX, MPI.MIN, MPI.PROD):
for rcnt in range(1,size):
sbuf = array([rank]*rcnt*size, typecode)
rbuf = array(-1, typecode, rcnt)
if op == MPI.PROD:
sbuf = array([rank+1]*rcnt*size, typecode)
self.COMM.Reduce_scatter_block(sbuf.as_mpi(),
rbuf.as_mpi(),
op)
max_val = maxvalue(rbuf)
v_sum = (size*(size-1))/2
v_prod = 1
for i in range(1,size+1): v_prod *= i
v_max = size-1
v_min = 0
for i, value in enumerate(rbuf):
if op == MPI.SUM:
if v_sum <= max_val:
self.assertAlmostEqual(value, v_sum)
elif op == MPI.PROD:
if v_prod <= max_val:
self.assertAlmostEqual(value, v_prod)
elif op == MPI.MAX:
self.assertEqual(value, v_max)
elif op == MPI.MIN:
self.assertEqual(value, v_min)
def testScan(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
# --
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for op in (MPI.SUM, MPI.PROD, MPI.MAX, MPI.MIN):
sbuf = array(range(size), typecode)
rbuf = array(0, typecode, size)
self.COMM.Scan(sbuf.as_mpi(),
rbuf.as_mpi(),
op)
max_val = maxvalue(rbuf)
for i, value in enumerate(rbuf):
if op == MPI.SUM:
if (i * (rank + 1)) < max_val:
self.assertAlmostEqual(value, i * (rank + 1))
elif op == MPI.PROD:
if (i ** (rank + 1)) < max_val:
self.assertAlmostEqual(value, i ** (rank + 1))
elif op == MPI.MAX:
self.assertEqual(value, i)
elif op == MPI.MIN:
self.assertEqual(value, i)
def testExscan(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for op in (MPI.SUM, MPI.PROD, MPI.MAX, MPI.MIN):
sbuf = array(range(size), typecode)
rbuf = array(0, typecode, size)
try:
self.COMM.Exscan(sbuf.as_mpi(),
rbuf.as_mpi(),
op)
except NotImplementedError:
self.skipTest('mpi-exscan')
if rank == 1:
for i, value in enumerate(rbuf):
self.assertEqual(value, i)
elif rank > 1:
max_val = maxvalue(rbuf)
for i, value in enumerate(rbuf):
if op == MPI.SUM:
if (i * rank) < max_val:
self.assertAlmostEqual(value, i * rank)
elif op == MPI.PROD:
if (i ** rank) < max_val:
self.assertAlmostEqual(value, i ** rank)
elif op == MPI.MAX:
self.assertEqual(value, i)
elif op == MPI.MIN:
self.assertEqual(value, i)
def testBcastTypeIndexed(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode, datatype in arrayimpl.TypeMap.items():
for root in range(size):
#
if rank == root:
buf = array(range(10), typecode).as_raw()
else:
buf = array(-1, typecode, 10).as_raw()
indices = list(range(0, len(buf), 2))
newtype = datatype.Create_indexed_block(1, indices)
newtype.Commit()
newbuf = (buf, 1, newtype)
self.COMM.Bcast(newbuf, root=root)
newtype.Free()
if rank != root:
for i, value in enumerate(buf):
if (i % 2):
self.assertEqual(value, -1)
else:
self.assertEqual(value, i)
#
if rank == root:
buf = array(range(10), typecode).as_raw()
else:
buf = array(-1, typecode, 10).as_raw()
indices = list(range(1, len(buf), 2))
newtype = datatype.Create_indexed_block(1, indices)
newtype.Commit()
newbuf = (buf, 1, newtype)
self.COMM.Bcast(newbuf, root)
newtype.Free()
if rank != root:
for i, value in enumerate(buf):
if not (i % 2):
self.assertEqual(value, -1)
else:
self.assertEqual(value, i)
class BaseTestCCOBufInplace(object):
def testGather(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
count = root+3
if rank == root:
sbuf = MPI.IN_PLACE
buf = array(-1, typecode, (size, count))
#buf.flat[(rank*count):((rank+1)*count)] = \
# array(root, typecode, count)
s, e = rank*count, (rank+1)*count
for i in range(s, e): buf.flat[i] = root
rbuf = buf.as_mpi()
else:
buf = array(root, typecode, count)
sbuf = buf.as_mpi()
rbuf = None
self.COMM.Gather(sbuf, rbuf, root=root)
for value in buf.flat:
self.assertEqual(value, root)
@unittest.skipMPI('msmpi(==10.0.0)')
def testScatter(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
for count in range(1, 10):
if rank == root:
buf = array(root, typecode, (size, count))
sbuf = buf.as_mpi()
rbuf = MPI.IN_PLACE
else:
buf = array(-1, typecode, count)
sbuf = None
rbuf = buf.as_mpi()
self.COMM.Scatter(sbuf, rbuf, root=root)
for value in buf.flat:
self.assertEqual(value, root)
def testAllgather(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for count in range(1, 10):
buf = array(-1, typecode, (size, count))
#buf.flat[(rank*count):((rank+1)*count)] = \
# array(count, typecode, count)
s, e = rank*count, (rank+1)*count
for i in range(s, e): buf.flat[i] = count
self.COMM.Allgather(MPI.IN_PLACE, buf.as_mpi())
for value in buf.flat:
self.assertEqual(value, count)
def assertAlmostEqual(self, first, second):
num = float(float(second-first))
den = float(second+first)/2 or 1.0
if (abs(num/den) > 1e-2):
raise self.failureException('%r != %r' % (first, second))
def testReduce(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
for op in (MPI.SUM, MPI.PROD, MPI.MAX, MPI.MIN):
count = size
if rank == root:
buf = array(range(size), typecode)
sbuf = MPI.IN_PLACE
rbuf = buf.as_mpi()
else:
buf = array(range(size), typecode)
buf2 = array(range(size), typecode)
sbuf = buf.as_mpi()
rbuf = buf2.as_mpi()
self.COMM.Reduce(sbuf, rbuf, op, root)
if rank == root:
max_val = maxvalue(buf)
for i, value in enumerate(buf):
if op == MPI.SUM:
if (i * size) < max_val:
self.assertAlmostEqual(value, i*size)
elif op == MPI.PROD:
if (i ** size) < max_val:
self.assertAlmostEqual(value, i**size)
elif op == MPI.MAX:
self.assertEqual(value, i)
elif op == MPI.MIN:
self.assertEqual(value, i)
def testAllreduce(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for op in (MPI.SUM, MPI.MAX, MPI.MIN, MPI.PROD):
buf = array(range(size), typecode)
sbuf = MPI.IN_PLACE
rbuf = buf.as_mpi()
self.COMM.Allreduce(sbuf, rbuf, op)
max_val = maxvalue(buf)
for i, value in enumerate(buf):
if op == MPI.SUM:
if (i * size) < max_val:
self.assertAlmostEqual(value, i*size)
elif op == MPI.PROD:
if (i ** size) < max_val:
self.assertAlmostEqual(value, i**size)
elif op == MPI.MAX:
self.assertEqual(value, i)
elif op == MPI.MIN:
self.assertEqual(value, i)
def testReduceScatterBlock(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for op in (MPI.SUM, MPI.MAX, MPI.MIN, MPI.PROD):
for rcnt in range(size):
if op == MPI.PROD:
rbuf = array([rank+1]*rcnt*size, typecode)
else:
rbuf = array([rank]*rcnt*size, typecode)
self.COMM.Reduce_scatter_block(MPI.IN_PLACE,
rbuf.as_mpi(),
op)
max_val = maxvalue(rbuf)
for i, value in enumerate(rbuf):
if i >= rcnt:
if op == MPI.PROD:
self.assertEqual(value, rank+1)
else:
self.assertEqual(value, rank)
else:
if op == MPI.SUM:
redval = sum(range(size))
if redval < max_val:
self.assertAlmostEqual(value, redval)
elif op == MPI.PROD:
redval = prod(range(1,size+1))
if redval < max_val:
self.assertAlmostEqual(value, redval)
elif op == MPI.MAX:
self.assertEqual(value, size-1)
elif op == MPI.MIN:
self.assertEqual(value, 0)
@unittest.skipMPI('MVAPICH2')
def testReduceScatter(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for op in (MPI.SUM, MPI.MAX, MPI.MIN, MPI.PROD):
rcnt = list(range(1, size+1))
if op == MPI.PROD:
rbuf = array([rank+1]*sum(rcnt), typecode)
else:
rbuf = array([rank]*sum(rcnt), typecode)
self.COMM.Reduce_scatter(MPI.IN_PLACE,
rbuf.as_mpi(),
rcnt, op)
max_val = maxvalue(rbuf)
for i, value in enumerate(rbuf):
if i >= rcnt[rank]:
if op == MPI.PROD:
self.assertEqual(value, rank+1)
else:
self.assertEqual(value, rank)
else:
if op == MPI.SUM:
redval = sum(range(size))
if redval < max_val:
self.assertAlmostEqual(value, redval)
elif op == MPI.PROD:
redval = prod(range(1,size+1))
if redval < max_val:
self.assertAlmostEqual(value, redval)
elif op == MPI.MAX:
self.assertEqual(value, size-1)
elif op == MPI.MIN:
self.assertEqual(value, 0)
@unittest.skipMPI('openmpi(<=1.8.4)')
def testScan(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
# --
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for op in (MPI.SUM, MPI.PROD, MPI.MAX, MPI.MIN):
buf = array(range(size), typecode)
self.COMM.Scan(MPI.IN_PLACE,
buf.as_mpi(),
op)
max_val = maxvalue(buf)
for i, value in enumerate(buf):
if op == MPI.SUM:
if (i * (rank + 1)) < max_val:
self.assertAlmostEqual(value, i * (rank + 1))
elif op == MPI.PROD:
if (i ** (rank + 1)) < max_val:
self.assertAlmostEqual(value, i ** (rank + 1))
elif op == MPI.MAX:
self.assertEqual(value, i)
elif op == MPI.MIN:
self.assertEqual(value, i)
@unittest.skipMPI('openmpi(<=1.8.4)')
@unittest.skipMPI('msmpi(<=4.2.0)')
def testExscan(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for op in (MPI.SUM, MPI.PROD, MPI.MAX, MPI.MIN):
buf = array(range(size), typecode)
try:
self.COMM.Exscan(MPI.IN_PLACE,
buf.as_mpi(),
op)
except NotImplementedError:
self.skipTest('mpi-exscan')
if rank == 1:
for i, value in enumerate(buf):
self.assertEqual(value, i)
elif rank > 1:
max_val = maxvalue(buf)
for i, value in enumerate(buf):
if op == MPI.SUM:
if (i * rank) < max_val:
self.assertAlmostEqual(value, i * rank)
elif op == MPI.PROD:
if (i ** rank) < max_val:
self.assertAlmostEqual(value, i ** rank)
elif op == MPI.MAX:
self.assertEqual(value, i)
elif op == MPI.MIN:
self.assertEqual(value, i)
class TestReduceLocal(unittest.TestCase):
def testReduceLocal(self):
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for op in (MPI.SUM, MPI.PROD, MPI.MAX, MPI.MIN):
size = 5
sbuf = array(range(1,size+1), typecode)
rbuf = array(range(0,size+0), typecode)
try:
op.Reduce_local(sbuf.as_mpi(), rbuf.as_mpi())
except NotImplementedError:
self.skipTest('mpi-op-reduce_local')
for i, value in enumerate(rbuf):
self.assertEqual(sbuf[i], i+1)
if op == MPI.SUM:
self.assertAlmostEqual(value, i+(i+1))
elif op == MPI.PROD:
self.assertAlmostEqual(value, i*(i+1))
elif op == MPI.MAX:
self.assertEqual(value, i+1)
elif op == MPI.MIN:
self.assertEqual(value, i)
for array in arrayimpl.ArrayTypes:
for op in (MPI.SUM, MPI.PROD, MPI.MAX, MPI.MIN):
sbuf = array(range(3), "i")
rbuf = array(range(3), "i")
def f(): op.Reduce_local(sbuf.as_mpi_c(2),
rbuf.as_mpi_c(3))
self.assertRaises(ValueError, f)
def f(): op.Reduce_local([sbuf.as_raw(), 1, MPI.INT],
[rbuf.as_raw(), 1, MPI.SHORT])
self.assertRaises(ValueError, f)
class TestCCOBufSelf(BaseTestCCOBuf, unittest.TestCase):
COMM = MPI.COMM_SELF
class TestCCOBufWorld(BaseTestCCOBuf, unittest.TestCase):
COMM = MPI.COMM_WORLD
@unittest.skipMPI('MPICH1')
@unittest.skipMPI('LAM/MPI')
@unittest.skipIf(MPI.IN_PLACE == MPI.BOTTOM, 'mpi-in-place')
class TestCCOBufInplaceSelf(BaseTestCCOBufInplace, unittest.TestCase):
COMM = MPI.COMM_SELF
@unittest.skipMPI('MPICH1')
@unittest.skipMPI('LAM/MPI')
@unittest.skipIf(MPI.IN_PLACE == MPI.BOTTOM, 'mpi-in-place')
class TestCCOBufInplaceWorld(BaseTestCCOBufInplace, unittest.TestCase):
COMM = MPI.COMM_WORLD
@unittest.skipMPI('IntelMPI', MPI.COMM_WORLD.Get_size() > 1)
def testReduceScatter(self):
super(TestCCOBufInplaceWorld, self).testReduceScatter()
class TestCCOBufSelfDup(TestCCOBufSelf):
def setUp(self):
self.COMM = MPI.COMM_SELF.Dup()
def tearDown(self):
self.COMM.Free()
@unittest.skipMPI('openmpi(<1.4.0)', MPI.Query_thread() > MPI.THREAD_SINGLE)
class TestCCOBufWorldDup(TestCCOBufWorld):
def setUp(self):
self.COMM = MPI.COMM_WORLD.Dup()
def tearDown(self):
self.COMM.Free()
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_comm_topo.py 0000644 0001750 0001750 00000022211 13200562156 020357 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
class BaseTestTopo(object):
COMM = MPI.COMM_NULL
def checkFortran(self, oldcomm):
fint = oldcomm.py2f()
newcomm = MPI.Comm.f2py(fint)
self.assertEqual(newcomm, oldcomm)
self.assertEqual(type(newcomm), type(oldcomm))
def testCartcomm(self):
comm = self.COMM
size = comm.Get_size()
rank = comm.Get_rank()
for ndim in (1,2,3,4,5):
dims = MPI.Compute_dims(size, [0]*ndim)
periods = [True] * len(dims)
topo = comm.Create_cart(dims, periods=periods)
self.assertTrue(topo.is_topo)
self.assertTrue(topo.topology, MPI.CART)
self.checkFortran(topo)
self.assertEqual(topo.dim, len(dims))
self.assertEqual(topo.ndim, len(dims))
coordinates = topo.coords
self.assertEqual(coordinates, topo.Get_coords(topo.rank))
neighbors = []
for i in range(ndim):
for d in (-1, +1):
coord = list(coordinates)
coord[i] = (coord[i]+d) % dims[i]
neigh = topo.Get_cart_rank(coord)
self.assertEqual(coord, topo.Get_coords(neigh))
source, dest = topo.Shift(i, d)
self.assertEqual(neigh, dest)
neighbors.append(neigh)
self.assertEqual(topo.indegree, len(neighbors))
self.assertEqual(topo.outdegree, len(neighbors))
self.assertEqual(topo.inedges, neighbors)
self.assertEqual(topo.outedges, neighbors)
inedges, outedges = topo.inoutedges
self.assertEqual(inedges, neighbors)
self.assertEqual(outedges, neighbors)
if ndim == 1:
topo.Free()
continue
for i in range(ndim):
rem_dims = [1]*ndim
rem_dims[i] = 0
sub = topo.Sub(rem_dims)
if sub != MPI.COMM_NULL:
self.assertEqual(sub.dim, ndim-1)
dims = topo.dims
del dims[i]
self.assertEqual(sub.dims, dims)
sub.Free()
topo.Free()
@unittest.skipMPI('MPI(<2.0)')
def testCartcommZeroDim(self):
comm = self.COMM
topo = comm.Create_cart([])
if topo == MPI.COMM_NULL: return
self.assertEqual(topo.dim, 0)
self.assertEqual(topo.dims, [])
self.assertEqual(topo.periods, [])
self.assertEqual(topo.coords, [])
rank = topo.Get_cart_rank([])
self.assertEqual(rank, 0)
inedges, outedges = topo.inoutedges
self.assertEqual(inedges, [])
self.assertEqual(outedges, [])
topo.Free()
def testGraphcomm(self):
comm = self.COMM
size = comm.Get_size()
rank = comm.Get_rank()
index, edges = [0], []
for i in range(size):
pos = index[-1]
index.append(pos+2)
edges.append((i-1)%size)
edges.append((i+1)%size)
topo = comm.Create_graph(index[1:], edges)
self.assertTrue(topo.is_topo)
self.assertTrue(topo.topology, MPI.GRAPH)
self.checkFortran(topo)
topo.Free()
topo = comm.Create_graph(index, edges)
self.assertEqual(topo.dims, (len(index)-1, len(edges)))
self.assertEqual(topo.nnodes, len(index)-1)
self.assertEqual(topo.nedges, len(edges))
self.assertEqual(topo.index, index[1:])
self.assertEqual(topo.edges, edges)
neighbors = edges[index[rank]:index[rank+1]]
self.assertEqual(neighbors, topo.neighbors)
for rank in range(size):
neighs = topo.Get_neighbors(rank)
self.assertEqual(neighs, [(rank-1)%size, (rank+1)%size])
self.assertEqual(topo.indegree, len(neighbors))
self.assertEqual(topo.outdegree, len(neighbors))
self.assertEqual(topo.inedges, neighbors)
self.assertEqual(topo.outedges, neighbors)
inedges, outedges = topo.inoutedges
self.assertEqual(inedges, neighbors)
self.assertEqual(outedges, neighbors)
topo.Free()
@unittest.skipMPI('msmpi')
def testDistgraphcommAdjacent(self):
comm = self.COMM
size = comm.Get_size()
rank = comm.Get_rank()
try:
topo = comm.Create_dist_graph_adjacent(None, None)
topo.Free()
except NotImplementedError:
self.skipTest('mpi-comm-create_dist_graph_adjacent')
#
sources = [(rank-2)%size, (rank-1)%size]
destinations = [(rank+1)%size, (rank+2)%size]
topo = comm.Create_dist_graph_adjacent(sources, destinations)
self.assertTrue(topo.is_topo)
self.assertTrue(topo.topology, MPI.DIST_GRAPH)
self.checkFortran(topo)
self.assertEqual(topo.Get_dist_neighbors_count(), (2, 2, False))
self.assertEqual(topo.Get_dist_neighbors(), (sources, destinations, None))
self.assertEqual(topo.indegree, len(sources))
self.assertEqual(topo.outdegree, len(destinations))
self.assertEqual(topo.inedges, sources)
self.assertEqual(topo.outedges, destinations)
inedges, outedges = topo.inoutedges
self.assertEqual(inedges, sources)
self.assertEqual(outedges, destinations)
topo.Free()
#
sourceweights = [1, 2]
destweights = [3, 4]
weights = (sourceweights, destweights)
topo = comm.Create_dist_graph_adjacent(sources, destinations,
sourceweights, destweights)
self.assertEqual(topo.Get_dist_neighbors_count(), (2, 2, True))
self.assertEqual(topo.Get_dist_neighbors(), (sources, destinations, weights))
topo.Free()
#
topo = comm.Create_dist_graph_adjacent(sources, None, MPI.UNWEIGHTED, None)
self.assertEqual(topo.Get_dist_neighbors_count(), (2, 0, False))
self.assertEqual(topo.Get_dist_neighbors(), (sources, [], None))
topo.Free()
topo = comm.Create_dist_graph_adjacent(None, destinations, None, MPI.UNWEIGHTED)
self.assertEqual(topo.Get_dist_neighbors_count(), (0, 2, False))
self.assertEqual(topo.Get_dist_neighbors(), ([], destinations, None))
topo.Free()
if MPI.VERSION < 3: return
topo = comm.Create_dist_graph_adjacent([], [], MPI.WEIGHTS_EMPTY, MPI.WEIGHTS_EMPTY)
self.assertEqual(topo.Get_dist_neighbors_count(), (0, 0, True))
self.assertEqual(topo.Get_dist_neighbors(), ([], [], ([], [])))
topo.Free()
@unittest.skipMPI('msmpi')
@unittest.skipMPI('PlatformMPI')
def testDistgraphcomm(self):
comm = self.COMM
size = comm.Get_size()
rank = comm.Get_rank()
#
try:
topo = comm.Create_dist_graph([], [], [], MPI.UNWEIGHTED)
topo.Free()
except NotImplementedError:
self.skipTest('mpi-comm-create_dist_graph')
#
sources = [rank]
degrees = [3]
destinations = [(rank-1)%size, rank, (rank+1)%size]
topo = comm.Create_dist_graph(sources, degrees, destinations, MPI.UNWEIGHTED)
self.assertTrue(topo.is_topo)
self.assertTrue(topo.topology, MPI.DIST_GRAPH)
self.checkFortran(topo)
self.assertEqual(topo.Get_dist_neighbors_count(), (3, 3, False))
topo.Free()
weights = list(range(1,4))
topo = comm.Create_dist_graph(sources, degrees, destinations, weights)
self.assertEqual(topo.Get_dist_neighbors_count(), (3, 3, True))
topo.Free()
def testCartMap(self):
comm = self.COMM
size = comm.Get_size()
for ndim in (1,2,3,4,5):
for periods in (None, True, False):
dims = MPI.Compute_dims(size, [0]*ndim)
topo = comm.Create_cart(dims, periods, reorder=True)
rank = comm.Cart_map(dims, periods)
self.assertEqual(topo.Get_rank(), rank)
topo.Free()
def testGraphMap(self):
comm = self.COMM
size = comm.Get_size()
index, edges = [0], []
for i in range(size):
pos = index[-1]
index.append(pos+2)
edges.append((i-1)%size)
edges.append((i+1)%size)
# Version 1
topo = comm.Create_graph(index, edges, reorder=True)
rank = comm.Graph_map(index, edges)
self.assertEqual(topo.Get_rank(), rank)
topo.Free()
# Version 2
topo = comm.Create_graph(index[1:], edges, reorder=True)
rank = comm.Graph_map(index[1:], edges)
self.assertEqual(topo.Get_rank(), rank)
topo.Free()
class TestTopoSelf(BaseTestTopo, unittest.TestCase):
COMM = MPI.COMM_SELF
class TestTopoWorld(BaseTestTopo, unittest.TestCase):
COMM = MPI.COMM_WORLD
class TestTopoSelfDup(TestTopoSelf):
def setUp(self):
self.COMM = MPI.COMM_SELF.Dup()
def tearDown(self):
self.COMM.Free()
class TestTopoWorldDup(TestTopoWorld):
def setUp(self):
self.COMM = MPI.COMM_WORLD.Dup()
def tearDown(self):
self.COMM.Free()
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_cco_vec.py 0000644 0001750 0001750 00000043705 13200562156 017777 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
import arrayimpl
def maxvalue(a):
try:
typecode = a.typecode
except AttributeError:
typecode = a.dtype.char
if typecode == ('f'):
return 1e30
elif typecode == ('d'):
return 1e300
else:
return 2 ** (a.itemsize * 7) - 1
class BaseTestCCOVec(object):
COMM = MPI.COMM_NULL
def testGatherv(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
for count in range(size):
sbuf = array(root, typecode, count)
rbuf = array( -1, typecode, size*size)
counts = [count] * size
displs = list(range(0, size*size, size))
recvbuf = rbuf.as_mpi_v(counts, displs)
if rank != root: recvbuf=None
self.COMM.Barrier()
self.COMM.Gatherv(sbuf.as_mpi(), recvbuf, root)
self.COMM.Barrier()
if recvbuf is not None:
for i in range(size):
row = rbuf[i*size:(i+1)*size]
a, b = row[:count], row[count:]
for va in a:
self.assertEqual(va, root)
for vb in b:
self.assertEqual(vb, -1)
def testGatherv2(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
for count in range(size):
sbuf = array(root, typecode, size)
rbuf = array( -1, typecode, size*size)
sendbuf = sbuf.as_mpi_c(count)
recvbuf = rbuf.as_mpi_v(count, size)
if rank != root: recvbuf=None
self.COMM.Barrier()
self.COMM.Gatherv(sendbuf, recvbuf, root)
self.COMM.Barrier()
if recvbuf is not None:
for i in range(size):
row = rbuf[i*size:(i+1)*size]
a, b = row[:count], row[count:]
for va in a:
self.assertEqual(va, root)
for vb in b:
self.assertEqual(vb, -1)
def testGatherv3(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
for count in range(size+1):
#
sbuf = array(root, typecode, count).as_raw()
rbuf = array( -1, typecode, count*size).as_raw()
sendbuf = sbuf
recvbuf = [rbuf, count]
if rank != root: recvbuf=None
self.COMM.Barrier()
self.COMM.Gatherv(sendbuf, recvbuf, root)
self.COMM.Barrier()
if recvbuf is not None:
for v in rbuf:
self.assertEqual(v, root)
#
sbuf = array(root, typecode, count).as_raw()
if rank == root:
rbuf = array( -1, typecode, count*size).as_raw()
else:
rbuf = None
self.COMM.Gatherv(sbuf, rbuf, root)
self.COMM.Barrier()
if rank == root:
for v in rbuf:
self.assertEqual(v, root)
def testScatterv(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
for count in range(size):
#
sbuf = array(root, typecode, size*size)
rbuf = array( -1, typecode, count)
counts = [count] * size
displs = list(range(0, size*size, size))
sendbuf = sbuf.as_mpi_v(counts, displs)
if rank != root: sendbuf = None
self.COMM.Scatterv(sendbuf, rbuf.as_mpi(), root)
for vr in rbuf:
self.assertEqual(vr, root)
def testScatterv2(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
for count in range(size):
sbuf = array(root, typecode, size*size)
rbuf = array( -1, typecode, size)
sendbuf = sbuf.as_mpi_v(count, size)
recvbuf = rbuf.as_mpi_c(count)
if rank != root: sendbuf = None
self.COMM.Scatterv(sendbuf, recvbuf, root)
a, b = rbuf[:count], rbuf[count:]
for va in a:
self.assertEqual(va, root)
for vb in b:
self.assertEqual(vb, -1)
def testScatterv3(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
for count in range(size+1):
#
sbuf = array(root, typecode, count*size).as_raw()
rbuf = array( -1, typecode, count).as_raw()
sendbuf = [sbuf, count]
recvbuf = rbuf
if rank != root: sendbuf = None
self.COMM.Scatterv(sendbuf, recvbuf, root)
for v in rbuf:
self.assertEqual(v, root)
#
if rank == root:
sbuf = array(root, typecode, count*size).as_raw()
else:
sbuf = None
rbuf = array( -1, typecode, count).as_raw()
self.COMM.Scatterv(sbuf, rbuf, root)
for v in rbuf:
self.assertEqual(v, root)
def testAllgatherv(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
for count in range(size):
sbuf = array(root, typecode, count)
rbuf = array( -1, typecode, size*size)
counts = [count] * size
displs = list(range(0, size*size, size))
sendbuf = sbuf.as_mpi()
recvbuf = rbuf.as_mpi_v(counts, displs)
self.COMM.Allgatherv(sendbuf, recvbuf)
for i in range(size):
row = rbuf[i*size:(i+1)*size]
a, b = row[:count], row[count:]
for va in a:
self.assertEqual(va, root)
for vb in b:
self.assertEqual(vb, -1)
def testAllgatherv2(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
for count in range(size):
sbuf = array(root, typecode, size)
rbuf = array( -1, typecode, size*size)
sendbuf = sbuf.as_mpi_c(count)
recvbuf = rbuf.as_mpi_v(count, size)
self.COMM.Allgatherv(sendbuf, recvbuf)
for i in range(size):
row = rbuf[i*size:(i+1)*size]
a, b = row[:count], row[count:]
for va in a:
self.assertEqual(va, root)
for vb in b:
self.assertEqual(vb, -1)
def testAllgatherv3(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
for count in range(size+1):
#
sbuf = array(root, typecode, count).as_raw()
rbuf = array( -1, typecode, count*size).as_raw()
sendbuf = sbuf
recvbuf = [rbuf, count]
self.COMM.Allgatherv(sendbuf, recvbuf)
for v in rbuf:
self.assertEqual(v, root)
#
sbuf = array(root, typecode, count).as_raw()
rbuf = array( -1, typecode, count*size).as_raw()
self.COMM.Allgatherv(sbuf, rbuf)
for v in rbuf:
self.assertEqual(v, root)
def testAlltoallv(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
for count in range(size):
sbuf = array(root, typecode, size*size)
rbuf = array( -1, typecode, size*size)
counts = [count] * size
displs = list(range(0, size*size, size))
sendbuf = sbuf.as_mpi_v(counts, displs)
recvbuf = rbuf.as_mpi_v(counts, displs)
self.COMM.Alltoallv(sendbuf, recvbuf)
for i in range(size):
row = rbuf[i*size:(i+1)*size]
a, b = row[:count], row[count:]
for va in a:
self.assertEqual(va, root)
for vb in b:
self.assertEqual(vb, -1)
def testAlltoallv2(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
for count in range(size):
sbuf = array(root, typecode, size*size)
rbuf = array( -1, typecode, size*size)
sendbuf = sbuf.as_mpi_v(count, size)
recvbuf = rbuf.as_mpi_v(count, size)
self.COMM.Alltoallv(sendbuf, recvbuf)
for i in range(size):
row = rbuf[i*size:(i+1)*size]
a, b = row[:count], row[count:]
for va in a:
self.assertEqual(va, root)
for vb in b:
self.assertEqual(vb, -1)
def testAlltoallv3(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for root in range(size):
for count in range(size+1):
#
sbuf = array(root, typecode, count*size).as_raw()
rbuf = array( -1, typecode, count*size).as_raw()
sendbuf = [sbuf, count]
recvbuf = [rbuf, count]
self.COMM.Alltoallv(sendbuf, recvbuf)
for v in rbuf:
self.assertEqual(v, root)
#
sbuf = array(root, typecode, count*size).as_raw()
rbuf = array( -1, typecode, count*size).as_raw()
self.COMM.Alltoallv(sbuf, rbuf)
for v in rbuf:
self.assertEqual(v, root)
def testAlltoallw(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for n in range(1,size+1):
sbuf = array( n, typecode, (size, n))
rbuf = array(-1, typecode, (size, n))
sdt, rdt = sbuf.mpidtype, rbuf.mpidtype
sdsp = list(range(0, size*n*sdt.extent, n*sdt.extent))
rdsp = list(range(0, size*n*rdt.extent, n*rdt.extent))
smsg = (sbuf.as_raw(), ([n]*size, sdsp), [sdt]*size)
rmsg = (rbuf.as_raw(), ([n]*size, rdsp), [rdt]*size)
try:
self.COMM.Alltoallw(smsg, rmsg)
except NotImplementedError:
self.skipTest('mpi-alltoallw')
for value in rbuf.flat:
self.assertEqual(value, n)
@unittest.skipMPI('openmpi(<1.8.0)')
@unittest.skipMPI('msmpi(<8.1.0)')
@unittest.skipIf(MPI.BOTTOM == MPI.IN_PLACE, 'mpi-in-place')
class BaseTestCCOVecInplace(object):
COMM = MPI.COMM_NULL
def testAlltoallv(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for count in range(size):
rbuf = array(-1, typecode, size*size)
counts = [count] * size
displs = list(range(0, size*size, size))
for i in range(size):
for j in range(count):
rbuf[i*size+j] = rank
recvbuf = rbuf.as_mpi_v(counts, displs)
self.COMM.Alltoallv(MPI.IN_PLACE, recvbuf)
for i in range(size):
row = rbuf[i*size:(i+1)*size]
a, b = row[:count], row[count:]
for va in a:
self.assertEqual(va, i)
for vb in b:
self.assertEqual(vb, -1)
def testAlltoallw(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for count in range(size):
rbuf = array(-1, typecode, size*size)
for i in range(size):
for j in range(count):
rbuf[i*size+j] = rank
rdt = rbuf.mpidtype
rdsp = list(range(0, size*size*rdt.extent, size*rdt.extent))
rmsg = (rbuf.as_raw(), ([count]*size, rdsp), [rdt]*size)
try:
self.COMM.Alltoallw(MPI.IN_PLACE, rmsg)
except NotImplementedError:
self.skipTest('mpi-alltoallw')
for i in range(size):
row = rbuf[i*size:(i+1)*size]
a, b = row[:count], row[count:]
for va in a:
self.assertEqual(va, i)
for vb in b:
self.assertEqual(vb, -1)
def testAlltoallw2(self):
size = self.COMM.Get_size()
rank = self.COMM.Get_rank()
for array in arrayimpl.ArrayTypes:
for typecode in arrayimpl.TypeMap:
for count in range(size):
rbuf = array(-1, typecode, size*size)
for i in range(size):
for j in range(count):
rbuf[i*size+j] = rank
rdt = rbuf.mpidtype
rdsp = list(range(0, size*size*rdt.extent, size*rdt.extent))
rmsg = (rbuf.as_raw(), [count]*size, rdsp, [rdt]*size)
try:
self.COMM.Alltoallw(MPI.IN_PLACE, rmsg)
except NotImplementedError:
self.skipTest('mpi-alltoallw')
for i in range(size):
row = rbuf[i*size:(i+1)*size]
a, b = row[:count], row[count:]
for va in a:
self.assertEqual(va, i)
for vb in b:
self.assertEqual(vb, -1)
class TestCCOVecSelf(BaseTestCCOVec, unittest.TestCase):
COMM = MPI.COMM_SELF
class TestCCOVecWorld(BaseTestCCOVec, unittest.TestCase):
COMM = MPI.COMM_WORLD
class TestCCOVecSelfDup(TestCCOVecSelf):
def setUp(self):
self.COMM = MPI.COMM_SELF.Dup()
def tearDown(self):
self.COMM.Free()
class TestCCOVecWorldDup(TestCCOVecWorld):
def setUp(self):
self.COMM = MPI.COMM_WORLD.Dup()
def tearDown(self):
self.COMM.Free()
class TestCCOVecInplaceSelf(BaseTestCCOVecInplace, unittest.TestCase):
COMM = MPI.COMM_SELF
class TestCCOVecInplaceWorld(BaseTestCCOVecInplace, unittest.TestCase):
COMM = MPI.COMM_WORLD
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_mpimem.py 0000644 0001750 0001750 00000001521 13200562156 017650 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
class TestMemory(unittest.TestCase):
def testMemory1(self):
for size in range(0, 10000, 100):
size = max(1, size) # Open MPI
try:
mem1 = MPI.Alloc_mem(size)
self.assertEqual(len(mem1), size)
MPI.Free_mem(mem1)
except NotImplementedError:
self.skipTest('mpi-alloc_mem')
def testMemory2(self):
for size in range(0, 10000, 100):
size = max(1, size) # Open MPI
try:
mem2 = MPI.Alloc_mem(size, MPI.INFO_NULL)
self.assertEqual(len(mem2), size)
MPI.Free_mem(mem2)
except NotImplementedError:
self.skipTest('mpi-alloc_mem')
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_doc.py 0000644 0001750 0001750 00000003404 13200562156 017133 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
import sys, types
ModuleType = type(MPI)
ClassType = type(MPI.Comm)
FunctionType = type(MPI.Init)
MethodDescrType = type(MPI.Comm.Get_rank)
GetSetDescrType = type(MPI.Comm.rank)
def getdocstr(mc, docstrings, namespace=None):
name = getattr(mc, '__name__', None)
if name is None: return
if name in ('__builtin__', 'builtins'): return
if name.startswith('_'): return
if namespace: name = '%s.%s' % (namespace, name)
if type(mc) in (ModuleType, ClassType):
doc = getattr(mc, '__doc__', None)
if doc == "": return
docstrings[name] = doc
for k, v in vars(mc).items():
if isinstance(v, staticmethod):
v = v.__get__(object)
getdocstr(v, docstrings, name)
elif type(mc) in (FunctionType, MethodDescrType, GetSetDescrType):
doc = getattr(mc, '__doc__', None)
if doc == "": return
docstrings[name] = doc
class TestDoc(unittest.TestCase):
@unittest.skipIf(hasattr(sys, 'pypy_version_info'), 'pypy')
def testDoc(self):
missing = False
docs = { }
getdocstr(MPI, docs)
for k in docs:
if not k.startswith('_'):
doc = docs[k]
if doc is None:
print ("'%s': missing docstring" % k)
missing = True
else:
doc = doc.strip()
if not doc:
print ("'%s': empty docstring" % k)
missing = True
if 'mpi4py.MPI' in doc:
print ("'%s': bad format docstring" % k)
self.assertFalse(missing)
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/test/test_address.py 0000644 0001750 0001750 00000003315 13200562156 020014 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import mpiunittest as unittest
try:
import array
except ImportError:
array = None
try:
import numpy
except ImportError:
numpy = None
class TestAddress(unittest.TestCase):
@unittest.skipIf(array is None, 'array')
def testGetAddress1(self):
from struct import pack, unpack
location = array.array('i', range(10))
bufptr, _ = location.buffer_info()
addr = MPI.Get_address(location)
addr = unpack('P', pack('P', addr))[0]
self.assertEqual(addr, bufptr)
@unittest.skipIf(numpy is None, 'numpy')
def testGetAddress2(self):
from struct import pack, unpack
location = numpy.asarray(range(10), dtype='i')
bufptr, _ = location.__array_interface__['data']
addr = MPI.Get_address(location)
addr = unpack('P', pack('P', addr))[0]
self.assertEqual(addr, bufptr)
@unittest.skipMPI('openmpi(<=1.10.2)')
def testBottom(self):
base = MPI.Get_address(MPI.BOTTOM)
addr = MPI.Aint_add(base, 0)
self.assertEqual(addr, base)
diff = MPI.Aint_diff(base, base)
self.assertEqual(diff, 0)
@unittest.skipIf(array is None, 'array')
def testAintAdd(self):
location = array.array('i', range(10))
base = MPI.Get_address(location)
addr = MPI.Aint_add(base, 4)
self.assertEqual(addr, base + 4)
@unittest.skipIf(array is None, 'array')
def testAintDiff(self):
location = array.array('i', range(10))
base = MPI.Get_address(location)
addr1 = base + 8
addr2 = base + 4
diff = MPI.Aint_diff(addr1, addr2)
self.assertEqual(diff, 4)
if __name__ == '__main__':
unittest.main()
mpi4py-3.0.3/mpi.cfg 0000644 0001750 0001750 00000012063 13200562156 015245 0 ustar dalcinl dalcinl 0000000 0000000 # Some Linux distributions have RPM's for some MPI implementations.
# In such a case, headers and libraries usually are in default system
# locations, and you should not need any special configuration.
# If you do not have MPI distribution in a default location, please
# uncomment and fill-in appropriately the following lines. Yo can use
# as examples the [mpich2], [openmpi], and [deinompi] sections
# below the [mpi] section (wich is the one used by default).
# If you specify multiple locations for includes and libraries,
# please separate them with the path separator for your platform,
# i.e., ':' on Unix-like systems and ';' on Windows
# Default configuration
# ---------------------
[mpi]
## mpi_dir = /usr
## mpi_dir = /usr/local
## mpi_dir = /usr/local/mpi
## mpi_dir = /opt
## mpi_dir = /opt/mpi
## mpi_dir = = $ProgramFiles\MPI
## mpicc = %(mpi_dir)s/bin/mpicc
## mpicxx = %(mpi_dir)s/bin/mpicxx
## define_macros =
## undef_macros =
## include_dirs = %(mpi_dir)s/include
## libraries = mpi
## library_dirs = %(mpi_dir)s/lib
## runtime_library_dirs = %(mpi_dir)s/lib
## extra_compile_args =
## extra_link_args =
## extra_objects =
# MPICH3 example
# --------------
[mpich3]
mpi_dir = /home/devel/mpi/mpich-3.1.4
mpicc = %(mpi_dir)s/bin/mpicc
mpicxx = %(mpi_dir)s/bin/mpicxx
#include_dirs = %(mpi_dir)s/include
#libraries = mpich opa mpl rt pthread
#library_dirs = %(mpi_dir)s/lib
#runtime_library_dirs = %(library_dirs)s
# Open MPI example
# ----------------
[openmpi]
mpi_dir = /home/devel/mpi/openmpi-1.8.6
mpicc = %(mpi_dir)s/bin/mpicc
mpicxx = %(mpi_dir)s/bin/mpicxx
#include_dirs = %(mpi_dir)s/include
#libraries = mpi
library_dirs = %(mpi_dir)s/lib
runtime_library_dirs = %(library_dirs)s
# MPICH2 example
# --------------
[mpich2]
mpi_dir = /home/devel/mpi/mpich2-1.4.1
mpicc = %(mpi_dir)s/bin/mpicc
mpicxx = %(mpi_dir)s/bin/mpicxx
#include_dirs = %(mpi_dir)s/include
#libraries = mpich opa mpl
#library_dirs = %(mpi_dir)s/lib
#runtime_library_dirs = %(library_dirs)s
# Sun MPI example
# ---------------
[sunmpi]
#mpi_dir = /opt/SUNWhpc/HPC8.2.1/gnu
mpi_dir = /opt/SUNWhpc/HPC8.1/sun
mpicc = %(mpi_dir)s/bin/mpicc
mpicxx = %(mpi_dir)s/bin/mpicxx
#include_dirs = %(mpi_dir)s/include
#libraries = mpi open-rte open-pal
library_dirs = %(mpi_dir)s/lib
runtime_library_dirs = %(library_dirs)s
# Platform MPI example
# --------------------
[pcmpi-linux-64bit]
mpi_dir = /opt/ibm/platform_mpi
mpicc = %(mpi_dir)s/bin/mpicc
mpicxx = %(mpi_dir)s/bin/mpiCC
define_macros = NON_BLOCKING_COLLECTIVES
runtime_library_dirs = %(mpi_dir)s/lib/linux_amd64
[pcmpi-linux-32bit]
mpi_dir = /opt/ibm/platform_mpi
mpicc = %(mpi_dir)s/bin/mpicc
mpicxx = %(mpi_dir)s/bin/mpiCC
define_macros = NON_BLOCKING_COLLECTIVES
runtime_library_dirs = %(mpi_dir)s/lib/linux_ia32
# SGI MPI example
# ---------------
[sgimpi]
define_macros = SGI_MPI=1
mpi_dir = /usr
mpicc = icc
mpicxx = icpc
include_dirs = %(mpi_dir)s/include
libraries = mpi
library_dirs = %(mpi_dir)s/lib
runtime_library_dirs = %(library_dirs)s
# IBM POE/MPI example
# -------------------
[poempi]
mpicc = mpcc_r
mpicxx = mpCC_r
# Microsoft MPI example
# ---------------------
[msmpi-32bit]
mpi_dir = $ProgramFiles\Microsoft SDKs\MPI
include_dirs = %(mpi_dir)s\Include
libraries = msmpi
library_dirs = %(mpi_dir)s\Lib\x86
[msmpi-64bit]
mpi_dir = $ProgramFiles\Microsoft SDKs\MPI
include_dirs = %(mpi_dir)s\Include
libraries = msmpi
library_dirs = %(mpi_dir)s\Lib\x64
# SiCortex MPI example
# --------------------
[sicortex]
mpicc = mpicc --gnu
mpicxx = mpicxx --gnu
# LAM/MPI example
# ---------------
[lammpi]
mpi_dir = /home/devel/mpi/lam-7.1.4
mpicc = %(mpi_dir)s/bin/mpicc
mpicxx = %(mpi_dir)s/bin/mpic++
include_dirs = %(mpi_dir)s/include
libraries = lammpio mpi lam
library_dirs = %(mpi_dir)s/lib
runtime_library_dirs = %(library_dirs)s
# MPICH1 example
# --------------
[mpich1]
mpi_dir = /home/devel/mpi/mpich-1.2.7p1
mpicc = %(mpi_dir)s/bin/mpicc
mpicxx = %(mpi_dir)s/bin/mpicxx
include_dirs = %(mpi_dir)s/include
libraries = mpich
library_dirs = %(mpi_dir)s/lib/shared:%(mpi_dir)s/lib
runtime_library_dirs = %(mpi_dir)s/lib/shared
# Fake MPI, just for testing
# --------------------------
[fakempi]
mpicc = cc
mpicxx = c++
include_dirs = misc/fakempi
# PETSc MPIUNI, just for testing
# ------------------------------
[mpiuni]
mpicc = cc
mpicxx = c++
include_dirs = misc/mpiuni:$PETSC_DIR/include:$PETSC_DIR/$PETSC_ARCH/include
mpi4py-3.0.3/DESCRIPTION.rst 0000644 0001750 0001750 00000006421 13200562156 016315 0 ustar dalcinl dalcinl 0000000 0000000 MPI for Python
==============
This package provides Python bindings for the **Message Passing
Interface** (MPI_) standard. It is implemented on top of the MPI-1/2/3
specification and exposes an API which grounds on the standard MPI-2
C++ bindings.
.. _MPI: http://www.mpi-forum.org/
Features
--------
This package supports:
* Convenient communication of any *picklable* Python object
+ point-to-point (send & receive)
+ collective (broadcast, scatter & gather, reductions)
* Fast communication of Python object exposing the *Python buffer
interface* (NumPy arrays, builtin bytes/string/array objects)
+ point-to-point (blocking/nonbloking/persistent send & receive)
+ collective (broadcast, block/vector scatter & gather, reductions)
* Process groups and communication domains
+ Creation of new intra/inter communicators
+ Cartesian & graph topologies
* Parallel input/output:
+ read & write
+ blocking/nonbloking & collective/noncollective
+ individual/shared file pointers & explicit offset
* Dynamic process management
+ spawn & spawn multiple
+ accept/connect
+ name publishing & lookup
* One-sided operations
+ remote memory access (put, get, accumulate)
+ passive target syncronization (start/complete & post/wait)
+ active target syncronization (lock & unlock)
Install
-------
Once you have a working MPI implementation and the ``mpicc`` compiler
wrapper is on your search path, you can install this package
* using ``pip``::
$ pip install mpi4py
* using ``easy_install`` (deprecated)::
$ easy_install mpi4py
You can also install the in-development version of mpi4py
* using ``pip``::
$ pip install git+https://bitbucket.org/mpi4py/mpi4py
or::
$ pip install https://bitbucket.org/mpi4py/mpi4py/get/master.tar.gz
* using ``easy_install`` (deprecated)::
$ easy_install git+https://bitbucket.org/mpi4py/mpi4py
or::
$ easy_install https://bitbucket.org/mpi4py/mpi4py/get/master.tar.gz
You can also install it directly on Fedora (as well as RHEL and their
derivatives using the EPEL software repository)
* using ``dnf`` and the ``mpich`` package on ``x86_64``::
$ dnf install mpi4py-mpich
* using ``dnf`` and the ``openmpi`` package on ``x86_64``::
$ dnf install mpi4py-openmpi
Please remember to load the correct module for your choosen MPI environment
* for ``mpich`` package on ``x86_64`` do::
$ module load mpi/mpich-x86_64
$ python -c "import mpi4py"
* for ``openmpi`` package on ``x86_64`` do::
$ module load mpi/openmpi-x86_64
$ python -c "import mpi4py"
Citations
---------
If MPI for Python been significant to a project that leads to an
academic publication, please acknowledge that fact by citing the
project.
* L. Dalcin, P. Kler, R. Paz, and A. Cosimo,
*Parallel Distributed Computing using Python*,
Advances in Water Resources, 34(9):1124-1139, 2011.
http://dx.doi.org/10.1016/j.advwatres.2011.04.013
* L. Dalcin, R. Paz, M. Storti, and J. D'Elia,
*MPI for Python: performance improvements and MPI-2 extensions*,
Journal of Parallel and Distributed Computing, 68(5):655-662, 2008.
http://dx.doi.org/10.1016/j.jpdc.2007.09.005
* L. Dalcin, R. Paz, and M. Storti,
*MPI for Python*,
Journal of Parallel and Distributed Computing, 65(9):1108-1115, 2005.
http://dx.doi.org/10.1016/j.jpdc.2005.03.010
mpi4py-3.0.3/LICENSE.rst 0000644 0001750 0001750 00000002623 13431514445 015620 0 ustar dalcinl dalcinl 0000000 0000000 =======================
LICENSE: MPI for Python
=======================
:Author: Lisandro Dalcin
:Contact: dalcinl@gmail.com
Copyright (c) 2019, Lisandro Dalcin.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mpi4py-3.0.3/PKG-INFO 0000664 0001750 0001750 00000014167 13560002767 015113 0 ustar dalcinl dalcinl 0000000 0000000 Metadata-Version: 1.2
Name: mpi4py
Version: 3.0.3
Summary: Python bindings for MPI
Home-page: https://bitbucket.org/mpi4py/mpi4py/
Author: Lisandro Dalcin
Author-email: dalcinl@gmail.com
Maintainer: Lisandro Dalcin
Maintainer-email: dalcinl@gmail.com
License: BSD
Download-URL: https://bitbucket.org/mpi4py/mpi4py/downloads/mpi4py-3.0.3.tar.gz
Description: MPI for Python
==============
This package provides Python bindings for the **Message Passing
Interface** (MPI_) standard. It is implemented on top of the MPI-1/2/3
specification and exposes an API which grounds on the standard MPI-2
C++ bindings.
.. _MPI: http://www.mpi-forum.org/
Features
--------
This package supports:
* Convenient communication of any *picklable* Python object
+ point-to-point (send & receive)
+ collective (broadcast, scatter & gather, reductions)
* Fast communication of Python object exposing the *Python buffer
interface* (NumPy arrays, builtin bytes/string/array objects)
+ point-to-point (blocking/nonbloking/persistent send & receive)
+ collective (broadcast, block/vector scatter & gather, reductions)
* Process groups and communication domains
+ Creation of new intra/inter communicators
+ Cartesian & graph topologies
* Parallel input/output:
+ read & write
+ blocking/nonbloking & collective/noncollective
+ individual/shared file pointers & explicit offset
* Dynamic process management
+ spawn & spawn multiple
+ accept/connect
+ name publishing & lookup
* One-sided operations
+ remote memory access (put, get, accumulate)
+ passive target syncronization (start/complete & post/wait)
+ active target syncronization (lock & unlock)
Install
-------
Once you have a working MPI implementation and the ``mpicc`` compiler
wrapper is on your search path, you can install this package
* using ``pip``::
$ pip install mpi4py
* using ``easy_install`` (deprecated)::
$ easy_install mpi4py
You can also install the in-development version of mpi4py
* using ``pip``::
$ pip install git+https://bitbucket.org/mpi4py/mpi4py
or::
$ pip install https://bitbucket.org/mpi4py/mpi4py/get/master.tar.gz
* using ``easy_install`` (deprecated)::
$ easy_install git+https://bitbucket.org/mpi4py/mpi4py
or::
$ easy_install https://bitbucket.org/mpi4py/mpi4py/get/master.tar.gz
You can also install it directly on Fedora (as well as RHEL and their
derivatives using the EPEL software repository)
* using ``dnf`` and the ``mpich`` package on ``x86_64``::
$ dnf install mpi4py-mpich
* using ``dnf`` and the ``openmpi`` package on ``x86_64``::
$ dnf install mpi4py-openmpi
Please remember to load the correct module for your choosen MPI environment
* for ``mpich`` package on ``x86_64`` do::
$ module load mpi/mpich-x86_64
$ python -c "import mpi4py"
* for ``openmpi`` package on ``x86_64`` do::
$ module load mpi/openmpi-x86_64
$ python -c "import mpi4py"
Citations
---------
If MPI for Python been significant to a project that leads to an
academic publication, please acknowledge that fact by citing the
project.
* L. Dalcin, P. Kler, R. Paz, and A. Cosimo,
*Parallel Distributed Computing using Python*,
Advances in Water Resources, 34(9):1124-1139, 2011.
http://dx.doi.org/10.1016/j.advwatres.2011.04.013
* L. Dalcin, R. Paz, M. Storti, and J. D'Elia,
*MPI for Python: performance improvements and MPI-2 extensions*,
Journal of Parallel and Distributed Computing, 68(5):655-662, 2008.
http://dx.doi.org/10.1016/j.jpdc.2007.09.005
* L. Dalcin, R. Paz, and M. Storti,
*MPI for Python*,
Journal of Parallel and Distributed Computing, 65(9):1108-1115, 2005.
http://dx.doi.org/10.1016/j.jpdc.2005.03.010
Keywords: scientific computing,parallel computing,message passing interface,MPI
Platform: Mac OS X
Platform: Linux
Platform: Solaris
Platform: Unix
Platform: Windows
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: BSD License
Classifier: Operating System :: MacOS :: MacOS X
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: POSIX
Classifier: Operating System :: POSIX :: Linux
Classifier: Operating System :: POSIX :: SunOS/Solaris
Classifier: Operating System :: Unix
Classifier: Programming Language :: C
Classifier: Programming Language :: Cython
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 2
Classifier: Programming Language :: Python :: 2.7
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.3
Classifier: Programming Language :: Python :: 3.4
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: Implementation :: PyPy
Classifier: Topic :: Scientific/Engineering
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: System :: Distributed Computing
Provides: mpi4py
mpi4py-3.0.3/demo/ 0000775 0001750 0001750 00000000000 13560002767 014731 5 ustar dalcinl dalcinl 0000000 0000000 mpi4py-3.0.3/demo/osu_barrier.py 0000644 0001750 0001750 00000002110 12750576064 017615 0 ustar dalcinl dalcinl 0000000 0000000 # http://mvapich.cse.ohio-state.edu/benchmarks/
from mpi4py import MPI
def osu_bcast(
BENCHMARH = "MPI Barrier Latency Test",
skip = 1000,
loop = 10000,
skip_large = 10,
loop_large = 100,
large_message_size = 8192,
MAX_MSG_SIZE = 1<<20,
):
comm = MPI.COMM_WORLD
myid = comm.Get_rank()
numprocs = comm.Get_size()
if numprocs < 2:
if myid == 0:
errmsg = "This test requires at least two processes"
else:
errmsg = None
raise SystemExit(errmsg)
if myid == 0:
print ('# %s' % (BENCHMARH,))
if myid == 0:
print ('# %-8s%20s' % ("Size [B]", "Latency [us]"))
skip = skip_large
loop = loop_large
iterations = list(range(loop+skip))
#
comm.Barrier()
for i in iterations:
if i == skip:
t_start = MPI.Wtime()
comm.Barrier()
t_end = MPI.Wtime()
comm.Barrier()
#
if myid == 0:
latency = (t_end - t_start) * 1e6 / loop
print ('%-10d%20.2f' % (0, latency))
if __name__ == '__main__':
osu_bcast()
mpi4py-3.0.3/demo/mpi-ref-v1/ 0000775 0001750 0001750 00000000000 13560002767 016614 5 ustar dalcinl dalcinl 0000000 0000000 mpi4py-3.0.3/demo/mpi-ref-v1/ex-3.04.py 0000644 0001750 0001750 00000000227 12523721016 020154 0 ustar dalcinl dalcinl 0000000 0000000 execfile('ex-3.02.py')
count = 3
newtype = dtype.Create_contiguous(count)
assert newtype.extent == dtype.extent * count
dtype.Free()
newtype.Free()
mpi4py-3.0.3/demo/mpi-ref-v1/runtests.sh 0000755 0001750 0001750 00000001515 12523721016 021033 0 ustar dalcinl dalcinl 0000000 0000000 #!/bin/sh
MPIEXEC=mpiexec
NP_FLAG=-n
NP=3
PYTHON=python
set -x
$MPIEXEC $NP_FLAG $NP $PYTHON ex-2.01.py
$MPIEXEC $NP_FLAG $NP $PYTHON ex-2.08.py
$MPIEXEC $NP_FLAG $NP $PYTHON ex-2.16.py
$MPIEXEC $NP_FLAG $NP $PYTHON ex-2.29.py
$MPIEXEC $NP_FLAG $NP $PYTHON ex-2.32.py
$MPIEXEC $NP_FLAG $NP $PYTHON ex-2.34.py
$MPIEXEC $NP_FLAG $NP $PYTHON ex-2.35.py
$MPIEXEC $NP_FLAG $NP $PYTHON ex-3.01.py
$MPIEXEC $NP_FLAG $NP $PYTHON ex-3.02.py
$MPIEXEC $NP_FLAG $NP $PYTHON ex-3.03.py
$MPIEXEC $NP_FLAG $NP $PYTHON ex-3.04.py
$MPIEXEC $NP_FLAG $NP $PYTHON ex-3.05.py
$MPIEXEC $NP_FLAG $NP $PYTHON ex-3.06.py
$MPIEXEC $NP_FLAG $NP $PYTHON ex-3.07.py
$MPIEXEC $NP_FLAG $NP $PYTHON ex-3.08.py
$MPIEXEC $NP_FLAG $NP $PYTHON ex-3.09.py
$MPIEXEC $NP_FLAG $NP $PYTHON ex-3.11.py
$MPIEXEC $NP_FLAG $NP $PYTHON ex-3.12.py
$MPIEXEC $NP_FLAG $NP $PYTHON ex-3.13.py
mpi4py-3.0.3/demo/mpi-ref-v1/ex-3.09.py 0000644 0001750 0001750 00000002040 12523721016 020154 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
try:
import numpy
except ImportError:
raise SystemExit
# transpose a matrix a into b
a = numpy.empty((100, 100), dtype=float, order='fortran')
b = numpy.empty((100, 100), dtype=float, order='fortran')
a.flat = numpy.arange(a.size, dtype=float)
lb, sizeofdouble = MPI.DOUBLE.Get_extent()
# create datatype dor one row
# (vector with 100 double entries and stride 100)
row = MPI.DOUBLE.Create_vector(100, 1, 100)
# create datatype for matrix in row-major order
# (one hundred copies of the row datatype, strided one word
# apart; the succesive row datatypes are interleaved)
xpose = row.Create_hvector(100, 1, sizeofdouble)
xpose.Commit()
# send matrix in row-major order and receive in column major order
abuf = (a, xpose)
bbuf = (b, MPI.DOUBLE)
myrank = MPI.COMM_WORLD.Get_rank()
status = MPI.Status()
MPI.COMM_WORLD.Sendrecv(abuf, myrank, 0, bbuf, myrank, 0, status)
assert numpy.allclose(a, b.transpose())
assert status.Get_count(xpose) == 1
assert status.Get_count(MPI.DOUBLE) == b.size
row.Free()
xpose.Free()
mpi4py-3.0.3/demo/mpi-ref-v1/runtests.bat 0000644 0001750 0001750 00000002222 13200562156 021161 0 ustar dalcinl dalcinl 0000000 0000000 @echo off
setlocal ENABLEEXTENSIONS
set MPI=Microsoft MPI
set PATH="%ProgramFiles%\%MPI%\bin";%PATH%
set MPIEXEC=mpiexec
set NP_FLAG=-n
set NP=5
set PYTHON=C:\Python27\python.exe
set PYTHON=C:\Python36\python.exe
set PYTHON=python
@echo on
set MPIEXEC=
set NP_FLAG=
set NP=
%MPIEXEC% %NP_FLAG% %NP% %PYTHON% ex-2.01.py
%MPIEXEC% %NP_FLAG% %NP% %PYTHON% ex-2.08.py
%MPIEXEC% %NP_FLAG% %NP% %PYTHON% ex-2.16.py
%MPIEXEC% %NP_FLAG% %NP% %PYTHON% ex-2.29.py
%MPIEXEC% %NP_FLAG% %NP% %PYTHON% ex-2.32.py
%MPIEXEC% %NP_FLAG% %NP% %PYTHON% ex-2.34.py
%MPIEXEC% %NP_FLAG% %NP% %PYTHON% ex-2.35.py
%MPIEXEC% %NP_FLAG% %NP% %PYTHON% ex-3.01.py
%MPIEXEC% %NP_FLAG% %NP% %PYTHON% ex-3.02.py
%MPIEXEC% %NP_FLAG% %NP% %PYTHON% ex-3.03.py
%MPIEXEC% %NP_FLAG% %NP% %PYTHON% ex-3.04.py
%MPIEXEC% %NP_FLAG% %NP% %PYTHON% ex-3.05.py
%MPIEXEC% %NP_FLAG% %NP% %PYTHON% ex-3.06.py
%MPIEXEC% %NP_FLAG% %NP% %PYTHON% ex-3.07.py
%MPIEXEC% %NP_FLAG% %NP% %PYTHON% ex-3.08.py
%MPIEXEC% %NP_FLAG% %NP% %PYTHON% ex-3.09.py
%MPIEXEC% %NP_FLAG% %NP% %PYTHON% ex-3.11.py
%MPIEXEC% %NP_FLAG% %NP% %PYTHON% ex-3.12.py
%MPIEXEC% %NP_FLAG% %NP% %PYTHON% ex-3.13.py
mpi4py-3.0.3/demo/mpi-ref-v1/ex-2.01.py 0000644 0001750 0001750 00000001757 12523721016 020161 0 ustar dalcinl dalcinl 0000000 0000000 ## mpiexec -n 2 python ex-2.01.py
# Process 0 sends a message to process 1
# --------------------------------------------------------------------
from mpi4py import MPI
import array
if MPI.COMM_WORLD.Get_size() < 2:
raise SystemExit
# --------------------------------------------------------------------
s = "Hello there"
msg = array.array('c', '\0'*20)
tag = 99
status = MPI.Status()
myrank = MPI.COMM_WORLD.Get_rank()
if myrank == 0:
msg[:len(s)] = array.array('c', s)
MPI.COMM_WORLD.Send([msg, len(s)+1, MPI.CHAR], 1, tag)
elif myrank == 1:
MPI.COMM_WORLD.Recv([msg, 20, MPI.CHAR], 0, tag, status)
# --------------------------------------------------------------------
if myrank == 1:
assert list(msg[:len(s)]) == list(s)
assert msg[len(s)] == '\0'
assert status.source == 0
assert status.tag == tag
assert status.error == MPI.SUCCESS
assert status.Get_count(MPI.CHAR) == len(s)+1
# --------------------------------------------------------------------
mpi4py-3.0.3/demo/mpi-ref-v1/makefile 0000644 0001750 0001750 00000000600 12523721016 020277 0 ustar dalcinl dalcinl 0000000 0000000 .PHONY: default build test clean test_seq test_mpi
default: build test clean
build:
PYTHON = python
MPIEXEC = mpiexec
NP_FLAG = -n
NP = 3
test_seq:
${MAKE} MPIEXEC= NP_FLAG= NP= test_mpi
test_mpi:
-@for i in `ls ex-*.py`; do \
echo ${MPIEXEC} ${NP_FLAG} ${NP} ${PYTHON} $$i; \
${MPIEXEC} ${NP_FLAG} ${NP} ${PYTHON} $$i; \
done
test: test_seq test_mpi
clean:
mpi4py-3.0.3/demo/mpi-ref-v1/ex-3.05.py 0000644 0001750 0001750 00000000277 12523721016 020162 0 ustar dalcinl dalcinl 0000000 0000000 execfile('ex-3.02.py')
count = 2
blklen = 3
stride = 4
newtype = dtype.Create_vector(count, blklen, stride)
assert newtype.size == dtype.size * count * blklen
dtype.Free()
newtype.Free()
mpi4py-3.0.3/demo/mpi-ref-v1/ex-2.35.py 0000644 0001750 0001750 00000001337 12523721016 020162 0 ustar dalcinl dalcinl 0000000 0000000 ## mpiexec -n 1 python ex-2.35.py
# Calls to attach and detach buffers
# --------------------------------------------------------------------
from mpi4py import MPI
try:
from numpy import empty
except ImportError:
from array import array
def empty(size, dtype):
return array(dtype, [0]*size)
# --------------------------------------------------------------------
BUFSISE = 10000 + MPI.BSEND_OVERHEAD
buff = empty(BUFSISE, dtype='b')
MPI.Attach_buffer(buff)
buff2 = MPI.Detach_buffer()
MPI.Attach_buffer(buff2)
MPI.Detach_buffer()
# --------------------------------------------------------------------
assert len(buff2) == BUFSISE
# --------------------------------------------------------------------
mpi4py-3.0.3/demo/mpi-ref-v1/ex-3.03.py 0000644 0001750 0001750 00000000175 12523721016 020155 0 ustar dalcinl dalcinl 0000000 0000000 execfile('ex-3.02.py')
assert dtype.size == MPI.DOUBLE.size + MPI.CHAR.size
assert dtype.extent >= dtype.size
dtype.Free()
mpi4py-3.0.3/demo/mpi-ref-v1/README.txt 0000644 0001750 0001750 00000000660 12523721016 020303 0 ustar dalcinl dalcinl 0000000 0000000 @Book{MPI-Ref-V1,
title = {{MPI} - The Complete Reference: Volume 1, The {MPI} Core},
author = {Marc Snir and Steve Otto and Steven Huss-Lederman
and David Walker and Jack Dongarra},
edition = {2nd.},
year = 1998,
publisher = {MIT Press},
volume = {1, The MPI Core},
series = {Scientific and Engineering Computation},
address = {Cambridge, MA, USA},
}
mpi4py-3.0.3/demo/mpi-ref-v1/ex-2.29.py 0000644 0001750 0001750 00000002230 12523721016 020156 0 ustar dalcinl dalcinl 0000000 0000000 ## mpiexec -n 3 python ex-2.29.py
# Use a blocking probe to wait for an incoming message
# --------------------------------------------------------------------
from mpi4py import MPI
import array
if MPI.COMM_WORLD.Get_size() < 3:
raise SystemExit
# --------------------------------------------------------------------
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
if rank == 0:
i = array.array('i', [7]*5)
comm.Send([i, MPI.INT], 2, 0)
elif rank == 1:
x = array.array('f', [7]*5)
comm.Send([x, MPI.FLOAT], 2, 0)
elif rank == 2:
i = array.array('i', [0]*5)
x = array.array('f', [0]*5)
status = MPI.Status()
for j in range(2):
comm.Probe(MPI.ANY_SOURCE, 0, status)
if status.Get_source() == 0:
comm.Recv([i, MPI.INT], 0, 0, status)
else:
comm.Recv([x, MPI.FLOAT], 1, 0, status)
# --------------------------------------------------------------------
if rank == 2:
for v in i: assert v == 7
for v in x: assert v == 7
assert status.source in (0, 1)
assert status.tag == 0
assert status.error == 0
# --------------------------------------------------------------------
mpi4py-3.0.3/demo/mpi-ref-v1/ex-3.08.py 0000644 0001750 0001750 00000001602 12523721016 020156 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
try:
import numpy
except ImportError:
raise SystemExit
# extract the section a[0:6:2, 0:5:2] and store it in e[:,:]
a = numpy.empty((6, 5), dtype=float, order='fortran')
e = numpy.empty((3, 3), dtype=float, order='fortran')
a.flat = numpy.arange(a.size, dtype=float)
lb, sizeofdouble = MPI.DOUBLE.Get_extent()
# create datatype for a 1D section
oneslice = MPI.DOUBLE.Create_vector(3, 1, 2)
# create datatype for a 2D section
twoslice = oneslice.Create_hvector(3, 1, 12*sizeofdouble)
twoslice.Commit()
# send and recv on same process
myrank = MPI.COMM_WORLD.Get_rank()
status = MPI.Status()
MPI.COMM_WORLD.Sendrecv([a, 1, twoslice], myrank, 0,
(e, MPI.DOUBLE), myrank, 0, status)
assert numpy.allclose(a[::2, ::2], e)
assert status.Get_count(twoslice) == 1
assert status.Get_count(MPI.DOUBLE) == e.size
oneslice.Free()
twoslice.Free()
mpi4py-3.0.3/demo/mpi-ref-v1/ex-2.16.py 0000644 0001750 0001750 00000003514 12523721016 020160 0 ustar dalcinl dalcinl 0000000 0000000 ## mpiexec -n 4 python ex-2.16.py
# Jacobi code
# version of parallel code using sendrecv and null proceses.
# --------------------------------------------------------------------
from mpi4py import MPI
try:
import numpy
except ImportError:
raise SystemExit
# --------------------------------------------------------------------
n = 5 * MPI.COMM_WORLD.Get_size()
# compute number of processes and myrank
p = MPI.COMM_WORLD.Get_size()
myrank = MPI.COMM_WORLD.Get_rank()
# compute size of local block
m = n/p
if myrank < (n - p * m):
m = m + 1
#compute neighbors
if myrank == 0:
left = MPI.PROC_NULL
else:
left = myrank - 1
if myrank == p - 1:
right = MPI.PROC_NULL
else:
right = myrank + 1
# allocate local arrays
A = numpy.empty((n+2, m+2), dtype='d', order='fortran')
B = numpy.empty((n, m), dtype='d', order='fortran')
A.fill(1)
A[0, :] = A[-1, :] = 0
A[:, 0] = A[:, -1] = 0
# main loop
converged = False
while not converged:
# compute, B = 0.25 * ( N + S + E + W)
N, S = A[:-2, 1:-1], A[2:, 1:-1]
E, W = A[1:-1, :-2], A[1:-1, 2:]
numpy.add(N, S, B)
numpy.add(E, B, B)
numpy.add(W, B, B)
B *= 0.25
A[1:-1, 1:-1] = B
# communicate
tag = 0
MPI.COMM_WORLD.Sendrecv([B[:, -1], MPI.DOUBLE], right, tag,
[A[:, 0], MPI.DOUBLE], left, tag)
MPI.COMM_WORLD.Sendrecv((B[:, 0], MPI.DOUBLE), left, tag,
(A[:, -1], MPI.DOUBLE), right, tag)
# convergence
myconv = numpy.allclose(B, 0)
loc_conv = numpy.asarray(myconv, dtype='i')
glb_conv = numpy.asarray(0, dtype='i')
MPI.COMM_WORLD.Allreduce([loc_conv, MPI.INT],
[glb_conv, MPI.INT],
op=MPI.LAND)
converged = bool(glb_conv)
# --------------------------------------------------------------------
mpi4py-3.0.3/demo/mpi-ref-v1/ex-2.08.py 0000644 0001750 0001750 00000002465 12523721016 020165 0 ustar dalcinl dalcinl 0000000 0000000 ## mpiexec -n 2 python ex-2.08.py
# An exchange of messages
# --------------------------------------------------------------------
from mpi4py import MPI
import array
if MPI.COMM_WORLD.Get_size() < 2:
raise SystemExit
# --------------------------------------------------------------------
sendbuf = array.array('d', [0]*10)
recvbuf = array.array('d', [0]*10)
tag = 0
status = MPI.Status()
myrank = MPI.COMM_WORLD.Get_rank()
if myrank == 0:
sendbuf[:] = array.array('d', range(len(sendbuf)))
MPI.COMM_WORLD.Send([sendbuf, MPI.DOUBLE], 1, tag)
MPI.COMM_WORLD.Recv([recvbuf, MPI.DOUBLE], 1, tag, status)
elif myrank == 1:
MPI.COMM_WORLD.Recv([recvbuf, MPI.DOUBLE], 0, tag, status)
sendbuf[:] = recvbuf
MPI.COMM_WORLD.Send([sendbuf, MPI.DOUBLE], 0, tag)
# --------------------------------------------------------------------
if myrank == 0:
assert status.source == 1
assert status.tag == tag
assert status.error == MPI.SUCCESS
assert status.Get_count(MPI.DOUBLE) == len(recvbuf)
assert sendbuf == recvbuf
elif myrank == 1:
assert status.source == 0
assert status.tag == tag
assert status.error == MPI.SUCCESS
assert status.Get_count(MPI.DOUBLE) == len(recvbuf)
assert sendbuf == recvbuf
# --------------------------------------------------------------------
mpi4py-3.0.3/demo/mpi-ref-v1/ex-3.07.py 0000644 0001750 0001750 00000000317 12523721016 020157 0 ustar dalcinl dalcinl 0000000 0000000 execfile('ex-3.02.py')
count = 2
blklen = 3
stride = 4 * dtype.extent
newtype = dtype.Create_hvector(count, blklen, stride)
assert newtype.size == dtype.size * count * blklen
dtype.Free()
newtype.Free()
mpi4py-3.0.3/demo/mpi-ref-v1/ex-3.13.py 0000644 0001750 0001750 00000000442 12523721016 020153 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
blens = (1, 1)
disps = (0, MPI.DOUBLE.size)
types = (MPI.DOUBLE, MPI.CHAR)
type1 = MPI.Datatype.Create_struct(blens, disps, types)
B = (2, 1, 3)
D = (0, 16, 26)
T = (MPI.FLOAT, type1, MPI.CHAR)
dtype = MPI.Datatype.Create_struct(B, D, T)
type1.Free()
dtype.Free()
mpi4py-3.0.3/demo/mpi-ref-v1/ex-2.32.py 0000644 0001750 0001750 00000004413 12523721016 020155 0 ustar dalcinl dalcinl 0000000 0000000 # Jacobi computation, using persitent requests
from mpi4py import MPI
try:
import numpy
except ImportError:
raise SystemExit
n = 5 * MPI.COMM_WORLD.Get_size()
# compute number of processes and myrank
p = MPI.COMM_WORLD.Get_size()
myrank = MPI.COMM_WORLD.Get_rank()
# compute size of local block
m = n/p
if myrank < (n - p * m):
m = m + 1
#compute neighbors
if myrank == 0:
left = MPI.PROC_NULL
else:
left = myrank - 1
if myrank == p - 1:
right = MPI.PROC_NULL
else:
right = myrank + 1
# allocate local arrays
A = numpy.empty((n+2, m+2), dtype=float, order='fortran')
B = numpy.empty((n, m), dtype=float, order='fortran')
A.fill(1)
A[0, :] = A[-1, :] = 0
A[:, 0] = A[:, -1] = 0
# create persintent requests
tag = 0
sreq1 = MPI.COMM_WORLD.Send_init((B[:, 0], MPI.DOUBLE), left, tag)
sreq2 = MPI.COMM_WORLD.Send_init((B[:, -1], MPI.DOUBLE), right, tag)
rreq1 = MPI.COMM_WORLD.Recv_init((A[:, 0], MPI.DOUBLE), left, tag)
rreq2 = MPI.COMM_WORLD.Recv_init((A[:, -1], MPI.DOUBLE), right, tag)
reqlist = [sreq1, sreq2, rreq1, rreq2]
for req in reqlist:
assert req != MPI.REQUEST_NULL
# main loop
converged = False
while not converged:
# compute boundary columns
N, S = A[ :-2, 1], A[2:, 1]
E, W = A[1:-1, 0], A[1:-1, 2]
C = B[:, 0]
numpy.add(N, S, C)
numpy.add(C, E, C)
numpy.add(C, W, C)
C *= 0.25
N, S = A[ :-2, -2], A[2:, -2]
E, W = A[1:-1, -3], A[1:-1, -1]
C = B[:, -1]
numpy.add(N, S, C)
numpy.add(C, E, C)
numpy.add(C, W, C)
C *= 0.25
# start communication
#MPI.Prequest.Startall(reqlist)
for r in reqlist:
r.Start()
# compute interior
N, S = A[ :-2, 2:-2], A[2, 2:-2]
E, W = A[1:-1, 2:-2], A[1:-1, 2:-2]
C = B[:, 1:-1]
numpy.add(N, S, C)
numpy.add(E, C, C)
numpy.add(W, C, C)
C *= 0.25
A[1:-1, 1:-1] = B
# complete communication
MPI.Prequest.Waitall(reqlist)
# convergence
myconv = numpy.allclose(B, 0)
loc_conv = numpy.asarray(myconv, dtype='i')
glb_conv = numpy.asarray(0, dtype='i')
MPI.COMM_WORLD.Allreduce([loc_conv, MPI.INT],
[glb_conv, MPI.INT],
op=MPI.LAND)
converged = bool(glb_conv)
# free persintent requests
for req in reqlist:
req.Free()
mpi4py-3.0.3/demo/mpi-ref-v1/ex-3.06.py 0000644 0001750 0001750 00000000300 12523721016 020146 0 ustar dalcinl dalcinl 0000000 0000000 execfile('ex-3.02.py')
count = 3
blklen = 1
stride = -2
newtype = dtype.Create_vector(count, blklen, stride)
assert newtype.size == dtype.size * count * blklen
dtype.Free()
newtype.Free()
mpi4py-3.0.3/demo/mpi-ref-v1/ex-3.12.py 0000644 0001750 0001750 00000000200 12523721016 020142 0 ustar dalcinl dalcinl 0000000 0000000 execfile('ex-3.02.py')
B = (3, 1)
D = (4 * dtype.extent, 0)
newtype = dtype.Create_hindexed(B, D)
dtype.Free()
newtype.Free()
mpi4py-3.0.3/demo/mpi-ref-v1/ex-2.34.py 0000644 0001750 0001750 00000002246 12523721016 020161 0 ustar dalcinl dalcinl 0000000 0000000 ## mpiexec -n 2 python ex-2.34.py
# Use of ready-mode and synchonous-mode
# --------------------------------------------------------------------
from mpi4py import MPI
try:
import numpy
except ImportError:
raise SystemExit
if MPI.COMM_WORLD.Get_size() < 2:
raise SystemExit
# --------------------------------------------------------------------
comm = MPI.COMM_WORLD
buff = numpy.empty((1000,2), dtype='f', order='fortran')
rank = comm.Get_rank()
if rank == 0:
req1 = comm.Irecv([buff[:, 0], MPI.FLOAT], 1, 1)
req2 = comm.Irecv([buff[:, 1], MPI.FLOAT], 1, 2)
status = [MPI.Status(), MPI.Status()]
MPI.Request.Waitall([req1, req2], status)
elif rank == 1:
buff[:, 0] = 5
buff[:, 1] = 7
comm.Ssend([buff[:, 1], MPI.FLOAT], 0, 2)
comm.Rsend([buff[:, 0], MPI.FLOAT], 0, 1)
# --------------------------------------------------------------------
all = numpy.all
if rank == 0:
assert all(buff[:, 0] == 5)
assert all(buff[:, 1] == 7)
assert status[0].source == 1
assert status[0].tag == 1
assert status[1].source == 1
assert status[1].tag == 2
# --------------------------------------------------------------------
mpi4py-3.0.3/demo/mpi-ref-v1/ex-3.01.py 0000644 0001750 0001750 00000001366 12523721016 020156 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
try:
import numpy
except ImportError:
raise SystemExit
# send a upper triangular matrix
N = 10
a = numpy.empty((N, N), dtype=float, order='c')
b = numpy.zeros((N, N), dtype=float, order='c')
a.flat = numpy.arange(a.size, dtype=float)
# compute start and size of each row
i = numpy.arange(N)
blocklen = N - i
disp = N * i + i
# create datatype for upper triangular part
upper = MPI.DOUBLE.Create_indexed(blocklen, disp)
upper.Commit()
# send and recv matrix
myrank = MPI.COMM_WORLD.Get_rank()
MPI.COMM_WORLD.Sendrecv((a, 1, upper), myrank, 0,
[b, 1, upper], myrank, 0)
assert numpy.allclose(numpy.triu(b), numpy.triu(a))
assert numpy.allclose(numpy.tril(b, -1), numpy.zeros((N,N)))
upper.Free()
mpi4py-3.0.3/demo/mpi-ref-v1/ex-3.02.py 0000644 0001750 0001750 00000000355 12523721016 020154 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
# Type = { (double, 0), (char, 8) }
blens = (1, 1)
disps = (0, MPI.DOUBLE.size)
types = (MPI.DOUBLE, MPI.CHAR)
dtype = MPI.Datatype.Create_struct(blens, disps, types)
if 'ex-3.02' in __file__:
dtype.Free()
mpi4py-3.0.3/demo/mpi-ref-v1/ex-3.11.py 0000644 0001750 0001750 00000000160 12523721016 020146 0 ustar dalcinl dalcinl 0000000 0000000 execfile('ex-3.02.py')
B = (3, 1)
D = (4, 0)
newtype = dtype.Create_indexed(B, D)
dtype.Free()
newtype.Free()
mpi4py-3.0.3/demo/cython/ 0000775 0001750 0001750 00000000000 13560002767 016235 5 ustar dalcinl dalcinl 0000000 0000000 mpi4py-3.0.3/demo/cython/makefile 0000644 0001750 0001750 00000001114 13200562156 017722 0 ustar dalcinl dalcinl 0000000 0000000 .PHONY: default
default: build test clean
PYTHON = python
PYTHON_CONFIG = ${PYTHON} ../python-config
CYTHON = cython
.PHONY: src
src: helloworld.c
helloworld.c: helloworld.pyx
${CYTHON} $<
MPICC = mpicc
CFLAGS = -fPIC ${shell ${PYTHON_CONFIG} --includes}
LDFLAGS = -shared ${shell ${PYTHON_CONFIG} --libs}
SO = ${shell ${PYTHON_CONFIG} --extension-suffix}
.PHONY: build
build: helloworld${SO}
helloworld${SO}: helloworld.c
${MPICC} ${CFLAGS} -o $@ $< ${LDFLAGS}
.PHONY: test
test: build
${PYTHON} -c 'import helloworld'
.PHONY: clean
clean:
${RM} helloworld.c helloworld${SO}
mpi4py-3.0.3/demo/cython/mpi-compat.h 0000644 0001750 0001750 00000000440 12750576064 020456 0 ustar dalcinl dalcinl 0000000 0000000 /* Author: Lisandro Dalcin */
/* Contact: dalcinl@gmail.com */
#ifndef MPI_COMPAT_H
#define MPI_COMPAT_H
#include
#if (MPI_VERSION < 3) && !defined(PyMPI_HAVE_MPI_Message)
typedef void *PyMPI_MPI_Message;
#define MPI_Message PyMPI_MPI_Message
#endif
#endif/*MPI_COMPAT_H*/
mpi4py-3.0.3/demo/cython/helloworld.pyx 0000644 0001750 0001750 00000002546 13200562156 021151 0 ustar dalcinl dalcinl 0000000 0000000 cdef extern from "mpi-compat.h": pass
# ---------
# Python-level module import
# (file: mpi4py/MPI.so)
from mpi4py import MPI
# Python-level objects and code
size = MPI.COMM_WORLD.Get_size()
rank = MPI.COMM_WORLD.Get_rank()
pname = MPI.Get_processor_name()
hwmess = "Hello, World! I am process %d of %d on %s."
print (hwmess % (rank, size, pname))
# ---------
# Cython-level cimport
# this make available mpi4py's Python extension types
# (file: mpi4py/include/mpi4py/MPI.pxd)
from mpi4py cimport MPI
from mpi4py.MPI cimport Intracomm as IntracommType
# C-level cdef, typed, Python objects
cdef MPI.Comm WORLD = MPI.COMM_WORLD
cdef IntracommType SELF = MPI.COMM_SELF
# ---------
# Cython-level cimport with PXD file
# this make available the native MPI C API
# with namespace-protection (stuff accessed as mpi.XXX)
# (file: mpi4py/include/mpi4py/libmpi.pxd)
from mpi4py cimport libmpi as mpi
cdef mpi.MPI_Comm world1 = WORLD.ob_mpi
cdef int ierr1=0
cdef int size1 = 0
ierr1 = mpi.MPI_Comm_size(mpi.MPI_COMM_WORLD, &size1)
cdef int rank1 = 0
ierr1 = mpi.MPI_Comm_rank(mpi.MPI_COMM_WORLD, &rank1)
cdef int rlen1=0
cdef char pname1[mpi.MPI_MAX_PROCESSOR_NAME]
ierr1 = mpi.MPI_Get_processor_name(pname1, &rlen1)
pname1[rlen1] = 0 # just in case ;-)
hwmess = "Hello, World! I am process %d of %d on %s."
print (hwmess % (rank1, size1, pname1))
# ---------
mpi4py-3.0.3/demo/mandelbrot/ 0000775 0001750 0001750 00000000000 13560002767 017060 5 ustar dalcinl dalcinl 0000000 0000000 mpi4py-3.0.3/demo/mandelbrot/mandelbrot-seq.py 0000644 0001750 0001750 00000002025 13200562156 022336 0 ustar dalcinl dalcinl 0000000 0000000 import numpy as np
import time
tic = time.time()
x1 = -2.0
x2 = 1.0
y1 = -1.0
y2 = 1.0
w = 150
h = 100
maxit = 127
def mandelbrot(x, y, maxit):
c = x + y*1j
z = 0 + 0j
it = 0
while abs(z) < 2 and it < maxit:
z = z**2 + c
it += 1
return it
dx = (x2 - x1) / w
dy = (y2 - y1) / h
C = np.empty([h, w], dtype='i')
for k in np.arange(h):
y = y1 + k * dy
for j in np.arange(w):
x = x1 + j * dx
C[k, j] = mandelbrot(x, y, maxit)
M = C
toc = time.time()
print('wall clock time: %8.2f seconds' % (toc-tic))
# eye candy (requires matplotlib)
if 1:
try:
from matplotlib import pyplot as plt
plt.imshow(M, aspect='equal')
try:
plt.nipy_spectral()
except AttributeError:
plt.spectral()
try:
import signal
def action(*args): raise SystemExit
signal.signal(signal.SIGALRM, action)
signal.alarm(2)
except:
pass
plt.show()
except:
pass
mpi4py-3.0.3/demo/mandelbrot/mandelbrot-master.py 0000644 0001750 0001750 00000003110 13200562156 023035 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import numpy as np
x1 = -2.0
x2 = 1.0
y1 = -1.0
y2 = 1.0
w = 600
h = 400
maxit = 255
import os
dirname = os.path.abspath(os.path.dirname(__file__))
executable = os.path.join(dirname, 'mandelbrot-worker.exe')
# spawn worker
worker = MPI.COMM_SELF.Spawn(executable, maxprocs=7)
size = worker.Get_remote_size()
# send parameters
rmsg = np.array([x1, x2, y1, y2], dtype='f')
imsg = np.array([w, h, maxit], dtype='i')
worker.Bcast([rmsg, MPI.REAL], root=MPI.ROOT)
worker.Bcast([imsg, MPI.INTEGER], root=MPI.ROOT)
# gather results
counts = np.empty(size, dtype='i')
indices = np.empty(h, dtype='i')
cdata = np.empty([h, w], dtype='i')
worker.Gather(sendbuf=None,
recvbuf=[counts, MPI.INTEGER],
root=MPI.ROOT)
worker.Gatherv(sendbuf=None,
recvbuf=[indices, (counts, None), MPI.INTEGER],
root=MPI.ROOT)
worker.Gatherv(sendbuf=None,
recvbuf=[cdata, (counts * w, None), MPI.INTEGER],
root=MPI.ROOT)
# disconnect worker
worker.Disconnect()
# reconstruct full result
M = np.zeros([h, w], dtype='i')
M[indices, :] = cdata
# eye candy (requires matplotlib)
if 1:
try:
from matplotlib import pyplot as plt
plt.imshow(M, aspect='equal')
try:
plt.nipy_spectral()
except AttributeError:
plt.spectral()
try:
import signal
def action(*args): raise SystemExit
signal.signal(signal.SIGALRM, action)
signal.alarm(2)
except:
pass
plt.show()
except:
pass
mpi4py-3.0.3/demo/mandelbrot/makefile 0000644 0001750 0001750 00000000753 13200562156 020555 0 ustar dalcinl dalcinl 0000000 0000000 .PHONY: default build test clean
default: build test clean
build: mandelbrot-worker.exe
MPIF90=mpif90
FFLAGS= -O3
ifneq (${MPI_FORTRAN_MOD_DIR},)
FFLAGS += -I${MPI_FORTRAN_MOD_DIR}
endif
mandelbrot-worker.exe: mandelbrot-worker.f90
${MPIF90} ${FFLAGS} -o $@ $<
PYTHON=python
MPIEXEC=mpiexec
NP_FLAG=-n
test: build
${MPIEXEC} ${NP_FLAG} 1 ${PYTHON} mandelbrot-master.py
${MPIEXEC} ${NP_FLAG} 7 ${PYTHON} mandelbrot.py
${PYTHON} mandelbrot-seq.py
clean:
${RM} mandelbrot-worker.exe
mpi4py-3.0.3/demo/mandelbrot/mandelbrot.py 0000644 0001750 0001750 00000005131 13200562156 021551 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import numpy as np
tic = MPI.Wtime()
x1 = -2.0
x2 = 1.0
y1 = -1.0
y2 = 1.0
w = 150
h = 100
maxit = 127
def mandelbrot(x, y, maxit):
c = x + y*1j
z = 0 + 0j
it = 0
while abs(z) < 2 and it < maxit:
z = z**2 + c
it += 1
return it
comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()
rmsg = np.empty(4, dtype='f')
imsg = np.empty(3, dtype='i')
if rank == 0:
rmsg[:] = [x1, x2, y1, y2]
imsg[:] = [w, h, maxit]
comm.Bcast([rmsg, MPI.FLOAT], root=0)
comm.Bcast([imsg, MPI.INT], root=0)
x1, x2, y1, y2 = [float(r) for r in rmsg]
w, h, maxit = [int(i) for i in imsg]
dx = (x2 - x1) / w
dy = (y2 - y1) / h
# number of lines to compute here
N = h // size + (h % size > rank)
N = np.array(N, dtype='i')
# indices of lines to compute here
I = np.arange(rank, h, size, dtype='i')
# compute local lines
C = np.empty([N, w], dtype='i')
for k in np.arange(N):
y = y1 + I[k] * dy
for j in np.arange(w):
x = x1 + j * dx
C[k, j] = mandelbrot(x, y, maxit)
# gather results at root
counts = 0
indices = None
cdata = None
if rank == 0:
counts = np.empty(size, dtype='i')
indices = np.empty(h, dtype='i')
cdata = np.empty([h, w], dtype='i')
comm.Gather(sendbuf=[N, MPI.INT],
recvbuf=[counts, MPI.INT],
root=0)
comm.Gatherv(sendbuf=[I, MPI.INT],
recvbuf=[indices, (counts, None), MPI.INT],
root=0)
comm.Gatherv(sendbuf=[C, MPI.INT],
recvbuf=[cdata, (counts*w, None), MPI.INT],
root=0)
# reconstruct full result at root
if rank == 0:
M = np.zeros([h,w], dtype='i')
M[indices, :] = cdata
toc = MPI.Wtime()
wct = comm.gather(toc-tic, root=0)
if rank == 0:
for task, time in enumerate(wct):
print('wall clock time: %8.2f seconds (task %d)' % (time, task))
def mean(seq): return sum(seq)/len(seq)
print ('all tasks, mean: %8.2f seconds' % mean(wct))
print ('all tasks, min: %8.2f seconds' % min(wct))
print ('all tasks, max: %8.2f seconds' % max(wct))
print ('all tasks, sum: %8.2f seconds' % sum(wct))
# eye candy (requires matplotlib)
if rank == 0:
try:
from matplotlib import pyplot as plt
plt.imshow(M, aspect='equal')
try:
plt.nipy_spectral()
except AttributeError:
plt.spectral()
try:
import signal
def action(*args): raise SystemExit
signal.signal(signal.SIGALRM, action)
signal.alarm(2)
except:
pass
plt.show()
except:
pass
MPI.COMM_WORLD.Barrier()
mpi4py-3.0.3/demo/mandelbrot/mandelbrot-worker.f90 0000644 0001750 0001750 00000004473 12523721016 023035 0 ustar dalcinl dalcinl 0000000 0000000 ! $ mpif90 -o mandelbrot.exe mandelbrot.f90
program main
use MPI
implicit none
integer master, nprocs, myrank, ierr
real :: rmsg(4), x1, x2, y1, y2
integer :: imsg(3), w, h, maxit
integer :: N
integer, allocatable :: I(:)
integer, allocatable :: C(:,:)
integer :: j, k
real :: x, dx, y, dy
call MPI_Init(ierr)
call MPI_Comm_get_parent(master, ierr)
if (master == MPI_COMM_NULL) then
print *, "parent communicator is MPI_COMM_NULL"
call MPI_Abort(MPI_COMM_WORLD, 1, ierr)
end if
call MPI_Comm_size(master, nprocs, ierr)
call MPI_Comm_rank(master, myrank, ierr)
! receive parameters and unpack
call MPI_Bcast(rmsg, 4, MPI_REAL, 0, master, ierr)
call MPI_Bcast(imsg, 3, MPI_INTEGER, 0, master, ierr)
x1 = rmsg(1); x2 = rmsg(2)
y1 = rmsg(3); y2 = rmsg(4)
w = imsg(1); h = imsg(2); maxit = imsg(3)
dx = (x2-x1)/real(w)
dy = (y2-y1)/real(h)
! number of lines to compute here
N = h / nprocs
if (modulo(h, nprocs) > myrank) then
N = N + 1
end if
! indices of lines to compute here
allocate( I(0:N-1) )
I = (/ (k, k=myrank, h-1, nprocs) /)
! compute local lines
allocate( C(0:w-1, 0:N-1) )
do k = 0, N-1
y = y1 + real(I(k)) * dy
do j = 0, w-1
x = x1 + real(j) * dx
C(j, k) = mandelbrot(x, y, maxit)
end do
end do
! send number of lines computed here
call MPI_Gather(N, 1, MPI_INTEGER, &
MPI_BOTTOM, 0, MPI_BYTE, &
0, master, ierr)
! send indices of lines computed here
call MPI_Gatherv(I, N, MPI_INTEGER, &
MPI_BOTTOM, MPI_BOTTOM, MPI_BOTTOM, MPI_BYTE, &
0, master, ierr)
! send data of lines computed here
call MPI_Gatherv(C, N*w, MPI_INTEGER, &
MPI_BOTTOM, MPI_BOTTOM, MPI_BOTTOM, MPI_BYTE, &
0, master, ierr)
deallocate(C)
deallocate(I)
! we are done
call MPI_Comm_disconnect(master, ierr)
call MPI_Finalize(ierr)
contains
function mandelbrot(x, y, maxit) result (it)
implicit none
real, intent(in) :: x, y
integer, intent(in) :: maxit
integer :: it
complex :: z, c
z = cmplx(0, 0)
c = cmplx(x, y)
it = 0
do while (abs(z) < 2.0 .and. it < maxit)
z = z*z + c
it = it + 1
end do
end function mandelbrot
end program main
mpi4py-3.0.3/demo/threads/ 0000775 0001750 0001750 00000000000 13560002767 016363 5 ustar dalcinl dalcinl 0000000 0000000 mpi4py-3.0.3/demo/threads/makefile 0000644 0001750 0001750 00000000170 12523721016 020050 0 ustar dalcinl dalcinl 0000000 0000000 .PHONY: default build test clean
default: build test clean
PYTHON=python
build:
test:
${PYTHON} sendrecv.py
clean: mpi4py-3.0.3/demo/threads/sendrecv.py 0000644 0001750 0001750 00000002137 12523721016 020540 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import sys
if MPI.Query_thread() < MPI.THREAD_MULTIPLE:
sys.stderr.write("MPI does not provide enough thread support\n")
sys.exit(0)
try:
import threading
except ImportError:
sys.stderr.write("threading module not available\n")
sys.exit(0)
try:
import numpy
except ImportError:
sys.stderr.write("NumPy package not available\n")
sys.exit(0)
send_msg = numpy.arange(1000000, dtype='i')
recv_msg = numpy.zeros_like(send_msg)
start_event = threading.Event()
def self_send():
start_event.wait()
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
comm.Send([send_msg, MPI.INT], dest=rank, tag=0)
def self_recv():
start_event.wait()
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
comm.Recv([recv_msg, MPI.INT], source=rank, tag=0)
send_thread = threading.Thread(target=self_send)
recv_thread = threading.Thread(target=self_recv)
for t in (recv_thread, send_thread):
t.start()
assert not numpy.allclose(send_msg, recv_msg)
start_event.set()
for t in (recv_thread, send_thread):
t.join()
assert numpy.allclose(send_msg, recv_msg)
mpi4py-3.0.3/demo/wrap-swig/ 0000775 0001750 0001750 00000000000 13560002767 016651 5 ustar dalcinl dalcinl 0000000 0000000 mpi4py-3.0.3/demo/wrap-swig/makefile 0000644 0001750 0001750 00000001476 12750576064 020365 0 ustar dalcinl dalcinl 0000000 0000000 .PHONY: default
default: build test clean
PYTHON = python
PYTHON_CONFIG = ${PYTHON} ../python-config
MPI4PY_INCLUDE = ${shell ${PYTHON} -c 'import mpi4py; print( mpi4py.get_include() )'}
SWIG = swig
SWIG_PY = ${SWIG} -python
.PHONY: src
src: helloworld_wrap.c
helloworld_wrap.c: helloworld.i
${SWIG_PY} -I${MPI4PY_INCLUDE} -o $@ $<
MPICC = mpicc
CFLAGS = -fPIC ${shell ${PYTHON_CONFIG} --includes}
LDFLAGS = -shared ${shell ${PYTHON_CONFIG} --libs}
SO = ${shell ${PYTHON_CONFIG} --extension-suffix}
.PHONY: build
build: _helloworld${SO}
_helloworld${SO}: helloworld_wrap.c
${MPICC} ${CFLAGS} -I${MPI4PY_INCLUDE} -o $@ $< ${LDFLAGS}
MPIEXEC = mpiexec
NP_FLAG = -n
NP = 5
.PHONY: test
test: build
${MPIEXEC} ${NP_FLAG} ${NP} ${PYTHON} test.py
.PHONY: clean
clean:
${RM} helloworld_wrap.c helloworld.py* _helloworld${SO}
mpi4py-3.0.3/demo/wrap-swig/test.py 0000644 0001750 0001750 00000000332 12523721016 020167 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
import helloworld as hw
null = MPI.COMM_NULL
hw.sayhello(null)
comm = MPI.COMM_WORLD
hw.sayhello(comm)
try:
hw.sayhello(list())
except:
pass
else:
assert 0, "exception not raised"
mpi4py-3.0.3/demo/wrap-swig/helloworld.i 0000644 0001750 0001750 00000001216 12523721016 021165 0 ustar dalcinl dalcinl 0000000 0000000 %module helloworld
%{
#define MPICH_SKIP_MPICXX 1
#define OMPI_SKIP_MPICXX 1
#include
#include
void sayhello(MPI_Comm comm) {
int size, rank;
char pname[MPI_MAX_PROCESSOR_NAME]; int len;
if (comm == MPI_COMM_NULL) {
printf("You passed MPI_COMM_NULL !!!\n");
return;
}
MPI_Comm_size(comm, &size);
MPI_Comm_rank(comm, &rank);
MPI_Get_processor_name(pname, &len);
pname[len] = 0;
printf("Hello, World! I am process %d of %d on %s.\n",
rank, size, pname);
}
%}
%include mpi4py/mpi4py.i
%mpi4py_typemap(Comm, MPI_Comm);
void sayhello(MPI_Comm comm);
/*
* Local Variables:
* mode: C
* End:
*/
mpi4py-3.0.3/demo/nxtval/ 0000775 0001750 0001750 00000000000 13560002767 016245 5 ustar dalcinl dalcinl 0000000 0000000 mpi4py-3.0.3/demo/nxtval/nxtval-threads.py 0000644 0001750 0001750 00000004312 12523721016 021552 0 ustar dalcinl dalcinl 0000000 0000000 # -----------------------------------------------------------------------------
from mpi4py import MPI
from array import array
from threading import Thread
class Counter(object):
def __init__(self, comm):
# duplicate communicator
assert not comm.Is_inter()
self.comm = comm.Dup()
# start counter thread
self.thread = None
rank = self.comm.Get_rank()
if rank == 0:
self.thread = Thread(target=self._counter_thread)
self.thread.start()
def _counter_thread(self):
incr = array('i', [0])
ival = array('i', [0])
status = MPI.Status()
while True: # server loop
self.comm.Recv([incr, MPI.INT],
MPI.ANY_SOURCE, MPI.ANY_TAG,
status)
if status.Get_tag() == 1:
return
self.comm.Send([ival, MPI.INT],
status.Get_source(), 0)
ival[0] += incr[0]
def free(self):
self.comm.Barrier()
# stop counter thread
rank = self.comm.Get_rank()
if rank == 0:
self.comm.Send([None, MPI.INT], 0, 1)
self.thread.join()
#
self.comm.Free()
def next(self):
incr = array('i', [1])
ival = array('i', [0])
self.comm.Send([incr, MPI.INT], 0, 0)
self.comm.Recv([ival, MPI.INT], 0, 0)
nxtval = ival[0]
return nxtval
# -----------------------------------------------------------------------------
def test_thread_level():
import sys
flag = (MPI.Query_thread() == MPI.THREAD_MULTIPLE)
flag = MPI.COMM_WORLD.bcast(flag, root=0)
if not flag:
if MPI.COMM_WORLD.Get_rank() == 0:
sys.stderr.write("MPI does not provide enough thread support\n")
sys.exit(0)
def test():
vals = []
counter = Counter(MPI.COMM_WORLD)
for i in range(5):
c = counter.next()
vals.append(c)
counter.free()
vals = MPI.COMM_WORLD.allreduce(vals)
assert sorted(vals) == list(range(len(vals)))
if __name__ == '__main__':
test_thread_level()
test()
# -----------------------------------------------------------------------------
mpi4py-3.0.3/demo/nxtval/runtests.sh 0000755 0001750 0001750 00000000404 12523721016 020460 0 ustar dalcinl dalcinl 0000000 0000000 #!/bin/sh
MPIEXEC=mpiexec
NP_FLAG=-n
NP=5
PYTHON=python
set -x
$MPIEXEC $NP_FLAG $NP $PYTHON nxtval-threads.py
$MPIEXEC $NP_FLAG $NP $PYTHON nxtval-dynproc.py
$MPIEXEC $NP_FLAG $NP $PYTHON nxtval-onesided.py
$MPIEXEC $NP_FLAG $NP $PYTHON nxtval-scalable.py
mpi4py-3.0.3/demo/nxtval/runtests.bat 0000644 0001750 0001750 00000000731 13200562156 020615 0 ustar dalcinl dalcinl 0000000 0000000 @echo off
setlocal ENABLEEXTENSIONS
set MPI=Microsoft MPI
set PATH="%ProgramFiles%\%MPI%\bin";%PATH%
set MPIEXEC=mpiexec
set NP_FLAG=-n
set NP=5
set PYTHON=C:\Python27\python.exe
set PYTHON=C:\Python36\python.exe
set PYTHON=python
@echo on
%MPIEXEC% %NP_FLAG% %NP% %PYTHON% nxtval-threads.py
%MPIEXEC% %NP_FLAG% %NP% %PYTHON% nxtval-dynproc.py
%MPIEXEC% %NP_FLAG% %NP% %PYTHON% nxtval-onesided.py
%MPIEXEC% %NP_FLAG% %NP% %PYTHON% nxtval-scalable.py
mpi4py-3.0.3/demo/nxtval/nxtval-mpi3.py 0000644 0001750 0001750 00000004105 13200562156 020771 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
from array import array as _array
import struct as _struct
# --------------------------------------------------------------------
class Counter(object):
def __init__(self, comm):
rank = comm.Get_rank()
itemsize = MPI.INT.Get_size()
if rank == 0:
n = 1
else:
n = 0
self.win = MPI.Win.Allocate(n*itemsize, itemsize,
MPI.INFO_NULL, comm)
if rank == 0:
mem = self.win.tomemory()
mem[:] = _struct.pack('i', 0)
def free(self):
self.win.Free()
def next(self, increment=1):
incr = _array('i', [increment])
nval = _array('i', [0])
self.win.Lock(0)
self.win.Get_accumulate([incr, 1, MPI.INT],
[nval, 1, MPI.INT],
0, op=MPI.SUM)
self.win.Unlock(0)
return nval[0]
# -----------------------------------------------------------------------------
class Mutex(object):
def __init__(self, comm):
self.counter = Counter(comm)
def __enter__(self):
self.lock()
return self
def __exit__(self, *exc):
self.unlock()
return None
def free(self):
self.counter.free()
def lock(self):
value = self.counter.next(+1)
while value != 0:
value = self.counter.next(-1)
value = self.counter.next(+1)
def unlock(self):
self.counter.next(-1)
# -----------------------------------------------------------------------------
def test_counter():
vals = []
counter = Counter(MPI.COMM_WORLD)
for i in range(5):
c = counter.next()
vals.append(c)
counter.free()
vals = MPI.COMM_WORLD.allreduce(vals)
assert sorted(vals) == list(range(len(vals)))
def test_mutex():
mutex = Mutex(MPI.COMM_WORLD)
mutex.lock()
mutex.unlock()
mutex.free()
if __name__ == '__main__':
test_counter()
test_mutex()
# -----------------------------------------------------------------------------
mpi4py-3.0.3/demo/nxtval/makefile 0000644 0001750 0001750 00000000540 12750576064 017750 0 ustar dalcinl dalcinl 0000000 0000000 MPIEXEC=mpiexec
NP_FLAG=-n
NP=5
PYTHON=python
.PHONY: test
test:
${MPIEXEC} ${NP_FLAG} ${NP} ${PYTHON} nxtval-threads.py
${MPIEXEC} ${NP_FLAG} ${NP} ${PYTHON} nxtval-dynproc.py
${MPIEXEC} ${NP_FLAG} ${NP} ${PYTHON} nxtval-onesided.py
${MPIEXEC} ${NP_FLAG} ${NP} ${PYTHON} nxtval-scalable.py
# ${MPIEXEC} ${NP_FLAG} ${NP} ${PYTHON} nxtval-mpi3.py
mpi4py-3.0.3/demo/nxtval/nxtval-scalable.py 0000644 0001750 0001750 00000010027 12750576064 021703 0 ustar dalcinl dalcinl 0000000 0000000 from mpi4py import MPI
# -----------------------------------------------------------------------------
import struct as _struct
try:
from numpy import empty as _empty
def _array_new(size, typecode, init=0):
a = _empty(size, typecode)
a.fill(init)
return a
def _array_set(ary, value):
ary.fill(value)
def _array_sum(ary):
return ary.sum()
except ImportError:
from array import array as _array
def _array_new(size, typecode, init=0):
return _array(typecode, [init]) * size
def _array_set(ary, value):
for i, _ in enumerate(ary):
ary[i] = value
def _array_sum(ary):
return sum(ary, 0)
# -----------------------------------------------------------------------------
class Counter(object):
def __init__(self, comm, init=0):
#
size = comm.Get_size()
rank = comm.Get_rank()
mask = 1
while mask < size:
mask <<= 1
mask >>= 1
idx = 0
get_idx = []
acc_idx = []
while mask >= 1:
left = idx + 1
right = idx + (mask<<1)
if rank < mask:
acc_idx.append( left )
get_idx.append( right )
idx = left
else:
acc_idx.append( right )
get_idx.append( left )
idx = right
rank = rank % mask
mask >>= 1
#
typecode = 'i'
datatype = MPI.INT
itemsize = datatype.Get_size()
#
root = 0
rank = comm.Get_rank()
if rank == root:
nlevels = len(get_idx) + 1
nentries = (1<