Main Page | Namespace List | Class Hierarchy | Alphabetical List | 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.7.2.1 2005/06/29 17:49:35 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_Constraints_Bounds.hpp"
    00045 #include "APPSPACK_Solver.hpp"
    00046 #include "APPSPACK_Vector.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     APPSPACK::Constraints::Bounds bounds(params.sublist("Bounds"));
    00100
    00101     // Instantiate the solver
    00102     APPSPACK::Solver solver(params.sublist("Solver"), executor, bounds);
    00103
    00104     // Solver the problem
    00105     APPSPACK::Solver::State state = solver.solve();
    00106
    00107     // Send a termination command to every worker
    00108     APPSPACK::GCI::initSend();
    00109     APPSPACK::GCI::pack(1);
    00110     for (int i = 0; i < nWorkers; i ++)
    00111       APPSPACK::GCI::send(APPSPACK::Executor::MPI::Terminate, i+1);
    00112
    00113   }
    00114
    00115   // *** Worker ***
    00116   else
    00117   {
    00118     // Unpack parameter list sent by master
    00119     APPSPACK::GCI::recv(APPSPACK::Executor::MPI::Init,0);
    00120     APPSPACK::Parameter::List params;
    00121     params.unpack();
    00122
    00123     // Set up evaluator
    00124     APPSPACK::Evaluator::SystemCall evaluator(params);
    00125
    00126     // Continuously receive and process incoming messages
    00127     while (1)
    00128     {
    00129       // Blocking receive for the next message
    00130       int msgtag, junk;
    00131       APPSPACK::GCI::recv();
    00132       APPSPACK::GCI::bufinfo(msgtag, junk);
    00133
    00134       // Check for termination
    00135       if (msgtag == APPSPACK::Executor::MPI::Terminate)
    00136         break;
    00137
    00138       // Local vars to be packed and unpacked
    00139       int tag;
    00140       APPSPACK::Vector x;
    00141       bool isF;
    00142       double f;
    00143       string msg;
    00144
    00145       // Unpack the latest message
    00146       APPSPACK::GCI::unpack(tag);
    00147       APPSPACK::GCI::unpack(x);
    00148
    00149       // Evaluate the function
    00150       evaluator(tag,x,isF,f,msg);
    00151
    00152       // Send a reply
    00153       APPSPACK::GCI::initSend();
    00154       APPSPACK::GCI::pack(tag);
    00155       APPSPACK::GCI::pack(isF);
    00156       APPSPACK::GCI::pack(f);
    00157       APPSPACK::GCI::pack(msg);
    00158       APPSPACK::GCI::send(APPSPACK::Executor::MPI::Feval,0);
    00159
    00160     }
    00161
    00162   } // worker
    00163
    00164   APPSPACK::GCI::exit();
    00165
    00166 } // main
    00167

© Sandia Corporation | Site Contact | Privacy and Security

Generated on Wed Dec 14 18:41:04 2005 for APPSPACK 4.0.2 by 1.3.8 written by Dimitri van Heesch, © 1997-2002