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

APPSPACK_Main_MPI.cpp

Go to the documentation of this file.
00001 // $Id: APPSPACK_Main_MPI.cpp,v 1.15 2006/06/30 01:42:08 tgkolda Exp $ 
00002 // $Source: /space/CVS-Acro/acro/packages/appspack/appspack/src/APPSPACK_Main_MPI.cpp,v $ 
00003 
00004 //@HEADER
00005 // ************************************************************************
00006 // 
00007 //          APPSPACK: Asynchronous Parallel Pattern Search
00008 //                 Copyright (2003) Sandia Corporation
00009 // 
00010 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00011 // license for use of this work by or on behalf of the U.S. Government.
00012 // 
00013 // This library is free software; you can redistribute it and/or modify
00014 // it under the terms of the GNU Lesser General Public License as
00015 // published by the Free Software Foundation; either version 2.1 of the
00016 // License, or (at your option) any later version.
00017 //  
00018 // This library is distributed in the hope that it will be useful, but
00019 // WITHOUT ANY WARRANTY; without even the implied warranty of
00020 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00021 // Lesser General Public License for more details.
00022 //                                                                                 
00023 // You should have received a copy of the GNU Lesser General Public
00024 // License along with this library; if not, write to the Free Software
00025 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00026 // USA.                                                                           .
00027 // 
00028 // Questions? Contact Tammy Kolda (tgkolda@sandia.gov) 
00029 // 
00030 // ************************************************************************
00031 //@HEADER
00032 
00038 #include "APPSPACK_GCI.hpp"
00039 #include "APPSPACK_Common.hpp"
00040 #include "APPSPACK_Parameter_List.hpp"
00041 #include "APPSPACK_Utils.hpp"
00042 #include "APPSPACK_Evaluator_SystemCall.hpp"
00043 #include "APPSPACK_Executor_MPI.hpp"
00044 #include "APPSPACK_Solver.hpp"
00045 #include "APPSPACK_Vector.hpp"
00046 #include "APPSPACK_Constraints_Linear.hpp"
00047 
00049 
00054 int main(int argc, char* argv[])
00055 {
00056   // *** Start MPI ***
00057   // (also processes argc and argv which may have had extra "stuff"
00058   // added on by mpirun)
00059   int rank = APPSPACK::GCI::init(argc, argv);
00060  
00061   // *** Check usage, exit if error ***
00062   if (argc < 2)
00063   {
00064     if (rank == 0)
00065       cout << "Usage: " << argv[0] << " <input file>" << endl;
00066     APPSPACK::GCI::exit();
00067     return 1;
00068   }
00069     
00070   // *** Check that there are at least two processes ***
00071   int nWorkers = APPSPACK::GCI::getNumProcs() - 1;
00072   if (nWorkers < 1)
00073   {
00074     cerr << "Error: This program requires at least 2 processes." << endl;
00075     APPSPACK::GCI::exit();
00076     return 1;
00077   }
00078   
00079   // *** Master ***
00080   if (rank == 0)        
00081   {
00082 
00083     // Read the input file
00084     APPSPACK::Parameter::List params;
00085     bool retCode = APPSPACK::parseTextInputFile(argv[1], params);
00086     if (retCode == false)
00087       return 1;
00088 
00089     // Send the evaluator information to each worker
00090     APPSPACK::GCI::initSend();
00091     params.sublist("Evaluator").pack();
00092     for (int i = 0; i < nWorkers; i ++)
00093       APPSPACK::GCI::send(APPSPACK::Executor::MPI::Init, i+1);
00094 
00095     // Create an executor object
00096     APPSPACK::Executor::MPI executor;
00097 
00098     // Create a constraints object
00099     string constraintType = "Linear";
00100     if (!params.isParameterSublist("Linear"))
00101       constraintType = "Bounds";
00102 
00103     APPSPACK::Constraints::Linear linear(params.sublist(constraintType));
00104     
00105     // Instantiate the solver
00106     APPSPACK::Solver solver(params.sublist("Solver"), executor, linear);
00107 
00108     // Solver the problem
00109     APPSPACK::Solver::State state = solver.solve();
00110 
00111     // Send a termination command to every worker
00112     APPSPACK::GCI::initSend();
00113     APPSPACK::GCI::pack(1);
00114     for (int i = 0; i < nWorkers; i ++)
00115       APPSPACK::GCI::send(APPSPACK::Executor::MPI::Terminate, i+1);
00116 
00117   }
00118 
00119   // *** Worker ***
00120   else                  
00121   {
00122     // Unpack parameter list sent by master
00123     APPSPACK::GCI::recv(APPSPACK::Executor::MPI::Init,0);
00124     APPSPACK::Parameter::List params;
00125     params.unpack();
00126 
00127     // Set up evaluator
00128     APPSPACK::Evaluator::SystemCall evaluator(params);
00129     
00130     // Continuously receive and process incoming messages
00131     while (1)
00132     {
00133       // Blocking receive for the next message
00134       int msgtag, junk;
00135       APPSPACK::GCI::recv();
00136       APPSPACK::GCI::bufinfo(msgtag, junk);
00137       
00138       // Check for termination
00139       if (msgtag == APPSPACK::Executor::MPI::Terminate) 
00140         break;
00141 
00142       // Local vars to be packed and unpacked
00143       int tag;
00144       APPSPACK::Vector x;
00145       bool isF;
00146       APPSPACK::Vector f;
00147       string msg;
00148 
00149       // Unpack the latest message
00150       APPSPACK::GCI::unpack(tag);
00151       APPSPACK::GCI::unpack(x);
00152 
00153       // Evaluate the function
00154       evaluator(tag,x,f,msg);
00155 
00156       // Send a reply
00157       APPSPACK::GCI::initSend();
00158       APPSPACK::GCI::pack(tag);
00159       APPSPACK::GCI::pack(f);
00160       APPSPACK::GCI::pack(msg);
00161       APPSPACK::GCI::send(APPSPACK::Executor::MPI::Feval,0);
00162 
00163     }
00164 
00165   } // worker
00166 
00167   APPSPACK::GCI::exit();
00168 
00169 } // main
00170 

 

© 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