Mar 012011
 

A simple way to use the Intel compilers, MKL libraries with your Mex file creation?

Go no further 🙂

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/make
 
BINARY = mexfile
MEX_SUFFIX = mexa64
 
MATLAB = /home/dwyer3/Software/R2010b
INTEL = /opt/intel/composerxe-2011.0.084
MKLROOT = $(INTEL)/mkl/lib/intel64
 
CC = icpc
OPTS = -DMATLAB_MEX_FILE -DMKL_ILP64 -fPIC -fno-omit-frame-pointer -DMX_COMPAT_32 -O3 -xHost 
SRC = mexfile.cpp
OBJS = $(SRC:.cpp=.o)
 
INC = -I$(MATLAB)/extern/include -I$(MATLAB)/simulink/include
LIBS = -shared -Wl,--version-script,$(MATLAB)/extern/lib/glnxa64/mexFunction.map -Wl,--no-undefined 
LIBS += -Wl,-rpath-link,$(MATLAB)/bin/glnxa64 -L$(MATLAB)/bin/glnxa64 -lmx -lmex -lmat 
LIBS += -Wl,--start-group $(MKLROOT)/libmkl_intel_ilp64.a $(MKLROOT)/libmkl_sequential.a $(MKLROOT)/libmkl_core.a -Wl,--end-group
LIBS += -lpthread $(INTEL)/compiler/lib/intel64/libimf.a
 
.SUFFIXES: .cpp .o
 
.cpp.o:
	$(CC) $(OPTS) $(INC) -c $< -o $@
 
Go: $(OBJS)
	$(CC) $(OBJS) $(OPTS) -o $(BINARY).$(MEX_SUFFIX) $(LIBS)
	@echo Binary created!!
 
clean:
	set nonomatch; rm -f $(BINARY).$(MEX_SUFFIX) *.o

It is no mistake that in the above I am statically linking the mkl libraries. Matlab comes bundled with its own mkl and when I wrote the above makefile, there was a difference between them (MatlabR2010b: MKL 10.0.1, Me: MKL 10.3):
symbol lookup error: /opt/intel/composerxe-2011.0.084/mkl/lib/intel64/libmkl_vml_mc3.so: undefined symbol: mkl_serv_mkl_malloc

Note: To find out which version of MKL your matlab is using, open the matlab help and search for mkl. It should be the first hit.

Nice one Matlab! If you dynamically link your libraries, it can pay to take two minutes and examine that your mex file is linked to the correct libraries. If I created dynamically linked libraries, check out the difference

Outside Matlab:

        $ldd mexfile.mexa64
	linux-vdso.so.1 =>  (0x00007fffe23ff000)
	libmx.so => not found
	libmex.so => not found
	libmat.so => not found
	libmkl_intel_ilp64.so => /opt/intel/composerxe-2011.0.084/mkl/lib/intel64/libmkl_intel_ilp64.so (0x00007fa470c50000)
	libmkl_sequential.so => /opt/intel/composerxe-2011.0.084/mkl/lib/intel64/libmkl_sequential.so (0x00007fa470660000)
	libmkl_core.so => /opt/intel/composerxe-2011.0.084/mkl/lib/intel64/libmkl_core.so (0x00007fa46f7c0000)
	libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fa46f5a3000)
	libimf.so => /opt/intel/composerxe-2011.0.084/compiler/lib/intel64/libimf.so (0x00007fa46f1c5000)
	libsvml.so => /opt/intel/composerxe-2011.0.084/compiler/lib/intel64/libsvml.so (0x00007fa46eb8c000)
	libm.so.6 => /lib64/libm.so.6 (0x00007fa46e907000)
	libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00007fa46e601000)
	libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007fa46e3eb000)
	libintlc.so.5 => /opt/intel/composerxe-2011.0.084/compiler/lib/intel64/libintlc.so.5 (0x00007fa46e29b000)
	libc.so.6 => /lib64/libc.so.6 (0x00007fa46df1d000)
	libdl.so.2 => /lib64/libdl.so.2 (0x00007fa46dd19000)
	/lib64/ld-linux-x86-64.so.2 (0x0000003f70000000)

Inside Matlab:

        !ldd mexfile.mexa64
	linux-vdso.so.1 =>  (0x00007fff48794000)
	libmx.so => /home/dwyer3/Software/R2010b/bin/glnxa64/libmx.so (0x00007ff5a74e8000)
	libmex.so => /home/dwyer3/Software/R2010b/bin/glnxa64/libmex.so (0x00007ff5a72da000)
	libmat.so => /home/dwyer3/Software/R2010b/bin/glnxa64/libmat.so (0x00007ff5a70a0000)
	libmkl_intel_ilp64.so => /home/dwyer3/Software/R2010b/bin/glnxa64/libmkl_intel_ilp64.so (0x00007ff5a6a10000)
	libmkl_sequential.so =>/home/dwyer3/Software/R2010b/bin/glnxa64/libmkl_sequential.so (0x00007ff5a6420000)
	libmkl_core.so => /home/dwyer3/Software/R2010b/bin/glnxa64/libmkl_core.so (0x00007ff5a5580000)
	libpthread.so.0 => /lib64/libpthread.so.0 (0x00007ff5a5344000)
	libimf.so => /home/dwyer3/Software/R2010b/sys/os/glnxa64/libimf.so (0x00007ff5a4fb2000)
	libsvml.so => /home/dwyer3/Software/R2010b/sys/os/glnxa64/libsvml.so (0x00007ff5a4d9b000)
	libm.so.6 => /lib64/libm.so.6 (0x00007ff5a4b16000)
	libstdc++.so.6 => /home/dwyer3/Software/R2010b/sys/os/glnxa64/libstdc++.so.6 (0x00007ff5a4810000)
	libgcc_s.so.1 => /home/dwyer3/Software/R2010b/sys/os/glnxa64/libgcc_s.so.1 (0x00007ff5a45fa000)
	libintlc.so.5 => /home/dwyer3/Software/R2010b/sys/os/glnxa64/libintlc.so.5 (0x00007ff5a44bc000)
	libc.so.6 => /lib64/libc.so.6 (0x00007ff5a413e000)
	libdl.so.2 => /lib64/libdl.so.2 (0x00007ff5a3f3a000)
	libut.so => /home/dwyer3/Software/R2010b/bin/glnxa64/../../bin/glnxa64/libut.so (0x00007ff5a3c53000)
	libmwfl.so => /home/dwyer3/Software/R2010b/bin/glnxa64/../../bin/glnxa64/libmwfl.so (0x00007ff5a39ad000)
	libicudata.so.42 => /home/dwyer3/Software/R2010b/bin/glnxa64/../../bin/glnxa64/libicudata.so.42 (0x00007ff5a2867000)
	libicuuc.so.42 => /home/dwyer3/Software/R2010b/bin/glnxa64/../../bin/glnxa64/libicuuc.so.42 (0x00007ff5a2508000)
	libicui18n.so.42 => /home/dwyer3/Software/R2010b/bin/glnxa64/../../bin/glnxa64/libicui18n.so.42 (0x00007ff5a2165000)
	libicuio.so.42 => /home/dwyer3/Software/R2010b/bin/glnxa64/../../bin/glnxa64/libicuio.so.42 (0x00007ff5a1f59000)
	libz.so.1 => /lib64/libz.so.1 (0x00007ff5a1d43000)
	libmwservices.so => /home/dwyer3/Software/R2010b/bin/glnxa64/../../bin/glnxa64/libmwservices.so (0x00007ff5a198e000)
	libmwmpath.so => /home/dwyer3/Software/R2010b/bin/glnxa64/../../bin/glnxa64/libmwmpath.so (0x00007ff5a1750000)
	libmwm_dispatcher.so => /home/dwyer3/Software/R2010b/bin/glnxa64/../../bin/glnxa64/libmwm_dispatcher.so (0x00007ff5a14d1000)
	libhdf5_hl.so.6 => /home/dwyer3/Software/R2010b/bin/glnxa64/../../bin/glnxa64/libhdf5_hl.so.6 (0x00007ff5a12a0000)
	libhdf5.so.6 => /home/dwyer3/Software/R2010b/bin/glnxa64/../../bin/glnxa64/libhdf5.so.6 (0x00007ff5a0c30000)
	/lib64/ld-linux-x86-64.so.2 (0x0000003f70000000)
	librt.so.1 => /lib64/librt.so.1 (0x00007ff5a0a27000)
	libmwi18n.so => /home/dwyer3/Software/R2010b/bin/glnxa64/../../bin/glnxa64/../../bin/glnxa64/libmwi18n.so (0x00007ff5a07e2000)
	libboost_date_time.so.1.40.0 => /home/dwyer3/Software/R2010b/bin/glnxa64/../../bin/glnxa64/../../bin/glnxa64/libboost_date_time.so.1.40.0 (0x00007ff5a05cf000)
	libboost_system.so.1.40.0 => /home/dwyer3/Software/R2010b/bin/glnxa64/../../bin/glnxa64/../../bin/glnxa64/libboost_system.so.1.40.0 (0x00007ff5a03cb000)
	libboost_thread.so.1.40.0 => /home/dwyer3/Software/R2010b/bin/glnxa64/../../bin/glnxa64/../../bin/glnxa64/libboost_thread.so.1.40.0 (0x00007ff5a01b6000)
	libexpat.so.1 => /home/dwyer3/Software/R2010b/bin/glnxa64/../../bin/glnxa64/../../bin/glnxa64/libexpat.so.1 (0x00007ff59ff8e000)
	libtbb.so.2 => /home/dwyer3/Software/R2010b/bin/glnxa64/../../bin/glnxa64/../../bin/glnxa64/libtbb.so.2 (0x00007ff59fe56000)
	libtbbmalloc.so.2 => /home/dwyer3/Software/R2010b/bin/glnxa64/../../bin/glnxa64/../../bin/glnxa64/libtbbmalloc.so.2 (0x00007ff59fd31000)
	libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00007ff59faf9000)
	libboost_filesystem.so.1.40.0 => /home/dwyer3/Software/R2010b/bin/glnxa64/../../bin/glnxa64/../../bin/glnxa64/libboost_filesystem.so.1.40.0 (0x00007ff59f8e5000)
	libboost_signals.so.1.40.0 => /home/dwyer3/Software/R2010b/bin/glnxa64/../../bin/glnxa64/../../bin/glnxa64/libboost_signals.so.1.40.0 (0x00007ff59f6cf000)
	libmwmathutil.so => /home/dwyer3/Software/R2010b/bin/glnxa64/../../bin/glnxa64/../../bin/glnxa64/libmwmathutil.so (0x00007ff59f451000)
	libmwmlutil.so => /home/dwyer3/Software/R2010b/bin/glnxa64/../../bin/glnxa64/../../bin/glnxa64/libmwmlutil.so (0x00007ff59f106000)
	libboost_serialization.so.1.40.0 => /home/dwyer3/Software/R2010b/bin/glnxa64/../../bin/glnxa64/../../bin/glnxa64/libboost_serialization.so.1.40.0 (0x00007ff59ee94000)
	libncurses.so.5 => /lib64/libncurses.so.5 (0x00007ff59ec71000)
	libmwxmlcore.so => /home/dwyer3/Software/R2010b/bin/glnxa64/../../bin/glnxa64/../../bin/glnxa64/libmwxmlcore.so (0x00007ff59e9f6000)
	libboost_regex.so.1.40.0 => /home/dwyer3/Software/R2010b/bin/glnxa64/../../bin/glnxa64/../../bin/glnxa64/libboost_regex.so.1.40.0 (0x00007ff59e723000)
	libxerces-c.so.27 => /home/dwyer3/Software/R2010b/bin/glnxa64/../../bin/glnxa64/../../bin/glnxa64/libxerces-c.so.27 (0x00007ff59e124000)
	libfreebl3.so => /lib64/libfreebl3.so (0x00007ff59dec6000)
	libtinfo.so.5 => /lib64/libtinfo.so.5 (0x00007ff59dc9d000)

You wouldn’t believe the pain I’ve experienced in the past trying to figure out what was going on.

Tread carefully!

  4 Responses to “Matlab: Mex and MKL”

  1. Thanks for the enlightenment !

    I was having a complete headache trying to link mkl with mex. In addition of mkl,
    I am using openmp (liomp5) and I need to do “export KMP_DUPLICATE_LIB_OK=TRUE”, otherwise it crashes.
    Not a big deal though.

    Thanks again for the nice makefile.

    Julien.

    • Thanks Julien,

      I haven’t had time to check out the KMP_DUPLICATE_LIB_OK env. var yet. Looks interesting … I’d done OpenMP mex and MKL mex previously but not the two together.

      I’m busy with a big MPI job at the moment, but once it is done, I look forward to checking out what you did.

      Mark

    • Hi

      Thank you for this make file. I was wondering? Could someone share the opnmp makefile with liomp5 ? It would be greatly appreciated. Thank you again for the post.

      • Hi Ali,

        I no longer have access to the Intel libraries or Matlab but it should be fairly simple

        COMPILER_LIB = $(INTEL)/compilers_and_libraries/linux/lib/intel64

        and edit
        LIBS += -Wl,–start-group $(MKLROOT)/libmkl_intel_ilp64.a $(MKLROOT)/libmkl_sequential.a $(MKLROOT)/libmkl_core.a -Wl,–end-group

        to be

        LIBS += -Wl,–start-group $(MKLROOT)/libmkl_intel_ilp64.a $(MKLROOT)/libmkl_sequential.a $(MKLROOT)/libmkl_core.a $(COMPILER_LIB)/libiomp5.a -Wl,–end-group

        or, if that doesn’t work

        LIBS += $(COMPILER_LIB)/libiomp5.a

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

Human Conf Test * Time limit is exhausted. Please reload CAPTCHA.