53 #include "IterationPack_AlgorithmState.hpp"
54 #include "IterationPack_AlgorithmTracker.hpp"
55 #include "IterationPack_AlgorithmStep.hpp"
57 #include "Teuchos_Assert.hpp"
60 namespace IterationPack {
129 enum { DOES_NOT_EXIST = 1000 };
131 enum ERunningState { NOT_RUNNING = 0, RUNNING = 1, RUNNING_BEING_CONFIGURED = 2 };
135 {
public:
DoesNotExist(
const std::string& what_arg) : std::logic_error(what_arg) {}};
139 {
public:
AlreadyExists(
const std::string& what_arg) : std::logic_error(what_arg) {}};
147 {
public:
InvalidRunningState(
const std::string& what_arg) : std::logic_error(what_arg) {}};
151 {
public:
InvalidConfigChange(
const std::string& what_arg) : std::logic_error(what_arg) {}};
318 ,
const std::string& assoc_step_name)
const;
420 ,
const std::string& assoc_step_name,
const step_ptr_t& assoc_step);
486 virtual void do_step_next(
const std::string& step_name);
530 virtual bool do_step(
const std::string& step_name);
660 void get_final_step_stats(
size_t step,
double* total,
double* average,
double* min,
double* max,
double* percent)
const;
670 template<
class T_Step_ptr>
671 struct step_ptr_and_name {
673 step_ptr_and_name(
const T_Step_ptr& _step_ptr
674 ,
const std::string& _name )
675 : step_ptr(_step_ptr), name(_name)
684 typedef step_ptr_and_name<step_ptr_t> steps_ele_t;
686 typedef std::deque<steps_ele_t> steps_t;
689 typedef step_ptr_and_name<step_ptr_t> assoc_steps_ele_list_ele_t;
691 typedef std::list<assoc_steps_ele_list_ele_t> assoc_steps_ele_list_t;
693 struct assoc_steps_ele_t {
695 assoc_steps_ele_list_t& operator[](
int i)
696 {
return assoc_steps_lists_[i]; }
698 const assoc_steps_ele_list_t& operator[](
int i)
const
699 {
return assoc_steps_lists_[i]; }
701 assoc_steps_ele_list_t assoc_steps_lists_[2];
706 typedef std::deque<assoc_steps_ele_t> assoc_steps_t;
709 enum ETerminateStatus { STATUS_KEEP_RUNNING, STATUS_TERMINATE_TRUE, STATUS_TERMINATE_FALSE };
712 template<
class T_ele>
716 name_comp(
const std::string& name) : name_(name) {}
718 bool operator()(
const T_ele& ele) {
return ele.name == name_; }
720 const std::string& name_;
723 typedef std::vector<double> step_times_t;
727 TIME_STAT_TOTALS_OFFSET = 0,
728 TIME_STAT_AV_OFFSET = 1,
729 TIME_STAT_MIN_OFFSET = 2,
730 TIME_STAT_MAX_OFFSET = 3,
731 TIME_STAT_PERCENT_OFFSET = 4;
733 enum { NUM_STEP_TIME_STATS = 5 };
736 typedef void (AlgorithmStep::*inform_func_ptr_t)(
748 #ifdef DOXYGEN_COMPILE
749 AlgorithmState *
state;
750 AlgorithmTracker *
track;
751 AlgorithmStep *steps;
780 double max_run_time_;
783 ETerminateStatus terminate_status_;
789 const std::string* next_step_name_;
792 bool do_step_next_called_;
802 std::string saved_curr_step_name_;
806 std::string saved_next_step_name_;
823 assoc_steps_t assoc_steps_;
834 mutable step_times_t step_times_;
850 mutable bool time_stats_computed_;
853 mutable double total_time_;
863 poss_type validate(
const assoc_steps_ele_list_t& assoc_list,
poss_type assoc_step_poss,
int past_end = 0)
const;
875 void validate_not_curr_step(
poss_type step_poss)
const;
878 void validate_not_next_step(
const std::string& step_name)
const;
882 steps_t::iterator step_itr(
const std::string& step_name);
885 steps_t::const_iterator step_itr(
const std::string& step_name)
const;
889 steps_t::iterator step_itr_and_assert(
const std::string& step_name);
892 steps_t::const_iterator step_itr_and_assert(
const std::string& step_name)
const;
896 static assoc_steps_ele_list_t::iterator assoc_step_itr(assoc_steps_ele_list_t& assoc_list
897 ,
const std::string& assoc_step_name);
900 static assoc_steps_ele_list_t::const_iterator assoc_step_itr(
const assoc_steps_ele_list_t& assoc_list
901 ,
const std::string& assoc_step_name);
907 bool imp_do_assoc_steps(EAssocStepType type);
910 void imp_inform_steps(inform_func_ptr_t inform_func_ptr);
913 void imp_print_algorithm(std::ostream& out,
bool print_steps)
const;
916 EDoStepType do_step_type(EAssocStepType assoc_step_type);
919 EAlgoReturn finalize_algorithm( EAlgoReturn algo_return );
922 void compute_final_time_stats()
const;
925 void look_for_interrupt();
930 static void interrupt();
985 {
return running_state_; }
990 Algorithm::steps_t::iterator Algorithm::step_itr(
const std::string& step_name)
992 return std::find_if( steps_.begin() , steps_.end()
993 , name_comp<steps_ele_t>(step_name) );
997 Algorithm::steps_t::const_iterator Algorithm::step_itr(
const std::string& step_name)
const
999 return std::find_if( steps_.begin() , steps_.end()
1000 , name_comp<steps_ele_t>(step_name) );
1004 Algorithm::assoc_steps_ele_list_t::iterator Algorithm::assoc_step_itr(
1005 assoc_steps_ele_list_t& assoc_list,
const std::string& assoc_step_name)
1007 return std::find_if( assoc_list.begin() , assoc_list.end()
1008 , name_comp<assoc_steps_ele_list_ele_t>(assoc_step_name) );
1012 Algorithm::assoc_steps_ele_list_t::const_iterator Algorithm::assoc_step_itr(
1013 const assoc_steps_ele_list_t& assoc_list,
const std::string& assoc_step_name)
1015 return std::find_if( assoc_list.begin() , assoc_list.end()
1016 , name_comp<assoc_steps_ele_list_ele_t>(assoc_step_name) );
1020 EDoStepType Algorithm::do_step_type(EAssocStepType assoc_step_type) {
1021 switch(assoc_step_type) {
1022 case PRE_STEP :
return DO_PRE_STEP;
1023 case POST_STEP :
return DO_POST_STEP;
1031 #endif // ALGORITHM_H
virtual EAlgoReturn do_algorithm(poss_type step_poss=1)
Called by clients to begin an algorithm.
Teuchos::RCP< AlgorithmTracker > track_ptr_t
Algorithm()
Constructs an algorithm with no steps and a default of max_iter() == 100.
virtual poss_type get_step_poss(const std::string &step_name) const
Return the possition in the major loop of a named step.
ERunningState running_state() const
Return the current running state of this algorithm object.
virtual void print_steps(std::ostream &out) const
Print out just a listing of the steps, their positions in the algorithm and the subclasses.
state_ptr_t & get_state()
virtual void begin_config_update()
Changes from running_state() == RUNNING to running_state() == RUNNING_BEING_CONFIGURED.
virtual void insert_assoc_step(poss_type step_poss, EAssocStepType type, poss_type assoc_step_poss, const std::string &assoc_step_name, const step_ptr_t &assoc_step)
Insert an pre or post step into for the main step step_poss into the possition assoc_step_poss.
Thrown if name or id does not exist.
Thrown if an invalid control protocal is used.
virtual bool algo_timing() const
void get_step_times_k(int offset, double step_times[]) const
Returns the step_times for iteration offset.
virtual const std::string & what_is_next_step_name() const
Returns the name of the next step this will call the next time it calls a step.
void get_final_step_stats(size_t step, double *total, double *average, double *min, double *max, double *percent) const
Returns the final statistics for a given step Do not call when algorithm is running.
virtual void insert_step(poss_type step_poss, const std::string &step_name, const step_ptr_t &step)
Insert a step object with the name step_name into the possition step_poss.
virtual int num_steps() const
Return the number of main steps.
virtual void change_step_name(poss_type step_poss, const std::string &new_name)
Change the name of an existing step.
STANDARD_MEMBER_COMPOSITION_MEMBERS(std::string, interrupt_file_name)
Name of an file that will cause the algorithm to terminate.
virtual void print_algorithm(std::ostream &out) const
Print out the entire algorithm by calling print_step(...) on the step objects.
virtual bool do_step(const std::string &step_name)
Calls do_step() on all of the pre step objects the step object and the post step objects in order for...
virtual void terminate(bool success)
Called by step objects to terminate the algorithm.
void set_state(const state_ptr_t &state)
virtual void replace_step(poss_type step_poss, const step_ptr_t &step)
Replace the step object of an existing step.
virtual void do_step_next(const std::string &step_name)
Called by step objects to set the step (given its name) that this will envoke the next time this call...
virtual poss_type what_is_next_step_poss() const
Returns the possition of the next step this will call the next time it calls a step.
Abstacts a set of iteration quantities for an iterative algorithm.
virtual poss_type get_assoc_step_poss(poss_type step_poss, EAssocStepType type, const std::string &assoc_step_name) const
Return the possition of the pre or post step for the main step_poss.
AlgorithmTracker & track()
virtual int num_assoc_steps(poss_type step_poss, EAssocStepType type) const
Return the number of pre or post steps for the main step step_poss.
Teuchos::RCP< AlgorithmState > state_ptr_t
virtual step_ptr_t & get_assoc_step(poss_type step_poss, EAssocStepType type, poss_type assoc_step_poss)
Return the RCP<...> object for the associated step object at step_poss and assoc_step_poss.
virtual step_ptr_t & get_step(poss_type step_poss)
Return the RCP<...> object for the step object at step_poss.
virtual void end_config_update()
Changes from running_state() == RUNNING_BEING_CONFIGURED to running_state() == RUNNING.
virtual const std::string & get_step_name(poss_type step_poss) const
Return the name of a step given its possition.
virtual double max_run_time() const
track_ptr_t & get_track()
Thrown if a member function is called while this is in an invalid running state.. ...
Acts as the central hub for an iterative algorithm.
Thrown if Algorithm was interrupted by the user.
virtual void remove_assoc_step(poss_type step_poss, EAssocStepType type, poss_type assoc_step_poss)
Remove an pre or post step for the main step step_poss in the possition assoc_step_poss.
virtual void print_algorithm_times(std::ostream &out) const
Outputs table of times for each step, cummulative times and other statistics.
Used to ouput iteration results and other information.
virtual const std::string & get_assoc_step_name(poss_type step_poss, EAssocStepType type, poss_type assoc_step_poss) const
Return the name of the pre or post step at step_poss and at assoc_step_poss.
Thrown if a member function is called while this is in an invalid running state.. ...
virtual void remove_step(poss_type step_poss)
Remove an existing step object and all of its pre and post steps.
Thrown if name already exists.
virtual size_t max_iter() const
Teuchos::RCP< AlgorithmStep > step_ptr_t
virtual void set_algo_timing(bool algo_timing)
Causes algorithm to be timed.
void set_track(const track_ptr_t &track)
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)