Matlab / mex compile error - fPIC required

Hello,

I am an experienced matlab user but new at CUDA.

I have spent hours trying to resolve an error that occurs when compiling example code from a book “Accelerating Matlab with GPC Computing” (Jung W Suh / Youngmin Kim). The error is from example / chapter 2.5.

When compiling the code I receive a notice saying that “fPIC” is required.

The suggested code is:

mex AddVectorsCuda.cpp AddVectors.o -lcudart -L"/usr/local/cuda"

I have installed cuda. I can compile other examples. My question is – how can I recompile with fPIC ?

All help is greatly appreciated - this error has become very frustrating.

what command did you use to create AddVectors.o ?

You appear to be using a Linux platform. PIC is “position independent code” as used on x64 platforms, that is, your host, not the GPU.

Try adding -Xcompiler -fPIC to the nvcc commandline, to pass the -fPIC flag to the host compiler (gcc). You may want to familiarize yourself with the details of the host tool chain.

Bob,

The file was generated from a previously installed an older version of the cuda development tools that I had installed on my system via

sudo apt install nvidia-cuda-toolkit

The package installed seems to be, by default for an older version of cuda (7.5). I am having difficulty trying to install the tools for cuda 8 on an ubuntu 16.04 fresh install (as mentioned in another post https://devtalk.nvidia.com/default/topic/995277/cuda-8-0-toolkit-install-nvcc-not-found-ubuntu-16-04/#5090835).

njuffa,

Thank you for the note. I undestand the purpose of PIC, unfortunately tho I don’t know how to implement it when compiling in matlab / mex.

As mentioned I have used the example provided in the book “Accelerating Matlab with GPU Computing” to write the following call to compile the code in sample files, but I do not know how to implment or call the fPIC process during the mex compile.

As a novice to this I thought that adding the flag -fPIC to the command would work but it doesn’t. Mex doesn’t recognize it.

So again, would you have any suggestions for how to implement fPIC in the following compile?

mex AddVectorsCuda.cpp AddVectors.o -lcudart -L"/usr/local/cuda"

none of that answered my question.

But njuffa has already given you the gist of what I was going to suggest anyway.

let’s suppose that AddVectors.o was created using a command like this:

nvcc AddVectors.cu -o AddVectors.o

Then as suggested by njuffa, try

nvcc -Xcompiler -fPIC AddVectors.cu -o AddVectors.o

then try your mex command as-is. If that doesn’t work, then it would probably be necessary (for me, anyway) to see the full actual example in its entirety). I was really just guessing here anyway.

As an aside, I wouldn’t normally recommend using objects built on an older version of the CUDA toolkit with a newer version. Rebuild those objects (excepting so or dll libraries, possibly, in certain cases).

txbob / njuffa,

Thank you each for these suggestions. The process is much clearer now - thanks for pointing out the compile subtleties and distinctions between host and device. The book I was using didn’t express the logic as well as each of you have.

Appreciated.