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

发布于 2024-12-08 10:46:37 字数 490 浏览 0 评论 0原文

我了解如何从向量转换为原始指针,但我跳过了如何向后转换的节拍。

// 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

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

I understand how to go from a vector to a raw pointer but im skipping a beat on how to go backwards.

// 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

Can someone explain/point me to an example?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

美人如玉 2024-12-15 10:46:37

http://code.google.com/p/thrust/source/browse/ example/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]);

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

Thrust provides a good example for this question.

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

And getting the raw pointer from thrust containers is as answered already by yourself..

dbl2* ptrDVec = thrust::raw_pointer_cast(&d_vec[0]);
鸩远一方 2024-12-15 10:46:37

您可以像标准容器一样初始化和填充推力向量,即通过迭代器:

#include <thrust/device_vector.h>
#include <thrust/device_ptr.h>

int main()
{
  thrust::device_vector<double> v1(10);                    // create a vector of size 10
  thrust::device_ptr<double> dp = v1.data();               // or &v1[0]

  thrust::device_vector<double> v2(v1);                    // from copy
  thrust::device_vector<double> v3(dp, dp + 10);           // from iterator range
  thrust::device_vector<double> v4(v1.begin(), v1.end());  // from iterator range
}

在您的简单示例中,无需通过指针绕道,因为您可以直接复制其他容器。一般来说,如果您有一个指向数组开头的指针,并且提供了数组大小,则可以使用 v3 版本。

You initialize and populate thrust vectors just like standard containers, i.e. via iterators:

#include <thrust/device_vector.h>
#include <thrust/device_ptr.h>

int main()
{
  thrust::device_vector<double> v1(10);                    // create a vector of size 10
  thrust::device_ptr<double> dp = v1.data();               // or &v1[0]

  thrust::device_vector<double> v2(v1);                    // from copy
  thrust::device_vector<double> v3(dp, dp + 10);           // from iterator range
  thrust::device_vector<double> v4(v1.begin(), v1.end());  // from iterator range
}

In your simple example there's no need to go the detour via pointers, as you can just copy the other container directly. In general, if you have a pointer to the beginning of an array, you can use the version for v3 if you supply the array size.

不疑不惑不回忆 2024-12-15 10:46:37

dbl2* ptrDVec = 推力::raw_pointer_cast(&d_vec[0]);有没有办法从中返回 device_vector?

没有。尽管您应该能够重用初始向量变量。

dbl2* ptrDVec = thrust::raw_pointer_cast(&d_vec[0]); is there a way to get back to a device_vector from this?

There is not. Although you should be able to reuse the initial vector variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文