Sandia Home Sandia Home
Main Page | Publications | Downloads | Configuration | Running the Code | Solver Parameters | FAQ | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

cev_main.cpp

Go to the documentation of this file.
00001 // $Id: cev_main.cpp,v 1.3 2006/06/30 19:14:31 tgkolda Exp $ 
00002 // $Source: /space/CVS-Acro/acro/packages/appspack/appspack/example-custom-evaluator/cev_main.cpp,v $ 
00003 
00044 #include "cev_evaluator.hpp"    // Custom evaluator
00045 #include "APPSPACK_GCI.hpp"     // Interface to MPI
00046 #include "APPSPACK_Parameter_List.hpp" // Parameter list
00047 #include "APPSPACK_Executor_MPI.hpp" // MPI executor
00048 #include "APPSPACK_Constraints_Linear.hpp" // constraints
00049 #include "APPSPACK_Solver.hpp"  // solver
00050 
00051 
00053 int main(int argc, char* argv[])
00054 {
00055   // *** Start MPI ***
00056   // (also processes argc and argv which may have had extra "stuff"
00057   // added on by mpirun)
00058   int rank = APPSPACK::GCI::init(argc, argv);
00059 
00060   // *** Check that there are at least two processes ***
00061   int nWorkers = APPSPACK::GCI::getNumProcs() - 1;
00062   if (nWorkers < 1)
00063   {
00064     cerr << "Error: This program requires at least 2 processes." << endl;
00065     APPSPACK::GCI::exit();
00066     return 1;
00067   }
00068 
00069   // *** Master ***
00070   if (rank == 0)        
00071   {
00072 
00073     // Set up the parameter list to be passed to the solver
00074 
00075     APPSPACK::Parameter::List params;
00076 
00077     APPSPACK::Vector initialx(2);
00078     initialx[0] = 2e-1;
00079     initialx[1] = 0.3;
00080     params.sublist("Solver").setParameter("Initial X", initialx);
00081 
00082     params.sublist("Solver").setParameter("Debug", 4);
00083 
00084     APPSPACK::Vector lowerbounds(2,-1);
00085     params.sublist("Linear").setParameter("Lower", lowerbounds);
00086 
00087     APPSPACK::Vector upperbounds(2,3);
00088     params.sublist("Linear").setParameter("Upper", upperbounds);
00089 
00090     // Create an executor object
00091     APPSPACK::Executor::MPI executor;
00092     
00093     // Create a constraints object
00094     APPSPACK::Constraints::Linear linear(params.sublist("Linear"));
00095     
00096     // Instantiate the solver
00097     APPSPACK::Solver solver(params.sublist("Solver"), executor, linear);
00098 
00099     // Solver the problem
00100     APPSPACK::Solver::State state = solver.solve();
00101 
00102     // Send a termination command to every worker
00103     APPSPACK::GCI::initSend();
00104     APPSPACK::GCI::pack(1);
00105     for (int i = 0; i < nWorkers; i ++)
00106       APPSPACK::GCI::send(APPSPACK::Executor::MPI::Terminate, i+1);
00107 
00108   }
00109 
00110   // *** Worker ***
00111   else                  
00112   {
00113     // Set up custom evaluator
00114     CustomEvaluator evaluator;
00115     
00116     // Continuously receive and process incoming messages
00117     while (1)
00118     {
00119       // Blocking receive for the next message. 
00120       int msgtag, junk;
00121       APPSPACK::GCI::recv();                // Block for next message
00122       APPSPACK::GCI::bufinfo(msgtag, junk); // Extract the message tag
00123       
00124       // Check for termination based on the message tag.
00125       if (msgtag == APPSPACK::Executor::MPI::Terminate) 
00126         break;
00127 
00128       // Local vars to be packed and unpacked
00129       int tag;
00130       APPSPACK::Vector x;
00131       APPSPACK::Vector f;
00132       string msg;
00133 
00134       // Unpack the latest message - must be done just like this.
00135       APPSPACK::GCI::unpack(tag);
00136       APPSPACK::GCI::unpack(x);
00137 
00138       // Evaluate the function
00139       evaluator(tag,x,f,msg);
00140 
00141       // Send a reply - must be done just like this.
00142       // NOTE FOR USERS UPGRADING TO VERSION 5.0: The variable isf is gone!
00143       APPSPACK::GCI::initSend();
00144       APPSPACK::GCI::pack(tag);
00145       APPSPACK::GCI::pack(f);
00146       APPSPACK::GCI::pack(msg);
00147       APPSPACK::GCI::send(APPSPACK::Executor::MPI::Feval,0);
00148 
00149     }
00150 
00151   } // worker
00152 
00153   APPSPACK::GCI::exit();
00154 
00155 } // main
00156 

 

© Sandia Corporation | Site Contact | Privacy and Security

Generated on Fri Feb 16 10:33:35 2007 for APPSPACK 5.0.1 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2002