nlesc-recruit / cudawrappers

C++ wrapper for the Nvidia C libraries (e.g. CUDA driver, nvrtc, cuFFT etc.)
https://cudawrappers.readthedocs.io/en/latest/
Apache License 2.0
5 stars 5 forks source link

Add build script for vector_add #116

Closed abelsiqueira closed 2 years ago

abelsiqueira commented 2 years ago

I have reached a point of being able to compile the example (#123), but when I try to run it on DAS-5, I get

cu::Error: named symbol not found

So I went back to version 0.1.0 and tried to compile the code before any of the major changes, but I still get the same error. Maybe someone with more C++ experience can help out? cc. @benvanwerkhoven @isazi @csbnw @john-romein @svlugt

Steps to reproduce on DAS-5, no CMake, no new changes:

module load cuda10.1
git clone https://github.com/nlesc-recruit/cudawrappers
cd cudawrappers
git checkout 0.1.0
nvcc cu/*.cc examples/vector_add/vector_add.cc examples/vector_add/vector_add_kernel.cu -o vector_add -I. -lcuda -lnvrtc
alias gpurun='srun -N 1 -C TitanX --gres=gpu:1'
gpurun vector_add
benvanwerkhoven commented 2 years ago

Looks to me like you are not linking the cudawrappers library.

The Makefile that I used to compile the vector_add example is:

LIBS = -L../../build/ -lcuda -lnvrtc

CUDA_WRAPPER_INCLUDE = -I../../
CUDA_WRAPPER_LIB = -Wl,-rpath=../../build/ -lcu

all: vector_add

vector_add: vector_add.cc vector_add_kernel.cu
    g++ $(LIBS) $(CUDA_WRAPPER_LIB) vector_add.cc $(CUDA_WRAPPER_INCLUDE) -o vector_add

clean:
    rm vector_add
abelsiqueira commented 2 years ago

Looks to me like you are not linking the cudawrappers library.

I added the source files explicitly (See my command nvcc cu/*.cc ...) to remove CMake from the issue, so there is no library to be linked, but the cudawrappers information is there.

But your answer showed me the solution. Apparently, the .cu is not necessary but it needs to be in the same folder. This works now:

nvcc ../../cu/*.cc vector_add.cc -o vector_add -I../../ -lcuda -lnvrtc
gpurun vector_add
abelsiqueira commented 2 years ago

@benvanwerkhoven, do you know the proper way of handling the issue of having .cu in the same folder?

benvanwerkhoven commented 2 years ago

That's the thing with runtime compilation. The cu file contains the kernel code, which is read in at run time and compiled by the runtime compiler. The running application needs to have a path to this file containing the kernel code. It needs to know where this file is. There are several ways in which you could make known to your application where the kernel codes are, but it's ultimately up to the application developer to decide how to organize these source files.

One trick that is used in the Tensor-core Correlator is to embed the kernel source code as a string, as you can see here: https://git.astron.nl/RD/tensor-core-correlator/-/blob/master/libtcc/Correlator.cc#L82

I seem to remember some people achieved this inclusion of a file's contents as a string in some way using the C preprocessor and C++ R raw strings, but I can't seem to find any examples looking for this right now.

benvanwerkhoven commented 2 years ago

This link: https://stackoverflow.com/questions/37292222/import-text-file-into-raw-string-literal-at-compile-time

Seems to suggest that it can't be done this way in C++. I do seem to remember seeing this trick being used somewhere at some point in the past. Well anyway, there are other options as well.

fdiblen commented 2 years ago

fixed in #123