请问,Taichi的GPU并行支持虚函数的调用吗?若支持,是如何实现的?
Taichi可以支持虚函数的调用,可以参考我的代码:
# Python program showing
# Taichi abstract base class work
import taichi as ti
from abc import ABC, abstractmethod
ti.init(arch=ti.cuda)
@ti.data_oriented
class Polygon(ABC):
@ti.kernel
@abstractmethod
def noofsides(self):
pass
@ti.data_oriented
class Triangle(Polygon):
def __init__(self):
super().__init__()
self.pos = ti.field(ti.f32, shape=3)
self.init()
@ti.kernel
def init(self):
for i in self.pos:
self.pos[i] = i
# overriding abstract method
@ti.kernel
def noofsides(self):
for i in self.pos:
print(self.pos[i])
print("I have 3 sides")
@ti.data_oriented
class Pentagon(Polygon):
def __init__(self):
super().__init__()
self.vel = ti.field(ti.f32, shape=5)
self.init()
@ti.kernel
def init(self):
for i in self.vel:
self.vel[i] = i
# overriding abstract method
@ti.kernel
def noofsides(self):
for i in self.vel:
print(self.vel[i])
print("I have 5 sides")
# Driver code
R = Triangle()
R.noofsides()
R = Pentagon()
R.noofsides()
关于Taichi中kernel函数的并行实现,可以参考:here
3 个赞