Skip to content

Commit

Permalink
Backport master 3980 3994 (#3996)
Browse files Browse the repository at this point in the history
* Fix 1: bpls handle when BP5 throws logic error on minmax for unsupported type
Fix 2: examples/basics/values.F90 need to use adios2_mode_readRandomAccess

* Fix the C example stepping and reading data. Simplify code for shape/start/count
  • Loading branch information
pnorbert committed Jan 9, 2024
1 parent 1b5e36b commit 0fa6e62
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 73 deletions.
24 changes: 12 additions & 12 deletions examples/basics/globalArray1D/decomp.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@

/* random integer from {minv, minv+1, ..., maxv}
including minv and maxv */
long long int get_random(int minv, int maxv)
size_t get_random(int minv, int maxv, int rank)
{
long long int n;
size_t n;
time_t t;
/* Intializes random number generator */
srand((unsigned)time(&t));
n = (rand() % (maxv - minv + 1)) + minv;
srand((unsigned)time(&t) + rank);
n = (size_t)((rand() % (maxv - minv + 1)) + minv);
return n;
}
/* gather the local sizes of arrays and sum them up
so that each process knows the global shape
and its own offset in the global space */
void gather_decomp_1d(long long int *mysize, long long int *myshape, long long int *myoffset)
void gather_decomp_1d(size_t *mysize, size_t *myshape, size_t *myoffset)
{
long long int *sizes;
size_t *sizes;
int i;
sizes = malloc(sizeof(long long int) * (size_t)nproc);
sizes = malloc(sizeof(size_t) * (size_t)nproc);
MPI_Allgather(mysize, 1, MPI_LONG_LONG, sizes, 1, MPI_LONG_LONG, app_comm);

*myshape = 0;
Expand All @@ -49,14 +49,14 @@ void gather_decomp_1d(long long int *mysize, long long int *myshape, long long i
return;
}

void decomp_1d(long long int globalsize, long long int *myoffset, long long int *mysize)
void decomp_1d(size_t *globalsize, size_t *myoffset, size_t *mysize)
{
long long int rem;
*mysize = globalsize / nproc;
rem = globalsize - (nproc * *mysize);
size_t rem;
*mysize = *globalsize / nproc;
rem = *globalsize - (nproc * *mysize);
if (rank < rem)
{
mysize = mysize + 1;
*mysize = *mysize + 1;
*myoffset = rank * *mysize;
}
else
Expand Down
8 changes: 5 additions & 3 deletions examples/basics/globalArray1D/decomp.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
#ifndef ADIOS2EXAMPLES_DECOMP_H
#define ADIOS2EXAMPLES_DECOMP_H

extern long long int get_random(int, int);
extern void gather_decomp_1d(long long int *, long long int *, long long int *);
extern void decomp_1d(long long int, long long int *, long long int *);
#include <stddef.h>

extern size_t get_random(int minv, int maxv, int rank);
extern void gather_decomp_1d(size_t *, size_t *, size_t *);
extern void decomp_1d(size_t *, size_t *, size_t *);
#endif // ADIOS2EXAMPLES_DECOMP_H
54 changes: 32 additions & 22 deletions examples/basics/globalArray1D/globalArray1DRead.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,67 @@
#include "decomp.h"
#include "mpivars.h"
#include <adios2_c.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

void reader(adios2_adios *adios)
{
int step;
float *g;
float *g = NULL;
const char *streamname = "adios2-global-array-1d-c.bp";
adios2_step_status err;

long long int fixed_shape = 0, fixed_start = 0, fixed_count = 0;
size_t shape[1], start[1], count[1];

adios2_io *io = adios2_declare_io(adios, "input");
size_t shape[1];
shape[0] = (size_t)fixed_shape;

adios2_engine *engine = adios2_open(io, streamname, adios2_mode_read);
step = 0;
do
while (1)
{
adios2_begin_step(engine, adios2_step_mode_read, 10.0, &err);
if (err == adios2_step_status_end_of_stream)
{
break;
}
if (err != adios2_step_status_ok)
{
printf("Unexpected status when calling adios2_begin_step() for step %d", step);
break;
}
adios2_variable *var_g = adios2_inquire_variable(io, "GlobalArray");
if (step == 0)
{
/* fixed_shape is allocated in the next call*/
adios2_variable_shape(shape, var_g);
fixed_shape = (long long int)shape[0];
decomp_1d(fixed_shape, &fixed_start, &fixed_count);
g = malloc((size_t)fixed_count * sizeof(float));
decomp_1d(shape, start, count);
g = malloc(count[0] * sizeof(float));

printf("Read plan rank = %d global shape = %zu local count = %zu offset = %zu\n", rank,
shape[0], count[0], start[0]);
}

printf("Read plan rank = %d global shape = %lld local count = %lld "
"offset = %lld\n",
rank, fixed_shape, fixed_count, fixed_start);
adios2_variable *var = adios2_inquire_variable(io, "GlobalArray");
if (!var)
{
printf("ERROR: Variable 'GlobalArray' was not found in step %d", step);
break;
}

adios2_set_selection(var, 1, start, count);
// Initiate reading data in default/deferred mode: data is available after end_step
adios2_get(engine, var, g, adios2_mode_deferred);

adios2_end_step(engine);
step++;
} while (err != adios2_step_status_end_of_stream);
}
// Close the output
adios2_close(engine);
free(g);

if (rank == 0)
if (g != NULL)
{
printf("Try the following: \n");
printf(" bpls -la adios2-global-array-1d-c.bp GlobalArray -d -n "
"%lld \n",
fixed_shape);
printf(" bpls -la adios2-global-array-1d-c.bp GlobalArray -d -t -n "
"%lld \n ",
fixed_shape);
printf(" mpirun -n 2 ./adios2-global-array-1d-read-c \n");
free(g);
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/basics/globalArray1D/globalArray1DWrite.F90
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ subroutine writer
" bpls -la adios2-global-array-1d-f.bp ", &
"GlobalArray -d -t -n ", fixed_shape(1)
write (*,'(a)') &
" mpirun -n 2 ./adios2-global-array-1d-read-f "
" mpirun -n 2 ./adios2_basics_globalArray1DRead_f "
endif
end subroutine writer

Expand Down
34 changes: 11 additions & 23 deletions examples/basics/globalArray1D/globalArray1DWrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "decomp.h"
#include "mpivars.h"
#include <adios2_c.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

Expand All @@ -19,35 +20,26 @@ void writer(adios2_adios *adios)
const int numsteps = 5;
adios2_step_status err;

long long int fixed_shape = 0, fixed_start = 0, fixed_count = 0;
size_t shape[1], start[1], count[1];

/* Application variables
g = 1D distributed array,
global shape and per-process size is fixed */
fixed_count = get_random(mincount, maxcount);
g = malloc((size_t)fixed_count * sizeof(float));
gather_decomp_1d(&fixed_count, &fixed_shape, &fixed_start);
count[0] = get_random(mincount, maxcount, rank);
g = malloc((size_t)count[0] * sizeof(float));
gather_decomp_1d(count, shape, start);

adios2_io *io = adios2_declare_io(adios, "output");
size_t shape[1];
shape[0] = (size_t)fixed_shape;

size_t start[1];
start[0] = (size_t)fixed_start;

size_t count[1];
count[0] = (size_t)fixed_count;

adios2_variable *var_g = adios2_define_variable(io, "GlobalArray", adios2_type_float, 1, shape,
start, count, adios2_constant_dims_true);

adios2_engine *engine = adios2_open(io, "adios2-global-array-1d-c.bp", adios2_mode_write);
printf("Decmp rank = %d global shape = %lld local count = %lld offset = "
"%lld\n",
rank, fixed_shape, fixed_count, fixed_start);
printf("Decomp rank = %d global shape = %zu local count = %zu offset = %zu\n", rank, shape[0],
count[0], start[0]);
for (step = 0; step < numsteps; step++)
{
for (i = 0; i < fixed_count; i++)
for (i = 0; i < count[0]; i++)
{
g[i] = (float)(rank + (step + 1) / 100.0);
}
Expand All @@ -63,13 +55,9 @@ void writer(adios2_adios *adios)
if (rank == 0)
{
printf("Try the following: \n");
printf(" bpls -la adios2-global-array-1d-c.bp GlobalArray -d -n "
"%lld \n",
fixed_shape);
printf(" bpls -la adios2-global-array-1d-c.bp GlobalArray -d -t -n "
"%lld \n ",
fixed_shape);
printf(" mpirun -n 2 ./adios2-global-array-1d-read-c \n");
printf(" bpls -la adios2-global-array-1d-c.bp GlobalArray -d -n %zu \n", shape[0]);
printf(" bpls -la adios2-global-array-1d-c.bp GlobalArray -d -t -n %zu \n ", shape[0]);
printf(" mpirun -n 2 ./adios2_basics_globalArray1DRead_c \n");
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/basics/values/values.F90
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ subroutine reader
! Note, every process reads everything in this example

call adios2_declare_io(io, adios, "ValuesInput", ierr)
call adios2_open(engine, io, "adios2-values-f.bp", adios2_mode_read, MPI_COMM_SELF, ierr)
call adios2_open(engine, io, "adios2-values-f.bp", adios2_mode_readRandomAccess, MPI_COMM_SELF, ierr)

call adios2_inquire_variable(var_gc, io, "GlobalConstant", ierr)
call adios2_get(engine, var_gc, gc , ierr)
Expand Down
8 changes: 8 additions & 0 deletions examples/basics/values/valuesWrite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

#include <iostream>
#include <string>
#include <vector>

#include <adios2.h>
Expand Down Expand Up @@ -80,6 +81,8 @@ int main(int argc, char *argv[])

// 2. Global value, same value across processes, varying value over time
adios2::Variable<int> varStep = io.DefineVariable<int>("Step");
adios2::Variable<std::string> varGlobalString =
io.DefineVariable<std::string>("GlobalString");

// 3. Local value, varying across processes, constant over time
adios2::Variable<int> varProcessID =
Expand Down Expand Up @@ -110,6 +113,11 @@ int main(int argc, char *argv[])
writer.Put<int>(varNproc, nproc);
}
writer.Put<int>(varStep, step);

std::string str = "This is step " + std::to_string(step);
// str will go out of scope before EndStep(), so we must use
// Sync mode in Put()
writer.Put<std::string>(varGlobalString, str, adios2::Mode::Sync);
}

// 3. and 4. Writing a local value on every process. Will be shown
Expand Down
28 changes: 17 additions & 11 deletions source/utils/bpls/bpls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1144,21 +1144,27 @@ int printVariableInfo(core::Engine *fp, core::IO *io, core::Variable<T> *variabl
if (timestep == false)
{
MinMaxStruct MinMax;
if (fp->VariableMinMax(*variable, DefaultSizeT, MinMax))
try
{
fprintf(outf, " = ");
print_data(&MinMax.MinUnion, 0, adiosvartype, false);
fprintf(outf, " / ");
print_data(&MinMax.MaxUnion, 0, adiosvartype, false);
if (fp->VariableMinMax(*variable, DefaultSizeT, MinMax))
{
fprintf(outf, " = ");
print_data(&MinMax.MinUnion, 0, adiosvartype, false);
fprintf(outf, " / ");
print_data(&MinMax.MaxUnion, 0, adiosvartype, false);
}
else
{
fprintf(outf, " = ");
print_data(&variable->m_Min, 0, adiosvartype, false);
fprintf(outf, " / ");
print_data(&variable->m_Max, 0, adiosvartype, false);
}
// fprintf(outf," {MIN / MAX} ");
}
else
catch (std::logic_error &)
{
fprintf(outf, " = ");
print_data(&variable->m_Min, 0, adiosvartype, false);
fprintf(outf, " / ");
print_data(&variable->m_Max, 0, adiosvartype, false);
}
// fprintf(outf," {MIN / MAX} ");
}
#if 0
else
Expand Down

0 comments on commit 0fa6e62

Please sign in to comment.