thrust::device_pointer_cast(pnt* p)是thrust库中的一个指针类型转换函数,可将thrust::raw指针转换为thrust::device_ptr指针,其作用与thrust::raw_pointer_cast(pnt* p)刚好相反。
经过类型转换,就可以很方便的使用thrust::sort等函数,而无需将其赋值给thrust::device_vector。
#include <cuda_runtime.h>
#include <thrust/sort.h>
#include <iostream>
int main()
{
int* ptr = new int[16];
memset(ptr, 0, sizeof(int) * 16);
ptr[5] = 3;
int *d_ptr;
cudaMalloc((void**)&d_ptr, sizeof(int) * 16);
cudaMemcpy(d_ptr, ptr, sizeof(int) * 16, cudaMemcpyHostToDevice);
thrust::sort(thrust::device_pointer_cast(d_ptr), thrust::device_pointer_cast(d_ptr + 16));
cudaMemcpy(ptr, d_ptr, sizeof(int) * 16, cudaMemcpyDeviceToHost);
for (int i = 0; i < 16; ++i) {
std::cout << ptr[i] << ' ';
}
return 0;
}
用该函数即可使用thrust中排序函数,可以减轻工作量。<thrust/sort.h>中排序函数与C++中函数类似,也可自定以比较函数,如下:
struct com {
__device__ bool operator()(int a, int b) {
return a > b;
}
};
int main()
{
int* ptr = new int[16];
memset(ptr, 0, sizeof(int) * 16);
ptr[5] = 3;
int *d_ptr;
cudaMalloc((void**)&d_ptr, sizeof(int) * 16);
cudaMemcpy(d_ptr, ptr, sizeof(int) * 16, cudaMemcpyHostToDevice);
thrust::sort(thrust::device_pointer_cast(d_ptr), thrust::device_pointer_cast(d_ptr + 16), com());
cudaMemcpy(ptr, d_ptr, sizeof(int) * 16, cudaMemcpyDeviceToHost);
for (int i = 0; i < 16; ++i) {
std::cout << ptr[i] << ' ';
}
return 0;
}
即将数组逆序排列。