Skip to content
test_tree.py 19.4 KiB
Newer Older
Andreas Klöckner's avatar
Andreas Klöckner committed
from __future__ import division
Andreas Klöckner's avatar
Andreas Klöckner committed
from __future__ import absolute_import
from __future__ import print_function
import six
from six.moves import range
Andreas Klöckner's avatar
Andreas Klöckner committed

__copyright__ = "Copyright (C) 2012 Andreas Kloeckner"

__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""


Andreas Klöckner's avatar
Andreas Klöckner committed
import numpy as np
import sys
Andreas Klöckner's avatar
Andreas Klöckner committed

import pyopencl as cl
Andreas Klöckner's avatar
Andreas Klöckner committed
from pyopencl.tools import (  # noqa
        pytest_generate_tests_for_pyopencl as pytest_generate_tests)
from boxtree.tools import make_normal_particle_array
logger = logging.getLogger(__name__)
Andreas Klöckner's avatar
Andreas Klöckner committed

# {{{ bounding box test

@pytest.mark.parametrize("dtype", [np.float32, np.float64])
Andreas Klöckner's avatar
Andreas Klöckner committed
@pytest.mark.parametrize("dims", [2, 3])
@pytest.mark.parametrize("nparticles", [9, 4096, 10**5])
def test_bounding_box(ctx_getter, dtype, dims, nparticles):
    logging.basicConfig(level=logging.INFO)

    ctx = ctx_getter()
    queue = cl.CommandQueue(ctx)

    from boxtree.tools import AXIS_NAMES
    from boxtree.bounding_box import BoundingBoxFinder

    bbf = BoundingBoxFinder(ctx)

    axis_names = AXIS_NAMES[:dims]
    logger.info("%s - %s %s" % (dtype, dims, nparticles))
    particles = make_normal_particle_array(queue, nparticles, dims, dtype)
    bbox_min = [np.min(x.get()) for x in particles]
    bbox_max = [np.max(x.get()) for x in particles]
    bbox_cl, evt = bbf(particles, radii=None)
    bbox_cl = bbox_cl.get()
    bbox_min_cl = np.empty(dims, dtype)
    bbox_max_cl = np.empty(dims, dtype)
    for i, ax in enumerate(axis_names):
        bbox_min_cl[i] = bbox_cl["min_"+ax]
        bbox_max_cl[i] = bbox_cl["max_"+ax]
    assert (bbox_min == bbox_min_cl).all()
    assert (bbox_max == bbox_max_cl).all()
Andreas Klöckner's avatar
Andreas Klöckner committed

# {{{ test basic (no source/target distinction) tree build
Andreas Klöckner's avatar
Andreas Klöckner committed

Andreas Klöckner's avatar
Andreas Klöckner committed
def run_build_test(builder, queue, dims, dtype, nparticles, do_plot,
        max_particles_in_box=30, **kwargs):
    dtype = np.dtype(dtype)
Andreas Klöckner's avatar
Andreas Klöckner committed

    if dtype == np.float32:
        tol = 1e-4
    elif dtype == np.float64:
        tol = 1e-12
    else:
        raise RuntimeError("unsupported dtype: %s" % dtype)

    if (dtype == np.float32
            and dims == 2
            and queue.device.platform.name == "Portable Computing Language"):
        pytest.xfail("2D float doesn't work on POCL")

    logger.info(75*"-")
    logger.info("%dD %s - %d particles - max %d per box - %s" % (
            dims, dtype.type.__name__, nparticles, max_particles_in_box,
Andreas Klöckner's avatar
Andreas Klöckner committed
            " - ".join("%s: %s" % (k, v) for k, v in six.iteritems(kwargs))))
    particles = make_normal_particle_array(queue, nparticles, dims, dtype)
Andreas Klöckner's avatar
Andreas Klöckner committed

    if do_plot:
        import matplotlib.pyplot as pt
        pt.plot(particles[0].get(), particles[1].get(), "x")
Andreas Klöckner's avatar
Andreas Klöckner committed

    queue.finish()
    tree, _ = builder(queue, particles,
            max_particles_in_box=max_particles_in_box, debug=True,
Andreas Klöckner's avatar
Andreas Klöckner committed

    sorted_particles = np.array(list(tree.sources))
Andreas Klöckner's avatar
Andreas Klöckner committed

    unsorted_particles = np.array([pi.get() for pi in particles])
    assert (sorted_particles
            == unsorted_particles[:, tree.user_source_ids]).all()
Andreas Klöckner's avatar
Andreas Klöckner committed

    all_good_so_far = True
Andreas Klöckner's avatar
Andreas Klöckner committed

        from boxtree.visualization import TreePlotter
        plotter = TreePlotter(tree)
        plotter.draw_tree(fill=False, edgecolor="black", zorder=10)
        plotter.set_bounding_box()
Andreas Klöckner's avatar
Andreas Klöckner committed

    from boxtree import box_flags_enum as bfe

    scaled_tol = tol*tree.root_extent
Andreas Klöckner's avatar
Andreas Klöckner committed
    for ibox in range(tree.nboxes):

        # Empty boxes exist in non-pruned trees--which themselves are undocumented.
        # These boxes will fail these tests.
        if not (tree.box_flags[ibox] & bfe.HAS_OWN_SRCNTGTS):
        extent_low, extent_high = tree.get_box_extent(ibox)
Andreas Klöckner's avatar
Andreas Klöckner committed

        assert (extent_low >= tree.bounding_box[0] - scaled_tol).all(), (
                ibox, extent_low, tree.bounding_box[0])
        assert (extent_high <= tree.bounding_box[1] + scaled_tol).all(), (
                ibox, extent_high, tree.bounding_box[1])
Andreas Klöckner's avatar
Andreas Klöckner committed

        start = tree.box_source_starts[ibox]
        box_children = tree.box_child_ids[:, ibox]
        existing_children = box_children[box_children != 0]

        assert (tree.box_source_counts_nonchild[ibox]
                + np.sum(tree.box_source_counts_cumul[existing_children])
                == tree.box_source_counts_cumul[ibox])

        box_particles = sorted_particles[:,
                start:start+tree.box_source_counts_cumul[ibox]]
                (box_particles < extent_high[:, np.newaxis] + scaled_tol)
                (extent_low[:, np.newaxis] - scaled_tol <= box_particles)
Andreas Klöckner's avatar
Andreas Klöckner committed

        all_good_here = good.all()
        if do_plot and not all_good_here and all_good_so_far:
            pt.plot(
                    box_particles[0, np.where(~good)[1]],
                    box_particles[1, np.where(~good)[1]], "ro")
Andreas Klöckner's avatar
Andreas Klöckner committed

            plotter.draw_box(ibox, edgecolor="red")
Andreas Klöckner's avatar
Andreas Klöckner committed

        if not all_good_here:
Andreas Klöckner's avatar
Andreas Klöckner committed
            print("BAD BOX", ibox)
Andreas Klöckner's avatar
Andreas Klöckner committed

        all_good_so_far = all_good_so_far and all_good_here
    if do_plot:
        pt.gca().set_aspect("equal", "datalim")
        pt.show()
Andreas Klöckner's avatar
Andreas Klöckner committed

    assert all_good_so_far
Andreas Klöckner's avatar
Andreas Klöckner committed

Andreas Klöckner's avatar
Andreas Klöckner committed
def particle_tree_test_decorator(f):
    f = pytest.mark.opencl(f)
    f = pytest.mark.parametrize("dtype", [np.float64, np.float32])(f)
    f = pytest.mark.parametrize("dims", [2, 3])(f)

    def wrapper(*args, **kwargs):
        logging.basicConfig(level=logging.INFO)
        f(*args, **kwargs)
Andreas Klöckner's avatar
Andreas Klöckner committed
    return f


@particle_tree_test_decorator
def test_single_boxparticle_tree(ctx_getter, dtype, dims, do_plot=False):
    ctx = ctx_getter()
    queue = cl.CommandQueue(ctx)
Andreas Klöckner's avatar
Andreas Klöckner committed

    from boxtree import TreeBuilder
    builder = TreeBuilder(ctx)
Andreas Klöckner's avatar
Andreas Klöckner committed

    run_build_test(builder, queue, dims,
Andreas Klöckner's avatar
Andreas Klöckner committed
            dtype, 4, do_plot=do_plot)


@particle_tree_test_decorator
def test_two_level_particle_tree(ctx_getter, dtype, dims, do_plot=False):
    ctx = ctx_getter()
    queue = cl.CommandQueue(ctx)

    from boxtree import TreeBuilder
    builder = TreeBuilder(ctx)
    run_build_test(builder, queue, dims,
Andreas Klöckner's avatar
Andreas Klöckner committed
            dtype, 50, do_plot=do_plot)


@particle_tree_test_decorator
def test_unpruned_particle_tree(ctx_getter, dtype, dims, do_plot=False):
    ctx = ctx_getter()
    queue = cl.CommandQueue(ctx)

    from boxtree import TreeBuilder
    builder = TreeBuilder(ctx)
    # test unpruned tree build
    run_build_test(builder, queue, dims, dtype, 10**5,
Andreas Klöckner's avatar
Andreas Klöckner committed
            do_plot=do_plot, skip_prune=True)


@particle_tree_test_decorator
def test_particle_tree_with_reallocations(ctx_getter, dtype, dims, do_plot=False):
    ctx = ctx_getter()
    queue = cl.CommandQueue(ctx)

    from boxtree import TreeBuilder
    builder = TreeBuilder(ctx)
    run_build_test(builder, queue, dims, dtype, 10**5,
Andreas Klöckner's avatar
Andreas Klöckner committed
            do_plot=do_plot, nboxes_guess=5)


@particle_tree_test_decorator
def test_particle_tree_with_many_empty_leaves(
        ctx_getter, dtype, dims, do_plot=False):
    ctx = ctx_getter()
    queue = cl.CommandQueue(ctx)

    from boxtree import TreeBuilder
    builder = TreeBuilder(ctx)
    run_build_test(builder, queue, dims, dtype, 10**5,
Andreas Klöckner's avatar
Andreas Klöckner committed
            do_plot=do_plot, max_particles_in_box=5)


@particle_tree_test_decorator
def test_vanilla_particle_tree(ctx_getter, dtype, dims, do_plot=False):
    ctx = ctx_getter()
    queue = cl.CommandQueue(ctx)

    from boxtree import TreeBuilder
    builder = TreeBuilder(ctx)
    run_build_test(builder, queue, dims, dtype, 10**5,
            do_plot=do_plot)
Andreas Klöckner's avatar
Andreas Klöckner committed
@particle_tree_test_decorator
def test_non_adaptive_particle_tree(ctx_getter, dtype, dims, do_plot=False):
    ctx = ctx_getter()
    queue = cl.CommandQueue(ctx)

    from boxtree import TreeBuilder
    builder = TreeBuilder(ctx)

    run_build_test(builder, queue, dims, dtype, 10**4,
            do_plot=do_plot, non_adaptive=True)

# }}}


# {{{ source/target tree

@pytest.mark.opencl
@pytest.mark.parametrize("dims", [2, 3])
def test_source_target_tree(ctx_getter, dims, do_plot=False):
    logging.basicConfig(level=logging.INFO)

    nsources = 2 * 10**5
    ntargets = 3 * 10**5
    dtype = np.float64
    sources = make_normal_particle_array(queue, nsources, dims, dtype,
    targets = make_normal_particle_array(queue, ntargets, dims, dtype,
    if do_plot:
        import matplotlib.pyplot as pt
        pt.plot(sources[0].get(), sources[1].get(), "rx")
        pt.plot(targets[0].get(), targets[1].get(), "g+")
Andreas Klöckner's avatar
Andreas Klöckner committed
        pt.show()
    from boxtree import TreeBuilder
    tb = TreeBuilder(ctx)
    tree, _ = tb(queue, sources, targets=targets,
            max_particles_in_box=10, debug=True)
    sorted_sources = np.array(list(tree.sources))
    sorted_targets = np.array(list(tree.targets))
    unsorted_sources = np.array([pi.get() for pi in sources])
    unsorted_targets = np.array([pi.get() for pi in targets])
    assert (sorted_sources
            == unsorted_sources[:, tree.user_source_ids]).all()
    user_target_ids = np.empty(tree.ntargets, dtype=np.intp)
    user_target_ids[tree.sorted_target_ids] = np.arange(tree.ntargets, dtype=np.intp)
    assert (sorted_targets
            == unsorted_targets[:, user_target_ids]).all()
    if do_plot:
        from boxtree.visualization import TreePlotter
        plotter = TreePlotter(tree)
        plotter.draw_tree(fill=False, edgecolor="black", zorder=10)
        plotter.set_bounding_box()
Andreas Klöckner's avatar
Andreas Klöckner committed
    for ibox in range(tree.nboxes):
        extent_low, extent_high = tree.get_box_extent(ibox)
Andreas Klöckner's avatar
Andreas Klöckner committed
        assert (extent_low >=
                tree.bounding_box[0] - 1e-12*tree.root_extent).all(), ibox
        assert (extent_high <=
                tree.bounding_box[1] + 1e-12*tree.root_extent).all(), ibox
        src_start = tree.box_source_starts[ibox]
        tgt_start = tree.box_target_starts[ibox]
        box_children = tree.box_child_ids[:, ibox]
        existing_children = box_children[box_children != 0]

        assert (tree.box_source_counts_nonchild[ibox]
                + np.sum(tree.box_source_counts_cumul[existing_children])
                == tree.box_source_counts_cumul[ibox])
        assert (tree.box_target_counts_nonchild[ibox]
                + np.sum(tree.box_target_counts_cumul[existing_children])
                == tree.box_target_counts_cumul[ibox])

Andreas Klöckner's avatar
Andreas Klöckner committed
                ("sources", sorted_sources[:,
                    src_start:src_start+tree.box_source_counts_cumul[ibox]]),
Andreas Klöckner's avatar
Andreas Klöckner committed
                ("targets", sorted_targets[:,
                    tgt_start:tgt_start+tree.box_target_counts_cumul[ibox]]),
                ]:
            good = (
                    (particles < extent_high[:, np.newaxis])
                    &
                    (extent_low[:, np.newaxis] <= particles)
                    ).all(axis=0)
            all_good_here = good.all()
            if do_plot and not all_good_here:
                pt.plot(
                        particles[0, np.where(~good)[0]],
                        particles[1, np.where(~good)[0]], "ro")
                plotter.draw_box(ibox, edgecolor="red")
                pt.show()
Andreas Klöckner's avatar
Andreas Klöckner committed
            print("BAD BOX %s %d" % (what, ibox))
        all_good_so_far = all_good_so_far and all_good_here
    if do_plot:
        pt.gca().set_aspect("equal", "datalim")
        pt.show()
Andreas Klöckner's avatar
Andreas Klöckner committed

@pytest.mark.opencl
@pytest.mark.parametrize("dims", [2, 3])
def test_extent_tree(ctx_getter, dims, do_plot=False):
    logging.basicConfig(level=logging.INFO)

    ctx = ctx_getter()
    queue = cl.CommandQueue(ctx)

    nsources = 100000
    ntargets = 200000
    dtype = np.float64
    npoint_sources_per_source = 16
    sources = make_normal_particle_array(queue, nsources, dims, dtype,
    targets = make_normal_particle_array(queue, ntargets, dims, dtype,
            seed=19)

    from pyopencl.clrandom import RanluxGenerator
    rng = RanluxGenerator(queue, seed=13)
    source_radii = 2**rng.uniform(queue, nsources, dtype=dtype,
            a=-10, b=0)
    target_radii = 2**rng.uniform(queue, ntargets, dtype=dtype,
            a=-10, b=0)

    from boxtree import TreeBuilder
    tb = TreeBuilder(ctx)

    queue.finish()
    dev_tree, _ = tb(queue, sources, targets=targets,
            source_radii=source_radii, target_radii=target_radii,
            max_particles_in_box=10, debug=True)

    logger.info("transfer tree, check orderings")

    tree = dev_tree.get(queue=queue)

    sorted_sources = np.array(list(tree.sources))
    sorted_targets = np.array(list(tree.targets))
    sorted_source_radii = tree.source_radii
    sorted_target_radii = tree.target_radii

    unsorted_sources = np.array([pi.get() for pi in sources])
    unsorted_targets = np.array([pi.get() for pi in targets])
    unsorted_source_radii = source_radii.get()
    unsorted_target_radii = target_radii.get()
    assert (sorted_sources
            == unsorted_sources[:, tree.user_source_ids]).all()
    assert (sorted_source_radii
            == unsorted_source_radii[tree.user_source_ids]).all()

    # {{{ test box structure, stick-out criterion

    logger.info("test box structure, stick-out criterion")

    user_target_ids = np.empty(tree.ntargets, dtype=np.intp)
    user_target_ids[tree.sorted_target_ids] = np.arange(tree.ntargets, dtype=np.intp)
    if ntargets:
        assert (sorted_targets
                == unsorted_targets[:, user_target_ids]).all()
        assert (sorted_target_radii
                == unsorted_target_radii[user_target_ids]).all()

    all_good_so_far = True

Andreas Klöckner's avatar
Andreas Klöckner committed
    for ibox in range(tree.nboxes):
        extent_low, extent_high = tree.get_box_extent(ibox)

        box_radius = np.max(extent_high-extent_low) * 0.5
        stick_out_dist = tree.stick_out_factor * box_radius

Andreas Klöckner's avatar
Andreas Klöckner committed
        assert (extent_low >=
                tree.bounding_box[0] - 1e-12*tree.root_extent).all(), ibox
        assert (extent_high <=
                tree.bounding_box[1] + 1e-12*tree.root_extent).all(), ibox
        box_children = tree.box_child_ids[:, ibox]
        existing_children = box_children[box_children != 0]

        assert (tree.box_source_counts_nonchild[ibox]
                + np.sum(tree.box_source_counts_cumul[existing_children])
                == tree.box_source_counts_cumul[ibox])
        assert (tree.box_target_counts_nonchild[ibox]
                + np.sum(tree.box_target_counts_cumul[existing_children])
                == tree.box_target_counts_cumul[ibox])
        for what, starts, counts, points, radii in [
                ("source", tree.box_source_starts, tree.box_source_counts_cumul,
                    sorted_sources, sorted_source_radii),
                ("target", tree.box_target_starts, tree.box_target_counts_cumul,
                    sorted_targets, sorted_target_radii),
                ]:
            bstart = starts[ibox]
            bslice = slice(bstart, bstart+counts[ibox])
            check_particles = points[:, bslice]
            check_radii = radii[bslice]
            good = (
                    (check_particles + check_radii
                        < extent_high[:, np.newaxis] + stick_out_dist)
                    &
                    (extent_low[:, np.newaxis] - stick_out_dist
                        <= check_particles - check_radii)
                    ).all(axis=0)
Andreas Klöckner's avatar
Andreas Klöckner committed
                print("BAD BOX %s %d level %d" % (what, ibox, tree.box_levels[ibox]))
            all_good_so_far = all_good_so_far and all_good_here
            assert all_good_here
    assert all_good_so_far

    # }}}

    # {{{ create, link point sources

    logger.info("creating point sources")

    np.random.seed(20)

    from pytools.obj_array import make_obj_array
    point_sources = make_obj_array([
            cl.array.to_device(queue,
                unsorted_sources[i][:, np.newaxis]
                + unsorted_source_radii[:, np.newaxis]
                * np.random.uniform(
Andreas Klöckner's avatar
Andreas Klöckner committed
                    -1, 1, size=(nsources, npoint_sources_per_source))
                 )
            for i in range(dims)])

    point_source_starts = cl.array.arange(queue,
            0, (nsources+1)*npoint_sources_per_source, npoint_sources_per_source,
            dtype=tree.particle_id_dtype)

    from boxtree.tree import link_point_sources
    dev_tree = link_point_sources(queue, dev_tree,
            point_source_starts, point_sources,
            debug=True)

    # }}}
Andreas Klöckner's avatar
Andreas Klöckner committed

# {{{ geometry query test

@pytest.mark.opencl
@pytest.mark.parametrize("dims", [2, 3])
def test_geometry_query(ctx_getter, dims, do_plot=False):
    logging.basicConfig(level=logging.INFO)

    ctx = ctx_getter()
    queue = cl.CommandQueue(ctx)

    nparticles = 10**5
    dtype = np.float64

    particles = make_normal_particle_array(queue, nparticles, dims, dtype)
        import matplotlib.pyplot as pt
        pt.plot(particles[0].get(), particles[1].get(), "x")

    from boxtree import TreeBuilder
    tb = TreeBuilder(ctx)

    queue.finish()
    tree, _ = tb(queue, particles, max_particles_in_box=30, debug=True)

    nballs = 10**4
    ball_centers = make_normal_particle_array(queue, nballs, dims, dtype)
    ball_radii = cl.array.empty(queue, nballs, dtype).fill(0.1)

    from boxtree.geo_lookup import LeavesToBallsLookupBuilder
    lblb = LeavesToBallsLookupBuilder(ctx)

    lbl, _ = lblb(queue, tree, ball_centers, ball_radii)

    # get data to host for test
    tree = tree.get(queue=queue)
    lbl = lbl.get(queue=queue)
    ball_centers = np.array([x.get() for x in ball_centers]).T
    ball_radii = ball_radii.get()

    from boxtree import box_flags_enum

Andreas Klöckner's avatar
Andreas Klöckner committed
    for ibox in range(tree.nboxes):
        # We only want leaves here.
        if tree.box_flags[ibox] & box_flags_enum.HAS_CHILDREN:
            continue

        box_center = tree.box_centers[:, ibox]
        ext_l, ext_h = tree.get_box_extent(ibox)
        box_rad = 0.5*(ext_h-ext_l)[0]

        linf_circle_dists = np.max(np.abs(ball_centers-box_center), axis=-1)
        near_circles, = np.where(linf_circle_dists - ball_radii < box_rad)

        start, end = lbl.balls_near_box_starts[ibox:ibox+2]
        #print sorted(lbl.balls_near_box_lists[start:end])
        #print sorted(near_circles)
        assert sorted(lbl.balls_near_box_lists[start:end]) == sorted(near_circles)

# }}}

Andreas Klöckner's avatar
Andreas Klöckner committed

# You can test individual routines by typing
Andreas Klöckner's avatar
Andreas Klöckner committed
# $ python test_tree.py 'test_routine(cl.create_some_context)'
Andreas Klöckner's avatar
Andreas Klöckner committed

if __name__ == "__main__":
    if len(sys.argv) > 1:
        exec(sys.argv[1])
    else:
        from py.test.cmdline import main
        main([__file__])

# vim: fdm=marker