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 92 # Two read accesses should not generate an error 93 a = x.get_array_read() 94 b = x.get_array_read() 95 96 x.restore_array_read() 97 x.restore_array_read() 98 99# ------------------------------------------------------------------------------- 100# Test setting one vector from array of another vector 101# ------------------------------------------------------------------------------- 102 103 104def test_103(ceed_resource): 105 ceed = libceed.Ceed(ceed_resource) 106 107 n = 10 108 109 x = ceed.Vector(n) 110 y = ceed.Vector(n) 111 112 a = np.arange(10, 10 + n, dtype=ceed.scalar_type()) 113 x.set_array(a, cmode=libceed.USE_POINTER) 114 115 with x.array() as x_array: 116 y.set_array(x_array, cmode=libceed.USE_POINTER) 117 118 with y.array_read() as y_array: 119 for i in range(n): 120 assert y_array[i] == 10 + i 121 122# ------------------------------------------------------------------------------- 123# Test getArray to modify array 124# ------------------------------------------------------------------------------- 125 126 127def test_104(ceed_resource): 128 ceed = libceed.Ceed(ceed_resource) 129 130 n = 10 131 132 x = ceed.Vector(n) 133 a = np.zeros(n, dtype=ceed.scalar_type()) 134 x.set_array(a, cmode=libceed.USE_POINTER) 135 136 with x.array() as b: 137 b[3] = -3.14 138 139 if libceed.lib.CEED_SCALAR_TYPE == libceed.SCALAR_FP32: 140 assert a[3] == np.float32(-3.14) 141 else: 142 assert a[3] == -3.14 143 144# ------------------------------------------------------------------------------- 145# Test creation, setting, reading, restoring, and destroying of a vector using 146# CEED_MEM_DEVICE 147# ------------------------------------------------------------------------------- 148 149 150def test_105(ceed_resource): 151 # Skip test for non-GPU backend 152 if 'gpu' in ceed_resource: 153 ceed = libceed.Ceed(ceed_resource) 154 155 n = 10 156 x = ceed.Vector(n) 157 y = ceed.Vector(n) 158 159 a = np.arange(10, 10 + n, dtype=ceed.scalar_type()) 160 x.set_array(a, cmode=libceed.USE_POINTER) 161 162 arr = x.get_array_read(memtype=libceed.MEM_DEVICE) 163 y.set_array(arr, memtype=libceed.MEM_DEVICE) 164 x.restore_array_read() 165 166 with y.array_read() as b: 167 for i in range(n): 168 assert b[i] == 10 + i 169 170# ------------------------------------------------------------------------------- 171# Test view 172# ------------------------------------------------------------------------------- 173 174 175def test_107(ceed_resource, capsys): 176 ceed = libceed.Ceed(ceed_resource) 177 178 n = 10 179 x = ceed.Vector(n) 180 181 a = np.arange(10, 10 + n, dtype=ceed.scalar_type()) 182 x.set_array(a, cmode=libceed.USE_POINTER) 183 184 print(x) 185 186 stdout, stderr, ref_stdout = check.output(capsys) 187 assert not stderr 188 assert stdout == ref_stdout 189 190# ------------------------------------------------------------------------------- 191# Test norms 192# ------------------------------------------------------------------------------- 193 194 195def test_108(ceed_resource, capsys): 196 ceed = libceed.Ceed(ceed_resource) 197 198 n = 10 199 x = ceed.Vector(n) 200 201 a = np.arange(0, n, dtype=ceed.scalar_type()) 202 for i in range(n): 203 if (i % 2 == 0): 204 a[i] *= -1 205 x.set_array(a, cmode=libceed.USE_POINTER) 206 207 norm = x.norm(normtype=libceed.NORM_1) 208 209 assert abs(norm - 45.) < TOL 210 211 norm = x.norm() 212 213 assert abs(norm - np.sqrt(285.)) < TOL 214 215 norm = x.norm(normtype=libceed.NORM_MAX) 216 217 assert abs(norm - 9.) < TOL 218 219# ------------------------------------------------------------------------------- 220# Test taking the reciprocal of a vector 221# ------------------------------------------------------------------------------- 222 223 224def test_119(ceed_resource): 225 ceed = libceed.Ceed(ceed_resource) 226 227 n = 10 228 x = ceed.Vector(n) 229 230 a = np.arange(10, 10 + n, dtype=ceed.scalar_type()) 231 x.set_array(a, cmode=libceed.USE_POINTER) 232 x.reciprocal() 233 234 with x.array_read() as b: 235 for i in range(n): 236 assert abs(b[i] - 1. / (10 + i)) < TOL 237 238# ------------------------------------------------------------------------------- 239# Test AXPY 240# ------------------------------------------------------------------------------- 241 242 243def test_121(ceed_resource, capsys): 244 ceed = libceed.Ceed(ceed_resource) 245 246 n = 10 247 x = ceed.Vector(n) 248 y = ceed.Vector(n) 249 250 a = np.arange(10, 10 + n, dtype=ceed.scalar_type()) 251 x.set_array(a, cmode=libceed.COPY_VALUES) 252 y.set_array(a, cmode=libceed.COPY_VALUES) 253 254 y.axpy(-0.5, x) 255 with y.array() as b: 256 assert np.allclose(.5 * a, b) 257 258# ------------------------------------------------------------------------------- 259# Test pointwise multiplication 260# ------------------------------------------------------------------------------- 261 262 263def test_122(ceed_resource, capsys): 264 ceed = libceed.Ceed(ceed_resource) 265 266 n = 10 267 w = ceed.Vector(n) 268 x = ceed.Vector(n) 269 y = ceed.Vector(n) 270 271 a = np.arange(0, n, dtype=ceed.scalar_type()) 272 w.set_array(a, cmode=libceed.COPY_VALUES) 273 x.set_array(a, cmode=libceed.COPY_VALUES) 274 y.set_array(a, cmode=libceed.COPY_VALUES) 275 276 w.pointwise_mult(x, y) 277 with w.array() as b: 278 for i in range(len(b)): 279 assert abs(b[i] - i * i) < 1e-14 280 281 w.pointwise_mult(w, y) 282 with w.array() as b: 283 for i in range(len(b)): 284 assert abs(b[i] - i * i * i) < 1e-14 285 286 w.pointwise_mult(x, w) 287 with w.array() as b: 288 for i in range(len(b)): 289 assert abs(b[i] - i * i * i * i) < 1e-14 290 291 y.pointwise_mult(y, y) 292 with y.array() as b: 293 for i in range(len(b)): 294 assert abs(b[i] - i * i) < 1e-14 295 296# ------------------------------------------------------------------------------- 297# Test Scale 298# ------------------------------------------------------------------------------- 299 300 301def test_123(ceed_resource, capsys): 302 ceed = libceed.Ceed(ceed_resource) 303 304 n = 10 305 x = ceed.Vector(n) 306 307 a = np.arange(10, 10 + n, dtype=ceed.scalar_type()) 308 x.set_array(a, cmode=libceed.COPY_VALUES) 309 310 x.scale(-0.5) 311 with x.array() as b: 312 assert np.allclose(-.5 * a, b) 313 314# ------------------------------------------------------------------------------- 315# Test modification of reshaped array 316# ------------------------------------------------------------------------------- 317 318 319def test_199(ceed_resource): 320 """Modification of reshaped array""" 321 ceed = libceed.Ceed(ceed_resource) 322 323 vec = ceed.Vector(12) 324 with vec.array(4, 3) as x: 325 x[...] = np.eye(4, 3) 326 327 with vec.array_read(3, 4) as x: 328 assert np.all(x == np.eye(4, 3).reshape(3, 4)) 329 330# ------------------------------------------------------------------------------- 331