【问题标题】:Linear interpolation using pycuda (lerp)使用 pycuda (lerp) 进行线性插值
【发布时间】:2012-01-05 23:15:43
【问题描述】:

我是一个刚刚进入 pyCUDA 的休闲 Python 达人。我试图弄清楚如何使用 pyCUDA 实现线性插值(lerp)。 CUDA CG函数为:http://http.developer.nvidia.com/Cg/lerp.html

我的最终目标是从一组加权随机点在 pycuda 中进行双线性插值。我从未为此编写过 C 或 CUDA 编程,并且正在学习中。

这是我已经走了多远:

import pycuda.autoinit
import pycuda.driver as drv
import pycuda.compiler as comp

lerpFunction = """__global__ float lerp(float a, float b, float w)
{
    return a + w*(b-a);
}"""

mod = comp.SourceModule(lerpFunction) # This returns an error telling me a global must return a void. :(

在这方面的任何帮助都会很棒!

【问题讨论】:

  • __global__ 是做什么的?为什么你认为你需要它?
  • @MarkRansom:这是 CUDA,它是必要的 - __global__ 向 NVIDIA 编译器驱动程序表示该函数是 gpu 代码。
  • 如果你想进一步探索 Python 上的 CUDA,试试这个。 accelereyes.com/afpy.html
  • 谢谢帕万!我整个周末都在摆弄那个代码,看看我能做些什么来明智地进行插值。

标签: python cuda interpolation pycuda


【解决方案1】:

错误消息非常明确 - CUDA 内核无法返回值,它们必须声明为 void,并且可修改的参数作为指针传递。将您的 lerp 实现声明为这样的设备函数会更有意义:

__device__ float lerp(float a, float b, float w)
{
    return a + w*(b-a);
}

然后从内核内部为每个需要插值的值调用。作为有用的 CUDA 内核,您的 lerp 函数缺少很多“基础设施”。


编辑:一个非常基本的内核可能看起来像这样:

__global__ void lerp_kernel(const float *a, const float *b, const float w, float *y)
{
    int tid = threadIdx.x + blockIdx.x*blockDim.x; // unique thread number in the grid
    y[tid] = a[tid] + w*(b[tid]-a[tid]);
}

【讨论】:

  • 那么,还有更多类似的东西吗? pycuda.elementwise.ElementwiseKernel("float a,float b,float w","re​​turn a+b+w","lerp")
  • 不,仍然有同样的问题——内核函数不能返回值。这不像老式的着色器语言,内存访问是通过作为函数参数传递的指针来完成的。听起来您应该阅读一些文档或查看大量介绍性教程之一,如果您选择搜索,您选择的搜索引擎会为您找到这些教程。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-16
  • 2012-11-09
  • 2020-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多