Matlab中的nvmex问题 [英] problem with nvmex in matlab

查看:244
本文介绍了Matlab中的nvmex问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在系统上安装了matlab,还安装了Windows的CUDA SDK.但是,我无法编译任何.cu文件.我已经在Matlab安装路径的bin目录中包含了nvmex脚本文件.身体可以帮忙吗?

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?

推荐答案

在任何最新版本的Matlab或Cuda SDK中均不真正支持nvmex.相反,我建议在Visual Studio中编写一个使用标准MEX接口运行Cuda的简单DLL.我将假设您的项目名为"addAtoB",并且您只想将两个数字加起来以使示例更简单.

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.

在安装Cuda SDK时,需要告诉它将CUDA自定义生成规则添加到Visual Studio,以便它知道如何编译.CU文件.

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.

您的主要cpp应该看起来像这样:

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();
}

您需要将几个目录添加到包含路径":C:\ Program Files \ MATLAB \ R2009a \ extern \ include和CUDA目录.

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

添加到链接器路径:C:\ Program Files \ MATLAB \ R2009a \ extern \ lib \ win32 \ microsoft,$(CUDA_PATH)\ lib \ $(PlatformName)

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

接下来,将一个.DEF文件添加到您的项目中,如下所示:

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

LIBRARY    "addAtoB"
EXPORTS
    mexFunction

接下来,在当前目录中创建一个名为runMyCUDAKernel.cu的文件,输入类似以下内容,然后将该文件添加到您的项目中:

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);
}

构建DLL,并将其从.dll重命名为.mexw32(如果使用的是64位Matlab,则将其重命名为.mexw64).然后,您应该可以使用命令addAtoB(1, 2)运行它.

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

这篇关于Matlab中的nvmex问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆