summaryrefslogtreecommitdiffstats
path: root/src/H5Farray.c
blob: 0bbb355036982d09ae38a6de2217f541bfb0d0a5 (plain)
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
/*
 * Copyright (C) 1998 Spizella Software
 *                    All rights reserved.
 *
 * Programmer:  Robb Matzke <robb@arborea.spizella.com>
 *              Thursday, January 15, 1998
 *
 * Purpose:	Provides I/O facilities for multi-dimensional arrays of bytes
 *		stored with various layout policies.  If the caller is
 *		interested in arrays of elements >1 byte then add an extra
 *		dimension.  For example, a 10x20 array of int32 would
 *		translate to a 10x20x4 array of bytes at this level.
 */
#include <H5private.h>
#include <H5Dprivate.h>
#include <H5Eprivate.h>
#include <H5Fprivate.h>
#include <H5MFprivate.h>
#include <H5Oprivate.h>
#include <H5Vprivate.h>

/* Interface initialization */
#define PABLO_MASK	H5F_arr_mask
#define INTERFACE_INIT	NULL
static intn interface_initialize_g = FALSE;



/*-------------------------------------------------------------------------
 * Function:	H5F_arr_create
 *
 * Purpose:	Creates an array of bytes.
 *
 * Return:	Success:	SUCCEED
 *
 *		Failure:	FAIL
 *
 * Programmer:	Robb Matzke
 *              Friday, January 16, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5F_arr_create (H5F_t *f, struct H5O_layout_t *layout/*in,out*/)
{
    intn		i;
    size_t	nbytes;
   
    FUNC_ENTER (H5F_arr_create, FAIL);

    /* check args */
    assert (f);
    assert (layout);
    H5F_addr_undef (&(layout->addr)); /*just in case we fail*/
   
    switch (layout->type) {
    case H5D_CONTIGUOUS:
	/* Reserve space in the file for the entire array */
	for (i=0, nbytes=1; i<layout->ndims; i++) nbytes *= layout->dim[i];
	assert (nbytes>0);
	if (H5MF_alloc (f, H5MF_RAW, nbytes, &(layout->addr)/*out*/)<0) {
	    HRETURN_ERROR (H5E_IO, H5E_NOSPACE, FAIL,
			   "unable to reserve file space");
	}
	break;

    case H5D_CHUNKED:
	/* Create the root of the B-tree that describes chunked storage */
	if (H5F_istore_create (f, layout/*out*/)<0) {
	    HRETURN_ERROR (H5E_IO, H5E_CANTINIT, FAIL,
			   "unable to initialize chunked storage");
	}
	break;

    default:
	assert ("not implemented yet" && 0);
	HRETURN_ERROR (H5E_IO, H5E_UNSUPPORTED, FAIL,
		       "unsupported storage layout");
	break;
    }

    FUNC_LEAVE (SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5F_arr_read
 *
 * Purpose:	Reads a hyperslab of a file byte array into a hyperslab of
 *		a byte array in	memory.  The data is read from file F and the
 *		array's size and storage information is in LAYOUT.  The
 *		hyperslab offset is FILE_OFFSET[] in the file and
 *		MEM_OFFSET[] in memory (offsets are relative to the origin of
 *		the array) and the size of the hyperslab is HSLAB_SIZE[]. The
 *		total size of the file array is implied in the LAYOUT
 *		argument and the total size of the memory array is
 *		MEM_SIZE[]. The dimensionality of these vectors is implied by
 *		the LAYOUT argument.
 *
 * Return:	Success:	SUCCEED
 *
 *		Failure:	FAIL
 *
 * Programmer:	Robb Matzke
 *              Friday, January 16, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5F_arr_read (H5F_t *f, const struct H5O_layout_t *layout,
	      const size_t _hslab_size[], const size_t mem_size[],
	      const size_t mem_offset[], const size_t file_offset[],
	      void *_buf/*out*/)
{
    uint8	*buf = (uint8 *)_buf;		/*cast for arithmetic	*/
    intn	file_stride[H5O_LAYOUT_NDIMS];	/*strides through file	*/
    intn	mem_stride[H5O_LAYOUT_NDIMS];	/*strides through memory*/
    size_t	hslab_size[H5O_LAYOUT_NDIMS];	/*hyperslab size	*/
    size_t	idx[H5O_LAYOUT_NDIMS];		/*multi-dim counter	*/
    size_t	mem_start, file_start;		/*byte offsets to start	*/
    size_t	elmt_size = 1;			/*bytes per element	*/
    intn	nelmts;				/*number of elements	*/
    intn	ndims;				/*stride dimensionality	*/
    haddr_t	addr;				/*address in file	*/
    intn	i, j;				/*counters		*/
    hbool_t	carray;				/*carry for subtraction	*/
   
    FUNC_ENTER (H5F_arr_read, FAIL);

    /* Check args */
    assert (f);
    assert (layout);
    assert (_hslab_size);
    assert (file_offset);
    assert (mem_offset);
    assert (mem_size);
    assert (buf);

    /* Make a local copy of size so we can modify it */
    H5V_vector_cpy (layout->ndims, hslab_size, _hslab_size);

    switch (layout->type) {
    case H5D_CONTIGUOUS:
	/*
	 * Calculate the strides needed to walk through the array on disk
	 * and memory. Optimize the strides to result in the fewest number of
	 * I/O requests.
	 */
	ndims = layout->ndims;
	mem_start = H5V_hyper_stride (ndims, hslab_size, mem_size,
				      mem_offset, mem_stride/*out*/);
	file_start = H5V_hyper_stride (ndims, hslab_size, layout->dim,
				       file_offset, file_stride/*out*/);
	H5V_stride_optimize2 (&ndims, &elmt_size, hslab_size,
			      mem_stride, file_stride);

	/*
	 * Initialize loop variables.  The loop is a multi-dimensional loop
	 * that counts from SIZE down to zero and IDX is the counter.  Each
	 * element of IDX is treated as a digit with IDX[0] being the least
	 * significant digit.
	 */
	H5V_vector_cpy (ndims, idx, hslab_size);
	nelmts = H5V_vector_reduce_product (ndims, hslab_size);
	addr = layout->addr;
	H5F_addr_inc (&addr, file_start);
	buf += mem_start;

	/*
	 * Now begin to walk through the array, copying data from disk to
	 * memory.
	 */
	for (i=0; i<nelmts; i++) {

	    /* Read from file */
	    if (H5F_block_read (f, &addr, elmt_size, buf)<0) {
		HRETURN_ERROR (H5E_IO, H5E_READERROR, FAIL,
			       "block read failed");
	    }

	    /* Decrement indices and advance pointers */
	    for (j=ndims-1, carray=TRUE; j>=0 && carray; --j) {
		
		H5F_addr_inc (&addr, file_stride[j]);
		buf += mem_stride[j];

		if (--idx[j]) carray = FALSE;
		else idx[j] = hslab_size[j];
	    }
	}
	break;

    case H5D_CHUNKED:
	/*
	 * This method is unable to copy into a proper hyperslab.
	 */
	for (i=0; i<layout->ndims; i++) {
	    if (0!=mem_offset[i] || hslab_size[i]!=mem_size[i]) {
		HRETURN_ERROR (H5E_IO, H5E_UNSUPPORTED, FAIL,
			       "unable to copy into a proper hyperslab");
	    }
	}
	if (H5F_istore_read (f, layout, file_offset, hslab_size, buf)<0) {
	    HRETURN_ERROR (H5E_IO, H5E_READERROR, FAIL, "chunked read failed");
	}
	break;

    default:
	assert ("not implemented yet" && 0);
	HRETURN_ERROR (H5E_IO, H5E_UNSUPPORTED, FAIL,
		       "unsupported storage layout");
	break;
    }

    FUNC_LEAVE (SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5F_arr_write
 *
 * Purpose:	Copies a hyperslab of a memory array to a hyperslab of a
 *		file array.  The data is written to file F and the file
 *		array's size and storage information is implied by LAYOUT.
 *		The hyperslab offset is FILE_OFFSET[] in the file and
 *		MEM_OFFSET[] in memory (offsets are relative to the origin of
 *		the array) and the size of the hyperslab is HSLAB_SIZE[].
 *		The total size of the file array is implied by the LAYOUT
 *		argument and the total size of the memory array is
 *		MEM_SIZE[].  The dimensionality of these vectors is implied
 *		by the LAYOUT argument.
 *
 * Return:	Success:	SUCCEED
 *
 *		Failure:	FAIL
 *
 * Programmer:	Robb Matzke
 *              Friday, January 16, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5F_arr_write (H5F_t *f, const struct H5O_layout_t *layout,
	       const size_t _hslab_size[], const size_t mem_size[],
	       const size_t mem_offset[], const size_t file_offset[],
	       const void *_buf)
{
    const uint8	*buf = (const uint8 *)_buf;	/*cast for arithmetic	*/
    intn	file_stride[H5O_LAYOUT_NDIMS];	/*strides through file	*/
    intn	mem_stride[H5O_LAYOUT_NDIMS];	/*strides through memory*/
    size_t	hslab_size[H5O_LAYOUT_NDIMS];	/*hyperslab size	*/
    size_t	idx[H5O_LAYOUT_NDIMS];		/*multi-dim counter	*/
    size_t	mem_start, file_start;		/*byte offsets to start	*/
    size_t	elmt_size = 1;			/*bytes per element	*/
    intn	nelmts;				/*number of elements	*/
    intn	ndims;				/*dimensionality	*/
    haddr_t	addr;				/*address in file	*/
    intn	i, j;				/*counters		*/
    hbool_t	carray;				/*carry for subtraction	*/
   
    FUNC_ENTER (H5F_arr_write, FAIL);

    /* Check args */
    assert (f);
    assert (layout);
    assert (_hslab_size);
    assert (file_offset);
    assert (mem_offset);
    assert (mem_size);
    assert (buf);

    /* Make a local copy of _size so we can modify it */
    H5V_vector_cpy (layout->ndims, hslab_size, _hslab_size);


    switch (layout->type) {
    case H5D_CONTIGUOUS:
	/*
	 * Calculate the strides needed to walk through the array on disk.
	 * Optimize the strides to result in the fewest number of I/O
	 * requests.
	 */
	ndims = layout->ndims;
	mem_start = H5V_hyper_stride (ndims, hslab_size, mem_size,
				      mem_offset, mem_stride/*out*/);
	file_start = H5V_hyper_stride (ndims, hslab_size, layout->dim,
				       file_offset, file_stride/*out*/);
	H5V_stride_optimize2 (&ndims, &elmt_size, hslab_size,
			      mem_stride, file_stride);

	/*
	 * Initialize loop variables.  The loop is a multi-dimensional loop
	 * that counts from SIZE down to zero and IDX is the counter.  Each
	 * element of IDX is treated as a digit with IDX[0] being the least
	 * significant digit.
	 */
	H5V_vector_cpy (ndims, idx, hslab_size);
	nelmts = H5V_vector_reduce_product (ndims, hslab_size);
	addr = layout->addr;
	H5F_addr_inc (&addr, file_start);
	buf += mem_start;

	/*
	 * Now begin to walk through the array, copying data from memory to
	 * disk.
	 */
	for (i=0; i<nelmts; i++) {

	    /* Write to file */
	    if (H5F_block_write (f, &addr, elmt_size, buf)<0) {
		HRETURN_ERROR (H5E_IO, H5E_WRITEERROR, FAIL,
			       "block write failed");
	    }

	    /* Decrement indices and advance pointers */
	    for (j=ndims-1, carray=TRUE; j>=0 && carray; --j) {
		
		H5F_addr_inc (&addr, file_stride[j]);
		buf += mem_stride[j];
		
		if (--idx[j]) carray = FALSE;
		else idx[j] = hslab_size[j];
	    }

	}
	break;

    case H5D_CHUNKED:
	/*
	 * This method is unable to copy from a proper hyperslab.
	 */
	for (i=0; i<layout->ndims; i++) {
	    if (0!=mem_offset[i] || hslab_size[i]!=mem_size[i]) {
		HRETURN_ERROR (H5E_IO, H5E_UNSUPPORTED, FAIL,
			       "unable to copy from a proper hyperslab");
	    }
	}
	if (H5F_istore_write (f, layout, file_offset, hslab_size, buf)<0) {
	    HRETURN_ERROR (H5E_IO, H5E_WRITEERROR, FAIL,
			   "chunked write failed");
	}
	break;

    default:
	assert ("not implemented yet" && 0);
	HRETURN_ERROR (H5E_IO, H5E_UNSUPPORTED, FAIL,
		       "unsupported storage layout");
	break;
    }

    FUNC_LEAVE (SUCCEED);
}