1 /*  _______________________________________________________________________
2 
3     DAKOTA: Design Analysis Kit for Optimization and Terascale Applications
4     Copyright 2014-2020 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
5     This software is distributed under the GNU Lesser General Public License.
6     For more information, see the README file in the top Dakota directory.
7     _______________________________________________________________________ */
8 
9 //- Class:        DirectApplicInterface
10 //- Description:  Derived interface class managing cases where analysis code
11 //-               simulators are linked into the code and may be invoked
12 //-               directly. This manages parts common to all direct simulators.
13 //- Owner:        Mike Eldred, Brian Adams
14 //- Version: $Id: DirectApplicInterface.hpp 7024 2010-10-16 01:24:42Z mseldre $
15 
16 #ifndef DIRECT_APPLIC_INTERFACE_H
17 #define DIRECT_APPLIC_INTERFACE_H
18 
19 #include "ApplicationInterface.hpp"
20 //#ifndef OSF
21 //#include <pthread.h>
22 //#endif
23 
24 namespace Dakota {
25 
26 // BMA: Need another design for managing these enums as they (mostly)
27 // belong in TestDriverInterface, but are primarily used in this
28 // class. Consider the vision for using them in plug-in or service
29 // interfaces like Matlab
30 
31 /// enumeration of possible variable types (to index to names)
32 enum var_t { VAR_x1, VAR_x2, VAR_x3, // generic (Rosenbrock, Ishigami)
33 	     VAR_b, VAR_h, VAR_P, VAR_M, VAR_Y, // short column
34 	     VAR_w, VAR_t, VAR_R, VAR_E, VAR_X, VAR_area_type, /* VAR_Y, */ // cantilever beam
35 	     VAR_Fs, VAR_P1, VAR_P2, VAR_P3, VAR_B, VAR_D, VAR_H,
36 	     VAR_F0, VAR_d, /* VAR_b, VAR_h, VAR_E */ // steel column
37 	     VAR_MForm, // mf_*() test functions
38        VAR_x, VAR_xi, VAR_Af, VAR_Ac }; //Problem18
39 //enum x3_var_t  { X1, X2, X3 }; // generic up to 3 dimensions
40 //enum shc_var_t { SHC_B, SHC_H, SHC_P, SHC_M, SHC_Y }; // short column
41 //enum cb_var_t  { CB_W, CB_T, CB_R, CB_E, CB_X, CB_Y }; // cantilever beam
42 //enum stc_var_t { STC_FS, STC_P1, STC_P2, STC_P3, STC_B, STC_D, STC_H,
43 //		   STC_F0, STC_E }; // steel column
44 
45 /// enumeration of possible direct driver types (to index to names)
46 enum driver_t { NO_DRIVER=0, CANTILEVER_BEAM, MOD_CANTILEVER_BEAM, CANTILEVER_BEAM_ML,
47 		CYLINDER_HEAD, EXTENDED_ROSENBROCK, GENERALIZED_ROSENBROCK,
48 		LF_ROSENBROCK, EXTRA_LF_ROSENBROCK, MF_ROSENBROCK, MODIFIED_ROSENBROCK,
49 		ROSENBROCK, LF_POLY_PROD, POLY_PROD,
50 		GERSTNER, SCALABLE_GERSTNER, LOGNORMAL_RATIO, MULTIMODAL,
51 		PLUGIN_ROSENBROCK, PLUGIN_TEXT_BOOK,
52 		SHORT_COLUMN, LF_SHORT_COLUMN, MF_SHORT_COLUMN,
53 		SIDE_IMPACT_COST, SIDE_IMPACT_PERFORMANCE,
54 		SOBOL_RATIONAL, SOBOL_G_FUNCTION, SOBOL_ISHIGAMI,
55 		STEEL_COLUMN_COST, STEEL_COLUMN_PERFORMANCE, TEXT_BOOK,
56 		TEXT_BOOK1, TEXT_BOOK2, TEXT_BOOK3, TEXT_BOOK_OUU,
57 		SCALABLE_TEXT_BOOK, SCALABLE_MONOMIALS,
58 		MOGATEST1, MOGATEST2, MOGATEST3,
59 		ILLUMINATION, BARNES, BARNES_LF,
60 		HERBIE, SMOOTH_HERBIE, SHUBERT,
61 		SALINAS, MODELCENTER, GENZ, DAMPED_OSCILLATOR,
62 		ANISOTROPIC_QUADRATIC_FORM , BAYES_LINEAR,
63 		STEADY_STATE_DIFFUSION_1D, SS_DIFFUSION_DISCREPANCY,
64 		TRANSIENT_DIFFUSION_1D,	PREDATOR_PREY, PROBLEM18};
65 
66 /// enumeration for how local variables are stored (values must employ
67 /// a bit representation)
68 enum local_data_t { VARIABLES_MAP=1, VARIABLES_VECTOR=2 };
69 
70 
71 /// Derived application interface class which spawns simulation codes
72 /// and testers using direct procedure calls.
73 
74 /** DirectApplicInterface uses a few linkable simulation codes and several
75     internal member functions to perform parameter to response mappings. */
76 class DirectApplicInterface: public ApplicationInterface
77 {
78 public:
79 
80   //
81   //- Heading: Constructor and destructor
82   //
83 
84   DirectApplicInterface(const ProblemDescDB& problem_db); ///< constructor
85   ~DirectApplicInterface();                               ///< destructor
86 
87   //
88   //- Heading: Virtual function redefinitions
89   //
90 
91   void derived_map(const Variables& vars, const ActiveSet& set,
92 		   Response& response, int fn_eval_id);
93   void derived_map_asynch(const ParamResponsePair& pair);
94 
95   void wait_local_evaluations(PRPQueue& prp_queue);
96   void test_local_evaluations(PRPQueue& prp_queue);
97 
98   int  synchronous_local_analysis(int analysis_id);
99 
100   const StringArray& analysis_drivers() const;
101 
102   void init_communicators_checks(int max_eval_concurrency);
103   void  set_communicators_checks(int max_eval_concurrency);
104 
105   //void clear_bookkeeping(); // clears threadIdMap
106 
107 protected:
108 
109   //
110   //- Heading: New virtual fns (redefined by derived interface plug-ins)
111   //
112 
113   /// execute the input filter portion of a direct evaluation invocation
114   virtual int derived_map_if(const Dakota::String& if_name);
115   /// execute an analysis code portion of a direct evaluation invocation
116   virtual int derived_map_ac(const Dakota::String& ac_name);
117   /// execute the output filter portion of a direct evaluation invocation
118   virtual int derived_map_of(const Dakota::String& of_name);
119 
120   //
121   //- Heading: Methods
122   //
123 
124   /// convenience function for local test simulators which sets per-evaluation
125   /// variable and active set attributes; derived classes
126   /// reimplementing this likely need to invoke the base class API
127   virtual void set_local_data(const Variables& vars, const ActiveSet& set);
128   /// convenience function for local test simulators which sets per-evaluation
129   /// response attributes; derived classes
130   /// reimplementing this likely need to invoke the base class API
131   virtual void set_local_data(const Response& response);
132   /// convenience function for local test simulators which sets per-evaluation
133   /// variable, active set, and response attributes; derived classes
134   /// reimplementing this likely need to invoke the base class API
135   virtual void set_local_data(const Variables& vars, const ActiveSet& set,
136 		      const Response& response);
137 
138   /// convenience function for local test simulators which overlays
139   /// response contributions from multiple analyses using MPI_Reduce
140   void overlay_response(Response& response);
141 
142   //
143   //- Heading: Data
144   //
145 
146   String iFilterName; ///< name of the direct function input filter
147   String oFilterName; ///< name of the direct function output filter
148   driver_t iFilterType; ///< enum type of the direct function input filter
149   driver_t oFilterType; ///< enum type of the direct function output filter
150 
151   // map of pthread id's to function evaluation id's for asynch evaluations
152   //std::map<pthread_t, int> threadIdMap;
153 
154   // data used by direct fns is class scope to allow common utility usage
155   bool gradFlag;  ///< signals use of fnGrads in direct simulator functions
156   bool hessFlag;  ///< signals use of fnHessians in direct simulator functions
157 
158   size_t numFns;  ///< number of functions in fnVals
159   size_t numVars; ///< total number of continuous and discrete variables
160   size_t numACV;  ///< total number of continuous variables
161   size_t numADIV;  ///< total number of discete integer variables
162   size_t numADRV;  ///< total number of discete real variables
163   size_t numADSV;  ///< total number of discete string variables
164   size_t numDerivVars; ///< number of active derivative variables
165 
166   /// bit-wise record of which local data views are active;
167   /// see enum local_data_t
168   unsigned short localDataView;
169 
170   // Keeping a copy of the data passed to derived_map allows common prototypes
171   // with SysCall and ForkApplicInterface in virtual functions (since SysCall
172   // and Fork don't need vars/asv/response outside of write_parameters/
173   // read_responses.
174   //Variables directFnVars; ///< class scope variables object
175   RealVector xC;  ///< continuous variables used within direct simulator fns
176   IntVector  xDI; ///< discrete int variables used within direct simulator fns
177   RealVector xDR; ///< discrete real variables used within direct simulator fns
178   StringMultiArray xDS; ///< discrete string variables used within direct simulator fns
179   StringMultiArray xCLabels;  ///< continuous variable labels
180   StringMultiArray xDILabels; ///< discrete integer variable labels
181   StringMultiArray xDRLabels; ///< discrete real variable labels
182   StringMultiArray xDSLabels; ///< discrete string variable labels
183 
184   std::map<String, var_t>    varTypeMap;    ///< map from variable label to enum
185   std::map<String, driver_t> driverTypeMap; ///< map from driver name to enum
186   std::map<var_t, Real> xCM;  ///< map from var_t enum to continuous value
187   std::map<var_t, int>  xDIM; ///< map from var_t enum to discrete int value
188   std::map<var_t, Real> xDRM; ///< map from var_t enum to discrete real value
189   std::map<var_t, String> xDSM; ///< map from var_t enum to discrete string value
190 
191   /// var_t enumerations corresponding to DVV components
192   std::vector<var_t> varTypeDVV;
193   /// var_t enumerations corresponding to continuous variable labels
194   std::vector<var_t> xCMLabels;
195   /// var_t enumerations corresponding to discrete integer variable labels
196   std::vector<var_t> xDIMLabels;
197   /// var_t enumerations corresponding to discrete real variable labels
198   std::vector<var_t> xDRMLabels;
199   /// var_t enumerations corresponding to discrete string variable labels
200   std::vector<var_t> xDSMLabels;
201 
202   //ActiveSet directFnActSet; // class scope ActiveSet object
203   ShortArray directFnASV; ///< class scope active set vector
204   SizetArray directFnDVV; ///< class scope derivative variables vector
205 
206   //Response  directFnResponse; // class scope response object
207   RealVector      fnVals;    ///< response fn values within direct simulator fns
208   RealMatrix      fnGrads;   ///< response fn gradients w/i direct simulator fns
209   RealSymMatrixArray fnHessians; ///< response fn Hessians within direct fns
210 
211   /// the set of analyses within each function evaluation (from the
212   /// analysis_drivers interface specification)
213   StringArray analysisDrivers;
214   /// conversion of analysisDrivers to driver_t
215   std::vector<driver_t> analysisDriverTypes;
216 
217   /// the index of the active analysis driver within analysisDrivers
218   size_t analysisDriverIndex;
219 
220 private:
221   /// map labels in src to var_t in dest
222   void map_labels_to_enum(StringMultiArrayConstView &src,
223       std::vector<var_t> &dest);
224 };
225 
226 
227 /** This code provides the derived function used by
228     ApplicationInterface::serve_analyses_synch(). */
synchronous_local_analysis(int analysis_id)229 inline int DirectApplicInterface::synchronous_local_analysis(int analysis_id)
230 {
231   analysisDriverIndex = analysis_id-1;
232   return derived_map_ac(analysisDrivers[analysisDriverIndex]);
233 }
234 
235 
analysis_drivers() const236 inline const StringArray& DirectApplicInterface::analysis_drivers() const
237 { return analysisDrivers; }
238 
239 
240 /** Process init issues as warnings since some contexts (e.g.,
241     HierarchSurrModel) initialize more configurations than will be
242     used and DirectApplicInterface allows override by derived plug-ins. */
243 inline void DirectApplicInterface::
init_communicators_checks(int max_eval_concurrency)244 init_communicators_checks(int max_eval_concurrency)
245 {
246   bool warn = true;
247   check_asynchronous(warn, max_eval_concurrency);
248   check_multiprocessor_asynchronous(warn, max_eval_concurrency);
249 }
250 
251 
252 /** Process run-time issues as hard errors. */
253 inline void DirectApplicInterface::
set_communicators_checks(int max_eval_concurrency)254 set_communicators_checks(int max_eval_concurrency)
255 {
256   bool warn = false,  mp1 = check_asynchronous(warn, max_eval_concurrency),
257        mp2 = check_multiprocessor_asynchronous(warn, max_eval_concurrency);
258   if (mp1 || mp2)
259     abort_handler(-1);
260 }
261 
262 
263 //inline void DirectApplicInterface::clear_bookkeeping()
264 //{ threadIdMap.clear(); }
265 
266 
267 inline void DirectApplicInterface::
set_local_data(const Variables & vars,const ActiveSet & set,const Response & response)268 set_local_data(const Variables& vars, const ActiveSet& set,
269 	       const Response& response)
270 { set_local_data(vars, set); set_local_data(response); }
271 
272 } // namespace Dakota
273 
274 #endif
275