problem with nvmex in matlab
Asked Answered
E

2

1

i have installed matlab on my system and also have installed the CUDA SDK for windows. however i am not able to compile any .cu files. I have included the nvmex script file in the bin directory of the Matlab installation path. Can some body help?

Exothermic answered 6/9, 2011 at 15:56 Comment(0)
C
2

nvmex isn't really supported in any recent versions of Matlab or the Cuda SDK. Instead, I would suggest writing a simple DLL in Visual Studio which uses the standard MEX interface to run Cuda. I'm going to assume that your project is called "addAtoB" and that you just want to add two numbers together to make the example simpler.

When you installed the Cuda SDK, you need to tell it to add the CUDA Custom Build Rules to Visual Studio so that it will know how to compile .CU files.

Your main cpp should look something like this:

// addAtoB.cpp
#include <mex.h>
#include <cuda.h>
#include <driver_types.h>
#include <cuda_runtime_api.h>

#pragma comment(lib,"libmx.lib") // link with the Matlab MEX API
#pragma comment(lib,"libmex.lib")
#pragma comment(lib,"cudart.lib") // link with CUDA

// forward declare the function in the .cu file
void runMyCUDAKernel(void);

// input and output variables for the function in the .cu file
float A, B, C;

void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
    A = (float) mxGetScalar(prhs[0]);
    B = (float) mxGetScalar(prhs[1]);

    runMyCUDAKernel();

    // allocate output
    nlhs = 1;
    plhs[0] = mxCreateDoubleScalar(C);

    mexPrintf("GPU: %f + %f = %f\nCPU: %f", A, B, C, A+B);

    cudaDeviceReset();
}

You need add several directories to your Include Path: C:\Program Files\MATLAB\R2009a\extern\include and the CUDA directories.

Add to your Linker Path: C:\Program Files\MATLAB\R2009a\extern\lib\win32\microsoft , $(CUDA_PATH)\lib\$(PlatformName)

Next, add a .DEF file to your project which looks something like this:

LIBRARY    "addAtoB"
EXPORTS
    mexFunction

Next, create a file called runMyCUDAKernel.cu in the current directory, type in contents something like this, and then add the file to your project:

// runMyCUDAKernel.cu:
#include <cuda.h>
extern float A, B, C;

// Device kernel
__global__ void addAtoB(float A1, float B1, float* C1)
{
    *C1 = A1+B1;
}

void runMyCUDAKernel(void)
{
    float* pOutput;
    cudaMalloc( (void**) &pOutput, 1*sizeof(float));
    dim3 dimBlock(1, 1);
    dim3 dimGrid(1, 1);

    addAtoB<<< dimGrid, dimBlock >>>(A, B, pOutput);

    cudaMemcpy( &C, pOutput, 1*sizeof(float), cudaMemcpyDeviceToHost);
    cudaDeviceSynchronize();
    cudaFree(pOutput);
}

Build the DLL and rename it from .dll to .mexw32 (or .mexw64, if you're using a 64-bit Matlab). Then you should be able to run it with the command addAtoB(1, 2).

Columbarium answered 6/9, 2011 at 20:42 Comment(2)
My matlab version is R2010a and Microsoft visual studio's is 2008. i copied the nvmexopts.bat file to the buin directory of my matlab installation path and when i run the examples provided in the link: developer.download.nvidia.com/compute/cuda/1_1/… , i get the following error : Undefined subroutine &main::uuidgen called at C:\Program Files\MATLAB\R2010a\bin\nvmex.pl line 728. ??? Error using ==> nvmex at 206 Unable to complete successfully.Exothermic
nvmex is not supported in Matlab 2010a. You have to make a DLL instead. In Visual Studio: File -> New -> Project. Choose Win32. Choose Application Type -> DLL.Columbarium
C
0

I would suggest using CUDA mex from the Matlab file exchange.

It enables you to compile through Matlab. This gets better compatibility across Matlab and Visual Studio versions by not forcing you to specify the mex dependencies explicitly through Visual Studio.

Chariness answered 8/9, 2011 at 0:58 Comment(2)
"CUDA mex" is really just a re-implementation of nvmex. I disagree that it has "better compatibility" -- the Cuda Runtime dependency exists whether you want to explicitly indicate it or not. You will still need to distribute cudart32_40_17.dll with your .mexw32 file to other computers, or else it won't run or Matlab will crash.Columbarium
Sure, but it works well for me on recent version of Matlab (R2010b and R2011a). It's easier to recompile rather than changing mexversion.rc in the VS solution.Chariness

© 2022 - 2024 — McMap. All rights reserved.