problem with nvmex in matlab

血红的双手。 提交于 2019-12-13 20:04:38

问题


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?


回答1:


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




回答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.



来源:https://stackoverflow.com/questions/7322732/problem-with-nvmex-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!