h5u_write.c 14.4 KB
Newer Older
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
#include <stdlib.h>
#include "testframe.h"
#include "params.h"

static void
test_write_file_attribs(h5_file_t file, int position)
{
	h5_err_t status;
	char name[ATTR_NAME_SIZE];

	TEST("Writing file attributes");

	get_attr_name(name, "str", position);
	status = H5WriteFileAttribString(file, name, ATTR_STR_VAL);
	RETURN(status, H5_SUCCESS, "H5WriteFileAttribString");

	get_attr_name(name, "i32", position);
	h5_int32_t i32 = ATTR_INT32_VAL;
	status = H5WriteFileAttribInt32(file, name, &i32, 1);
	RETURN(status, H5_SUCCESS, "H5WriteFileAttribInt32");

	get_attr_name(name, "i64", position);
	h5_int64_t i64 = ATTR_INT64_VAL;
	status = H5WriteFileAttribInt64(file, name, &i64, 1);
	RETURN(status, H5_SUCCESS, "H5WriteFileAttribInt64");

	get_attr_name(name, "f32", position);
	h5_float32_t f32 = ATTR_FLOAT_VAL;
	status = H5WriteFileAttribFloat32(file, name, &f32, 1);
	RETURN(status, H5_SUCCESS, "H5WriteFileAttribFloat32");

	get_attr_name(name, "f64", position);
	h5_float64_t f64 = ATTR_FLOAT_VAL;
	status = H5WriteFileAttribFloat64(file, name, &f64, 1);
	RETURN(status, H5_SUCCESS, "H5WriteFileAttribFloat64");
}

static void
test_write_step_attribs(h5_file_t file, int position)
{
	h5_err_t status;
	char name[ATTR_NAME_SIZE];

gsell's avatar
gsell committed
44
	TEST("Writing step attributes");
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

	get_attr_name(name, "str", position);
	status = H5WriteStepAttribString(file, name, ATTR_STR_VAL);
	RETURN(status, H5_SUCCESS, "H5WriteStepAttribString");

	get_attr_name(name, "i32", position);
	h5_int32_t i32 = ATTR_INT32_VAL;
	status = H5WriteStepAttribInt32(file, name, &i32, 1);
	RETURN(status, H5_SUCCESS, "H5WriteStepAttribInt32");

	get_attr_name(name, "i64", position);
	h5_int64_t i64 = ATTR_INT64_VAL;
	status = H5WriteStepAttribInt64(file, name, &i64, 1);
	RETURN(status, H5_SUCCESS, "H5WriteStepAttribInt64");

	get_attr_name(name, "f32", position);
	h5_float32_t f32 = ATTR_FLOAT_VAL;
	status = H5WriteStepAttribFloat32(file, name, &f32, 1);
	RETURN(status, H5_SUCCESS, "H5WriteStepAttribFloat32");

	get_attr_name(name, "f64", position);
	h5_float64_t f64 = ATTR_FLOAT_VAL;
	status = H5WriteStepAttribFloat64(file, name, &f64, 1);
	RETURN(status, H5_SUCCESS, "H5WriteStepAttribFloat64");
}

static void
test_write_data64(h5_file_t file, int nparticles, int step)
{
	int i,t;
	h5_int64_t status, val;

	double *x,*y,*z;
	double *px,*py,*pz;
	h5_int64_t *id;

	x=(double*)malloc(nparticles*sizeof(double));
	y=(double*)malloc(nparticles*sizeof(double));
	z=(double*)malloc(nparticles*sizeof(double));
	px=(double*)malloc(nparticles*sizeof(double));
	py=(double*)malloc(nparticles*sizeof(double));
	pz=(double*)malloc(nparticles*sizeof(double));
	id=(h5_int64_t*)malloc(nparticles*sizeof(h5_int64_t));

	TEST("Writing 64-bit data");

	for (t=step; t<step+NTIMESTEPS; t++)
	{
		for (i=0; i<nparticles; i++)
		{
			x[i]  = 0.0 + (double)(i+nparticles*t);
			y[i]  = 0.1 + (double)(i+nparticles*t);
			z[i]  = 0.2 + (double)(i+nparticles*t);
			px[i] = 0.3 + (double)(i+nparticles*t);
			py[i] = 0.4 + (double)(i+nparticles*t);
			pz[i] = 0.5 + (double)(i+nparticles*t);
			id[i] = i + nparticles*t;
		}

		val = H5HasStep(file, t);

		status = H5SetStep(file, t);
		RETURN(status, H5_SUCCESS, "H5SetStep");

		if (val == 0) test_write_step_attribs(file, t);

		status = H5PartSetNumParticles(file, nparticles);
		RETURN(status, H5_SUCCESS, "H5PartSetNumParticles");

		status = H5PartWriteDataFloat64(file, "x", x);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataFloat64");

		status = H5PartWriteDataFloat64(file, "y", y);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataFloat64");

		status = H5PartWriteDataFloat64(file, "z", z);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataFloat64");

		status = H5PartWriteDataFloat64(file, "px", px);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataFloat64");

		status = H5PartWriteDataFloat64(file, "py", py);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataFloat64");

		status = H5PartWriteDataFloat64(file, "pz", pz);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataFloat64");

		status = H5PartWriteDataInt64(file, "id", id);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataInt64");
	}
}

static void
test_write_strided_data64(h5_file_t file, int nparticles, int step)
{
	int i,t;
	h5_int64_t status;

	double *data;

	data=(double*)malloc(6*nparticles*sizeof(double));

	status = H5PartSetNumParticlesStrided(file, nparticles, 6);
	RETURN(status, H5_SUCCESS, "H5PartSetNumParticlesStrided");

	TEST("Writing 64-bit strided data");

	for (t=step; t<step+NTIMESTEPS; t++)
	{
		for (i=0; i<nparticles; i++)
		{
			data[6*i]   = 0.0 + (double)(i+nparticles*t);
			data[6*i+1] = 0.1 + (double)(i+nparticles*t);
			data[6*i+2] = 0.2 + (double)(i+nparticles*t);
			data[6*i+3] = 0.3 + (double)(i+nparticles*t);
			data[6*i+4] = 0.4 + (double)(i+nparticles*t);
			data[6*i+5] = 0.5 + (double)(i+nparticles*t);
		}

		status = H5SetStep(file, t);
		RETURN(status, H5_SUCCESS, "H5SetStep");

		status = H5PartSetNumParticlesStrided(file, nparticles, 6);
		RETURN(status, H5_SUCCESS, "H5PartSetNumParticlesStrided");

		status = H5PartWriteDataFloat64(file, "x", data+0);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataFloat64");

		status = H5PartWriteDataFloat64(file, "y", data+1);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataFloat64");

		status = H5PartWriteDataFloat64(file, "z", data+2);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataFloat64");

		status = H5PartWriteDataFloat64(file, "px", data+3);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataFloat64");

		status = H5PartWriteDataFloat64(file, "py", data+4);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataFloat64");

		status = H5PartWriteDataFloat64(file, "pz", data+5);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataFloat64");

		test_write_step_attribs(file, t);
	}
}

static void
test_write_data32(h5_file_t file, int nparticles, int step)
{
	int i,t;
	h5_err_t status;
	h5_size_t val;

	float *x,*y,*z;
	float *px,*py,*pz;
	int *id;

	x=(float*)malloc(nparticles*sizeof(float));
	y=(float*)malloc(nparticles*sizeof(float));
	z=(float*)malloc(nparticles*sizeof(float));
	px=(float*)malloc(nparticles*sizeof(float));
	py=(float*)malloc(nparticles*sizeof(float));
	pz=(float*)malloc(nparticles*sizeof(float));
	id=(int*)malloc(nparticles*sizeof(int));

	status = H5PartSetNumParticles(file, nparticles);
	RETURN(status, H5_SUCCESS, "H5PartSetNumParticles");

214
#if defined(H5_HAVE_PARALLEL)
gsell's avatar
test/*:  
gsell committed
215
	int rank, nprocs;
216 217 218
	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
	MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
#else
gsell's avatar
test/*:  
gsell committed
219
	int rank = 0;
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
#endif

	TEST("Writing 32-bit data");

	for (t=step; t<step+NTIMESTEPS; t++)
	{
		for (i=0; i<nparticles; i++)
		{
			x[i]  = 0.0F + (float)(i+nparticles*t);
			y[i]  = 0.1F + (float)(i+nparticles*t);
			z[i]  = 0.2F + (float)(i+nparticles*t);
			px[i] = 0.3F + (float)(i+nparticles*t);
			py[i] = 0.4F + (float)(i+nparticles*t);
			pz[i] = 0.5F + (float)(i+nparticles*t);
			id[i] = i + nparticles*t;
		}

		val = H5HasStep(file, t);
		if (val == 0) {
			status = H5SetStep(file, t);
			RETURN(status, H5_SUCCESS, "H5SetStep");
		}

		/* test a two-part write using views */
		status = H5PartSetView(file,
		                       rank*nparticles,
		                       rank*nparticles + 31);
		RETURN(status, H5_SUCCESS, "H5PartSetView");

		status = H5PartWriteDataFloat32(file, "x", x);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataFloat32");

		test_write_step_attribs(file, t);

		status = H5PartWriteDataFloat32(file, "y", y);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataFloat32");

		status = H5PartWriteDataFloat32(file, "z", z);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataFloat32");

		status = H5PartWriteDataFloat32(file, "px", px);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataFloat32");

		status = H5PartWriteDataFloat32(file, "py", py);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataFloat32");

		status = H5PartWriteDataFloat32(file, "pz", pz);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataFloat32");

Gsell Achim's avatar
* C-API  
Gsell Achim committed
269
		status = H5PartWriteDataInt32(file, "id", id);
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
		RETURN(status, H5_SUCCESS, "H5PartWriteDataInt32");

		/* the second write phase... */
		status = H5PartSetView(file,
		                       rank*nparticles + 32,
		                       rank*nparticles + nparticles - 1);
		RETURN(status, H5_SUCCESS, "H5PartSetView");
		/* offset the input arrays */
		status = H5PartWriteDataFloat32(file, "x", x+32);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataFloat32");

		status = H5PartWriteDataFloat32(file, "y", y+32);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataFloat32");

		status = H5PartWriteDataFloat32(file, "z", z+32);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataFloat32");

		status = H5PartWriteDataFloat32(file, "px", px+32);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataFloat32");

		status = H5PartWriteDataFloat32(file, "py", py+32);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataFloat32");

		status = H5PartWriteDataFloat32(file, "pz", pz+32);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataFloat32");

Gsell Achim's avatar
* C-API  
Gsell Achim committed
296
		status = H5PartWriteDataInt32(file, "id", id+32);
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
		RETURN(status, H5_SUCCESS, "H5PartWriteDataInt32");
	}
}

static void
test_write_strided_data32(h5_file_t file, int nparticles, int step)
{
	int i,t;
	h5_int64_t status;

	float *data;

	data=(float*)malloc(6*nparticles*sizeof(float));

	TEST("Writing 32-bit strided data");

	for (t=step; t<step+NTIMESTEPS; t++)
	{
		for (i=0; i<nparticles; i++)
		{
			data[6*i]   = 0.0F + (float)(i+nparticles*t);
			data[6*i+1] = 0.1F + (float)(i+nparticles*t);
			data[6*i+2] = 0.2F + (float)(i+nparticles*t);
			data[6*i+3] = 0.3F + (float)(i+nparticles*t);
			data[6*i+4] = 0.4F + (float)(i+nparticles*t);
			data[6*i+5] = 0.5F + (float)(i+nparticles*t);
		}

		status = H5SetStep(file, t);
		RETURN(status, H5_SUCCESS, "H5SetStep");

		test_write_step_attribs(file, t);

		status = H5PartSetNumParticlesStrided(file, nparticles, 6);
		RETURN(status, H5_SUCCESS, "H5PartSetNumParticlesStrided");

		status = H5PartWriteDataFloat32(file, "x", data+0);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataFloat32");

		status = H5PartWriteDataFloat32(file, "y", data+1);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataFloat32");

		status = H5PartWriteDataFloat32(file, "z", data+2);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataFloat32");

		status = H5PartWriteDataFloat32(file, "px", data+3);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataFloat32");

		status = H5PartWriteDataFloat32(file, "py", data+4);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataFloat32");

		status = H5PartWriteDataFloat32(file, "pz", data+5);
		RETURN(status, H5_SUCCESS, "H5PartWriteDataFloat32");
	}
}

void h5u_test_write1(void)
{
	h5_file_t file1;
	h5_err_t status;

	TEST("Opening file once, write-truncate");
gsell's avatar
gsell committed
359
        h5_prop_t props = H5CreateFileProp ();
gsell's avatar
gsell committed
360

361
#if defined(H5_HAVE_PARALLEL)
gsell's avatar
gsell committed
362
        MPI_Comm comm = MPI_COMM_WORLD;
gsell's avatar
test/*:  
gsell committed
363 364
        status = H5SetPropFileMPIOCollective (props, &comm);
	RETURN(status, H5_SUCCESS, "H5SetPropFileMPIOCollective");
gsell's avatar
gsell committed
365
#endif
gsell's avatar
gsell committed
366
	file1 = H5OpenFile(FILENAME, H5_O_WRONLY, props);
367 368 369
	status = H5CheckFile(file1);
	RETURN(status, H5_SUCCESS, "H5CheckFile");

gsell's avatar
gsell committed
370 371 372
        status = H5CloseProp (props);
	RETURN(status, H5_SUCCESS, "H5CloseProp");

373 374 375 376 377 378 379 380 381 382 383 384 385 386
	test_write_data32(file1, NPARTICLES, 1);
	test_write_file_attribs(file1, 0);

	status = H5CloseFile(file1);
	RETURN(status, H5_SUCCESS, "H5CloseFile");
}

void h5u_test_write2(void)
{
	h5_file_t file1;
	h5_file_t file2;
	h5_err_t status;

	TEST("Opening file twice, write-append + read-only");
gsell's avatar
gsell committed
387
        h5_prop_t props = H5CreateFileProp ();
gsell's avatar
gsell committed
388

389
#if defined(H5_HAVE_PARALLEL)
gsell's avatar
gsell committed
390
        MPI_Comm comm = MPI_COMM_WORLD;
gsell's avatar
test/*:  
gsell committed
391 392
        status = H5SetPropFileMPIOCollective (props, &comm);
	RETURN(status, H5_SUCCESS, "H5SetPropFileMPIOCollective");
gsell's avatar
gsell committed
393
#endif
gsell's avatar
gsell committed
394
	file1 = H5OpenFile(FILENAME, H5_O_APPENDONLY, props);
395 396 397
	status = H5CheckFile(file1);
	RETURN(status, H5_SUCCESS, "H5CheckFile");

gsell's avatar
gsell committed
398
	file2 = H5OpenFile(FILENAME, H5_O_RDONLY, props);
399 400 401
	status = H5CheckFile(file2);
	RETURN(status, H5_SUCCESS, "H5CheckFile");

gsell's avatar
gsell committed
402 403 404
        status = H5CloseProp (props);
	RETURN(status, H5_SUCCESS, "H5CloseProp");

405 406 407 408 409 410 411 412 413 414 415 416 417 418
	test_write_strided_data32(file1, NPARTICLES, NTIMESTEPS+1);
	test_write_file_attribs(file1, 1);

	status = H5CloseFile(file1);
	RETURN(status, H5_SUCCESS, "H5CloseFile");
	status = H5CloseFile(file2);
	RETURN(status, H5_SUCCESS, "H5CloseFile");
}

void h5u_test_write3(void)
{
	h5_file_t file1;
	h5_err_t status;

419
#if H5_VERSION_LE(1,8,12)
420
	TEST("Opening file once, write-truncate, MPI-POSIX VFD");
421 422 423 424
#else
	TEST("Opening file once, write-truncate, MPI-IO Independent VFD");
#endif
	
gsell's avatar
gsell committed
425
        h5_prop_t props = H5CreateFileProp ();
gsell's avatar
gsell committed
426

427
#if defined(H5_HAVE_PARALLEL)
gsell's avatar
gsell committed
428
        MPI_Comm comm = MPI_COMM_WORLD;
429 430

#if H5_VERSION_LE(1,8,12)
431 432
        status = H5SetPropFileMPIOPosix (props, &comm);
	RETURN(status, H5_SUCCESS, "H5SetPropFileMPIOPosix");
433 434 435
#else
        status = H5SetPropFileMPIOIndependent (props, &comm);
	RETURN(status, H5_SUCCESS, "H5SetPropFileMPIOIndependent");
gsell's avatar
gsell committed
436
#endif
437 438 439

#endif // H5_HAVE_PARALLEL

440
	file1 = H5OpenFile(FILENAME, H5_O_WRONLY, props);
441 442 443 444

	status = H5CheckFile(file1);
	RETURN(status, H5_SUCCESS, "H5CheckFile");

gsell's avatar
gsell committed
445 446 447
        status = H5CloseProp (props);
	RETURN(status, H5_SUCCESS, "H5CloseProp");

448
	TEST("Redefining step name");
Gsell Achim's avatar
* C-API  
Gsell Achim committed
449
	status = H5SetStepNameFormat(file1, "data", 16);
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
	RETURN(status, H5_SUCCESS, "H5SetStepNameFormat");

	test_write_strided_data64(file1, NPARTICLES, 0);
	test_write_file_attribs(file1, 0);

	status = H5CloseFile(file1);
	RETURN(status, H5_SUCCESS, "H5CloseFile");
}

void h5u_test_write4(void)
{
	h5_file_t file1;
	h5_file_t file2;

	h5_err_t status;
gsell's avatar
gsell committed
465
        h5_prop_t props;
466 467

	TEST("Opening file twice, write-append + read-only, MPI-IO Independent VFD");
gsell's avatar
gsell committed
468

gsell's avatar
gsell committed
469
        props = H5CreateFileProp ();
470
#if defined(H5_HAVE_PARALLEL)
gsell's avatar
gsell committed
471
        MPI_Comm comm = MPI_COMM_WORLD;
472 473
        status = H5SetPropFileMPIOIndependent (props, &comm);
	RETURN(status, H5_SUCCESS, "H5SetPropFileMPIOIndependent");
gsell's avatar
gsell committed
474
#endif
475
	file1 = H5OpenFile(FILENAME, H5_O_APPENDONLY, props);
476 477 478
	status = H5CheckFile(file1);
	RETURN(status, H5_SUCCESS, "H5CheckFile");

479
	file2 = H5OpenFile(FILENAME, H5_O_RDONLY, props);
480 481 482
	status = H5CheckFile(file2);
	RETURN(status, H5_SUCCESS, "H5CheckFile");

gsell's avatar
gsell committed
483 484 485
        status = H5CloseProp (props);
	RETURN(status, H5_SUCCESS, "H5CloseProp");

486
	TEST("Redefining step name");
Gsell Achim's avatar
* C-API  
Gsell Achim committed
487
	status = H5SetStepNameFormat(file1, "data", 16);
488 489
	RETURN(status, H5_SUCCESS, "H5SetStepNameFormat");

Gsell Achim's avatar
* C-API  
Gsell Achim committed
490
	status = H5SetStepNameFormat(file2, "data", 16);
491 492 493 494 495 496
	RETURN(status, H5_SUCCESS, "H5SetStepNameFormat");

	status = H5PartSetChunkSize(file1, NPARTICLES);
	RETURN(status, H5_SUCCESS, "H5PartSetChunk");

	test_write_data64(file1, NPARTICLES, NTIMESTEPS-2);
gsell's avatar
gsell committed
497
	test_write_file_attribs(file1, 2);
498 499 500 501 502 503 504

	status = H5CloseFile(file1);
	RETURN(status, H5_SUCCESS, "H5CloseFile");
	status = H5CloseFile(file2);
	RETURN(status, H5_SUCCESS, "H5CloseFile");
}