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 25# ------------------------------------------------------------------------------- 26# Utility 27# ------------------------------------------------------------------------------- 28 29 30def check_values(ceed, x, value): 31 with x.array_read() as b: 32 for i in range(len(b)): 33 assert b[i] == value 34 35# ------------------------------------------------------------------------------- 36# Test creation, setting, reading, restoring, and destroying of a vector 37# ------------------------------------------------------------------------------- 38 39 40def test_100(ceed_resource): 41 ceed = libceed.Ceed(ceed_resource) 42 43 n = 10 44 x = ceed.Vector(n) 45 46 a = np.arange(10, 10 + n, dtype="float64") 47 x.set_array(a, cmode=libceed.USE_POINTER) 48 49 with x.array_read() as b: 50 for i in range(n): 51 assert b[i] == 10 + i 52 53# ------------------------------------------------------------------------------- 54# Test setValue 55# ------------------------------------------------------------------------------- 56 57 58def test_101(ceed_resource): 59 ceed = libceed.Ceed(ceed_resource) 60 n = 10 61 x = ceed.Vector(n) 62 value = 1 63 a = np.arange(10, 10 + n, dtype="float64") 64 x.set_array(a, cmode=libceed.USE_POINTER) 65 66 with x.array() as b: 67 for i in range(len(b)): 68 assert b[i] == 10 + i 69 70 x.set_value(3.0) 71 check_values(ceed, x, 3.0) 72 del x 73 74 x = ceed.Vector(n) 75 # Set value before setting or getting the array 76 x.set_value(5.0) 77 check_values(ceed, x, 5.0) 78 79# ------------------------------------------------------------------------------- 80# Test getArrayRead state counter 81# ------------------------------------------------------------------------------- 82 83 84def test_102(ceed_resource): 85 ceed = libceed.Ceed(ceed_resource) 86 87 n = 10 88 x = ceed.Vector(n) 89 90 # Two read accesses should not generate an error 91 a = x.get_array_read() 92 b = x.get_array_read() 93 94 x.restore_array_read() 95 x.restore_array_read() 96 97# ------------------------------------------------------------------------------- 98# Test setting one vector from array of another vector 99# ------------------------------------------------------------------------------- 100 101 102def test_103(ceed_resource): 103 ceed = libceed.Ceed(ceed_resource) 104 105 n = 10 106 107 x = ceed.Vector(n) 108 y = ceed.Vector(n) 109 110 a = np.arange(10, 10 + n, dtype="float64") 111 x.set_array(a, cmode=libceed.USE_POINTER) 112 113 with x.array() as x_array: 114 y.set_array(x_array, cmode=libceed.USE_POINTER) 115 116 with y.array_read() as y_array: 117 for i in range(n): 118 assert y_array[i] == 10 + i 119 120# ------------------------------------------------------------------------------- 121# Test getArray to modify array 122# ------------------------------------------------------------------------------- 123 124 125def test_104(ceed_resource): 126 ceed = libceed.Ceed(ceed_resource) 127 128 n = 10 129 130 x = ceed.Vector(n) 131 a = np.zeros(n, dtype="float64") 132 x.set_array(a, cmode=libceed.USE_POINTER) 133 134 with x.array() as b: 135 b[3] = -3.14 136 137 assert a[3] == -3.14 138 139# ------------------------------------------------------------------------------- 140# Test creation, setting, reading, restoring, and destroying of a vector using 141# CEED_MEM_DEVICE 142# ------------------------------------------------------------------------------- 143 144 145def test_105(ceed_resource): 146 # Skip test for non-GPU backend 147 if 'gpu' in ceed_resource: 148 ceed = libceed.Ceed(ceed_resource) 149 150 n = 10 151 x = ceed.Vector(n) 152 y = ceed.Vector(n) 153 154 a = np.arange(10, 10 + n, dtype="float64") 155 x.set_array(a, cmode=libceed.USE_POINTER) 156 157 arr = x.get_array_read(memtype=libceed.MEM_DEVICE) 158 y.set_array(arr, memtype=libceed.MEM_DEVICE) 159 x.restore_array_read() 160 161 with y.array_read() as b: 162 for i in range(n): 163 assert b[i] == 10 + i 164 165# ------------------------------------------------------------------------------- 166# Test view 167# ------------------------------------------------------------------------------- 168 169 170def test_107(ceed_resource, capsys): 171 ceed = libceed.Ceed(ceed_resource) 172 173 n = 10 174 x = ceed.Vector(n) 175 176 a = np.arange(10, 10 + n, dtype="float64") 177 x.set_array(a, cmode=libceed.USE_POINTER) 178 179 print(x) 180 181 stdout, stderr, ref_stdout = check.output(capsys) 182 assert not stderr 183 assert stdout == ref_stdout 184 185# ------------------------------------------------------------------------------- 186# Test norms 187# ------------------------------------------------------------------------------- 188 189 190def test_108(ceed_resource, capsys): 191 ceed = libceed.Ceed(ceed_resource) 192 193 n = 10 194 x = ceed.Vector(n) 195 196 a = np.arange(0, n, dtype="float64") 197 for i in range(n): 198 if (i % 2 == 0): 199 a[i] *= -1 200 x.set_array(a, cmode=libceed.USE_POINTER) 201 202 norm = x.norm(normtype=libceed.NORM_1) 203 204 assert abs(norm - 45.) < 1e-14 205 206 norm = x.norm() 207 208 assert abs(norm - np.sqrt(285.)) < 1e-14 209 210 norm = x.norm(normtype=libceed.NORM_MAX) 211 212 assert abs(norm - 9.) < 1e-14 213 214# ------------------------------------------------------------------------------- 215# Test taking the reciprocal of a vector 216# ------------------------------------------------------------------------------- 217 218 219def test_119(ceed_resource): 220 ceed = libceed.Ceed(ceed_resource) 221 222 n = 10 223 x = ceed.Vector(n) 224 225 a = np.arange(10, 10 + n, dtype="float64") 226 x.set_array(a, cmode=libceed.USE_POINTER) 227 x.reciprocal() 228 229 with x.array_read() as b: 230 for i in range(n): 231 assert abs(b[i] - 1. / (10 + i)) < 1e-15 232 233# ------------------------------------------------------------------------------- 234# Test AXPY 235# ------------------------------------------------------------------------------- 236 237 238def test_121(ceed_resource, capsys): 239 ceed = libceed.Ceed(ceed_resource) 240 241 n = 10 242 x = ceed.Vector(n) 243 y = ceed.Vector(n) 244 245 a = np.arange(10, 10 + n, dtype="float64") 246 x.set_array(a, cmode=libceed.COPY_VALUES) 247 y.set_array(a, cmode=libceed.COPY_VALUES) 248 249 y.axpy(-0.5, x) 250 with y.array() as b: 251 assert np.allclose(.5 * a, b) 252 253# ------------------------------------------------------------------------------- 254# Test pointwise multiplication 255# ------------------------------------------------------------------------------- 256 257 258def test_122(ceed_resource, capsys): 259 ceed = libceed.Ceed(ceed_resource) 260 261 n = 10 262 w = ceed.Vector(n) 263 x = ceed.Vector(n) 264 y = ceed.Vector(n) 265 266 a = np.arange(0, n, dtype="float64") 267 w.set_array(a, cmode=libceed.COPY_VALUES) 268 x.set_array(a, cmode=libceed.COPY_VALUES) 269 y.set_array(a, cmode=libceed.COPY_VALUES) 270 271 w.pointwise_mult(x, y) 272 with w.array() as b: 273 for i in range(len(b)): 274 assert abs(b[i] - i * i) < 1e-14 275 276 w.pointwise_mult(w, y) 277 with w.array() as b: 278 for i in range(len(b)): 279 assert abs(b[i] - i * i * i) < 1e-14 280 281 w.pointwise_mult(x, w) 282 with w.array() as b: 283 for i in range(len(b)): 284 assert abs(b[i] - i * i * i * i) < 1e-14 285 286 y.pointwise_mult(y, y) 287 with y.array() as b: 288 for i in range(len(b)): 289 assert abs(b[i] - i * i) < 1e-14 290 291# ------------------------------------------------------------------------------- 292# Test Scale 293# ------------------------------------------------------------------------------- 294 295 296def test_123(ceed_resource, capsys): 297 ceed = libceed.Ceed(ceed_resource) 298 299 n = 10 300 x = ceed.Vector(n) 301 302 a = np.arange(10, 10 + n, dtype="float64") 303 x.set_array(a, cmode=libceed.COPY_VALUES) 304 305 x.scale(-0.5) 306 with x.array() as b: 307 assert np.allclose(-.5 * a, b) 308 309# ------------------------------------------------------------------------------- 310# Test modification of reshaped array 311# ------------------------------------------------------------------------------- 312 313 314def test_199(ceed_resource): 315 """Modification of reshaped array""" 316 ceed = libceed.Ceed(ceed_resource) 317 318 vec = ceed.Vector(12) 319 with vec.array(4, 3) as x: 320 x[...] = np.eye(4, 3) 321 322 with vec.array_read(3, 4) as x: 323 assert np.all(x == np.eye(4, 3).reshape(3, 4)) 324 325# ------------------------------------------------------------------------------- 326