parfors_cache_usecases.py 1.74 KB
Newer Older
dugupeiwen's avatar
dugupeiwen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import sys

import numpy as np

from numba import njit
from numba.tests.support import TestCase


@njit(parallel=True, cache=True)
def arrayexprs_case(arr):
    return arr / arr.sum()


@njit(parallel=True, cache=True)
def prange_case(arr):
    out = np.zeros_like(arr)
    c = 1 / arr.sum()
    for i in range(arr.size):
        out[i] = arr[i] * c
    return out


@njit(cache=True)
def caller_case(arr):
    return prange_case(arrayexprs_case(arr))


class _TestModule(TestCase):
    """
    Tests for functionality of this module's functions.
    Note this does not define any "test_*" method, instead check_module()
    should be called by hand.
    """
    def check_module(self, mod):
        total_cache_hits = 0
        for fn in [mod.arrayexprs_case, mod.prange_case, mod.caller_case]:
            arr = np.ones(20)
            np.testing.assert_allclose(
                fn(arr), fn.py_func(arr),
            )
            # Accumulate cache hits
            total_cache_hits += len(fn.stats.cache_hits)
        self.assertGreater(
            total_cache_hits, 0,
            msg="At least one dispatcher has used the cache",
        )

    def run_module(self, mod):
        # This just executes the module's functionality without asserting
        # anything about the cache, it's used in tests that ensure that
        # properties such as thread count aren't baked in to the cached object.
        for fn in [mod.arrayexprs_case, mod.prange_case, mod.caller_case]:
            arr = np.ones(20)
            np.testing.assert_allclose(
                fn(arr), fn.py_func(arr),
            )


def self_test():
    mod = sys.modules[__name__]
    _TestModule().check_module(mod)


def self_run():
    mod = sys.modules[__name__]
    _TestModule().run_module(mod)