I'm building a small R package using CUDA and Rcpp as a learning exercise (using Building a tiny R package with CUDA and Rcpp as inspiration). It includes a single function that takes a device ID and returns a few properties for that device.
This package compiles without any issues in WSL2 (Ubuntu 22.04, R 4.4.1, CUDA/nvcc 12.3) and returns the device properties as expected. To do so I'm using the Makevars
files as below:
CUDA_HOME = /usr/local/cuda-12.3
R_HOME = /home/killianmelsen/R/x86_64-pc-linux-gnu-library/4.4
R_INC = /usr/share/R/include
RCPP_INC = $(R_HOME)/Rcpp/include
NVCC = $(CUDA_HOME)/bin/nvcc
CUDA_INC = $(CUDA_HOME)/include
PKG_LIBS = -L$(CUDA_HOME)/lib64 -Wl,-rpath,$(CUDA_HOME)/lib64 -lcudart
NVCC_FLAGS = -x cu -Xcompiler "-fPIC" -gencode arch=compute_86,code=sm_86 -I$(R_INC)
### Define objects
sharedlibs = code.o RcppExports.o
sources = code.cpp RcppExports.cpp
OBJECTS = $(sharedlibs)
all : cudar.so
cudar.so: $(OBJECTS)
%.o: %.cpp $(sources)
$(NVCC) $(NVCC_FLAGS) -I$(CUDA_INC) -I$(R_INC) -I$(RCPP_INC) $< -c
The code.cpp
file includes the C++ as well as CUDA code. I simply tried "translating" the Makevars
to a Makevars.win
version that might work with Windows, but it didn't:
CUDA_HOME = "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.3"
R_HOME = "C:/Users/Killian/AppData/Local/R/win-library/4.2"
R_INC = "C:/Program Files/R/R-4.2.2/include"
RCPP_INC = $(R_HOME)/Rcpp/include
NVCC = $(CUDA_HOME)/bin/nvcc
CUDA_INC = $(CUDA_HOME)/include
PKG_LIBS = -L$(CUDA_HOME)/lib/x64 -Wl,-rpath,$(CUDA_HOME)/lib/x64 -lcudart
NVCC_FLAGS = -x cu -Xcompiler "-fPIC" -gencode arch=compute_86,code=sm_86 -I$(R_INC)
### Define objects
sharedlibs = code.obj RcppExports.obj
sources = code.cpp RcppExports.cpp
OBJECTS = $(sharedlibs)
all : cudar.dll
cudar.dll: $(OBJECTS)
%.obj: %.cpp $(sources)
$(NVCC) $(NVCC_FLAGS) -I$(CUDA_INC) -I$(R_INC) -I$(RCPP_INC) $< -c
At first I got an error that cl.exe
couldn't be found by nvcc, but it seems I fixed that by adding the folder containing cl.exe
to PATH
. At this point I get the following error:
── R CMD INSTALL ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
─ installing *source* package 'cudar' ...
** using staged installation
** libs
"C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.3"/bin/nvcc -x cu -Xcompiler "-fPIC" -gencode arch=compute_86,code=sm_86 -I"C:/Program Files/R/R-4.2.2/include" -I"C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.3"/include -I"C:/Program Files/R/R-4.2.2/include" -I"C:/Users/Killian/AppData/Local/R/win-library/4.2"/Rcpp/include code.cpp -c
cl : Command line warning D9002 : ignoring unknown option '-fPIC'
code.cpp
C:/Users/Killian/AppData/Local/R/win-library/4.2/Rcpp/include\Rcpp/platform/compiler.h(43): fatal error C1189: #error: "This compiler is not supported"
code.cpp
make: *** [Makevars.win:27: code.obj] Error 2
ERROR: compilation failed for package 'cudar'
─ removing 'C:/Users/Killian/AppData/Local/Temp/Rtmpgnlywh/devtools_install_44583263584d/cudar'
Error in `(function (command = NULL, args = character(), error_on_status = TRUE, …`:
! System command 'Rcmd.exe' failed
---
Exit status: 1
stdout & stderr: <printed>
---
Type .Last.error to see the more details.
I've uploaded the package to GitHub in case that helps: https://github.com/KillianMelsen/cudar/tree/main
What's going wrong here? Ideally I'd like to use CUDA in R within Windows as well.
Edit: I was able to get the linking to run by moving around some quotes (https://github.com/KillianMelsen/cudar/tree/split). However, there's a bunch of errors coming from the linking:
── R CMD INSTALL ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
─ installing *source* package 'cudar' ...
** using staged installation
** libs
g++ -std=gnu++11 -I"C:/Program Files/R/R-4.2.2/include" -I"C:/Users/Killian/AppData/Local/R/win-library/4.2/Rcpp/include" -std=gnu++11 -c CPP.cpp -o CPP.obj
g++ -std=gnu++11 -I"C:/Program Files/R/R-4.2.2/include" -I"C:/Users/Killian/AppData/Local/R/win-library/4.2/Rcpp/include" -std=gnu++11 -c RcppExports.cpp -o RcppExports.obj
"C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.3/bin/nvcc" -x cu -Xcompiler "-O2" -gencode arch=compute_86,code=sm_86 -I"C:/Program Files/R/R-4.2.2/include" -I"C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.3/include" -c CUDA.cu -o CUDA.obj
CUDA.cu
tmpxft_00004bcc_00000000-10_CUDA.cudafe1.cpp
g++ -std=gnu++11 -shared -static-libgcc -o cudar.dll tmp.def CPP.obj RcppExports.obj CUDA.obj -LC:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.3/lib/x64 -lcudart -LC:/Program Files/R/R-4.2.2/bin/x64 -lR -LC:/rtools42/x86_64-w64-mingw32.static.posix/lib/x64 -LC:/rtools42/x86_64-w64-mingw32.static.posix/lib -LC:/Program Files/R/R-4.2.2/bin/x64 -lR
Warning: corrupt .drectve at end of def file
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?abs@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?abs@@YANN@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?abs@@YA_J_J@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?acos@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?acosh@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?asin@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?asinh@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?atan2@@YAMMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?atan@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?atanh@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?cbrt@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?ceil@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?copysign@@YAMMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?cos@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?cosh@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?erf@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?erfc@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?exp2@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?exp@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?expm1@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?fabs@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?fdim@@YAMMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?floor@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?fma@@YAMMMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?fmax@@YAMMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?fmin@@YAMMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?fmod@@YAMMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?frexp@@YAMMPEAH@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?hypot@@YAMMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?ilogb@@YAHM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?ldexp@@YAMMH@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?lgamma@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?llrint@@YA_JM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?llround@@YA_JM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?log10@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?log1p@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?log2@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?log@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?logb@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?lrint@@YAJM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?lround@@YAJM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?modf@@YAMMPEAM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?nearbyint@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?nextafter@@YAMMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?pow@@YAMMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?remainder@@YAMMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?remquo@@YAMMMPEAH@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?rint@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?round@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?scalbln@@YAMMJ@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?signbit@@YA_NM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?signbit@@YA_NN@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?signbit@@YA_NO@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?sin@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?sinh@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?sqrt@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?tan@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?tanh@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?tgamma@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ?trunc@@YAMM@Z: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export fabsf: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export frexpf: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: cannot export ldexpf: symbol not defined
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: CUDA.obj:(.text$mn+0x10): undefined reference to `__security_cookie'
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: CUDA.obj:(.text$mn+0x86): undefined reference to `__security_check_cookie'
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: CUDA.obj:(.xdata[$unwind$getDeviceProps_CUDA]+0x10): undefined reference to `__GSHandlerCheck'
collect2.exe: error: ld returned 1 exit status
no DLL was created
ERROR: compilation failed for package 'cudar'
─ removing 'C:/Users/Killian/AppData/Local/Temp/RtmpMRqcYp/devtools_install_30807b7740ca/cudar'
Error in `(function (command = NULL, args = character(), error_on_status = TRUE, …`:
! System command 'Rcmd.exe' failed
---
Exit status: 1
stdout & stderr: <printed>
---
Type .Last.error to see the more details.
What's going on here?
This appears to be impossible.
The Windows build infrastructure for R appears to be MinGW/gcc based. CUDA requires the Visual C++ compiler and toolchain on Windows. This fundamental difference will make interoperability basically impossible.
Your only solution seems to be to move to a platform where both CUDA and R support the same toolchain (i.e. linux).
[Answer assembled from comments and added as a community wiki entry to get the question of the unanswered question queue for the CUDA tag. Feel free to edit as you see fit]