Skip to content

Commit

Permalink
Change client constructor to be called during dump
Browse files Browse the repository at this point in the history
creation and use environment variable to choose if a
cluster is being used.
  • Loading branch information
mellis13 committed Nov 4, 2021
1 parent 3b799ac commit 5e861a1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
43 changes: 24 additions & 19 deletions src/SMARTSIM/dump_atom_smartsim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,15 @@
using namespace LAMMPS_NS;

DumpAtomSmartSim::DumpAtomSmartSim(LAMMPS *lmp, int narg, char **arg)
: DumpAtom(lmp, narg, arg)
: DumpAtom(lmp, narg, arg),
_client(_use_cluster())
{
SmartRedis::Client* client = NULL;
this->_client = NULL;
try {
Client* client = new Client(true);
this->_client = client;
}
catch(std::exception& e) {
throw std::runtime_error(e.what());
}
catch(...) {
throw std::runtime_error("A non-standard exception "\
"was encountered during SmartRedis client "\
"construction.");
}
}

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

DumpAtomSmartSim::~DumpAtomSmartSim()
{
if(this->_client != NULL)
delete this->_client;
}

/* ---------------------------------------------------------------------- */
Expand All @@ -78,7 +63,6 @@ void DumpAtomSmartSim::write()
boxyz = domain->yz;
}


/* Construct DataSet object with unique
name based on user prefix, MPI rank, and
timestep
Expand Down Expand Up @@ -193,7 +177,7 @@ void DumpAtomSmartSim::write()

/* Send the DataSet to the SmartSim experiment database
*/
this->_client->put_dataset(dataset);
this->_client.put_dataset(dataset);

/* Free temporary memory needed to preprocess LAMMPS output
*/
Expand Down Expand Up @@ -243,3 +227,24 @@ void DumpAtomSmartSim::_pack_buf_into_array(T* data, int length,
data[c++] = buf[i];
}
}

bool DumpAtomSmartSim::_use_cluster()
{
char* use_cluster = std::getenv("SMARTREDIS_USE_CLUSTER");

// If the environment variable is not present, return false
if (use_cluster == NULL)
return false;

// Convert the environment variable value to lowercase
char* c = use_cluster;
while((*c)!=0) {
(*c) = std::tolower(*c);
c++;
}

if(std::strcmp(use_cluster, "true")==0)
return true;

return false;
}
23 changes: 18 additions & 5 deletions src/SMARTSIM/dump_atom_smartsim.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,24 @@ class DumpAtomSmartSim : public DumpAtom
virtual void write();
virtual void init_style();
private:
SmartRedis::Client* _client;
std::string _make_dataset_key();
template <typename T>
void _pack_buf_into_array(T* data, int length,
int start_pos, int stride);

// SmartRedis client object
SmartRedis::Client _client;

// Function to create a SmartRedis Dataset key
// to prevent key collisions
std::string _make_dataset_key();

// Function to take LAMMPS data structure and
// convert to array suitable for storage
template <typename T>
void _pack_buf_into_array(T* data, int length,
int start_pos, int stride);

// Function to read an environment variable to
// determine if a cluster is being used
bool _use_cluster();

};
}

Expand Down

0 comments on commit 5e861a1

Please sign in to comment.