h5b_write.c 6.17 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 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
#include <stdlib.h>
#include "testframe.h"
#include "params.h"

static void
test_write_field_attribs(
	h5_file_t file,
	const char *field_name,
	int position)
{
	h5_err_t status;
	char name[ATTR_NAME_SIZE];

	TEST("Writing field attributes");

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

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

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

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

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

static void
test_write_data64(h5_file_t file, int step)
{
	extern h5_size_t layout[6];

	int i,t;
	h5_int64_t status, val;

	double *e;
	double *ex,*ey,*ez;
	h5_int64_t *id;

	const size_t nelems =
	        (layout[1] - layout[0] + 1) *
	        (layout[3] - layout[2] + 1) *
	        (layout[5] - layout[4] + 1);

	e=(double*)malloc(nelems*sizeof(double));
	ex=(double*)malloc(nelems*sizeof(double));
	ey=(double*)malloc(nelems*sizeof(double));
	ez=(double*)malloc(nelems*sizeof(double));
	id=(h5_int64_t*)malloc(nelems*sizeof(h5_int64_t));

	TEST("Writing 64-bit data");

	for (t=step; t<step+NTIMESTEPS; t++)
	{
		for (i=0; i<nelems; i++)
		{
			e[i]   = 0.0 + (double)(i+nelems*t);
			ex[i]  = 0.1 + (double)(i+nelems*t);
			ey[i]  = 0.2 + (double)(i+nelems*t);
			ez[i]  = 0.3 + (double)(i+nelems*t);
			id[i] = i + nelems*t;
		}

		val = H5HasStep(file, t);

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

		if (val == 0) test_write_field_attribs(file, "e", t);

		status = H5Block3dSetView(file,
		                          layout[0], layout[1],
		                          layout[2], layout[3],
		                          layout[4], layout[5]);
		RETURN(status, H5_SUCCESS, "H5Block3dSetView");

		status = H5Block3dWriteScalarFieldFloat64(file, "e", e);
		RETURN(status, H5_SUCCESS, "H5Block3dWriteScalarFieldFloat64");

		status = H5Block3dWriteVector3dFieldFloat64(file,
		                                            "E", ex, ey, ez);
		RETURN(status, H5_SUCCESS,
		       "H5Block3dWriteVector3dFieldFloat64");

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

static void
test_write_data32(h5_file_t file, int step)
{
	int i,j,k,t;
	h5_int64_t status, val;

	float *e;
	float *ex,*ey,*ez;
	int *id;

	size_t nelems = NBLOCKX * (NBLOCKY+2) * (NBLOCKZ+4);

	e=(float*)malloc(nelems*sizeof(double));
	ex=(float*)malloc(nelems*sizeof(double));
	ey=(float*)malloc(nelems*sizeof(double));
	ez=(float*)malloc(nelems*sizeof(double));
	id=(int*)malloc(nelems*sizeof(int));

	nelems = NBLOCKX * NBLOCKY * NBLOCKZ;

	TEST("Writing 32-bit data");

	for (t=step; t<step+NTIMESTEPS; t++)
	{
		for (k=0; k<NBLOCKZ+4; k++)
			for (j=0; j<NBLOCKY+2; j++)
				for (i=0; i<NBLOCKX; i++)
				{
					int idx = i + j*NBLOCKX + k*NBLOCKX*(NBLOCKY+2);
					int n = i + (j-1)*NBLOCKX + (k-2)*NBLOCKX*NBLOCKY;
					e[idx]   = 0.0f + (float)(n+nelems*t);
					ex[idx]  = 0.1f + (float)(n+nelems*t);
					ey[idx]  = 0.2f + (float)(n+nelems*t);
					ez[idx]  = 0.3f + (float)(n+nelems*t);
					id[idx] = n + nelems*t;
				}

		val = H5HasStep(file, t);

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

		if (val == 0) test_write_field_attribs(file, "e", t);

152
#if defined(H5_HAVE_PARALLEL)
gsell's avatar
test/*:  
gsell committed
153 154
		extern h5_size_t grid[3];

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
		status = H5Block3dSetGrid(file, grid[0], grid[1], grid[2]);
		RETURN(status, H5_SUCCESS, "H5Block3dSetGrid");

		status = H5Block3dSetDims(file, NBLOCKX, NBLOCKY, NBLOCKZ);
		RETURN(status, H5_SUCCESS, "H5Block3dSetDims");
#endif
		status = H5Block3dSetHalo(file, 0, 1, 2);
		RETURN(status, H5_SUCCESS, "H5Block3dSetHalo");

		status = H5Block3dWriteScalarFieldFloat32(file, "e", e);
		RETURN(status, H5_SUCCESS, "H5Block3dWriteScalarFieldFloat32");

		status = H5Block3dWriteVector3dFieldFloat32(file,
		                                            "E", ex, ey, ez);
		RETURN(status, H5_SUCCESS,
		       "H5Block3dWriteVector3dFieldFloat32");

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

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

	TEST("Opening file once, write-truncate");
gsell's avatar
gsell committed
183
        h5_prop_t props = H5CreateFileProp ();
184
#if defined (H5_HAVE_PARALLEL)
gsell's avatar
gsell committed
185
        MPI_Comm comm = MPI_COMM_WORLD;
gsell's avatar
test/*:  
gsell committed
186 187
        status = H5SetPropFileMPIOCollective (props, &comm);
	RETURN(status, H5_SUCCESS, "H5SetPropFileMPIOCollective");
gsell's avatar
gsell committed
188 189
        status = H5SetPropFileThrottle (props, 2);
	RETURN(status, H5_SUCCESS, "H5SetPropFileThrottle");
gsell's avatar
gsell committed
190
#endif
gsell's avatar
gsell committed
191
	file1 = H5OpenFile(FILENAME, H5_O_WRONLY, props);
192 193 194
	status = H5CheckFile(file1);
	RETURN(status, H5_SUCCESS, "H5CheckFile");

gsell's avatar
gsell committed
195 196 197
        status = H5CloseProp (props);
	RETURN(status, H5_SUCCESS, "H5CloseProp");

198 199 200 201 202 203 204 205 206 207 208 209 210 211
	test_write_data64(file1, 1);

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

void h5b_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
212
	file1 = H5OpenFile(FILENAME, H5_O_APPENDONLY, H5_PROP_DEFAULT);
213 214 215 216

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

gsell's avatar
gsell committed
217
	file2 = H5OpenFile(FILENAME, H5_O_RDONLY, H5_PROP_DEFAULT);
218 219 220 221 222 223 224 225 226 227 228

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

	test_write_data32(file1, NTIMESTEPS+1);

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