Skip to content

Commit

Permalink
Added store and load functions for more map types, ref idaholab#8225
Browse files Browse the repository at this point in the history
  • Loading branch information
jessecarterMOOSE committed Dec 19, 2016
1 parent f31b00a commit 4fdc546
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 2 deletions.
72 changes: 72 additions & 0 deletions framework/include/restart/DataIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "HashMap.h"
#include "MooseError.h"
#include "Backup.h"
#include "MooseRandom.h"

// libMesh includes
#include "libmesh/vector_value.h"
Expand Down Expand Up @@ -82,6 +83,12 @@ inline void storeHelper(std::ostream & stream, std::set<P> & data, void * contex
template<typename P, typename Q>
inline void storeHelper(std::ostream & stream, std::map<P,Q> & data, void * context);

/**
* Unordered_map helper routine
*/
template<typename P, typename Q>
inline void storeHelper(std::ostream & stream, std::unordered_map<P,Q> & data, void * context);

/**
* HashMap helper routine
*/
Expand Down Expand Up @@ -124,6 +131,12 @@ inline void loadHelper(std::istream & stream, std::set<P> & data, void * context
template<typename P, typename Q>
inline void loadHelper(std::istream & stream, std::map<P,Q> & data, void * context);

/**
* Unordered_map helper routine
*/
template<typename P, typename Q>
inline void loadHelper(std::istream & stream, std::unordered_map<P,Q> & data, void * context);

/**
* Hashmap helper routine
*/
Expand Down Expand Up @@ -254,6 +267,27 @@ dataStore(std::ostream & stream, std::map<T,U> & m, void * context)
}
}

template<typename T, typename U>
inline void
dataStore(std::ostream & stream, std::unordered_map<T,U> & m, void * context)
{
// First store the size of the map
unsigned int size = m.size();
stream.write((char *) &size, sizeof(size));

typename std::unordered_map<T,U>::iterator it = m.begin();
typename std::unordered_map<T,U>::iterator end = m.end();

for (; it != end; ++it)
{
T & key = const_cast<T&>(it->first);

storeHelper(stream, key, context);

storeHelper(stream, it->second, context);
}
}

template<typename T, typename U>
inline void
dataStore(std::ostream & stream, HashMap<T,U> & m, void * context)
Expand Down Expand Up @@ -290,6 +324,7 @@ template<> void dataStore(std::ostream & stream, Elem * & e, void * context);
template<> void dataStore(std::ostream & stream, Node * & n, void * context);
template<> void dataStore(std::ostream & stream, std::stringstream & s, void * context);
template<> void dataStore(std::ostream & stream, std::stringstream * & s, void * context);
template<> void dataStore(std::ostream & stream, MooseRandom & v, void * context);

// global load functions

Expand Down Expand Up @@ -398,6 +433,26 @@ dataLoad(std::istream & stream, std::map<T,U> & m, void * context)
}
}

template<typename T, typename U>
inline void
dataLoad(std::istream & stream, std::unordered_map<T,U> & m, void * context)
{
m.clear();

// First read the size of the map
unsigned int size = 0;
stream.read((char *) &size, sizeof(size));

for (unsigned int i = 0; i < size; i++)
{
T key;
loadHelper(stream, key, context);

U & value = m[key];
loadHelper(stream, value, context);
}
}


template<typename T, typename U>
inline void
Expand Down Expand Up @@ -432,6 +487,7 @@ template<> void dataLoad(std::istream & stream, Elem * & e, void * context);
template<> void dataLoad(std::istream & stream, Node * & e, void * context);
template<> void dataLoad(std::istream & stream, std::stringstream & s, void * context);
template<> void dataLoad(std::istream & stream, std::stringstream * & s, void * context);
template<> void dataLoad(std::istream & stream, MooseRandom & v, void * context);

// Scalar Helper Function
template<typename P>
Expand Down Expand Up @@ -481,6 +537,14 @@ storeHelper(std::ostream & stream, std::map<P,Q> & data, void * context)
dataStore(stream, data, context);
}

// Unordered_map Helper Function
template<typename P, typename Q>
inline void
storeHelper(std::ostream & stream, std::unordered_map<P,Q> & data, void * context)
{
dataStore(stream, data, context);
}

// HashMap Helper Function
template<typename P, typename Q>
inline void
Expand Down Expand Up @@ -537,6 +601,14 @@ loadHelper(std::istream & stream, std::map<P,Q> & data, void * context)
dataLoad(stream, data, context);
}

// Unordered_map Helper Function
template<typename P, typename Q>
inline void
loadHelper(std::istream & stream, std::unordered_map<P,Q> & data, void * context)
{
dataLoad(stream, data, context);
}

// HashMap Helper Function
template<typename P, typename Q>
inline void
Expand Down
5 changes: 5 additions & 0 deletions framework/include/utils/MooseRandom.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ class MooseRandom
it->second.first = it->second.second;
}

LIBMESH_BEST_UNORDERED_MAP<unsigned int, std::pair<mt_state, mt_state> > getStates() { return _states; }

void setStates(LIBMESH_BEST_UNORDERED_MAP<unsigned int, std::pair<mt_state, mt_state> > states) { _states = states; }


private:

/**
Expand Down
19 changes: 19 additions & 0 deletions framework/src/restart/DataIO.C
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,15 @@ dataStore(std::ostream & stream, std::stringstream * & s, void * context)
dataStore(stream, *s, context);
}

template<>
void
dataStore(std::ostream & stream, MooseRandom & v, void * context)
{
Moose::out << "dataStore(std::ostream & stream, MooseRandom & v, void * context)" << std::endl;
LIBMESH_BEST_UNORDERED_MAP<unsigned int, std::pair<mt_state, mt_state> > states = v.getStates();
storeHelper(stream, states, context);
}

// global load functions

template<>
Expand Down Expand Up @@ -416,3 +425,13 @@ dataLoad(std::istream & stream, std::stringstream * & s, void * context)
{
dataLoad(stream, *s, context);
}

template<>
void
dataLoad(std::istream & stream, MooseRandom & v, void * context)
{
Moose::out << "dataStore(std::istream & stream, MooseRandom & v, void * context)" << std::endl;
LIBMESH_BEST_UNORDERED_MAP<unsigned int, std::pair<mt_state, mt_state> > states;
LoadHelper(stream, states, context);
v.setStates(states);
}
2 changes: 1 addition & 1 deletion test/include/postprocessors/RandomPostprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class RandomPostprocessor : public GeneralPostprocessor

const unsigned int _generator_id;

MooseRandom _random;
MooseRandom & _random;
};

#endif //RANDOMPOSTPROCESSOR_H
3 changes: 2 additions & 1 deletion test/src/postprocessors/RandomPostprocessor.C
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ InputParameters validParams<RandomPostprocessor>()

RandomPostprocessor::RandomPostprocessor(const InputParameters & parameters) :
GeneralPostprocessor(parameters),
_generator_id(getParam<unsigned int>("generator"))
_generator_id(getParam<unsigned int>("generator")),
_random(declareRestartableData<MooseRandom>("random_pps"))
{
_random.seed(_generator_id,getParam<unsigned int>("seed"));
}
Expand Down

0 comments on commit 4fdc546

Please sign in to comment.