返回介绍

CUDA Python

发布于 2025-02-25 23:44:04 字数 1910 浏览 0 评论 0 收藏 0

We will mostly foucs on the use of CUDA Python via the numbapro compiler. Low level Python code using the numbapro.cuda module is similar to CUDA C, and will compile to the same machine code, but with the benefits of integerating into Python for use of numpy arrays, convenient I/O, graphics etc.

Optionally, CUDA Python can provide

  • Automatic memory transfer
    • NumPy arrays are automatically transferred
    • CPU -> GPU
    • GPU -> CPU
  • Automatic work scheduling
    • The work is distributed the across all threads on the GPU
    • The GPU hardware handles the scheduling
  • Automatic GPU memory management
    • GPU memory is tied to object lifetime
    • freed automatically

but these can be over-riden with explicit control instructions if desired. Source

Python CUDA also provides syntactic sugar for obtaining thread identity. For example,

tx = cuda.threadIdx.x
ty = cuda.threadIdx.y
bx = cuda.blockIdx.x
by = cuda.blockIdx.y
bw = cuda.blockDim.x
bh = cuda.blockDim.y
x = tx + bx * bw
y = ty + by * bh
array[x, y] = something(x, y)

can be abbreivated to

x, y = cuda.grid(2)
array[x, y] = something(x, y)

Decorators are also provided for quick GPU parallelization, and it may be sufficient to use the high-level decorators jit , autojit , vectorize and guvectorize for running functoins on the GPU. When we need fine control, we can always drop back to CUDA Python.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文