-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_mat.py
67 lines (52 loc) · 1.38 KB
/
benchmark_mat.py
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
import timeit
import petsc4py.PETSc as PETSc
import numpy as np
from blockarray import blockmat as bmat
COMM = PETSc.COMM_WORLD
A = PETSc.Mat().create(COMM)
A.setSizes([3, 3])
# A.setPreallocationNNZ(np.array([2, 1, 1], dtype=np.int32)[None, :])
o_nnz = np.array([2, 1, 1], dtype=np.int32)
d_nnz = np.zeros(3, dtype=np.int32)
# A.setPreallocationNNZ((o_nnz, d_nnz))
# A.setPreallocationNNZ([2, 5, 5])
# A.setPreallocationNNZ(1)
A.setType('aij')
A.setUp()
# print(A.nnz())
# print(A.getInfo())
print(A.getOwnershipRange())
for nrow in range(A.getSize()[0]):
rows = np.array([nrow], dtype=np.int32)
cols = np.array([0, 1], dtype=np.int32)
vals = np.array([1.2, 2.4])
A.setValues(rows, cols, vals)
A.assemble()
# A.assemblyBegin()
# A.assemblyEnd()
# print(A.getInfo())
B = PETSc.Mat().create(COMM)
B.setSizes([3, 2])
B.setType('aij')
B.setUp()
B.setValues([0], [0], [5])
B.assemble()
print(B[:, :])
C = PETSc.Mat().create(COMM)
C.setSizes([2, 3])
C.setUp()
C.setValues([0], [0], [5])
C.assemble()
D = PETSc.Mat().create(COMM)
D.setSizes([2, 2])
D.setUp()
D.setValues([0], [0], [2.0])
D.assemble()
MATS = [[A, B], [C, D]]
def benchmark_create_bmat():
"""Create a BlockMatrix"""
bmat.BlockMatrix(MATS, labels=(('a', 'b'), ('a', 'b')))
return None
if __name__ == '__main__':
print(globals())
print(timeit.timeit('benchmark_create_bmat()', globals=globals(), number=100))