大多数在线提供的 Numba、CuPy 等示例都是简单的数组添加,显示了从 cpu 单核/线程到 GPU 的加速。并且命令文档大多缺乏好的示例。这篇文章旨在提供一个更全面的示例。
提供了初始代码here https://eraserpeel.wordpress.com/2015/05/08/cellular-automata-part-1-setting-up-the-pyglet-window/。它是经典元胞自动机的简单模型。最初,它甚至不使用 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, 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))
首先,我可以使用 numpy 在末尾添加generate_cells
this self.cells = np.asarray(self.cells)
并在结束时run_rules
this self.cells = np.asarray(temp)
,因为之前这样做不会带来加速,如所示here https://stackoverflow.com/questions/22392497/how-to-add-a-new-row-to-an-empty-numpy-array(实际上更改为 numpy 并没有带来明显的加速)
例如,关于 GPU,我添加了@jit
在每个功能之前,并且变得非常慢。
也尝试过使用@vectorize(['float32(float32, float32)'], target='cuda')
,但这提出了一个问题:如何使用@vectorize
在只有self
作为输入参数?
我也尝试用 numpy 代替 cupy,比如self.cells = cupy.asarray(self.cells)
,但也变得很慢。
按照 GPU 使用扩展示例的初步想法,解决该问题的正确方法是什么?放置修改/矢量化/并行化/numba/cupy 等的正确位置在哪里?最重要的是,为什么?
附加信息:除了提供的代码之外,这里是 main.py 文件:
import pyglet
from game_of_life import GameOfLife
class Window(pyglet.window.Window):
def __init__(self):
super().__init__(800,800)
self.gameOfLife = GameOfLife(self.get_size()[0],
self.get_size()[1],
15, # the lesser this value, more computation intensive will be
0.5)
pyglet.clock.schedule_interval(self.update, 1.0/24.0) # 24 frames per second
def on_draw(self):
self.clear()
self.gameOfLife.draw()
def update(self, dt):
self.gameOfLife.run_rules()
if __name__ == '__main__':
window = Window()
pyglet.app.run()