<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic In setup.py, ilp64 is used. in Intel® oneAPI Math Kernel Library</title>
    <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Writing-c-extension-for-python-that-calls-mkl-and-can-use-more/m-p/1098721#M23730</link>
    <description>&lt;P&gt;In setup.py, ilp64 is used.&lt;/P&gt;

&lt;P&gt;The 2nd parameter of csrcsc is request and has a MKL_INT type.&lt;/P&gt;

&lt;P&gt;MKL_INT should be long long instead of integer when ilp64 libraries are used.&lt;/P&gt;

&lt;P&gt;New setup.py that works:&lt;/P&gt;

&lt;PRE class="brush:python;"&gt;from distutils.core import setup, Extension
import numpy as np

d = {}
d['MKLROOT'] = "/gpfs/rxu/intel/compilers_and_libraries_2016.3.210/linux/mkl"

extra_compile_args = "-qopenmp -I{MKLROOT}/include".format(**d).split(' ')
extra_link_args = "-Wl,--start-group {MKLROOT}/lib/intel64/libmkl_intel_lp64.a {MKLROOT}/lib/intel64/libmkl_core.a {MKLROOT}/lib/intel64/libmkl_intel_thread.a -Wl,--end-group -lpthread -lm -ldl -liomp5".format(**d).split(' ')
ext_modules = [ Extension('mkl_helper', sources = ['mkl_helper.c'], extra_link_args=extra_link_args, extra_compile_args=extra_compile_args)] 

setup(
	name = 'mkl_helper',
	version = '1.0',
	include_dirs = [np.get_include()], #Add Include path of numpy
	ext_modules = ext_modules
)
&lt;/PRE&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 16 Jun 2016 20:13:00 GMT</pubDate>
    <dc:creator>Roger_X_</dc:creator>
    <dc:date>2016-06-16T20:13:00Z</dc:date>
    <item>
      <title>Writing c extension for python that calls mkl and can use more than 1 cpu.</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Writing-c-extension-for-python-that-calls-mkl-and-can-use-more/m-p/1098719#M23728</link>
      <description>&lt;P&gt;&lt;SPAN style="font-size: 13.008px; line-height: 19.512px;"&gt;I tried directly calling the mkl from python with ctypes, but in that case, mkl can only use a single cpu. The cause of that problem is unknown.&lt;/SPAN&gt;&lt;/P&gt;

&lt;P&gt;&lt;SPAN style="font-size: 13.008px; line-height: 19.512px;"&gt;I am writing a c extension for python that calls the mkl as an alternative approach.&lt;/SPAN&gt;&lt;/P&gt;

&lt;P&gt;&lt;SPAN style="line-height: 19.512px;"&gt;The following c extension can be imported into python without problem. However, when I call the function, it created the following error message:&lt;/SPAN&gt;&lt;/P&gt;

&lt;PRE class="brush:bash;"&gt;    Intel MKL FATAL ERROR: Cannot load libmkl_mc.so or libmkl_def.so&lt;/PRE&gt;

&lt;P&gt;What is the correct options for the icc compiler that I should use in setup.py?&lt;/P&gt;

&lt;P&gt;I get some of the options in the setup.py from the intel link line advisor. I can't put all the options into setup.py.&lt;/P&gt;

&lt;P&gt;mkl_helper.c&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt;#include "Python.h"
#include "mkl.h"
#include "numpy/arrayobject.h"

static PyObject* test4 (PyObject *self, PyObject *args)
{
	// test4 (m, n,
	//        a, ja, ia,
	//        c, jc, ic)
	
	PyArrayObject *shape_array;
	PyArrayObject *a_array;   // csr_matrix.data
	PyArrayObject *ja_array;  // csr_matrix.indices
	PyArrayObject *ia_array;  // csr_matrix.indptr
	PyArrayObject *c_array;
	PyArrayObject *jc_array;
	PyArrayObject *ic_array;
	
	if (!PyArg_ParseTuple(args, "O!O!O!O!O!O!O!", 
				&amp;amp;PyArray_Type, &amp;amp;shape_array,
				&amp;amp;PyArray_Type, &amp;amp;a_array,
				&amp;amp;PyArray_Type, &amp;amp;ja_array,
				&amp;amp;PyArray_Type, &amp;amp;ia_array,
				&amp;amp;PyArray_Type, &amp;amp;c_array,
				&amp;amp;PyArray_Type, &amp;amp;jc_array,
				&amp;amp;PyArray_Type, &amp;amp;ic_array))
	{
		return NULL;
	}

	int  * ptr_int     = shape_array-&amp;gt;data;
	int m               = ptr_int[0];
	int n               = ptr_int[1];
	int k               = n;

	float *  a_data_ptr =  a_array-&amp;gt;data;
	float * ja_data_ptr = ja_array-&amp;gt;data;
	float * ia_data_ptr = ia_array-&amp;gt;data;
	float *  c_data_ptr =  c_array-&amp;gt;data;
	float * jc_data_ptr = jc_array-&amp;gt;data;
	float * ic_data_ptr = ic_array-&amp;gt;data;

	char trans  = 'T';
	int sort    = 0;
	int nzmax   = n*n;
	int info    = 0;
	int request = 0;

	// This is supposed to "suggest" mkl use 12 threads.
&amp;nbsp;       // I also tried mkl_set_num_threads(&amp;amp;num_cpu);
&amp;nbsp;       // That also doesn't work.

	int num_cpu = 12;
	mkl_set_num_threads(12);
        mkl_set_num_threads_local(12);
        mkl_domain_set_num_threads(12,0);

	mkl_scsrmultcsr(&amp;amp;trans, &amp;amp;request, &amp;amp;sort,
			    &amp;amp;m, &amp;amp;n, &amp;amp;k,
			    a_data_ptr, ja_data_ptr, ia_data_ptr,
			    a_data_ptr, ja_data_ptr, ia_data_ptr,
			    c_data_ptr, jc_data_ptr, ic_data_ptr,
			    &amp;amp;nzmax, &amp;amp;info);

	return PyInt_FromLong(info);
}


static struct PyMethodDef methods[] = {
    {"test4", test4, METH_VARARGS, "test2(arr1)\n take a numpy array and return its shape as a tuple"},
    {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC
initmkl_helper (void)
{
    (void)Py_InitModule("mkl_helper", methods);
    import_array();
}
&lt;/PRE&gt;

&lt;P&gt;setup.py&lt;/P&gt;

&lt;PRE class="brush:python;"&gt;from distutils.core import setup, Extension
import numpy as np
extra_link_args=["-Bstatic","-I${MKLROOT}/include", "-L{$MKLROOT}/lib/intel64/"]
extra_link_args += ["-mkl"]
extra_link_args += ["-lrt" ]
extra_link_args += ["-L${MKLROOT}/lib/intel64/libmkl_intel_ilp64.a", "-L${MKLROOT}/lib/intel64/libmkl_core.a", "-L${MKLROOT}/lib/intel64/libmkl_intel_thread.a", "-lpthread", "-lm", "-ldl"] 

extra_link_args += ["-DMKL_ILP64", "-qopenmp" ,"-I${MKLROOT}/include"]

ext_modules = [ Extension('mkl_helper', sources = ['mkl_helper.c'], extra_link_args=extra_link_args) ]


setup(
        name = 'mkl_helper',
        version = '1.0',
        include_dirs = [np.get_include()], #Add Include path of numpy
        ext_modules = ext_modules
)
&lt;/PRE&gt;

&lt;P&gt;test.py&lt;/P&gt;

&lt;PRE class="brush:python;"&gt;import mkl_helper
import numpy as np

import numpy as np
import scipy.sparse as spsp

def get_csr_handle2(data, indices, indptr, shape):
	a_pointer   = data.ctypes.data_as(POINTER(c_float))
	ja_pointer  = indices.ctypes.data_as(POINTER(c_int))
	ia_pointer  = indptr.ctypes.data_as(POINTER(c_int))
	return (a_pointer, ja_pointer, ia_pointer, shape)

def get_csr_handle(A,clear=False):
	if clear == True:
		A.indptr[:] = 0
		A.indices[:] = 0
		A.data[:] = 0
	return get_csr_handle2(A.data, A.indices, A.indptr, A.shape)

print "test4"

test_size = 1200
test_size2 = 1200
AA = np.random.choice([0,1], size=(test_size,test_size2), replace=True, p=[0.99,0.01])
A_original = spsp.csr_matrix(AA)
print "Answer from scipy:"
print AA.dot(AA.T)
A = A_original.astype(np.float32).tocsc()
A = spsp.csr_matrix( (A.data, A.indices, A.indptr) )

A.indptr  += 1 # convert to 1-based indexing
A.indices += 1 # convert to 1-based indexing

C = spsp.csr_matrix( np.ones((test_size,test_size)), dtype=np.float32)

(m,n) = A.shape
shape_arr = np.array([m,n], dtype=np.int32)
while(True):
	ret = mkl_helper.test4(shape_arr, A.data, A.indices, A.indptr, C.data, C.indices, C.indptr)
C.indptr  -= 1
C.indices -= 1
nz = C.indptr[test_size]
print "nz:",nz
print "Answer from mkl"
C_fix = spsp.csr_matrix( (C.data[:nz], C.indices[:nz], C.indptr[:(test_size+1)]), shape=(test_size, test_size))
print C_fix.todense().astype(int)
#print C.todense()
print "ret:", ret&lt;/PRE&gt;</description>
      <pubDate>Thu, 16 Jun 2016 16:46:12 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Writing-c-extension-for-python-that-calls-mkl-and-can-use-more/m-p/1098719#M23728</guid>
      <dc:creator>Roger_X_</dc:creator>
      <dc:date>2016-06-16T16:46:12Z</dc:date>
    </item>
    <item>
      <title>I finally made a setup.py</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Writing-c-extension-for-python-that-calls-mkl-and-can-use-more/m-p/1098720#M23729</link>
      <description>&lt;P&gt;I finally made a setup.py that seems to work ...&amp;nbsp;&lt;/P&gt;

&lt;PRE class="brush:python;"&gt;from distutils.core import setup, Extension
import numpy as np
d = {}
d['MKLROOT'] = "/gpfs/rxu/intel/compilers_and_libraries_2016.3.210/linux/mkl"

extra_compile_args = "-DMKL_ILP64 -qopenmp -I{MKLROOT}/include".format(**d).split(' ')
extra_link_args = "-Wl,--start-group {MKLROOT}/lib/intel64/libmkl_intel_ilp64.a {MKLROOT}/lib/intel64/libmkl_core.a {MKLROOT}/lib/intel64/libmkl_intel_thread.a -Wl,--end-group -lpthread -lm -ldl -liomp5".format(**d).split(' ')
ext_modules = [ Extension('mkl_helper', sources = ['mkl_helper.c'], extra_link_args=extra_link_args, extra_compile_args=extra_compile_args)] 


setup(
        name = 'mkl_helper',
        version = '1.0',
        include_dirs = [np.get_include()], #Add Include path of numpy
        ext_modules = ext_modules
)&lt;/PRE&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;

&lt;P&gt;&lt;SPAN style="font-size: 1em; line-height: 1.5;"&gt;From mkl linkline advisor&lt;/SPAN&gt;&lt;/P&gt;

&lt;PRE class="brush:bash;"&gt;	Intel® Math Kernel Library (Intel® MKL) Link Line Advisor v4.6
	Select Intel® product:      Intel(R) Parallel Studio XE 2016
	Select OS:	                Linux*

	Select usage model of
	Intel® Xeon Phi™
	Coprocessor:                None

	Select compiler:	        Intel(R) C/C++
	Select architecture:        Intel(R) 64

	Select dynamic or
	static linking:	            Static

	Select interface layer:     ILP64 (64-bit integer)
	Select threading layer:     OpenMP threading
	Select OpenMP library:      Intel(R) (libiomp5)
	Select cluster library:     [ ] Cluster PARDISO (BLACS required)
								[ ] CDFT (BLACS required)
								[ ] ScaLAPACK (BLACS required)
								[ ] BLACS
	Select MPI library:         &amp;lt;Select MPI&amp;gt;

	Select the Fortran
	95 interfaces:              [ ] BLAS95
								[ ] LAPACK95

	Link with Intel® MKL
	libraries explicitly:       [Check] 

	Use this link line:
	 -Wl,--start-group ${MKLROOT}/lib/intel64/libmkl_intel_ilp64.a ${MKLROOT}/lib/intel64/libmkl_core.a ${MKLROOT}/lib/intel64/libmkl_intel_thread.a -Wl,--end-group -lpthread -lm -ldl

	Compiler options:
	 -DMKL_ILP64 -qopenmp -I${MKLROOT}/include&lt;/PRE&gt;

&lt;P&gt;&lt;SPAN style="font-size: 1em; line-height: 1.5;"&gt;shorthand:&lt;/SPAN&gt;&lt;/P&gt;

&lt;PRE class="brush:bash;"&gt;    %INTELHOME=/gpfs/rxu/intel/compilers_and_libraries_2016.3.210
    %MKLHOME=%INTELHOME/linux/mkl
    %USR=/home/rxu/local_icc/

&lt;/PRE&gt;

&lt;P&gt;Setting up evironment&lt;/P&gt;

&lt;PRE class="brush:bash;"&gt;    source %CCEHOME/bin/iccvars.sh 
    source %FCEHOME/bin/ifortvars.sh 
    source %OPENMPIHOME/bin/mpivars.sh
    source %INTELHOME/bin/compilervars.sh intel64
    export PATH=%USR/bin:/share/apps/pkgs/openmpi.1.8.1/bin:$PATH
    source %MKLHOME/bin/mklvars.sh intel64

    export CC="icc"
    export CXX="icpc"
    export F77=ifort
    export LD=xild
    export AR=xiar
    export CPP="icc -E"&lt;/PRE&gt;

&lt;P&gt;Output from python setup.py install&lt;/P&gt;

&lt;PRE class="brush:bash;"&gt;    running install
    running build
    running build_ext
    building 'mkl_helper' extension
    icc -fno-strict-aliasing -O3 -fp-model strict -fp-model source -xHost -ipo -prec-div -prec-sqrt -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -fPIC -I%USR/lib/python2.7/site-packages/numpy-1.11.0-py2.7-linux-x86_64.egg/numpy/core/include -I%USR/include/python2.7 -c mkl_helper.c -o build/temp.linux-x86_64-2.7/mkl_helper.o -DMKL_ILP64 -qopenmp -I%MKLHOME/include
    icc: command line warning #10006: ignoring unknown option '-qopenmp'

    ... some harmless warning ...

    icc -shared -L%USR/lib/ -L%USR/lib64/ -L%USR/lib/thread2.7.3 -L%USR/lib/itcl4.0.4 -L%USR/lib/tdbc1.0.4 -L%USR/lib/tdbcmysql1.0.4 -L%USR/lib/tdbcodbc1.0.4 -L%USR/lib/tdbcpostgres1.0.4 -L%USR/lib/sqlite3.11.0 -L%USR/lib/thread2.7.3/ -L%USR/lib/ -L%USR/lib64/ -L%USR/lib/thread2.7.3 -L%USR/lib/itcl4.0.4 -L%USR/lib/tdbc1.0.4 -L%USR/lib/tdbcmysql1.0.4 -L%USR/lib/tdbcodbc1.0.4 -L%USR/lib/tdbcpostgres1.0.4 -L%USR/lib/sqlite3.11.0 -L%USR/lib/thread2.7.3/ -L%USR/lib/ -L%USR/lib64/ -L%USR/thread2.7.3 build/temp.linux-x86_64-2.7/mkl_helper.o -L%USR/lib -lpython2.7 -o build/lib.linux-x86_64-2.7/mkl_helper.so -Wl,--start-group %MKLHOME/lib/intel64/libmkl_intel_ilp64.a %MKLHOME/lib/intel64/libmkl_core.a %MKLHOME/lib/intel64/libmkl_intel_thread.a -Wl,--end-group -lpthread -lm -ldl -liomp5
    %INTELHOME/linux/compiler/lib/intel64/libimf.so: warning: warning: feupdateenv is not implemented and will always fail
    running install_lib
    copying build/lib.linux-x86_64-2.7/mkl_helper.so -&amp;gt; %USR/lib/python2.7/site-packages
    running install_egg_info
    Removing %USR/lib/python2.7/site-packages/mkl_helper-1.0-py2.7.egg-info
    Writing %USR/lib/python2.7/site-packages/mkl_helper-1.0-py2.7.egg-info&lt;/PRE&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;

&lt;P&gt;The problem was that distutil library of python adds a bunch of flags. Those flags seems to be the ones I use when I manually compile numpy linked to mkl.&lt;/P&gt;

&lt;P&gt;The new error message is:&lt;/P&gt;

&lt;PRE class="brush:bash;"&gt;    Intel MKL ERROR: Parameter 2 was incorrect on entry to MKL_SCSRMULTCSR.&lt;/PRE&gt;

&lt;P&gt;Is this an error of the code, or an error of the compiler/linker flags in setup.py?&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jun 2016 19:30:00 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Writing-c-extension-for-python-that-calls-mkl-and-can-use-more/m-p/1098720#M23729</guid>
      <dc:creator>Roger_X_</dc:creator>
      <dc:date>2016-06-16T19:30:00Z</dc:date>
    </item>
    <item>
      <title>In setup.py, ilp64 is used.</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Writing-c-extension-for-python-that-calls-mkl-and-can-use-more/m-p/1098721#M23730</link>
      <description>&lt;P&gt;In setup.py, ilp64 is used.&lt;/P&gt;

&lt;P&gt;The 2nd parameter of csrcsc is request and has a MKL_INT type.&lt;/P&gt;

&lt;P&gt;MKL_INT should be long long instead of integer when ilp64 libraries are used.&lt;/P&gt;

&lt;P&gt;New setup.py that works:&lt;/P&gt;

&lt;PRE class="brush:python;"&gt;from distutils.core import setup, Extension
import numpy as np

d = {}
d['MKLROOT'] = "/gpfs/rxu/intel/compilers_and_libraries_2016.3.210/linux/mkl"

extra_compile_args = "-qopenmp -I{MKLROOT}/include".format(**d).split(' ')
extra_link_args = "-Wl,--start-group {MKLROOT}/lib/intel64/libmkl_intel_lp64.a {MKLROOT}/lib/intel64/libmkl_core.a {MKLROOT}/lib/intel64/libmkl_intel_thread.a -Wl,--end-group -lpthread -lm -ldl -liomp5".format(**d).split(' ')
ext_modules = [ Extension('mkl_helper', sources = ['mkl_helper.c'], extra_link_args=extra_link_args, extra_compile_args=extra_compile_args)] 

setup(
	name = 'mkl_helper',
	version = '1.0',
	include_dirs = [np.get_include()], #Add Include path of numpy
	ext_modules = ext_modules
)
&lt;/PRE&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jun 2016 20:13:00 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Writing-c-extension-for-python-that-calls-mkl-and-can-use-more/m-p/1098721#M23730</guid>
      <dc:creator>Roger_X_</dc:creator>
      <dc:date>2016-06-16T20:13:00Z</dc:date>
    </item>
    <item>
      <title>Yet. after all of this, mkl</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Writing-c-extension-for-python-that-calls-mkl-and-can-use-more/m-p/1098722#M23731</link>
      <description>&lt;P&gt;Yet. after all of this, mkl still just use one of the 12 cpu.&lt;/P&gt;

&lt;P&gt;Changing setup.py to the following would link mkl as shared library.&lt;/P&gt;

&lt;P&gt;This doesn't give any warning about icc fail to interpret any flag.&lt;/P&gt;

&lt;P&gt;extra_compile_args = "-I${MKLROOT}/include".format(**d).split(" ")&lt;BR /&gt;
	extra_link_args = "-L{MKLROOT}/lib/intel64 &amp;nbsp;-lmkl_rt -lpthread -lm -ldl".format(**d).split(" ")&lt;/P&gt;

&lt;P&gt;It still only use one out of the 12 cpu on the machine.&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jun 2016 21:28:00 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Writing-c-extension-for-python-that-calls-mkl-and-can-use-more/m-p/1098722#M23731</guid>
      <dc:creator>Roger_X_</dc:creator>
      <dc:date>2016-06-16T21:28:00Z</dc:date>
    </item>
    <item>
      <title>if we are talking about mkl</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Writing-c-extension-for-python-that-calls-mkl-and-can-use-more/m-p/1098723#M23732</link>
      <description>&lt;P&gt;if we are talking about&amp;nbsp;mkl_scsrmultcsr(...), then the problem size is pretty small and internally this computation is dispatched to 1 thread execution mode.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jun 2016 08:11:28 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Writing-c-extension-for-python-that-calls-mkl-and-can-use-more/m-p/1098723#M23732</guid>
      <dc:creator>Gennady_F_Intel</dc:creator>
      <dc:date>2016-06-17T08:11:28Z</dc:date>
    </item>
  </channel>
</rss>

