<commit_before>/*
 * Copyright 2006-2008 The FLWOR Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <iostream>
#include <fstream>
#include <sstream>
#include <atlconv.h>
#include <atlbase.h>

#include <zorba/zorba.h>
#include <zorba/config.h>
#include <simplestore/simplestore.h>
#include <simplestore/msdom/import.h>

#include <msxml.h>

using namespace zorba;
using namespace zorba::simplestore;

/**
 * Example to show how to use the MS DOM store in Zorba.
 * The MS DOM store stands for "the store which keeps its data internaly also
 *    in Microsoft DOM format".
 * An xml file is loaded into a variable, processed with a simple XPath filter
 *    and then the result is exported as MS DOM document.
 * The result text will be serialized by MS DOM and by the result item.
 * The outputs should look the same.
 * Note: In order to test this example you need to compile simplestore with
 *    the flag ZORBA_STORE_MSDOM set to ON in cmake.
 */
bool
msdom_store_example_1(Zorba* aZorba)
{
  //Get the SimpleStore object
  zorba::simplestore::SimpleStore* msdomstore =
        zorba::simplestore::SimpleStoreManager::getStore();

  std::auto_ptr<std::istream> lDocStream(
    new std::stringstream("<books><book>Book 1</book><book>Book 2</book><book processable=\"1\">Book 3</book><book processable=\"1\">Book 4<chapter1/><chapter2/></book></books>"));

  XQuery_t lQuery = aZorba->compileQuery("declare variable $var external; $var//book[@processable = 1]");

  DynamicContext* lCtx = lQuery->getDynamicContext();

  //load the above xml into variable $var, associating with URI "books.xml"
  //when loading, the MS DOM tree is constructed as well
  lCtx->setVariableAsDocument("var", "books.xml", lDocStream);

  try {
    ResultIterator_t    result;
    //get an iterator over the result items
    //the actual execution will be performed when asking for the next item
    result = lQuery->iterator();

    Item  item;
    result->open();//open the iterator
    while(result->next(item))
    {
      //export the MS DOM from Item
      IXMLDOMNode*  msdom_node;
      msdom_node = MSDOM2SimpleStore::exportItemAsMSDOM(item);
      std::cout << "Zorba item output:" << std::endl << std::endl;
      item.serialize(std::cout);
      std::cout << std::endl << std::endl;
      if(msdom_node)
      {
        BSTR  xml_bstr;
        msdom_node->get_xml(&xml_bstr);
        char  xml_ascii[1000];
        WideCharToMultiByte(CP_ACP, 0, xml_bstr, -1, xml_ascii, 1000, NULL, NULL);
        std::cout << "MSDOM output:" << std::endl << std::endl;
        std::cout << xml_ascii << std::endl << std::endl;
      }
      else
      {
        std::cout << "No MS DOM node associated" << std::endl;
      }
    }
    result->close();//close the iterator to free resources
  } catch (DynamicException& e) {
    std::cerr << e << std::endl;
    return false;
  }
  return true;
}

/**
 * Example to show how to import MS DOM tree in Zorba.
 * Load an xml into MS DOM and then import the MS DOM tree into Zorba.
 * Display the original MS DOM and the imported document to see if they match.

 * Note: In order to test this example you need to compile simplestore with
 *    the flag ZORBA_STORE_MSDOM set to ON in cmake.
 */
bool
msdom_store_example_2(Zorba* aZorba)
{
  //Get the SimpleStore object
  zorba::simplestore::SimpleStore* msdomstore =
        zorba::simplestore::SimpleStoreManager::getStore();

  const char xmlDoc[] = "<?xml version=\"1.0\" encoding = \"UTF-8\"?><books><book>Book 1</book><book>Book 2</book><book processable=\"1\">Book 3</book><book processable=\"1\">Book 4</book></books>";

  //load xml into MS DOM
  IXMLDOMDocument *domdoc;
  CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER,
       IID_IXMLDOMDocument, (void**)&domdoc);

  CComBSTR  xml_bstr(xmlDoc);
  VARIANT_BOOL  is_success;
  HRESULT   hr;
  hr = domdoc->loadXML(xml_bstr, &is_success);

  //display what has been loaded into MS DOM
  BSTR  xml_bstr2;
  domdoc->get_xml(&xml_bstr2);
  {
    char  xml_ascii[1000];
    WideCharToMultiByte(CP_ACP, 0, xml_bstr2, -1, xml_ascii, 1000, NULL, NULL);
    std::cout << "MS DOM version of xml:" << std::endl;
    std::cout << xml_ascii << std::endl;
  }

  //import the MS DOM into Zorba store
  MSDOM2SimpleStore::importMSDOM(domdoc,
                          String("importedDoc.xml"), String());

  XQuery_t lQuery = aZorba->compileQuery("fn:doc(\"importedDoc.xml\")");

  try {
    //execute the query
    std::cout << "Zorba version of xml:" << std::endl;
    std::cout << lQuery << std::endl;
  } catch (DynamicException& e) {
    std::cerr << e << std::endl;
    return false;
  }
  return true;
}

int
msdom(int argc, char *argv[])
{
  simplestore::SimpleStore* lStore = simplestore::SimpleStoreManager::getStore();

  Zorba* lZorba = Zorba::getInstance(lStore);

  bool res = false;

  std::cout << "executing example 1" << std::endl;
  res = msdom_store_example_1(lZorba);
  if (!res) return 1;
  std::cout << std::endl;

  std::cout << "executing example 2" << std::endl;
  res = msdom_store_example_2(lZorba);
  if (!res) return 1;
  std::cout << std::endl;

  lZorba->shutdown();
  simplestore::SimpleStoreManager::shutdownStore(lStore);

  return 0;
}
<commit_msg>Normalized \r\n in msdom files.<commit_after>/*
 * Copyright 2006-2008 The FLWOR Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <iostream>
#include <fstream>
#include <sstream>
#include <atlconv.h>
#include <atlbase.h>

#include <zorba/zorba.h>
#include <zorba/config.h>
#include <simplestore/simplestore.h>
#include <simplestore/msdom/import.h>

#include <msxml.h>

using namespace zorba;
using namespace zorba::simplestore;

/**
 * Example to show how to use the MS DOM store in Zorba.
 * The MS DOM store stands for "the store which keeps its data internaly also
 *    in Microsoft DOM format".
 * An xml file is loaded into a variable, processed with a simple XPath filter
 *    and then the result is exported as MS DOM document.
 * The result text will be serialized by MS DOM and by the result item.
 * The outputs should look the same.
 * Note: In order to test this example you need to compile simplestore with
 *    the flag ZORBA_STORE_MSDOM set to ON in cmake.
 */
bool
msdom_store_example_1(Zorba* aZorba)
{
  //Get the SimpleStore object
  zorba::simplestore::SimpleStore* msdomstore =
        zorba::simplestore::SimpleStoreManager::getStore();

  std::auto_ptr<std::istream> lDocStream(
    new std::stringstream("<books><book>Book 1</book><book>Book 2</book><book processable=\"1\">Book 3</book><book processable=\"1\">Book 4<chapter1/><chapter2/></book></books>"));

  XQuery_t lQuery = aZorba->compileQuery("declare variable $var external; $var//book[@processable = 1]");

  DynamicContext* lCtx = lQuery->getDynamicContext();

  //load the above xml into variable $var, associating with URI "books.xml"
  //when loading, the MS DOM tree is constructed as well
  lCtx->setVariableAsDocument("var", "books.xml", lDocStream);

  try {
    ResultIterator_t    result;
    //get an iterator over the result items
    //the actual execution will be performed when asking for the next item
    result = lQuery->iterator();

    Item  item;
    result->open();//open the iterator
    while(result->next(item))
    {
      //export the MS DOM from Item
      IXMLDOMNode*  msdom_node;
      msdom_node = MSDOM2SimpleStore::exportItemAsMSDOM(item);
      std::cout << "Zorba item output:" << std::endl << std::endl;
      item.serialize(std::cout);
      std::cout << std::endl << std::endl;
      if(msdom_node)
      {
        BSTR  xml_bstr;
        msdom_node->get_xml(&xml_bstr);
        char  xml_ascii[1000];
        WideCharToMultiByte(CP_ACP, 0, xml_bstr, -1, xml_ascii, 1000, NULL, NULL);
        std::cout << "MSDOM output:" << std::endl << std::endl;
        std::cout << xml_ascii << std::endl << std::endl;
      }
      else
      {
        std::cout << "No MS DOM node associated" << std::endl;
      }
    }
    result->close();//close the iterator to free resources
  } catch (DynamicException& e) {
    std::cerr << e << std::endl;
    return false;
  }
  return true;
}

/**
 * Example to show how to import MS DOM tree in Zorba.
 * Load an xml into MS DOM and then import the MS DOM tree into Zorba.
 * Display the original MS DOM and the imported document to see if they match.

 * Note: In order to test this example you need to compile simplestore with
 *    the flag ZORBA_STORE_MSDOM set to ON in cmake.
 */
bool
msdom_store_example_2(Zorba* aZorba)
{
  //Get the SimpleStore object
  zorba::simplestore::SimpleStore* msdomstore =
        zorba::simplestore::SimpleStoreManager::getStore();

  const char xmlDoc[] = "<?xml version=\"1.0\" encoding = \"UTF-8\"?><books><book>Book 1</book><book>Book 2</book><book processable=\"1\">Book 3</book><book processable=\"1\">Book 4</book></books>";

  //load xml into MS DOM
  IXMLDOMDocument *domdoc;
  CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER,
       IID_IXMLDOMDocument, (void**)&domdoc);

  CComBSTR  xml_bstr(xmlDoc);
  VARIANT_BOOL  is_success;
  HRESULT   hr;
  hr = domdoc->loadXML(xml_bstr, &is_success);

  //display what has been loaded into MS DOM
  BSTR  xml_bstr2;
  domdoc->get_xml(&xml_bstr2);
  {
    char  xml_ascii[1000];
    WideCharToMultiByte(CP_ACP, 0, xml_bstr2, -1, xml_ascii, 1000, NULL, NULL);
    std::cout << "MS DOM version of xml:" << std::endl;
    std::cout << xml_ascii << std::endl;
  }

  //import the MS DOM into Zorba store
  MSDOM2SimpleStore::importMSDOM(domdoc,
                          String("importedDoc.xml"), String());

  XQuery_t lQuery = aZorba->compileQuery("fn:doc(\"importedDoc.xml\")");

  try {
    //execute the query
    std::cout << "Zorba version of xml:" << std::endl;
    std::cout << lQuery << std::endl;
  } catch (DynamicException& e) {
    std::cerr << e << std::endl;
    return false;
  }
  return true;
}

int
msdom(int argc, char *argv[])
{
  simplestore::SimpleStore* lStore = simplestore::SimpleStoreManager::getStore();

  Zorba* lZorba = Zorba::getInstance(lStore);

  bool res = false;

  std::cout << "executing example 1" << std::endl;
  res = msdom_store_example_1(lZorba);
  if (!res) return 1;
  std::cout << std::endl;

  std::cout << "executing example 2" << std::endl;
  res = msdom_store_example_2(lZorba);
  if (!res) return 1;
  std::cout << std::endl;

  lZorba->shutdown();
  simplestore::SimpleStoreManager::shutdownStore(lStore);

  return 0;
}
<|endoftext|>


<commit_before>#include "stdafx.h"
#include "BoostGraph.h"
#include <sstream>
#include <boost/algorithm/string.hpp>

using std::vector;
using std::string;
using std::stringstream;

namespace
{

const char C_INPUT_DOT_1[] = R"***(
digraph G {
1 [root=1];
2;
3;
4;
5;
1->2 [weight=2];
2->3 [weight=3];
2->4 [weight=-3];
3->1 [weight=-4];
4->5 [weight=2];
}
)***";

const char C_INPUT_TEXT_1[] = R"***(
5 5 1
1 2 2
2 3 3
2 4 -3
3 1 -4
4 5 2
)***";

const char C_OUTPUT_1[] = R"***(
0 0
2 1 2
5 2 2 3
-1 2 2 4
1 3 2 4 5
)***";

const char C_INPUT_DOT_2[] = R"***(
digraph G {
1 [root=1];
2;
3;
4;
1->2 [weight=3];
1->3 [weight=7];
2->3 [weight=2];
2->4 [weight=1];
3->2 [weight=2];
3->4 [weight=2];
4->3 [weight=2];
}
)***";

const char C_OUTPUT_2[] = R"***(
0 0
3 1 2
5 2 2 3
4 2 2 4
)***";

const char C_INPUT_DOT_3[] = R"***(
digraph G {
1 [root=1];
2;
3;
4;
1->2 [weight=3];
3->4 [weight=4];
}
)***";

const char C_OUTPUT_3[] = R"***(
0 0
3 1 2
No
No
)***";

const char C_INPUT_TEXT_4[] = R"***(
4 4 1
1 2 2
2 3 4
3 1 -9
2 4 5
)***";

const char C_INPUT_DOT_4[] = R"***(
digraph G {
1 [root=1];
2;
3;
4;
1->2 [weight=2];
2->3 [weight=4];
3->1 [weight=-9];
2->4 [weight=5];
}
)***";

const char C_OUTPUT_4[] = R"***(
No
4 2 3 1 2
)***";

// Используем split для игнорирования пробелов и переносов строк при проверке.
vector<string> SplitWords(string const& text)
{
	std::string trimmed = boost::trim_copy(text);

	vector<string> words;
	boost::split(words, trimmed, boost::is_space(), boost::token_compress_on);
	return words;
}

}

BOOST_AUTO_TEST_SUITE(Graph_with_Bellmand_Ford_algorithm)
	BOOST_AUTO_TEST_CASE(can_load_graphviz_format)
	{
		CBoostGraph graph;
		stringstream input(C_INPUT_DOT_1);
		stringstream output;
		BOOST_CHECK(graph.ReadGraphviz(input));
		graph.RunBellmanFord(output);
		vector<string> words = SplitWords(output.str());
		vector<string> checkWords = SplitWords(C_OUTPUT_1);
		BOOST_CHECK(words == checkWords);
	}

	BOOST_AUTO_TEST_CASE(can_load_text_format)
	{
		CBoostGraph graph;
		stringstream input(C_INPUT_TEXT_1);
		stringstream output;
		BOOST_CHECK(graph.ReadText(input));
		graph.RunBellmanFord(output);
		vector<string> words = SplitWords(output.str());
		vector<string> checkWords = SplitWords(C_OUTPUT_1);
		BOOST_CHECK(words == checkWords);
	}

	BOOST_AUTO_TEST_CASE(has_no_negative_loop_false_positives)
	{
		CBoostGraph graph;
		stringstream input(C_INPUT_DOT_2);
		stringstream output;
		BOOST_CHECK(graph.ReadGraphviz(input));
		graph.RunBellmanFord(output);
		vector<string> words = SplitWords(output.str());
		vector<string> checkWords = SplitWords(C_OUTPUT_2);
		BOOST_CHECK(words == checkWords);
	}

	BOOST_AUTO_TEST_CASE(prints_No_if_no_path)
	{
		CBoostGraph graph;
		stringstream input(C_INPUT_DOT_3);
		stringstream output;
		BOOST_CHECK(graph.ReadGraphviz(input));
		graph.RunBellmanFord(output);
		vector<string> words = SplitWords(output.str());
		vector<string> checkWords = SplitWords(C_OUTPUT_3);
		BOOST_CHECK(words == checkWords);
	}

	BOOST_AUTO_TEST_CASE(finds_negative_loop_and_prints_it_for_dot_input)
	{
		CBoostGraph graph;
		stringstream input(C_INPUT_DOT_4);
		stringstream output;
		BOOST_CHECK(graph.ReadGraphviz(input));
		graph.RunBellmanFord(output);
		vector<string> words = SplitWords(output.str());
		vector<string> checkWords = SplitWords(C_OUTPUT_4);
		BOOST_CHECK(words == checkWords);
	}

	BOOST_AUTO_TEST_CASE(finds_negative_loop_and_prints_it_for_text_input)
	{
		CBoostGraph graph;
		stringstream input(C_INPUT_TEXT_4);
		stringstream output;
		BOOST_CHECK(graph.ReadText(input));
		graph.RunBellmanFord(output);
		vector<string> words = SplitWords(output.str());
		vector<string> checkWords = SplitWords(C_OUTPUT_4);
		BOOST_CHECK(words == checkWords);
	}
BOOST_AUTO_TEST_SUITE_END()<commit_msg>Упростил тесты.<commit_after>#include "stdafx.h"
#include "BoostGraph.h"
#include <sstream>
#include <boost/algorithm/string.hpp>

using std::vector;
using std::string;
using std::stringstream;

namespace
{

const char C_INPUT_DOT_1[] = R"***(
digraph G {
1 [root=1];
2;
3;
4;
5;
1->2 [weight=2];
2->3 [weight=3];
2->4 [weight=-3];
3->1 [weight=-4];
4->5 [weight=2];
}
)***";

const char C_INPUT_TEXT_1[] = R"***(
5 5 1
1 2 2
2 3 3
2 4 -3
3 1 -4
4 5 2
)***";

const char C_OUTPUT_1[] = R"***(
0 0
2 1 2
5 2 2 3
-1 2 2 4
1 3 2 4 5
)***";

const char C_INPUT_DOT_2[] = R"***(
digraph G {
1 [root=1];
2;
3;
4;
1->2 [weight=3];
1->3 [weight=7];
2->3 [weight=2];
2->4 [weight=1];
3->2 [weight=2];
3->4 [weight=2];
4->3 [weight=2];
}
)***";

const char C_OUTPUT_2[] = R"***(
0 0
3 1 2
5 2 2 3
4 2 2 4
)***";

const char C_INPUT_DOT_3[] = R"***(
digraph G {
1 [root=1];
2;
3;
4;
1->2 [weight=3];
3->4 [weight=4];
}
)***";

const char C_OUTPUT_3[] = R"***(
0 0
3 1 2
No
No
)***";

const char C_INPUT_TEXT_4[] = R"***(
4 4 1
1 2 2
2 3 4
3 1 -9
2 4 5
)***";

const char C_INPUT_DOT_4[] = R"***(
digraph G {
1 [root=1];
2;
3;
4;
1->2 [weight=2];
2->3 [weight=4];
3->1 [weight=-9];
2->4 [weight=5];
}
)***";

const char C_OUTPUT_4[] = R"***(
No
4 2 3 1 2
)***";

// Используем split для игнорирования пробелов и переносов строк при проверке.
vector<string> SplitWords(string const& text)
{
	std::string trimmed = boost::trim_copy(text);

	vector<string> words;
	boost::split(words, trimmed, boost::is_space(), boost::token_compress_on);
	return words;
}

void RunGraphvizInputTestcase(std::string const& inputText, std::string const& expectedOutput)
{
	CBoostGraph graph;
	stringstream input(inputText);
	stringstream output;
	BOOST_CHECK(graph.ReadGraphviz(input));
	graph.RunBellmanFord(output);
	vector<string> words = SplitWords(output.str());
	vector<string> checkWords = SplitWords(expectedOutput);
	BOOST_CHECK(words == checkWords);
}

void RunTextInputTestcase(std::string const& inputText, std::string const& expectedOutput)
{
	CBoostGraph graph;
	stringstream input(inputText);
	stringstream output;
	BOOST_CHECK(graph.ReadText(input));
	graph.RunBellmanFord(output);
	vector<string> words = SplitWords(output.str());
	vector<string> checkWords = SplitWords(expectedOutput);
	BOOST_CHECK(words == checkWords);
}

}

BOOST_AUTO_TEST_SUITE(Graph_with_Bellmand_Ford_algorithm)
	BOOST_AUTO_TEST_CASE(can_load_graphviz_format)
	{
		RunGraphvizInputTestcase(C_INPUT_DOT_1, C_OUTPUT_1);
	}

	BOOST_AUTO_TEST_CASE(can_load_text_format)
	{
		RunTextInputTestcase(C_INPUT_TEXT_1, C_OUTPUT_1);
	}

	BOOST_AUTO_TEST_CASE(has_no_negative_loop_false_positives)
	{
		RunGraphvizInputTestcase(C_INPUT_DOT_2, C_OUTPUT_2);
	}

	BOOST_AUTO_TEST_CASE(prints_No_if_no_path)
	{
		RunGraphvizInputTestcase(C_INPUT_DOT_3, C_OUTPUT_3);
	}

	BOOST_AUTO_TEST_CASE(finds_negative_loop_and_prints_it_for_dot_input)
	{
		RunGraphvizInputTestcase(C_INPUT_DOT_4, C_OUTPUT_4);
	}

	BOOST_AUTO_TEST_CASE(finds_negative_loop_and_prints_it_for_text_input)
	{
		RunTextInputTestcase(C_INPUT_TEXT_4, C_OUTPUT_4);
	}
BOOST_AUTO_TEST_SUITE_END()<|endoftext|>


<commit_before>/*********************************************************************
* Software License Agreement (BSD License)
*
*  Copyright (c) 2011, Rice University
*  All rights reserved.
*
*  Redistribution and use in source and binary forms, with or without
*  modification, are permitted provided that the following conditions
*  are met:
*
*   * Redistributions of source code must retain the above copyright
*     notice, this list of conditions and the following disclaimer.
*   * Redistributions in binary form must reproduce the above
*     copyright notice, this list of conditions and the following
*     disclaimer in the documentation and/or other materials provided
*     with the distribution.
*   * Neither the name of the Rice University nor the names of its
*     contributors may be used to endorse or promote products derived
*     from this software without specific prior written permission.
*
*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
*  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
*  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
*  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
*  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
*  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
*  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
*  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
*  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
*  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
*  POSSIBILITY OF SUCH DAMAGE.
*********************************************************************/

/* Author: Matt Maly */

#include "ompl/base/GoalSampleableRegion.h"
#include "ompl/control/planners/syclop/SyclopRRT.h"
#include "ompl/datastructures/NearestNeighborsSqrtApprox.h"
#include "ompl/base/spaces/SE2StateSpace.h"

void ompl::control::SyclopRRT::setup(void)
{
    Syclop::setup();
    sampler_ = si_->allocStateSampler();
    controlSampler_ = siC_->allocControlSampler();
}

void ompl::control::SyclopRRT::clear(void)
{
    Syclop::clear();
    sampler_.reset();
    controlSampler_.reset();
    freeMemory();
    motions.clear();
}

void ompl::control::SyclopRRT::getPlannerData(base::PlannerData& data) const
{
    Planner::getPlannerData(data);
    for (std::size_t i = 0 ; i < motions.size() ; ++i)
        data.recordEdge(motions[i]->parent ? motions[i]->parent->state : NULL, motions[i]->state);
}

ompl::control::Syclop::Motion* ompl::control::SyclopRRT::initializeTree(const base::State* s)
{
    Motion* motion = new Motion(siC_);
    si_->copyState(motion->state, s);
    siC_->nullControl(motion->control);
    motions.push_back(motion);
    return motion;
}

void ompl::control::SyclopRRT::selectAndExtend(Region& region, std::set<Motion*>& newMotions)
{
    Motion* nmotion = region.motions[rng.uniformInt(0,region.motions.size()-1)];

    base::State* newState = si_->allocState();
    Control* rctrl = siC_->allocControl();

    controlSampler_->sampleNext(rctrl, nmotion->control, nmotion->state);
    unsigned int duration = controlSampler_->sampleStepCount(siC_->getMinControlDuration(), siC_->getMaxControlDuration());
    duration = siC_->propagateWhileValid(nmotion->state, rctrl, duration, newState);

    base::CompoundState* cs = newState->as<base::CompoundState>();
    base::SE2StateSpace::StateType* location = cs->as<base::SE2StateSpace::StateType>(0);

    if (duration >= siC_->getMinControlDuration())
    {
        Motion* motion = new Motion(siC_);
        si_->copyState(motion->state, newState);
        siC_->copyControl(motion->control, rctrl);
        motion->steps = duration;
        motion->parent = nmotion;
        motions.push_back(motion);
        newMotions.insert(motion);
    }

    si_->freeState(newState);
    siC_->freeControl(rctrl);
}

void ompl::control::SyclopRRT::freeMemory(void)
{
    for (std::size_t i = 0 ; i < motions.size() ; ++i)
    {
        if (motions[i]->state)
            si_->freeState(motions[i]->state);
        if (motions[i]->control)
            siC_->freeControl(motions[i]->control);
        delete motions[i];
    }
}
<commit_msg>removed leftover debug code from syclopRRT<commit_after>/*********************************************************************
* Software License Agreement (BSD License)
*
*  Copyright (c) 2011, Rice University
*  All rights reserved.
*
*  Redistribution and use in source and binary forms, with or without
*  modification, are permitted provided that the following conditions
*  are met:
*
*   * Redistributions of source code must retain the above copyright
*     notice, this list of conditions and the following disclaimer.
*   * Redistributions in binary form must reproduce the above
*     copyright notice, this list of conditions and the following
*     disclaimer in the documentation and/or other materials provided
*     with the distribution.
*   * Neither the name of the Rice University nor the names of its
*     contributors may be used to endorse or promote products derived
*     from this software without specific prior written permission.
*
*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
*  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
*  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
*  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
*  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
*  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
*  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
*  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
*  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
*  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
*  POSSIBILITY OF SUCH DAMAGE.
*********************************************************************/

/* Author: Matt Maly */

#include "ompl/base/GoalSampleableRegion.h"
#include "ompl/control/planners/syclop/SyclopRRT.h"
#include "ompl/datastructures/NearestNeighborsSqrtApprox.h"
#include "ompl/base/spaces/SE2StateSpace.h"

void ompl::control::SyclopRRT::setup(void)
{
    Syclop::setup();
    sampler_ = si_->allocStateSampler();
    controlSampler_ = siC_->allocControlSampler();
}

void ompl::control::SyclopRRT::clear(void)
{
    Syclop::clear();
    sampler_.reset();
    controlSampler_.reset();
    freeMemory();
    motions.clear();
}

void ompl::control::SyclopRRT::getPlannerData(base::PlannerData& data) const
{
    Planner::getPlannerData(data);
    for (std::size_t i = 0 ; i < motions.size() ; ++i)
        data.recordEdge(motions[i]->parent ? motions[i]->parent->state : NULL, motions[i]->state);
}

ompl::control::Syclop::Motion* ompl::control::SyclopRRT::initializeTree(const base::State* s)
{
    Motion* motion = new Motion(siC_);
    si_->copyState(motion->state, s);
    siC_->nullControl(motion->control);
    motions.push_back(motion);
    return motion;
}

void ompl::control::SyclopRRT::selectAndExtend(Region& region, std::set<Motion*>& newMotions)
{
    Motion* nmotion = region.motions[rng.uniformInt(0,region.motions.size()-1)];

    base::State* newState = si_->allocState();
    Control* rctrl = siC_->allocControl();

    controlSampler_->sampleNext(rctrl, nmotion->control, nmotion->state);
    unsigned int duration = controlSampler_->sampleStepCount(siC_->getMinControlDuration(), siC_->getMaxControlDuration());
    duration = siC_->propagateWhileValid(nmotion->state, rctrl, duration, newState);

    if (duration >= siC_->getMinControlDuration())
    {
        Motion* motion = new Motion(siC_);
        si_->copyState(motion->state, newState);
        siC_->copyControl(motion->control, rctrl);
        motion->steps = duration;
        motion->parent = nmotion;
        motions.push_back(motion);
        newMotions.insert(motion);
    }

    si_->freeState(newState);
    siC_->freeControl(rctrl);
}

void ompl::control::SyclopRRT::freeMemory(void)
{
    for (std::size_t i = 0 ; i < motions.size() ; ++i)
    {
        if (motions[i]->state)
            si_->freeState(motions[i]->state);
        if (motions[i]->control)
            siC_->freeControl(motions[i]->control);
        delete motions[i];
    }
}
<|endoftext|>


<commit_before>// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/file_path.h"
#include "content/test/layout_browsertest.h"

class IndexedDBLayoutTest : public InProcessBrowserLayoutTest {
 public:
  IndexedDBLayoutTest() : InProcessBrowserLayoutTest(
      FilePath(), FilePath().AppendASCII("storage").AppendASCII("indexeddb")) {
  }

  virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
    InProcessBrowserLayoutTest::SetUpInProcessBrowserTestFixture();
    AddResourceForLayoutTest(
        FilePath().AppendASCII("fast").AppendASCII("js"),
        FilePath().AppendASCII("resources"));
  }

  void RunLayoutTests(const char* file_names[]) {
    for (size_t i = 0; file_names[i]; i++)
      RunLayoutTest(file_names[i]);
  }
};

namespace {

static const char* kBasicTests[] = {
  "basics.html",
  "basics-shared-workers.html",
  "basics-workers.html",
  "database-basics.html",
  "factory-basics.html",
  "index-basics.html",
  "objectstore-basics.html",
  NULL
};

static const char* kComplexTests[] = {
  "prefetch-bugfix-108071.html",
  NULL
};

static const char* kIndexTests[] = {
  "deleteIndex.html",
  "index-count.html",
  "index-cursor.html",  // Locally takes ~6s compared to <1 for the others.
  "index-get-key-argument-required.html",
  "index-multientry.html",
  "index-population.html",
  "index-unique.html",
  NULL
};

static const char* kKeyTests[] = {
  "key-generator.html",
  "keypath-basics.html",
  "keypath-edges.html",
  "keypath-fetch-key.html",
  "keyrange.html",
  "keyrange-required-arguments.html",
  "key-sort-order-across-types.html",
  "key-sort-order-date.html",
  "key-type-array.html",
  "key-type-infinity.html",
  "invalid-keys.html",
  NULL
};

static const char* kTransactionTests[] = {
//  "transaction-abort.html", // Flaky, http://crbug.com/83226
  "transaction-abort-with-js-recursion-cross-frame.html",
  "transaction-abort-with-js-recursion.html",
  "transaction-abort-workers.html",
  "transaction-after-close.html",
  "transaction-and-objectstore-calls.html",
  "transaction-basics.html",
  "transaction-crash-on-abort.html",
  "transaction-event-propagation.html",
  "transaction-read-only.html",
  "transaction-rollback.html",
  "transaction-storeNames-required.html",
  NULL
};

static const char* kRegressionTests[] = {
  "dont-commit-on-blocked.html",
  NULL
};

}

IN_PROC_BROWSER_TEST_F(IndexedDBLayoutTest, BasicTests) {
  RunLayoutTests(kBasicTests);
}

IN_PROC_BROWSER_TEST_F(IndexedDBLayoutTest, ComplexTests) {
  RunLayoutTests(kComplexTests);
}

// Frequently times out, sometimes due to webkit assertion failure.
// http://crbug.com/120924
IN_PROC_BROWSER_TEST_F(IndexedDBLayoutTest, DISABLED_IndexBasicsWorkersTest) {
  RunLayoutTest("deleteIndex.html");
  RunLayoutTest("index-basics-workers.html");
}

IN_PROC_BROWSER_TEST_F(IndexedDBLayoutTest, IndexTests) {
  RunLayoutTests(kIndexTests);
}

IN_PROC_BROWSER_TEST_F(IndexedDBLayoutTest, KeyTests) {
  RunLayoutTests(kKeyTests);
}

IN_PROC_BROWSER_TEST_F(IndexedDBLayoutTest, TransactionTests) {
  RunLayoutTests(kTransactionTests);
}

IN_PROC_BROWSER_TEST_F(IndexedDBLayoutTest, RegressionTests) {
  RunLayoutTests(kRegressionTests);
}
<commit_msg>Re-enable flaky test to see if it was fixed by http://trac.webkit.org/changeset/113818<commit_after>// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/file_path.h"
#include "content/test/layout_browsertest.h"

class IndexedDBLayoutTest : public InProcessBrowserLayoutTest {
 public:
  IndexedDBLayoutTest() : InProcessBrowserLayoutTest(
      FilePath(), FilePath().AppendASCII("storage").AppendASCII("indexeddb")) {
  }

  virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
    InProcessBrowserLayoutTest::SetUpInProcessBrowserTestFixture();
    AddResourceForLayoutTest(
        FilePath().AppendASCII("fast").AppendASCII("js"),
        FilePath().AppendASCII("resources"));
  }

  void RunLayoutTests(const char* file_names[]) {
    for (size_t i = 0; file_names[i]; i++)
      RunLayoutTest(file_names[i]);
  }
};

namespace {

static const char* kBasicTests[] = {
  "basics.html",
  "basics-shared-workers.html",
  "basics-workers.html",
  "database-basics.html",
  "factory-basics.html",
  "index-basics.html",
  "objectstore-basics.html",
  NULL
};

static const char* kComplexTests[] = {
  "prefetch-bugfix-108071.html",
  NULL
};

static const char* kIndexTests[] = {
  "deleteIndex.html",
  "index-count.html",
  "index-cursor.html",  // Locally takes ~6s compared to <1 for the others.
  "index-get-key-argument-required.html",
  "index-multientry.html",
  "index-population.html",
  "index-unique.html",
  NULL
};

static const char* kKeyTests[] = {
  "key-generator.html",
  "keypath-basics.html",
  "keypath-edges.html",
  "keypath-fetch-key.html",
  "keyrange.html",
  "keyrange-required-arguments.html",
  "key-sort-order-across-types.html",
  "key-sort-order-date.html",
  "key-type-array.html",
  "key-type-infinity.html",
  "invalid-keys.html",
  NULL
};

static const char* kTransactionTests[] = {
//  "transaction-abort.html", // Flaky, http://crbug.com/83226
  "transaction-abort-with-js-recursion-cross-frame.html",
  "transaction-abort-with-js-recursion.html",
  "transaction-abort-workers.html",
  "transaction-after-close.html",
  "transaction-and-objectstore-calls.html",
  "transaction-basics.html",
  "transaction-crash-on-abort.html",
  "transaction-event-propagation.html",
  "transaction-read-only.html",
  "transaction-rollback.html",
  "transaction-storeNames-required.html",
  NULL
};

static const char* kRegressionTests[] = {
  "dont-commit-on-blocked.html",
  NULL
};

}

IN_PROC_BROWSER_TEST_F(IndexedDBLayoutTest, BasicTests) {
  RunLayoutTests(kBasicTests);
}

IN_PROC_BROWSER_TEST_F(IndexedDBLayoutTest, ComplexTests) {
  RunLayoutTests(kComplexTests);
}

// Frequently times out, sometimes due to webkit assertion failure.
// http://crbug.com/120924
IN_PROC_BROWSER_TEST_F(IndexedDBLayoutTest, FAILS_IndexBasicsWorkersTest) {
  RunLayoutTest("deleteIndex.html");
  RunLayoutTest("index-basics-workers.html");
}

IN_PROC_BROWSER_TEST_F(IndexedDBLayoutTest, IndexTests) {
  RunLayoutTests(kIndexTests);
}

IN_PROC_BROWSER_TEST_F(IndexedDBLayoutTest, KeyTests) {
  RunLayoutTests(kKeyTests);
}

IN_PROC_BROWSER_TEST_F(IndexedDBLayoutTest, TransactionTests) {
  RunLayoutTests(kTransactionTests);
}

IN_PROC_BROWSER_TEST_F(IndexedDBLayoutTest, RegressionTests) {
  RunLayoutTests(kRegressionTests);
}
<|endoftext|>


<commit_before>#include "layer/residual_block.hpp"

using namespace Convolutional;
using namespace Convolutional::Layer;

auto ResidualBlock::ProcessMultiMatrix(const MultiMatrix & multiMatrix) -> MultiMatrix {
	auto mm = multiMatrix;
	for (size_t i = 0; i < layers.size(); ++i) {
		mm = layers[i]->ProcessMultiMatrix(mm);
	}
	mm += multiMatrix;
	mm = layers.back()->ProcessMultiMatrix(mm);
	return mm;
}

auto ResidualBlock::GetDimensionalityAfterProcessing(MultiMatrix::Dimensionality dimensionality) const noexcept -> MultiMatrix::Dimensionality {
	// TODO jnf: is this correct?
	return layers.GetDimensionalityAfterProcessing(dimensionality);
}<commit_msg>CNN: Add terminating newline<commit_after>#include "layer/residual_block.hpp"

using namespace Convolutional;
using namespace Convolutional::Layer;

auto ResidualBlock::ProcessMultiMatrix(const MultiMatrix & multiMatrix) -> MultiMatrix {
	auto mm = multiMatrix;
	for (size_t i = 0; i < layers.size(); ++i) {
		mm = layers[i]->ProcessMultiMatrix(mm);
	}
	mm += multiMatrix;
	mm = layers.back()->ProcessMultiMatrix(mm);
	return mm;
}

auto ResidualBlock::GetDimensionalityAfterProcessing(MultiMatrix::Dimensionality dimensionality) const noexcept -> MultiMatrix::Dimensionality {
	// TODO jnf: is this correct?
	return layers.GetDimensionalityAfterProcessing(dimensionality);
}
<|endoftext|>


<commit_before>/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/import/chips/p9/procedures/hwp/memory/lib/dimm/ddr4/state_machine.H $ */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2015,2019                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */

///
/// @file state_machine.H
/// @brief state_machine delcaration
///
// *HWP HWP Owner: Andre Marin <aamarin@us.ibm.com>
// *HWP HWP Backup: Brian Silver <bsilver@us.ibm.com>
// *HWP Team: Memory
// *HWP Level: 2
// *HWP Consumed by: HB:FSP

#ifndef _MSS_STATE_MACHINE_H_
#define _MSS_STATE_MACHINE_H_

#include <fapi2.H>
#include <generic/memory/lib/utils/c_str.H>
#include <lib/utils/checker.H>

namespace mss
{

enum class transition
{
    RISING_EDGE, ///< Looking for 1st Low to High transition
    FALLING_EDGE, ///< Looking for 1st High to Low transition
};

enum class fsm_state
{
    UNINITIALIZED, ///< Initial state
    HIGH, ///< High state (logical 1)
    LOW, ///< Low state (logical 0)
    DONE, ///< Final state (First 1->0 or 0->1 transition)
};

namespace ddr4
{
namespace db
{

///
/// @class state_machine
/// @brief State machine for LRDIMM training
///
class state_machine
{
    public:
        fsm_state iv_state;
        uint64_t iv_delay;

        ///
        /// @brief Default ctor
        /// @param[in] i_target DIMM target
        ///
        state_machine(const fapi2::Target< fapi2::TARGET_TYPE_DIMM >& i_target):
            iv_state(fsm_state::UNINITIALIZED),
            iv_delay(0),
            iv_target(i_target)
        {}

        ///
        /// @brief Default dctor
        ///
        ~state_machine() = default;

        ///
        /// @brief Sets current state
        /// @tparam T first transition we are looking for
        /// @param[in] i_data DRAM DQ data
        /// @param[in] i_nibble current nibble
        /// @param[in] i_phase_timing current phase step
        ///
        template< transition T >
        fapi2::ReturnCode next_transition( const fapi2::variable_buffer& i_data,
                                           const uint64_t i_nibble,
                                           const uint64_t i_phase_timing)
        {
            switch(iv_state)
            {
                case fsm_state::UNINITIALIZED:
                    uninitialized(i_data, i_nibble, i_phase_timing);
                    break;

                case fsm_state::HIGH:
                    high<T>(i_data, i_nibble, i_phase_timing);
                    break;

                case fsm_state::LOW:
                    low<T>(i_data, i_nibble, i_phase_timing);
                    break;

                case fsm_state::DONE:
                    FAPI_INF("%s No state change. Already in DONE state.", mss::c_str(iv_target) );
                    break;

                default:
                    // By default we are unitialized, we switch state based on
                    // AADR & AAER register data that represents DRAM DQ data.
                    // If we got here something bad happened, iv_state was corrupted somehow.
                    // Lets tell someone with FFDC
                    FAPI_TRY( check::invalid_dq_data(iv_target, false, i_data, i_phase_timing, i_nibble) );
                    break;
            }// switch

        fapi_try_exit:
            return fapi2::current_err;
        }

    private:
        static constexpr size_t NIBBLE_OFFSET = 4;
        const fapi2::Target< fapi2::TARGET_TYPE_DIMM > iv_target;
        char iv_str_buffer[fapi2::MAX_ECMD_STRING_LEN];

        ///
        /// @brief Helper function to set uninitialized state transition
        /// @param[in] i_data DRAM DQ data
        /// @param[in] i_nibble current nibble
        /// @param[in] i_phase_timing current phase step
        ///
        void uninitialized( const fapi2::variable_buffer& i_data,
                            const uint64_t i_nibble,
                            const uint64_t i_phase_timing );

        ///
        /// @brief Helper function to set high state transition
        /// @tparam T first transition we are looking for
        /// @param[in] i_data DRAM DQ data
        /// @param[in] i_nibble current nibble
        /// @param[in] i_phase_timing current phase step
        ///
        template< transition T >
        void high( const fapi2::variable_buffer& i_data,
                   const uint64_t i_nibble,
                   const uint64_t i_phase_timing );

        ///
        /// @brief Helper function to set low state transition
        /// @tparam T first transition we are looking for
        /// @param[in] i_data DRAM DQ data
        /// @param[in] i_nibble current nibble
        /// @param[in] i_phase_timing current phase step
        ///
        template< transition T >
        void low( const fapi2::variable_buffer& i_data,
                  const uint64_t i_nibble,
                  const uint64_t i_phase_timing );

        ///
        /// @brief Helper function for trace boilerplate
        /// @param[in] i_data DRAM DQ data
        /// @param[in] i_nibble current nibble
        /// @param[in] i_phase_timing current phase step
        ///
        void print_debug( const fapi2::variable_buffer& i_data,
                          const uint64_t i_nibble,
                          const uint64_t i_phase_timing );
};

}// db
}// ddr4
}// mss

#endif
<commit_msg>L3 support for ddr_phy_reset, termination_control<commit_after>/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/import/chips/p9/procedures/hwp/memory/lib/dimm/ddr4/state_machine.H $ */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2015,2019                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */

///
/// @file state_machine.H
/// @brief state_machine delcaration
///
// *HWP HWP Owner: Andre Marin <aamarin@us.ibm.com>
// *HWP HWP Backup: Jacob Harvey <jlharvey@us.ibm.com>
// *HWP Team: Memory
// *HWP Level: 2
// *HWP Consumed by: HB:FSP

#ifndef _MSS_STATE_MACHINE_H_
#define _MSS_STATE_MACHINE_H_

#include <fapi2.H>
#include <generic/memory/lib/utils/c_str.H>
#include <lib/utils/checker.H>

namespace mss
{

enum class transition
{
    RISING_EDGE, ///< Looking for 1st Low to High transition
    FALLING_EDGE, ///< Looking for 1st High to Low transition
};

enum class fsm_state
{
    UNINITIALIZED, ///< Initial state
    HIGH, ///< High state (logical 1)
    LOW, ///< Low state (logical 0)
    DONE, ///< Final state (First 1->0 or 0->1 transition)
};

namespace ddr4
{
namespace db
{

///
/// @class state_machine
/// @brief State machine for LRDIMM training
///
class state_machine
{
    public:
        fsm_state iv_state;
        uint64_t iv_delay;

        ///
        /// @brief Default ctor
        /// @param[in] i_target DIMM target
        ///
        state_machine(const fapi2::Target< fapi2::TARGET_TYPE_DIMM >& i_target):
            iv_state(fsm_state::UNINITIALIZED),
            iv_delay(0),
            iv_target(i_target)
        {}

        ///
        /// @brief Default dctor
        ///
        ~state_machine() = default;

        ///
        /// @brief Sets current state
        /// @tparam T first transition we are looking for
        /// @param[in] i_data DRAM DQ data
        /// @param[in] i_nibble current nibble
        /// @param[in] i_phase_timing current phase step
        ///
        template< transition T >
        fapi2::ReturnCode next_transition( const fapi2::variable_buffer& i_data,
                                           const uint64_t i_nibble,
                                           const uint64_t i_phase_timing)
        {
            switch(iv_state)
            {
                case fsm_state::UNINITIALIZED:
                    uninitialized(i_data, i_nibble, i_phase_timing);
                    break;

                case fsm_state::HIGH:
                    high<T>(i_data, i_nibble, i_phase_timing);
                    break;

                case fsm_state::LOW:
                    low<T>(i_data, i_nibble, i_phase_timing);
                    break;

                case fsm_state::DONE:
                    FAPI_INF("%s No state change. Already in DONE state.", mss::c_str(iv_target) );
                    break;

                default:
                    // By default we are unitialized, we switch state based on
                    // AADR & AAER register data that represents DRAM DQ data.
                    // If we got here something bad happened, iv_state was corrupted somehow.
                    // Lets tell someone with FFDC
                    FAPI_TRY( check::invalid_dq_data(iv_target, false, i_data, i_phase_timing, i_nibble) );
                    break;
            }// switch

        fapi_try_exit:
            return fapi2::current_err;
        }

    private:
        static constexpr size_t NIBBLE_OFFSET = 4;
        const fapi2::Target< fapi2::TARGET_TYPE_DIMM > iv_target;
        char iv_str_buffer[fapi2::MAX_ECMD_STRING_LEN];

        ///
        /// @brief Helper function to set uninitialized state transition
        /// @param[in] i_data DRAM DQ data
        /// @param[in] i_nibble current nibble
        /// @param[in] i_phase_timing current phase step
        ///
        void uninitialized( const fapi2::variable_buffer& i_data,
                            const uint64_t i_nibble,
                            const uint64_t i_phase_timing );

        ///
        /// @brief Helper function to set high state transition
        /// @tparam T first transition we are looking for
        /// @param[in] i_data DRAM DQ data
        /// @param[in] i_nibble current nibble
        /// @param[in] i_phase_timing current phase step
        ///
        template< transition T >
        void high( const fapi2::variable_buffer& i_data,
                   const uint64_t i_nibble,
                   const uint64_t i_phase_timing );

        ///
        /// @brief Helper function to set low state transition
        /// @tparam T first transition we are looking for
        /// @param[in] i_data DRAM DQ data
        /// @param[in] i_nibble current nibble
        /// @param[in] i_phase_timing current phase step
        ///
        template< transition T >
        void low( const fapi2::variable_buffer& i_data,
                  const uint64_t i_nibble,
                  const uint64_t i_phase_timing );

        ///
        /// @brief Helper function for trace boilerplate
        /// @param[in] i_data DRAM DQ data
        /// @param[in] i_nibble current nibble
        /// @param[in] i_phase_timing current phase step
        ///
        void print_debug( const fapi2::variable_buffer& i_data,
                          const uint64_t i_nibble,
                          const uint64_t i_phase_timing );
};

}// db
}// ddr4
}// mss

#endif
<|endoftext|>


<commit_before>/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: testdefaultbootstrapping.cxx,v $
 *
 *  $Revision: 1.5 $
 *
 *  last change: $Author: rt $ $Date: 2006-12-01 17:19:38 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_cppuhelper.hxx"

#include <sal/main.h>

#include <cstdio>

#include <rtl/process.h>
#include <cppuhelper/bootstrap.hxx>

#include <com/sun/star/lang/XMultiServiceFactory.hpp>

using namespace ::cppu;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::uno;
using namespace ::rtl;

SAL_IMPLEMENT_MAIN()
{
    sal_Bool result = sal_True;

    try {
        Reference<XComponentContext> xComponentContext = defaultBootstrap_InitialComponentContext();

        Reference<XMultiServiceFactory> smgr(xComponentContext->getServiceManager(), UNO_QUERY);

        for(sal_uInt32 i = 0; i < rtl_getAppCommandArgCount(); ++ i) {
            OUString arg;

            rtl_getAppCommandArg(i, &arg.pData);
            if (arg.getLength())
            {
                Reference<XInterface> xInterface = smgr->createInstance(arg);
                OString tmp = OUStringToOString(arg, RTL_TEXTENCODING_ASCII_US);
#if OSL_DEBUG_LEVEL > 1
                fprintf(stderr, "got the %s service %p\n", tmp.getStr(), xInterface.get());
#endif

                result = result && (xInterface.get() != 0);
            }
        }
    }
    catch(Exception & exception) {
        OString message = OUStringToOString(exception.Message, RTL_TEXTENCODING_ASCII_US);

        fprintf(stderr, "an exception occurred: %s\n", message.getStr());
    }

#if OSL_DEBUG_LEVEL > 1
    OSL_TRACE("---------------------------------- %i", result);
#endif

    return result;
}
<commit_msg>INTEGRATION: CWS changefileheader (1.5.72); FILE MERGED 2008/03/28 15:25:27 rt 1.5.72.1: #i87441# Change license header to LPGL v3.<commit_after>/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: testdefaultbootstrapping.cxx,v $
 * $Revision: 1.6 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_cppuhelper.hxx"

#include <sal/main.h>

#include <cstdio>

#include <rtl/process.h>
#include <cppuhelper/bootstrap.hxx>

#include <com/sun/star/lang/XMultiServiceFactory.hpp>

using namespace ::cppu;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::uno;
using namespace ::rtl;

SAL_IMPLEMENT_MAIN()
{
    sal_Bool result = sal_True;

    try {
        Reference<XComponentContext> xComponentContext = defaultBootstrap_InitialComponentContext();

        Reference<XMultiServiceFactory> smgr(xComponentContext->getServiceManager(), UNO_QUERY);

        for(sal_uInt32 i = 0; i < rtl_getAppCommandArgCount(); ++ i) {
            OUString arg;

            rtl_getAppCommandArg(i, &arg.pData);
            if (arg.getLength())
            {
                Reference<XInterface> xInterface = smgr->createInstance(arg);
                OString tmp = OUStringToOString(arg, RTL_TEXTENCODING_ASCII_US);
#if OSL_DEBUG_LEVEL > 1
                fprintf(stderr, "got the %s service %p\n", tmp.getStr(), xInterface.get());
#endif

                result = result && (xInterface.get() != 0);
            }
        }
    }
    catch(Exception & exception) {
        OString message = OUStringToOString(exception.Message, RTL_TEXTENCODING_ASCII_US);

        fprintf(stderr, "an exception occurred: %s\n", message.getStr());
    }

#if OSL_DEBUG_LEVEL > 1
    OSL_TRACE("---------------------------------- %i", result);
#endif

    return result;
}
<|endoftext|>


<commit_before>//
//  AY-3-8910.hpp
//  Clock Signal
//
//  Created by Thomas Harte on 14/10/2016.
//  Copyright © 2016 Thomas Harte. All rights reserved.
//

#ifndef AY_3_8910_hpp
#define AY_3_8910_hpp

#include "../../Outputs/Speaker.hpp"

namespace GI {

/*!
	Provides emulation of an AY-3-8910 / YM2149, which is a three-channel sound chip with a
	noise generator and a volume envelope generator, which also provides two bidirectional
	interface ports.
*/
class AY38910: public ::Outputs::Filter<AY38910> {
	public:
		/// Creates a new AY38910.
		AY38910();

		/// Sets the clock rate at which this AY38910 will be run.
		void set_clock_rate(double clock_rate);

		enum ControlLines {
			BC1		= (1 << 0),
			BC2		= (1 << 1),
			BCDIR	= (1 << 2)
		};

		/// Sets the value the AY would read from its data lines if it were not outputting.
		void set_data_input(uint8_t r);

		/// Gets the value that would appear on the data lines if only the AY is outputting.
		uint8_t get_data_output();

		/// Sets the
		void set_control_lines(ControlLines control_lines);

		/*!
			Gets the value that would appear on the requested interface port if it were in output mode.
			@parameter port_b @c true to get the value for Port B, @c false to get the value for Port A.
		*/
		uint8_t get_port_output(bool port_b);

		// to satisfy ::Outputs::Speaker (included via ::Outputs::Filter; not for public consumption
		void get_samples(unsigned int number_of_samples, int16_t *target);

	private:
		int selected_register_;
		uint8_t registers_[16], output_registers_[16];

		int master_divider_;

		int tone_periods_[3];
		int tone_counters_[3];
		int tone_outputs_[3];

		int noise_period_;
		int noise_counter_;
		int noise_shift_register_;
		int noise_output_;

		int envelope_period_;
		int envelope_divider_;
		int envelope_position_;
		int envelope_shapes_[16][32];
		int envelope_overflow_masks_[16];

		int volumes_[16];

		enum ControlState {
			Inactive,
			LatchAddress,
			Read,
			Write
		} control_state_;

		void select_register(uint8_t r);
		void set_register_value(uint8_t value);
		uint8_t get_register_value();

		uint8_t data_input_, data_output_;

		int16_t output_volume_;
		inline void evaluate_output_volume();
};

};

#endif /* AY_3_8910_hpp */
<commit_msg>Completed dangling comment.<commit_after>//
//  AY-3-8910.hpp
//  Clock Signal
//
//  Created by Thomas Harte on 14/10/2016.
//  Copyright © 2016 Thomas Harte. All rights reserved.
//

#ifndef AY_3_8910_hpp
#define AY_3_8910_hpp

#include "../../Outputs/Speaker.hpp"

namespace GI {

/*!
	Provides emulation of an AY-3-8910 / YM2149, which is a three-channel sound chip with a
	noise generator and a volume envelope generator, which also provides two bidirectional
	interface ports.
*/
class AY38910: public ::Outputs::Filter<AY38910> {
	public:
		/// Creates a new AY38910.
		AY38910();

		/// Sets the clock rate at which this AY38910 will be run.
		void set_clock_rate(double clock_rate);

		enum ControlLines {
			BC1		= (1 << 0),
			BC2		= (1 << 1),
			BCDIR	= (1 << 2)
		};

		/// Sets the value the AY would read from its data lines if it were not outputting.
		void set_data_input(uint8_t r);

		/// Gets the value that would appear on the data lines if only the AY is outputting.
		uint8_t get_data_output();

		/// Sets the current control line state, as a bit field.
		void set_control_lines(ControlLines control_lines);

		/*!
			Gets the value that would appear on the requested interface port if it were in output mode.
			@parameter port_b @c true to get the value for Port B, @c false to get the value for Port A.
		*/
		uint8_t get_port_output(bool port_b);

		// to satisfy ::Outputs::Speaker (included via ::Outputs::Filter; not for public consumption
		void get_samples(unsigned int number_of_samples, int16_t *target);

	private:
		int selected_register_;
		uint8_t registers_[16], output_registers_[16];

		int master_divider_;

		int tone_periods_[3];
		int tone_counters_[3];
		int tone_outputs_[3];

		int noise_period_;
		int noise_counter_;
		int noise_shift_register_;
		int noise_output_;

		int envelope_period_;
		int envelope_divider_;
		int envelope_position_;
		int envelope_shapes_[16][32];
		int envelope_overflow_masks_[16];

		int volumes_[16];

		enum ControlState {
			Inactive,
			LatchAddress,
			Read,
			Write
		} control_state_;

		void select_register(uint8_t r);
		void set_register_value(uint8_t value);
		uint8_t get_register_value();

		uint8_t data_input_, data_output_;

		int16_t output_volume_;
		inline void evaluate_output_volume();
};

};

#endif /* AY_3_8910_hpp */
<|endoftext|>


<commit_before>/*
 * This file is part of the CN24 semantic segmentation software,
 * copyright (C) 2015 Clemens-Alexander Brust (ikosa dot de at gmail dot com).
 *
 * For licensing information, see the LICENSE file included with this project.
 */
#include <sstream>
#include <cmath>
#include <chrono>

#include "Log.h"
#include "Net.h"

#include "StatLayer.h"

#include "Trainer.h"

namespace Conv {

	Trainer::Trainer(Conv::NetGraph& graph, TrainerSettings settings) :
		graph_(graph), settings_(settings) {
		LOGDEBUG << "Instance created";

		// We need a training layer to select training samples and some kind of
		// loss function to minimize
		if (graph_.GetTrainingNodes().size() == 0 || graph_.GetLossNodes().size() == 0) {
			FATAL("Net doesn't have training layer or loss function layer!");
		}

		// Ask the Net for parameters
		graph_.GetParameters(parameters_);

		LOGDEBUG << "Optimizing " << parameters_.size() << " sets of parameters.";

		unsigned int w = 0;

		for (unsigned int p = 0; p < parameters_.size(); p++) {
			w += parameters_[p]->data.elements();

			// Allocate Tensors for momentum
			Tensor* last_delta = new Tensor();
			Tensor* accumulated_gradient = new Tensor();
			last_delta->Resize(parameters_[p]->data);
			last_delta->Clear();
			accumulated_gradient->Resize(parameters_[p]->data);
			accumulated_gradient->Clear();

			last_deltas_.push_back(last_delta);
			accumulated_gradients_.push_back(accumulated_gradient);
		}

		// Outputs the number of weights
		LOGDEBUG << "Weights: " << w;

		first_training_layer_ = dynamic_cast<TrainingLayer*>(graph_.GetTrainingNodes()[0]->layer);
		sample_count_ = first_training_layer_->GetLabelWidth() * first_training_layer_->GetLabelHeight()
    * first_training_layer_->GetBatchSize();
}

void Trainer::Train (unsigned int epochs) {
  // net_.SetTestOnlyStatDisabled (false);
  graph_.SetIsTesting(false);

  for (unsigned int e = 0; e < epochs; e++)
    Epoch();

  // net_.SetTestOnlyStatDisabled (false);
}

void Trainer::Test() {
	datum* loss_sums = new datum[graph_.GetLossNodes().size()];
	for (unsigned int n = 0; n < graph_.GetLossNodes().size(); n++)
		loss_sums[n] = 0;

  unsigned int iterations = (first_training_layer_->GetSamplesInTestingSet()
                             / first_training_layer_->GetBatchSize()) + 1;
  iterations = (unsigned int) ( ( (datum) iterations) *
                                settings_.testing_ratio);

	for (NetGraphNode* training_node : graph_.GetTrainingNodes())
		(dynamic_cast<TrainingLayer*>(training_node->layer))->SetTestingMode(true);

  graph_.SetIsTesting(true);

  LOGDEBUG << "Testing, iterations: " << iterations <<
           ", batch size: " << first_training_layer_->GetBatchSize();

  auto t_begin = std::chrono::system_clock::now();

	for (unsigned int n = 0; n < graph_.GetLossNodes().size(); n++) {
		LossFunctionLayer* lossfunction_layer = dynamic_cast<LossFunctionLayer*>(graph_.GetLossNodes()[n]->layer);
		for (unsigned int i = 0; i < iterations; i++) {
			graph_.FeedForward();
			loss_sums[n] += lossfunction_layer->CalculateLossFunction();
		}
	}

  auto t_end = std::chrono::system_clock::now();
  std::chrono::duration<double> t_diff = t_end - t_begin;
  LOGINFO << "Testing, sps: " <<
          (datum) (sample_count_ * iterations)
          / (datum) t_diff.count();

  LOGINFO << "Testing, tps: " <<
          1000000.0f * (datum) t_diff.count() /
          (datum) (sample_count_ * iterations) << " us";

	for (unsigned int n = 0; n < graph_.GetLossNodes().size(); n++) {
		LossFunctionLayer* lossfunction_layer = dynamic_cast<LossFunctionLayer*>(graph_.GetLossNodes()[n]->layer);
		LOGDEBUG << "Testing, " << graph_.GetLossNodes()[n]->layer->GetLayerDescription() <<  " lps: " << loss_sums[n] / (datum)(iterations * sample_count_);
	}

	for (unsigned int n = 0; n < graph_.GetStatNodes().size(); n++) {
		StatLayer* stat_layer = dynamic_cast<StatLayer*>(graph_.GetStatNodes()[n]->layer);
    std::stringstream epochname;
    epochname << "Testing  - Epoch " << epoch_ << " -";
    stat_layer->Print (epochname.str(), false);
    stat_layer->Reset();
	}

	for (NetGraphNode* training_node : graph_.GetTrainingNodes())
		(dynamic_cast<TrainingLayer*>(training_node->layer))->SetTestingMode(false);

  graph_.SetIsTesting(false);

	delete[] loss_sums;
}

void Trainer::Epoch() {
	datum* loss_sums = new datum[graph_.GetLossNodes().size()];
	for (unsigned int n = 0; n < graph_.GetLossNodes().size(); n++)
		loss_sums[n] = 0;

  unsigned int iterations =
    settings_.iterations == 0 ?
    first_training_layer_->GetSamplesInTrainingSet() :
    settings_.iterations;
  iterations = (unsigned int) ( ( (datum) iterations) *
                                settings_.epoch_training_ratio);

  unsigned int fiftieth = 0;
  unsigned int tenth = 0;

	for (NetGraphNode* training_node : graph_.GetTrainingNodes())
		(dynamic_cast<TrainingLayer*>(training_node->layer))->SetTestingMode(false);

  LOGDEBUG << "Epoch: " << epoch_ << ", it: " << iterations <<
           ", bsize: " << first_training_layer_->GetBatchSize() * settings_.sbatchsize << ", lr0: " <<
           CalculateLR (epoch_ * iterations) << std::endl;

  auto t_begin = std::chrono::system_clock::now();

  for (unsigned int i = 0; i < iterations; i++) {
    if ( (50 * i / iterations) > fiftieth) {
      fiftieth = 50 * i / iterations;
      std::cout << "." << std::flush;
    }

    if ( (10 * i / iterations) > tenth) {
      tenth = 10 * i / iterations;
      std::cout << tenth << "0%" << std::flush;
    }

    // Reset gradients
    for (unsigned int np = 0; np < accumulated_gradients_.size(); np++)
      accumulated_gradients_[np]->Clear();

    for (unsigned int b = 0; b < settings_.sbatchsize; b++) {
      graph_.FeedForward();

      // Save errors
			for (unsigned int n = 0; n < graph_.GetLossNodes().size(); n++) {
				LossFunctionLayer* lossfunction_layer = dynamic_cast<LossFunctionLayer*>(graph_.GetLossNodes()[n]->layer);
				loss_sums[n] += lossfunction_layer->CalculateLossFunction();
			}

      // Correct errors
      graph_.BackPropagate();

      unsigned int np = 0;

      // Accumulate gradients
      for (unsigned int l = 0; l < graph_.GetNodes().size(); l++) {
				Layer* const layer = graph_.GetNodes()[l]->layer;
        for (unsigned int p = 0; p < layer->parameters().size(); p++) {
          Tensor& gradients = layer->parameters() [p]->delta;
#ifdef BUILD_OPENCL
          gradients.MoveToCPU();
#endif

          for (unsigned int e = 0; e < gradients.elements(); e++) {
            (* (accumulated_gradients_[np])) [e] += gradients[e];
          }

          np++;
        }
      }
    }

    // Calculate annealed learning rate
    const datum lr =
      CalculateLR (epoch_ * iterations + i);

    // Apply gradients with new learning rate
    ApplyGradients (lr);
  }

  auto t_end = std::chrono::system_clock::now();
  std::chrono::duration<double> t_diff = t_end - t_begin;
  LOGINFO << "Training, sps: " <<
          (datum) (sample_count_ * settings_.sbatchsize
                   * first_training_layer_->GetLossSamplingProbability() * iterations)
          / (datum) t_diff.count();

  LOGINFO << "Training, tps: " <<
          1000000.0f * (datum) t_diff.count() /
          (datum) (sample_count_ * settings_.sbatchsize
                   * first_training_layer_->GetLossSamplingProbability() * iterations) << " us";

  // Display training epoch_error
	for (unsigned int n = 0; n < graph_.GetLossNodes().size(); n++) {
		LossFunctionLayer* lossfunction_layer = dynamic_cast<LossFunctionLayer*>(graph_.GetLossNodes()[n]->layer);
		LOGDEBUG << "Training, " << graph_.GetLossNodes()[n]->layer->GetLayerDescription() <<  " lps: " << loss_sums[n] / (datum)(iterations * sample_count_ * settings_.sbatchsize * first_training_layer_->GetLossSamplingProbability());
	}

	for (unsigned int n = 0; n < graph_.GetStatNodes().size(); n++) {
		StatLayer* stat_layer = dynamic_cast<StatLayer*>(graph_.GetStatNodes()[n]->layer);
    std::stringstream epochname;
    epochname << "Training  - Epoch " << epoch_ << " -";
    stat_layer->Print (epochname.str(), true);
    stat_layer->Reset();
	}

  delete[] loss_sums;
  epoch_++;
}

void Trainer::ApplyGradients (datum lr) {
  unsigned int dp = 0;

	for (unsigned int l = 0; l < graph_.GetNodes().size(); l++) {
		Layer* const layer = graph_.GetNodes()[l]->layer;
    const datum llr = lr * layer->local_lr_;

    for (unsigned int p = 0; p < layer->parameters().size(); p++) {
      CombinedTensor* const param = layer->parameters_[p];
#ifdef BUILD_OPENCL
      param->data.MoveToCPU();
#endif

      for (unsigned int w = 0; w < param->data.elements(); w++) {
        const datum weight = param->data (w);
        const datum l1_gradient = (weight > 0) - (weight < 0);
        const datum l2_gradient = weight;
        const datum w_gradient = (*accumulated_gradients_[dp]) [w];

        /*
         * http://www.iro.umontreal.ca/~pift6266/H10/notes/gradient.html
         *
         * This site says that one should average the gradient over
         * the minibatch
         */
        const datum last_delta = (*last_deltas_[dp]) (w);
        const datum delta =

          // Average of gradient over minibatch
          llr * (w_gradient / (datum) (sample_count_ * settings_.sbatchsize)) +
          // Regularization
          llr * (settings_.l2_weight * l2_gradient + settings_.l1_weight * l1_gradient);

        const datum step = delta + settings_.momentum * last_delta;
        param->data[w] -= step;

        // Backup delta
        (*last_deltas_[dp]) [w] = step;
      }

      dp++;
    }
  }
}

std::ostream& operator<< (std::ostream & output,
                          const TrainerSettings settings) {
  output << "LR: " << settings.learning_rate << ", ";
  output << "GM: " << settings.gamma << ", ";
  output << "EX: " << settings.exponent << ", ";
  output << "SB: " << settings.sbatchsize << ", ";
  output << "PB: " << settings.pbatchsize << ", ";
  output << "L1: " << settings.l1_weight << ", ";
  output << "L2: " << settings.l2_weight << ", ";
  output << "MM: " << settings.momentum;
  return output;
}

}
<commit_msg>Trainer: Fixed bug in testing leading to wrong lps<commit_after>/*
 * This file is part of the CN24 semantic segmentation software,
 * copyright (C) 2015 Clemens-Alexander Brust (ikosa dot de at gmail dot com).
 *
 * For licensing information, see the LICENSE file included with this project.
 */
#include <sstream>
#include <cmath>
#include <chrono>

#include "Log.h"
#include "Net.h"

#include "StatLayer.h"

#include "Trainer.h"

namespace Conv {

	Trainer::Trainer(Conv::NetGraph& graph, TrainerSettings settings) :
		graph_(graph), settings_(settings) {
		LOGDEBUG << "Instance created";

		// We need a training layer to select training samples and some kind of
		// loss function to minimize
		if (graph_.GetTrainingNodes().size() == 0 || graph_.GetLossNodes().size() == 0) {
			FATAL("Net doesn't have training layer or loss function layer!");
		}

		// Ask the Net for parameters
		graph_.GetParameters(parameters_);

		LOGDEBUG << "Optimizing " << parameters_.size() << " sets of parameters.";

		unsigned int w = 0;

		for (unsigned int p = 0; p < parameters_.size(); p++) {
			w += parameters_[p]->data.elements();

			// Allocate Tensors for momentum
			Tensor* last_delta = new Tensor();
			Tensor* accumulated_gradient = new Tensor();
			last_delta->Resize(parameters_[p]->data);
			last_delta->Clear();
			accumulated_gradient->Resize(parameters_[p]->data);
			accumulated_gradient->Clear();

			last_deltas_.push_back(last_delta);
			accumulated_gradients_.push_back(accumulated_gradient);
		}

		// Outputs the number of weights
		LOGDEBUG << "Weights: " << w;

		first_training_layer_ = dynamic_cast<TrainingLayer*>(graph_.GetTrainingNodes()[0]->layer);
		sample_count_ = first_training_layer_->GetLabelWidth() * first_training_layer_->GetLabelHeight()
    * first_training_layer_->GetBatchSize();
}

void Trainer::Train (unsigned int epochs) {
  // net_.SetTestOnlyStatDisabled (false);
  graph_.SetIsTesting(false);

  for (unsigned int e = 0; e < epochs; e++)
    Epoch();

  // net_.SetTestOnlyStatDisabled (false);
}

void Trainer::Test() {
	datum* loss_sums = new datum[graph_.GetLossNodes().size()];
	for (unsigned int n = 0; n < graph_.GetLossNodes().size(); n++)
		loss_sums[n] = 0;

  unsigned int iterations = (first_training_layer_->GetSamplesInTestingSet()
                             / first_training_layer_->GetBatchSize()) + 1;
  iterations = (unsigned int) ( ( (datum) iterations) *
                                settings_.testing_ratio);

	for (NetGraphNode* training_node : graph_.GetTrainingNodes())
		(dynamic_cast<TrainingLayer*>(training_node->layer))->SetTestingMode(true);

  graph_.SetIsTesting(true);

  LOGDEBUG << "Testing, iterations: " << iterations <<
           ", batch size: " << first_training_layer_->GetBatchSize();

  auto t_begin = std::chrono::system_clock::now();

  for (unsigned int i = 0; i < iterations; i++) {
    graph_.FeedForward();
    for (unsigned int n = 0; n < graph_.GetLossNodes().size(); n++) {
      LossFunctionLayer* lossfunction_layer = dynamic_cast<LossFunctionLayer*>(graph_.GetLossNodes()[n]->layer);
			loss_sums[n] += lossfunction_layer->CalculateLossFunction();
		}
	}

  auto t_end = std::chrono::system_clock::now();
  std::chrono::duration<double> t_diff = t_end - t_begin;
  LOGINFO << "Testing, sps: " <<
          (datum) (sample_count_ * iterations)
          / (datum) t_diff.count();

  LOGINFO << "Testing, tps: " <<
          1000000.0f * (datum) t_diff.count() /
          (datum) (sample_count_ * iterations) << " us";

	for (unsigned int n = 0; n < graph_.GetLossNodes().size(); n++) {
		LossFunctionLayer* lossfunction_layer = dynamic_cast<LossFunctionLayer*>(graph_.GetLossNodes()[n]->layer);
		LOGDEBUG << "Testing, " << graph_.GetLossNodes()[n]->layer->GetLayerDescription() <<  " lps: " << loss_sums[n] / (datum)(iterations * sample_count_);
	}

	for (unsigned int n = 0; n < graph_.GetStatNodes().size(); n++) {
		StatLayer* stat_layer = dynamic_cast<StatLayer*>(graph_.GetStatNodes()[n]->layer);
    std::stringstream epochname;
    epochname << "Testing  - Epoch " << epoch_ << " -";
    stat_layer->Print (epochname.str(), false);
    stat_layer->Reset();
	}

	for (NetGraphNode* training_node : graph_.GetTrainingNodes())
		(dynamic_cast<TrainingLayer*>(training_node->layer))->SetTestingMode(false);

  graph_.SetIsTesting(false);

	delete[] loss_sums;
}

void Trainer::Epoch() {
	datum* loss_sums = new datum[graph_.GetLossNodes().size()];
	for (unsigned int n = 0; n < graph_.GetLossNodes().size(); n++)
		loss_sums[n] = 0;

  unsigned int iterations =
    settings_.iterations == 0 ?
    first_training_layer_->GetSamplesInTrainingSet() :
    settings_.iterations;
  iterations = (unsigned int) ( ( (datum) iterations) *
                                settings_.epoch_training_ratio);

  unsigned int fiftieth = 0;
  unsigned int tenth = 0;

	for (NetGraphNode* training_node : graph_.GetTrainingNodes())
		(dynamic_cast<TrainingLayer*>(training_node->layer))->SetTestingMode(false);

  LOGDEBUG << "Epoch: " << epoch_ << ", it: " << iterations <<
           ", bsize: " << first_training_layer_->GetBatchSize() * settings_.sbatchsize << ", lr0: " <<
           CalculateLR (epoch_ * iterations) << std::endl;

  auto t_begin = std::chrono::system_clock::now();

  for (unsigned int i = 0; i < iterations; i++) {
    if ( (50 * i / iterations) > fiftieth) {
      fiftieth = 50 * i / iterations;
      std::cout << "." << std::flush;
    }

    if ( (10 * i / iterations) > tenth) {
      tenth = 10 * i / iterations;
      std::cout << tenth << "0%" << std::flush;
    }

    // Reset gradients
    for (unsigned int np = 0; np < accumulated_gradients_.size(); np++)
      accumulated_gradients_[np]->Clear();

    for (unsigned int b = 0; b < settings_.sbatchsize; b++) {
      graph_.FeedForward();

      // Save errors
			for (unsigned int n = 0; n < graph_.GetLossNodes().size(); n++) {
				LossFunctionLayer* lossfunction_layer = dynamic_cast<LossFunctionLayer*>(graph_.GetLossNodes()[n]->layer);
				loss_sums[n] += lossfunction_layer->CalculateLossFunction();
			}

      // Correct errors
      graph_.BackPropagate();

      unsigned int np = 0;

      // Accumulate gradients
      for (unsigned int l = 0; l < graph_.GetNodes().size(); l++) {
				Layer* const layer = graph_.GetNodes()[l]->layer;
        for (unsigned int p = 0; p < layer->parameters().size(); p++) {
          Tensor& gradients = layer->parameters() [p]->delta;
#ifdef BUILD_OPENCL
          gradients.MoveToCPU();
#endif

          for (unsigned int e = 0; e < gradients.elements(); e++) {
            (* (accumulated_gradients_[np])) [e] += gradients[e];
          }

          np++;
        }
      }
    }

    // Calculate annealed learning rate
    const datum lr =
      CalculateLR (epoch_ * iterations + i);

    // Apply gradients with new learning rate
    ApplyGradients (lr);
  }

  auto t_end = std::chrono::system_clock::now();
  std::chrono::duration<double> t_diff = t_end - t_begin;
  LOGINFO << "Training, sps: " <<
          (datum) (sample_count_ * settings_.sbatchsize
                   * first_training_layer_->GetLossSamplingProbability() * iterations)
          / (datum) t_diff.count();

  LOGINFO << "Training, tps: " <<
          1000000.0f * (datum) t_diff.count() /
          (datum) (sample_count_ * settings_.sbatchsize
                   * first_training_layer_->GetLossSamplingProbability() * iterations) << " us";

  // Display training epoch_error
	for (unsigned int n = 0; n < graph_.GetLossNodes().size(); n++) {
		LossFunctionLayer* lossfunction_layer = dynamic_cast<LossFunctionLayer*>(graph_.GetLossNodes()[n]->layer);
		LOGDEBUG << "Training, " << graph_.GetLossNodes()[n]->layer->GetLayerDescription() <<  " lps: " << loss_sums[n] / (datum)(iterations * sample_count_ * settings_.sbatchsize * first_training_layer_->GetLossSamplingProbability());
	}

	for (unsigned int n = 0; n < graph_.GetStatNodes().size(); n++) {
		StatLayer* stat_layer = dynamic_cast<StatLayer*>(graph_.GetStatNodes()[n]->layer);
    std::stringstream epochname;
    epochname << "Training  - Epoch " << epoch_ << " -";
    stat_layer->Print (epochname.str(), true);
    stat_layer->Reset();
	}

  delete[] loss_sums;
  epoch_++;
}

void Trainer::ApplyGradients (datum lr) {
  unsigned int dp = 0;

	for (unsigned int l = 0; l < graph_.GetNodes().size(); l++) {
		Layer* const layer = graph_.GetNodes()[l]->layer;
    const datum llr = lr * layer->local_lr_;

    for (unsigned int p = 0; p < layer->parameters().size(); p++) {
      CombinedTensor* const param = layer->parameters_[p];
#ifdef BUILD_OPENCL
      param->data.MoveToCPU();
#endif

      for (unsigned int w = 0; w < param->data.elements(); w++) {
        const datum weight = param->data (w);
        const datum l1_gradient = (weight > 0) - (weight < 0);
        const datum l2_gradient = weight;
        const datum w_gradient = (*accumulated_gradients_[dp]) [w];

        /*
         * http://www.iro.umontreal.ca/~pift6266/H10/notes/gradient.html
         *
         * This site says that one should average the gradient over
         * the minibatch
         */
        const datum last_delta = (*last_deltas_[dp]) (w);
        const datum delta =

          // Average of gradient over minibatch
          llr * (w_gradient / (datum) (sample_count_ * settings_.sbatchsize)) +
          // Regularization
          llr * (settings_.l2_weight * l2_gradient + settings_.l1_weight * l1_gradient);

        const datum step = delta + settings_.momentum * last_delta;
        param->data[w] -= step;

        // Backup delta
        (*last_deltas_[dp]) [w] = step;
      }

      dp++;
    }
  }
}

std::ostream& operator<< (std::ostream & output,
                          const TrainerSettings settings) {
  output << "LR: " << settings.learning_rate << ", ";
  output << "GM: " << settings.gamma << ", ";
  output << "EX: " << settings.exponent << ", ";
  output << "SB: " << settings.sbatchsize << ", ";
  output << "PB: " << settings.pbatchsize << ", ";
  output << "L1: " << settings.l1_weight << ", ";
  output << "L2: " << settings.l2_weight << ", ";
  output << "MM: " << settings.momentum;
  return output;
}

}
<|endoftext|>


<commit_before>//===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This builds an AST and converts it to LLVM Code.
//
//===----------------------------------------------------------------------===//

#include "clang/CodeGen/ModuleBuilder.h"
#include "CGDebugInfo.h"
#include "CodeGenModule.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/Expr.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Frontend/CodeGenOptions.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
using namespace clang;

namespace clang {
  class CodeGeneratorImpl : public CodeGenerator {
    DiagnosticsEngine &Diags;
    OwningPtr<const llvm::DataLayout> TD;
    ASTContext *Ctx;
    const CodeGenOptions CodeGenOpts;  // Intentionally copied in.
  protected:
    OwningPtr<llvm::Module> M;
    OwningPtr<CodeGen::CodeGenModule> Builder;
  public:
    CodeGeneratorImpl(DiagnosticsEngine &diags, const std::string& ModuleName,
                      const CodeGenOptions &CGO, llvm::LLVMContext& C)
      : Diags(diags), CodeGenOpts(CGO),
        M(new llvm::Module(ModuleName, C)) {}

    virtual ~CodeGeneratorImpl() {}

    virtual llvm::Module* GetModule() {
      return M.get();
    }

    virtual llvm::Module* ReleaseModule() {
      assert(M && "Releasing 0?");
      Builder->Release();
      return M.take();
    }

     void print(llvm::raw_ostream& out) {
        //llvm::SmallPtrSet<llvm::GlobalValue*, 10> WeakRefReferences;
        //llvm::StringMap<GlobalDecl> DeferredDecls;
        //std::vector<DeferredGlobal> DeferredDeclsToEmit;
        //std::vector<GlobalDecl> Aliases;
        //ReplacementsTy Replacements;
        //std::vector<const CXXRecordDecl*> DeferredVTables;
        //std::vector<llvm::WeakVH> LLVMUsed;
        //CtorList GlobalCtors;
        //CtorList GlobalDtors;
        //llvm::DenseMap<GlobalDecl, StringRef> MangledDeclNames;
        //std::vector<llvm::Constant*> Annotations;
        //llvm::StringMap<llvm::Constant*> AnnotationStrings;
        //llvm::StringMap<llvm::Constant*> CFConstantStringMap;
        //llvm::StringMap<llvm::GlobalVariable*> ConstantStringMap;
        out << "\n\nCodeGen:\n";
        out << " ConstantStringMap @ " << &Builder->ConstantStringMap << "\n";
        for(llvm::StringMap<llvm::GlobalVariable*>::const_iterator I
               = Builder->ConstantStringMap.begin(),
               E = Builder->ConstantStringMap.end(); I != E;
            ++I) {
           out << I->getKey().str().c_str() << " : " << I->getValue() << "\n";
        }
        //llvm::DenseMap<const Decl*, llvm::Constant *> StaticLocalDeclMap;
        //llvm::DenseMap<const Decl*, llvm::GlobalVariable*> StaticLocalDeclGuardMap;
        //llvm::DenseMap<const Expr*, llvm::Constant *> MaterializedGlobalTemporaryMap;
        //llvm::DenseMap<QualType, llvm::Constant *> AtomicSetterHelperFnMap;
        //llvm::DenseMap<QualType, llvm::Constant *> AtomicGetterHelperFnMap;
        //llvm::DenseMap<QualType, llvm::Constant *> TypeDescriptorMap;
        //StaticExternCMap StaticExternCValues;
        //std::vector<std::pair<const VarDecl *, llvm::GlobalVariable *> >
        // CXXThreadLocals;
        //std::vector<llvm::Constant*> CXXThreadLocalInits;
        //std::vector<llvm::Constant*> CXXGlobalInits;
        //llvm::DenseMap<const Decl*, unsigned> DelayedCXXInitPosition;
        //SmallVector<GlobalInitData, 8> PrioritizedCXXGlobalInits;
        //std::vector<std::pair<llvm::WeakVH,llvm::Constant*> > CXXGlobalDtors;
        //llvm::SetVector<clang::Module *> ImportedModules;
        //SmallVector<llvm::Value *, 16> LinkerOptionsMetadata;
        //
        out.flush();
     }

    virtual void Initialize(ASTContext &Context) {
      Ctx = &Context;

      M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
      M->setDataLayout(Ctx->getTargetInfo().getTargetDescription());
      TD.reset(new llvm::DataLayout(Ctx->getTargetInfo().getTargetDescription()));
      Builder.reset(new CodeGen::CodeGenModule(Context, CodeGenOpts, *M, *TD,
                                               Diags));

      for (size_t i = 0, e = CodeGenOpts.DependentLibraries.size(); i < e; ++i)
        HandleDependentLibrary(CodeGenOpts.DependentLibraries[i]);
    }

    virtual void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
      if (Diags.hasErrorOccurred())
        return;

      Builder->HandleCXXStaticMemberVarInstantiation(VD);
    }

    virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
      if (Diags.hasErrorOccurred())
        return true;

      // Make sure to emit all elements of a Decl.
      for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
        Builder->EmitTopLevelDecl(*I);
      return true;
    }

    /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
    /// to (e.g. struct, union, enum, class) is completed. This allows the
    /// client hack on the type, which can occur at any point in the file
    /// (because these can be defined in declspecs).
    virtual void HandleTagDeclDefinition(TagDecl *D) {
      if (Diags.hasErrorOccurred())
        return;

      Builder->UpdateCompletedType(D);

      // In C++, we may have member functions that need to be emitted at this
      // point.
      if (Ctx->getLangOpts().CPlusPlus && !D->isDependentContext()) {
        for (DeclContext::decl_iterator M = D->decls_begin(),
                                     MEnd = D->decls_end();
             M != MEnd; ++M)
          if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*M))
            if (Method->doesThisDeclarationHaveABody() &&
                (Method->hasAttr<UsedAttr>() ||
                 Method->hasAttr<ConstructorAttr>()))
              Builder->EmitTopLevelDecl(Method);
      }
    }

    virtual void HandleTagDeclRequiredDefinition(const TagDecl *D) LLVM_OVERRIDE {
      if (Diags.hasErrorOccurred())
        return;

      if (CodeGen::CGDebugInfo *DI = Builder->getModuleDebugInfo())
        if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
          DI->completeRequiredType(RD);
    }

    virtual void HandleTranslationUnit(ASTContext &Ctx) {
      if (Diags.hasErrorOccurred()) {
        if (Builder)
          Builder->clear();
        M.reset();
        return;
      }

      if (Builder)
        Builder->Release();
    }

    virtual void CompleteTentativeDefinition(VarDecl *D) {
      if (Diags.hasErrorOccurred())
        return;

      Builder->EmitTentativeDefinition(D);
    }

    virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {
      if (Diags.hasErrorOccurred())
        return;

      Builder->EmitVTable(RD, DefinitionRequired);
    }

    virtual void HandleLinkerOptionPragma(llvm::StringRef Opts) {
      Builder->AppendLinkerOptions(Opts);
    }

    virtual void HandleDetectMismatch(llvm::StringRef Name,
                                      llvm::StringRef Value) {
      Builder->AddDetectMismatch(Name, Value);
    }

    virtual void HandleDependentLibrary(llvm::StringRef Lib) {
      Builder->AddDependentLib(Lib);
    }
  };
}

void CodeGenerator::anchor() { }

CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags,
                                        const std::string& ModuleName,
                                        const CodeGenOptions &CGO,
                                        const TargetOptions &/*TO*/,
                                        llvm::LLVMContext& C) {
  return new CodeGeneratorImpl(Diags, ModuleName, CGO, C);
}
<commit_msg>2 col indent.<commit_after>//===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This builds an AST and converts it to LLVM Code.
//
//===----------------------------------------------------------------------===//

#include "clang/CodeGen/ModuleBuilder.h"
#include "CGDebugInfo.h"
#include "CodeGenModule.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/Expr.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Frontend/CodeGenOptions.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
using namespace clang;

namespace clang {
  class CodeGeneratorImpl : public CodeGenerator {
    DiagnosticsEngine &Diags;
    OwningPtr<const llvm::DataLayout> TD;
    ASTContext *Ctx;
    const CodeGenOptions CodeGenOpts;  // Intentionally copied in.
  protected:
    OwningPtr<llvm::Module> M;
    OwningPtr<CodeGen::CodeGenModule> Builder;
  public:
    CodeGeneratorImpl(DiagnosticsEngine &diags, const std::string& ModuleName,
                      const CodeGenOptions &CGO, llvm::LLVMContext& C)
      : Diags(diags), CodeGenOpts(CGO),
        M(new llvm::Module(ModuleName, C)) {}

    virtual ~CodeGeneratorImpl() {}

    virtual llvm::Module* GetModule() {
      return M.get();
    }

    virtual llvm::Module* ReleaseModule() {
      assert(M && "Releasing 0?");
      Builder->Release();
      return M.take();
    }

    void print(llvm::raw_ostream& out) {
      //llvm::SmallPtrSet<llvm::GlobalValue*, 10> WeakRefReferences;
      //llvm::StringMap<GlobalDecl> DeferredDecls;
      //std::vector<DeferredGlobal> DeferredDeclsToEmit;
      //std::vector<GlobalDecl> Aliases;
      //ReplacementsTy Replacements;
      //std::vector<const CXXRecordDecl*> DeferredVTables;
      //std::vector<llvm::WeakVH> LLVMUsed;
      //CtorList GlobalCtors;
      //CtorList GlobalDtors;
      //llvm::DenseMap<GlobalDecl, StringRef> MangledDeclNames;
      //std::vector<llvm::Constant*> Annotations;
      //llvm::StringMap<llvm::Constant*> AnnotationStrings;
      //llvm::StringMap<llvm::Constant*> CFConstantStringMap;
      //llvm::StringMap<llvm::GlobalVariable*> ConstantStringMap;
      out << "\n\nCodeGen:\n";
      out << " ConstantStringMap @ " << &Builder->ConstantStringMap << "\n";
      for(llvm::StringMap<llvm::GlobalVariable*>::const_iterator I
            = Builder->ConstantStringMap.begin(),
            E = Builder->ConstantStringMap.end(); I != E; ++I) {
        out << I->getKey().str().c_str() << " : " << I->getValue() << "\n";
      }
      //llvm::DenseMap<const Decl*, llvm::Constant *> StaticLocalDeclMap;
      //llvm::DenseMap<const Decl*, llvm::GlobalVariable*> StaticLocalDeclGuardMap;
      //llvm::DenseMap<const Expr*, llvm::Constant *> MaterializedGlobalTemporaryMap;
      //llvm::DenseMap<QualType, llvm::Constant *> AtomicSetterHelperFnMap;
      //llvm::DenseMap<QualType, llvm::Constant *> AtomicGetterHelperFnMap;
      //llvm::DenseMap<QualType, llvm::Constant *> TypeDescriptorMap;
      //StaticExternCMap StaticExternCValues;
      //std::vector<std::pair<const VarDecl *, llvm::GlobalVariable *> >
      // CXXThreadLocals;
      //std::vector<llvm::Constant*> CXXThreadLocalInits;
      //std::vector<llvm::Constant*> CXXGlobalInits;
      //llvm::DenseMap<const Decl*, unsigned> DelayedCXXInitPosition;
      //SmallVector<GlobalInitData, 8> PrioritizedCXXGlobalInits;
      //std::vector<std::pair<llvm::WeakVH,llvm::Constant*> > CXXGlobalDtors;
      //llvm::SetVector<clang::Module *> ImportedModules;
      //SmallVector<llvm::Value *, 16> LinkerOptionsMetadata;
      //
      out.flush();
    }

    virtual void Initialize(ASTContext &Context) {
      Ctx = &Context;

      M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
      M->setDataLayout(Ctx->getTargetInfo().getTargetDescription());
      TD.reset(new llvm::DataLayout(Ctx->getTargetInfo().getTargetDescription()));
      Builder.reset(new CodeGen::CodeGenModule(Context, CodeGenOpts, *M, *TD,
                                               Diags));

      for (size_t i = 0, e = CodeGenOpts.DependentLibraries.size(); i < e; ++i)
        HandleDependentLibrary(CodeGenOpts.DependentLibraries[i]);
    }

    virtual void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
      if (Diags.hasErrorOccurred())
        return;

      Builder->HandleCXXStaticMemberVarInstantiation(VD);
    }

    virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
      if (Diags.hasErrorOccurred())
        return true;

      // Make sure to emit all elements of a Decl.
      for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
        Builder->EmitTopLevelDecl(*I);
      return true;
    }

    /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
    /// to (e.g. struct, union, enum, class) is completed. This allows the
    /// client hack on the type, which can occur at any point in the file
    /// (because these can be defined in declspecs).
    virtual void HandleTagDeclDefinition(TagDecl *D) {
      if (Diags.hasErrorOccurred())
        return;

      Builder->UpdateCompletedType(D);

      // In C++, we may have member functions that need to be emitted at this
      // point.
      if (Ctx->getLangOpts().CPlusPlus && !D->isDependentContext()) {
        for (DeclContext::decl_iterator M = D->decls_begin(),
                                     MEnd = D->decls_end();
             M != MEnd; ++M)
          if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*M))
            if (Method->doesThisDeclarationHaveABody() &&
                (Method->hasAttr<UsedAttr>() ||
                 Method->hasAttr<ConstructorAttr>()))
              Builder->EmitTopLevelDecl(Method);
      }
    }

    virtual void HandleTagDeclRequiredDefinition(const TagDecl *D) LLVM_OVERRIDE {
      if (Diags.hasErrorOccurred())
        return;

      if (CodeGen::CGDebugInfo *DI = Builder->getModuleDebugInfo())
        if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
          DI->completeRequiredType(RD);
    }

    virtual void HandleTranslationUnit(ASTContext &Ctx) {
      if (Diags.hasErrorOccurred()) {
        if (Builder)
          Builder->clear();
        M.reset();
        return;
      }

      if (Builder)
        Builder->Release();
    }

    virtual void CompleteTentativeDefinition(VarDecl *D) {
      if (Diags.hasErrorOccurred())
        return;

      Builder->EmitTentativeDefinition(D);
    }

    virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {
      if (Diags.hasErrorOccurred())
        return;

      Builder->EmitVTable(RD, DefinitionRequired);
    }

    virtual void HandleLinkerOptionPragma(llvm::StringRef Opts) {
      Builder->AppendLinkerOptions(Opts);
    }

    virtual void HandleDetectMismatch(llvm::StringRef Name,
                                      llvm::StringRef Value) {
      Builder->AddDetectMismatch(Name, Value);
    }

    virtual void HandleDependentLibrary(llvm::StringRef Lib) {
      Builder->AddDependentLib(Lib);
    }
  };
}

void CodeGenerator::anchor() { }

CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags,
                                        const std::string& ModuleName,
                                        const CodeGenOptions &CGO,
                                        const TargetOptions &/*TO*/,
                                        llvm::LLVMContext& C) {
  return new CodeGeneratorImpl(Diags, ModuleName, CGO, C);
}
<|endoftext|>