[127] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | # Examples of setup |
---|
| 4 | # |
---|
| 5 | # python setup.py install |
---|
| 6 | # python setup.py install --prefix=/opt/python-2.3 |
---|
| 7 | # python setup.py install --home=~ |
---|
| 8 | # |
---|
| 9 | # Some times you'll have to add a file called pypar.pth |
---|
| 10 | # containing the word pypar to site-packages |
---|
| 11 | |
---|
| 12 | |
---|
| 13 | |
---|
| 14 | from distutils.core import setup, Extension |
---|
| 15 | import distutils.sysconfig |
---|
[3443] | 16 | import distutils.debug |
---|
[127] | 17 | import os, sys |
---|
| 18 | import popen2 |
---|
| 19 | import string |
---|
| 20 | import tempfile |
---|
| 21 | |
---|
| 22 | def setup_compiler(): |
---|
| 23 | distutils.sysconfig.get_config_vars() |
---|
| 24 | config_vars = distutils.sysconfig._config_vars |
---|
[3443] | 25 | |
---|
[127] | 26 | if sys.platform == 'sunos5': |
---|
| 27 | config_vars['LDSHARED'] = "gcc -G" |
---|
| 28 | config_vars['CCSHARED'] = "" |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | def uniq_arr(arr): |
---|
| 32 | """Remove repeated values from an array and return new array.""" |
---|
| 33 | ret = [] |
---|
| 34 | for i in arr: |
---|
| 35 | if i not in ret: |
---|
| 36 | ret.append(i) |
---|
| 37 | return ret |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | def _run_command(cmd): |
---|
| 41 | out_file, in_file, err_file = popen2.popen3(cmd) |
---|
| 42 | output = out_file.read() + err_file.read() |
---|
| 43 | out_file.close() |
---|
| 44 | in_file.close() |
---|
| 45 | err_file.close() |
---|
| 46 | # need this hack to get the exit status |
---|
| 47 | out_file = os.popen(cmd) |
---|
| 48 | if out_file.close(): |
---|
| 49 | # close returns exit status of command. |
---|
| 50 | return "" |
---|
| 51 | else: |
---|
| 52 | # no errors, out_file.close() returns None. |
---|
| 53 | return output |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | def _get_mpi_cmd(): |
---|
| 57 | """Returns the output of the command used to compile using |
---|
| 58 | mpicc.""" |
---|
| 59 | # LAM |
---|
| 60 | output = _run_command("mpicc -showme") |
---|
| 61 | if output: |
---|
| 62 | return output |
---|
| 63 | |
---|
| 64 | # MPICH |
---|
| 65 | # works with MPICH version 1.2.1 (on Debian) |
---|
| 66 | output = _run_command("mpicc -compile_info -link_info") |
---|
| 67 | if output: |
---|
| 68 | return output |
---|
| 69 | |
---|
| 70 | # old version of MPICH needs this hack. |
---|
| 71 | tmp_base = tempfile.mktemp() |
---|
| 72 | tmp_c = tmp_base + ".c" |
---|
| 73 | tmp_o = tmp_base + ".o" |
---|
| 74 | tmp_file = open(tmp_c, "w") |
---|
| 75 | tmp_file.write('#include "mpi.h"\nint main(){return 0;}\n') |
---|
| 76 | tmp_file.close() |
---|
| 77 | output = _run_command("mpicc -show;"\ |
---|
| 78 | "mpicc -echo -c %s -o %s"%(tmp_c, tmp_o)) |
---|
| 79 | os.remove(tmp_c) |
---|
| 80 | if os.path.exists(tmp_o): |
---|
| 81 | os.remove(tmp_o) |
---|
| 82 | if output: |
---|
| 83 | return output |
---|
| 84 | else: |
---|
| 85 | return "" |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | def get_mpi_flags(): |
---|
| 89 | output = _get_mpi_cmd() |
---|
[2778] | 90 | print output |
---|
[127] | 91 | if not output: |
---|
| 92 | if sys.platform=='win32': #From Simon Frost |
---|
| 93 | output = "gcc -L$MPICH_DIR\SDK.gcc\lib -lmpich -I$MPICH_DIR\SDK.gcc\include" |
---|
| 94 | else: |
---|
| 95 | output = "cc -L/usr/opt/mpi -lmpi -lelan" |
---|
| 96 | |
---|
| 97 | |
---|
| 98 | # now get the include, library dirs and the libs to link with. |
---|
| 99 | flags = string.split(output) |
---|
| 100 | flags = uniq_arr(flags) # remove repeated values. |
---|
| 101 | inc_dirs = [] |
---|
| 102 | lib_dirs = [] |
---|
| 103 | libs = [] |
---|
| 104 | def_macros = [] |
---|
| 105 | undef_macros = [] |
---|
| 106 | for f in flags: |
---|
| 107 | if f[:2] == '-I': |
---|
| 108 | inc_dirs.append(f[2:]) |
---|
| 109 | elif f[:2] == '-L': |
---|
| 110 | lib_dirs.append(f[2:]) |
---|
| 111 | elif f[:2] == '-l': |
---|
| 112 | libs.append(f[2:]) |
---|
| 113 | elif f[:2] == '-U': |
---|
| 114 | undef_macros.append(f[2:]) |
---|
| 115 | elif f[:2] == '-D': |
---|
| 116 | tmp = string.split(f[2:], '=') |
---|
| 117 | if len(tmp) == 1: |
---|
| 118 | def_macros.append((tmp[0], None)) |
---|
| 119 | else: |
---|
| 120 | def_macros.append(tuple(tmp)) |
---|
| 121 | return {'inc_dirs': inc_dirs, 'lib_dirs': lib_dirs, 'libs':libs, |
---|
| 122 | 'def_macros': def_macros, 'undef_macros': undef_macros} |
---|
| 123 | |
---|
| 124 | |
---|
| 125 | if __name__ == "__main__": |
---|
| 126 | setup_compiler() |
---|
| 127 | |
---|
| 128 | mpi_flags = get_mpi_flags() |
---|
| 129 | |
---|
[3443] | 130 | |
---|
| 131 | # FIXME: It would be good to set specific compiler flags, e.g. |
---|
| 132 | # for our AMD opteron cluster which is using the portland group |
---|
| 133 | # compiler pgcc, but I can't get this to work let alone get distutils |
---|
| 134 | # to give some diagnostics on what flags it is actually using. |
---|
| 135 | if os.name == 'posix' and os.uname()[4] == 'x86_64': |
---|
| 136 | #Extra flags for 64 bit architectures |
---|
| 137 | #extra_compile_args = ' -fPIC -m64' #Valid for gcc |
---|
| 138 | extra_compile_args = ' -fPIC -tp amd64' #Valid for pgcc |
---|
| 139 | else: |
---|
| 140 | extra_compile_args = None |
---|
| 141 | |
---|
| 142 | |
---|
| 143 | |
---|
| 144 | |
---|
[127] | 145 | setup(name="Pypar", |
---|
[3443] | 146 | version="1.9.2", |
---|
[127] | 147 | description="Pypar - Parallel Python", |
---|
| 148 | long_description="Pypar - Parallel Python, no-frills MPI interface", |
---|
| 149 | author="Ole Nielsen", |
---|
| 150 | author_email="Ole.Nielsen@anu.edu.au", |
---|
| 151 | url="http://datamining.anu.edu.au/pypar", |
---|
| 152 | package_dir = {'': 'lib'}, |
---|
| 153 | packages = ['pypar'], |
---|
| 154 | ext_modules = [Extension('pypar.mpiext', |
---|
| 155 | ['mpiext.c'], |
---|
| 156 | include_dirs=mpi_flags['inc_dirs'], |
---|
| 157 | library_dirs=mpi_flags['lib_dirs'], |
---|
| 158 | libraries=mpi_flags['libs'], |
---|
| 159 | define_macros=mpi_flags['def_macros'], |
---|
[3443] | 160 | undef_macros=mpi_flags['undef_macros'], |
---|
| 161 | extra_compile_args=extra_compile_args)] |
---|
[127] | 162 | ) |
---|