@@ -431,16 +431,29 @@ mca_io_ompio_file_preallocate (ompi_file_t *fh,
431431
432432 tmp = diskspace ;
433433
434- data -> ompio_fh .f_comm -> c_coll .coll_bcast (& tmp ,
435- 1 ,
436- OMPI_OFFSET_DATATYPE ,
437- OMPIO_ROOT ,
438- data -> ompio_fh .f_comm ,
439- data -> ompio_fh .f_comm -> c_coll .coll_bcast_module );
434+ ret = data -> ompio_fh .f_comm -> c_coll .coll_bcast (& tmp ,
435+ 1 ,
436+ OMPI_OFFSET_DATATYPE ,
437+ OMPIO_ROOT ,
438+ data -> ompio_fh .f_comm ,
439+ data -> ompio_fh .f_comm -> c_coll .coll_bcast_module );
440+ if ( OMPI_SUCCESS != ret ) {
441+ return OMPI_ERROR ;
442+ }
440443
441444 if (tmp != diskspace ) {
442445 return OMPI_ERROR ;
443446 }
447+ ret = data -> ompio_fh .f_fs -> fs_file_get_size (& data -> ompio_fh ,
448+ & current_size );
449+ if ( OMPI_SUCCESS != ret ) {
450+ return OMPI_ERROR ;
451+ }
452+
453+ if ( current_size > diskspace ) {
454+ return OMPI_SUCCESS ;
455+ }
456+
444457
445458 /* ROMIO explanation
446459 On file systems with no preallocation function, we have to
@@ -450,8 +463,8 @@ mca_io_ompio_file_preallocate (ompi_file_t *fh,
450463 preallocation is needed.
451464 */
452465 if (OMPIO_ROOT == data -> ompio_fh .f_rank ) {
453- ret = data -> ompio_fh . f_fs -> fs_file_get_size ( & data -> ompio_fh ,
454- & current_size );
466+ OMPI_MPI_OFFSET_TYPE prev_offset ;
467+ ompio_io_ompio_file_get_position ( & data -> ompio_fh , & prev_offset );
455468
456469 size = diskspace ;
457470 if (size > current_size ) {
@@ -463,7 +476,8 @@ mca_io_ompio_file_preallocate (ompi_file_t *fh,
463476 buf = (char * ) malloc (OMPIO_PREALLOC_MAX_BUF_SIZE );
464477 if (NULL == buf ) {
465478 opal_output (1 , "OUT OF MEMORY\n" );
466- return OMPI_ERR_OUT_OF_RESOURCE ;
479+ ret = OMPI_ERR_OUT_OF_RESOURCE ;
480+ goto exit ;
467481 }
468482 written = 0 ;
469483
@@ -474,11 +488,11 @@ mca_io_ompio_file_preallocate (ompi_file_t *fh,
474488 }
475489 ret = mca_io_ompio_file_read (fh , buf , len , MPI_BYTE , status );
476490 if (ret != OMPI_SUCCESS ) {
477- return OMPI_ERROR ;
491+ goto exit ;
478492 }
479493 ret = mca_io_ompio_file_write (fh , buf , len , MPI_BYTE , status );
480494 if (ret != OMPI_SUCCESS ) {
481- return OMPI_ERROR ;
495+ goto exit ;
482496 }
483497 written += len ;
484498 }
@@ -495,20 +509,25 @@ mca_io_ompio_file_preallocate (ompi_file_t *fh,
495509 }
496510 ret = mca_io_ompio_file_write (fh , buf , len , MPI_BYTE , status );
497511 if (ret != OMPI_SUCCESS ) {
498- return OMPI_ERROR ;
512+ goto exit ;
499513 }
500514 written += len ;
501515 }
502516 }
503- if (NULL != buf ) {
504- free (buf );
505- buf = NULL ;
506- }
517+
518+ // This operation should not affect file pointer position.
519+ ompi_io_ompio_set_explicit_offset ( & data -> ompio_fh , prev_offset );
520+ }
521+
522+ exit :
523+ free ( buf );
524+ fh -> f_comm -> c_coll .coll_bcast ( & ret , 1 , MPI_INT , OMPIO_ROOT , fh -> f_comm ,
525+ fh -> f_comm -> c_coll .coll_bcast_module );
526+
527+ if ( diskspace > current_size ) {
528+ data -> ompio_fh .f_fs -> fs_file_set_size (& data -> ompio_fh , diskspace );
507529 }
508- ret = data -> ompio_fh .f_fs -> fs_file_set_size (& data -> ompio_fh , diskspace );
509530
510- fh -> f_comm -> c_coll .coll_barrier (fh -> f_comm ,
511- fh -> f_comm -> c_coll .coll_barrier_module );
512531 return ret ;
513532}
514533
0 commit comments