Skip to content

Commit

Permalink
Feature GMT: cleanup/tweaks on sphere examples
Browse files Browse the repository at this point in the history
  • Loading branch information
cburstedde committed Feb 15, 2024
1 parent 2ae2d94 commit 6909f06
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
7 changes: 3 additions & 4 deletions example/gmt/gmt2.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,14 @@ main (int argc, char **argv)
ue = usagerrf (opt, "maxlevel not between minlevel and %d",
P4EST_QMAXLEVEL);
}
if (g->synthetic >= 0 ? (g->latlongno >= 0 || g->sphere == 1)
: (g->latlongno >= 0 && g->sphere == 1)
if (g->synthetic >= 0 ? (g->latlongno >= 0 || g->sphere)
: (g->latlongno >= 0 && g->sphere)
) {
ue =
usagerrf (opt,
"set only one of the synthetic, sphere and latlong models");
}
if (g->synthetic < 0 && g->latlongno < 0 && g->sphere == 0) {
if (g->synthetic < 0 && g->latlongno < 0 && !g->sphere) {
ue =
usagerrf (opt,
"set one of the synthetic, sphere, and latlong models");
Expand All @@ -334,7 +334,6 @@ main (int argc, char **argv)

/* cleanup application model */
if (g->model != NULL) {
p4est_connectivity_destroy (g->model->conn);
p4est_gmt_model_destroy (g->model);
}

Expand Down
59 changes: 45 additions & 14 deletions example/gmt/gmt_models.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ static int
model_latlong_intersect (p4est_topidx_t which_tree, const double coord[4],
size_t m, void *vmodel)
{
#ifdef P4EST_ENABLE_DEBUG
p4est_gmt_model_t *model = (p4est_gmt_model_t *) vmodel;
#endif

P4EST_ASSERT (model != NULL);
P4EST_ASSERT (m < model->M);
Expand Down Expand Up @@ -341,15 +343,25 @@ p4est_gmt_model_sphere_new (int resolution, const char *input,
p4est_gmt_model_sphere_t *sdata = NULL;
size_t global_num_points = 0;
size_t local_num_points = 0;
int local_int_bytes;
int rank;
int mpiret;
int mpival;
int count;
int mpireslen;
char mpierrstr[sc_MPI_MAX_ERROR_STRING];
sc_MPI_Offset mpi_offset;

/* Get rank */
mpiret = sc_MPI_Comm_rank (mpicomm, &rank);
SC_CHECK_MPI (mpiret);

/* clean initialization */
mpival = sc_MPI_SUCCESS;
file_handle = sc_MPI_FILE_NULL;
model = NULL;

/* Check for required parameters */
if (input == NULL) {
P4EST_GLOBAL_LERROR ("Sphere model expects non-NULL input filename.\n");
P4EST_GLOBAL_LERROR ("Use the -F flag to set a filename.\n");
Expand All @@ -360,6 +372,9 @@ p4est_gmt_model_sphere_new (int resolution, const char *input,
mpiret = sc_io_open (mpicomm, input, SC_IO_READ, sc_MPI_INFO_NULL,
&file_handle);
if (mpiret != sc_MPI_SUCCESS) {
mpiret = sc_MPI_Error_string (mpiret, mpierrstr, &mpireslen);
SC_CHECK_MPI (mpiret);
P4EST_GLOBAL_LERRORF ("Error opening file: %s\n", mpierrstr);
P4EST_GLOBAL_LERRORF ("Could not open input file: %s\n", input);
P4EST_GLOBAL_LERROR ("Check you have run the preprocessing script.\n");
P4EST_GLOBAL_LERROR ("Check you specified the input path correctly\n");
Expand All @@ -368,19 +383,25 @@ p4est_gmt_model_sphere_new (int resolution, const char *input,

if (rank == 0) {
/* read the global number of points from file */
mpiret = sc_io_read_at (file_handle, 0, &global_num_points,
mpival = sc_io_read_at (file_handle, 0, &global_num_points,
sizeof (size_t), sc_MPI_BYTE, &count);

/* did we read the right number of bytes for the count variable? */
if (mpival == sc_MPI_SUCCESS && count != (int) sizeof (size_t)) {
P4EST_GLOBAL_LERROR ("Count mismatch: reading number of global points\n");
mpival = sc_MPI_ERR_COUNT;
}
}

/* broadcast and check possible errors */
sc_MPI_Bcast(&mpiret, sizeof (int), sc_MPI_BYTE, 0, mpicomm);
if (mpiret != sc_MPI_SUCCESS) {
mpiret = sc_MPI_Bcast (&mpival, 1, sc_MPI_INT, 0, mpicomm);
SC_CHECK_MPI (mpiret);
if (mpival != sc_MPI_SUCCESS) {
mpiret = sc_MPI_Error_string (mpival, mpierrstr, &mpireslen);
SC_CHECK_MPI (mpiret);
P4EST_GLOBAL_LERRORF ("Error reading file: %s\n", mpierrstr);
P4EST_GLOBAL_LERROR ("Error reading number of global points\n");
return NULL;
}
sc_MPI_Bcast(&count, sizeof (int), sc_MPI_BYTE, 0, mpicomm);
if (count != (int) sizeof (size_t)) {
P4EST_GLOBAL_LERROR ("Count mismatch: reading number of global points\n");
(void) sc_io_close (&file_handle);
return NULL;
}

Expand All @@ -389,10 +410,20 @@ p4est_gmt_model_sphere_new (int resolution, const char *input,
sc_MPI_BYTE, 0, mpicomm);
SC_CHECK_MPI (mpiret);

/* check for legal value of point count */
if (global_num_points * sizeof (p4est_gmt_sphere_geoseg_t) > (size_t) INT_MAX) {
P4EST_GLOBAL_LERRORF ("Global number of points %lld is too big\n",
(long long) global_num_points);
(void) sc_io_close (&file_handle);
return NULL;
}

/* set read offsets */
/* note: these will be more relevant in the distributed version */
/* note: these will be generalized in the distributed version */
mpi_offset = 0;
local_num_points = global_num_points;
local_int_bytes = (int) (local_num_points * sizeof (p4est_gmt_sphere_geoseg_t));
P4EST_ASSERT (local_int_bytes >= 0);

/* allocate model */
model = P4EST_ALLOC_ZERO (p4est_gmt_model_t, 1);
Expand All @@ -403,12 +434,11 @@ p4est_gmt_model_sphere_new (int resolution, const char *input,
/* each mpi process reads its data for its own offset */
mpiret = sc_io_read_at_all (file_handle, mpi_offset + sizeof (size_t),
sdata->geodesics,
local_num_points *
sizeof (p4est_gmt_sphere_geoseg_t), sc_MPI_BYTE,
&count);
local_int_bytes, sc_MPI_BYTE, &count);
SC_CHECK_MPI (mpiret);
SC_CHECK_ABORT (count == (int) (local_num_points
* sizeof (p4est_gmt_sphere_geoseg_t)),

/* TO DO: should we soft check this? */
SC_CHECK_ABORT (count == local_int_bytes,
"Read points: count mismatch");

/* close the file collectively */
Expand Down Expand Up @@ -453,5 +483,6 @@ p4est_gmt_model_destroy (p4est_gmt_model_t * model)
P4EST_FREE (model->model_data);
}
p4est_geometry_destroy (model->model_geom);
p4est_connectivity_destroy (model->conn);
P4EST_FREE (model);
}
3 changes: 2 additions & 1 deletion example/gmt/sphere_preprocessing.c
Original file line number Diff line number Diff line change
Expand Up @@ -574,9 +574,10 @@ main (int argc, char **argv)
mpiret = sc_MPI_Init (&argc, &argv);
SC_CHECK_MPI (mpiret);

/* default communicator used for logging */
/* default communicator and packages used for logging */
mpicomm = sc_MPI_COMM_WORLD;
sc_init (mpicomm, 1, 1, NULL, SC_LP_DEFAULT);
p4est_init (NULL, SC_LP_DEFAULT);

/* Get rank and number of processes */
mpiret = sc_MPI_Comm_size (mpicomm, &num_procs);
Expand Down

0 comments on commit 6909f06

Please sign in to comment.