bh107 / bohrium

Automatic parallelization of Python/NumPy, C, and C++ codes on Linux and MacOSX
http://www.bh107.org
Apache License 2.0
220 stars 31 forks source link

Bh107 wishlist #616

Open dionhaefner opened 5 years ago

dionhaefner commented 5 years ago

This is a growing list of what I am personally missing from the new bh107 interface.

I will probably add some of these in a PR, but it is unlikely that I'll find the time to do them all.

Showstoppers

(critical features I would need to adopt bh107 in Veros)

Quality of Life

dionhaefner commented 5 years ago

I am planning to contribute an implementation leveraging __array_ufunc__ and __array_function__ for bh107. The following minimal example works:

import os
os.environ['NUMPY_EXPERIMENTAL_ARRAY_FUNCTION'] = '1'

import numpy as np

class MyArray:
    def __init__(self, array):
        self.array = array

    def __array_function__(self, func, types, args, kwargs):
        cls = self.__class__
        args = (a.array if isinstance(a, cls) else a for a in args)
        return cls(func(*args, **kwargs))

    def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
        cls = self.__class__
        inputs = (a.array if isinstance(a, cls) else a for a in inputs)
        return cls(getattr(ufunc, method)(*inputs, **kwargs))

    def __repr__(self):
        if np.isscalar(self.array):
            return repr(self.array)
        return self.__class__.__name__ + repr(self.array)[5:]

Then, you can use it as

>>> a = MyArray(np.random.rand(10))
>>> np.divide(a, np.ones(10))
MyArray([0.22199372, 0.14071593, 0.45037375, 0.2900408 , 0.28482036,
       0.1315491 , 0.65760868, 0.29693582, 0.780299  , 0.57618724])
>>> np.add.accumulate(a)
MyArray([0.22199372, 0.36270965, 0.8130834 , 1.1031242 , 1.38794456,
       1.51949366, 2.17710234, 2.47403816, 3.25433716, 3.8305244 ])
>>> np.argsort(a)
MyArray([5, 1, 0, 4, 3, 7, 2, 9, 6, 8])
>>> np.allclose(a, np.ones(100))
False

That way, we would get sane default implementations for virtually all NumPy functions without -m bohrium or import bohrium as np magic, with the possibility to implement high-performance drop-ins for everything we want to support.

madsbk commented 5 years ago

That would be great!