test_barrier.py 1.94 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
67
68
69
70
71
72
73
import numpy as np

from numba import roc, float32
import unittest


class TestBarrier(unittest.TestCase):
    def test_proper_lowering(self):
        @roc.jit("void(float32[::1])")
        def twice(A):
            i = roc.get_global_id(0)
            d = A[i]
            roc.barrier(roc.CLK_LOCAL_MEM_FENCE)  # local mem fence
            A[i] = d * 2

        N = 256
        arr = np.random.random(N).astype(np.float32)
        orig = arr.copy()

        twice[2, 128](arr)

        # Assembly contains barrier instruction?
        self.assertIn("s_barrier", twice.assembly)
        # The computation is correct?
        np.testing.assert_allclose(orig * 2, arr)

    def test_no_arg_barrier_support(self):
        @roc.jit("void(float32[::1])")
        def twice(A):
            i = roc.get_global_id(0)
            d = A[i]
            # no argument defaults to global mem fence
            # which is the same for local in hsail
            roc.barrier()
            A[i] = d * 2

        N = 256
        arr = np.random.random(N).astype(np.float32)
        orig = arr.copy()

        twice[2, 128](arr)

        # Assembly contains barrier instruction?
        self.assertIn("s_barrier", twice.assembly)
        # The computation is correct?
        np.testing.assert_allclose(orig * 2, arr)

    def test_local_memory(self):
        blocksize = 10

        @roc.jit("void(float32[::1])")
        def reverse_array(A):
            sm = roc.shared.array(shape=blocksize, dtype=float32)
            i = roc.get_global_id(0)

            # preload
            sm[i] = A[i]
            # barrier
            roc.barrier(roc.CLK_LOCAL_MEM_FENCE)  # local mem fence
            # write
            A[i] += sm[blocksize - 1 - i]

        arr = np.arange(blocksize).astype(np.float32)
        orig = arr.copy()

        reverse_array[1, blocksize](arr)

        expected = orig[::-1] + orig
        np.testing.assert_allclose(expected, arr)


if __name__ == '__main__':
    unittest.main()