00001 // $Id: APPSPACK_Evaluator_SystemCall.cpp,v 1.14 2004/11/11 16:30:39 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 00042 APPSPACK::Evaluator::SystemCall::SystemCall(const string& executableName_in, 00043 const string& inputPrefix_in, 00044 const string& outputPrefix_in) : 00045 executableName(executableName_in), 00046 inputPrefix(inputPrefix_in), 00047 outputPrefix(outputPrefix_in), 00048 isInputFile(false), 00049 isOutputFile(false) 00050 { 00051 } 00052 00053 APPSPACK::Evaluator::SystemCall::SystemCall(const Parameter::List& params) : 00054 executableName(params.getParameter("Executable Name", "a.out")), 00055 inputPrefix(params.getParameter("Input Prefix", "input")), 00056 outputPrefix(params.getParameter("Output Prefix", "output")), 00057 isInputFile(false), 00058 isOutputFile(false) 00059 { 00060 } 00061 00062 void APPSPACK::Evaluator::SystemCall::operator()(int tag_in, 00063 const Vector& x_in, 00064 bool& isF_out, 00065 double& f_out, 00066 string& msg_out) 00067 { 00068 createStrings(tag_in); 00069 writeInputFile(x_in); 00070 runProgram(); 00071 readOutputFile(); 00072 deleteFiles(); 00073 00074 isF_out = isF; 00075 f_out = f; 00076 msg_out = msg; 00077 00078 return; 00079 } 00080 00081 00082 void APPSPACK::Evaluator::SystemCall::createStrings(int tag_in) 00083 { 00084 char tmp[128]; 00085 sprintf(tmp, "%d", tag_in); 00086 tag = tmp; 00087 00088 inputFileName = inputPrefix + "." + tag; 00089 outputFileName = outputPrefix + "." + tag; 00090 execString = executableName + " " + inputFileName + " " + outputFileName + " " + tag; 00091 } 00092 00093 void APPSPACK::Evaluator::SystemCall::writeInputFile(const Vector& x) 00094 { 00095 ofstream stream; 00096 stream.open(inputFileName.c_str()); 00097 00098 if (!stream) 00099 { 00100 cerr << "APPSPACK::Evaluator::SystemCall::writeInputFile - Unable to open the following file: " << 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 isF = false; 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 isF = false; 00139 msg = "Empty Output File"; 00140 fin.close(); 00141 return; 00142 } 00143 00144 string line; 00145 getline(fin, line); 00146 00147 string::size_type pos = 0; 00148 if (!APPSPACK::getNextDouble(line, pos, f)) 00149 { 00150 cerr << "\nWarning - no function value for the point corresponding to tag=" << tag << endl; 00151 msg = (line.empty()) ? "No Floating Point Value Found in Output File" : line; 00152 isF = false; 00153 fin.close(); 00154 return; 00155 } 00156 00157 msg = "Success"; 00158 isF = true; 00159 fin.close(); 00160 } 00161 00162 void APPSPACK::Evaluator::SystemCall::deleteFiles() 00163 { 00164 #ifndef SNL_TFLOPS_ENV 00165 if (isInputFile) 00166 unlink(inputFileName.c_str()); 00167 if (isOutputFile) 00168 unlink(outputFileName.c_str()); 00169 #endif 00170 00171 isInputFile = false; 00172 isOutputFile = false; 00173 } 00174 00175 void APPSPACK::Evaluator::SystemCall::print() const 00176 { 00177 cout << "Using SystemCall Evaluator" << endl; 00178 cout << "Executable Name = " << executableName << endl; 00179 cout << "Input File Name Prefix = " << inputPrefix << endl; 00180 cout << "Output File Name Prefix = " << outputPrefix << endl; 00181 }
© 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