Software Archive
Read-only legacy content
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.

Got to work Python as a native application

Emilio_C_
Beginner
1,944 Views

Dear all,

I have managed to crosscompile python and run it on the mic, In our research group we use python + multiprocessing module to build quick & fast prototypes. Since the mic architecture has 60 x86 cores I though it would be nice to give it a try.

I want to share my thoughs about it because some people may find this interesting and useful.

This python cross-compilation is mostly functional, although the ctypes module does not work. This module uses the libffi, a library where some parts are written in asm and use MMX instructions, so for the moment it is not possible to compile it directly for the xeon phi. I am working on trying to get it up & running.

The main reference I used was this website: http://randomsplat.com/id5-cross-compiling-python-for-embedded-linux.html

I selected python 2.7.2.

First of all you need to obtain a host working binary of the interpreter and the pgen this are build and executed during the build process so it will give an error if python is cross-compiled directly.

this can be done with the usual "./configure && make", then: "mv python hostpython && mv Parser/pgen Parser/hostpgen && make distclean",

now you have to download the following patch Python-2.7.2-xcompile.patch (there are patches for other versions at randomsplat). and apply it "patch -p1 < Python-2.7.2-xcompile.patch".

Now we have to switch compilers and use the icc with -mmic option: I do recommend the following:

 ./configure CC="icc -mmic"  CXX="icpc -mmic" --host=x86_64 --without-gcc , put -mmic as part of the compiler name, I know it is dirty but when put at as a common flag it wont be used everywhere and the compilation will break.

Now open the makefile and from BASECFLAGS remove -OPT:Olimit=0, otherwise it will be giving annoying warnings for each file, 

make HOSTPYTHON=./hostpython HOSTPGEN=./Parser/hostpgen  CROSS_COMPILE=k1om- CROSS_COMPILE_TARGET=yes HOSTARCH=k1om BUILDARCH=x86_64-linux-gnu EXTRA_CFLAGS="-fp-model precise"

the -fp-model precise is needed in order to avoid problems with fp airthmetic that makes the cmath test to fail.

At the end it will tell us that ctypes module could not be built. As I said above, the x86 version of the libffi uses MMX instructions, so it should be ported to the intel mic. (If anyone knows how to make it work I will be very grateful).

now do "make install HOSTPYTHON=./hostpython  CROSS_COMPILE_TARGET=yes prefix=~/Python-2.7.2/_install" it will copy us all the needed files in the folder _install.

Before executing python on the mic, we need to copy all the required libraries from intel composer (which are nearly all), as root do:

scp /opt/intel/composer_xe_2013.3.163/compiler/lib/mic/*.so* mic0:/lib64/

now:

scp -r  _install/ mic0:

go to the mic and copy the _install/lib/python2.7 folder to /usr/lib/python2.7 (make the lib directory if it does not exists, you need to do it as root), also copy the _install/bin/ files to /usr/bin/

Once this is done, you only need to set the following variable "export PYTHONHOME=/usr/ " and you can start pythoning by typing "python"!

You have some tests available at cd _install/lib/python2.7/test/

[villar@phi-mic0 test]$ python test_cmath.py
test_abs (__main__.CMathTests) ... ok
test_cmath_matches_math (__main__.CMathTests) ... ok
test_constants (__main__.CMathTests) ... ok
test_input_type (__main__.CMathTests) ... ok
test_isinf (__main__.CMathTests) ... ok
test_isnan (__main__.CMathTests) ... ok
test_phase (__main__.CMathTests) ... ok
test_polar (__main__.CMathTests) ... ok
test_rect (__main__.CMathTests) ... ok
test_specific_values (__main__.CMathTests) ... ok
test_user_object (__main__.CMathTests) ... ok

----------------------------------------------------------------------
Ran 11 tests in 0.405s

I have run some tests succesfully but the test___all__.py seems to be borked when searching files.

This is all, I hope this is useful and interesting to you, and excuse me for my poor english!

0 Kudos
9 Replies
Loc_N_Intel
Employee
1,944 Views

Hello Emilio,

I verified your prodedure using python 2.7.2. It confirmed that it worked well with my current system configuration: MPSS 2.1.6720-15, Intel(R) 64 Compiler XE version 14.0.0.051.

I just want to add one comment: in the procedure, we also need to copy the directory "/root/_install/include/python2.7" to "/usr/include/python2.7/" as well. 

Thank you very much for sharing the procedure.

0 Kudos
Surya_Narayanan_N_
1,944 Views

Hello,

      I am following the same procedure but am stuck with the configure step I get the following error.

snataraj@phileas:~/Python-2.7.5$ ./configure CC="icc -mmic" CXX="icpc -mmic" --host=x86_64
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-pc-none
configure: error: Cross compiling required --host=HOST-TUPLE and --build=ARCH

can you pleae help me here?

0 Kudos
Surya_Narayanan_N_
1,944 Views

Hello,

      I am following the same procedure but am stuck with the configure step I get the following error.

snataraj@phileas:~/Python-2.7.5$ ./configure CC="icc -mmic" CXX="icpc -mmic" --host=x86_64
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-pc-none
configure: error: Cross compiling required --host=HOST-TUPLE and --build=ARCH

can you pleae help me here?

0 Kudos
Loc_N_Intel
Employee
1,944 Views

Hi Surya,

I followed the instruction including the option "--without-gcc" and it worked for me:

./configure CC="icc -mmic"  CXX="icpc -mmic" --host=x86_64 --without-gcc

0 Kudos
Emilio_C_
Beginner
1,944 Views

You are using Python-2.7.5

The patch is for Python-2.7.2

Btw, I ported the libffi to the xeon phi, and I have ctypes working properly, therefore numpy can be used too.
Right now I dont have access to the code, since I am away from my home institute, but I'll try to upload it once I get back home. 

0 Kudos
Korotkoff__Al
Beginner
1,944 Views

Hello Emilio!

This method is working fine, but i got some problems with numpy and another libs.

Can you tell please, how you managed to install Numpy on mic?

Thank you, and sry for my poor english

0 Kudos
Emilio_C_
Beginner
1,944 Views

Dear all,

Please find hereby attached a version of ctypes that works with the mic.

We changed the code in libffi/x86/unix64.S to make it use the phi ABI, it was a prototype so its very dirty. I don't have access to the phi anymore so I didnt care about it too much. To see the changes I made you can diff the files in the x86 folder with the libffi provided at Python 2.7.3 in Modules/_ctypes/libffi if anyone wants to do a clean port to the phi using the code we wrote is more than welcome.

This libffi should work straightforward and allow to compile Python with ctypes. Therefore allowing you to use numpy. 

Just replace the Python-2.7.3/Modules/_ctypes folder with the attached one.

Good luck! and just drop a line in case you have any problem,

Kind regards.

0 Kudos
BelindaLiviero
Employee
1,944 Views

Hi Emilio et al, thank you for sharing your experiences here!

One comment / suggestion:

rather than copy the files to the coprocessor (and take up even more ramdisk space) you might want to consider [NFS or other] mounting the install directory on the coprocessor directly.

0 Kudos
Brenton_P_
Beginner
1,944 Views

I'll write a longer update later, but in case it helps anyone else immediately, I've been doing a lot of work getting Numpy and Scipy to work on the Xeon Phi. YMMV but there is a Makefile that implements all the above and more here:

https://github.com/bpartridge/pyphi

0 Kudos
Reply