CUDA Toolkit集成Thrust库缺失universal_vector.h等文件问题咨询
问题解决:CUDA 11.1中Thrust缺失
universal_vector.h等文件的编译错误 问题根源
CUDA 11.1附带的Thrust版本较旧,universal_vector.h、universal_allocator.h这类属于Thrust后续版本新增的特性,并未包含在CUDA 11.1的默认Thrust包中,因此会出现头文件找不到的编译错误。
解决方案
方案1:升级CUDA Toolkit
将CUDA版本升级到11.7及以上版本,这些版本的CUDA Toolkit自带的Thrust已经包含universal_vector相关头文件,升级后无需修改代码,直接按原有方式引用即可正常编译。
方案2:引入最新版Thrust并正确配置CMake
如果无法升级CUDA,可以单独引入最新版Thrust,同时调整CMake配置确保优先使用新Thrust:
- 下载最新版Thrust(克隆仓库或下载压缩包解压)到本地路径;
- 修改CMakeLists.txt,加入最新Thrust的include目录,并确保其优先级高于CUDA自带的Thrust:
cmake_minimum_required(VERSION 3.10) set(CMAKE_CXX_STANDARD 17) set(CMAKE_BUILD_TYPE Release) project(hellocuda LANGUAGES CXX CUDA) # 替换为你本地最新Thrust的include目录路径 set(THRUST_NEW_INCLUDE "/path/to/thrust/repo/include") # 优先引入新Thrust的头文件 target_include_directories(main PRIVATE ${THRUST_NEW_INCLUDE}) add_executable(main main.cu) target_include_directories(main PUBLIC ../../include) target_compile_options(main PUBLIC $<$<COMPILE_LANGUAGE:CUDA>:--extended-lambda>) target_compile_options(main PUBLIC $<$<COMPILE_LANGUAGE:CUDA>:--expt-relaxed-constexpr>)
方案3:使用CUDA 11.1支持的替代API
如果不想升级或引入新Thrust,可以改用CUDA 11.1原生支持的host_vector和device_vector实现CPU/GPU数据传输,代码示例:
#include <thrust/host_vector.h> #include <thrust/device_vector.h> #include <thrust/copy.h> int main() { // 主机端向量 thrust::host_vector<int> host_data(10); for (int i = 0; i < 10; ++i) { host_data[i] = i; } // 拷贝到设备端 thrust::device_vector<int> device_data = host_data; // 执行GPU计算... // 拷贝回主机端 thrust::copy(device_data.begin(), device_data.end(), host_data.begin()); return 0; }
内容的提问来源于stack exchange,提问作者riversandroads
相关产品推荐
相关产品推荐

