xref: /libCEED/python/tests/test-1-vector.py (revision b19271b6eb0791edc605fd8a4a305de5b22bda53)
1# Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
2# the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
3# reserved. See files LICENSE and NOTICE for details.
4#
5# This file is part of CEED, a collection of benchmarks, miniapps, software
6# libraries and APIs for efficient high-order finite element and spectral
7# element discretizations for exascale applications. For more information and
8# source code availability see http://github.com/ceed.
9#
10# The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11# a collaborative effort of two U.S. Department of Energy organizations (Office
12# of Science and the National Nuclear Security Administration) responsible for
13# the planning and preparation of a capable exascale ecosystem, including
14# software, applications, hardware, advanced system engineering and early
15# testbed platforms, in support of the nation's exascale computing imperative.
16
17# @file
18# Test Ceed Vector functionality
19
20import os
21import libceed
22import numpy as np
23import check
24
25TOL = libceed.EPSILON * 256
26
27# -------------------------------------------------------------------------------
28# Utility
29# -------------------------------------------------------------------------------
30
31
32def check_values(ceed, x, value):
33    with x.array_read() as b:
34        for i in range(len(b)):
35            assert b[i] == value
36
37# -------------------------------------------------------------------------------
38# Test creation, setting, reading, restoring, and destroying of a vector
39# -------------------------------------------------------------------------------
40
41
42def test_100(ceed_resource):
43    ceed = libceed.Ceed(ceed_resource)
44
45    n = 10
46    x = ceed.Vector(n)
47
48    a = np.arange(10, 10 + n, dtype=ceed.scalar_type())
49    x.set_array(a, cmode=libceed.USE_POINTER)
50
51    with x.array_read() as b:
52        for i in range(n):
53            assert b[i] == 10 + i
54
55# -------------------------------------------------------------------------------
56# Test setValue
57# -------------------------------------------------------------------------------
58
59
60def test_101(ceed_resource):
61    ceed = libceed.Ceed(ceed_resource)
62    n = 10
63    x = ceed.Vector(n)
64    value = 1
65    a = np.arange(10, 10 + n, dtype=ceed.scalar_type())
66    x.set_array(a, cmode=libceed.USE_POINTER)
67
68    with x.array() as b:
69        for i in range(len(b)):
70            assert b[i] == 10 + i
71
72    x.set_value(3.0)
73    check_values(ceed, x, 3.0)
74    del x
75
76    x = ceed.Vector(n)
77    # Set value before setting or getting the array
78    x.set_value(5.0)
79    check_values(ceed, x, 5.0)
80
81# -------------------------------------------------------------------------------
82# Test getArrayRead state counter
83# -------------------------------------------------------------------------------
84
85
86def test_102(ceed_resource):
87    ceed = libceed.Ceed(ceed_resource)
88
89    n = 10
90    x = ceed.Vector(n)
91    x.set_value(0)
92
93    # Two read accesses should not generate an error
94    a = x.get_array_read()
95    b = x.get_array_read()
96
97    x.restore_array_read()
98    x.restore_array_read()
99
100# -------------------------------------------------------------------------------
101# Test setting one vector from array of another vector
102# -------------------------------------------------------------------------------
103
104
105def test_103(ceed_resource):
106    ceed = libceed.Ceed(ceed_resource)
107
108    n = 10
109
110    x = ceed.Vector(n)
111    y = ceed.Vector(n)
112
113    a = np.arange(10, 10 + n, dtype=ceed.scalar_type())
114    x.set_array(a, cmode=libceed.USE_POINTER)
115
116    with x.array() as x_array:
117        y.set_array(x_array, cmode=libceed.USE_POINTER)
118
119    with y.array_read() as y_array:
120        for i in range(n):
121            assert y_array[i] == 10 + i
122
123# -------------------------------------------------------------------------------
124# Test getArray to modify array
125# -------------------------------------------------------------------------------
126
127
128def test_104(ceed_resource):
129    ceed = libceed.Ceed(ceed_resource)
130
131    n = 10
132
133    x = ceed.Vector(n)
134    a = np.zeros(n, dtype=ceed.scalar_type())
135    x.set_array(a, cmode=libceed.USE_POINTER)
136
137    with x.array() as b:
138        b[3] = -3.14
139
140    if libceed.lib.CEED_SCALAR_TYPE == libceed.SCALAR_FP32:
141        assert a[3] == np.float32(-3.14)
142    else:
143        assert a[3] == -3.14
144
145# -------------------------------------------------------------------------------
146# Test creation, setting, reading, restoring, and destroying of a vector using
147#   CEED_MEM_DEVICE
148# -------------------------------------------------------------------------------
149
150
151def test_105(ceed_resource):
152    # Skip test for non-GPU backend
153    if 'gpu' in ceed_resource:
154        ceed = libceed.Ceed(ceed_resource)
155
156        n = 10
157        x = ceed.Vector(n)
158        y = ceed.Vector(n)
159
160        a = np.arange(10, 10 + n, dtype=ceed.scalar_type())
161        x.set_array(a, cmode=libceed.USE_POINTER)
162
163        arr = x.get_array_read(memtype=libceed.MEM_DEVICE)
164        y.set_array(arr, memtype=libceed.MEM_DEVICE)
165        x.restore_array_read()
166
167        with y.array_read() as b:
168            for i in range(n):
169                assert b[i] == 10 + i
170
171# -------------------------------------------------------------------------------
172# Test view
173# -------------------------------------------------------------------------------
174
175
176def test_107(ceed_resource, capsys):
177    ceed = libceed.Ceed(ceed_resource)
178
179    n = 10
180    x = ceed.Vector(n)
181
182    a = np.arange(10, 10 + n, dtype=ceed.scalar_type())
183    x.set_array(a, cmode=libceed.USE_POINTER)
184
185    print(x)
186
187    stdout, stderr, ref_stdout = check.output(capsys)
188    assert not stderr
189    assert stdout == ref_stdout
190
191# -------------------------------------------------------------------------------
192# Test norms
193# -------------------------------------------------------------------------------
194
195
196def test_108(ceed_resource, capsys):
197    ceed = libceed.Ceed(ceed_resource)
198
199    n = 10
200    x = ceed.Vector(n)
201
202    a = np.arange(0, n, dtype=ceed.scalar_type())
203    for i in range(n):
204        if (i % 2 == 0):
205            a[i] *= -1
206    x.set_array(a, cmode=libceed.USE_POINTER)
207
208    norm = x.norm(normtype=libceed.NORM_1)
209
210    assert abs(norm - 45.) < TOL
211
212    norm = x.norm()
213
214    assert abs(norm - np.sqrt(285.)) < TOL
215
216    norm = x.norm(normtype=libceed.NORM_MAX)
217
218    assert abs(norm - 9.) < TOL
219
220# -------------------------------------------------------------------------------
221# Test taking the reciprocal of a vector
222# -------------------------------------------------------------------------------
223
224
225def test_119(ceed_resource):
226    ceed = libceed.Ceed(ceed_resource)
227
228    n = 10
229    x = ceed.Vector(n)
230
231    a = np.arange(10, 10 + n, dtype=ceed.scalar_type())
232    x.set_array(a, cmode=libceed.USE_POINTER)
233    x.reciprocal()
234
235    with x.array_read() as b:
236        for i in range(n):
237            assert abs(b[i] - 1. / (10 + i)) < TOL
238
239# -------------------------------------------------------------------------------
240# Test AXPY
241# -------------------------------------------------------------------------------
242
243
244def test_121(ceed_resource, capsys):
245    ceed = libceed.Ceed(ceed_resource)
246
247    n = 10
248    x = ceed.Vector(n)
249    y = ceed.Vector(n)
250
251    a = np.arange(10, 10 + n, dtype=ceed.scalar_type())
252    x.set_array(a, cmode=libceed.COPY_VALUES)
253    y.set_array(a, cmode=libceed.COPY_VALUES)
254
255    y.axpy(-0.5, x)
256    with y.array() as b:
257        assert np.allclose(.5 * a, b)
258
259# -------------------------------------------------------------------------------
260# Test pointwise multiplication
261# -------------------------------------------------------------------------------
262
263
264def test_122(ceed_resource, capsys):
265    ceed = libceed.Ceed(ceed_resource)
266
267    n = 10
268    w = ceed.Vector(n)
269    x = ceed.Vector(n)
270    y = ceed.Vector(n)
271
272    a = np.arange(0, n, dtype=ceed.scalar_type())
273    w.set_array(a, cmode=libceed.COPY_VALUES)
274    x.set_array(a, cmode=libceed.COPY_VALUES)
275    y.set_array(a, cmode=libceed.COPY_VALUES)
276
277    w.pointwise_mult(x, y)
278    with w.array() as b:
279        for i in range(len(b)):
280            assert abs(b[i] - i * i) < 1e-14
281
282    w.pointwise_mult(w, y)
283    with w.array() as b:
284        for i in range(len(b)):
285            assert abs(b[i] - i * i * i) < 1e-14
286
287    w.pointwise_mult(x, w)
288    with w.array() as b:
289        for i in range(len(b)):
290            assert abs(b[i] - i * i * i * i) < 1e-14
291
292    y.pointwise_mult(y, y)
293    with y.array() as b:
294        for i in range(len(b)):
295            assert abs(b[i] - i * i) < 1e-14
296
297# -------------------------------------------------------------------------------
298# Test Scale
299# -------------------------------------------------------------------------------
300
301
302def test_123(ceed_resource, capsys):
303    ceed = libceed.Ceed(ceed_resource)
304
305    n = 10
306    x = ceed.Vector(n)
307
308    a = np.arange(10, 10 + n, dtype=ceed.scalar_type())
309    x.set_array(a, cmode=libceed.COPY_VALUES)
310
311    x.scale(-0.5)
312    with x.array() as b:
313        assert np.allclose(-.5 * a, b)
314
315# -------------------------------------------------------------------------------
316# Test getArrayWrite to modify array
317# -------------------------------------------------------------------------------
318
319
320def test_124(ceed_resource):
321    ceed = libceed.Ceed(ceed_resource)
322
323    n = 10
324
325    x = ceed.Vector(n)
326
327    with x.array_write() as a:
328        for i in range(len(a)):
329            a[i] = 3 * i
330
331    with x.array_read() as a:
332        for i in range(len(a)):
333            assert a[i] == 3 * i
334
335# -------------------------------------------------------------------------------
336# Test modification of reshaped array
337# -------------------------------------------------------------------------------
338
339
340def test_199(ceed_resource):
341    """Modification of reshaped array"""
342    ceed = libceed.Ceed(ceed_resource)
343
344    vec = ceed.Vector(12)
345    vec.set_value(0.0)
346    with vec.array(4, 3) as x:
347        x[...] = np.eye(4, 3)
348
349    with vec.array_read(3, 4) as x:
350        assert np.all(x == np.eye(4, 3).reshape(3, 4))
351
352# -------------------------------------------------------------------------------
353