cuda+matlab AddVectors

1. 创建AddVectors.h文件

1. 创建AddVectors.h文件

#ifndef __ADDVECTORS_H__
#define __ADDVECTORS_H_

extern void addVectors(float*A, float*B, float*C, int size);

#endif// ____ADDVECTORS_H_
2.AddVectors.cu函数

#include "AddVectors.h"

#include "D:\MATLAB\R2014b\extern\include\mex.h"

__global__ void addVectorsMask(float*A, float*B, float*C, int size)
{
    int i = blockIdx.x;
    if(i>= size)
        return;

    C[i] = A[i] + B[i];
}

void addVectors(float*A, float*B, float*C, int size)
{
    float *devPtrA = 0,*devPtrB = 0,*devPtrC = 0;
    cudaMalloc(&devPtrA,sizeof(float)* size);
    cudaMalloc(&devPtrB,sizeof(float)* size);
    cudaMalloc(&devPtrC,sizeof(float)* size);

    cudaMemcpy(devPtrA,A, sizeof(float)* size, cudaMemcpyHostToDevice);
    cudaMemcpy(devPtrB,B, sizeof(float)* size, cudaMemcpyHostToDevice);
    addVectorsMask<< <size,1>> >(devPtrA,devPtrB, devPtrC, size);
    cudaMemcpy(C,devPtrC, sizeof(float)* size, cudaMemcpyDeviceToHost);

    cudaFree(devPtrA);
    cudaFree(devPtrB);
    cudaFree(devPtrC);

}

3. AddVectors.cpp文件

#include "mex.h"
#include "addVectors.h"

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[]) 
{
    if (nrhs != 2)
        mexErrMsgTxt("Invaid number of input arguments");
    if (nlhs != 1)
        mexErrMsgTxt("Invalid number of outputs");
    if (!mxIsSingle(prhs[0]) && !mxIsSingle(prhs[1]))
        mexErrMsgTxt("input vector data type must be single");
    
    int numRowsA = (int)mxGetM(prhs[0]); 
    int numColsA = (int)mxGetN(prhs[0]); 
    int numRowsB = (int)mxGetM(prhs[1]); 
    int numColsB = (int)mxGetN(prhs[1]);
    
    if (numRowsA != numRowsB || numColsA != numColsB) 
        mexErrMsgTxt("Invalid size. The sizes of two vectors must be same");
    
    int minSize = (numRowsA < numColsA) ? numRowsA : numColsA; 
    int maxSize = (numRowsA > numColsA) ? numRowsA : numColsA;
    
    if (minSize != 1)
        mexErrMsgTxt("Invalid size. The vector must be one dimentional");

    float* A = (float*)mxGetData(prhs[0]);
    float* B = (float*)mxGetData(prhs[1]);

    //create the output vector
    plhs[0] = mxCreateNumericMatrix(numRowsA, numColsB, mxSINGLE_CLASS, mxREAL);

    float* C = (float*)mxGetData(plhs[0]);

    addVectors(A, B, C, maxSize);
}
4. runaddVectors.cpp文件
clc
clear all
close all

disp('1.nvccAddVectors.cu compiling...');
system('nvcc -c AddVectors.cu -ccbin "D:\Microsoft Visual Studio 9.0\VC\bin"')
disp('nvcccompiling done!');
disp('2.C/C++ compiling for AddVectorsCuda.cpp withAddVectors.obj...');
mex AddVectors.cpp AddVectors.obj -lcudart -L"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.0\lib\x64"
disp('C/C++compiling done!');
disp('3.TestAddVectors()...');
disp('Twoinput arrays:');

A = single([1 2 3 4 5 6 7 8 9 10]);
B = single([10 9 8 7 6 5 4 3 2 1]);
C = AddVectors(A,B);
5.结果
A = single([1 2 3 4 5 6 7 8 9 10]); 
B = single([10 9 8 7 6 5 4 3 2 1]); 
C = AddVectorsCuda(A,B);

 C = 11 11 11 11 11 11 11 11 11 11

6.可能出现的问题

出现错误提醒:

CUDA linking error - Visual Express 2008 - nvcc fatal due to (null) configuration file

解决办法:

Make sure that Windows SDK is targeting the correct version (7.0) by launching the Windows SDK Configuration Tool from start menu, choose the right version (v7.0), and click "Make Current".

Make sure to include the following directories for x64 compiling (under "Tools->Options->Projects And Solutions->VC++ Directories): C:\CUDA\bin64 C:\CUDA\lib64 C:\CUDA\include C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\amd64

Now there's one other thing to change for a 64-bit system. Apparently the cuda compiler has a "hard-coded" directory for 64-bit compilers on Visual Express 2008. To make the fix, copy the needed file "vcvars64.bat" and rename it to "vcvarsamd64.bat" as follows:

C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\vcvars64.bat

to

C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\amd64\vcvarsamd64.bat








评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值