Python 理解CUDA、Numba、Cupy等的扩展示例

Python 理解CUDA、Numba、Cupy等的扩展示例,python,gpu,numba,cupy,Python,Gpu,Numba,Cupy,大多数在线可用的Numba、CuPy等示例都是简单的阵列添加,显示了从cpu单核/线程到gpu的加速。而命令文档大多缺乏好的例子。这篇文章旨在提供一个更全面的例子 提供了初始代码。这是经典细胞自动机的一个简单模型。最初,它甚至不使用numpy,只使用普通python和Pyglet模块进行可视化 我的目标是将这段代码扩展到一个特定的问题(这将是非常大的问题),但首先我认为最好已经针对GPU的使用进行了优化 _of_life.py的游戏如下: import random as rnd import

大多数在线可用的Numba、CuPy等示例都是简单的阵列添加,显示了从cpu单核/线程到gpu的加速。而命令文档大多缺乏好的例子。这篇文章旨在提供一个更全面的例子

提供了初始代码。这是经典细胞自动机的一个简单模型。最初,它甚至不使用numpy,只使用普通python和Pyglet模块进行可视化

我的目标是将这段代码扩展到一个特定的问题(这将是非常大的问题),但首先我认为最好已经针对GPU的使用进行了优化

_of_life.py的游戏如下:

import random as rnd
import pyglet
#import numpy as np
#from numba import vectorize, cuda, jit

class GameOfLife: 
 
    def __init__(self, window_width, window_height, cell_size, percent_fill):
        self.grid_width = int(window_width / cell_size) # cell_size 
        self.grid_height = int(window_height / cell_size) # 
        self.cell_size = cell_size
        self.percent_fill = percent_fill
        self.cells = []
        self.generate_cells()
  
    def generate_cells(self):
        for row in range(0, self.grid_height): 
            self.cells.append([])
            for col in range(0, self.grid_width):
                if rnd.random() < self.percent_fill:
                    self.cells[row].append(1)
                else:
                    self.cells[row].append(0)
                
    def run_rules(self): 
        temp = []
        for row in range(0, self.grid_height):
            temp.append([])
            for col in range(0, self.grid_width):
                cell_sum = sum([self.get_cell_value(row - 1, col),
                                self.get_cell_value(row - 1, col - 1),
                                self.get_cell_value(row,     col - 1),
                                self.get_cell_value(row + 1, col - 1),
                                self.get_cell_value(row + 1, col),
                                self.get_cell_value(row + 1, col + 1),
                                self.get_cell_value(row,     col + 1),
                                self.get_cell_value(row - 1, col + 1)])
                
                if self.cells[row][col] == 0 and cell_sum == 3:
                    temp[row].append(1)
                elif self.cells[row][col] == 1 and (cell_sum == 3 or cell_sum == 2):
                    temp[row].append(1)
                else:                 
                    temp[row].append(0)
        
        self.cells = temp

    def get_cell_value(self, row, col): 
        if row >= 0 and row < self.grid_height and col >= 0 and col < self.grid_width:
           return self.cells[row][col]
        return 0

    def draw(self): 
        for row in range(0, self.grid_height):
            for col in range(0, self.grid_width):
                if self.cells[row][col] == 1:
                    #(0, 0) (0, 20) (20, 0) (20, 20)
                    square_coords = (row * self.cell_size,                  col * self.cell_size,
                                     row * self.cell_size,                  col * self.cell_size + self.cell_size,
                                     row * self.cell_size + self.cell_size, col * self.cell_size,
                                     row * self.cell_size + self.cell_size, col * self.cell_size + self.cell_size)
                    pyglet.graphics.draw_indexed(4, pyglet.gl.GL_TRIANGLES,
                                         [0, 1, 2, 1, 2, 3],
                                         ('v2i', square_coords))

我不太明白你的例子,但我只需要GPU计算。经过几天的痛苦,我可能会明白它的用法,所以我会给你看,希望能帮助你。 此外,我需要指出的是,当使用“…kernel(cuts,cuts)”时,我将放两个。因为第一个指定传入时的类型,所以它将被内核用作遍历元素,并且不能被索引读取。因此我使用第二个来计算自由索引数据

```
binsort_kernel = cp.ElementwiseKernel(
'int32 I,raw T cut,raw T ind,int32 row,int32 col,int32 q','raw T out,raw T bin,raw T num',    
'''
int i_x = i / col;                
int i_y = i % col;                
int b_f = i_x*col;                
int b_l = b_f+col;                
int n_x = i_x * q;                
int inx = i_x%row*col;            
////////////////////////////////////////////////////////////////////////////////////////
int r_x = 0; int adi = 0; int adb = 0;  
////////////////////////////////////////////////////////////////////////////////////////
if (i_y == 0)
{
for(size_t j=b_f; j<b_l; j++){
    if (cut[j]<q){                
        r_x = inx + j -b_f;       
        adb = n_x + cut[j];       
        adi = bin[adb] + num[adb];
        out[adi] = ind[r_x];      
        num[adb]+= 1;             
    }}
}
////////////////////////////////////////////////////////////////////////////////////////
''','binsort')

binsort_kernel(cuts,cuts,ind,row,col,q,iout,bins,bnum)

我对使用
cuda.jit
decorator的理解非常有限,但在我看来,这种内核性能不佳的主要原因是在CPU和GPU之间传输过多数据。为了避免这种情况,必须只传递必要的变量,特别是在谈论大型阵列时。我认为使用self作为每个函数的一个参数(可能是内核),您可能正在传递不必要的数据。另外,请记住,每个线程都在数组的单个元素上运行,因此使用
for
对数组进行迭代将不会被并行化。希望这能有所帮助。@boi,感谢您指出这一点。我3个月前开始使用Python,这是我使用类的第一种语言。我从未使用过呃,过去,这对我来说是新的东西,尽管我已经编写了10年的代码。像
self
\u init
等东西对我来说都是新的。我会更仔细地研究如何正确地传递参数。关于
for
,你知道Python是否有类似于
parfor
,比如Matlab?事实上是的,
numba.prange
m这正是您想要的,尽管我认为不可能在
numba.cuda
中并行化循环。以下是文档:。我对所有这些都很陌生:)。
```
binsort_kernel = cp.ElementwiseKernel(
'int32 I,raw T cut,raw T ind,int32 row,int32 col,int32 q','raw T out,raw T bin,raw T num',    
'''
int i_x = i / col;                
int i_y = i % col;                
int b_f = i_x*col;                
int b_l = b_f+col;                
int n_x = i_x * q;                
int inx = i_x%row*col;            
////////////////////////////////////////////////////////////////////////////////////////
int r_x = 0; int adi = 0; int adb = 0;  
////////////////////////////////////////////////////////////////////////////////////////
if (i_y == 0)
{
for(size_t j=b_f; j<b_l; j++){
    if (cut[j]<q){                
        r_x = inx + j -b_f;       
        adb = n_x + cut[j];       
        adi = bin[adb] + num[adb];
        out[adi] = ind[r_x];      
        num[adb]+= 1;             
    }}
}
////////////////////////////////////////////////////////////////////////////////////////
''','binsort')

binsort_kernel(cuts,cuts,ind,row,col,q,iout,bins,bnum)