Skip to content

Commit

Permalink
Fix logic of enabling in-place byte swap
Browse files Browse the repository at this point in the history
This affects only when running on a Big Endian machine.
  • Loading branch information
wkliao committed Aug 16, 2024
1 parent 16a4c59 commit 32196ba
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 32 deletions.
17 changes: 9 additions & 8 deletions src/drivers/ncmpio/ncmpio_getput.m4
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ put_varm(NC *ncp,
{
void *xbuf=NULL;
int mpireturn, err=NC_NOERR, status=NC_NOERR, buftype_is_contig;
int el_size, need_convert, need_swap, in_place_swap, need_swap_back_buf=0;
int coll_indep, xtype_is_contig=1;
int el_size, need_convert, need_swap, need_swap_back_buf=0;
int coll_indep, xtype_is_contig=1, can_swap_in_place;
MPI_Offset nelems=0, bnelems=0, nbytes=0, offset=0;
MPI_Datatype itype, xtype=MPI_BYTE, imaptype, filetype=MPI_BYTE;
MPI_File fh;
Expand Down Expand Up @@ -168,13 +168,14 @@ put_varm(NC *ncp,
need_convert = ncmpii_need_convert(ncp->format, varp->xtype, itype);
need_swap = NEED_BYTE_SWAP(varp->xtype, itype);

in_place_swap = 0;
/* check if in-place byte swap can be enabled */
can_swap_in_place = 1;
if (need_swap) {
if (fIsSet(ncp->flags, NC_MODE_SWAP_ON))
in_place_swap = 1;
else if (! fIsSet(ncp->flags, NC_MODE_SWAP_OFF)) { /* auto mode */
if (! fIsSet(ncp->flags, NC_MODE_SWAP_OFF)) /* hint set by user */
can_swap_in_place = 0;
else if (! fIsSet(ncp->flags, NC_MODE_SWAP_ON)) { /* auto mode */
if (nbytes > NC_BYTE_SWAP_BUFFER_SIZE)
in_place_swap = 1;
can_swap_in_place = 0;
}
}

Expand All @@ -185,7 +186,7 @@ put_varm(NC *ncp,
if (err != NC_NOERR) goto err_check;

if (!need_convert && imaptype == MPI_DATATYPE_NULL &&
(!need_swap || (in_place_swap && buftype_is_contig))) {
(!need_swap || (can_swap_in_place && buftype_is_contig))) {
/* reuse buftype, bufcount, buf in later MPI file write */
xbuf = buf;
if (need_swap) {
Expand Down
23 changes: 11 additions & 12 deletions src/drivers/ncmpio/ncmpio_i_getput.m4
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ ncmpio_igetput_varm(NC *ncp,
void *xbuf=NULL;
int i, err=NC_NOERR, abuf_index=-1, isize, xsize, new_nreqs, rem;
int mpireturn, buftype_is_contig=1, need_convert, free_xbuf=0;
int need_swap, in_place_swap, need_swap_back_buf=0;
int need_swap, can_swap_in_place, need_swap_back_buf=0;
MPI_Offset nelems=0, nbytes, *ptr;
MPI_Datatype itype, xtype, imaptype;
NC_lead_req *lead_req;
Expand Down Expand Up @@ -232,16 +232,15 @@ ncmpio_igetput_varm(NC *ncp,
need_convert = ncmpii_need_convert(ncp->format, varp->xtype, itype);
need_swap = NEED_BYTE_SWAP(varp->xtype, itype);

/* check if we can do byte swap in place */
if (fIsSet(ncp->flags, NC_MODE_SWAP_ON))
in_place_swap = 1;
else if (fIsSet(ncp->flags, NC_MODE_SWAP_OFF))
in_place_swap = 0;
else { /* mode is auto */
if (nbytes <= NC_BYTE_SWAP_BUFFER_SIZE)
in_place_swap = 0;
else
in_place_swap = 1;
/* check if in-place byte swap can be enabled */
can_swap_in_place = 1;
if (need_swap) {
if (! fIsSet(ncp->flags, NC_MODE_SWAP_OFF)) /* hint set by user */
can_swap_in_place = 0;
else if (! fIsSet(ncp->flags, NC_MODE_SWAP_ON)) { /* auto mode */
if (nbytes > NC_BYTE_SWAP_BUFFER_SIZE)
can_swap_in_place = 0;
}
}

/* check whether this is a true varm call, if yes, imaptype will be a
Expand All @@ -265,7 +264,7 @@ ncmpio_igetput_varm(NC *ncp,
}
else {
if (!buftype_is_contig || imaptype != MPI_DATATYPE_NULL ||
need_convert || (need_swap && in_place_swap == 0)) {
need_convert || (need_swap && can_swap_in_place == 0)) {
/* cannot use buf for I/O, must allocate xbuf */
xbuf = NCI_Malloc((size_t)nbytes);
free_xbuf = 1;
Expand Down
12 changes: 6 additions & 6 deletions src/drivers/ncmpio/ncmpio_i_varn.m4
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,13 @@ igetput_varn(NC *ncp,

if (fIsSet(reqMode, NC_REQ_WR)) {
/* check if in-place byte swap can be enabled */
int in_place_swap = 0;
int can_swap_in_place = 1;
if (need_swap) {
if (fIsSet(ncp->flags, NC_MODE_SWAP_ON))
in_place_swap = 1;
else if (! fIsSet(ncp->flags, NC_MODE_SWAP_OFF)) { /* auto mode */
if (! fIsSet(ncp->flags, NC_MODE_SWAP_OFF)) /* hint set by user */
can_swap_in_place = 0;
else if (! fIsSet(ncp->flags, NC_MODE_SWAP_ON)) { /* auto mode */
if (nbytes > NC_BYTE_SWAP_BUFFER_SIZE)
in_place_swap = 1;
can_swap_in_place = 0;
}
}

Expand All @@ -195,7 +195,7 @@ igetput_varn(NC *ncp,
err = ncmpio_abuf_malloc(ncp, nbytes, &xbuf, &abuf_index);
if (err != NC_NOERR) goto fn_exit;
}
else if (!need_convert && in_place_swap && isContig) {
else if (!need_convert && can_swap_in_place && isContig) {
/* reuse buf and break it into multiple vara requests */
xbuf = buf;
if (need_swap) need_swap_back_buf = 1;
Expand Down
14 changes: 8 additions & 6 deletions src/drivers/ncmpio/ncmpio_vard.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,19 @@ getput_vard(NC *ncp,
need_swap = NEED_BYTE_SWAP(varp->xtype, etype);

if (fIsSet(reqMode, NC_REQ_WR)) {
int in_place_swap = 0;
/* check if in-place byte swap can be enabled */
int can_swap_in_place = 1;
if (need_swap) {
if (fIsSet(ncp->flags, NC_MODE_SWAP_ON))
in_place_swap = 1;
else if (! fIsSet(ncp->flags, NC_MODE_SWAP_OFF)) { /* auto mode */
if (! fIsSet(ncp->flags, NC_MODE_SWAP_OFF)) /* hint set by user */
can_swap_in_place = 0;
else if (! fIsSet(ncp->flags, NC_MODE_SWAP_ON)) { /* auto mode */
if (filetype_size > NC_BYTE_SWAP_BUFFER_SIZE)
in_place_swap = 1;
can_swap_in_place = 0;
}
}

if (!need_convert &&
(!need_swap || (in_place_swap && buftype_is_contig))) {
(!need_swap || (can_swap_in_place && buftype_is_contig))) {
/* reuse buftype, bufcount, buf in later MPI file write */
xbuf = buf;
if (need_swap) {
Expand Down

0 comments on commit 32196ba

Please sign in to comment.