Skip to content

Instantly share code, notes, and snippets.

@bpartridge
Created January 31, 2014 00:21
Show Gist options
  • Save bpartridge/8723020 to your computer and use it in GitHub Desktop.
Save bpartridge/8723020 to your computer and use it in GitHub Desktop.
Python and Numpy for Xeon Phi
PY_VER = 2.7.3
SRC = $(shell readlink -f python)
MIC_PY_HOME = $(SRC)/_install
MIC_PY_PATH = $(MIC_PY_HOME)/lib/python2.7
CTYPES = $(SRC)/Modules/_ctypes
all: $(MIC_PY_PATH)/_ctypes
install:
@echo "To install, copy $(MIC_PY_HOME) where it can be accessed from the MIC card."
$(error)
python.tgz:
wget http://www.python.org/ftp/python/$(PY_VER)/Python-$(PY_VER).tgz -O $@
$(SRC): python.tgz
mkdir -p $@
tar -xf $< -C $@ --strip-components=1
$(SRC)/hostpython: | $(SRC)
# TODO: fail if patched
cd $< && ./configure && make
cd $< && mv python hostpython && mv Parser/pgen Parser/hostpgen && make distclean
xcompile.patch:
wget http://randomsplat.com/wp-content/uploads/2012/10/Python-$(PY_VER)-xcompile.patch -O $@
$(SRC)/patched: xcompile.patch $(SRC)/hostpython
cd $(SRC) && patch -p1 < ../$<
touch $@
$(SRC)/Makefile: $(SRC)/patched $(CTYPES)/.git
cd $(SRC) && ./configure CC="icc -mmic" CXX="icpc -mmic" --host=x86_64 --without-gcc
$(CTYPES)/.git: | $(SRC)
mv $(CTYPES) $(CTYPES).orig
git clone https://github.com/bpartridge/xeon_phi_ctypes.git $(CTYPES)
$(MIC_PY_PATH)/_ctypes: $(SRC)/Makefile $(SRC)/patched $(CTYPES)/.git
sed -e "s/-OPT:Olimit=0//g" -i.backup $(SRC)/Makefile
cd $(SRC) && make HOSTPYTHON=./hostpython HOSTPGEN=./Parser/hostpgen \
CROSS_COMPILE=k1om- CROSS_COMPILE_TARGET=yes HOSTARCH=x86_64 BUILDARCH=x86_64-linux-gnu \
EXTRA_CFLAGS="-fp-model precise -shared -fPIC" LDFLAGS=""
mkdir -p $(MIC_PY_HOME)
cd $(SRC) && make install HOSTPYTHON=./hostpython CROSS_COMPILE_TARGET=yes prefix=$(MIC_PY_HOME)
micclean: miccleanobjs
rm $(SRC)/Makefile
miccleanobjs:
rm -rvf $(SRC)/**/*.o
rm -rf $(SRC)/build
rm -rf $(MIC_PY_HOME)
numpy.tgz:
wget http://downloads.sourceforge.net/project/numpy/NumPy/1.8.0/numpy-1.8.0.tar.gz -O $@
numpy: numpy.tgz
mkdir $@
tar -xf $< -C $@ --strip-components=1
numpyclean: numpy
rm -rf numpy/build
rm -rvf numpy/**/*.o
numpyxc: numpy | $(MIC_PY_HOME)
# build_clib forces config.h to be created; it needs to be modified before extensions are built
cd numpy && PYTHONXCPREFIX=$(MIC_PY_HOME) python numpyxc.py build_clib
sed '/INTRIN/ d' -i numpy/build/*/numpy/core/include/numpy/config.h
cd numpy && PYTHONXCPREFIX=$(MIC_PY_HOME) python numpyxc.py build
.PHONY: all micclean miccleanobjs numpyxc
import os
xcprefix = os.environ.get('PYTHONXCPREFIX', None)
if not xcprefix:
raise Exception("Must specify PYTHONXCPREFIX as an argument to make this work")
from setup import setup_package
from numpy.distutils import fcompiler, ccompiler
from numpy.distutils.intelccompiler import IntelCCompiler
class IntelMICCompiler(IntelCCompiler):
""" A modified Intel compiler compatible with an gcc built Python."""
compiler_type = 'intelmic'
cc_exe = 'icc'
def __init__ (self, verbose=0, dry_run=0, force=0):
IntelCCompiler.__init__ (self, verbose, dry_run, force)
compiler = "icc -mmic -fPIC -I{0}/include -L{0}/lib -L{0}/lib/python2.7".format(xcprefix)
self.set_executables(compiler=compiler + ' -shared',
compiler_so=compiler,
compiler_cxx=compiler,
linker_exe=compiler,
linker_so=compiler + ' -shared')
# Export it into a module that is __import__ed by np.distutils.ccompiler
import sys, imp
imc = imp.new_module('numpy.distutils.intelmiccompiler')
sys.modules['numpy.distutils.intelmiccompiler'] = imc
imc.IntelMICCompiler = IntelMICCompiler
# print ccompiler.compiler_class # {name: (module_name, module_member_name, desc), ...}
ccompiler.compiler_class['intelmic'] = ('intelmiccompiler', 'IntelMICCompiler', 'Intel Compiler for Xeon Phi')
fcompiler.load_all_fcompiler_classes()
# print fcompiler.fcompiler_class # {name: (name, klass, desc), ...}
# The following taken from https://bitbucket.org/lambacck/distutilscross
# import os
# from distutils import sysconfig
# xcprefix = os.environ.get('PYTHONXCPREFIX', None)
# if xcprefix:
# _get_python_lib = sysconfig.get_python_lib
# def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
# return _get_python_lib(plat_specific, standard_lib, xcprefix)
# sysconfig.get_python_lib = get_python_lib
# _get_python_inc = sysconfig.get_python_inc
# def get_python_inc(plat_specific=0, prefix=None):
# return _get_python_inc(plat_specific, xcprefix)
# sysconfig.get_python_inc = get_python_inc
# sysconfig.PREFIX = xcprefix
# sysconfig.EXEC_PREFIX = xcprefix
# # now force a reload from the given prefix
# sysconfig._config_vars = None
# print sysconfig.get_config_var("LDSHARED")
# Disable SSE intrinsics since they don't seem to work
# import numpy.core.setup as core_setup
# setup_common.OPTIONAL_HEADERS = []
# setup_common.OPTIONAL_INTRINSICS = [] # TODO: reintroduce other builtins
setup_package()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment