扩展示例以了解CUDA,Numba,Cupy等

问题描述

几乎所有在线提供的Numba,CuPy等示例都是简单的数组添加,显示了从CPU单核/线程到GPU的加速。而且命令文档大多缺少良好的示例。这篇文章旨在提供一个更全面的示例。

here提供了初始代码。它是经典Cellular Automata的简单模型。最初,它甚至不使用numpy,而仅使用普通python和Pyglet模块进行可视化。

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

game_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,self.get_cell_value(row + 1,col + 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 + self.cell_size)
                    pyglet.graphics.draw_indexed(4,pyglet.gl.GL_TRIANGLES,[0,1,2,3],('v2i',square_coords))

首先,我可以在此{{1}的generate_cells的末尾和此self.cells = np.asarray(self.cells)的{​​{1}}的末尾使用numpy添加,因为在此之前不会进行加速,如here所示。(实际上更改为numpy并没有明显提高速度)

例如,关于gpu,我在每个函数之前都添加了run_rules,但变得非常慢。 还尝试使用self.cells = np.asarray(temp),但这引发了一个问题:如何在仅以@jit作为输入参数的函数中使用@vectorize(['float32(float32,float32)'],target='cuda')

我也尝试用numpy代替@vectorize这样的cupy,但是也变得非常慢。

遵循扩展gpu使用示例的最初想法,解决该问题的正确方法是什么?放置修改/向量化/并行化/数字/ cupy等的正确位置在哪里?而且最重要的是为什么?

其他信息:除了提供的代码外,还有main.py文件:

self

解决方法

我不太了解您的示例,但是我只需要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)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...