开发者

problem with nvmex in matlab

开发者 https://www.devze.com 2023-04-03 05:02 出处:网络
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

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 pat开发者_Python百科h. Can some body help?


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).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号