CUDA学习日记(一):thrust::device_pointer_cast(pnt* p)

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;
}

即将数组逆序排列。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值