Skip to content

Commit 71d7207

Browse files
committed
Alloc/copy map only if maplen > 0
The behavior of malloc(0 bytes) and std::copy/transform(NULL, ...) are implementation defined. Alloc map only if maplen > 0. And Copy/Transform map only if maplen > 0 Updating asserts in functions to allow NULL compmaps (when maplen == 0)
1 parent d7499fb commit 71d7207

3 files changed

Lines changed: 34 additions & 30 deletions

File tree

src/clib/core/pioc.cpp

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ static int subset_rearranger_init(iosystem_desc_t *ios, io_desc_t *iodesc, const
535535
int ret = PIO_NOERR;
536536

537537
assert(ios && iodesc);
538-
assert(iodesc->map && iodesc->dimlen);
538+
assert(((iodesc->maplen == 0) || iodesc->map) && iodesc->dimlen);
539539

540540
iodesc->num_aiotasks = ios->num_iotasks;
541541
LOG((2, "creating subset rearranger iodesc->num_aiotasks = %d", iodesc->num_aiotasks));
@@ -552,7 +552,7 @@ static int box_rearranger_init(iosystem_desc_t *ios, io_desc_t *iodesc, const PI
552552
{
553553
int ret = PIO_NOERR;
554554
assert(ios && iodesc);
555-
assert(iodesc->map && iodesc->dimlen && iodesc->firstregion);
555+
assert(((iodesc->maplen == 0) || iodesc->map) && iodesc->dimlen && iodesc->firstregion);
556556

557557
if(ios->ioproc){
558558
/* Unless the user specifies the start and count for each
@@ -652,7 +652,7 @@ static int contig_rearranger_init(iosystem_desc_t *ios, io_desc_t *iodesc, const
652652
int ret = PIO_NOERR;
653653

654654
assert(ios && iodesc);
655-
assert(iodesc->map && iodesc->dimlen);
655+
assert(((iodesc->maplen == 0) || iodesc->map) && iodesc->dimlen);
656656

657657
if(iostart && iocount){
658658
/* FIXME: We should at least convert iostart/iocount arrays to decomp maps */
@@ -849,25 +849,27 @@ static int initdecomp(int iosysid, int pio_type, int ndims, const int *gdimlen,
849849
}
850850

851851
/* Cache the local decomposition map */
852-
if(map_zero_based){
853-
/* BOX and SUBSET rearrangers expect map to the 1-based */
854-
if((iodesc->rearranger == PIO_REARR_BOX) || (iodesc->rearranger == PIO_REARR_SUBSET)){
855-
std::transform(compmap, compmap + maplen, iodesc->map,
856-
[](PIO_Offset off) { return off + 1; });
857-
}
858-
else{
859-
std::copy(compmap, compmap + maplen, iodesc->map);
860-
}
861-
}
862-
else{
863-
/* The decomposition map is 1-based */
864-
if(iodesc->rearranger == PIO_REARR_CONTIG){
865-
/* CONTIG rearranger expects map to be 0-based */
866-
std::transform(compmap, compmap + maplen, iodesc->map,
867-
[](PIO_Offset off) { return off - 1; });
852+
if(maplen > 0){
853+
if(map_zero_based){
854+
/* BOX and SUBSET rearrangers expect map to the 1-based */
855+
if((iodesc->rearranger == PIO_REARR_BOX) || (iodesc->rearranger == PIO_REARR_SUBSET)){
856+
std::transform(compmap, compmap + maplen, iodesc->map,
857+
[](PIO_Offset off) { return off + 1; });
858+
}
859+
else{
860+
std::copy(compmap, compmap + maplen, iodesc->map);
861+
}
868862
}
869863
else{
870-
std::copy(compmap, compmap + maplen, iodesc->map);
864+
/* The decomposition map is 1-based */
865+
if(iodesc->rearranger == PIO_REARR_CONTIG){
866+
/* CONTIG rearranger expects map to be 0-based */
867+
std::transform(compmap, compmap + maplen, iodesc->map,
868+
[](PIO_Offset off) { return off - 1; });
869+
}
870+
else{
871+
std::copy(compmap, compmap + maplen, iodesc->map);
872+
}
871873
}
872874
}
873875

src/clib/core/pioc_support.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,9 +1725,11 @@ int malloc_iodesc(iosystem_desc_t *ios, int piotype, int ndims, int maplen,
17251725
}
17261726

17271727
/* Allocate memory for the local decomposition map */
1728-
if(!((*iodesc)->map = (PIO_Offset *) malloc(sizeof(PIO_Offset) * maplen))){
1729-
return pio_err(ios, NULL, PIO_ENOMEM, __FILE__, __LINE__,
1730-
"Internal error while allocating memory (%lld bytes) to store I/O decomposition map", (unsigned long long) (sizeof(PIO_Offset) * maplen));
1728+
if(maplen > 0){
1729+
if(!((*iodesc)->map = (PIO_Offset *) malloc(sizeof(PIO_Offset) * maplen))){
1730+
return pio_err(ios, NULL, PIO_ENOMEM, __FILE__, __LINE__,
1731+
"Internal error while allocating memory (%lld bytes) to store I/O decomposition map", (unsigned long long) (sizeof(PIO_Offset) * maplen));
1732+
}
17311733
}
17321734

17331735
/* Allocate memory for storing the dimension lengths of variables that use this decomposition */
@@ -1819,7 +1821,7 @@ int PIOc_freedecomp_impl(int iosysid, int ioid)
18191821
}
18201822

18211823
/* Free the map. */
1822-
free(iodesc->map);
1824+
if(iodesc->map) { free(iodesc->map); }
18231825

18241826
/* Free the dimlens. */
18251827
free(iodesc->dimlen);

src/clib/core/rearr/pio_rearrange.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void expand_region(int dim, const int *gdimlen, int maplen, const PIO_Offset *ma
9999
int expansion_done = 0;
100100

101101
/* Check inputs. */
102-
pioassert(dim >= 0 && gdimlen && maplen >= 0 && map && region_size >= 0 &&
102+
pioassert(dim >= 0 && gdimlen && maplen >= 0 && ((maplen == 0) || map) && region_size >= 0 &&
103103
maplen >= region_size && region_stride >= 0 && max_size && count,
104104
"invalid input", __FILE__, __LINE__);
105105

@@ -1218,7 +1218,7 @@ int determine_fill(iosystem_desc_t *ios, io_desc_t *iodesc, const int *gdimlen,
12181218
int mpierr; /* Return code from MPI calls. */
12191219

12201220
/* Check inputs. */
1221-
pioassert(ios && iodesc && gdimlen && compmap, "invalid input",
1221+
pioassert(ios && iodesc && gdimlen && ((iodesc->ndof == 0) || compmap), "invalid input",
12221222
__FILE__, __LINE__);
12231223

12241224
/* Determine size of data space. */
@@ -1298,7 +1298,7 @@ int box_rearrange_create(iosystem_desc_t *ios, int maplen, const PIO_Offset *com
12981298

12991299
GPTLstart("PIO:box_rearrange_create");
13001300
/* Check inputs. */
1301-
pioassert(ios && maplen >= 0 && compmap && gdimlen && ndims > 0 && iodesc,
1301+
pioassert(ios && maplen >= 0 && ((maplen == 0) || compmap) && gdimlen && ndims > 0 && iodesc,
13021302
"invalid input", __FILE__, __LINE__);
13031303
LOG((1, "box_rearrange_create maplen = %d ndims = %d ios->num_comptasks = %d "
13041304
"ios->num_iotasks = %d", maplen, ndims, ios->num_comptasks, ios->num_iotasks));
@@ -1629,7 +1629,7 @@ int box_rearrange_create_with_holes(iosystem_desc_t *ios, int maplen, const PIO_
16291629

16301630
GPTLstart("PIO:box_rearrange_create_with_holes");
16311631
/* Check inputs. */
1632-
pioassert(ios && maplen >= 0 && compmap && gdimlen && ndims > 0 && iodesc,
1632+
pioassert(ios && maplen >= 0 && ((maplen == 0) || compmap) && gdimlen && ndims > 0 && iodesc,
16331633
"invalid input", __FILE__, __LINE__);
16341634
LOG((1, "box_rearrange_create maplen = %d ndims = %d ios->num_comptasks = %d "
16351635
"ios->num_iotasks = %d", maplen, ndims, ios->num_comptasks, ios->num_iotasks));
@@ -1983,7 +1983,7 @@ int get_regions(int ndims, const int *gdimlen, int maplen, const PIO_Offset *map
19831983
int ret;
19841984

19851985
/* Check inputs. */
1986-
pioassert(ndims >= 0 && gdimlen && maplen >= 0 && maxregions && firstregion,
1986+
pioassert(ndims >= 0 && gdimlen && maplen >= 0 && ((maplen == 0) || map) && maxregions && firstregion,
19871987
"invalid input", __FILE__, __LINE__);
19881988
LOG((1, "get_regions ndims = %d maplen = %d", ndims, maplen));
19891989

@@ -2201,7 +2201,7 @@ int subset_rearrange_create(iosystem_desc_t *ios, int maplen, PIO_Offset *compma
22012201

22022202
GPTLstart("PIO:subset_rearrange_create");
22032203
/* Check inputs. */
2204-
pioassert(ios && maplen >= 0 && compmap && gdimlen && ndims >= 0 && iodesc,
2204+
pioassert(ios && maplen >= 0 && ((maplen == 0) || compmap) && gdimlen && ndims >= 0 && iodesc,
22052205
"invalid input", __FILE__, __LINE__);
22062206

22072207
LOG((2, "subset_rearrange_create maplen = %d ndims = %d", maplen, ndims));

0 commit comments

Comments
 (0)