open-atmos / PySDM

Pythonic particle-based (super-droplet) warm-rain/aqueous-chemistry cloud microphysics package with box, parcel & 1D/2D prescribed-flow examples in Python, Julia and Matlab
https://open-atmos.github.io/PySDM/
GNU General Public License v3.0
57 stars 34 forks source link

Possible error in ThrustRTC handing of numpy float types #1102

Open bradybhalla opened 1 year ago

bradybhalla commented 1 year ago

When scaling a Storage by a numpy float type (e.g. np.float64) using the ThrustRTC backend, an exception is thrown

AttributeError: 'memoryview' object has no attribute 'ndarray'

To reproduce:

import numpy as np
from PySDM.backends import CPU, GPU
from PySDM import Particulator

particulator = Particulator(n_sd=0, backend=GPU())

storage = particulator.Storage.empty((1,), dtype=float)
storage.fill(1)
storage *= np.exp(1) # AttributeError: 'memoryview' object has no attribute 'ndarray'

In the example above, the error disappears using backend=CPU() or by converting to a float with storage *= float(np.exp(1)).

If this behavior is expected, there could be an error message explaining how to fix the exception.

@edejong-caltech

slayoo commented 1 year ago

Thank you for reporting!

One workaround is to use math.exp instead of numpy.exp:

import math
from PySDM.backends import GPU

a = GPU().Storage.empty((1,), dtype=float)
a *= math.exp(1)

This behaviour is kind of expected as the GPU backend doesn't really depend on, rely or even cater to NumPy code much. Of course, the error message is not clear.

The good news is that this has been already addressed in the Storage-refactor PR #994, where a new is_storage(multiplier) check is used instead of the hasattr(multiplier, "data") one which causes the problem (the problem originates from hasattr(np.exp(1), "data") evaluating to True).

The not so good news is that we are lacking resources to work on #994 - to make it mergable, review it and merge it :(

github-actions[bot] commented 1 year ago

Stale issue message

github-actions[bot] commented 9 months ago

Stale issue message