Skip to content

Commit

Permalink
try to sort arrays - not working yet
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards4b committed Dec 14, 2018
1 parent 96e562f commit 8061b90
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 39 deletions.
9 changes: 9 additions & 0 deletions src/clib/pio.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@ typedef struct io_desc_t
* 1-based mappings to the global array for that task. */
PIO_Offset *map;

/** If the map passed in is not monotonically increasing
* then map is sorted and remap is an array of original
* indices of map. */

int *remap;

/** Number of tasks involved in the communication between comp and
* io tasks. */
int nrecvs;
Expand Down Expand Up @@ -294,6 +300,9 @@ typedef struct io_desc_t
* everywhere (false) */
bool needsfill;

/** If the map is not monotonically increasing we will need to sort it. */
bool needssort;

/** The maximum number of bytes of this iodesc before flushing. */
int maxbytes;

Expand Down
21 changes: 17 additions & 4 deletions src/clib/pio_darray.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ int PIOc_write_darray_multi(int ncid, const int *varids, int ioid, int nvars,
int fndims; /* Number of dims in the var in the file. */
int mpierr = MPI_SUCCESS, mpierr2; /* Return code from MPI function calls. */
int ierr; /* Return code. */
void *tmparray;

/* Get the file info. */
if ((ierr = pio_get_file(ncid, &file)))
Expand Down Expand Up @@ -264,9 +265,18 @@ int PIOc_write_darray_multi(int ncid, const int *varids, int ioid, int nvars,
return pio_err(ios, file, PIO_ENOMEM, __FILE__, __LINE__);
LOG((3, "allocated token for variable buffer"));
}
if (iodesc->needssort)
{
tmparray = pio_sorted_copy(iodesc, array, arraylen, nvars);
}
else
{
tmparray = array;
}


/* Move data from compute to IO tasks. */
if ((ierr = rearrange_comp2io(ios, iodesc, array, file->iobuf, nvars)))
if ((ierr = rearrange_comp2io(ios, iodesc, tmparray, file->iobuf, nvars)))
return pio_err(ios, file, ierr, __FILE__, __LINE__);

/* Write the darray based on the iotype. */
Expand Down Expand Up @@ -364,6 +374,9 @@ int PIOc_write_darray_multi(int ncid, const int *varids, int ioid, int nvars,
}
}

if(iodesc->needssort && tmparray != NULL)
free(tmparray);

/* Flush data to disk for pnetcdf. */
if (ios->ioproc && file->iotype == PIO_IOTYPE_PNETCDF)
if ((ierr = flush_output_buffer(file, flushtodisk, 0)))
Expand Down Expand Up @@ -403,7 +416,7 @@ pio_inq_var_fill_expected(int ncid, int varid, int pio_type, PIO_Offset type_siz
unsigned long long uint64_fill_value = NC_FILL_UINT64;
char *string_fill_value = "";
int ret;

/* Check inputs. */
assert(fillvalue);

Expand All @@ -412,7 +425,7 @@ pio_inq_var_fill_expected(int ncid, int varid, int pio_type, PIO_Offset type_siz

/* Is there a _FillValue attribute? */
ret = PIOc_inq_att_eh(ncid, varid, "_FillValue", 0, NULL, NULL);

LOG((3, "pio_inq_var_fill_expected ret %d", ret));

/* If there is a fill value, get it. */
Expand Down Expand Up @@ -472,7 +485,7 @@ pio_inq_var_fill_expected(int ncid, int varid, int pio_type, PIO_Offset type_siz
return PIO_EBADTYPE;
}
}

return PIO_NOERR;
}

Expand Down
123 changes: 123 additions & 0 deletions src/clib/pio_darray_int.c
Original file line number Diff line number Diff line change
Expand Up @@ -1905,3 +1905,126 @@ int flush_buffer(int ncid, wmulti_buffer *wmb, bool flushtodisk)

return PIO_NOERR;
}

void *pio_sorted_copy(io_desc_t *iodesc, void *array, PIO_Offset arraylen, int nvars)
{
void *tmparray;
if (!(tmparray = malloc(iodesc->piotype_size * arraylen)))
return NULL;

int maplen = arraylen / nvars;
switch (iodesc->piotype)
{
case PIO_BYTE:
for (int v=0; v < nvars; v++)
{
for (int m=0; m < maplen; m++)
{
((signed char *)tmparray)[iodesc->remap[m]] = ((signed char *)array)[m+maplen*v];
}
}
break;
case PIO_CHAR:
for (int v=0; v < nvars; v++)
{
for (int m=0; m < maplen; m++)
{
((char *)tmparray)[iodesc->remap[m]] = ((char *)array)[m+maplen*v];
}
}
break;
case PIO_SHORT:
for (int v=0; v < nvars; v++)
{
for (int m=0; m < maplen; m++)
{
((short *)tmparray)[iodesc->remap[m]] = ((short *)array)[m+maplen*v];
}
}

break;
case PIO_INT:
for (int v=0; v < nvars; v++)
{
for (int m=0; m < maplen; m++)
{
((int *)tmparray)[iodesc->remap[m]] = ((int *)array)[m+maplen*v];
}
}
break;
case PIO_FLOAT:
for (int v=0; v < nvars; v++)
{
for (int m=0; m < maplen; m++)
{
((float *)tmparray)[iodesc->remap[m]] = ((float *)array)[m+maplen*v];
}
}
break;
case PIO_DOUBLE:
for (int v=0; v < nvars; v++)
{
for (int m=0; m < maplen; m++)
{
((double *)tmparray)[iodesc->remap[m]] = ((double *)array)[m+maplen*v];
}
}
break;
case PIO_UBYTE:
for (int v=0; v < nvars; v++)
{
for (int m=0; m < maplen; m++)
{
((unsigned char *)tmparray)[iodesc->remap[m]] = ((unsigned char *)array)[m+maplen*v];
}
}
break;
case PIO_USHORT:
for (int v=0; v < nvars; v++)
{
for (int m=0; m < maplen; m++)
{
((unsigned short *)tmparray)[iodesc->remap[m]] = ((unsigned short *)array)[m+maplen*v];
}
}
break;
case PIO_UINT:
for (int v=0; v < nvars; v++)
{
for (int m=0; m < maplen; m++)
{
((unsigned int *)tmparray)[iodesc->remap[m]] = ((unsigned int *)array)[m+maplen*v];
}
}
break;
case PIO_INT64:
for (int v=0; v < nvars; v++)
{
for (int m=0; m < maplen; m++)
{
((long long *)tmparray)[iodesc->remap[m]] = ((long long *)array)[m+maplen*v];
}
}
break;
case PIO_UINT64:
for (int v=0; v < nvars; v++)
{
for (int m=0; m < maplen; m++)
{
((unsigned long long *)tmparray)[iodesc->remap[m]] = ((unsigned long long *)array)[m+maplen*v];
}
}
break;
case PIO_STRING:
for (int v=0; v < nvars; v++)
{
for (int m=0; m < maplen; m++)
{
((char **)tmparray)[iodesc->remap[m]] = ((char **)array)[m+maplen*v];
}
}
break;
default:
return NULL;
}
}
5 changes: 3 additions & 2 deletions src/clib/pio_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ extern "C" {
int pio_get_file(int ncid, file_desc_t **filep);
int pio_delete_file_from_list(int ncid);
void pio_add_to_file_list(file_desc_t *file);

/* List operations for var_desc_t list. */
int add_to_varlist(int varid, int rec_var, int pio_type, int pio_type_size,
MPI_Datatype mpi_type, int mpi_type_size, var_desc_t **varlist);
Expand Down Expand Up @@ -312,7 +312,7 @@ extern "C" {

int pio_read_darray_nc(file_desc_t *file, io_desc_t *iodesc, int vid, void *iobuf);
int pio_read_darray_nc_serial(file_desc_t *file, io_desc_t *iodesc, int vid, void *iobuf);
int find_var_fillvalue(file_desc_t *file, int varid, var_desc_t *vdesc);
int find_var_fillvalue(file_desc_t *file, int varid, var_desc_t *vdesc);

/* Read atts with type conversion. */
int PIOc_get_att_tc(int ncid, int varid, const char *name, nc_type memtype, void *ip);
Expand Down Expand Up @@ -361,6 +361,7 @@ extern "C" {
int determine_procs(int num_io_procs, int component_count, int *num_procs_per_comp,
int **proc_list, int **my_proc_list);

void *pio_sorted_copy(io_desc_t *iodesc, void *array, PIO_Offset arraylen, int nvars);
#if defined(__cplusplus)
}
#endif
Expand Down
45 changes: 43 additions & 2 deletions src/clib/pioc.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,27 @@ int PIOc_set_iosystem_error_handling(int iosysid, int method, int *old_method)
return PIO_NOERR;
}

void pio_map_sort(const PIO_Offset *map, int *remap, int maplen)
{
bool switched=false;
do
{
switched = false;
for(int i=1; i<maplen; i++)
{
if (map[remap[i-1]] < map[remap[i]])
{
int remaptemp = remap[i];
remap[i] = remap[i-1];
remap[i-1] = remaptemp;
switched = true;
}
}
}
while(switched);
}


/**
* Initialize the decomposition used with distributed arrays. The
* decomposition describes how the data will be distributed between
Expand Down Expand Up @@ -495,12 +516,32 @@ int PIOc_InitDecomp(int iosysid, int pio_type, int ndims, const int *gdimlen, in
/* Remember the map. */
if (!(iodesc->map = malloc(sizeof(PIO_Offset) * maplen)))
return pio_err(ios, NULL, PIO_ENOMEM, __FILE__, __LINE__);
iodesc->needssort = false;
iodesc->remap = NULL;
for (int m = 0; m < maplen; m++)
{
iodesc->map[m] = compmap[m];
if(m > 0 && compmap[m] > 0 && compmap[m] < compmap[m-1])
iodesc->needssort = true;
LOG((4, "compmap[%d] = %d", m, compmap[m]));
}

if (iodesc->needssort){
if (!(iodesc->remap = malloc(sizeof(int) * maplen)))
return pio_err(ios, NULL, PIO_ENOMEM, __FILE__, __LINE__);
for (int m=0; m < maplen; m++)
iodesc->remap[m] = m;
pio_map_sort(compmap, iodesc->remap, maplen);
for (int m=0; m < maplen; m++)
{
iodesc->map[iodesc->remap[m]] = compmap[m];
}
}
else
{
for (int m=0; m < maplen; m++)
{
iodesc->map[m] = compmap[m];
}
}
/* Remember the dim sizes. */
if (!(iodesc->dimlen = malloc(sizeof(int) * ndims)))
return pio_err(ios, NULL, PIO_ENOMEM, __FILE__, __LINE__);
Expand Down
Loading

0 comments on commit 8061b90

Please sign in to comment.