# Mex nvcc matlab & linux

**URL:** <https://forums.developer.nvidia.com/t/mex-nvcc-matlab-linux/34319>\
**Category:** CUDA Setup and Installation\
**Created:** [August 4, 2014, 4:37pm UTC](https://forums.developer.nvidia.com/t/mex-nvcc-matlab-linux/34319 "2014-08-04T16:37:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giovmar9497](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Giovmar9497](https://forums.developer.nvidia.com/u/Giovmar9497)\
**Post date:** [August 4, 2014, 4:37pm UTC](https://forums.developer.nvidia.com/t/mex-nvcc-matlab-linux/34319/1 "2014-08-04T16:37:24Z")

</div>

Hi,  
Sorry if this topic is redundant, but I have not found any information on the web.  
I am new in GPU cuda programming and I am trying to copy a mex example on my pc, without success. I do not know if it is related to linux, matlab 2014 or something else.

I copied the following code, named AddVectorsCuda.cpp:

#include “mex.h”  
#include “AddVectors.h”

void mexFunction(int nlhs,mxArray \*plhs, int nrhs, mxArray \*prhs)  
{  
if (nrhs!=2)  
mexErrMsgTxt(“Invalid 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 the same.");

int minSize=(numRowsA<numColsA) ? numRowsA:numColsA;
int maxSize=(numRowsA>numColsA) ? numRowsA:numColsA;

if (minSize!=1)
    mexErrMsgTxt("Invalid size. The sizes of two vectors must be one dimentional.");

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

plhs[0]=mxCreateNumericMatrix(numRowsA,numColsB, mxSINGLE_CLASS, mxREAL);

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

addVectors(A,B,C,maxSize);

```

}

Where AddVectors.h  
#ifndef **ADDVECTORS\_H**  
#define **ADDVECTORS\_H**  
extern void addVectors(float\* A,float\* B,float\* C,int size);  
#endif // **ADDVECTORS\_H**

and AddVectors.cu:

#include “AddVectors.h”  
#include “/usr/local/MATLAB/R2014a/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);  
}

I created the file object by using:

system(‘nvcc -c AddVectors.cu’)

Finally I compiled mex by using:  
mex AddVectorsCuda.cpp AddVectors.o -lcudart -L"/usr/local/cuda/lib"

As result I got the following error message:

Building with ‘g++’.  
Error using mex  
/usr/bin/ld: AddVectors.o: relocation R\_X86\_64\_32 against `.bss’ can not be used when making a shared  
object; recompile with -fPIC  
AddVectors.o: could not read symbols: Bad value  
collect2: ld returned 1 exit status

What’s the problem?  
Thank you in advance for your help

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [August 4, 2014, 5:02pm UTC](https://forums.developer.nvidia.com/t/mex-nvcc-matlab-linux/34319/2 "2014-08-04T17:02:54Z")

</div>

Have you worked through the mathworks-provided example?

[url][http://www.mathworks.com/help/distcomp/run-mex-functions-containing-cuda-code.html[/url]](http://www.mathworks.com/help/distcomp/run-mex-functions-containing-cuda-code.html%5B/url%5D)

---

<div class="post-metadata">

**Author:** ![Giovmar9497](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Giovmar9497](https://forums.developer.nvidia.com/u/Giovmar9497)\
**Post date:** [August 6, 2014, 9:25am UTC](https://forums.developer.nvidia.com/t/mex-nvcc-matlab-linux/34319/3 "2014-08-06T09:25:06Z")

</div>

Unfortunately their examples do not work. I contact them, but they have not answered yet.

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [August 6, 2014, 12:24pm UTC](https://forums.developer.nvidia.com/t/mex-nvcc-matlab-linux/34319/4 "2014-08-06T12:24:59Z")

</div>

Well it doesn’t look like you have done it correctly. Nowhere in the example I linked does it suggest that you compile your mex function with:

system(‘nvcc -c AddVectors.cu’)

Saying “their examples do not work” isn’t that useful. You might instead identify, if you went through their sequence, at which point things did not work, and what the output you observed was.

Specifically, what happened when you tried to work through the mathworks example?
