我的机器上安装了cuda 8.0(Linux SL7),我已经下载了推力1.8.1并用新的1.8.1替换了现有的推力库。
据我所知,从推力1.8开始支持并且可以在内核中使用。我引用他们的网站:
Thrust 1.8.0引入了对CUDA算法调用的支持 __device__代码,对CUDA流的支持以及算法性能改进。用户现在可以从CUDA调用Thrust算法 __device__代码
然而,当我使用Nsight eclipse构建应用程序时,它向我显示了这个错误:
从__global中调用__host__函数(“thrust :: sort”) 函数(“mykernel”)是不允许的。
请提出任何建议?
这是我的代码:
#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;
}
答案 0 :(得分:8)
你是对的。 Thrust 1.8和更新版本支持设备代码中的算法调用。但是,要利用这一点,您需要使用新的execution policies使库在设备代码中正常工作。
如果您使用的sort版本包含如下执行政策:
__global__ void mykernel(int* a, int* b)
{
thrust::sort(thrust::device, a, a + 10);
}
你应该找到正确编译的代码。