The forest is a 2x2 brick covering the unit square. First, it is refined uniformly to a user-specified level. Then, it is refined adaptively around two refinement center points a and b. The query points are created with a random distribution that clusters them around the refinement point b and another point c. As a result, both searches are tested for
The adaptive refinement of the forest can be controlled by its maxlevel and its level of uniform refinement. In particular, by choosing the maxlevel small enough, a uniform p4est can be enforced. Similarly, the query point creation can be controlled by the "clustering exponent" c. Setting c to 0 leads to a uniform distribution. Higher values of c lead to the query points increasingly clustering around point b and point c. To check the distribution, the vtk output of the uniform forest can be checked for the amount of queries found in each quadrant.
#include <sc_options.h>
#ifndef P4_TO_P8
#else
#endif
#define COORDINATE_IROOTLEN (1. / P4EST_ROOT_LEN)
typedef struct search_partition_global
{
double a[3], b[3], c[3];
int uniform_level;
int max_level;
int seed;
double clustering_exponent;
sc_array_t *queries;
int num_local_queries;
sc_array_t *num_queries_per_rank;
sc_array_t *global_nlq;
int write_vtk;
sc_array_t *num_queries_per_quad;
}
search_partition_global_t;
typedef struct query_point
{
double xyz[3];
int is_local;
int rank;
}
query_point_t;
static void
{
xyz[0] = 0.5 * (COORDINATE_IROOTLEN * x + which_tree % 2);
xyz[1] = 0.5 * (COORDINATE_IROOTLEN * y + (which_tree / 2) % 2);
#ifndef P4_TO_P8
xyz[2] = 0.;
#else
xyz[2] = 0.5 * (COORDINATE_IROOTLEN * z + which_tree / 4);
#endif
}
static int
{
double xyz[3];
double dist, min_dist;
P4EST_ASSERT (
p4est != NULL);
search_partition_global_t *g =
P4EST_ASSERT (quadrant != NULL);
p4est_quadrant_volume_coordinates (quadrant, coords);
map_coordinates (coords[0], coords[1],
#ifndef P4_TO_P8
0,
#else
coords[2],
#endif
which_tree, xyz);
dist = (g->a[0] - xyz[0]) * (g->a[0] - xyz[0]) +
(g->a[1] - xyz[1]) * (g->a[1] - xyz[1]) +
(g->a[2] - xyz[2]) * (g->a[2] - xyz[2]);
min_dist = sqrt (dist);
dist = (g->b[0] - xyz[0]) * (g->b[0] - xyz[0]) +
(g->b[1] - xyz[1]) * (g->b[1] - xyz[1]) +
(g->b[2] - xyz[2]) * (g->b[2] - xyz[2]);
min_dist = SC_MIN (min_dist, sqrt (dist));
return (quadrant->level <
g->max_level -
floor (min_dist * (g->max_level - g->uniform_level) / 0.2));
}
static void
create_p4est (search_partition_global_t *g)
{
int il;
g->conn = p4est_connectivity_new_brick (2, 2,
#ifdef P4_TO_P8
2,
#endif
0, 0
#ifdef P4_TO_P8
, 0
#endif
);
g->p4est =
p4est_new_ext (sc_MPI_COMM_WORLD, g->conn, 0, g->uniform_level, 1, 0,
NULL, g);
p4est_partition (g->p4est, 1, NULL);
for (il = g->uniform_level; il < g->max_level; il++) {
old_gnq = g->p4est->global_num_quadrants;
p4est_refine (g->p4est, 0, refine_fn, NULL);
if (old_gnq == g->p4est->global_num_quadrants) {
break;
}
p4est_partition (g->p4est, 0, NULL);
}
}
static void
generate_queries (search_partition_global_t *g)
{
int id;
query_point_t *p;
double t;
int mpiret;
g->queries =
sc_array_new_count (sizeof (query_point_t), g->num_global_queries);
sc_array_memset (g->queries, 0);
if (g->p4est->mpirank == 0) {
srand (g->seed);
nqh = g->num_global_queries / 2;
for (iq = 0; iq < g->num_global_queries; iq++) {
p = (query_point_t *) sc_array_index_int (g->queries, iq);
p->is_local = 0;
p->rank = -1;
p->xyz[id] = rand () / (RAND_MAX + 1.);
}
t = pow (rand () / (RAND_MAX + 1.), g->clustering_exponent);
if (iq < nqh) {
p->xyz[0] = t * p->xyz[0] + (1 - t) * g->b[0];
p->xyz[1] = t * p->xyz[1] + (1 - t) * g->b[1];
p->xyz[2] = t * p->xyz[2] + (1 - t) * g->b[2];
}
else {
p->xyz[0] = t * p->xyz[0] + (1 - t) * g->c[0];
p->xyz[1] = t * p->xyz[1] + (1 - t) * g->c[1];
p->xyz[2] = t * p->xyz[2] + (1 - t) * g->c[2];
}
}
}
mpiret = sc_MPI_Bcast (g->queries->array,
g->num_global_queries * sizeof (query_point_t),
sc_MPI_BYTE, 0, g->p4est->mpicomm);
SC_CHECK_MPI (mpiret);
P4EST_GLOBAL_PRODUCTIONF ("Created %lld global queries.\n",
(unsigned long long) g->queries->elem_count);
}
static int
{
double qxyz[3];
double qlen;
map_coordinates (quadrant->
x, quadrant->
y,
#ifndef P4_TO_P8
0,
#else
quadrant->z,
#endif
which_tree, qxyz);
if (p->xyz[0] < qxyz[0] || p->xyz[0] > qxyz[0] + qlen ||
p->xyz[1] < qxyz[1] || p->xyz[1] > qxyz[1] + qlen ||
p->xyz[2] < qxyz[2] || p->xyz[2] > qxyz[2] + qlen) {
return 0;
}
return 1;
}
static int
void *point)
{
P4EST_ASSERT (point != NULL);
query_point_t *p = (query_point_t *) point;
P4EST_ASSERT (
p4est != NULL);
search_partition_global_t *g =
if (!quadrant_contains_query (quadrant, which_tree, p)) {
return 0;
}
if (local_num >= 0) {
p->is_local = 1;
g->num_local_queries++;
*(double *) sc_array_index (g->num_queries_per_quad, local_num) += 1.;
}
return 1;
}
static void
search_local (search_partition_global_t *g)
{
int gnq;
size_t il;
int mpiret;
g->num_local_queries = 0;
g->num_queries_per_quad =
sc_array_new_count (sizeof (double), g->p4est->local_num_quadrants);
sc_array_memset (g->num_queries_per_quad, 0);
p4est_search_local (g->p4est, 0, NULL, local_callback, g->queries);
P4EST_INFOF ("Queries found in local search = %d\n", g->num_local_queries);
g->global_nlq = sc_array_new_count (sizeof (int), g->p4est->mpisize);
mpiret = sc_MPI_Allgather (&g->num_local_queries, 1, sc_MPI_INT,
g->global_nlq->array, 1, sc_MPI_INT,
g->p4est->mpicomm);
SC_CHECK_MPI (mpiret);
gnq = 0;
for (il = 0; il < g->global_nlq->elem_count; il++) {
gnq += *(int *) sc_array_index (g->global_nlq, il);
}
P4EST_GLOBAL_PRODUCTIONF
("Queries found globally during local search: %d (expected %d)\n",
gnq, (int) g->num_global_queries);
}
static int
void *point)
{
P4EST_ASSERT (point != NULL);
query_point_t *p = (query_point_t *) point;
P4EST_ASSERT (
p4est != NULL);
search_partition_global_t *g =
if (!quadrant_contains_query (quadrant, which_tree, p)) {
return 0;
}
if (pfirst == plast) {
p->rank = pfirst;
P4EST_ASSERT (((p->rank ==
p4est->
mpirank) && p->is_local) ||
*(size_t *) sc_array_index (g->num_queries_per_rank, pfirst) += 1;
}
return 1;
}
static void
search_partition (search_partition_global_t *g)
{
size_t iz, lenz, buffer_size;
int num_queries_found;
int retb;
char *buffer;
g->num_queries_per_rank =
sc_array_new_count (sizeof (int), g->p4est->mpisize);
sc_array_memset (g->num_queries_per_rank, 0);
p4est_search_partition (g->p4est, 0, NULL, partition_callback, g->queries);
buffer_size = 0;
for (iz = 0; iz < g->num_queries_per_rank->elem_count; iz++) {
if (iz % 10 == 0) {
buffer_size += 1;
}
retb = snprintf (NULL, 0, "%7d ", *(int *)
sc_array_index (g->num_queries_per_rank, iz));
SC_CHECK_ABORT (retb > 0, "Overflow in snprintf");
buffer_size += (size_t) retb;
}
lenz = 0;
num_queries_found = 0;
for (iz = 0; iz < g->num_queries_per_rank->elem_count; iz++) {
if (iz % 10 == 0) {
P4EST_ASSERT (buffer_size >= lenz);
retb = snprintf (buffer + lenz, buffer_size - lenz, "\n");
SC_CHECK_ABORT (retb == 1, "Overflow in snprintf");
lenz += retb;
}
retb = snprintf (buffer + lenz, buffer_size - lenz, "%7d ", *(int *)
sc_array_index (g->num_queries_per_rank, iz));
SC_CHECK_ABORT (retb > 0, "Overflow in snprintf");
lenz += retb;
num_queries_found +=
*(int *) sc_array_index (g->num_queries_per_rank, iz);
}
P4EST_GLOBAL_PRODUCTIONF
("Queries found during partition search: %d (expected %d)\n",
num_queries_found, (int) g->num_global_queries);
P4EST_GLOBAL_STATISTICSF
("Partition search found the following query counts %s\n", buffer);
for (iz = 0; iz < g->num_queries_per_rank->elem_count; iz++) {
P4EST_ASSERT (*(int *) sc_array_index (g->num_queries_per_rank, iz) ==
*(int *) sc_array_index (g->global_nlq, iz));
}
}
static void
write_vtk (search_partition_global_t *g)
{
char filename[BUFSIZ];
if (!g->write_vtk) {
return;
}
cont = NULL;
do {
snprintf (filename, BUFSIZ, "search_partition%d_%d_%d_%d_%d_%.2f",
(int) g->num_global_queries, g->seed, g->clustering_exponent);
cont = p4est_vtk_context_new (g->p4est, filename);
if (NULL == p4est_vtk_write_header (cont)) {
P4EST_LERRORF ("Failed to write header for %s\n", filename);
break;
}
if (NULL == p4est_vtk_write_cell_dataf
(cont, 1, 1, 1, 0, 1, 0, "num_queries", g->num_queries_per_quad,
cont)) {
P4EST_LERRORF ("Failed to write cell data for %s\n", filename);
break;
}
if (p4est_vtk_write_footer (cont)) {
P4EST_LERRORF ("Failed to write footer for %s\n", filename);
break;
}
}
while (0);
}
static void
cleanup (search_partition_global_t *g)
{
sc_array_destroy (g->queries);
sc_array_destroy (g->global_nlq);
sc_array_destroy (g->num_queries_per_quad);
sc_array_destroy (g->num_queries_per_rank);
p4est_destroy (g->p4est);
p4est_connectivity_destroy (g->conn);
}
static void
run (search_partition_global_t *g)
{
create_p4est (g);
generate_queries (g);
search_local (g);
search_partition (g);
write_vtk (g);
cleanup (g);
}
int
main (int argc, char **argv)
{
int mpiret;
sc_options_t *opt;
int first_argc, ue, help;
search_partition_global_t global, *g = &global;
int ngq;
mpiret = sc_MPI_Init (&argc, &argv);
SC_CHECK_MPI (mpiret);
sc_init (sc_MPI_COMM_WORLD, 1, 1, NULL, SC_LP_DEFAULT);
opt = sc_options_new (argv[0]);
sc_options_add_switch (opt, 'h', "help", &help,
"Print help message and exit cleanly");
sc_options_add_int (opt, 'l', "minlevel", &g->uniform_level, 3,
"Level of uniform refinement");
sc_options_add_int (opt, 'L', "maxlevel", &g->max_level, 5,
"Level of maximum refinement");
sc_options_add_int (opt, 'q', "num-queries", &ngq, 100,
"Number of queries created per process");
sc_options_add_int (opt, 's', "seed", &g->seed, 0,
"Seed for random queries");
sc_options_add_bool (opt, 'v', "write-vtk", &g->write_vtk, 0,
"Activate vtk output");
sc_options_add_double (opt, 'c', "clustering-exponent",
&g->clustering_exponent, 0.5,
"Clustering of queries");
ue = 0;
do {
opt, argc, argv);
if (first_argc < 0) {
P4EST_GLOBAL_LERROR ("Invalid option format.\n");
ue = 1;
break;
}
P4EST_GLOBAL_LERRORF ("Uniform level out of bounds 0..%d\n",
ue = 1;
}
P4EST_GLOBAL_LERRORF ("Maximum level out of bounds 0..%d\n",
ue = 1;
}
if (g->num_global_queries < 0) {
P4EST_GLOBAL_LERROR ("Number of queries has to be non-negative.\n");
ue = 1;
}
if ((long long) g->num_global_queries * sizeof (query_point_t) > INT_MAX) {
P4EST_GLOBAL_LERROR ("Number of queries too large for MPI buffer.\n");
ue = 1;
}
if (g->seed < 0) {
P4EST_GLOBAL_LERROR ("Seed has to be non-negative.\n");
ue = 1;
}
if (g->clustering_exponent < 0.) {
P4EST_GLOBAL_LERROR ("Clustering exponent has to be non-negative.\n");
ue = 1;
}
if (ue || help) {
break;
}
g->a[0] = 0.2;
g->a[1] = 0.4;
g->a[2] = 0.4;
g->b[0] = 0.7;
g->b[1] = 0.55;
g->b[2] = 0.55;
g->c[0] = 0.3;
g->c[1] = 0.8;
g->c[2] = 0.8;
#ifndef P4_TO_P8
g->a[2] = g->b[2] = g->c[2] = 0.;
#endif
run (g);
}
while (0);
if (ue || help) {
}
sc_options_destroy (opt);
sc_finalize ();
mpiret = sc_MPI_Finalize ();
SC_CHECK_MPI (mpiret);
return ue ? EXIT_FAILURE : EXIT_SUCCESS;
}
#define P4EST_QMAXLEVEL
The finest level of the quadtree for representing quadrant corners.
Definition p4est.h:59
#define P4EST_QUADRANT_LEN(l)
The length of a quadrant of level l.
Definition p4est.h:65
int32_t p4est_qcoord_t
Typedef for quadrant coordinates.
Definition p4est_base.h:81
#define P4EST_FREE(p)
free an allocated array
Definition p4est_base.h:212
#define P4EST_ALLOC(t, n)
allocate a t-array with n elements
Definition p4est_base.h:201
int32_t p4est_topidx_t
Typedef for counting topological entities (trees, tree vertices).
Definition p4est_base.h:93
int p4est_get_package_id(void)
Query the package identity as registered in libsc.
int32_t p4est_locidx_t
Typedef for processor-local indexing of quadrants and nodes.
Definition p4est_base.h:106
void p4est_init(sc_log_handler_t log_handler, int log_threshold)
Registers p4est with the SC Library and sets the logging behavior.
int64_t p4est_gloidx_t
Typedef for globally unique indexing of quadrants.
Definition p4est_base.h:118
Routines for manipulating quadrants (neighbors, parents, children, etc.)
#define P4EST_DIM
The spatial dimension.
Definition p4est_connectivity.h:71
#define P4EST_CHILDREN
The number of children of a quadrant, also the number of corners.
Definition p4est_connectivity.h:75
Interface routines with extended capabilities.
Search through quadrants, the local part of a forest, or the partition.
Routines for printing a forest and associated fields to VTK format.
struct p4est_vtk_context p4est_vtk_context_t
Opaque context type for writing VTK output with multiple function calls.
Definition p4est_vtk.h:42
Routines for manipulating quadrants (neighbors, parents, children, etc.)
Interface routines with extended capabilities.
Search through quadrants, the local part of a forest, or the partition.
Routines for printing a forest and associated fields to VTK format.
This structure holds the 2D inter-tree connectivity information.
Definition p4est_connectivity.h:190
The 2D quadrant datatype.
Definition p4est.h:76
int8_t level
level of refinement
Definition p4est.h:80
p4est_qcoord_t y
coordinates
Definition p4est.h:78
The p4est forest datatype.
Definition p4est.h:150
void * user_pointer
convenience pointer for users, never touched by p4est
Definition p4est.h:157
int mpirank
this process's MPI rank
Definition p4est.h:153