将 Typename1 的 Cuda Array1 转换为 Typename2 的 Array2

发布于 2024-10-06 09:41:20 字数 419 浏览 1 评论 0原文

亲爱的 Cuda 学者,
寻找以下问题的解决方案

a)我有两个数组 1) size1 的 array1,其类型为 name1 2) array2 of size1 is of typename2

b) 我想编写以下原型的内核

__global__ 内核(void* dest, void* src, int dest_sizeoftype, int src_sizeoftype, int num_array_elts);

c) 假设我创建 num_array_elts 个 cuda 线程,每个线程将其 elt 从 src 复制到目标。

问题: a) 我遇到困难的地方是使用哪个函数将 num_bytes 从 src 复制到内核中的 dest 。

预先感谢您 问候, 纳加拉朱

Dear Cuda Scholars,
Looking for solution for the below problem

a) I have two arrays
1) array1 of size1 which is of typename1
2) array2 of size1 which is of typename2

b) I am wanting to write a kernel of the following prototype


__global__ kernel(void* dest, void* src, int dest_sizeoftype, int src_sizeoftype, int num_array_elts);

c) Supposing I create num_array_elts cuda threads, each threads copying its elt to from src to destination.

Issue:
a) The place I am getting stuck is which function to use to copy num_bytes from src to dest in the kernel.

Thanking you in advance
Regards,
Nagaraju

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

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

发布评论

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

评论(2

网白 2024-10-13 09:41:20

Thrust 中的复制算法使这一切变得简单。

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

int   * src = ...
float * dst = ...

// first wrap the 'raw' pointers
thrust::device_ptr<int>   wrapped_src(src);
thrust::device_ptr<float> wrapped_dst(dst);

// then pass wrapped pointers to copy()
thrust::copy(wrapped_src, wrapped_src + num_array_elts, wrapped_dst);

有关 Thrust 的更多信息,请参阅快速入门指南。

The copy algorithm in Thrust makes this easy.

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

int   * src = ...
float * dst = ...

// first wrap the 'raw' pointers
thrust::device_ptr<int>   wrapped_src(src);
thrust::device_ptr<float> wrapped_dst(dst);

// then pass wrapped pointers to copy()
thrust::copy(wrapped_src, wrapped_src + num_array_elts, wrapped_dst);

Refer to the QuickStart guide for additional info about Thrust.

2024-10-13 09:41:20

如果您知道两个数组的类型,这个问题就变得相当简单。

__global__ kernel(float* dest, int* src){

    int idx=blockIdx.x*blockDim.x+threadIdx.x;
    dest[ idx ] = src[ idx ];

}

如果您的目标数组使用更大的字,例如双精度数,这仍然有效,并且不需要知道字节数。只需确保在使用 cudamalloc

If you know the types of the 2 arrays this problem becomes fairly trivial.

__global__ kernel(float* dest, int* src){

    int idx=blockIdx.x*blockDim.x+threadIdx.x;
    dest[ idx ] = src[ idx ];

}

If your dest array used a larger word e.g. a double, this would still work and there would be no need to know the number of bytes. Just make sure you allocate the correct number of bytes when using cudamalloc.

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