diff --git a/examples/clock_print.py b/examples/clock_print.py new file mode 100644 index 0000000..30a994f --- /dev/null +++ b/examples/clock_print.py @@ -0,0 +1,15 @@ +from sleep import clock, usleep + +import numba as nb + +@nb.njit(nogil=True) +def numba_clock(): + c1 = clock() + print("c1", c1) + usleep(1000000) + c2 = clock() + print("c2", c2) + print("time", c2-c1) + +if __name__ == "__main__": + numba_clock() \ No newline at end of file diff --git a/examples/example_signature.py b/examples/example_signature.py new file mode 100644 index 0000000..1c8bfb0 --- /dev/null +++ b/examples/example_signature.py @@ -0,0 +1,19 @@ +# example code to use the progressbar with an explicit signature + +from sleep import usleep +import numba as nb +from numba_progress import ProgressBar, ProgressBarType + + +@nb.njit(nb.void(nb.uint64, nb.uint64, ProgressBarType), nogil=True) +def numba_sleeper(num_iterations, sleep_us, progress_hook): + for i in range(num_iterations): + usleep(sleep_us) + progress_hook.update(1) + + +if __name__ == "__main__": + num_iterations = 30 + sleep_time_us = 250_000 + with ProgressBar(total=num_iterations, ncols=80) as numba_progress: + numba_sleeper(num_iterations, sleep_time_us, numba_progress) \ No newline at end of file diff --git a/examples/sleep.py b/examples/sleep.py index d6e1450..4730447 100644 --- a/examples/sleep.py +++ b/examples/sleep.py @@ -12,4 +12,18 @@ def usleep(usec): def usleep_impl(usec): func_usleep(usec) - return usleep_impl \ No newline at end of file + return usleep_impl + +@nb.generated_jit(nogil=True, nopython=True) +def clock(): + import ctypes + libc = ctypes.CDLL('libc.so.6') + libc.clock.argtypes = () + libc.clock.restype = ctypes.c_ulong + func_clock = libc.clock + CLOCKS_PER_SEC = 1_000_000 + + def clock_impl(): + return func_clock() / CLOCKS_PER_SEC + + return clock_impl \ No newline at end of file diff --git a/numba_progress/__init__.py b/numba_progress/__init__.py index ab25d1e..9f820c7 100644 --- a/numba_progress/__init__.py +++ b/numba_progress/__init__.py @@ -1,2 +1,2 @@ -from .progress import ProgressBar +from .progress import * from ._version import __version__ diff --git a/numba_progress/_version.py b/numba_progress/_version.py index 81f0fde..5becc17 100644 --- a/numba_progress/_version.py +++ b/numba_progress/_version.py @@ -1 +1 @@ -__version__ = "0.0.4" +__version__ = "1.0.0" diff --git a/numba_progress/progress.py b/numba_progress/progress.py index 3422ba2..ee6ddc6 100644 --- a/numba_progress/progress.py +++ b/numba_progress/progress.py @@ -13,7 +13,7 @@ from numba.core import cgutils from numba.core.boxing import unbox_array -__all__ = ['ProgressBar'] +__all__ = ['ProgressBar', 'ProgressBarType'] def is_notebook(): """Determine if we're running within an IPython kernel @@ -121,23 +121,24 @@ def __exit__(self, exc_type, exc_val, exc_tb): # Numba Native Implementation for the ProgressBar Class -class ProgressBarType(types.Type): +class ProgressBarTypeImpl(types.Type): def __init__(self): super().__init__(name='ProgressBar') -progressbar_type = ProgressBarType() +# This is the numba type representation of the ProgressBar class to be used in signatures +ProgressBarType = ProgressBarTypeImpl() @typeof_impl.register(ProgressBar) def typeof_index(val, c): - return progressbar_type + return ProgressBarType -as_numba_type.register(ProgressBar, progressbar_type) +as_numba_type.register(ProgressBar, ProgressBarType) -@register_model(ProgressBarType) +@register_model(ProgressBarTypeImpl) class ProgressBarModel(models.StructModel): def __init__(self, dmm, fe_type): members = [ @@ -147,17 +148,17 @@ def __init__(self, dmm, fe_type): # make the hook attribute accessible -make_attribute_wrapper(ProgressBarType, 'hook', 'hook') +make_attribute_wrapper(ProgressBarTypeImpl, 'hook', 'hook') -@overload_attribute(ProgressBarType, 'value') +@overload_attribute(ProgressBarTypeImpl, 'value') def get_value(progress_bar): def getter(progress_bar): return progress_bar.hook[0] return getter -@unbox(ProgressBarType) +@unbox(ProgressBarTypeImpl) def unbox_progressbar(typ, obj, c): """ Convert a ProgressBar to it's native representation (proxy object) @@ -170,18 +171,18 @@ def unbox_progressbar(typ, obj, c): return NativeValue(progress_bar._getvalue(), is_error=is_error) -@box(ProgressBarType) +@box(ProgressBarTypeImpl) def box_progressbar(typ, val, c): raise TypeError("Native representation of ProgressBar cannot be converted back to a python object " "as it contains internal python state.") -@overload_method(ProgressBarType, "update", jit_options={"nogil": True}) +@overload_method(ProgressBarTypeImpl, "update", jit_options={"nogil": True}) def _ol_update(self, n=1): """ Numpy implementation of the update method. """ - if isinstance(self, ProgressBarType): + if isinstance(self, ProgressBarTypeImpl): def _update_impl(self, n=1): atomic_add(self.hook, 0, n) return _update_impl