• Main Page
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

worker.h

Go to the documentation of this file.
00001 /// @addtogroup unifexp
00002 /// @{
00003 
00004 /////////////////////////////////////////////////////////////////////////////
00005 ///
00006 /// @file worker.h
00007 ///
00008 /// This file contains the definition of the worker class
00009 /// whose objects process single chunks of data.
00010 ///
00011 /// @author Pawel Pilarczyk
00012 ///
00013 /////////////////////////////////////////////////////////////////////////////
00014 
00015 // Copyright (C) 2007 by Pawel Pilarczyk.
00016 //
00017 // This file is part of my research program package.  This is free software;
00018 // you can redistribute it and/or modify it under the terms of the GNU
00019 // General Public License as published by the Free Software Foundation;
00020 // either version 2 of the License, or (at your option) any later version.
00021 //
00022 // This software is distributed in the hope that it will be useful,
00023 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00024 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025 // GNU General Public License for more details.
00026 //
00027 // You should have received a copy of the GNU General Public License along
00028 // with this software; see the file "license.txt".  If not, write to the
00029 // Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00030 // MA 02111-1307, USA.
00031 
00032 // Started on March 8, 2007. Last revision: August 22, 2007.
00033 
00034 #ifndef _worker_h_
00035 #define _worker_h_
00036 
00037 #include "chomp/system/config.h"
00038 #include "chomp/system/textfile.h"
00039 #include "chomp/system/timeused.h"
00040 #include "chomp/multiwork/mw.h"
00041 
00042 #include "graphs.h"
00043 #include "maptypes.h"
00044 #include "parttypes.h"
00045 
00046 
00047 namespace unifexp {
00048 
00049 // --------------------------------------------------
00050 // ---------------- the worker class -----------------
00051 // --------------------------------------------------
00052 
00053 /// The worker class that processes single chunks of data.
00054 class Worker: public chomp::multiwork::mwWorker
00055 {
00056 public:
00057         /// The default constructor.
00058         Worker (int _controlNumber, bool _local = false);
00059 
00060 private:
00061         /// The control number to recognize a compatible coordinator.
00062         int controlNumber;
00063 
00064         /// Is the work being done locally? That is, in the same process
00065         /// as the coordinator? If so, then the global variables are not
00066         /// reset, such as pointbase, because this might affect the
00067         /// coordinator's data.
00068         bool local;
00069 
00070         /// A function for the initialization of a worker.
00071         int Initialize (chomp::multiwork::mwData &data);
00072 
00073         /// A function for processing a piece of data by a worker.
00074         int Process (chomp::multiwork::mwData &data);
00075 
00076 private:
00077         /// An object which contains all the map objects to choose from.
00078         mapTypes<double> maps;
00079 
00080         /// An object which contains all the partition objects
00081         /// to choose from.
00082         partTypes<double> partitions;
00083 
00084 }; /* class Worker */
00085 
00086 // --------------------------------------------------
00087 
00088 inline Worker::Worker (int _controlNumber, bool _local):
00089         controlNumber (_controlNumber), local (_local)
00090 {
00091         return;
00092 } /* Worker::Worker */
00093 
00094 // --------------------------------------------------
00095 
00096 inline int Worker::Initialize (chomp::multiwork::mwData &data)
00097 {
00098         // ignore the received data and say it was Ok
00099         return chomp::multiwork::mwOk;
00100 } /* Worker::Initialize */
00101 
00102 inline int Worker::Process (chomp::multiwork::mwData &data)
00103 {
00104         using namespace chomp::homology;
00105         using namespace chomp::multiwork;
00106 
00107         // remember the time used so far
00108         double timeStamp = static_cast<double> (program_time);
00109 
00110         // decode the number of the data pack to process
00111         int currentLevel = 0, currentItem = 0;
00112         data >> currentLevel;
00113         data >> currentItem;
00114 
00115         // say which data pack is being processed
00116         sout << "- " << currentLevel << ":" << currentItem << ".\n";
00117 
00118         // identify the map object to use
00119         std::string mapName;
00120         data >> mapName;
00121         mapType<double> *theMap = maps. get (mapName);
00122         if (!theMap)
00123         {
00124                 sout << "! Cannot find the map '" << mapName << "'.\n";
00125                 data. Reset ();
00126                 return mwReject;
00127         }
00128 
00129         // identify the partition object to use
00130         std::string partName;
00131         data >> partName;
00132         partType<double> *thePart = partitions. get (partName);
00133         if (!thePart)
00134         {
00135                 sout << "! Cannot find the partition '" <<
00136                         partName << "'.\n";
00137                 data. Reset ();
00138                 return mwReject;
00139         }
00140 
00141         // the size of the partition
00142         int partCount = 0;
00143         data >> partCount;
00144 
00145         // the parameter range
00146         double paramMin = 0, paramMax = 0;
00147         data >> paramMin;
00148         data >> paramMax;
00149         theMap -> setParam (paramMin, paramMax);
00150 
00151         // other parameters of the computations
00152         double deltaBad = 0;
00153         double delta = 0;
00154         double resolution = 0.01;
00155         double lambda = 0;
00156         data >> delta;
00157         data >> resolution;
00158         data >> lambda;
00159 
00160         // what to compute and how?
00161         int computeDelta = 0;
00162         int computeDelta0 = 0;
00163         int computeLambda = 0;
00164         int computeC = 0;
00165         int computeLambda0 = 0;
00166         int rigorous = 0;
00167         int sparseGraph = -1;
00168         data >> computeDelta;
00169         data >> computeDelta0;
00170         data >> computeLambda;
00171         data >> computeC;
00172         data >> computeLambda0;
00173         data >> rigorous;
00174         data >> sparseGraph;
00175 
00176         // the precision for writing real numbers to the screen and log files
00177         int loprec = 6;
00178         int hiprec = 6;
00179         data >> loprec;
00180         data >> hiprec;
00181 
00182         // the ending control number
00183         int ctrl = 0;
00184         data >> ctrl;
00185         if (ctrl != controlNumber)
00186         {
00187                 sout << "! Data incomplete. Rejecting it.\n";
00188                 data. Reset ();
00189                 return mwReject;
00190         }
00191 
00192         // remember the default precision and set a good one for 'sbug'
00193         int precision = std::cout. precision ();
00194         sbug << std::setprecision (hiprec);
00195         sout << std::setprecision (hiprec);
00196 
00197         // run one set of computations
00198         double logC = 0, lambda0 = 0;
00199         int result = 0;
00200         try
00201         {
00202                 // compute delta unless it has been fixed already
00203                 if (computeDelta || computeDelta0)
00204                 {
00205                         bool considerPaths = !!computeDelta0;
00206                         findDeltaBisection (lambda, resolution,
00207                                 *theMap, *thePart, partCount,
00208                                 deltaBad, delta, considerPaths,
00209                                 rigorous, sparseGraph);
00210                 }
00211 
00212                 // compute the constants lambda, C, and lambda0
00213                 if (computeLambda || computeC || computeLambda0)
00214                 {
00215                         findLambdaC (delta, *theMap, *thePart, partCount,
00216                                 computeLambda ? &lambda : 0,
00217                                 computeC ? &logC : 0,
00218                                 computeLambda0 ? &lambda0 : 0,
00219                                 rigorous, sparseGraph);
00220                 }
00221         }
00222         catch (const char *msg)
00223         {
00224                 sout << "REJECT: " << msg << '\n';
00225                 result = -1;
00226         }
00227         catch (const std::exception &e)
00228         {
00229                 sout << "REJECT: " << e. what () << '\n';
00230                 result = -1;
00231         }
00232         catch (...)
00233         {
00234                 sout << "REJECT: An unknown error occurred.\n";
00235                 result = -1;
00236         }
00237 
00238         // restore the defaulut precision of the two major output streams
00239         sbug << std::setprecision (precision);
00240         sout << std::setprecision (precision);
00241 
00242         // if the result is wrong then reject this piece of data
00243         if (result < 0)
00244         {
00245                 data. Reset ();
00246                 return mwReject;
00247         }
00248 
00249         // say what has been computed
00250         sout << "= " << currentLevel << ":" << currentItem << ".\n";
00251 
00252         // send back the data containing the result of the processing
00253         data. Reset ();
00254         data << currentLevel;
00255         data << currentItem;
00256         data << paramMin;
00257         data << paramMax;
00258         data << partCount;
00259         data << deltaBad;
00260         data << delta;
00261         data << lambda;
00262         data << logC;
00263         data << lambda0;
00264         data << (static_cast<double> (program_time) - timeStamp);
00265         data << controlNumber;
00266 
00267         // return the result
00268         return mwOk;
00269 } /* Worker::Process */
00270 
00271 
00272 } // namespace unifexp
00273 
00274 #endif // _worker_h_
00275 
00276 /// @}
00277 

Generated on Sun Feb 3 2013 12:40:32 for The Uniform Expansion Software by  doxygen 1.7.2