我也在我的机器上安装了cuda 8.0(Linux SL7),我已经下载了推力1.8.1并用新的1.8.1替换了现有的推力库.
据我所知,从推力1.8开始支持并可以在内核中使用.我引用他们的网站:
Thrust 1.8.0引入了对CUDA __device__代码的算法调用的支持,对CUDA流的支持以及算法性能的改进.用户现在可以从CUDA __device__代码调用Thrust算法
但是,当我使用Nsight eclipse构建应用程序时,它会向我显示以下错误:
不允许从__global__函数("mykernel")调用__host__函数("thrust :: sort").
请问有什么建议吗?
这是我的代码:
#include <iostream>
#include <numeric>
#include <stdlib.h>
#include <stdio.h>
#include <cuda_runtime.h>
#include <cuda.h>
#include <thrust/sort.h>
#include <thrust/execution_policy.h>
__global__ void mykernel(int* a, int* b)
{
thrust::sort(a, a + 10);
}
int main(void)
{
int a[10] = { 0, 9, 7, 3, 1, 6, 4, 5, 2, 8 };
int b[10];
int *d_a, *d_c;
cudaMalloc((void**)&d_a, 10 * sizeof(int));
cudaMalloc((void**)&d_c, 10 * sizeof(int));
std::cout << "A\n";
for (int i = 0; i < 10; ++i) {
std::cout << a[i] << " ";
}
cudaMemcpy(d_a, a, 10 * sizeof(int), cudaMemcpyHostToDevice);
mykernel<<<1, 1> > >(d_a, d_c);
cudaMemcpy(a, d_c, 10 * sizeof(int), cudaMemcpyDeviceToHost);
std::cout << "\nA\n";
for (int i = 0; i < 10; ++i) {
std::cout << a[i] << " ";
}
cudaFree(d_a);
cudaFree(d_c);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
你是对的.Thrust 1.8和更新版本支持设备代码中的算法调用.但是,要利用此功能,您需要使用新的执行策略使库在设备代码中正常工作.
如果您使用包含执行策略的sort版本,如下所示:
__global__ void mykernel(int* a, int* b)
{
thrust::sort(thrust::device, a, a + 10);
}
Run Code Online (Sandbox Code Playgroud)
你应该找到正确编译的代码.