gpt4 book ai didi

c++ - 从 Thrust::device_vector 到原始指针并返回?

转载 作者:行者123 更新时间:2023-12-03 01:14:00 27 4
gpt4 key购买 nike

我了解如何从 vector 到原始指针,但我跳过了如何向后的节拍。

// our host vector
thrust::host_vector<dbl2> hVec;

// pretend we put data in it here

// get a device_vector
thrust::device_vector<dbl2> dVec = hVec;

// get the device ptr
thrust::device_ptr devPtr = &d_vec[0];

// now how do i get back to device_vector?
thrust::device_vector<dbl2> dVec2 = devPtr; // gives error
thrust::device_vector<dbl2> dVec2(devPtr); // gives error

有人可以解释/给我举个例子吗?

最佳答案

http://code.google.com/p/thrust/source/browse/examples/cuda/wrap_pointer.cu

Thrust 为这个问题提供了一个很好的例子。

#include <thrust/device_ptr.h>
#include <thrust/fill.h>
#include <cuda.h>

int main(void)
{
size_t N = 10;

// obtain raw pointer to device memory
int * raw_ptr;
cudaMalloc((void **) &raw_ptr, N * sizeof(int));

// wrap raw pointer with a device_ptr
thrust::device_ptr<int> dev_ptr = thrust::device_pointer_cast(raw_ptr);

// use device_ptr in Thrust algorithms
thrust::fill(dev_ptr, dev_ptr + N, (int) 0);

// access device memory transparently through device_ptr
dev_ptr[0] = 1;

// free memory
cudaFree(raw_ptr);

return 0;
}

从推力容器获取原始指针已经由您自己回答了..

dbl2* ptrDVec = thrust::raw_pointer_cast(&d_vec[0]);

关于c++ - 从 Thrust::device_vector 到原始指针并返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7678995/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com