taichi icon indicating copy to clipboard operation
taichi copied to clipboard

How to parallel run the code partly on GPU and partly on CPU?

Open qmdxcube opened this issue 1 year ago • 2 comments

Hi, I wish to run all the code on GPU, however, the GPU memory is not large enough. Thus, I want to run part of the code on GPU and the left on CPU. In the code for CPU, many numpy arrays are used, so I also wish to accelerate the code using python. Is this possible? In my experience, all the code will be compiled for GPU or CPU, is it possible to compile part for CPU and part for GPU. Would you please show me an example? Thanks.

qmdxcube avatar Aug 13 '23 03:08 qmdxcube

Hi qmdxcube, Taichi currently haven't supported heterogeneous computation yet. One possible (yet stupid) way to do so is to save temporary results in a numpy array, then use ti.init() to reset the backend and pass the saved numpy array to the successive kernels.

ti.init(arch=ti.cuda)

@ti.kernel
def test(x: ti.types.ndarray()):
    ....

x = np.random.random([...])
test(x)

ti.init(arch=ti.cpu)

@ti.kernel
def test2(x: ti.types.ndarray()):
  ....

test2(x)

Something like that

jim19930609 avatar Aug 17 '23 10:08 jim19930609

Hi qmdxcube, Taichi currently haven't supported heterogeneous computation yet. One possible (yet stupid) way to do so is to save temporary results in a numpy array, then use ti.init() to reset the backend and pass the saved numpy array to the successive kernels.

ti.init(arch=ti.cuda)

@ti.kernel
def test(x: ti.types.ndarray()):
    ....

x = np.random.random([...])
test(x)

ti.init(arch=ti.cpu)

@ti.kernel
def test2(x: ti.types.ndarray()):
  ....

test2(x)

Something like that

Thanks for your kind reply!

qmdxcube avatar Aug 29 '23 13:08 qmdxcube