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

Go to the documentation of this file.
00001 // $Id: APPSPACK_Cache_Manager.cpp,v 1.15 2006/06/30 01:42:08 tgkolda Exp $ 
00002 // $Source: /space/CVS-Acro/acro/packages/appspack/appspack/src/APPSPACK_Cache_Manager.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 
00037 #include "APPSPACK_Common.hpp"
00038 #include "APPSPACK_Cache_Manager.hpp"
00039 #include "APPSPACK_Utils.hpp"
00040 #include "APPSPACK_Print.hpp"
00041 #include "APPSPACK_Float.hpp"
00042 
00043 APPSPACK::Cache::Manager::Manager(Parameter::List& params, const Vector& scaling) :
00044   isFout(false)
00045 {
00046   Cache::Point::setStaticScaling(scaling);
00047   Cache::Point::setStaticTolerance(params.getParameter("Cache Comparison Tolerance", 
00048                                    params.getDoubleParameter("Step Tolerance") / 2.0));
00049 
00050   precision = params.getParameter("Cache File Precision", 14);
00051 
00052   treeptr = new SplayTree<Point>;
00053 
00054   parseInputFile(params);
00055   openOutputFile(params);
00056 
00057   if (params.isParameterVector("Initial X") && params.isParameterVector("Initial F"))
00058   {
00059     Vector x = params.getVectorParameter("Initial X");
00060     Vector f = params.getVectorParameter("Initial F");
00061     insert (x,f);       
00062   }
00063 
00064 } 
00065 
00066 APPSPACK::Cache::Manager::~Manager()
00067 {
00068   delete treeptr;
00069   closeOutputFile();
00070 }
00071 
00072 bool APPSPACK::Cache::Manager::insert(const Vector& x, const Vector& f)
00073 {
00074   Point cp(x,f);
00075   bool isInserted = treeptr->insert(cp);
00076   if (isInserted)
00077     writeToOutputFile(x,f);
00078   return isInserted;
00079 }
00080 
00081 bool APPSPACK::Cache::Manager::isCached(const Vector& x, Vector& f)
00082 {
00083   Point cp(x,f);
00084 
00085   if ( ! (treeptr->find(cp)) )
00086   {
00087     return false;
00088   }
00089 
00090   f = cp.getF();
00091   return true;
00092 }
00093 
00094 void APPSPACK::Cache::Manager::parseInputFile(APPSPACK::Parameter::List& params)
00095 {
00096   string inname;
00097   inname = params.getParameter("Cache Input File", inname);
00098   if (inname.empty())
00099     return;
00100 
00101   ifstream fin;
00102   fin.open(inname.c_str());
00103   
00104   if (!fin)
00105   {
00106     cout << "The cache input file \"" << inname << "\" does not exist." << endl;
00107     return;
00108   }
00109 
00110   string line;
00111   while (!fin.eof())
00112   {
00113     getline(fin, line);
00114     processInputLine(line);
00115   }
00116 
00117   fin.close();
00118 }
00119 
00120 void APPSPACK::Cache::Manager::processInputLine(string& line)
00121 {
00122 
00123   string::size_type line_pos;
00124   
00125   string element;
00126   string::size_type element_pos;
00127   
00128   // Skip "f="
00129   line_pos = 0;
00130 
00131   if (!getNextString(line, line_pos, element))
00132     return;
00133 
00134   // Do nothing if line does not have the right format
00135   if (element != "f=[") 
00136     return;
00137   
00138   // Read elements of F 
00139   Vector f;
00140   f.resize(0);
00141   double d;
00142   while (1)
00143   {
00144     // Read the next element of F
00145     if (!getNextString(line, line_pos, element))
00146       return;
00147     
00148     // Check for termination
00149     if (element == "]")
00150       break;
00151 
00152     // Process into a value
00153     element_pos = 0;
00154     if (!getNextDouble(element, element_pos, d))
00155       return;
00156     f.push_back(d);
00157   }
00158   Vector v=f;
00159 
00160   // Skip "x=["
00161   if (!getNextString(line, line_pos, element))
00162     return;
00163   
00164   // Do nothing if line does not have the right format
00165   if (element != "x=[") 
00166     return;
00167 
00168   // Read elements of x 
00169   Vector x;
00170   x.resize(0);
00171   while (1)
00172   {
00173     // Read the next element of x
00174     if (!getNextString(line, line_pos, element))
00175       return;
00176     
00177     // Check for termination
00178     if (element == "]")
00179       break;
00180 
00181     // Process into a value
00182     element_pos = 0;
00183     if (!getNextDouble(element, element_pos, d))
00184       return;
00185     x.push_back(d);
00186   }
00187 
00188   // Insert into the queue
00189   insert(x, v);
00190   
00191 }
00192 
00193 void APPSPACK::Cache::Manager::openOutputFile(APPSPACK::Parameter::List& params)
00194 {
00195   string outname;
00196   outname = params.getParameter("Cache Output File", outname);
00197   if (outname.empty())
00198     return;
00199 
00200   // Open file for append
00201   fout.open(outname.c_str(), std::ios::out|std::ios::app);
00202   
00203   if (!fout)
00204   {
00205     cerr << "APPSPACK::Cache::Manager::openOutputFile - Unable to open cache output file" << endl;
00206     return;
00207   }
00208 
00209   isFout = true;
00210 }
00211 
00212 void APPSPACK::Cache::Manager::writeToOutputFile(const Vector& x, const Vector& f)
00213 {
00214   if (!isFout)
00215     return;
00216 
00217   fout << "f=[ ";
00218   f.leftshift(fout,precision);
00219   fout << " ]";
00220 
00221   fout << " x=[ ";
00222   x.leftshift(fout,precision);
00223   fout << " ]" << endl;
00224 }
00225 
00226 void APPSPACK::Cache::Manager::closeOutputFile()
00227 {
00228   if (!isFout)
00229     fout.close();
00230 }

 

© Sandia Corporation | Site Contact | Privacy and Security

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