From 1d8673970c32b00178bbfe46b723f1c4ee63571e Mon Sep 17 00:00:00 2001
From: Alexandru Fikl <alexfikl@gmail.com>
Date: Tue, 19 May 2020 11:12:36 -0500
Subject: [PATCH] use logging and ctx_factory in tests

* uses logger instead of print
* uses the provided `ctx_factory` instead of `create_some_context`
---
 test/test_grudge.py            | 57 ++++++++++++++++++----------------
 test/test_mpi_communication.py | 47 ++++++++++++++--------------
 2 files changed, 55 insertions(+), 49 deletions(-)

diff --git a/test/test_grudge.py b/test/test_grudge.py
index 53bd4975..74512768 100644
--- a/test/test_grudge.py
+++ b/test/test_grudge.py
@@ -38,14 +38,17 @@ from pyopencl.tools import (  # noqa
         pytest_generate_tests_for_pyopencl as pytest_generate_tests)
 
 import logging
+
 logger = logging.getLogger(__name__)
+logging.basicConfig()
+logger.setLevel(logging.INFO)
 
 from grudge import sym, bind, DGDiscretizationWithBoundaries
 
 
 @pytest.mark.parametrize("dim", [2, 3])
 def test_inverse_metric(ctx_factory, dim):
-    cl_ctx = cl.create_some_context()
+    cl_ctx = ctx_factory()
     queue = cl.CommandQueue(cl_ctx)
 
     from meshmode.mesh.generation import generate_regular_rect_mesh
@@ -84,8 +87,8 @@ def test_inverse_metric(ctx_factory, dim):
             tgt = 1 if i == j else 0
 
             err = np.max(np.abs((mat[i, j] - tgt).get(queue=queue)))
-            print(i, j, err)
-            assert err < 1e-12, (i, j, err)
+            logger.info("error[%d, %d]: %.5e", i, j, err)
+            assert err < 1.0e-12, (i, j, err)
 
 
 @pytest.mark.parametrize("ambient_dim", [1, 2, 3])
@@ -94,7 +97,7 @@ def test_mass_mat_trig(ctx_factory, ambient_dim, quad_tag):
     """Check the integral of some trig functions on an interval using the mass
     matrix.
     """
-    cl_ctx = cl.create_some_context()
+    cl_ctx = ctx_factory()
     queue = cl.CommandQueue(cl_ctx)
 
     nelements = 17
@@ -161,7 +164,7 @@ def test_tri_diff_mat(ctx_factory, dim, order=4):
     Uses sines as the function to differentiate.
     """
 
-    cl_ctx = cl.create_some_context()
+    cl_ctx = ctx_factory()
     queue = cl.CommandQueue(cl_ctx)
 
     from meshmode.mesh.generation import generate_regular_rect_mesh
@@ -190,8 +193,7 @@ def test_tri_diff_mat(ctx_factory, dim, order=4):
             axis_eoc_recs[axis].add_data_point(1/n, linf_error)
 
     for axis, eoc_rec in enumerate(axis_eoc_recs):
-        print(axis)
-        print(eoc_rec)
+        logger.info("axis %d\n%s", axis, eoc_rec)
         assert eoc_rec.order_estimate() >= order
 
 
@@ -213,7 +215,7 @@ def test_2d_gauss_theorem(ctx_factory):
     from meshmode.mesh.io import from_meshpy
     mesh = from_meshpy(mesh_info, order=1)
 
-    cl_ctx = cl.create_some_context()
+    cl_ctx = ctx_factory()
     queue = cl.CommandQueue(cl_ctx)
 
     discr = DGDiscretizationWithBoundaries(cl_ctx, mesh, order=2)
@@ -250,7 +252,8 @@ def test_2d_gauss_theorem(ctx_factory):
 def test_convergence_advec(ctx_factory, mesh_name, mesh_pars, op_type, flux_type,
         order, visualize=False):
     """Test whether 2D advection actually converges"""
-    cl_ctx = cl.create_some_context()
+
+    cl_ctx = ctx_factory()
     queue = cl.CommandQueue(cl_ctx)
 
     from pytools.convergence import EOCRecorder
@@ -321,8 +324,6 @@ def test_convergence_advec(ctx_factory, mesh_name, mesh_pars, op_type, flux_type
                 flux_type=flux_type)
 
         bound_op = bind(discr, op.sym_operator())
-        #print(bound_op)
-        #1/0
 
         u = bind(discr, u_analytic(sym.nodes(dim)))(queue, t=0)
 
@@ -352,7 +353,7 @@ def test_convergence_advec(ctx_factory, mesh_name, mesh_pars, op_type, flux_type
         for event in dt_stepper.run(t_end=final_time):
             if isinstance(event, dt_stepper.StateComputed):
                 step += 1
-                print(event.t)
+                logger.debug("[%04d] t = %.5f", step, event.t)
 
                 last_t = event.t
                 last_u = event.state_component
@@ -364,10 +365,10 @@ def test_convergence_advec(ctx_factory, mesh_name, mesh_pars, op_type, flux_type
         error_l2 = bind(discr,
             sym.norm(2, sym.var("u")-u_analytic(sym.nodes(dim))))(
                 queue, t=last_t, u=last_u)
-        print(h_max, error_l2)
+        logger.info("h_max %.5e error %.5e", h_max, error_l2)
         eoc_rec.add_data_point(h_max, error_l2)
 
-    print(eoc_rec.pretty_print(
+    logger.info("\n%s", eoc_rec.pretty_print(
         abscissa_label="h",
         error_label="L2 Error"))
 
@@ -378,7 +379,7 @@ def test_convergence_advec(ctx_factory, mesh_name, mesh_pars, op_type, flux_type
 def test_convergence_maxwell(ctx_factory,  order):
     """Test whether 3D Maxwell's actually converges"""
 
-    cl_ctx = cl.create_some_context()
+    cl_ctx = ctx_factory()
     queue = cl.CommandQueue(cl_ctx)
 
     from pytools.convergence import EOCRecorder
@@ -419,7 +420,7 @@ def test_convergence_maxwell(ctx_factory,  order):
         from grudge.shortcuts import set_up_rk4
         dt_stepper = set_up_rk4("w", dt, fields, rhs)
 
-        print("dt=%g nsteps=%d" % (dt, nsteps))
+        logger.info("dt %.5e nsteps %5d", dt, nsteps)
 
         norm = bind(discr, sym.norm(2, sym.var("u")))
 
@@ -430,15 +431,16 @@ def test_convergence_maxwell(ctx_factory,  order):
                 esc = event.state_component
 
                 step += 1
-                print(step)
+                logger.debug("[%04d] t = %.5e", step, event.t)
 
         sol = analytic_sol(queue, mu=mu, epsilon=epsilon, t=step * dt)
         vals = [norm(queue, u=(esc[i] - sol[i])) / norm(queue, u=sol[i]) for i in range(5)] # noqa E501
         total_error = sum(vals)
         eoc_rec.add_data_point(1.0/n, total_error)
 
-    print(eoc_rec.pretty_print(abscissa_label="h",
-            error_label="L2 Error"))
+    logger.info("\n%s", eoc_rec.pretty_print(
+        abscissa_label="h",
+        error_label="L2 Error"))
 
     assert eoc_rec.order_estimate() > order
 
@@ -451,7 +453,7 @@ def test_improvement_quadrature(ctx_factory, order):
     from pytools.convergence import EOCRecorder
     from meshmode.discretization.poly_element import QuadratureSimplexGroupFactory
 
-    cl_ctx = cl.create_some_context()
+    cl_ctx = ctx_factory()
     queue = cl.CommandQueue(cl_ctx)
 
     dims = 2
@@ -467,9 +469,9 @@ def test_improvement_quadrature(ctx_factory, order):
         return sym.exp(-np.dot(sym_x, sym_x) / source_width**2)
 
     def conv_test(descr, use_quad):
-        print("-"*75)
-        print(descr)
-        print("-"*75)
+        logger.info("-" * 75)
+        logger.info(descr)
+        logger.info("-" * 75)
         eoc_rec = EOCRecorder()
 
         ns = [20, 25]
@@ -498,12 +500,15 @@ def test_improvement_quadrature(ctx_factory, order):
             total_error = norm(queue, u=esc)
             eoc_rec.add_data_point(1.0/n, total_error)
 
-        print(eoc_rec.pretty_print(abscissa_label="h", error_label="LInf Error"))
+        logger.info("\n%s", eoc_rec.pretty_print(
+            abscissa_label="h",
+            error_label="L2 Error"))
 
         return eoc_rec.order_estimate(), np.array([x[1] for x in eoc_rec.history])
 
     eoc, errs = conv_test("no quadrature", False)
     q_eoc, q_errs = conv_test("with quadrature", True)
+
     assert q_eoc > eoc
     assert (q_errs < errs).all()
     assert q_eoc > order
@@ -513,7 +518,7 @@ def test_foreign_points(ctx_factory):
     pytest.importorskip("sumpy")
     import sumpy.point_calculus as pc
 
-    cl_ctx = cl.create_some_context()
+    cl_ctx = ctx_factory()
     queue = cl.CommandQueue(cl_ctx)
 
     dim = 2
@@ -551,7 +556,7 @@ def test_op_collector_order_determinism():
 
 
 def test_bessel(ctx_factory):
-    cl_ctx = cl.create_some_context()
+    cl_ctx = ctx_factory()
     queue = cl.CommandQueue(cl_ctx)
 
     dims = 2
diff --git a/test/test_mpi_communication.py b/test/test_mpi_communication.py
index 9327bd51..f9b12d3b 100644
--- a/test/test_mpi_communication.py
+++ b/test/test_mpi_communication.py
@@ -30,7 +30,10 @@ import os
 import numpy as np
 import pyopencl as cl
 import logging
+
 logger = logging.getLogger(__name__)
+logging.basicConfig()
+logger.setLevel(logging.INFO)
 
 from grudge import sym, bind, DGDiscretizationWithBoundaries
 from grudge.shortcuts import set_up_rk4
@@ -81,16 +84,14 @@ def simple_mpi_communication_entrypoint():
             ) - (sym_all_faces_func - sym_bdry_faces_func)
             )
 
-    # print(bound_face_swap)
-    # 1/0
-
     hopefully_zero = bound_face_swap(queue, myfunc=myfunc)
     import numpy.linalg as la
     error = la.norm(hopefully_zero.get())
 
-    np.set_printoptions(threshold=100000000, suppress=True)
-    print(hopefully_zero)
-    print(error)
+    print(__file__)
+    with np.printoptions(threshold=100000000, suppress=True):
+        logger.debug(hopefully_zero)
+    logger.info("error: %.5e", error)
 
     assert error < 1e-14
 
@@ -184,10 +185,8 @@ def mpi_communication_entrypoint():
     for quantity in log_quantities.values():
         logmgr.add_quantity(quantity)
 
-    # print(sym.pretty(op.sym_operator()))
+    logger.debug("\n%s", sym.pretty(op.sym_operator()))
     bound_op = bind(vol_discr, op.sym_operator())
-    # print(bound_op)
-    # 1/0
 
     def rhs(t, w):
         val, rhs.profile_data = bound_op(queue, profile_data=rhs.profile_data,
@@ -200,7 +199,7 @@ def mpi_communication_entrypoint():
 
     final_t = 4
     nsteps = int(final_t/dt)
-    print("rank=%d dt=%g nsteps=%d" % (i_local_rank, dt, nsteps))
+    logger.info("[%04d] dt %.5e nsteps %4d", i_local_rank, dt, nsteps)
 
     # from grudge.shortcuts import make_visualizer
     # vis = make_visualizer(vol_discr, vis_order=order)
@@ -218,8 +217,10 @@ def mpi_communication_entrypoint():
             assert event.component_id == "w"
 
             step += 1
-            print(step, event.t, norm(queue, u=event.state_component[0]),
-                  time()-t_last_step)
+            logger.debug("[%04d] t = %.5e |u| = %.5e ellapsed %.5e",
+                    step, event.t,
+                    norm(queue, u=event.state_component[0]),
+                    time() - t_last_step)
 
             # if step % 10 == 0:
             #     vis.write_vtk_file("rank%d-fld-%04d.vtu" % (i_local_rank, step),
@@ -231,20 +232,20 @@ def mpi_communication_entrypoint():
     logmgr.tick_after()
 
     def print_profile_data(data):
-        print("""execute() for rank %d:
-            \tInstruction Evaluation: %f%%
-            \tFuture Evaluation: %f%%
-            \tBusy Wait: %f%%
-            \tTotal: %f seconds""" %
-            (i_local_rank,
-             data['insn_eval_time'] / data['total_time'] * 100,
-             data['future_eval_time'] / data['total_time'] * 100,
-             data['busy_wait_time'] / data['total_time'] * 100,
-             data['total_time']))
+        logger.info("""execute() for rank %d:\n
+            \tInstruction Evaluation: %g\n
+            \tFuture Evaluation: %g\n
+            \tBusy Wait: %g\n
+            \tTotal: %g seconds""",
+            i_local_rank,
+            data['insn_eval_time'] / data['total_time'] * 100,
+            data['future_eval_time'] / data['total_time'] * 100,
+            data['busy_wait_time'] / data['total_time'] * 100,
+            data['total_time'])
 
     print_profile_data(rhs.profile_data)
     logmgr.close()
-    logger.debug("Rank %d exiting", i_local_rank)
+    logger.info("Rank %d exiting", i_local_rank)
 
 
 # {{{ MPI test pytest entrypoint
-- 
GitLab