summaryrefslogtreecommitdiffstats
path: root/src/H5Farray.c
blob: e58131514645058215607ba97bfa5078ec18c324 (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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
/*
 * 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 int would
 *		translate to a 10x20x4 array of bytes at this level.
 */
#include <H5private.h>
#include <H5Dprivate.h>
#include <H5Eprivate.h>
#include <H5Fprivate.h>
#include <H5FDprivate.h>	/*file driver				  */
#include <H5Iprivate.h>
#include <H5MFprivate.h>
#include <H5MMprivate.h>	/*memory management			  */
#include <H5Oprivate.h>
#include <H5Pprivate.h>
#include <H5Vprivate.h>

/* MPIO driver functions are needed for some special checks */
#include <H5FDmpio.h>

/* Interface initialization */
#define PABLO_MASK	H5Farray_mask
#define INTERFACE_INIT	NULL
static intn interface_initialize_g = 0;



/*-------------------------------------------------------------------------
 * Function:	H5F_arr_create
 *
 * Purpose:	Creates an array of bytes.
 *
 * Return:	Non-negative on success/Negative on failure
 *
 * 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;
    hsize_t		nbytes;
   
    FUNC_ENTER (H5F_arr_create, FAIL);

    /* check args */
    assert (f);
    assert (layout);
    layout->addr = HADDR_UNDEF; /*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 (HADDR_UNDEF==(layout->addr=H5MF_alloc(f, H5FD_MEM_DRAW, nbytes))) {
	    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");
    }

    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.  External
 *		files are described according to the external file list, EFL.
 *		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:	Non-negative on success/Negative on failure
 *
 * Programmer:	Robb Matzke
 *              Friday, January 16, 1998
 *
 * Modifications:
 *		Albert Cheng, 1998-06-02
 *		Added xfer_mode argument
 *
 * 		Robb Matzke, 1998-09-28
 *		Added the `xfer' argument and removed the `xfer_mode'
 *		argument since it's a field of `xfer'.
 *
 * 		Robb Matzke, 1999-08-02
 *		Data transfer properties are passed by ID since that's how
 *		the virtual file layer wants them.
 *-------------------------------------------------------------------------
 */
herr_t
H5F_arr_read(H5F_t *f, hid_t dxpl_id, const struct H5O_layout_t *layout,
	     const struct H5O_pline_t *pline, const H5O_fill_t *fill,
	     const struct H5O_efl_t *efl, const hsize_t _hslab_size[],
	     const hsize_t mem_size[], const hssize_t mem_offset[],
	     const hssize_t file_offset[], void *_buf/*out*/)
{
    uint8_t	*buf = (uint8_t*)_buf;		/*cast for arithmetic	*/
    hssize_t	file_stride[H5O_LAYOUT_NDIMS];	/*strides through file	*/
    hssize_t	mem_stride[H5O_LAYOUT_NDIMS];	/*strides through memory*/
    hsize_t	hslab_size[H5O_LAYOUT_NDIMS];	/*hyperslab size	*/
    hsize_t	idx[H5O_LAYOUT_NDIMS];		/*multi-dim counter	*/
    size_t	mem_start;			/*byte offset to start	*/
    hsize_t	file_start;			/*byte offset to start	*/
    hsize_t	elmt_size = 1;			/*bytes per element	*/
    size_t	nelmts, z;			/*number of elements	*/
    intn	ndims;				/*stride dimensionality	*/
    haddr_t	addr;				/*address in file	*/
    haddr_t	eof;		        /*end of file address		*/
    intn	i, j;				/*counters		*/
    hbool_t	carray;				/*carry for subtraction	*/
#ifdef H5_HAVE_PARALLEL
    H5FD_mpio_xfer_t xfer_mode=H5FD_MPIO_INDEPENDENT;
#endif
#ifdef COALESCE_READS
    H5F_xfer_t *xfer_parms;                     /*transfer property list*/
#endif
   
    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);

#ifdef H5_HAVE_PARALLEL
    {
	/* Get the transfer mode */
	H5F_xfer_t *dxpl;
	H5FD_mpio_dxpl_t *dx;
	if (H5P_DEFAULT!=dxpl_id && (dxpl=H5I_object(dxpl_id)) &&
	    H5FD_MPIO==dxpl->driver_id && (dx=dxpl->driver_info) &&
	    H5FD_MPIO_INDEPENDENT!=dx->xfer_mode) {
	    xfer_mode = dx->xfer_mode;
	}
    }
#endif
    
#ifdef H5_HAVE_PARALLEL
    /* Collective MPIO access is unsupported for non-contiguous datasets */
    if (H5D_CONTIGUOUS!=layout->type && H5FD_MPIO_COLLECTIVE==xfer_mode) {
	HRETURN_ERROR (H5E_DATASET, H5E_READERROR, FAIL,
		       "collective access on non-contiguous datasets not "
		       "supported yet");
    }
#endif
#ifdef QAK
{
    extern int qak_debug;

    if(qak_debug) {
        printf("%s: layout->ndims=%d\n",FUNC,(int)layout->ndims);
        for(i=0; i<layout->ndims; i++)
            printf("%s: %d: hslab_size=%d, mem_size=%d, mem_offset=%d, file_offset=%d\n",FUNC,i,(int)_hslab_size[i],(int)mem_size[i],(int)mem_offset[i],(int)file_offset[i]);
        printf("%s: *buf=%d, *(buf+1)=%d\n", FUNC,(int)*(const uint16_t *)buf,(int)*((const uint16 *)buf+1));
    }
}
#endif /* QAK */

    switch (layout->type) {
    case H5D_CONTIGUOUS:
	ndims = layout->ndims;
	/*
	 * Offsets must not be negative for this type of storage.
	 */
	for (i=0; i<ndims; i++) {
	    if (mem_offset[i]<0 || file_offset[i]<0) {
		HRETURN_ERROR(H5E_IO, H5E_READERROR, FAIL,
			      "negative offsets are not valid");
	    }
	}

	/*
	 * Filters cannot be used for contiguous data.
	 */
	if (pline && pline->nfilters>0) {
	    HRETURN_ERROR(H5E_IO, H5E_READERROR, FAIL,
			  "filters are not allowed for contiguous data");
	}
	
	/*
	 * 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.
	 */
	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);
	if (efl && efl->nused>0) {
	    addr = 0;
	} else {
	    addr = layout->addr;
	}
	addr += file_start;
	buf += mem_start;

	/*
	 * Now begin to walk through the array, copying data from disk to
	 * memory.
	 */
#ifdef H5_HAVE_PARALLEL
	if (H5FD_MPIO_COLLECTIVE==xfer_mode){
	    /*
	     * Currently supports same number of collective access. Need to
	     * be changed LATER to combine all reads into one collective MPIO
	     * call.
	     */
	    unsigned long max, min, temp;

	    temp = nelmts;
	    assert(temp==nelmts);	/* verify no overflow */
	    MPI_Allreduce(&temp, &max, 1, MPI_UNSIGNED_LONG, MPI_MAX,
			  H5FD_mpio_communicator(f->shared->lf));
	    MPI_Allreduce(&temp, &min, 1, MPI_UNSIGNED_LONG, MPI_MIN,
			  H5FD_mpio_communicator(f->shared->lf));
#ifdef AKC
	    printf("nelmts=%lu, min=%lu, max=%lu\n", temp, min, max);
#endif
	    if (max != min)
		HRETURN_ERROR(H5E_DATASET, H5E_READERROR, FAIL,
			      "collective access with unequal number of "
			      "blocks not supported yet");
	}
#endif

#ifdef COALESCE_READS
        /* Get the dataset transfer property list */
        if (H5P_DEFAULT == dxpl_id) {
            xfer_parms = &H5F_xfer_dflt;
        } else if (H5P_DATA_XFER != H5P_get_class (dxpl_id) ||
               NULL == (xfer_parms = H5I_object (dxpl_id))) {
            HRETURN_ERROR (H5E_ARGS, H5E_BADTYPE, FAIL, "not xfer parms");
        }

	for (z=0, xfer_parms->gather_reads = nelmts - 1;
             z<nelmts;
             z++, xfer_parms->gather_reads--) {
#else
#ifdef QAK
printf("%s: nelmts=%d, addr=%lu, elmt_size=%lu\n",FUNC,(int)nelmts,(unsigned long)addr,(unsigned long)elmt_size);
printf("%s: sieve_buf=%p, sieve_loc=%lu, sieve_size=%lu, sieve_buf_size=%lu, sieve_dirty=%u\n",FUNC,f->shared->sieve_buf,(unsigned long)f->shared->sieve_loc,(unsigned long)f->shared->sieve_size,(unsigned long)f->shared->sieve_buf_size,(unsigned)f->shared->sieve_dirty);
printf("%s: feature_flags=%lx\n",FUNC,(unsigned long)f->shared->lf->feature_flags);
#endif /* QAK */
	for (z=0; z<nelmts; z++) {
#endif

        /* Read directly from file if the dataset is in an external file */
        /* Note: We can't use data sieve buffers for datasets in external files
         *  because the 'addr' of all external files is set to 0 (above) and
         *  all datasets in external files would alias to the same set of
         *  file offsets, totally mixing up the data sieve buffer information. -QAK
         */
        if (efl && efl->nused>0) {
            if (H5O_efl_read(f, efl, addr, elmt_size, buf)<0) {
                HRETURN_ERROR(H5E_IO, H5E_READERROR, FAIL,
                      "external data read failed");
            }
        } else {
            /* Check if data sieving is enabled */
            if(f->shared->lf->feature_flags&H5FD_FEAT_DATA_SIEVE) {
                /* Try reading from the data sieve buffer */
                if(f->shared->sieve_buf) {
                    /* If entire read is within the sieve buffer, read it from the buffer */
                    if((addr>=f->shared->sieve_loc && addr<(f->shared->sieve_loc+f->shared->sieve_size))
                            && ((addr+elmt_size-1)>=f->shared->sieve_loc && (addr+elmt_size-1)<(f->shared->sieve_loc+f->shared->sieve_size))) {
                        /* Grab the data out of the buffer */
                        HDmemcpy(buf,f->shared->sieve_buf+(addr-f->shared->sieve_loc),elmt_size);
                    } /* end if */
                    /* Entire request is not within this data sieve buffer */
                    else {
                        /* Check if we can actually hold the I/O request in the sieve buffer */
                        if(elmt_size>f->shared->sieve_buf_size) {
                            /* Check for any overlap with the current sieve buffer */
                            if((f->shared->sieve_loc>=addr && f->shared->sieve_loc<(addr+elmt_size))
                                    || ((f->shared->sieve_loc+f->shared->sieve_size-1)>=addr && (f->shared->sieve_loc+f->shared->sieve_size-1)<(addr+elmt_size))) {
                                /* Flush the sieve buffer, if it's dirty */
                                if(f->shared->sieve_dirty) {
                                    /* Write to file */
                                    if (H5F_block_write(f, H5FD_MEM_DRAW, f->shared->sieve_loc, f->shared->sieve_size, dxpl_id, f->shared->sieve_buf)<0) {
                                        HRETURN_ERROR(H5E_IO, H5E_WRITEERROR, FAIL,
                                          "block write failed");
                                    }

                                    /* Reset sieve buffer dirty flag */
                                    f->shared->sieve_dirty=0;
                                } /* end if */
                            } /* end if */

                            /* Read directly into the user's buffer */
                            if (H5F_block_read(f, addr, elmt_size, dxpl_id, buf)<0) {
                                HRETURN_ERROR(H5E_IO, H5E_READERROR, FAIL,
                                          "block read failed");
                            }
                        } /* end if */
                        /* Element size fits within the buffer size */
                        else {
                            /* Flush the sieve buffer if it's dirty */
                            if(f->shared->sieve_dirty) {
                                /* Write to file */
                                if (H5F_block_write(f, H5FD_MEM_DRAW, f->shared->sieve_loc, f->shared->sieve_size, dxpl_id, f->shared->sieve_buf)<0) {
                                    HRETURN_ERROR(H5E_IO, H5E_WRITEERROR, FAIL,
                                      "block write failed");
                                }

                                /* Reset sieve buffer dirty flag */
                                f->shared->sieve_dirty=0;
                            } /* end if */

                            /* Determine the new sieve buffer size & location */
                            f->shared->sieve_loc=addr;

                            /* Make certain we don't read off the end of the file */
                            if (HADDR_UNDEF==(eof=H5FD_get_eof(f->shared->lf))) {
                                HRETURN_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL,
                                    "unable to determine file size");
                            }
                            f->shared->sieve_size=MIN(eof-addr,f->shared->sieve_buf_size);

                            /* Read the new sieve buffer */
                            if (H5F_block_read(f, f->shared->sieve_loc, f->shared->sieve_size, dxpl_id, f->shared->sieve_buf)<0) {
                                HRETURN_ERROR(H5E_IO, H5E_READERROR, FAIL,
                                          "block read failed");
                            }

                            /* Reset sieve buffer dirty flag */
                            f->shared->sieve_dirty=0;

                            /* Grab the data out of the buffer (must be first piece of data in buffer ) */
                            HDmemcpy(buf,f->shared->sieve_buf,elmt_size);
                        } /* end else */
                    } /* end else */
                } /* end if */
                /* No data sieve buffer yet, go allocate one */
                else {
                    /* Check if we can actually hold the I/O request in the sieve buffer */
                    if(elmt_size>f->shared->sieve_buf_size) {
                        if (H5F_block_read(f, addr, elmt_size, dxpl_id, buf)<0) {
                            HRETURN_ERROR(H5E_IO, H5E_READERROR, FAIL,
                                      "block read failed");
                        }
                    } /* end if */
                    else {
                        /* Allocate room for the data sieve buffer */
                        if (NULL==(f->shared->sieve_buf=H5MM_malloc(f->shared->sieve_buf_size))) {
                            HRETURN_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL,
                                  "memory allocation failed");
                        }

                        /* Determine the new sieve buffer size & location */
                        f->shared->sieve_loc=addr;

                        /* Make certain we don't read off the end of the file */
                        if (HADDR_UNDEF==(eof=H5FD_get_eof(f->shared->lf))) {
                            HRETURN_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL,
                                "unable to determine file size");
                        }
                        f->shared->sieve_size=MIN(eof-addr,f->shared->sieve_buf_size);

                        /* Read the new sieve buffer */
                        if (H5F_block_read(f, f->shared->sieve_loc, f->shared->sieve_size, dxpl_id, f->shared->sieve_buf)<0) {
                            HRETURN_ERROR(H5E_IO, H5E_READERROR, FAIL,
                                      "block read failed");
                        }

                        /* Reset sieve buffer dirty flag */
                        f->shared->sieve_dirty=0;

                        /* Grab the data out of the buffer (must be first piece of data in buffer ) */
                        HDmemcpy(buf,f->shared->sieve_buf,elmt_size);
                    } /* end else */
                } /* end else */
            } /* end if */
            else {
                if (H5F_block_read(f, addr, elmt_size, dxpl_id, buf)<0) {
                    HRETURN_ERROR(H5E_IO, H5E_READERROR, FAIL,
                              "block read failed");
                }
            } /* end else */
        } /* end else */

	    /* Decrement indices and advance pointers */
	    for (j=ndims-1, carray=TRUE; j>=0 && carray; --j) {
		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 access external raw data files or to copy
	 * into a proper hyperslab.
	 */
	if (efl && efl->nused>0) {
	    HRETURN_ERROR(H5E_IO, H5E_UNSUPPORTED, FAIL,
			  "chunking and external files are mutually exclusive");
	}
	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, dxpl_id, layout, pline, fill, 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");
    }

    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 data is stored in external files according to the
 *		external file list, EFL. 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:	Non-negative on success/Negative on failure
 *
 * Programmer:	Robb Matzke
 *              Friday, January 16, 1998
 *
 * Modifications:
 *		Albert Cheng, 1998-06-02
 *		Added xfer_mode argument
 *
 * 		Robb Matzke, 1998-09-28
 *		Added `xfer' argument, removed `xfer_mode' argument since it
 *		is a member of H5F_xfer_t.
 *
 * 		Robb Matzke, 1999-08-02
 *		Data transfer properties are passed by ID since that's how
 *		the virtual file layer wants them.
 *-------------------------------------------------------------------------
 */
herr_t
H5F_arr_write(H5F_t *f, hid_t dxpl_id, const struct H5O_layout_t *layout,
	      const struct H5O_pline_t *pline,
	      const struct H5O_fill_t *fill, const struct H5O_efl_t *efl,
	      const hsize_t _hslab_size[], const hsize_t mem_size[],
	      const hssize_t mem_offset[], const hssize_t file_offset[],
	      const void *_buf)
{
    const uint8_t *buf = (const uint8_t *)_buf;	/*cast for arithmetic	*/
    hssize_t	file_stride[H5O_LAYOUT_NDIMS];	/*strides through file	*/
    hssize_t	mem_stride[H5O_LAYOUT_NDIMS];	/*strides through memory*/
    hsize_t	hslab_size[H5O_LAYOUT_NDIMS];	/*hyperslab size	*/
    hsize_t	idx[H5O_LAYOUT_NDIMS];		/*multi-dim counter	*/
    hsize_t	mem_start;			/*byte offset to start	*/
    hsize_t	file_start;			/*byte offset to start	*/
    hsize_t	elmt_size = 1;			/*bytes per element	*/
    size_t	nelmts, z;			/*number of elements	*/
    intn	ndims;				/*dimensionality	*/
    haddr_t	addr;				/*address in file	*/
    haddr_t	eof;		        /*end of file address		*/
    intn	i, j;				/*counters		*/
    hbool_t	carray;				/*carry for subtraction	*/
#ifdef H5_HAVE_PARALLEL
    H5FD_mpio_xfer_t xfer_mode=H5FD_MPIO_INDEPENDENT;
#endif
   
    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);

#ifdef H5_HAVE_PARALLEL
    {
	/* Get the transfer mode */
	H5F_xfer_t *dxpl;
	H5FD_mpio_dxpl_t *dx;
	if (H5P_DEFAULT!=dxpl_id && (dxpl=H5I_object(dxpl_id)) &&
	    H5FD_MPIO==dxpl->driver_id && (dx=dxpl->driver_info) &&
	    H5FD_MPIO_INDEPENDENT!=dx->xfer_mode) {
	    xfer_mode = dx->xfer_mode;
	}
    }
#endif
    
#ifdef H5_HAVE_PARALLEL
    if (H5D_CONTIGUOUS!=layout->type && H5FD_MPIO_COLLECTIVE==xfer_mode) {
	HRETURN_ERROR (H5E_DATASET, H5E_WRITEERROR, FAIL,
		       "collective access on non-contiguous datasets not "
		       "supported yet");
    }
#endif
    
#ifdef QAK
    {
	extern int qak_debug;

	printf("%s: layout->ndims=%d\n",FUNC,(int)layout->ndims);
	for(i=0; i<layout->ndims; i++)
	    printf("%s: %d: hslab_size=%d, mem_size=%d, mem_offset=%d, "
		   "file_offset=%d\n", FUNC, i, (int)_hslab_size[i],
		   (int)mem_size[i],(int)mem_offset[i],(int)file_offset[i]);
	if(qak_debug) {
	    printf("%s: *buf=%d, *(buf+1)=%d\n", FUNC,
		   (int)*(const uint16_t *)buf, (int)*((const uint16_t *)buf+1));
	}
    }
#endif /* QAK */

    switch (layout->type) {
    case H5D_CONTIGUOUS:
	ndims = layout->ndims;
	/*
	 * Offsets must not be negative for this type of storage.
	 */
	for (i=0; i<ndims; i++) {
	    if (mem_offset[i]<0 || file_offset[i]<0) {
		HRETURN_ERROR(H5E_IO, H5E_WRITEERROR, FAIL,
			      "negative offsets are not valid");
	    }
	}

	/*
	 * Filters cannot be used for contiguous data
	 */
	if (pline && pline->nfilters>0) {
	    HRETURN_ERROR(H5E_IO, H5E_WRITEERROR, FAIL,
			  "filters are not allowed for contiguous data");
	}
	
	/*
	 * Calculate the strides needed to walk through the array on disk.
	 * Optimize the strides to result in the fewest number of I/O
	 * requests.
	 */
	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);
	if (efl && efl->nused>0) {
	    addr = 0;
	} else {
	    addr = layout->addr;
	}
	addr += file_start;
	buf += mem_start;

	/*
	 * Now begin to walk through the array, copying data from memory to
	 * disk.
	 */
#ifdef H5_HAVE_PARALLEL
	if (H5FD_MPIO_COLLECTIVE==xfer_mode){
	    /*
	     * Currently supports same number of collective access. Need to
	     * be changed LATER to combine all writes into one collective
	     * MPIO call.
	     */
	    unsigned long max, min, temp;

	    temp = nelmts;
	    assert(temp==nelmts);	/* verify no overflow */
	    MPI_Allreduce(&temp, &max, 1, MPI_UNSIGNED_LONG, MPI_MAX,
			  H5FD_mpio_communicator(f->shared->lf));
	    MPI_Allreduce(&temp, &min, 1, MPI_UNSIGNED_LONG, MPI_MIN,
			  H5FD_mpio_communicator(f->shared->lf));
#ifdef AKC
	    printf("nelmts=%lu, min=%lu, max=%lu\n", temp, min, max);
#endif
	    if (max != min) {
		HRETURN_ERROR(H5E_DATASET, H5E_WRITEERROR, FAIL,
			      "collective access with unequal number of "
			      "blocks not supported yet");
	    }
	}
#endif

	for (z=0; z<nelmts; z++) {

	    /* Write to file */
	    if (efl && efl->nused>0) {
            if (H5O_efl_write(f, efl, addr, elmt_size, buf)<0) {
                HRETURN_ERROR(H5E_IO, H5E_READERROR, FAIL,
                      "external data write failed");
            }
	    } else {
            /* Check if data sieving is enabled */
            if(f->shared->lf->feature_flags&H5FD_FEAT_DATA_SIEVE) {
                /* Try writing to the data sieve buffer */
                if(f->shared->sieve_buf) {
                    /* If entire write is within the sieve buffer, write it to the buffer */
                    if((addr>=f->shared->sieve_loc && addr<(f->shared->sieve_loc+f->shared->sieve_size))
                            && ((addr+elmt_size-1)>=f->shared->sieve_loc && (addr+elmt_size-1)<(f->shared->sieve_loc+f->shared->sieve_size))) {
                        /* Grab the data out of the buffer */
                        HDmemcpy(f->shared->sieve_buf+(addr-f->shared->sieve_loc),buf,elmt_size);

                        /* Set sieve buffer dirty flag */
                        f->shared->sieve_dirty=1;

                    } /* end if */
                    /* Entire request is not within this data sieve buffer */
                    else {
                        /* Check if we can actually hold the I/O request in the sieve buffer */
                        if(elmt_size>f->shared->sieve_buf_size) {
                            /* Check for any overlap with the current sieve buffer */
                            if((f->shared->sieve_loc>=addr && f->shared->sieve_loc<(addr+elmt_size))
                                    || ((f->shared->sieve_loc+f->shared->sieve_size-1)>=addr && (f->shared->sieve_loc+f->shared->sieve_size-1)<(addr+elmt_size))) {
                                /* Flush the sieve buffer, if it's dirty */
                                if(f->shared->sieve_dirty) {
                                    /* Write to file */
                                    if (H5F_block_write(f, H5FD_MEM_DRAW, f->shared->sieve_loc, f->shared->sieve_size, dxpl_id, f->shared->sieve_buf)<0) {
                                        HRETURN_ERROR(H5E_IO, H5E_WRITEERROR, FAIL,
                                          "block write failed");
                                    }

                                    /* Reset sieve buffer dirty flag */
                                    f->shared->sieve_dirty=0;
                                } /* end if */

                                /* Force the sieve buffer to be re-read the next time */
                                f->shared->sieve_loc=HADDR_UNDEF;
                                f->shared->sieve_size=0;
                            } /* end if */

                            /* Write directly from the user's buffer */
                            if (H5F_block_write(f, H5FD_MEM_DRAW, addr, elmt_size, dxpl_id, buf)<0) {
                                HRETURN_ERROR(H5E_IO, H5E_WRITEERROR, FAIL,
                                          "block write failed");
                            }
                        } /* end if */
                        /* Element size fits within the buffer size */
                        else {
                            /* Flush the sieve buffer if it's dirty */
                            if(f->shared->sieve_dirty) {
                                /* Write to file */
                                if (H5F_block_write(f, H5FD_MEM_DRAW, f->shared->sieve_loc, f->shared->sieve_size, dxpl_id, f->shared->sieve_buf)<0) {
                                    HRETURN_ERROR(H5E_IO, H5E_WRITEERROR, FAIL,
                                      "block write failed");
                                }

                                /* Reset sieve buffer dirty flag */
                                f->shared->sieve_dirty=0;
                            } /* end if */

                            /* Determine the new sieve buffer size & location */
                            f->shared->sieve_loc=addr;

                            /* Make certain we don't read off the end of the file */
                            if (HADDR_UNDEF==(eof=H5FD_get_eof(f->shared->lf))) {
                                HRETURN_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL,
                                    "unable to determine file size");
                            }
                            f->shared->sieve_size=MIN(eof-addr,f->shared->sieve_buf_size);

                            /* Read the new sieve buffer */
                            if (H5F_block_read(f, f->shared->sieve_loc, f->shared->sieve_size, dxpl_id, f->shared->sieve_buf)<0) {
                                HRETURN_ERROR(H5E_IO, H5E_READERROR, FAIL,
                                          "block read failed");
                            }

                            /* Grab the data out of the buffer (must be first piece of data in buffer) */
                            HDmemcpy(f->shared->sieve_buf,buf,elmt_size);

                            /* Set sieve buffer dirty flag */
                            f->shared->sieve_dirty=1;

                        } /* end else */
                    } /* end else */
                } /* end if */
                /* No data sieve buffer yet, go allocate one */
                else {
                    /* Check if we can actually hold the I/O request in the sieve buffer */
                    if(elmt_size>f->shared->sieve_buf_size) {
                        if (H5F_block_write(f, H5FD_MEM_DRAW, addr, elmt_size, dxpl_id, buf)<0) {
                            HRETURN_ERROR(H5E_IO, H5E_WRITEERROR, FAIL,
                                      "block write failed");
                        }
                    } /* end if */
                    else {
                        /* Allocate room for the data sieve buffer */
                        if (NULL==(f->shared->sieve_buf=H5MM_malloc(f->shared->sieve_buf_size))) {
                            HRETURN_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL,
                                  "memory allocation failed");
                        }

                        /* Determine the new sieve buffer size & location */
                        f->shared->sieve_loc=addr;

                        /* Make certain we don't read off the end of the file */
                        if (HADDR_UNDEF==(eof=H5FD_get_eof(f->shared->lf))) {
                            HRETURN_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL,
                                "unable to determine file size");
                        }
                        f->shared->sieve_size=MIN(eof-addr,f->shared->sieve_buf_size);

                        /* Read the new sieve buffer */
                        if (H5F_block_read(f, f->shared->sieve_loc, f->shared->sieve_size, dxpl_id, f->shared->sieve_buf)<0) {
                            HRETURN_ERROR(H5E_IO, H5E_READERROR, FAIL,
                                      "block read failed");
                        }

                        /* Grab the data out of the buffer (must be first piece of data in buffer) */
                        HDmemcpy(f->shared->sieve_buf,buf,elmt_size);

                        /* Set sieve buffer dirty flag */
                        f->shared->sieve_dirty=1;
                    } /* end else */
                } /* end else */
            } /* end if */
            else {
                if (H5F_block_write(f, H5FD_MEM_DRAW, addr, elmt_size, dxpl_id, buf)<0) {
                    HRETURN_ERROR(H5E_IO, H5E_WRITEERROR, FAIL,
                              "block write failed");
                }
            } /* end else */
	    } /* end else */

	    /* Decrement indices and advance pointers */
	    for (j=ndims-1, carray=TRUE; j>=0 && carray; --j) {
		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 access external raw data files or to copy
	 * from a proper hyperslab.
	 */
	if (efl && efl->nused>0) {
	    HRETURN_ERROR(H5E_IO, H5E_UNSUPPORTED, FAIL,
			  "chunking and external files are mutually "
			  "exclusive");
	}
	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, dxpl_id, layout, pline, fill, 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");
    }

    FUNC_LEAVE (SUCCEED);
}