Cmake 协作组::此网格()导致任何CUDA API调用返回“未知错误”
按照中的相同步骤启动内核并使用协作组跨网格同步::此\u grid.sync会导致任何CUDA API调用失败。使用时 协作_组::此_thread_block.sync工作正常并给出正确的结果 我使用下面的代码和CMakeLists.txt cmake版本3.11.1,在TITAN V GPU驱动程序版本410.73和Ubuntu 16.04.5 LTS上使用CUDA 10对其进行测试。上还提供了代码,以便于再现错误 该代码读取一个数组,然后将其从[0 1 2…9]反转为[9 8 7…0]。为此,每个线程从数组中读取一个元素,进行同步,然后将其元素写入正确的目标。可以轻松修改代码,以确保此_thread_block.sync工作正常。只需将arr_大小更改为小于1024,并使用cg::thread_block barrier=cg::this_thread_block;相反 测试_cg.cu 运行此代码将提供: 第67行的/home/ahdhn/test_cg/test_cg.cu中存在未知错误Cmake 协作组::此网格()导致任何CUDA API调用返回“未知错误”,cmake,cuda,gpu,cg,gpu-cooperative-groups,Cmake,Cuda,Gpu,Cg,Gpu Cooperative Groups,按照中的相同步骤启动内核并使用协作组跨网格同步::此\u grid.sync会导致任何CUDA API调用失败。使用时 协作_组::此_thread_block.sync工作正常并给出正确的结果 我使用下面的代码和CMakeLists.txt cmake版本3.11.1,在TITAN V GPU驱动程序版本410.73和Ubuntu 16.04.5 LTS上使用CUDA 10对其进行测试。上还提供了代码,以便于再现错误 该代码读取一个数组,然后将其从[0 1 2…9]反转为[9 8 7…0]。为
这是使用Cudamaloc的第一行。我通过从设备中查询uu CUDA_ARCH_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。如果您发现我在代码或CMakeLists.txt文件中出错,请告诉我 在外部帮助下,使代码正常工作的解决方案是在第二个setCUDA\u NVCC\u标志后添加stringAPPEND CMAKE\u CUDA\u标志-gencode arch=compute\u 70,code=sm\u 70-cudart shared。。。。。。原因是我的/usr/local/cuda-10.0/lib64/下只有libcudadevrt.a,因此我必须向cuda发送信号以链接共享/动态运行时库,因为默认情况下是链接到静态库。stringAPPEND CMAKE_CUDA_标志-第二个setCUDA_NVCC_标志后的gencode arch=compute_70,code=sm_70。。。。。。原因是sm_70标志未正确传递给链接器
此外,仅使用CUDA_NVCC_标志将仅将sm_70信息传递给编译器,而不是链接器。虽然仅使用CMAKE_NVCC_标志将报告错误:命名空间协作_组没有成员网格_组错误 在外部帮助下,使代码正常工作的解决方案是在第二个setCUDA\u NVCC\u标志后添加stringAPPEND CMAKE\u CUDA\u标志-gencode arch=compute\u 70,code=sm\u 70-cudart shared。。。。。。原因是我的/usr/local/cuda-10.0/lib64/下只有libcudadevrt.a,因此我必须向cuda发送信号以链接共享/动态运行时库,因为默认情况下是链接到静态库。stringAPPEND CMAKE_CUDA_标志-第二个setCUDA_NVCC_标志后的gencode arch=compute_70,code=sm_70。。。。。。原因是sm_70标志未正确传递给链接器
此外,仅使用CUDA_NVCC_标志将仅将sm_70信息传递给编译器,而不是链接器。虽然仅使用CMAKE_NVCC_标志将报告错误:命名空间协作_组没有成员网格_组错误 cudamaloc调用应该是触发上下文初始化的调用。如果失败,你可能在CUDA运行时发现了一个bug。也许你的CUDA安装被破坏了。可能您的cmake设置没有创建正确的编译设置。在任何情况下,此代码都不可能工作,因为您正试图使用1的数组大小感谢您提供有用的注释。在外部帮助下,我能够使用CMake运行代码,数组大小为1时,您的代码无法在数组大小为的情况下正常工作1@RobertCrovella同意。我必须减小数组的大小以使代码正常工作并产生一些有意义的结果。如果失败,你可能在CUDA运行时发现了一个bug。也许你的CUDA安装被破坏了。可能您的cmake设置没有创建正确的编译设置。在任何情况下,此代码都不可能工作,因为您正试图使用1的数组大小感谢您提供有用的注释。在外部帮助下,我能够使用CMake运行代码,数组大小为1时,您的代码无法在数组大小为的情况下正常工作1@RobertCrovella同意。我必须减小数组大小以使代码正常工作并产生一些有意义的结果。libcudadevrt.a没有动态版本,-cudart共享标志不影响链接到libcudadevrt(设备运行时)。这会影响到普通运行时libcudart的链接。没有libcudadevrt.a的动态版本,-cudart shared标志不会影响到libcudadevrt(设备运行时)的链接。这会影响到与普通运行库libcudart的链接。
#include <cuda_runtime_api.h>
#include <stdio.h>
#include <stdint.h>
#include <cstdint>
#include <numeric>
#include <cuda.h>
#include <cooperative_groups.h>
namespace cg = cooperative_groups;
//********************** CUDA_ERROR
inline void HandleError(cudaError_t err, const char *file, int line) {
//Error handling micro, wrap it around function whenever possible
if (err != cudaSuccess) {
printf("\n%s in %s at line %d\n", cudaGetErrorString(err), file, line);
#ifdef _WIN32
system("pause");
#else
exit(EXIT_FAILURE);
#endif
}
}
#define CUDA_ERROR( err ) (HandleError( err, __FILE__, __LINE__ ))
//******************************************************************************
//********************** cg kernel
__global__ void testing_cg_grid_sync(const uint32_t num_elements,
uint32_t *d_arr){
uint32_t tid = threadIdx.x + blockDim.x*blockIdx.x;
if (tid < num_elements){
uint32_t my_element = d_arr[tid];
//to sync across the whole grid
cg::grid_group barrier = cg::this_grid();
//to sync within a single block
//cg::thread_block barrier = cg::this_thread_block();
//wait for all reads
barrier.sync();
uint32_t tar_id = num_elements - tid - 1;
d_arr[tar_id] = my_element;
}
}
//******************************************************************************
//********************** execute
void execute_test(const int sm_count){
//host array
const uint32_t arr_size = 1 << 20; //1M
uint32_t* h_arr = (uint32_t*)malloc(arr_size * sizeof(uint32_t));
//fill with sequential numbers
std::iota(h_arr, h_arr + arr_size, 0);
//device array
uint32_t* d_arr;
CUDA_ERROR(cudaMalloc((void**)&d_arr, arr_size*sizeof(uint32_t)));
CUDA_ERROR(cudaMemcpy(d_arr, h_arr, arr_size*sizeof(uint32_t),
cudaMemcpyHostToDevice));
//launch config
const int threads = 512;
//following the same steps done in conjugateGradientMultiBlockCG.cu
//cuda sample to launch kernel that sync across grid
//https://github.com/NVIDIA/cuda-samples/blob/master/Samples/conjugateGradientMultiBlockCG/conjugateGradientMultiBlockCG.cu#L436
int num_blocks_per_sm = 0;
CUDA_ERROR(cudaOccupancyMaxActiveBlocksPerMultiprocessor(&num_blocks_per_sm,
(void*)testing_cg_grid_sync, threads, 0));
dim3 grid_dim(sm_count * num_blocks_per_sm, 1, 1), block_dim(threads, 1, 1);
if(arr_size > grid_dim.x*block_dim.x){
printf("\n The grid size (numBlocks*numThreads) is less than array size.\n");
exit(EXIT_FAILURE);
}
printf("\n Launching %d blocks, each containing %d threads", grid_dim.x,
block_dim.x);
//argument passed to the kernel
void *kernel_args[] = {
(void *)&arr_size,
(void *)&d_arr, };
//finally launch the kernel
cudaLaunchCooperativeKernel((void*)testing_cg_grid_sync,
grid_dim, block_dim, kernel_args);
//make sure everything went okay
CUDA_ERROR(cudaGetLastError());
CUDA_ERROR(cudaDeviceSynchronize());
//get results on the host
CUDA_ERROR(cudaMemcpy(h_arr, d_arr, arr_size*sizeof(uint32_t),
cudaMemcpyDeviceToHost));
//validate
for (uint32_t i = 0; i < arr_size; i++){
if (h_arr[i] != arr_size - i - 1){
printf("\n Result mismatch in h_arr[%u] = %u\n", i, h_arr[i]);
exit(EXIT_FAILURE);
}
}
}
//******************************************************************************
int main(int argc, char**argv) {
//set to Titan V
uint32_t device_id = 0;
cudaSetDevice(device_id);
//get sm count
cudaDeviceProp devProp;
CUDA_ERROR(cudaGetDeviceProperties(&devProp, device_id));
int sm_count = devProp.multiProcessorCount;
//execute
execute_test(sm_count);
printf("\n Mission accomplished \n");
return 0;
}
cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
set(PROJECT_NAME "test_cg")
project(${PROJECT_NAME} LANGUAGES CXX CUDA)
#default build type is Release
if (CMAKE_BUILD_TYPE STREQUAL "")
set(CMAKE_BUILD_TYPE Release)
endif ()
SET(CUDA_SEPARABLE_COMPILATION ON)
########## Libraries/flags Starts Here ######################
find_package(CUDA REQUIRED)
include_directories("${CUDA_INCLUDE_DIRS}")
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS}; -lineinfo; -std=c++11; -expt-extended-lambda; -O3; -use_fast_math; -rdc=true;)
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS};-gencode=arch=compute_70,code=sm_70) #for TITAN V
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64 -Wall -std=c++11")
########## Libraries/flags Ends Here ######################
########## inc/libs/exe/features Starts Here ######################
set(CMAKE_INCLUDE_CURRENT_DIR ON)
CUDA_ADD_EXECUTABLE(${PROJECT_NAME} test_cg.cu)
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_11)
set_target_properties(${PROJECT_NAME} PROPERTIES POSITION_INDEPENDENT_CODE ON)
set_target_properties(${PROJECT_NAME} PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
target_link_libraries(${PROJECT_NAME} ${CUDA_LIBRARIES} ${CUDA_cudadevrt_LIBRARY})
########## inc/libs/exe/features Ends Here ######################