我正在研究一个使用CUDA和Rcpp 的微型R包,它是根据输出改编的Rcpp.package.skeleton().我将首先描述主分支上标题为"fixed namespace"的提交.如果我忘记了CUDA,那么包安装成功(即,如果我删除src/Makefile,将src/rcppcuda.cu更改为src/rcppcuda.cpp,并注释掉定义和调用内核的代码).但是原样,编译失败了.
我也想知道如何使用Makevars或Makevars.in而不是Makefile进行编译,并且通常尝试将其作为平台独立实现.我在R扩展手册中读过关于Makevars的内容,但我仍然无法使其工作.
你们中的一些人可能会建议rCUDA,但我真正想要的是改进我已经开发了一段时间的大包装,而且我不确定切换是否值得从头开始.
无论如何,这是当我做一个R CMD build和R CMD INSTALL在这一个(主分支,提交标题为"固定名称空间")时发生的事情.
* installing to library ‘/home/landau/.R/library’
* installing *source* package ‘rcppcuda’ ...
** libs
** arch -
/usr/local/cuda/bin/nvcc -c rcppcuda.cu -o rcppcuda.o --shared -Xcompiler "-fPIC" -gencode arch=compute_20,code=sm_20 -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -I/apps/R-3.2.0/include -I/usr/local/cuda/include
rcppcuda.cu:1:18: error: Rcpp.h: No such file or directory
make: *** [rcppcuda.o] Error 1
ERROR: compilation failed for package ‘rcppcuda’
* removing ‘/home/landau/.R/library/rcppcuda’
Run Code Online (Sandbox Code Playgroud)
...这很奇怪,因为我确实包含了Rcpp.h,并且安装了Rcpp.
$ R
R version 3.2.0 (2015-04-16) -- "Full of Ingredients"
Copyright (C) 2015 The R Foundation for Statistical Computing
Platform: x86_64-unknown-linux-gnu (64-bit)
Run Code Online (Sandbox Code Playgroud)
...
> library(Rcpp)
> sessionInfo()
R version 3.2.0 (2015-04-16)
Platform: x86_64-unknown-linux-gnu (64-bit)
Running under: CentOS release 6.6 (Final)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] Rcpp_0.11.6
>
Run Code Online (Sandbox Code Playgroud)
我正在使用CentOS,
$ cat /etc/*-release
CentOS release 6.6 (Final)
LSB_VERSION=base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
CentOS release 6.6 (Final)
CentOS release 6.6 (Final)
Run Code Online (Sandbox Code Playgroud)
CUDA第6版,
$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2013 NVIDIA Corporation
Built on Thu_Mar_13_11:58:58_PDT_2014
Cuda compilation tools, release 6.0, V6.0.1
Run Code Online (Sandbox Code Playgroud)
我可以访问相同品牌和型号的4个GPU.
$ /usr/local/cuda/samples/bin/x86_64/linux/release/deviceQuery
/usr/local/cuda/samples/bin/x86_64/linux/release/deviceQuery Starting...
CUDA Device Query (Runtime API) version (CUDART static linking)
Detected 4 CUDA Capable device(s)
Device 0: "Tesla M2070"
CUDA Driver Version / Runtime Version 6.0 / 6.0
CUDA Capability Major/Minor version number: 2.0
Total amount of global memory: 5375 MBytes (5636554752 bytes)
(14) Multiprocessors, ( 32) CUDA Cores/MP: 448 CUDA Cores
GPU Clock rate: 1147 MHz (1.15 GHz)
Memory Clock rate: 1566 Mhz
Memory Bus Width: 384-bit
L2 Cache Size: 786432 bytes
Maximum Texture Dimension Size (x,y,z) 1D=(65536), 2D=(65536, 65535), 3D=(2048, 2048, 2048)
Maximum Layered 1D Texture Size, (num) layers 1D=(16384), 2048 layers
Maximum Layered 2D Texture Size, (num) layers 2D=(16384, 16384), 2048 layers
Total amount of constant memory: 65536 bytes
Total amount of shared memory per block: 49152 bytes
Total number of registers available per block: 32768
Warp size: 32
Maximum number of threads per multiprocessor: 1536
Maximum number of threads per block: 1024
Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
Max dimension size of a grid size (x,y,z): (65535, 65535, 65535)
Maximum memory pitch: 2147483647 bytes
Texture alignment: 512 bytes
Concurrent copy and kernel execution: Yes with 2 copy engine(s)
Run time limit on kernels: No
Integrated GPU sharing Host Memory: No
Support host page-locked memory mapping: Yes
Alignment requirement for Surfaces: Yes
Device has ECC support: Enabled
Device supports Unified Addressing (UVA): Yes
Device PCI Bus ID / PCI location ID: 11 / 0
Compute Mode:
< Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >
Run Code Online (Sandbox Code Playgroud)
...
> Peer access from Tesla M2070 (GPU0) -> Tesla M2070 (GPU1) : Yes
> Peer access from Tesla M2070 (GPU0) -> Tesla M2070 (GPU2) : Yes
> Peer access from Tesla M2070 (GPU0) -> Tesla M2070 (GPU3) : Yes
> Peer access from Tesla M2070 (GPU1) -> Tesla M2070 (GPU1) : No
> Peer access from Tesla M2070 (GPU1) -> Tesla M2070 (GPU2) : Yes
> Peer access from Tesla M2070 (GPU1) -> Tesla M2070 (GPU3) : Yes
> Peer access from Tesla M2070 (GPU2) -> Tesla M2070 (GPU1) : Yes
> Peer access from Tesla M2070 (GPU2) -> Tesla M2070 (GPU2) : No
> Peer access from Tesla M2070 (GPU2) -> Tesla M2070 (GPU3) : Yes
> Peer access from Tesla M2070 (GPU1) -> Tesla M2070 (GPU0) : Yes
> Peer access from Tesla M2070 (GPU1) -> Tesla M2070 (GPU1) : No
> Peer access from Tesla M2070 (GPU1) -> Tesla M2070 (GPU2) : Yes
> Peer access from Tesla M2070 (GPU2) -> Tesla M2070 (GPU0) : Yes
> Peer access from Tesla M2070 (GPU2) -> Tesla M2070 (GPU1) : Yes
> Peer access from Tesla M2070 (GPU2) -> Tesla M2070 (GPU2) : No
> Peer access from Tesla M2070 (GPU3) -> Tesla M2070 (GPU0) : Yes
> Peer access from Tesla M2070 (GPU3) -> Tesla M2070 (GPU1) : Yes
> Peer access from Tesla M2070 (GPU3) -> Tesla M2070 (GPU2) : Yes
deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 6.0, CUDA Runtime Version = 6.0, NumDevs = 4, Device0 = Tesla M2070, Device1 = Tesla M2070, Device2 = Tesla M2070, Device3 = Tesla M2070
Result = PASS
Run Code Online (Sandbox Code Playgroud)
编辑:它编译任何一个分支上的"固定命名空间"之后的任何提交,但是仍然存在组合Rcpp和CUDA的问题
为了使包编译,事实证明,我只需要我的C++和CUDA代码中分离成单独*.cpp和*.cu文件.但是,当我尝试在主分支上"分别编译cpp和cu"时,我得到了
> library(rcppcuda)
> hello()
An object of class "MyClass"
Slot "x":
[1] 1 2 3 4 5 6 7 8 9 10
Slot "y":
[1] 1 2 3 4 5 6 7 8 9 10
Error in .Call("someCPPcode", r) :
"someCPPcode" not resolved from current namespace (rcppcuda)
>
Run Code Online (Sandbox Code Playgroud)
该错误在withoutCUDA标题为"添加分支而没有CORA"的提交中的分支中消失.
> library(rcppcuda)
> hello()
An object of class "MyClass"
Slot "x":
[1] 1 2 3 4 5 6 7 8 9 10
Slot "y":
[1] 1 2 3 4 5 6 7 8 9 10
[1] "Object changed."
An object of class "MyClass"
Slot "x":
[1] 500 2 3 4 5 6 7 8 9 10
Slot "y":
[1] 1 1000 3 4 5 6 7 8 9 10
>
Run Code Online (Sandbox Code Playgroud)
"分别编译cpp和cu"提交master和"添加分支withoutCUDA"提交之间的唯一区别withoutCUDA是
withoutCUDA.withoutCUDA,所有引用someCUDAcode()都来自someCPPcode.cpp.此外,在同一*.cu文件中使用CUDA和Rcpp仍然很方便.我真的想知道如何修复主分支上的"固定命名空间"提交.
cde*_*man 10
通过您的包,有多个方面需要更改.
extern "C".您将为.cu文件中的函数添加前缀,并在文件的开头声明它cpp.以下Makevars为我工作,我修改了我的CUDA_HOME,R_HOME和RCPP_INC(为您切换).请注意,这是configure建议使用文件使程序包尽可能移植的位置.
CUDA_HOME = /usr/local/cuda
R_HOME = /apps/R-3.2.0
CXX = /usr/bin/g++
# This defines what the shared object libraries will be
PKG_LIBS= -L/usr/local/cuda-7.0/lib64 -Wl,-rpath,/usr/local/cuda-7.0/lib64 -lcudart -d
#########################################
R_INC = /usr/share/R/include
RCPP_INC = $(R_HOME)/library/Rcpp/include
NVCC = $(CUDA_HOME)/bin/nvcc
CUDA_INC = $(CUDA_HOME)/include
CUDA_LIB = $(CUDA_HOME)/lib64
LIBS = -lcudart -d
NVCC_FLAGS = -Xcompiler "-fPIC" -gencode arch=compute_20,code=sm_20 -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -I$(R_INC)
### Define objects
cu_sources := $(wildcard *cu)
cu_sharedlibs := $(patsubst %.cu, %.o,$(cu_sources))
cpp_sources := $(wildcard *.cpp)
cpp_sharedlibs := $(patsubst %.cpp, %.o, $(cpp_sources))
OBJECTS = $(cu_sharedlibs) $(cpp_sharedlibs)
all : rcppcuda.so
rcppcuda.so: $(OBJECTS)
%.o: %.cpp $(cpp_sources)
$(CXX) $< -c -fPIC -I$(R_INC) -I$(RCPP_INC)
%.o: %.cu $(cu_sources)
$(NVCC) $(NVCC_FLAGS) -I$(CUDA_INC) $< -c
Run Code Online (Sandbox Code Playgroud)
后续要点(正如你所说这是一个学习练习):
答:你没有使用Rcpp的一个部分来使它成为一个非常棒的包,即"属性".以下是您的cpp文件的外观:
#include <Rcpp.h>
using namespace Rcpp;
extern "C"
void someCUDAcode();
//[[Rcpp::export]]
SEXP someCPPcode(SEXP r) {
S4 c(r);
double *x = REAL(c.slot("x"));
int *y = INTEGER(c.slot("y"));
x[0] = 500.0;
y[1] = 1000;
someCUDAcode();
return R_NilValue;
}
Run Code Online (Sandbox Code Playgroud)
这将自动生成相应的RcppExports.cpp和RcppExports.R文件,您不再需要.Call自己的功能.你只需要调用该函数.现在.Call('someCPPcode', r)变成someCPPcode(r):)
为完整起见,这是更新的someCUDAcode.cu文件:
__global__ void mykernel(int a){
int id = threadIdx.x;
int b = a;
b++;
id++;
}
extern "C"
void someCUDAcode() {
mykernel<<<1, 1>>>(1);
}
Run Code Online (Sandbox Code Playgroud)
关于配置文件(使用autoconf),欢迎您使用Rcpp,CUDA和ViennaCL(C++ GPU计算库)查看我的gpuRcuda包.