Skip to content

Commit

Permalink
moving code to function so that multiple computation units can be han…
Browse files Browse the repository at this point in the history
…dled
  • Loading branch information
edhartnett committed Sep 9, 2019
1 parent fc92097 commit 94cdb68
Showing 1 changed file with 106 additions and 88 deletions.
194 changes: 106 additions & 88 deletions tests/ncint/tst_async_multi.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
/* Test netcdf integration layer.
This tests that multiple computation units can work in async mode,
using the netCDF integration layer.
Ed Hartnett
*/

#include "config.h"
#include <pio.h>
#include "pio_err_macros.h"

#define FILE_NAME "tst_async_multi.nc"
#define TEST_NAME "tst_async_multi"
#define VAR_NAME "data_var"
#define DIM_NAME_UNLIMITED "dim_unlimited"
#define DIM_NAME_X "dim_x"
Expand All @@ -22,6 +25,105 @@ extern NC_Dispatch NCINT_dispatcher;
/* Number of computational components to create. */
#define COMPONENT_COUNT 1

/* Create a file with one 2D var. */
int
create_file_0(int my_rank, int ntasks, int num_io_procs, int iosysid)
{
int ncid, ioid;
int dimid[NDIM3], varid;
int dimlen[NDIM3] = {NC_UNLIMITED, DIM_LEN_X, DIM_LEN_Y};
size_t elements_per_pe;
size_t *compdof; /* The decomposition mapping. */
int *my_data;
int *data_in;
char file_name[NC_MAX_NAME + 1];
int i;

/* Create a file with a 3D record var. */
sprintf(file_name, "%s_component_%d.nc", TEST_NAME, 0);
if (nc_create(file_name, NC_PIO|NC_NETCDF4, &ncid)) PERR;
if (nc_def_dim(ncid, DIM_NAME_UNLIMITED, dimlen[0], &dimid[0])) PERR;
if (nc_def_dim(ncid, DIM_NAME_X, dimlen[1], &dimid[1])) PERR;
if (nc_def_dim(ncid, DIM_NAME_Y, dimlen[2], &dimid[2])) PERR;
if (nc_def_var(ncid, VAR_NAME, NC_INT, NDIM3, dimid, &varid)) PERR;
if (nc_enddef(ncid)) PERR;

/* Calculate a decomposition for distributed arrays. */
elements_per_pe = DIM_LEN_X * DIM_LEN_Y / (ntasks - num_io_procs);
/* printf("my_rank %d elements_per_pe %ld\n", my_rank, elements_per_pe); */
if (!(compdof = malloc(elements_per_pe * sizeof(size_t))))
PERR;
for (i = 0; i < elements_per_pe; i++)
{
compdof[i] = (my_rank - num_io_procs) * elements_per_pe + i;
/* printf("my_rank %d compdof[%d]=%ld\n", my_rank, i, compdof[i]); */
}

/* Create the PIO decomposition for this test. */
if (nc_def_decomp(iosysid, PIO_INT, NDIM2, &dimlen[1], elements_per_pe,
compdof, &ioid, 1, NULL, NULL)) PERR;
free(compdof);

/* Create some data on this processor. */
if (!(my_data = malloc(elements_per_pe * sizeof(int)))) PERR;
for (i = 0; i < elements_per_pe; i++)
my_data[i] = my_rank * 10 + i;

/* Write some data with distributed arrays. */
if (nc_put_vard_int(ncid, varid, ioid, 0, my_data)) PERR;
if (nc_close(ncid)) PERR;

/* Reopen the file using netCDF integration. */
{
int ndims, nvars, ngatts, unlimdimid;
nc_type xtype_in;
char var_name_in[NC_MAX_NAME + 1];
char dim_name_in[NC_MAX_NAME + 1];
int natts_in;
int dimids_in[NDIM3];
size_t dim_len_in;

/* Open the file. */
if (nc_open(file_name, NC_PIO, &ncid)) PERR;

/* Check the file. */
if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) PERR;
if (ndims != 3 || nvars != 1 || ngatts != 0 ||
unlimdimid != 0) PERR;
if (nc_inq_var(ncid, 0, var_name_in, &xtype_in, &ndims,
dimids_in, &natts_in)) PERR;
if (strcmp(var_name_in, VAR_NAME) || xtype_in != NC_INT || ndims != NDIM3
|| dimids_in[0] != 0 || dimids_in[1] != 1 || dimids_in[2] != 2 ||
natts_in != 0) PERR;
if (nc_inq_dim(ncid, 0, dim_name_in, &dim_len_in)) PERR;
if (strcmp(dim_name_in, DIM_NAME_UNLIMITED) || dim_len_in != 1) PERR;
if (nc_inq_dim(ncid, 1, dim_name_in, &dim_len_in)) PERR;
if (strcmp(dim_name_in, DIM_NAME_X) || dim_len_in != DIM_LEN_X) PERR;
if (nc_inq_dim(ncid, 2, dim_name_in, &dim_len_in)) PERR;
if (strcmp(dim_name_in, DIM_NAME_Y) || dim_len_in != DIM_LEN_Y) PERR;

/* Read distributed arrays. */
if (!(data_in = malloc(elements_per_pe * sizeof(int)))) PERR;
if (nc_get_vard_int(ncid, varid, ioid, 0, data_in)) PERR;

/* Check results. */
for (i = 0; i < elements_per_pe; i++)
if (data_in[i] != my_data[i]) PERR;

/* Close file. */
if (nc_close(ncid)) PERR;

/* Free resources. */
free(data_in);
}

/* Release resources. */
free(my_data);
if (nc_free_decomp(ioid)) PERR;

return 0;
}

int
main(int argc, char **argv)
{
Expand All @@ -40,17 +142,9 @@ main(int argc, char **argv)
if (!my_rank)
printf("*** testing simple async use of netCDF integration layer...");
{
int ncid, ioid;
int dimid[NDIM3], varid;
int dimlen[NDIM3] = {NC_UNLIMITED, DIM_LEN_X, DIM_LEN_Y};
int iosysid;
size_t elements_per_pe;
size_t *compdof; /* The decomposition mapping. */
int *my_data;
int *data_in;
int num_procs2[COMPONENT_COUNT] = {3};
int num_io_procs = 1;
int i;

/* Turn on logging for PIO library. */
/* PIOc_set_log_level(4); */
Expand All @@ -66,85 +160,9 @@ main(int argc, char **argv)

if (my_rank)
{
/* Create a file with a 3D record var. */
if (nc_create(FILE_NAME, NC_PIO|NC_NETCDF4, &ncid)) PERR;
if (nc_def_dim(ncid, DIM_NAME_UNLIMITED, dimlen[0], &dimid[0])) PERR;
if (nc_def_dim(ncid, DIM_NAME_X, dimlen[1], &dimid[1])) PERR;
if (nc_def_dim(ncid, DIM_NAME_Y, dimlen[2], &dimid[2])) PERR;
if (nc_def_var(ncid, VAR_NAME, NC_INT, NDIM3, dimid, &varid)) PERR;
if (nc_enddef(ncid)) PERR;

/* Calculate a decomposition for distributed arrays. */
elements_per_pe = DIM_LEN_X * DIM_LEN_Y / (ntasks - num_io_procs);
/* printf("my_rank %d elements_per_pe %ld\n", my_rank, elements_per_pe); */
if (!(compdof = malloc(elements_per_pe * sizeof(size_t))))
PERR;
for (i = 0; i < elements_per_pe; i++)
{
compdof[i] = (my_rank - num_io_procs) * elements_per_pe + i;
/* printf("my_rank %d compdof[%d]=%ld\n", my_rank, i, compdof[i]); */
}

/* Create the PIO decomposition for this test. */
if (nc_def_decomp(iosysid, PIO_INT, NDIM2, &dimlen[1], elements_per_pe,
compdof, &ioid, 1, NULL, NULL)) PERR;
free(compdof);

/* Create some data on this processor. */
if (!(my_data = malloc(elements_per_pe * sizeof(int)))) PERR;
for (i = 0; i < elements_per_pe; i++)
my_data[i] = my_rank * 10 + i;

/* Write some data with distributed arrays. */
if (nc_put_vard_int(ncid, varid, ioid, 0, my_data)) PERR;
if (nc_close(ncid)) PERR;

/* Reopen the file using netCDF integration. */
{
int ndims, nvars, ngatts, unlimdimid;
nc_type xtype_in;
char var_name_in[NC_MAX_NAME + 1];
char dim_name_in[NC_MAX_NAME + 1];
int natts_in;
int dimids_in[NDIM3];
size_t dim_len_in;

/* Open the file. */
if (nc_open(FILE_NAME, NC_PIO, &ncid)) PERR;

/* Check the file. */
if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) PERR;
if (ndims != 3 || nvars != 1 || ngatts != 0 ||
unlimdimid != 0) PERR;
if (nc_inq_var(ncid, 0, var_name_in, &xtype_in, &ndims,
dimids_in, &natts_in)) PERR;
if (strcmp(var_name_in, VAR_NAME) || xtype_in != NC_INT || ndims != NDIM3
|| dimids_in[0] != 0 || dimids_in[1] != 1 || dimids_in[2] != 2 ||
natts_in != 0) PERR;
if (nc_inq_dim(ncid, 0, dim_name_in, &dim_len_in)) PERR;
if (strcmp(dim_name_in, DIM_NAME_UNLIMITED) || dim_len_in != 1) PERR;
if (nc_inq_dim(ncid, 1, dim_name_in, &dim_len_in)) PERR;
if (strcmp(dim_name_in, DIM_NAME_X) || dim_len_in != DIM_LEN_X) PERR;
if (nc_inq_dim(ncid, 2, dim_name_in, &dim_len_in)) PERR;
if (strcmp(dim_name_in, DIM_NAME_Y) || dim_len_in != DIM_LEN_Y) PERR;

/* Read distributed arrays. */
if (!(data_in = malloc(elements_per_pe * sizeof(int)))) PERR;
if (nc_get_vard_int(ncid, varid, ioid, 0, data_in)) PERR;

/* Check results. */
for (i = 0; i < elements_per_pe; i++)
if (data_in[i] != my_data[i]) PERR;

/* Close file. */
if (nc_close(ncid)) PERR;

/* Free resources. */
free(data_in);
}

free(my_data);
if (nc_free_decomp(ioid)) PERR;
/* Create a file, write some data, and check it. */
if (create_file_0(my_rank, ntasks, num_io_procs, iosysid)) PERR;

if (nc_free_iosystem(iosysid)) PERR;
}
}
Expand Down

0 comments on commit 94cdb68

Please sign in to comment.