Generated on Tue May 22 09:39:49 2018 for Gecode by doxygen 1.6.3

tracer.cpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Maxim Shishmarev <maxim.shishmarev@monash.edu>
00005  *
00006  *  Contributing authors:
00007  *     Kevin Leo <kevin.leo@monash.edu>
00008  *     Christian Schulte <schulte@gecode.org>
00009  *
00010  *  Copyright:
00011  *     Kevin Leo, 2017
00012  *     Christian Schulte, 2017
00013  *     Maxim Shishmarev, 2017
00014  *
00015  *  This file is part of Gecode, the generic constraint
00016  *  development environment:
00017  *     http://www.gecode.org
00018  *
00019  *  Permission is hereby granted, free of charge, to any person obtaining
00020  *  a copy of this software and associated documentation files (the
00021  *  "Software"), to deal in the Software without restriction, including
00022  *  without limitation the rights to use, copy, modify, merge, publish,
00023  *  distribute, sublicense, and/or sell copies of the Software, and to
00024  *  permit persons to whom the Software is furnished to do so, subject to
00025  *  the following conditions:
00026  *
00027  *  The above copyright notice and this permission notice shall be
00028  *  included in all copies or substantial portions of the Software.
00029  *
00030  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00031  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00032  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00033  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00034  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00035  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00036  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00037  *
00038  */
00039 
00040 #include <gecode/search.hh>
00041 
00042 #ifdef GECODE_HAS_CPPROFILER
00043 
00044 #include <gecode/search/cpprofiler/message.hpp>
00045 #include <gecode/search/cpprofiler/connector.hpp>
00046 
00047 namespace Gecode {
00048 
00049   CPProfilerSearchTracer::GetInfo::GetInfo(void) {}
00050 
00051   CPProfilerSearchTracer::GetInfo::~GetInfo(void) {}
00052 
00053 
00054   CPProfilerSearchTracer::CPProfilerSearchTracer(int eid, std::string name0,
00055                                                  unsigned int port,
00056                                                  const GetInfo* pgetinfo) :
00057     connector(new CPProfiler::Connector(port)), execution_id(eid), name(name0), restart(0), 
00058     pgi(pgetinfo) {
00059   }
00060 
00061   void
00062   CPProfilerSearchTracer::init(void) {
00063     // Try to find out whether engine is a restart engine
00064     bool restarts = ((engines() == 2U) &&
00065                      (engine(0U).type() == EngineType::RBS));
00066     connector->connect();
00067     connector->start(name, execution_id, restarts);
00068   }
00069 
00070   void
00071   CPProfilerSearchTracer::round(unsigned int) {
00072     restart++;
00073     connector->restart(restart);
00074   }
00075 
00076   void
00077   CPProfilerSearchTracer::skip(const EdgeInfo& ei) {
00078     CPProfiler::NodeUID dummy_node{-1, restart, -1};
00079     CPProfiler::NodeUID parent{-1,restart,-1};
00080 
00081     int alt = 0;
00082     std::string label;
00083     if (ei) {
00084       parent.nid = static_cast<int>(ei.nid());
00085       parent.tid = static_cast<int>(ei.wid());
00086       dummy_node.tid = static_cast<int>(ei.wid());
00087       alt = static_cast<int>(ei.alternative());
00088       label = ei.string();
00089     }
00090 
00091     auto node = connector->createNode(dummy_node, parent,
00092                                      alt, 0, CPProfiler::NodeStatus::SKIPPED)
00093       .set_label(label)
00094       .set_info("");
00095     connector->sendNode(node);
00096   }
00097 
00098   void
00099   CPProfilerSearchTracer::node(const EdgeInfo& ei, const NodeInfo& ni) {
00100     CPProfiler::NodeUID this_node{static_cast<int>(ni.nid()),
00101         restart,
00102         static_cast<int>(ni.wid())};
00103     CPProfiler::NodeUID parent {-1,restart,-1};
00104 
00105     int alt = 0;
00106     int alts = 0;
00107 
00108     CPProfiler::NodeStatus ns = CPProfiler::NodeStatus::FAILED;
00109     switch(ni.type()) {
00110     case NodeType::SOLVED:
00111       ns = CPProfiler::NodeStatus::SOLVED;
00112       break;
00113     case NodeType::BRANCH:
00114       ns = CPProfiler::NodeStatus::BRANCH;
00115       alts = static_cast<int>(ni.choice().alternatives());
00116       break;
00117     case NodeType::FAILED:
00118       ns = CPProfiler::NodeStatus::FAILED;
00119       break;
00120     default:
00121       GECODE_NEVER;
00122     }
00123 
00124     std::string label;
00125     if(ei) {
00126       parent = {static_cast<int>(ei.nid()),
00127                 restart,
00128                 static_cast<int>(ei.wid())};
00129 
00130       alt = static_cast<int>(ei.alternative());
00131       label = ei.string();
00132     } else {
00133       alt = restart;
00134     }
00135 
00136     std::string info;
00137     if(pgi) {
00138       info = pgi->getInfo(ni.space());
00139     }
00140 
00141     auto node = connector->createNode(this_node, parent, alt, alts, ns)
00142       .set_label(label)
00143       .set_info(info);
00144     connector->sendNode(node);
00145   }
00146 
00147   void
00148   CPProfilerSearchTracer::done(void) {
00149     connector->disconnect();
00150   }
00151 
00152   CPProfilerSearchTracer::~CPProfilerSearchTracer(void) {
00153     delete connector;
00154     delete pgi;
00155   }
00156 
00157 }
00158 
00159 #endif
00160 
00161 // STATISTICS: search-trace