如何使用Dask在GPU上运行python代码? [英] How to use Dask to run python code on the GPU?

查看:416
本文介绍了如何使用Dask在GPU上运行python代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些使用Numba cuda.jit的代码,以便我可以在gpu上运行,如果可能的话,我想在其上分层.

I have some code that uses Numba cuda.jit in order for me to run on the gpu, and I would like to layer dask on top of it if possible.

示例代码

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from numba import cuda, njit
import numpy as np
from dask.distributed import Client, LocalCluster


@cuda.jit()
def addingNumbersCUDA (big_array, big_array2, save_array):
    i = cuda.grid(1)
    if i < big_array.shape[0]:
        for j in range (big_array.shape[1]):
            save_array[i][j] = big_array[i][j] * big_array2[i][j]


if __name__ == "__main__":
    cluster = LocalCluster()
    client = Client(cluster)

    big_array = np.random.random_sample((100, 3000))
    big_array2  = np.random.random_sample((100, 3000))
    save_array = np.zeros(shape=(100, 3000))

    arraysize = 100
    threadsperblock = 64
    blockspergrid = (arraysize + (threadsperblock - 1))

    d_big_array = cuda.to_device(big_array)
    d_big_array2 = cuda.to_device(big_array2)
    d_save_array = cuda.to_device(save_array)

    addingNumbersCUDA[blockspergrid, threadsperblock](d_big_array, d_big_array2, d_save_array)

    save_array = d_save_array.copy_to_host()

如果我的函数addingNumbersCUDA不使用任何CUDA,我只需将client.submit放在我的函数前面(以及在集合之后),它将起作用.但是,由于我使用的是CUDA,因此无法将提交放在函数的前面.简短的文档说,您可以针对gpu,但是尚不清楚如何在实践中进行实际设置.我该如何设置我的函数以在目标gpu和cuda.jit上使用dask?

If my function addingNumbersCUDA didn't use any CUDA I would just put client.submit in front of my function (along with gather after) and it would work. But, since I'm using CUDA putting submit in front of the function doesn't work. The dask documentation says that you can target the gpu, but it's unclear as to how to actually set it up in practice. How would I set up my function to use dask with the gpu targeted and with cuda.jit if possible?

推荐答案

您可能希望浏览

但是,由于我使用的是CUDA,因此无法将提交放在函数的前面.

But, since I'm using CUDA putting submit in front of the function doesn't work.

没有这种特殊情况的原因.所有Dask都会在另一台计算机上运行您的功能.它不会以任何方式更改或修改您的功能.

There is no particular reason why this should be the case. All Dask does it run your function on a different computer. It doesn't change or modify your function in any way.

这篇关于如何使用Dask在GPU上运行python代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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