将C ++ / CUDA类传递给PyCUDA的SourceModule [英] Passing a C++/CUDA class to PyCUDA's SourceModule

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

问题描述

我有一个用C ++编写的类,也使用了cuda_runtime.h中的一些定义,这是来自名为ADOL-C的opensource项目的一部分,你可以看看这里

I have a class written in C++ that uses also some definitions from cuda_runtime.h, this is a part from opensource project named ADOL-C, you can have a look here!

这在我使用CUDA-C时有效,但是我想以某种方式在PyCUDA中导入这个类,如果有可能的话。所以,我将使用这个类在内核(不在'main')定义用于计算函数的导数的特定变量。有什么办法把这个类传递给PyCUDA的SourceModule?

This works when I'm using CUDA-C, but I want somehow to import this class in PyCUDA, if there is a possibility of doing that. So, I will use this class inside of kernels (not in 'main') to define specific variables that are used for computing the derivatives of a function. Is there any way for of passing this class to PyCUDA's SourceModule?

我问了一个类似的问题,但在这里我想更多地解释一下。所以,有一个解决方案编译我的C代码使用nvcc -cubin(感谢talonmies),然后导入它与driver.module_from_file(),但是,我想使用SourceModule和写这些内核的.py文件,因此,它可以更加用户友好。我的例子看起来像这样:

I asked a similar question, but here I would like to explain a bit more that that. So, there is a solution compiling my C code using nvcc -cubin (thanks to talonmies) and then importing it with driver.module_from_file(), but, I would like to use SourceModule and write those kernels inside of a .py file, so it could be more user-friendly. My example would look something like this:

from pycuda import driver, gpuarray
from pycuda.compiler import SourceModule
import pycuda.autoinit
kernel_code_template="""
__global__ void myfunction(float* inx, float* outy, float* outderiv)
{
    //defining thread index
    ...
    //declare dependent and independet variables as adoubles
    //this is a part of my question
    adtl::adouble y[3];
    adtl::adouble x[3];
    // ... 
}
"""

...这只是一个想法,但SourceModule不会知道adouble的是什么,因为它们是在类定义adoublecuda.h中定义的,所以我希望你了解我的问题现在更好。有没有人有一个线索我应该从哪里开始?如果没有,我会写这个内核在CUDA-C,并使用nvcc -cubin选项。

... this is just an idea, but SourceModule will not know what are "adouble's", because they are defined in class definition adoublecuda.h, so I hope you understand my question now better. Does anyone have a clue where should I begin? If not, I will write this kernels in CUDA-C, and use nvcc -cubin option.

感谢您的帮助!

推荐答案

PyCUDA SourceModule系统只有一种方法可以将代码传递到一个文件中,使用 nvcc 将该文件编译到cubin文件中,并且(可选)将该cubin文件加载到当前CUDA上下文中。 PyCUDA编译器模块完全不知道CUDA内核语法或代码,并且对编译的代码(几乎没有影响)[几乎限定符是因为它可以包括用户提交的代码与 externC {} 声明以停止C ++符号调整]。

The PyCUDA SourceModule system is really only a way of getting the code you pass into a file, compiling that file with nvcc into a cubin file, and (optionally) loading that cubin file into the current CUDA context. The PyCUDA compiler module knows absolutely nothing about CUDA kernel syntax or code, and has (almost) no influence on the code that is compiled [the almost qualifier is because it can bracket user submitted code with an extern "C" { } declaration to stop C++ symbol mangling].

因此,为了做我认为你问的问题, code> #include 语句为您的设备代码在提交的字符串中需要的一个头,以及一组合适的搜索路径
在通过 include_dirs 关键字选项。如果你这样做:

So to do what I think you are asking about, you should only require an #include statement for whatever headers your device code needs in the submitted string, and a suitable set of search paths in a python list passed via the include_dirs keyword option. If you do something like this:

from pycuda import driver, gpuarray 
from pycuda.compiler import SourceModule 
import pycuda.autoinit 
kernel_code_template="""

#include "adoublecuda.h" 
__global__ void myfunction(float* inx, float* outy, float* outderiv) 
{ 
    //defining thread index 
    ... 
    //declare dependent and independet variables as adoubles 
    //this is a part of my question 
    adtl::adouble y[3]; 
    adtl::adouble x[3]; 
    // ...  
}

""" 

module = SourceModule(kernel_code_template, include_dirs=['path/to/adoublecuda'])

并且它应该自动工作(注意未经测试,自行承担风险)。

and it should automagically work (note untested, use at own risk).

这篇关于将C ++ / CUDA类传递给PyCUDA的SourceModule的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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