Vinay
Vinay

Reputation: 27

Matlab 2013a + MEX C program

I am trying to run a program that implements kmeans which could be called from matlab2013a. However, I am getting unresolved external symbols on these..

Error   10  error LNK2001: unresolved external symbol _mxCreateNumericMatrix_730    D:\Users\VinayB\Documents\Visual Studio 2013\Projects\kmeans_ba\kmeans_ba\kmeans_serial.obj kmeans_ba

Error   11  error LNK2001: unresolved external symbol _mxGetData    D:\Users\VinayB\Documents\Visual Studio 2013\Projects\kmeans_ba\kmeans_ba\kmeans_serial.obj kmeans_ba

Error   12  error LNK2001: unresolved external symbol _mxGetN   D:\Users\VinayB\Documents\Visual Studio 2013\Projects\kmeans_ba\kmeans_ba\kmeans_serial.obj kmeans_ba

Error   13  error LNK2001: unresolved external symbol _mxGetPr  D:\Users\VinayB\Documents\Visual Studio 2013\Projects\kmeans_ba\kmeans_ba\kmeans_serial.obj kmeans_ba

Error   14  error LNK2001: unresolved external symbol _mexPrintf    D:\Users\VinayB\Documents\Visual Studio 2013\Projects\kmeans_ba\kmeans_ba\kmeans_serial.obj kmeans_ba

Error   15  error LNK2001: unresolved external symbol _mxGetM   D:\Users\VinayB\Documents\Visual Studio 2013\Projects\kmeans_ba\kmeans_ba\kmeans_serial.obj kmeans_ba

I have included appropriate include directories and lib files..

Library Directories

\extern\lib\win64\microsoft \extern\lib\win32\microsoft

Include Directories

<matlab dir>\extern\include
<matlab dir>\extern\include\win64

Additional Dependencies

cudart.lib
kernel32.lib
user32.lib
gdi32.lib
winspool.lib
comdlg32.lib
advapi32.lib
shell32.lib
ole32.lib
oleaut32.lib
uuid.lib
odbc32.lib
odbccp32.lib
libemlrt.lib
libeng.lib
libfixedpoint.lib
libmat.lib
libmex.lib
libmwblas.lib
libmwblascompat32.lib
libmwcgir_construct.lib
libmwimreconstruct.lib
libmwimregionalmax.lib
libmwippreconstruct.lib
libmwlapack.lib
libmwmathutil.lib
libmwrtiostreamutils.lib
libmwservices.lib
libmwsilpilprofiling.lib
libmwslexec_parallel.lib
libmwsl_fileio.lib
libmwsl_solver_rtw.lib
libmx.lib
libut.lib
mclbase.lib
mclcommain.lib
mclmcr.lib
mclmcrrt.lib
mclxlmain.lib
ne_mli.lib
ne_rtl.lib
physmod_common_foundation_core_util.lib
rtwcg.lib
SimulinkBlock.lib

Upvotes: 0

Views: 518

Answers (1)

Saharsh Bishnoi
Saharsh Bishnoi

Reputation: 361

So here is a list of all the include paths and library dependencies required to build this for Windows 8 x64 with CudaToolkit NVCC and MEX DLLs using VS2013

Includes:

C:\Program Files\MATLAB\R2014b\extern\include;$(IncludePath) %(AdditionalIncludeDirectories) $(CudaToolkitIncludeDir)

Libs:

C:\Program Files\MATLAB\R2014b\extern\lib\win64\microsoft
$(CudaToolkitLibDir)

libmx.lib libmat.lib libmex.lib cublas.lib cuda.lib cudart.lib cufft.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib

As correctly setting up NVCC with VS is quite laborious I started out with the Nsight Visual Studio CUDA vector example and added the libs manually according to the makefile. Apart from -largeArrayDims which is an indication to the compiler to use 64bit addressing. When using 64bit libs this is selected by default. After deleting the Win32 and Debug configurations I used the Release x64 config for compiling MEX files.

There are basically 2 main projects cudaKmeans (using .cu) & kmeans_serial (using .c) compiled normally and then with the -D TIMEONLY preprocessor directive for additional timing code. That means we need a total of 4 build projects with cudaKmeans_Timedemo & kmeans_serial_Timedemo also. I used 4 directories for these 4 projects to generate 4 mex files (with 4 def files) in the bin directory with the solution file (for building all these projects) in the main directory.

Test results for cudaKmeans: cudaKmeans

Gains for cuda against c: cgains Note that there is no gain due to the memory overhead. The CPU prefetcher with C optimizations are responsible for no clear gains when using CUDA. The CPU is a server class Xeon E5 with lots of cache which is why the results show the CPU is faster. I was using a 860GTX.

Here are the gains against Matlab: matlab gains

Quite a mixed bag. To be honest the timing seems off. A little bit random due to the way timing is resolved under windows. The test code was using rubbish generic timers rather than RDTSC so I wouldn't worry too much about that.

Here or here is the link to the modified code and VS solution. I used CudaToolkit 6.5 and MATLAB2014b for the projects but changing it to a different MATLAB ver should be very easy. Note that in the projects I've selected 3.0 as the CUDA arch (the default was 2.0). You might want to change that if your GPU has a compute score of < 3.0. This library and the test code is a bit buggy and does not look "production quality". You might be better off using Matlab's kmeans accelerated by Parallel Computing Toolbox or this or preferably this

Upvotes: 1

Related Questions