File: test_device.py

package info (click to toggle)
petsc4py 3.24.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,612 kB
  • sloc: python: 13,569; ansic: 1,768; makefile: 345; f90: 313; sh: 14
file content (115 lines) | stat: -rw-r--r-- 3,493 bytes parent folder | download | duplicates (2)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from petsc4py import PETSc
import unittest

# --------------------------------------------------------------------


class TestDevice(unittest.TestCase):
    def testCurrent(self):
        dctx = PETSc.DeviceContext().getCurrent()
        if not dctx:
            return
        self.assertEqual(dctx.getRefCount(), 2)
        device = dctx.getDevice()
        del device
        del dctx
        dctx = PETSc.DeviceContext().getCurrent()
        self.assertEqual(dctx.getRefCount(), 2)
        device = dctx.getDevice()
        del device
        del dctx

    def testDevice(self):
        device = PETSc.Device.create()
        device.configure()
        _ = device.getDeviceType()
        _ = device.getDeviceId()
        del device

    def testDeviceContext(self):
        dctx = PETSc.DeviceContext().create()
        if not dctx:
            return
        self.assertEqual(dctx.getRefCount(), 1)
        dctx.setUp()
        self.assertTrue(dctx.idle())
        dctx.destroy()
        self.assertEqual(dctx.getRefCount(), 0)

    def testStream(self):
        dctx = PETSc.DeviceContext().getCurrent()
        if not dctx:
            return
        self.assertEqual(dctx.getRefCount(), 2)
        stype = dctx.getStreamType()
        dctx.setStreamType(stype)
        dctx.destroy()
        self.assertEqual(dctx.getRefCount(), 0)

    def testSetFromOptions(self):
        dctx = PETSc.DeviceContext().create()
        if not dctx:
            return
        self.assertEqual(dctx.getRefCount(), 1)
        dctx.setFromOptions()
        dctx.setUp()
        dctx.destroy()
        self.assertEqual(dctx.getRefCount(), 0)

    def testDuplicate(self):
        dctx = PETSc.DeviceContext().getCurrent()
        if not dctx:
            return
        self.assertEqual(dctx.getRefCount(), 2)
        dctx2 = dctx.duplicate()
        self.assertEqual(dctx2.getRefCount(), 1)
        dctx.destroy()
        self.assertEqual(dctx.getRefCount(), 0)
        dctx2.destroy()
        self.assertEqual(dctx2.getRefCount(), 0)

    def testWaitFor(self):
        dctx = PETSc.DeviceContext().create()
        if not dctx:
            return
        self.assertEqual(dctx.getRefCount(), 1)
        dctx.setUp()
        dctx2 = PETSc.DeviceContext().create()
        self.assertEqual(dctx2.getRefCount(), 1)
        dctx2.setUp()
        dctx.waitFor(dctx2)
        dctx.destroy()
        self.assertEqual(dctx.getRefCount(), 0)
        dctx2.destroy()
        dctx2.destroy()
        self.assertEqual(dctx2.getRefCount(), 0)

    def testForkJoin(self):
        dctx = PETSc.DeviceContext().getCurrent()
        if not dctx:
            return
        self.assertEqual(dctx.getRefCount(), 2)
        jdestroy = PETSc.DeviceContext.JoinMode.DESTROY
        jtypes = [
            PETSc.DeviceContext.JoinMode.SYNC,
            PETSc.DeviceContext.JoinMode.NO_SYNC,
        ]
        for j in jtypes:
            dctxs = dctx.fork(4)
            for ctx in dctxs:
                self.assertEqual(ctx.getRefCount(), 1)
            dctx.join(j, dctxs[0::2])
            dctx.join(j, dctxs[3::-2])
            for ctx in dctxs:
                self.assertEqual(ctx.getRefCount(), 1)
            dctx.join(jdestroy, dctxs)
            for ctx in dctxs:
                self.assertEqual(ctx.getRefCount(), 0)
        dctx.destroy()
        self.assertEqual(dctx.getRefCount(), 0)


# --------------------------------------------------------------------

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