desihub / gpu_specter

Scratch work for porting spectroperfectionism extractions to GPUs
BSD 3-Clause "New" or "Revised" License
2 stars 3 forks source link

gpu - avoid cpu/gpu double implementations of same code #20

Closed lastephey closed 1 year ago

lastephey commented 4 years ago

In a perfect world it be ideal to avoid

if cpu:
    do something
if gpu:
    do something else

and instead have a nicer way (maybe a decorator) of making this more portable.

dmargala commented 4 years ago

It looks like there is support for using the numpy api directly on cupy objects which is achieved through the __array_function__ protocol: https://numpy.org/neps/nep-0018-array-function-protocol.html

For example, consider the following code:

import cupy as cp
import numpy as np

x = cp.random.normal(size=(1000, 1000))
w, v = np.linalg.eigh(x)

print(w.device)

Outputs:

<CUDA Device 0>
lastephey commented 4 years ago

I like that this could help us prevent repeated code. I dislike that it makes it harder to read/debug the code and immediately see if we're on the cpu or gpu, but that's maybe unavoidable.

lastephey commented 1 year ago

Thanks for your help. Closing in the spirit of Closember.