Skip to content

Commit 10b8849

Browse files
committed
port test_matrixgen to arraycontext
1 parent e5f1cd7 commit 10b8849

File tree

1 file changed

+81
-70
lines changed

1 file changed

+81
-70
lines changed

test/test_matrixgen.py

Lines changed: 81 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,25 @@
2020
THE SOFTWARE.
2121
"""
2222

23+
import pytest
2324
import sys
25+
2426
import numpy as np
2527
import numpy.linalg as la
2628

27-
import pyopencl as cl
28-
import pyopencl.array # noqa
29-
30-
from sumpy.tools import vector_to_device
31-
32-
import pytest
33-
from pyopencl.tools import ( # noqa
34-
pytest_generate_tests_for_pyopencl as pytest_generate_tests)
35-
29+
from arraycontext import pytest_generate_tests_for_array_contexts
30+
from sumpy.array_context import ( # noqa: F401
31+
PytestPyOpenCLArrayContextFactory, _acf)
3632

3733
import logging
3834
logger = logging.getLogger(__name__)
3935

40-
import faulthandler
41-
faulthandler.enable()
36+
pytest_generate_tests = pytest_generate_tests_for_array_contexts([
37+
PytestPyOpenCLArrayContextFactory,
38+
])
4239

4340

44-
def _build_geometry(queue, ntargets, nsources, mode, target_radius=1.0):
41+
def _build_geometry(actx, ntargets, nsources, mode, target_radius=1.0):
4542
# source points
4643
t = np.linspace(0.0, 2.0 * np.pi, nsources, endpoint=False)
4744
sources = np.array([np.cos(t), np.sin(t)])
@@ -59,14 +56,14 @@ def _build_geometry(queue, ntargets, nsources, mode, target_radius=1.0):
5956
centers = (1.0 - radius) * targets
6057
expansion_radii = np.full(ntargets, radius)
6158

62-
return (cl.array.to_device(queue, targets),
63-
cl.array.to_device(queue, sources),
64-
vector_to_device(queue, centers),
65-
cl.array.to_device(queue, expansion_radii),
66-
cl.array.to_device(queue, sigma))
59+
return (actx.from_numpy(targets),
60+
actx.from_numpy(sources),
61+
actx.from_numpy(centers),
62+
actx.from_numpy(expansion_radii),
63+
actx.from_numpy(sigma))
6764

6865

69-
def _build_subset_indices(queue, ntargets, nsources, factor):
66+
def _build_subset_indices(actx, ntargets, nsources, factor):
7067
tgtindices = np.arange(0, ntargets)
7168
srcindices = np.arange(0, nsources)
7269

@@ -82,17 +79,19 @@ def _build_subset_indices(queue, ntargets, nsources, factor):
8279

8380
tgtindices, srcindices = np.meshgrid(tgtindices, srcindices)
8481
return (
85-
cl.array.to_device(queue, tgtindices.ravel()).with_queue(None),
86-
cl.array.to_device(queue, srcindices.ravel()).with_queue(None))
82+
actx.freeze(actx.from_numpy(tgtindices.ravel())),
83+
actx.freeze(actx.from_numpy(srcindices.ravel())))
8784

8885

86+
# {{{ test_qbx_direct
87+
8988
@pytest.mark.parametrize("factor", [1.0, 0.6])
9089
@pytest.mark.parametrize("lpot_id", [1, 2])
91-
def test_qbx_direct(ctx_factory, factor, lpot_id):
92-
logging.basicConfig(level=logging.INFO)
90+
def test_qbx_direct(actx_factory, factor, lpot_id, visualize=False):
91+
if visualize:
92+
logging.basicConfig(level=logging.INFO)
9393

94-
ctx = ctx_factory()
95-
queue = cl.CommandQueue(ctx)
94+
actx = actx_factory()
9695

9796
ndim = 2
9897
order = 12
@@ -106,79 +105,88 @@ def test_qbx_direct(ctx_factory, factor, lpot_id):
106105
base_knl = LaplaceKernel(ndim)
107106
knl = DirectionalSourceDerivative(base_knl, dir_vec_name="dsource_vec")
108107
else:
109-
raise ValueError("unknow lpot_id")
108+
raise ValueError(f"unknown lpot_id: {lpot_id}")
110109

111110
from sumpy.expansion.local import LineTaylorLocalExpansion
112111
expn = LineTaylorLocalExpansion(knl, order)
113112

114113
from sumpy.qbx import LayerPotential
115-
lpot = LayerPotential(ctx, expansion=expn, source_kernels=(knl,),
114+
lpot = LayerPotential(actx.context, expansion=expn, source_kernels=(knl,),
116115
target_kernels=(base_knl,))
117116

118117
from sumpy.qbx import LayerPotentialMatrixGenerator
119-
mat_gen = LayerPotentialMatrixGenerator(ctx, expansion=expn,
120-
source_kernels=(knl,), target_kernels=(base_knl,))
118+
mat_gen = LayerPotentialMatrixGenerator(actx.context,
119+
expansion=expn,
120+
source_kernels=(knl,),
121+
target_kernels=(base_knl,))
121122

122123
from sumpy.qbx import LayerPotentialMatrixSubsetGenerator
123-
blk_gen = LayerPotentialMatrixSubsetGenerator(ctx, expansion=expn,
124-
source_kernels=(knl,), target_kernels=(base_knl,))
124+
blk_gen = LayerPotentialMatrixSubsetGenerator(actx.context,
125+
expansion=expn,
126+
source_kernels=(knl,),
127+
target_kernels=(base_knl,))
125128

126129
for n in [200, 300, 400]:
127130
targets, sources, centers, expansion_radii, sigma = \
128-
_build_geometry(queue, n, n, mode_nr, target_radius=1.2)
131+
_build_geometry(actx, n, n, mode_nr, target_radius=1.2)
129132

130133
h = 2 * np.pi / n
131134
strengths = (sigma * h,)
132-
tgtindices, srcindices = _build_subset_indices(queue,
135+
tgtindices, srcindices = _build_subset_indices(actx,
133136
ntargets=n, nsources=n, factor=factor)
134137

135138
extra_kwargs = {}
136139
if lpot_id == 2:
137140
from pytools.obj_array import make_obj_array
138-
extra_kwargs["dsource_vec"] = \
139-
vector_to_device(queue, make_obj_array(np.ones((ndim, n))))
141+
extra_kwargs["dsource_vec"] = (
142+
actx.from_numpy(make_obj_array(np.ones((ndim, n))))
143+
)
140144

141-
_, (result_lpot,) = lpot(queue,
145+
_, (result_lpot,) = lpot(actx.queue,
142146
targets=targets,
143147
sources=sources,
144148
centers=centers,
145149
expansion_radii=expansion_radii,
146150
strengths=strengths, **extra_kwargs)
147-
result_lpot = result_lpot.get()
151+
result_lpot = actx.to_numpy(result_lpot)
148152

149-
_, (mat,) = mat_gen(queue,
153+
_, (mat,) = mat_gen(actx.queue,
150154
targets=targets,
151155
sources=sources,
152156
centers=centers,
153157
expansion_radii=expansion_radii, **extra_kwargs)
154-
mat = mat.get()
155-
result_mat = mat.dot(strengths[0].get())
158+
mat = actx.to_numpy(mat)
159+
result_mat = mat @ actx.to_numpy(strengths[0])
156160

157-
_, (blk,) = blk_gen(queue,
161+
_, (blk,) = blk_gen(actx.queue,
158162
targets=targets,
159163
sources=sources,
160164
centers=centers,
161165
expansion_radii=expansion_radii,
162166
tgtindices=tgtindices,
163167
srcindices=srcindices, **extra_kwargs)
164-
blk = blk.get()
168+
blk = actx.to_numpy(blk)
165169

166-
tgtindices = tgtindices.get(queue)
167-
srcindices = srcindices.get(queue)
170+
tgtindices = actx.to_numpy(tgtindices)
171+
srcindices = actx.to_numpy(srcindices)
168172

169173
eps = 1.0e-10 * la.norm(result_lpot)
170174
assert la.norm(result_mat - result_lpot) < eps
171175
assert la.norm(blk - mat[tgtindices, srcindices]) < eps
172176

177+
# }}}
178+
179+
180+
# {{{ test_p2p_direct
173181

174182
@pytest.mark.parametrize("exclude_self", [True, False])
175183
@pytest.mark.parametrize("factor", [1.0, 0.6])
176184
@pytest.mark.parametrize("lpot_id", [1, 2])
177-
def test_p2p_direct(ctx_factory, exclude_self, factor, lpot_id):
178-
logging.basicConfig(level=logging.INFO)
185+
def test_p2p_direct(actx_factory, exclude_self, factor, lpot_id, visualize=False):
186+
if visualize:
187+
logging.basicConfig(level=logging.INFO)
179188

180-
ctx = ctx_factory()
181-
queue = cl.CommandQueue(ctx)
189+
actx = actx_factory()
182190

183191
ndim = 2
184192
mode_nr = 25
@@ -190,70 +198,73 @@ def test_p2p_direct(ctx_factory, exclude_self, factor, lpot_id):
190198
lknl = LaplaceKernel(ndim)
191199
lknl = DirectionalSourceDerivative(lknl, dir_vec_name="dsource_vec")
192200
else:
193-
raise ValueError("unknow lpot_id")
201+
raise ValueError(f"unknown lpot_id: '{lpot_id}'")
194202

195203
from sumpy.p2p import P2P
196-
lpot = P2P(ctx, [lknl], exclude_self=exclude_self)
204+
lpot = P2P(actx.context, [lknl], exclude_self=exclude_self)
197205

198206
from sumpy.p2p import P2PMatrixGenerator
199-
mat_gen = P2PMatrixGenerator(ctx, [lknl], exclude_self=exclude_self)
207+
mat_gen = P2PMatrixGenerator(actx.context, [lknl], exclude_self=exclude_self)
200208

201209
from sumpy.p2p import P2PMatrixSubsetGenerator
202-
blk_gen = P2PMatrixSubsetGenerator(ctx, [lknl], exclude_self=exclude_self)
210+
blk_gen = P2PMatrixSubsetGenerator(
211+
actx.context, [lknl], exclude_self=exclude_self)
203212

204213
for n in [200, 300, 400]:
205-
targets, sources, _, _, sigma = \
206-
_build_geometry(queue, n, n, mode_nr, target_radius=1.2)
214+
targets, sources, _, _, sigma = (
215+
_build_geometry(actx, n, n, mode_nr, target_radius=1.2))
207216

208217
h = 2 * np.pi / n
209218
strengths = (sigma * h,)
210-
tgtindices, srcindices = _build_subset_indices(queue,
219+
tgtindices, srcindices = _build_subset_indices(actx,
211220
ntargets=n, nsources=n, factor=factor)
212221

213222
extra_kwargs = {}
214223
if exclude_self:
215-
extra_kwargs["target_to_source"] = \
216-
cl.array.arange(queue, 0, n, dtype=np.int32)
224+
extra_kwargs["target_to_source"] = (
225+
actx.from_numpy(np.arange(n, dtype=np.int32))
226+
)
217227
if lpot_id == 2:
218228
from pytools.obj_array import make_obj_array
219-
extra_kwargs["dsource_vec"] = \
220-
vector_to_device(queue, make_obj_array(np.ones((ndim, n))))
229+
extra_kwargs["dsource_vec"] = (
230+
actx.from_numpy(make_obj_array(np.ones((ndim, n)))))
221231

222-
_, (result_lpot,) = lpot(queue,
232+
_, (result_lpot,) = lpot(actx.queue,
223233
targets=targets,
224234
sources=sources,
225235
strength=strengths, **extra_kwargs)
226-
result_lpot = result_lpot.get()
236+
result_lpot = actx.to_numpy(result_lpot)
227237

228-
_, (mat,) = mat_gen(queue,
238+
_, (mat,) = mat_gen(actx.queue,
229239
targets=targets,
230240
sources=sources, **extra_kwargs)
231-
mat = mat.get()
232-
result_mat = mat.dot(strengths[0].get())
241+
mat = actx.to_numpy(mat)
242+
result_mat = mat @ actx.to_numpy(strengths[0])
233243

234-
_, (blk,) = blk_gen(queue,
244+
_, (blk,) = blk_gen(actx.queue,
235245
targets=targets,
236246
sources=sources,
237247
tgtindices=tgtindices,
238248
srcindices=srcindices, **extra_kwargs)
239-
blk = blk.get()
249+
blk = actx.to_numpy(blk)
240250

241-
tgtindices = tgtindices.get(queue)
242-
srcindices = srcindices.get(queue)
251+
tgtindices = actx.to_numpy(tgtindices)
252+
srcindices = actx.to_numpy(srcindices)
243253

244254
eps = 1.0e-10 * la.norm(result_lpot)
245255
assert la.norm(result_mat - result_lpot) < eps
246256
assert la.norm(blk - mat[tgtindices, srcindices]) < eps
247257

258+
# }}}
259+
248260

249261
# You can test individual routines by typing
250-
# $ python test_kernels.py "test_p2p(cl.create_some_context)"
262+
# $ python test_matrixgen.py 'test_p2p_direct(_acf, True, 1.0, 1, visualize=True)'
251263

252264
if __name__ == "__main__":
253265
if len(sys.argv) > 1:
254266
exec(sys.argv[1])
255267
else:
256-
from pytest import main
257-
main([__file__])
268+
pytest.main([__file__])
258269

259270
# vim: fdm=marker

0 commit comments

Comments
 (0)