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_Evaluator_SystemCall.cpp

Go to the documentation of this file.
00001 // $Id: APPSPACK_Evaluator_SystemCall.cpp,v 1.16 2006/06/30 01:42:08 tgkolda Exp $ 
00002 // $Source: /space/CVS-Acro/acro/packages/appspack/appspack/src/APPSPACK_Evaluator_SystemCall.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_Common.hpp"
00039 #include "APPSPACK_Evaluator_SystemCall.hpp"
00040 #include "APPSPACK_Utils.hpp"
00041 #include "APPSPACK_Float.hpp"
00042 
00043 APPSPACK::Evaluator::SystemCall::SystemCall(const string& executableName_in,      
00044                                             const string& inputPrefix_in,      
00045                                             const string& outputPrefix_in) :
00046   executableName(executableName_in),
00047   inputPrefix(inputPrefix_in),
00048   outputPrefix(outputPrefix_in),
00049   isInputFile(false),
00050   isOutputFile(false)
00051 {
00052 }
00053 
00054 APPSPACK::Evaluator::SystemCall::SystemCall(const Parameter::List& params) :
00055   executableName(params.getParameter("Executable Name", "a.out")),
00056   inputPrefix(params.getParameter("Input Prefix", "input")),
00057   outputPrefix(params.getParameter("Output Prefix", "output")),
00058   isInputFile(false),
00059   isOutputFile(false)
00060 {
00061 }
00062 
00063 void APPSPACK::Evaluator::SystemCall::operator()(int tag_in, 
00064                                                  const Vector& x_in, 
00065                                                  Vector& f_out,
00066                                                  string&  msg_out)
00067 {
00068   createStrings(tag_in);
00069   writeInputFile(x_in);
00070   runProgram();
00071   readOutputFile();
00072   deleteFiles();
00073 
00074   f_out = f;
00075   msg_out = msg;
00076 
00077   return;
00078 }
00079 
00080 
00081 void APPSPACK::Evaluator::SystemCall::createStrings(int tag_in)
00082 {
00083   char tmp[128];
00084   sprintf(tmp, "%d", tag_in);
00085   tag = tmp;
00086 
00087   inputFileName = inputPrefix + "." + tag;
00088   outputFileName = outputPrefix + "." + tag;
00089   execString = executableName + " " + inputFileName + " " + outputFileName + " " + tag;
00090 }
00091 
00092 void APPSPACK::Evaluator::SystemCall::writeInputFile(const Vector& x)
00093 {
00094   ofstream stream;
00095   stream.open(inputFileName.c_str());
00096 
00097   if (!stream) 
00098   {
00099     cerr << "APPSPACK::Evaluator::SystemCall::writeInputFile - Unable to open the following file: " 
00100          << inputFileName << endl;
00101     return;
00102   }
00103 
00104   isInputFile = true;
00105 
00106   stream << x.size() << "\n";
00107 
00108   for (int i = 0; i < x.size(); i ++)
00109     stream << x[i] << "\n";
00110 
00111   stream.close();
00112 }
00113 
00114 
00115 void APPSPACK::Evaluator::SystemCall::runProgram()
00116 {
00117   system(execString.c_str());
00118 }
00119 
00120 void APPSPACK::Evaluator::SystemCall::readOutputFile()
00121 {
00122   ifstream fin;
00123   fin.open(outputFileName.c_str());
00124 
00125   if (!fin) 
00126   {
00127     cerr << "\nWarning - no output file for the point corresponding to tag=" << tag << endl;
00128     f.resize(0);
00129     msg = "No Output File";
00130     return;
00131   }
00132 
00133   isOutputFile = true;
00134     
00135   if (fin.eof())
00136   {
00137     cerr << "\nWarning - empty output file for the point corresponding to tag=" << tag << endl;
00138     f.resize(0);
00139     msg = "Empty Output File";
00140     fin.close();
00141     return;
00142   }
00143 
00144   // Try to fill the vector f
00145   f.resize(0);
00146   string line;
00147   string str;
00148   double val;
00149   while(!fin.eof())
00150   {
00151     getline(fin, line);
00152     string::size_type pos = 0;
00153     getNextString(line,pos,str);
00154 
00155     if (str == "DNE")
00156       f.push_back(dne());
00157     else if (sscanf(str.c_str(), "%le", &val) == 1)
00158       f.push_back(val);
00159     else if (f.size() == 0) 
00160     {
00161       // Error on the 1st line, which may contain a message
00162       cerr << "\nWarning - error reading function value for tag=" << tag << endl;
00163       msg = (line.empty()) ? "No Floating Point Value Found in Output File" : line;
00164       fin.close();
00165       return;
00166     }
00167   }
00168   
00169   msg = "Success";
00170   fin.close();
00171   return;
00172 }
00173 
00174 void APPSPACK::Evaluator::SystemCall::deleteFiles()
00175 {
00176 #ifndef SNL_TFLOPS_ENV
00177   if (isInputFile)
00178     unlink(inputFileName.c_str());
00179   if (isOutputFile)
00180     unlink(outputFileName.c_str());
00181 #endif
00182 
00183   isInputFile = false;
00184   isOutputFile = false;
00185 }
00186 
00187 void APPSPACK::Evaluator::SystemCall::print() const
00188 {
00189   cout << "Using SystemCall Evaluator" << endl;
00190   cout << "Executable Name = " << executableName << endl;
00191   cout << "Input File Name Prefix = " << inputPrefix << endl;
00192   cout << "Output File Name Prefix = " << outputPrefix << endl;
00193 }

 

© 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