将内核函数的参数传递给C ++ struct? [英] passing parameters of an kernel function as C++ struct?

查看:230
本文介绍了将内核函数的参数传递给C ++ struct?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想传递内核函数的参数作为给定的结构

  struct kernel_data {
double *一个;
double * B;
double * C;
const int * A_dims;
const int * B_dims;
int C_dims [2];
};为了这个目的,我需要用 cudaMalloc()初始化一个设备指针

但我怎么能init。这样的结构通过这个函数包括这些参数我打算通过。

解决方案

只需将结构的值传递给内核,就像任何其他参数一样:

  struct kernel_data args; 

cudaMalloc(&(args.A),sizeof(double)* .....);
cudaMalloc(&(args.B),sizeof(double)* .....);
cudaMalloc(&(args.C),sizeof(double)* .....);
cudaMalloc(&(args.A_dims),sizeof(int)* .....);
cudaMalloc(&(args.B_dims),sizeof(int)* .....);

kernel<<< ...>>>>(args);

参数列表大小有理论上的限制,从256字节到4Kb,取决于什么硬件你使用,如果你超过它,将 args 结构复制到设备分配,并将其作为指针传递,或将其复制到常量内存指针。



要从主机初始化数组,只需使用标准 cudaMemcpy 调用:

  cudaMemcpy(args.A,hostA,sizeof(double)* .....,cudaMemcpyHostToDevice); 

等。


I want to pass the parameters of kernel function as the struct given like

struct kernel_data {
    double *A;
    double *B;
    double *C;
    const int *A_dims;
    const int *B_dims;
    int C_dims[2];
};

For the purpose I need to initialize a device pointer with cudaMalloc() but how could I init. such a struct by this function including these parameters I aim to pass. Or do I have to pass them separately?

解决方案

Just pass the structure by value to the kernel as you would any other argument:

struct kernel_data args;

cudaMalloc(&(args.A), sizeof(double)*.....);
cudaMalloc(&(args.B), sizeof(double)*.....);
cudaMalloc(&(args.C), sizeof(double)*.....);
cudaMalloc(&(args.A_dims), sizeof(int)*.....);
cudaMalloc(&(args.B_dims), sizeof(int)*.....);

kernel<<<....>>>(args);

There is a theoretical limit to argument list size, anything from 256 bytes to 4Kb, depending on what hardware you use, if you ever exceed it, copy the args structure to a device allocation and pass it as a pointer, or copy it to a constant memory pointer.

To initialise the arrays from the host, just use standard cudaMemcpy calls:

cudaMemcpy(args.A, hostA, sizeof(double)*....., cudaMemcpyHostToDevice);

etc.

这篇关于将内核函数的参数传递给C ++ struct?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆