1import numpy as np
2from numba import cuda
3from numba.cuda.testing import unittest, CUDATestCase
4
5
6class TestArrayAttr(CUDATestCase):
7    def test_contigous_2d(self):
8        ary = np.arange(10)
9        cary = ary.reshape(2, 5)
10        fary = np.asfortranarray(cary)
11
12        dcary = cuda.to_device(cary)
13        dfary = cuda.to_device(fary)
14        self.assertTrue(dcary.is_c_contiguous())
15        self.assertTrue(not dfary.is_c_contiguous())
16        self.assertTrue(not dcary.is_f_contiguous())
17        self.assertTrue(dfary.is_f_contiguous())
18
19    def test_contigous_3d(self):
20        ary = np.arange(20)
21        cary = ary.reshape(2, 5, 2)
22        fary = np.asfortranarray(cary)
23
24        dcary = cuda.to_device(cary)
25        dfary = cuda.to_device(fary)
26        self.assertTrue(dcary.is_c_contiguous())
27        self.assertTrue(not dfary.is_c_contiguous())
28        self.assertTrue(not dcary.is_f_contiguous())
29        self.assertTrue(dfary.is_f_contiguous())
30
31    def test_contigous_4d(self):
32        ary = np.arange(60)
33        cary = ary.reshape(2, 5, 2, 3)
34        fary = np.asfortranarray(cary)
35
36        dcary = cuda.to_device(cary)
37        dfary = cuda.to_device(fary)
38        self.assertTrue(dcary.is_c_contiguous())
39        self.assertTrue(not dfary.is_c_contiguous())
40        self.assertTrue(not dcary.is_f_contiguous())
41        self.assertTrue(dfary.is_f_contiguous())
42
43    def test_ravel_c(self):
44        ary = np.arange(60)
45        reshaped = ary.reshape(2, 5, 2, 3)
46
47        expect = reshaped.ravel(order='C')
48        dary = cuda.to_device(reshaped)
49        dflat = dary.ravel()
50        flat = dflat.copy_to_host()
51        self.assertEqual(flat.ndim, 1)
52        self.assertPreciseEqual(expect, flat)
53
54        # explicit order kwarg
55        for order in 'CA':
56            expect = reshaped.ravel(order=order)
57            dary = cuda.to_device(reshaped)
58            dflat = dary.ravel(order=order)
59            flat = dflat.copy_to_host()
60            self.assertEqual(flat.ndim, 1)
61            self.assertPreciseEqual(expect, flat)
62
63    def test_ravel_f(self):
64        ary = np.arange(60)
65        reshaped = np.asfortranarray(ary.reshape(2, 5, 2, 3))
66        for order in 'FA':
67            expect = reshaped.ravel(order=order)
68            dary = cuda.to_device(reshaped)
69            dflat = dary.ravel(order=order)
70            flat = dflat.copy_to_host()
71            self.assertEqual(flat.ndim, 1)
72            self.assertPreciseEqual(expect, flat)
73
74    def test_reshape_c(self):
75        ary = np.arange(10)
76        expect = ary.reshape(2, 5)
77        dary = cuda.to_device(ary)
78        dary_reshaped = dary.reshape(2, 5)
79        got = dary_reshaped.copy_to_host()
80        self.assertPreciseEqual(expect, got)
81
82    def test_reshape_f(self):
83        ary = np.arange(10)
84        expect = ary.reshape(2, 5, order='F')
85        dary = cuda.to_device(ary)
86        dary_reshaped = dary.reshape(2, 5, order='F')
87        got = dary_reshaped.copy_to_host()
88        self.assertPreciseEqual(expect, got)
89
90
91if __name__ == '__main__':
92    unittest.main()
93