|
| 1 | +#! /usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# vim:fenc=utf-8 |
| 4 | +# |
| 5 | +# Copyright 2020 Char Aznable <[email protected]> |
| 6 | +# Description: Utilities for pretty-printing Kokkos::View |
| 7 | +# |
| 8 | +# Distributed under terms of the 3-clause BSD license. |
| 9 | +import re |
| 10 | +import numpy as np |
| 11 | + |
| 12 | +shape = (3, 4, 5) |
| 13 | +strShape = ",".join(map(str, shape)) |
| 14 | +layout = "Kokkos::LayoutLeft" |
| 15 | +span = np.array(shape).prod() |
| 16 | +cpp = f""" |
| 17 | + #include <Kokkos_Core.hpp> |
| 18 | +
|
| 19 | + void breakpoint() {{ return; }} |
| 20 | +
|
| 21 | + int main(int argc, char* argv[]) {{ |
| 22 | + Kokkos::initialize(argc,argv); {{ |
| 23 | +
|
| 24 | + Kokkos::View<float***,{layout}> v("v",{strShape}); |
| 25 | +
|
| 26 | + Kokkos::parallel_for(v.span(), KOKKOS_LAMBDA (const int i) |
| 27 | + {{ *(v.data()+i) = i; }}); |
| 28 | +
|
| 29 | + breakpoint(); |
| 30 | +
|
| 31 | + }} Kokkos::finalize(); |
| 32 | +
|
| 33 | + }} |
| 34 | +""" |
| 35 | + |
| 36 | +def testLayout(generateCMakeProject, runGDB): |
| 37 | + p, _, fTest = generateCMakeProject |
| 38 | + fGDB = p.join("testLayout.gdbinit") |
| 39 | + r, resultStr = runGDB( |
| 40 | + """ |
| 41 | + py from GDBKokkos.printView import getKokkosViewLayout |
| 42 | + set confirm off |
| 43 | +
|
| 44 | + b breakpoint() |
| 45 | + commands |
| 46 | + silent |
| 47 | + frame 1 |
| 48 | + end |
| 49 | +
|
| 50 | + run |
| 51 | +
|
| 52 | + py v = gdb.parse_and_eval('v') |
| 53 | + py print(getKokkosViewLayout(v)) |
| 54 | + quit |
| 55 | +
|
| 56 | + """, |
| 57 | + fGDB, f"{fTest.realpath().strpath}" |
| 58 | + ) |
| 59 | + assert resultStr == layout, f"Wrong output layout: {r.stdout}" |
| 60 | + |
| 61 | + |
| 62 | +def testExtent(generateCMakeProject, runGDB): |
| 63 | + p, _, fTest = generateCMakeProject |
| 64 | + fGDB = p.join("testExtent.gdbinit") |
| 65 | + r, resultStr = runGDB( |
| 66 | + """ |
| 67 | + py from GDBKokkos.printView import getKokkosViewExtent |
| 68 | + set confirm off |
| 69 | +
|
| 70 | + b breakpoint() |
| 71 | + commands |
| 72 | + silent |
| 73 | + frame 1 |
| 74 | + end |
| 75 | +
|
| 76 | + run |
| 77 | +
|
| 78 | + py v = gdb.parse_and_eval('v') |
| 79 | + py print(getKokkosViewExtent(v)) |
| 80 | + quit |
| 81 | +
|
| 82 | + """, |
| 83 | + fGDB, f"{fTest.realpath().strpath}" |
| 84 | + ) |
| 85 | + result = np.array(re.sub(r"\D+", " ", resultStr).split(), dtype=int) |
| 86 | + assert (result == np.array(shape)).all(), f"Wrong output extent: {r.stdout}" |
| 87 | + |
| 88 | + |
| 89 | +def testStrides(generateCMakeProject, runGDB): |
| 90 | + p, _, fTest = generateCMakeProject |
| 91 | + fGDB = p.join("testExtent.gdbinit") |
| 92 | + r, resultStr = runGDB( |
| 93 | + """ |
| 94 | + py from GDBKokkos.printView import getKokkosViewStrides |
| 95 | + set confirm off |
| 96 | +
|
| 97 | + b breakpoint() |
| 98 | + commands |
| 99 | + silent |
| 100 | + frame 1 |
| 101 | + end |
| 102 | +
|
| 103 | + run |
| 104 | +
|
| 105 | + py v = gdb.parse_and_eval('v') |
| 106 | + py print(getKokkosViewStrides(v)) |
| 107 | + quit |
| 108 | +
|
| 109 | + """, |
| 110 | + fGDB, f"{fTest.realpath().strpath}" |
| 111 | + ) |
| 112 | + result = np.array(re.sub(r"\D+", " ", resultStr).split(), dtype=int) |
| 113 | + expected = np.ones_like(shape, dtype=int) |
| 114 | + expected[1:] = shape[:-1] |
| 115 | + expected = np.cumprod(expected) |
| 116 | + assert (result == expected).all(), f"Wrong output strides: {r.stdout}" |
| 117 | + |
| 118 | + |
| 119 | +def testTraits(generateCMakeProject, runGDB): |
| 120 | + p, _, fTest = generateCMakeProject |
| 121 | + fGDB = p.join("testTraits.gdbinit") |
| 122 | + r, resultStr = runGDB( |
| 123 | + """ |
| 124 | + py from GDBKokkos.printView import getKokkosViewTraits |
| 125 | + set confirm off |
| 126 | +
|
| 127 | + b breakpoint() |
| 128 | + commands |
| 129 | + silent |
| 130 | + frame 1 |
| 131 | + end |
| 132 | +
|
| 133 | + run |
| 134 | +
|
| 135 | + py v = gdb.parse_and_eval('v') |
| 136 | + py print(getKokkosViewTraits(v)) |
| 137 | + quit |
| 138 | +
|
| 139 | + """, |
| 140 | + fGDB, f"{fTest.realpath().strpath}" |
| 141 | + ) |
| 142 | + assert resultStr == f"Kokkos::ViewTraits<float***, {layout}>",\ |
| 143 | + "Wrong output traits: {r.stdout}" |
| 144 | + |
| 145 | + |
| 146 | +def testValueType(generateCMakeProject, runGDB): |
| 147 | + p, _, fTest = generateCMakeProject |
| 148 | + fGDB = p.join("testValueType.gdbinit") |
| 149 | + r, resultStr = runGDB( |
| 150 | + """ |
| 151 | + py from GDBKokkos.printView import getKokkosViewValueType |
| 152 | + set confirm off |
| 153 | +
|
| 154 | + b breakpoint() |
| 155 | + commands |
| 156 | + silent |
| 157 | + frame 1 |
| 158 | + end |
| 159 | +
|
| 160 | + run |
| 161 | +
|
| 162 | + py v = gdb.parse_and_eval('v') |
| 163 | + py print(getKokkosViewValueType(v)) |
| 164 | + quit |
| 165 | +
|
| 166 | + """, |
| 167 | + fGDB, f"{fTest.realpath().strpath}" |
| 168 | + ) |
| 169 | + assert resultStr == f"float",\ |
| 170 | + "Wrong output ValueType: {r.stdout}" |
| 171 | + |
| 172 | + |
| 173 | +def testSpan(generateCMakeProject, runGDB): |
| 174 | + p, _, fTest = generateCMakeProject |
| 175 | + fGDB = p.join("testSpan.gdbinit") |
| 176 | + r, resultStr = runGDB( |
| 177 | + """ |
| 178 | + py from GDBKokkos.printView import getKokkosViewSpan |
| 179 | + set confirm off |
| 180 | +
|
| 181 | + b breakpoint() |
| 182 | + commands |
| 183 | + silent |
| 184 | + frame 1 |
| 185 | + end |
| 186 | +
|
| 187 | + run |
| 188 | +
|
| 189 | + py v = gdb.parse_and_eval('v') |
| 190 | + py print(getKokkosViewSpan(v)) |
| 191 | + quit |
| 192 | +
|
| 193 | + """, |
| 194 | + fGDB, f"{fTest.realpath().strpath}" |
| 195 | + ) |
| 196 | + assert int(resultStr) == np.array(shape).prod(),\ |
| 197 | + "Wrong output Span: {r.stdout}" |
| 198 | + |
| 199 | + |
| 200 | +def testPrintView(generateCMakeProject, runGDB): |
| 201 | + p, _, fTest = generateCMakeProject |
| 202 | + fGDB = p.join("testPrintView.gdbinit") |
| 203 | + r, resultStr = runGDB( |
| 204 | + """ |
| 205 | + py import GDBKokkos |
| 206 | + set confirm off |
| 207 | +
|
| 208 | + b breakpoint() |
| 209 | + commands |
| 210 | + silent |
| 211 | + frame 1 |
| 212 | + end |
| 213 | +
|
| 214 | + run |
| 215 | +
|
| 216 | + printView v --noIndex |
| 217 | + quit |
| 218 | +
|
| 219 | + """, |
| 220 | + fGDB, f"{fTest.realpath().strpath}" |
| 221 | + ) |
| 222 | + |
| 223 | + result = np.array(resultStr.split()).astype(float).reshape(shape) |
| 224 | + expected = np.arange(span).astype(float).reshape(shape, order='F') |
| 225 | + assert (result == expected).all(),\ |
| 226 | + f"printView gives wrong view of shape {tuple}: {r.stdout}. Expected:\n"\ |
| 227 | + f"{expected}" |
0 commit comments