Skip to content

Commit

Permalink
Merge pull request lammps#3865 from lammps/compute-init-flags
Browse files Browse the repository at this point in the history
More fine grained monitoring if computes were initialized
  • Loading branch information
akohlmey committed Jul 21, 2023
2 parents b725f5d + 9669bf3 commit a4a206e
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 71 deletions.
5 changes: 3 additions & 2 deletions src/VTK/dump_vtk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,10 @@ int DumpVTK::count()
// cannot invoke before first run, otherwise invoke if necessary

if (ncompute) {
if (update->first_update == 0)
error->all(FLERR,"Dump compute cannot be invoked before first run");
for (i = 0; i < ncompute; i++) {
if (!compute[i]->is_initialized())
error->all(FLERR,"Dump compute ID {} cannot be invoked before initialization by a run",
compute[i]->id);
if (!(compute[i]->invoked_flag & Compute::INVOKED_PERATOM)) {
compute[i]->compute_peratom();
compute[i]->invoked_flag |= Compute::INVOKED_PERATOM;
Expand Down
10 changes: 10 additions & 0 deletions src/compute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Compute::Compute(LAMMPS *lmp, int narg, char **arg) :
dynamic = 0;
dynamic_group_allow = 1;

initialized_flag = 0;
invoked_scalar = invoked_vector = invoked_array = -1;
invoked_peratom = invoked_local = -1;
invoked_flag = INVOKED_NONE;
Expand Down Expand Up @@ -111,6 +112,15 @@ Compute::~Compute()

/* ---------------------------------------------------------------------- */

void Compute::init_flags()
{
initialized_flag = 1;
invoked_scalar = invoked_vector = invoked_array = -1;
invoked_peratom = invoked_local = -1;
}

/* ---------------------------------------------------------------------- */

void Compute::modify_params(int narg, char **arg)
{
if (narg == 0) error->all(FLERR,"Illegal compute_modify command");
Expand Down
6 changes: 5 additions & 1 deletion src/compute.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Compute : protected Pointers {
int size_local_rows; // rows in local vector or array
int size_local_cols; // 0 = vector, N = columns in local array

int pergrid_flag; // 0/1 if compute_pergrid() function exists
int pergrid_flag; // 0/1 if compute_pergrid() function exists

int extscalar; // 0/1 if global scalar is intensive/extensive
int extvector; // 0/1/-1 if global vector is all int/ext/extlist
Expand All @@ -88,6 +88,7 @@ class Compute : protected Pointers {
int maxtime; // max # of entries time list can hold
bigint *tlist; // list of timesteps the Compute is called on

int initialized_flag; // 1 if compute is initialized, 0 if not
int invoked_flag; // non-zero if invoked or accessed this step, 0 if not
bigint invoked_scalar; // last timestep on which compute_scalar() was invoked
bigint invoked_vector; // ditto for compute_vector()
Expand All @@ -114,6 +115,7 @@ class Compute : protected Pointers {
void modify_params(int, char **);
virtual void reset_extra_dof();

void init_flags();
virtual void init() = 0;
virtual void init_list(int, class NeighList *) {}
virtual void setup() {}
Expand Down Expand Up @@ -161,6 +163,8 @@ class Compute : protected Pointers {
int matchstep(bigint);
void clearstep();

bool is_initialized() const { return initialized_flag == 1; }

virtual double memory_usage() { return 0.0; }

virtual void pair_setup_callback(int, int) {}
Expand Down
5 changes: 3 additions & 2 deletions src/dump_custom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,9 +562,10 @@ int DumpCustom::count()
// cannot invoke before first run, otherwise invoke if necessary

if (ncompute) {
if (update->first_update == 0)
error->all(FLERR,"Dump compute cannot be invoked before first run");
for (i = 0; i < ncompute; i++) {
if (!compute[i]->is_initialized())
error->all(FLERR,"Dump compute ID {} cannot be invoked before initialization by a run",
compute[i]->id);
if (!(compute[i]->invoked_flag & Compute::INVOKED_PERATOM)) {
compute[i]->compute_peratom();
compute[i]->invoked_flag |= Compute::INVOKED_PERATOM;
Expand Down
5 changes: 3 additions & 2 deletions src/dump_grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,10 @@ int DumpGrid::count()
// cannot invoke before first run, otherwise invoke if necessary

if (ncompute) {
if (update->first_update == 0)
error->all(FLERR,"Dump compute cannot be invoked before first run");
for (i = 0; i < ncompute; i++) {
if (!compute[i]->is_initialized())
error->all(FLERR,"Dump compute ID {} cannot be invoked before initialization by a run",
compute[i]->id);
if (!(compute[i]->invoked_flag & Compute::INVOKED_PERGRID)) {
compute[i]->compute_pergrid();
compute[i]->invoked_flag |= Compute::INVOKED_PERGRID;
Expand Down
5 changes: 3 additions & 2 deletions src/dump_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -667,8 +667,9 @@ void DumpImage::write()
// cannot invoke before first run, otherwise invoke if necessary

if (grid_compute) {
if (update->first_update == 0)
error->all(FLERR,"Grid compute used in dump image cannot be invoked before first run");
if (!grid_compute->is_initialized())
error->all(FLERR,"Grid compute ID {} used in dump image cannot be invoked "
"before initialization by a run", grid_compute->id);
if (!(grid_compute->invoked_flag & Compute::INVOKED_PERGRID)) {
grid_compute->compute_pergrid();
grid_compute->invoked_flag |= Compute::INVOKED_PERGRID;
Expand Down
5 changes: 3 additions & 2 deletions src/dump_local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,10 @@ int DumpLocal::count()
// cannot invoke before first run, otherwise invoke if necessary

if (ncompute) {
if (update->first_update == 0)
error->all(FLERR,"Dump compute cannot be invoked before first run");
for (i = 0; i < ncompute; i++) {
if (!compute[i]->is_initialized())
error->all(FLERR,"Dump compute ID {} cannot be invoked before initialization by a run",
compute[i]->id);
if (!(compute[i]->invoked_flag & Compute::INVOKED_LOCAL)) {
compute[i]->compute_local();
compute[i]->invoked_flag |= Compute::INVOKED_LOCAL;
Expand Down
6 changes: 1 addition & 5 deletions src/modify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,7 @@ void Modify::init()

for (i = 0; i < ncompute; i++) {
compute[i]->init();
compute[i]->invoked_scalar = -1;
compute[i]->invoked_vector = -1;
compute[i]->invoked_array = -1;
compute[i]->invoked_peratom = -1;
compute[i]->invoked_local = -1;
compute[i]->init_flags();
}
addstep_compute_all(update->ntimestep);

Expand Down
41 changes: 16 additions & 25 deletions src/reset_atoms_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ void ResetAtomsImage::command(int narg, char **arg)
MPI_Barrier(world);
double time1 = platform::walltime();

// create computes and variables
// must come before lmp->init so the computes are properly initialized

auto frags = modify->add_compute("frags_r_i_f all fragment/atom single yes");
auto chunk = modify->add_compute("chunk_r_i_f all chunk/atom c_frags_r_i_f compress yes");
auto flags = modify->add_compute("flags_r_i_f all property/atom ix iy iz");
input->variable->set("ix_r_i_f atom c_flags_r_i_f[1]");
input->variable->set("iy_r_i_f atom c_flags_r_i_f[2]");
input->variable->set("iz_r_i_f atom c_flags_r_i_f[3]");
auto ifmin = modify->add_compute("ifmin_r_i_f all reduce/chunk chunk_r_i_f min "
"v_ix_r_i_f v_iy_r_i_f v_iz_r_i_f");
auto ifmax = modify->add_compute("ifmax_r_i_f all reduce/chunk chunk_r_i_f max "
"v_ix_r_i_f v_iy_r_i_f v_iz_r_i_f");
auto cdist = modify->add_compute("cdist_r_i_f all chunk/spread/atom chunk_r_i_f "
"c_ifmax_r_i_f[*] c_ifmin_r_i_f[*]");

// initialize system since comm->borders() will be invoked

lmp->init();
Expand All @@ -77,30 +93,7 @@ void ResetAtomsImage::command(int narg, char **arg)
comm->borders();
if (domain->triclinic) domain->lamda2x(atom->nlocal + atom->nghost);

// create computes and variables

auto frags = modify->add_compute("frags_r_i_f all fragment/atom single yes");
auto chunk = modify->add_compute("chunk_r_i_f all chunk/atom c_frags_r_i_f compress yes");
auto flags = modify->add_compute("flags_r_i_f all property/atom ix iy iz");
input->variable->set("ix_r_i_f atom c_flags_r_i_f[1]");
input->variable->set("iy_r_i_f atom c_flags_r_i_f[2]");
input->variable->set("iz_r_i_f atom c_flags_r_i_f[3]");
auto ifmin = modify->add_compute("ifmin_r_i_f all reduce/chunk chunk_r_i_f min "
"v_ix_r_i_f v_iy_r_i_f v_iz_r_i_f");
auto ifmax = modify->add_compute("ifmax_r_i_f all reduce/chunk chunk_r_i_f max "
"v_ix_r_i_f v_iy_r_i_f v_iz_r_i_f");
auto cdist = modify->add_compute("cdist_r_i_f all chunk/spread/atom chunk_r_i_f "
"c_ifmax_r_i_f[*] c_ifmin_r_i_f[*]");

// trigger computes
// need to ensure update->first_update = 1
// to allow this input script command prior to first run/minimize
// this is b/c internal variables are evaulated which invoke computes
// that will trigger an error unless first_update = 1
// reset update->first_update when done

int first_update_saved = update->first_update;
update->first_update = 1;

frags->compute_peratom();
chunk->compute_peratom();
Expand All @@ -109,8 +102,6 @@ void ResetAtomsImage::command(int narg, char **arg)
ifmax->compute_array();
cdist->compute_peratom();

update->first_update = first_update_saved;

// reset image flags for atoms in group

const int *const mask = atom->mask;
Expand Down
16 changes: 8 additions & 8 deletions src/thermo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1134,8 +1134,8 @@ void Thermo::check_temp(const std::string &keyword)
if (!temperature)
error->all(FLERR, "Thermo keyword {} in variable requires thermo to use/init temperature",
keyword);
if (update->first_update == 0)
error->all(FLERR,"Thermo keyword {} cannot be invoked before first run",keyword);
if (!temperature->is_initialized())
error->all(FLERR,"Thermo keyword {} cannot be invoked before initialization by a run",keyword);
if (!(temperature->invoked_flag & Compute::INVOKED_SCALAR)) {
temperature->compute_scalar();
temperature->invoked_flag |= Compute::INVOKED_SCALAR;
Expand All @@ -1153,8 +1153,8 @@ void Thermo::check_pe(const std::string &keyword)
if (!pe)
error->all(FLERR, "Thermo keyword {} in variable requires thermo to use/init potential energy",
keyword);
if (update->first_update == 0)
error->all(FLERR,"Thermo keyword {} cannot be invoked before first run",keyword);
if (!pe->is_initialized())
error->all(FLERR,"Thermo keyword {} cannot be invoked before initialization by a run",keyword);
if (!(pe->invoked_flag & Compute::INVOKED_SCALAR)) {
pe->compute_scalar();
pe->invoked_flag |= Compute::INVOKED_SCALAR;
Expand All @@ -1169,8 +1169,8 @@ void Thermo::check_press_scalar(const std::string &keyword)
{
if (!pressure)
error->all(FLERR, "Thermo keyword {} in variable requires thermo to use/init press", keyword);
if (update->first_update == 0)
error->all(FLERR,"Thermo keyword {} cannot be invoked before first run",keyword);
if (!pressure->is_initialized())
error->all(FLERR,"Thermo keyword {} cannot be invoked before initialization by a run",keyword);
if (!(pressure->invoked_flag & Compute::INVOKED_SCALAR)) {
pressure->compute_scalar();
pressure->invoked_flag |= Compute::INVOKED_SCALAR;
Expand All @@ -1185,8 +1185,8 @@ void Thermo::check_press_vector(const std::string &keyword)
{
if (!pressure)
error->all(FLERR, "Thermo keyword {} in variable requires thermo to use/init press", keyword);
if (update->first_update == 0)
error->all(FLERR,"Thermo keyword {} cannot be invoked before first run",keyword);
if (!pressure->is_initialized())
error->all(FLERR,"Thermo keyword {} cannot be invoked before initialization by a run",keyword);
if (!(pressure->invoked_flag & Compute::INVOKED_VECTOR)) {
pressure->compute_vector();
pressure->invoked_flag |= Compute::INVOKED_VECTOR;
Expand Down
55 changes: 33 additions & 22 deletions src/variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1505,8 +1505,9 @@ double Variable::evaluate(char *str, Tree **tree, int ivar)

if (nbracket == 0 && compute->scalar_flag && lowercase) {

if (update->first_update == 0)
print_var_error(FLERR,"Variable formula compute cannot be invoked before first run",ivar);
if (!compute->is_initialized())
print_var_error(FLERR,"Variable formula compute cannot be invoked before "
"initialization by a run",ivar);
if (!(compute->invoked_flag & Compute::INVOKED_SCALAR)) {
compute->compute_scalar();
compute->invoked_flag |= Compute::INVOKED_SCALAR;
Expand All @@ -1527,8 +1528,9 @@ double Variable::evaluate(char *str, Tree **tree, int ivar)
if (index1 > compute->size_vector &&
compute->size_vector_variable == 0)
print_var_error(FLERR,"Variable formula compute vector is accessed out-of-range",ivar,0);
if (update->first_update == 0)
print_var_error(FLERR,"Variable formula compute cannot be invoked before first run",ivar);
if (!compute->is_initialized())
print_var_error(FLERR,"Variable formula compute cannot be invoked before "
"initialization by a run",ivar);
if (!(compute->invoked_flag & Compute::INVOKED_VECTOR)) {
compute->compute_vector();
compute->invoked_flag |= Compute::INVOKED_VECTOR;
Expand All @@ -1553,8 +1555,9 @@ double Variable::evaluate(char *str, Tree **tree, int ivar)
print_var_error(FLERR,"Variable formula compute array is accessed out-of-range",ivar,0);
if (index2 > compute->size_array_cols)
print_var_error(FLERR,"Variable formula compute array is accessed out-of-range",ivar,0);
if (update->first_update == 0)
print_var_error(FLERR,"Variable formula compute cannot be invoked before first run",ivar);
if (!compute->is_initialized())
print_var_error(FLERR,"Variable formula compute cannot be invoked before "
"initialization by a run",ivar);
if (!(compute->invoked_flag & Compute::INVOKED_ARRAY)) {
compute->compute_array();
compute->invoked_flag |= Compute::INVOKED_ARRAY;
Expand All @@ -1580,8 +1583,9 @@ double Variable::evaluate(char *str, Tree **tree, int ivar)
print_var_error(FLERR,"Compute global vector in atom-style variable formula",ivar);
if (compute->size_vector == 0)
print_var_error(FLERR,"Variable formula compute vector is zero length",ivar);
if (update->first_update == 0)
print_var_error(FLERR,"Variable formula compute cannot be invoked before first run",ivar);
if (!compute->is_initialized())
print_var_error(FLERR,"Variable formula compute cannot be invoked before "
"initialization by a run",ivar);
if (!(compute->invoked_flag & Compute::INVOKED_VECTOR)) {
compute->compute_vector();
compute->invoked_flag |= Compute::INVOKED_VECTOR;
Expand All @@ -1604,8 +1608,9 @@ double Variable::evaluate(char *str, Tree **tree, int ivar)
print_var_error(FLERR,"Compute global vector in atom-style variable formula",ivar);
if (compute->size_array_rows == 0)
print_var_error(FLERR,"Variable formula compute array is zero length",ivar);
if (update->first_update == 0)
print_var_error(FLERR,"Variable formula compute cannot be invoked before first run",ivar);
if (!compute->is_initialized())
print_var_error(FLERR,"Variable formula compute cannot be invoked before "
"initialization by a run",ivar);
if (!(compute->invoked_flag & Compute::INVOKED_ARRAY)) {
compute->compute_array();
compute->invoked_flag |= Compute::INVOKED_ARRAY;
Expand All @@ -1623,8 +1628,9 @@ double Variable::evaluate(char *str, Tree **tree, int ivar)
} else if (nbracket == 1 && compute->peratom_flag &&
compute->size_peratom_cols == 0) {

if (update->first_update == 0)
print_var_error(FLERR,"Variable formula compute cannot be invoked before first run",ivar);
if (!compute->is_initialized())
print_var_error(FLERR,"Variable formula compute cannot be invoked before "
"initialization by a run",ivar);
if (!(compute->invoked_flag & Compute::INVOKED_PERATOM)) {
compute->compute_peratom();
compute->invoked_flag |= Compute::INVOKED_PERATOM;
Expand All @@ -1640,8 +1646,9 @@ double Variable::evaluate(char *str, Tree **tree, int ivar)

if (index2 > compute->size_peratom_cols)
print_var_error(FLERR,"Variable formula compute array is accessed out-of-range",ivar,0);
if (update->first_update == 0)
print_var_error(FLERR,"Variable formula compute cannot be invoked before first run",ivar);
if (!compute->is_initialized())
print_var_error(FLERR,"Variable formula compute cannot be invoked before "
"initialization by a run",ivar);
if (!(compute->invoked_flag & Compute::INVOKED_PERATOM)) {
compute->compute_peratom();
compute->invoked_flag |= Compute::INVOKED_PERATOM;
Expand All @@ -1663,8 +1670,9 @@ double Variable::evaluate(char *str, Tree **tree, int ivar)
print_var_error(FLERR,"Per-atom compute in equal-style variable formula",ivar);
if (treetype == VECTOR)
print_var_error(FLERR,"Per-atom compute in vector-style variable formula",ivar);
if (update->first_update == 0)
print_var_error(FLERR,"Variable formula compute cannot be invoked before first run",ivar);
if (!compute->is_initialized())
print_var_error(FLERR,"Variable formula compute cannot be invoked before "
"initialization by a run",ivar);
if (!(compute->invoked_flag & Compute::INVOKED_PERATOM)) {
compute->compute_peratom();
compute->invoked_flag |= Compute::INVOKED_PERATOM;
Expand All @@ -1687,8 +1695,9 @@ double Variable::evaluate(char *str, Tree **tree, int ivar)
print_var_error(FLERR,"Per-atom compute in vector-style variable formula",ivar);
if (index1 > compute->size_peratom_cols)
print_var_error(FLERR,"Variable formula compute array is accessed out-of-range",ivar,0);
if (update->first_update == 0)
print_var_error(FLERR,"Variable formula compute cannot be invoked before first run",ivar);
if (!compute->is_initialized())
print_var_error(FLERR,"Variable formula compute cannot be invoked before "
"initialization by a run",ivar);
if (!(compute->invoked_flag & Compute::INVOKED_PERATOM)) {
compute->compute_peratom();
compute->invoked_flag |= Compute::INVOKED_PERATOM;
Expand Down Expand Up @@ -4163,8 +4172,9 @@ int Variable::special_function(char *word, char *contents, Tree **tree, Tree **t
print_var_error(FLERR,mesg,ivar);
}
if (index == 0 && compute->vector_flag) {
if (update->first_update == 0)
print_var_error(FLERR,"Variable formula compute cannot be invoked before first run",ivar);
if (!compute->is_initialized())
print_var_error(FLERR,"Variable formula compute cannot be invoked before "
"initialization by a run",ivar);
if (!(compute->invoked_flag & Compute::INVOKED_VECTOR)) {
compute->compute_vector();
compute->invoked_flag |= Compute::INVOKED_VECTOR;
Expand All @@ -4174,8 +4184,9 @@ int Variable::special_function(char *word, char *contents, Tree **tree, Tree **t
} else if (index && compute->array_flag) {
if (index > compute->size_array_cols)
print_var_error(FLERR,"Variable formula compute array is accessed out-of-range",ivar,0);
if (update->first_update == 0)
print_var_error(FLERR,"Variable formula compute cannot be invoked before first run",ivar);
if (!compute->is_initialized())
print_var_error(FLERR,"Variable formula compute cannot be invoked before "
"initialization by a run",ivar);
if (!(compute->invoked_flag & Compute::INVOKED_ARRAY)) {
compute->compute_array();
compute->invoked_flag |= Compute::INVOKED_ARRAY;
Expand Down

0 comments on commit a4a206e

Please sign in to comment.