简体   繁体   English

PyCuda-如何在内核中使用以Python编写的函数?

[英]PyCuda - How can I use functions written in Python in the kernel?

I want to parallelize my Python code and I'm trying to use PyCuda. 我想并行化我的Python代码,并且尝试使用PyCuda。 What I saw so far is that you have to write a "Kernel" in C into your Python code. 到目前为止,我所看到的是您必须在C代码中编写一个“内核”。 This Kernel is what is going to be parallelized. 该内核将要并行化。 Am I right? 我对吗? Example (doubling an array of random numbers, from https://documen.tician.de/pycuda/tutorial.html ): 示例(从https://documen.tician.de/pycuda/tutorial.html将随机数数组加倍):

import pycuda.driver as cuda
import pycuda.autoinit
from pycuda.compiler import SourceModule
import numpy
a = numpy.random.randn(4, 4)
a = a.astype(numpy.float32)
a_gpu = cuda.mem_alloc(a.nbytes)
cuda.memcpy_htod(a_gpu, a)
# Kernel:
mod = SourceModule("""
  __global__ void doublify(float *a)
  {
    int idx = threadIdx.x + threadIdx.y*4;
    a[idx] *= 2;
  }
  """)
func = mod.get_function("doublify")
func(a_gpu, block=(4, 4, 1))
a_doubled = numpy.empty_like(a)
cuda.memcpy_dtoh(a_doubled, a_gpu)

print(a_doubled)
print(a)

The point is that my Python code has classes and other things all suitable with Python and unsuitable with C (ie untranslatable to C). 关键是我的Python代码具有所有适合Python且不适合C的类和其他内容(即不可翻译为C)。

Let me clarify: my has 256 independent for-loops that I want to parallelize. 让我澄清一下:我有256个要并行化的独立for循环。 These loops contain Python code that can't be translated to C. 这些循环包含无法转换为C的Python代码。

How can I parallelize an actual Python code with PyCuda without translating my code to C? 如何在不将我的代码转换为C的情况下将实际的Python代码与PyCuda并行化?

You can't. 你不能

PyCUDA doesn't support device side python, all device code must be written in the CUDA C dialect. PyCUDA不支持设备端python,所有设备代码都必须用CUDA C语言编写。

Numba includes a direct Python compiler which can allow an extremely limited subset of Python language features to be compiled and run directly on the GPU. Numba包含一个直接的Python编译器,该编译器可以编译极其有限的Python语言功能子集并直接在GPU上运行。 This does not include access to any Python libraries such as numpy, scipy, etc. 这不包括访问任何Python库,例如numpy,scipy等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM