1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
|
/* Copyright 2018, UCAR/Unidata See COPYRIGHT file for copying and
* redistribution conditions.
*
* This program tests netcdf-4 parallel I/O.
*
* Ed Hartnett
*/
#include <nc_tests.h>
#include "err_macros.h"
#include <mpi.h>
#define FILE "tst_parallel5.nc"
#define VAR_NAME "TheIrishRover"
#define DIM_NAME "number_of_masts"
#define MASTS 27
#define NDIMS1 1
#define DIMSIZE 4
#define NUM_PROC 4
#define NUM_SLABS 10
#define NUM_ACCESS_TESTS 2
int
main(int argc, char **argv)
{
int mpi_size, mpi_rank;
MPI_Comm comm = MPI_COMM_WORLD;
MPI_Info info = MPI_INFO_NULL;
int ncid, v1id, dimid;
size_t start[NDIMS1] = {0}, count[NDIMS1] = {0};
int data = MASTS;
int data_in = TEST_VAL_42;
int acc;
/* Initialize MPI. */
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
/* Require exactly 4 tasks. */
if (mpi_size != NUM_PROC) ERR;
if (!mpi_rank)
printf("\n*** Testing parallel I/O.\n");
if (!mpi_rank)
printf("*** testing whether we can write 0 elements from some tasks...");
{
for (acc = 0; acc < NUM_ACCESS_TESTS; acc++)
{
/* Create a parallel netcdf-4 file. */
/*nc_set_log_level(3);*/
if (nc_create_par(FILE, NC_NETCDF4, comm, info, &ncid)) ERR;
/* Create a dimension. */
if (nc_def_dim(ncid, DIM_NAME, DIMSIZE, &dimid)) ERR;
/* Create one var. */
if (nc_def_var(ncid, VAR_NAME, NC_INT, NDIMS1, &dimid, &v1id)) ERR;
/* Write metadata to file. */
if (nc_enddef(ncid)) ERR;
/* Set up slab for this process. */
if (!mpi_rank)
count[0] = 1;
if (nc_var_par_access(ncid, v1id, acc ? NC_COLLECTIVE : NC_INDEPENDENT)) ERR;
/* Write phoney data. */
if (nc_put_vara_int(ncid, v1id, start, count, &data)) ERR;
if (nc_sync(ncid)) ERR;
/* Read phoney data. */
if (nc_get_vara_int(ncid, v1id, start, count, &data_in)) ERR;
/* Task 0 has MASTS, the others have data_in remaining, as
* initialized, at TEST_VAL_42. */
if (data_in != (mpi_rank ? TEST_VAL_42 : MASTS)) ERR;
/* Close the netcdf file. */
if (nc_close(ncid)) ERR;
}
}
if (!mpi_rank)
SUMMARIZE_ERR;
if (!mpi_rank)
printf("*** testing enum type and parallel I/O...");
{
for (acc = 0; acc < NUM_ACCESS_TESTS; acc++)
{
#define ENUM_NAME "cargo"
#define ENUM_VAR_NAME "in_the_hold_of_the_Irish_Rover"
#define NUM_ENUM_FIELDS 8
int typeid;
int f;
char field_name[NUM_ENUM_FIELDS][NC_MAX_NAME + 1] = {"bags of the best Sligo rags", "barrels of bones",
"bails of old nanny goats' tails", "barrels of stones",
"dogs", "hogs", "barrels of porter",
"sides of old blind horses hides"};
unsigned long long field_value[NUM_ENUM_FIELDS] = {1000000, 2000000, 3000000, 4000000,
5000000, 6000000, 7000000, 8000000};
unsigned long long data = 1000000, data_in = TEST_VAL_42;
/* Create a parallel netcdf-4 file. */
/*nc_set_log_level(3);*/
if (nc_create_par(FILE, NC_NETCDF4, comm, info, &ncid)) ERR;
/* Create a dimension. */
if (nc_def_dim(ncid, DIM_NAME, DIMSIZE, &dimid)) ERR;
/* Create an enum type. */
if (nc_def_enum(ncid, NC_UINT64, ENUM_NAME, &typeid)) ERR;
for (f = 0; f < NUM_ENUM_FIELDS; f++)
if (nc_insert_enum(ncid, typeid, field_name[f], &field_value[f])) ERR;
/* Create one var. */
if (nc_def_var(ncid, ENUM_VAR_NAME, typeid, NDIMS1, &dimid, &v1id)) ERR;
/* Write metadata to file. */
if (nc_enddef(ncid)) ERR;
/* Set up slab for this process. */
if (!mpi_rank)
count[0] = 1;
if (nc_var_par_access(ncid, v1id, acc ? NC_COLLECTIVE : NC_INDEPENDENT)) ERR;
/* Write phoney data. */
if (nc_put_vara(ncid, v1id, start, count, &data)) ERR;
if (nc_sync(ncid)) ERR;
/* Read phoney data. */
if (nc_get_vara(ncid, v1id, start, count, &data_in)) ERR;
/* Task 0 has 1000000, the others have data_in remaining, as
* initialized, at TEST_VAL_42. */
if (data_in != (mpi_rank ? TEST_VAL_42 : 1000000)) ERR;
/* Close the netcdf file. */
if (nc_close(ncid)) ERR;
}
}
if (!mpi_rank)
SUMMARIZE_ERR;
if (!mpi_rank)
printf("*** testing compound type and parallel I/O...");
{
for (acc = 0; acc < NUM_ACCESS_TESTS; acc++)
{
#define COMPOUND_NAME "crew_info"
#define COMPOUND_VAR_NAME "whale_of_a_crew"
#define NUM_CREW 5
#define CREW_DIM_NAME "number_of_crew"
int typeid;
struct crew
{
char name[NC_MAX_NAME + 1];
char description[NC_MAX_NAME + 1];
char origin[NC_MAX_NAME + 1];
int age;
};
struct crew data = {"Mick McCann", "the skipper of the Irish Rover",
"from the banks of the Bann", 42};
struct crew data_in = {"", "", "", -42};
int dim_size = NC_MAX_NAME + 1;
/* Create a parallel netcdf-4 file. */
/*nc_set_log_level(3);*/
if (nc_create_par(FILE, NC_NETCDF4, comm, info, &ncid)) ERR;
/* Create a dimension. */
if (nc_def_dim(ncid, CREW_DIM_NAME, NUM_CREW, &dimid)) ERR;
/* Create a compound type. */
if (nc_def_compound(ncid, sizeof(struct crew), COMPOUND_NAME, &typeid)) ERR;
if (nc_insert_array_compound(ncid, typeid, "name", NC_COMPOUND_OFFSET(struct crew, name), NC_CHAR, 1, &dim_size)) ERR;
if (nc_insert_array_compound(ncid, typeid, "description", NC_COMPOUND_OFFSET(struct crew, description), NC_CHAR, 1, &dim_size)) ERR;
if (nc_insert_array_compound(ncid, typeid, "origin", NC_COMPOUND_OFFSET(struct crew, origin), NC_CHAR, 1, &dim_size)) ERR;
if (nc_insert_compound(ncid, typeid, "age", NC_COMPOUND_OFFSET(struct crew, age), NC_INT)) ERR;
/* Create one var. */
if (nc_def_var(ncid, COMPOUND_VAR_NAME, typeid, NDIMS1, &dimid, &v1id)) ERR;
/* Write metadata to file. */
if (nc_enddef(ncid)) ERR;
/* Set up slab for this process. */
if (!mpi_rank)
count[0] = 1;
if (nc_var_par_access(ncid, v1id, acc ? NC_COLLECTIVE : NC_INDEPENDENT)) ERR;
/* Write phoney data. */
if (nc_put_vara(ncid, v1id, start, count, &data)) ERR;
if (nc_sync(ncid)) ERR;
/* Read phoney data. */
if (nc_get_vara(ncid, v1id, start, count, &data_in)) ERR;
/* Task 0 has data, the others have nothing. */
if (!mpi_rank)
{
if (strcmp(data_in.name, data.name) || strcmp(data_in.description, data.description) ||
strcmp(data_in.origin, data.origin) || data_in.age != data.age) ERR;
}
else
{
if (strcmp(data_in.name, "") || strcmp(data_in.description, "") ||
strcmp(data_in.origin, "") || data_in.age != -42) ERR;
}
/* Close the netcdf file. */
if (nc_close(ncid)) ERR;
}
}
if (!mpi_rank)
SUMMARIZE_ERR;
if (!mpi_rank)
printf("*** testing string type and parallel I/O...");
{
for (acc = 0; acc < NUM_ACCESS_TESTS; acc++)
{
#define STORY_VAR_NAME "fate_of_the_Irish_Rover"
#define STORY_DIM_NAME "number_of_lines"
#define STORY_LEN 8
char *story[STORY_LEN] = {"We had sailed seven years when the measles broke out",
"And the ship lost it's way in the fog",
"And that whale of the crew was reduced down to two",
"Just myself and the captain's old dog",
"Then the ship struck a rock, oh Lord what a shock",
"The bulkhead was turned right over",
"Turned nine times around, and the poor dog was drowned",
"I'm the last of the Irish Rover"};
char *story_in[STORY_LEN];
int s;
/* Create a netcdf-4 file. Turns out that HDF5 does not
* support VLEN writes with parallel I/O. Strings are
* VLENS. So here I write a file with task 0 and then read it
* with all tasks. */
if (!mpi_rank)
{
if (nc_create(FILE, NC_NETCDF4, &ncid)) ERR;
/* Create a dimension. */
if (nc_def_dim(ncid, STORY_DIM_NAME, STORY_LEN, &dimid)) ERR;
/* Create one var. */
if (nc_def_var(ncid, STORY_VAR_NAME, NC_STRING, NDIMS1, &dimid, &v1id)) ERR;
/* Write metadata to file. */
if (nc_enddef(ncid)) ERR;
/* Set up slab for this process. */
count[0] = STORY_LEN;
/* Write phoney data. */
if (nc_put_vara(ncid, v1id, start, count, story)) ERR;
/* Close the netcdf file. */
if (nc_close(ncid)) ERR;
}
/* Now try parallel read. */
if (nc_open_par(FILE, 0, comm, info, &ncid)) ERR;
/* Task 0 reads all 8 lines, other tasks read 0. */
if (nc_get_vara(ncid, v1id, start, count, story_in)) ERR;
if (!mpi_rank)
{
for (s = 0; s < STORY_LEN; s++)
if (strcmp(story_in[s], story[s])) ERR;
if (nc_free_string(STORY_LEN, (char **)story_in)) ERR;
}
/* Close the netcdf file. */
if (nc_close(ncid)) ERR;
}
}
if (!mpi_rank)
SUMMARIZE_ERR;
if (!mpi_rank)
printf("*** testing NC_BYTE type and parallel I/O...");
{
/* This test is related to
* https://github.com/Unidata/netcdf-c/issues/1462. */
int ncid, varid;
signed char test_data_in, test_data = 42;
/* Crate a file with a scalar NC_BYTE value. */
if (nc_create_par(FILE, NC_NETCDF4, MPI_COMM_WORLD, MPI_INFO_NULL,
&ncid)) ERR;
if (nc_def_var(ncid, "fred", NC_BYTE, 0, NULL, &varid)) ERR;
if (nc_enddef(ncid)) ERR;
if (nc_put_var_schar(ncid, varid, &test_data)) ERR;
if (nc_close(ncid)) ERR;
/* Reopen the file and check. */
if (nc_open_par(FILE, 0, comm, info, &ncid)) ERR;
if (nc_get_var_schar(ncid, varid, &test_data_in)) ERR;
if (test_data_in != test_data) ERR;
if (nc_close(ncid)) ERR;
}
if (!mpi_rank)
SUMMARIZE_ERR;
#ifdef USE_SZIP
#ifdef HDF5_SUPPORTS_PAR_FILTERS
#define SZIP_DIM_LEN 256
#define SZIP_DIM_NAME "Barrels"
#define SZIP_VAR_NAME "Best_Sligo_Rags"
#define SZIP_PIXELS_PER_BLOCK 32
if (!mpi_rank)
printf("*** testing szip compression with parallel I/O...");
{
int ncid, dimid, varid;
float *data;
float *data_in;
int elements_per_pe = SZIP_DIM_LEN/mpi_size;
size_t start[NDIMS1], count[NDIMS1];
int i;
/* Create test data. */
if (!(data = malloc(elements_per_pe * sizeof(float)))) ERR;
for (i = 0; i < elements_per_pe; i++)
data[i] = mpi_rank + i * 0.1;
/* Crate a file with a scalar NC_BYTE value. */
if (nc_create_par(FILE, NC_NETCDF4, MPI_COMM_WORLD, MPI_INFO_NULL,
&ncid)) ERR;
if (nc_def_dim(ncid, SZIP_DIM_NAME, SZIP_DIM_LEN, &dimid)) ERR;
if (nc_def_var(ncid, SZIP_VAR_NAME, NC_FLOAT, NDIMS1, &dimid, &varid)) ERR;
if (nc_def_var_szip(ncid, varid, H5_SZIP_NN_OPTION_MASK,
SZIP_PIXELS_PER_BLOCK)) ERR;
if (nc_enddef(ncid)) ERR;
start[0] = mpi_rank * elements_per_pe;
count[0] = elements_per_pe;
if (nc_put_vara_float(ncid, varid, start, count, data)) ERR;
if (nc_close(ncid)) ERR;
/* Reopen the file and check. */
if (nc_open_par(FILE, 0, comm, info, &ncid)) ERR;
if (!(data_in = malloc(elements_per_pe * sizeof(float)))) ERR;
if (nc_get_vara_float(ncid, varid, start, count, data_in)) ERR;
if (nc_close(ncid)) ERR;
for (i = 0; i < elements_per_pe; i++)
if (data_in[i] != data[i]) ERR;
/* Release resources. */
free(data_in);
free(data);
}
if (!mpi_rank)
SUMMARIZE_ERR;
#endif /* HDF5_SUPPORTS_PAR_FILTERS */
#endif /* USE_SZIP */
/* Shut down MPI. */
MPI_Finalize();
if (!mpi_rank)
FINAL_RESULTS;
return 0;
}
|