<commit_before>// Begin CVS Header
//   $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/bindings/cpp_examples/example7/example7.cpp,v $
//   $Revision: 1.2 $
//   $Name:  $
//   $Author: gauges $
//   $Date: 2009/08/31 18:36:12 $
// End CVS Header

// Copyright (C) 2008 by Pedro Mendes, Virginia Tech Intellectual
// Properties, Inc., EML Research, gGmbH, University of Heidelberg,
// and The University of Manchester.
// All rights reserved.

/**
 * This is an example on how to create user defined kinetic functions with the COPASI API
 */
#include <iostream>
#include <vector>
#include <string>
#include <set>

#define COPASI_MAIN

#include "copasi/copasi.h"
#include "copasi/report/CCopasiRootContainer.h"
#include "copasi/CopasiDataModel/CCopasiDataModel.h"
#include "copasi/model/CModel.h"
#include "copasi/model/CCompartment.h"
#include "copasi/model/CMetab.h"
#include "copasi/model/CReaction.h"
#include "copasi/model/CChemEq.h"
#include "copasi/model/CModelValue.h"
#include "copasi/function/CFunctionDB.h"
#include "copasi/function/CFunction.h"
#include "copasi/function/CEvaluationTree.h"

int main()
{
  // initialize the backend library
  // since we are not interested in the arguments
  // that are passed to main, we pass 0 and NULL to
  // init
  CCopasiRootContainer::init(0, NULL);
  assert(CCopasiRootContainer::getRoot() != NULL);
  // create a new datamodel
  CCopasiDataModel* pDataModel = CCopasiRootContainer::addDatamodel();
  assert(CCopasiRootContainer::getDatamodelList()->size() == 1);
  // get the model from the datamodel
  CModel* pModel = pDataModel->getModel();
  assert(pModel != NULL);
  // set the units for the model
  // we want seconds as the time unit
  // microliter as the volume units
  // and nanomole as the substance units
  pModel->setTimeUnit(CModel::s);
  pModel->setVolumeUnit(CModel::microl);
  pModel->setQuantityUnit(CModel::nMol);

  // we have to keep a set of all the initial values that are changed during
  // the model building process
  // They are needed after the model has been built to make sure all initial
  // values are set to the correct initial value
  std::set<const CCopasiObject*> changedObjects;

  // create a compartment with the name cell and an initial volume of 5.0
  // microliter
  CCompartment* pCompartment = pModel->createCompartment("cell", 5.0);
  const CCopasiObject* pObject = pCompartment->getObject(CCopasiObjectName("Reference=InitialVolume"));
  assert(pObject != NULL);
  changedObjects.insert(pObject);
  assert(pCompartment != NULL);
  assert(pModel->getCompartments().size() == 1);
  // create a new metabolite with the name S and an inital
  // concentration of 10 nanomol
  // the metabolite belongs to the compartment we created and is is to be
  // fixed
  CMetab* pS = pModel->createMetabolite("S", pCompartment->getObjectName(), 10.0, CMetab::FIXED);
  pObject = pS->getObject(CCopasiObjectName("Reference=InitialConcentration"));
  assert(pObject != NULL);
  changedObjects.insert(pObject);
  assert(pCompartment != NULL);
  assert(pS != NULL);
  assert(pModel->getMetabolites().size() == 1);
  // create a second metabolite called P with an initial
  // concentration of 0. This metabolite is to be changed by reactions
  CMetab* pP = pModel->createMetabolite("P", pCompartment->getObjectName(), 0.0, CMetab::REACTIONS);
  assert(pP != NULL);
  pObject = pP->getObject(CCopasiObjectName("Reference=InitialConcentration"));
  assert(pObject != NULL);
  changedObjects.insert(pObject);
  assert(pModel->getMetabolites().size() == 2);
  // now we create a reaction
  CReaction* pReaction = pModel->createReaction("reaction");
  assert(pReaction != NULL);
  assert(pModel->getReactions().size() == 1);
  // reaction converts S to P
  // we can set these on the chemical equation of the reaction
  CChemEq* pChemEq = &pReaction->getChemEq();
  // glucose is a substrate with stoichiometry 1
  pChemEq->addMetabolite(pS->getKey(), 1.0, CChemEq::SUBSTRATE);
  // glucose-6-phosphate is a product with stoichiometry 1
  pChemEq->addMetabolite(pP->getKey(), 1.0, CChemEq::PRODUCT);
  assert(pChemEq->getSubstrates().size() == 1);
  assert(pChemEq->getProducts().size() == 1);
  // this reaction is to be irreversible
  pReaction->setReversible(false);
  assert(pReaction->isReversible() == false);

  CModelValue* pMV = pModel->createModelValue("K", 42.0);
  // set the status to FIXED
  pMV->setStatus(CModelValue::FIXED);

  assert(pMV != NULL);

  pObject = pMV->getObject(CCopasiObjectName("Reference=InitialValue"));
  assert(pObject != NULL);

  changedObjects.insert(pObject);
  assert(pModel->getModelValues().size() == 1);

  // now we ned to set a kinetic law on the reaction
  // for this we create a user defined function
  CFunctionDB* pFunDB = CCopasiRootContainer::getFunctionList();
  assert(pFunDB != NULL);

  CKinFunction* pFunction = new CKinFunction("My Rate Law");

  pFunDB->add(pFunction, true);
  CFunction* pRateLaw = dynamic_cast<CFunction*>(pFunDB->findFunction("My Rate Law"));

  assert(pRateLaw != NULL);

  // now we create the formula for the function and set it on the function
  std::string formula = "(1-0.4/(EXPONENTIALE^(temp-37)))*0.00001448471257*1.4^(temp-37)*substrate";

  bool result = pFunction->setInfix(formula);
  assert(result == true);
  // make the function irreversible
  pFunction->setReversible(TriFalse);
  // the formula string should have been parsed now
  // and COPASI should have determined that the formula string contained 2 parameters (temp and substrate)
  CFunctionParameters& variables = pFunction->getVariables();
  // per default the usage of those parameters will be set to VARIABLE
  unsigned C_INT32 index = pFunction->getVariableIndex("temp");
  assert(index != C_INVALID_INDEX);
  CFunctionParameter* pParam = variables[index];
  assert(pParam->getUsage() == CFunctionParameter::VARIABLE);
  // This is correct for temp, but substrate should get the usage SUBSTRATE in order
  // for us to use the function with the reaction created above
  // So we need to set the usage for "substrate" manually
  index = pFunction->getVariableIndex("substrate");
  assert(index != C_INVALID_INDEX);
  pParam = variables[index];
  pParam->setUsage(CFunctionParameter::SUBSTRATE);

  // set the rate law for the reaction
  pReaction->setFunction(pRateLaw);
  assert(pReaction->getFunction() != NULL);

  // COPASI also needs to know what object it has to assocuiate with the individual function parameters
  // In our case we need to tell COPASI that substrate is to be replaced by the substrate of the reaction
  // and temp is to be replaced by the global parameter K
  pReaction->setParameterMapping("substrate", pS->getKey());
  pReaction->setParameterMapping("temp", pMV->getKey());

  // finally compile the model
  // compile needs to be done before updating all initial values for
  // the model with the refresh sequence
  pModel->compileIfNecessary(NULL);

  // now that we are done building the model, we have to make sure all
  // initial values are updated according to their dependencies
  std::vector<Refresh*> refreshes = pModel->buildInitialRefreshSequence(changedObjects);
  std::vector<Refresh*>::iterator it2 = refreshes.begin(), endit2 = refreshes.end();

  while (it2 != endit2)
    {
      // call each refresh
      (**it2)();
      ++it2;
    }

  // save the model to a COPASI file
  // we save to a file named example1.cps, we don't want a progress report
  // and we want to overwrite any existing file with the same name
  // Default tasks are automatically generated and will always appear in cps
  // file unless they are explicitley deleted before saving.
  pDataModel->saveModel("example7.cps", NULL, true);

  // export the model to an SBML file
  // we save to a file named example1.xml, we want to overwrite any
  // existing file with the same name and we want SBML L2V3
  pDataModel->exportSBML("example7.xml", true, 2, 3);

  // destroy the root container once we are done
  CCopasiRootContainer::destroy();
}
<commit_msg>Small fixes to the comments and to the code.<commit_after>// Begin CVS Header
//   $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/bindings/cpp_examples/example7/example7.cpp,v $
//   $Revision: 1.3 $
//   $Name:  $
//   $Author: gauges $
//   $Date: 2009/08/31 19:34:31 $
// End CVS Header

// Copyright (C) 2008 by Pedro Mendes, Virginia Tech Intellectual
// Properties, Inc., EML Research, gGmbH, University of Heidelberg,
// and The University of Manchester.
// All rights reserved.

/**
 * This is an example on how to create user defined kinetic functions with the COPASI API
 */
#include <iostream>
#include <vector>
#include <string>
#include <set>

#define COPASI_MAIN

#include "copasi/copasi.h"
#include "copasi/report/CCopasiRootContainer.h"
#include "copasi/CopasiDataModel/CCopasiDataModel.h"
#include "copasi/model/CModel.h"
#include "copasi/model/CCompartment.h"
#include "copasi/model/CMetab.h"
#include "copasi/model/CReaction.h"
#include "copasi/model/CChemEq.h"
#include "copasi/model/CModelValue.h"
#include "copasi/function/CFunctionDB.h"
#include "copasi/function/CFunction.h"
#include "copasi/function/CEvaluationTree.h"

int main()
{
  // initialize the backend library
  // since we are not interested in the arguments
  // that are passed to main, we pass 0 and NULL to
  // init
  CCopasiRootContainer::init(0, NULL);
  assert(CCopasiRootContainer::getRoot() != NULL);
  // create a new datamodel
  CCopasiDataModel* pDataModel = CCopasiRootContainer::addDatamodel();
  assert(CCopasiRootContainer::getDatamodelList()->size() == 1);
  // get the model from the datamodel
  CModel* pModel = pDataModel->getModel();
  assert(pModel != NULL);
  // set the units for the model
  // we want seconds as the time unit
  // microliter as the volume units
  // and nanomole as the substance units
  pModel->setTimeUnit(CModel::s);
  pModel->setVolumeUnit(CModel::microl);
  pModel->setQuantityUnit(CModel::nMol);

  // we have to keep a set of all the initial values that are changed during
  // the model building process
  // They are needed after the model has been built to make sure all initial
  // values are set to the correct initial value
  std::set<const CCopasiObject*> changedObjects;

  // create a compartment with the name cell and an initial volume of 5.0
  // microliter
  CCompartment* pCompartment = pModel->createCompartment("cell", 5.0);
  const CCopasiObject* pObject = pCompartment->getObject(CCopasiObjectName("Reference=InitialVolume"));
  assert(pObject != NULL);
  changedObjects.insert(pObject);
  assert(pCompartment != NULL);
  assert(pModel->getCompartments().size() == 1);
  // create a new metabolite with the name S and an inital
  // concentration of 10 nanomol
  // the metabolite belongs to the compartment we created and is is to be
  // fixed
  CMetab* pS = pModel->createMetabolite("S", pCompartment->getObjectName(), 10.0, CMetab::FIXED);
  pObject = pS->getObject(CCopasiObjectName("Reference=InitialConcentration"));
  assert(pObject != NULL);
  changedObjects.insert(pObject);
  assert(pCompartment != NULL);
  assert(pS != NULL);
  assert(pModel->getMetabolites().size() == 1);
  // create a second metabolite called P with an initial
  // concentration of 0. This metabolite is to be changed by reactions
  CMetab* pP = pModel->createMetabolite("P", pCompartment->getObjectName(), 0.0, CMetab::REACTIONS);
  assert(pP != NULL);
  pObject = pP->getObject(CCopasiObjectName("Reference=InitialConcentration"));
  assert(pObject != NULL);
  changedObjects.insert(pObject);
  assert(pModel->getMetabolites().size() == 2);
  // now we create a reaction
  CReaction* pReaction = pModel->createReaction("reaction");
  assert(pReaction != NULL);
  assert(pModel->getReactions().size() == 1);
  // reaction converts S to P
  // we can set these on the chemical equation of the reaction
  CChemEq* pChemEq = &pReaction->getChemEq();
  // S is a substrate with stoichiometry 1
  pChemEq->addMetabolite(pS->getKey(), 1.0, CChemEq::SUBSTRATE);
  // P is a product with stoichiometry 1
  pChemEq->addMetabolite(pP->getKey(), 1.0, CChemEq::PRODUCT);
  assert(pChemEq->getSubstrates().size() == 1);
  assert(pChemEq->getProducts().size() == 1);
  // this reaction is to be irreversible
  pReaction->setReversible(false);
  assert(pReaction->isReversible() == false);

  CModelValue* pMV = pModel->createModelValue("K", 42.0);
  // set the status to FIXED
  pMV->setStatus(CModelValue::FIXED);
  assert(pMV != NULL);
  pObject = pMV->getObject(CCopasiObjectName("Reference=InitialValue"));
  assert(pObject != NULL);
  changedObjects.insert(pObject);
  assert(pModel->getModelValues().size() == 1);

  // now we ned to set a kinetic law on the reaction
  // for this we create a user defined function
  CFunctionDB* pFunDB = CCopasiRootContainer::getFunctionList();
  assert(pFunDB != NULL);

  CKinFunction* pFunction = new CKinFunction("My Rate Law");

  pFunDB->add(pFunction, true);
  CFunction* pRateLaw = dynamic_cast<CFunction*>(pFunDB->findFunction("My Rate Law"));

  assert(pRateLaw != NULL);

  // now we create the formula for the function and set it on the function
  std::string formula = "(1-0.4/(EXPONENTIALE^(temp-37)))*0.00001448471257*1.4^(temp-37)*substrate";

  bool result = pFunction->setInfix(formula);
  assert(result == true);
  // make the function irreversible
  pFunction->setReversible(TriFalse);
  // the formula string should have been parsed now
  // and COPASI should have determined that the formula string contained 2 parameters (temp and substrate)
  CFunctionParameters& variables = pFunction->getVariables();
  // per default the usage of those parameters will be set to VARIABLE
  unsigned C_INT32 index = pFunction->getVariableIndex("temp");
  assert(index != C_INVALID_INDEX);
  CFunctionParameter* pParam = variables[index];
  assert(pParam->getUsage() == CFunctionParameter::VARIABLE);
  // This is correct for temp, but substrate should get the usage SUBSTRATE in order
  // for us to use the function with the reaction created above
  // So we need to set the usage for "substrate" manually
  index = pFunction->getVariableIndex("substrate");
  assert(index != C_INVALID_INDEX);
  pParam = variables[index];
  pParam->setUsage(CFunctionParameter::SUBSTRATE);

  // set the rate law for the reaction
  pReaction->setFunction(pFunction);
  assert(pReaction->getFunction() != NULL);

  // COPASI also needs to know what object it has to assocuiate with the individual function parameters
  // In our case we need to tell COPASI that substrate is to be replaced by the substrate of the reaction
  // and temp is to be replaced by the global parameter K
  pReaction->setParameterMapping("substrate", pS->getKey());
  pReaction->setParameterMapping("temp", pMV->getKey());

  // finally compile the model
  // compile needs to be done before updating all initial values for
  // the model with the refresh sequence
  pModel->compileIfNecessary(NULL);

  // now that we are done building the model, we have to make sure all
  // initial values are updated according to their dependencies
  std::vector<Refresh*> refreshes = pModel->buildInitialRefreshSequence(changedObjects);
  std::vector<Refresh*>::iterator it2 = refreshes.begin(), endit2 = refreshes.end();

  while (it2 != endit2)
    {
      // call each refresh
      (**it2)();
      ++it2;
    }

  // save the model to a COPASI file
  // we save to a file named example1.cps, we don't want a progress report
  // and we want to overwrite any existing file with the same name
  // Default tasks are automatically generated and will always appear in cps
  // file unless they are explicitley deleted before saving.
  pDataModel->saveModel("example7.cps", NULL, true);

  // export the model to an SBML file
  // we save to a file named example1.xml, we want to overwrite any
  // existing file with the same name and we want SBML L2V3
  pDataModel->exportSBML("example7.xml", true, 2, 3);

  // destroy the root container once we are done
  CCopasiRootContainer::destroy();
}
<|endoftext|>


<commit_before>/*
-----------------------------------------------------------------------------
This source file is part of OGRE
(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/

Copyright (c) 2000-2009 Torus Knot Software Ltd

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/
#include "OgrePlatform.h"
#include "OgreStableHeaders.h"
#include "OgrePrerequisites.h"
#include "OgreMemoryTracker.h"
#include "OgreString.h"

namespace Ogre
{

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#	define Ogre_OutputCString(str) ::OutputDebugStringA(str)
#	define Ogre_OutputWString(str) ::OutputDebugStringW(str)
#else
#	define Ogre_OutputCString(str) std::err << str
#	define Ogre_OutputWString(str) std::err << str
#endif

#if OGRE_MEMORY_TRACKER
	//--------------------------------------------------------------------------
	MemoryTracker& MemoryTracker::get()
	{
		static MemoryTracker tracker;
		return tracker;
	}
	//--------------------------------------------------------------------------
	void MemoryTracker::_recordAlloc(void* ptr, size_t sz, unsigned int pool,
					  const char* file, size_t ln, const char* func)
	{
		OGRE_LOCK_AUTO_MUTEX

		assert(mAllocations.find(ptr) == mAllocations.end() && "Double allocation with same address - "
			"this probably means you have a mismatched allocation / deallocation style, "
			"check if you're are using OGRE_ALLOC_T / OGRE_FREE and OGRE_NEW_T / OGRE_DELETE_T consistently");

		mAllocations[ptr] = Alloc(sz, pool, file, ln, func);
		if(pool >= mAllocationsByPool.size())
			mAllocationsByPool.resize(pool+1, 0);
		mAllocationsByPool[pool] += sz;
		mTotalAllocations += sz;
	}
	//--------------------------------------------------------------------------
	void MemoryTracker::_recordDealloc(void* ptr)
	{
		// deal cleanly with null pointers
		if (!ptr)
			return;

		OGRE_LOCK_AUTO_MUTEX

		AllocationMap::iterator i = mAllocations.find(ptr);
		assert(i != mAllocations.end() && "Unable to locate allocation unit - "
			"this probably means you have a mismatched allocation / deallocation style, "
			"check if you're are using OGRE_ALLOC_T / OGRE_FREE and OGRE_NEW_T / OGRE_DELETE_T consistently");
		// update category stats
		mAllocationsByPool[i->second.pool] -= i->second.bytes;
		// global stats
		mTotalAllocations -= i->second.bytes;
		mAllocations.erase(i);
	}
	//--------------------------------------------------------------------------
	size_t MemoryTracker::getTotalMemoryAllocated() const
	{
		return mTotalAllocations;
	}
	//--------------------------------------------------------------------------
	size_t MemoryTracker::getMemoryAllocatedForPool(unsigned int pool) const
	{
		return mAllocationsByPool[pool];
	}
	//--------------------------------------------------------------------------
	void MemoryTracker::reportLeaks()
	{
		StringUtil::StrStreamType os;

		if (mAllocations.empty())
		{
			os << "Ogre Memory: No memory leaks" << std::endl;
		}
		else
		{
			os << "Ogre Memory: Detected memory leaks !!! " << std::endl;
			os << "Ogre Memory: (" << mAllocations.size() << ") Allocation(s) with total " << mTotalAllocations << " bytes." << std::endl;
			os << "Ogre Memory: Dumping allocations -> " << std::endl;

			for (AllocationMap::const_iterator i = mAllocations.begin(); i != mAllocations.end(); ++i)
			{
				const Alloc& alloc = i->second;
				if (!alloc.filename.empty())
					os << alloc.filename;
				else
					os << "(unknown source):";

				os << "(" << alloc.line << ") : {" << alloc.bytes << " bytes}" << " function: " << alloc.function << std::endl;

			}
			os << std::endl;
		}

		if (mDumpToStdOut)
			std::cout << os.str();

		std::cout << os.str();
		std::ofstream of;
		of.open(mLeakFileName.c_str());
		of << os.str();
		of.close();

		Ogre_OutputCString(os.str().c_str());
	}
#endif // OGRE_DEBUG_MODE

}

<commit_msg>Fix use of memory tracker on non-Windows platforms (std::cerr, not std::err)<commit_after>/*
-----------------------------------------------------------------------------
This source file is part of OGRE
(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/

Copyright (c) 2000-2009 Torus Knot Software Ltd

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/
#include "OgrePlatform.h"
#include "OgreStableHeaders.h"
#include "OgrePrerequisites.h"
#include "OgreMemoryTracker.h"
#include "OgreString.h"

namespace Ogre
{

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#	define Ogre_OutputCString(str) ::OutputDebugStringA(str)
#	define Ogre_OutputWString(str) ::OutputDebugStringW(str)
#else
#	define Ogre_OutputCString(str) std::cerr << str
#	define Ogre_OutputWString(str) std::cerr << str
#endif

#if OGRE_MEMORY_TRACKER
	//--------------------------------------------------------------------------
	MemoryTracker& MemoryTracker::get()
	{
		static MemoryTracker tracker;
		return tracker;
	}
	//--------------------------------------------------------------------------
	void MemoryTracker::_recordAlloc(void* ptr, size_t sz, unsigned int pool,
					  const char* file, size_t ln, const char* func)
	{
		OGRE_LOCK_AUTO_MUTEX

		assert(mAllocations.find(ptr) == mAllocations.end() && "Double allocation with same address - "
			"this probably means you have a mismatched allocation / deallocation style, "
			"check if you're are using OGRE_ALLOC_T / OGRE_FREE and OGRE_NEW_T / OGRE_DELETE_T consistently");

		mAllocations[ptr] = Alloc(sz, pool, file, ln, func);
		if(pool >= mAllocationsByPool.size())
			mAllocationsByPool.resize(pool+1, 0);
		mAllocationsByPool[pool] += sz;
		mTotalAllocations += sz;
	}
	//--------------------------------------------------------------------------
	void MemoryTracker::_recordDealloc(void* ptr)
	{
		// deal cleanly with null pointers
		if (!ptr)
			return;

		OGRE_LOCK_AUTO_MUTEX

		AllocationMap::iterator i = mAllocations.find(ptr);
		assert(i != mAllocations.end() && "Unable to locate allocation unit - "
			"this probably means you have a mismatched allocation / deallocation style, "
			"check if you're are using OGRE_ALLOC_T / OGRE_FREE and OGRE_NEW_T / OGRE_DELETE_T consistently");
		// update category stats
		mAllocationsByPool[i->second.pool] -= i->second.bytes;
		// global stats
		mTotalAllocations -= i->second.bytes;
		mAllocations.erase(i);
	}
	//--------------------------------------------------------------------------
	size_t MemoryTracker::getTotalMemoryAllocated() const
	{
		return mTotalAllocations;
	}
	//--------------------------------------------------------------------------
	size_t MemoryTracker::getMemoryAllocatedForPool(unsigned int pool) const
	{
		return mAllocationsByPool[pool];
	}
	//--------------------------------------------------------------------------
	void MemoryTracker::reportLeaks()
	{
		StringUtil::StrStreamType os;

		if (mAllocations.empty())
		{
			os << "Ogre Memory: No memory leaks" << std::endl;
		}
		else
		{
			os << "Ogre Memory: Detected memory leaks !!! " << std::endl;
			os << "Ogre Memory: (" << mAllocations.size() << ") Allocation(s) with total " << mTotalAllocations << " bytes." << std::endl;
			os << "Ogre Memory: Dumping allocations -> " << std::endl;

			for (AllocationMap::const_iterator i = mAllocations.begin(); i != mAllocations.end(); ++i)
			{
				const Alloc& alloc = i->second;
				if (!alloc.filename.empty())
					os << alloc.filename;
				else
					os << "(unknown source):";

				os << "(" << alloc.line << ") : {" << alloc.bytes << " bytes}" << " function: " << alloc.function << std::endl;

			}
			os << std::endl;
		}

		if (mDumpToStdOut)
			std::cout << os.str();

		std::cout << os.str();
		std::ofstream of;
		of.open(mLeakFileName.c_str());
		of << os.str();
		of.close();

		Ogre_OutputCString(os.str().c_str());
	}
#endif // OGRE_DEBUG_MODE

}

<|endoftext|>


<commit_before>// Copyright 2014 Alessio Sclocco <a.sclocco@vu.nl>
//
// 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 <string>

#include <utils.hpp>
#include <Observation.hpp>

#ifndef BEAM_FORMER_HPP
#define BEAM_FORMER_HPP

namespace RadioAstronomy {

// Sequential beam forming algorithm
template< typename T > void beamFormer(const AstroData::Observation & observation, std::vector< T > & samples, std::vector< T > & output, std::vector< float > & weights);
// OpenCL beam forming algorithm
std::string * getBeamFormerOpenCL(const bool local, const unsigned int nrSamplesPerBlock, const unsigned int nrBeamsPerBlock, const unsigned int nrSamplesPerThread, const unsigned int nrBeamsPerThread, const std::string & dataType, const AstroData::Observation & observation);

// Implementations
template< typename T > void beamFormer(const AstroData::Observation & observation, std::vector< T > & samples, std::vector< T > & output, std::vector< float > & weights) {
  for ( unsigned int channel = 0; channel < observation.getNrChannels(); channel++ ) {
    for ( unsigned int sample = 0; sample < observation.getNrSamplesPerSecond(); sample++ ) {
      for ( unsigned int beam = 0; beam < observation.getNrBeams(); beam++ ) {
        T beamP0_r = 0;
        T beamP0_i = 0;
        T beamP1_r = 0;
        T beamP1_i = 0;

        for ( unsigned int station = 0; station < observation.getNrStations(); station++ ) {
          T * samplePointer = &(samples.data()[(channel * observation.getNrStations() * observation.getNrSamplesPerPaddedSecond() * 4) + (station * observation.getNrSamplesPerPaddedSecond() * 4) + (sample * 4)]);
          float * weightPointer = &(weights.data()[(channel * observation.getNrStations() * observation.getNrPaddedBeams() * 2) + (station * observation.getNrPaddedBeams() * 2) + (beam * 2)]);

          beamP0_r += (samplePointer[0] * weightPointer[0]) - (samplePointer[1] * weightPointer[1]);
          beamP0_i += (samplePointer[0] * weightPointer[1]) + (samplePointer[1] * weightPointer[0]);
          beamP1_r += (samplePointer[2] * weightPointer[0]) - (samplePointer[3] * weightPointer[1]);
          beamP1_i += (samplePointer[2] * weightPointer[1]) + (samplePointer[3] * weightPointer[0]);
        }
        beamP0_r /= observation.getNrStations();
        beamP0_i /= observation.getNrStations();
        beamP1_r /= observation.getNrStations();
        beamP1_i /= observation.getNrStations();
        output[(beam * observation.getNrChannels() * observation.getNrSamplesPerPaddedSecond() * 4) + (channel * observation.getNrSamplesPerPaddedSecond() * 4) + (sample * 4)] = beamP0_r;
        output[(beam * observation.getNrChannels() * observation.getNrSamplesPerPaddedSecond() * 4) + (channel * observation.getNrSamplesPerPaddedSecond() * 4) + (sample * 4) + 1] = beamP0_i;
        output[(beam * observation.getNrChannels() * observation.getNrSamplesPerPaddedSecond() * 4) + (channel * observation.getNrSamplesPerPaddedSecond() * 4) + (sample * 4) + 2] = beamP1_r;
        output[(beam * observation.getNrChannels() * observation.getNrSamplesPerPaddedSecond() * 4) + (channel * observation.getNrSamplesPerPaddedSecond() * 4) + (sample * 4) + 3] = beamP1_i;
      }
    }
  }
}

std::string * getBeamFormerOpenCL(const bool local, const unsigned int nrSamplesPerBlock, const unsigned int nrBeamsPerBlock, const unsigned int nrSamplesPerThread, const unsigned int nrBeamsPerThread, const std::string & dataType, const AstroData::Observation & observation) {
  std::string * code = new std::string();

  // Begin kernel's template
  *code = "__kernel void beamFormer(__global const " + dataType + "4 * restrict const samples, __global " + dataType + "4 * restrict const output, __global const float2 * restrict const weights) {\n"
    "const unsigned int channel = get_group_id(2);\n"
    "const unsigned int beam = (get_group_id(1) * " + isa::utils::toString(nrBeamsPerBlock * nrBeamsPerThread) + ") + (get_local_id(1) * " + isa::utils::toString(nrBeamsPerThread) + ");\n"
    "<%DEF_SAMPLES%>"
    "<%DEF_SUMS%>"
    + dataType + "4 sample = (" + dataType + "4)(0);\n";
  if ( local ) {
    *code += "__local float2 localWeights[" + isa::utils::toString(nrBeamsPerBlock * nrBeamsPerThread) + "];\n"
      "__local float localSamples[" + isa::utils::toString(isa::utils::pad(nrSamplesPerBlock * nrSamplesPerThread, observation.getPadding()) * 4) + "];\n";
  }
  *code += "float2 weight = (float2)(0);\n"
    "\n"
    "for ( unsigned int station = 0; station < " + isa::utils::toString(observation.getNrStations()) + "; station++ ) {\n";
  if ( local ) {
    *code += "unsigned int itemGlobal = (channel * " + isa::utils::toString(observation.getNrStations() * observation.getNrPaddedBeams()) + ") + (station * " + isa::utils::toString(observation.getNrPaddedBeams()) + ") + (get_group_id(1) * " + isa::utils::toString(nrBeamsPerBlock * nrBeamsPerThread) + ") + (get_local_id(1) * " + isa::utils::toString(nrSamplesPerBlock) + ") + get_local_id(0);\n"
      "unsigned int itemLocal = (get_local_id(1) * " + isa::utils::toString(nrSamplesPerBlock) + ") + get_local_id(0);\n"
      "while ( itemLocal < " + isa::utils::toString(nrBeamsPerBlock * nrBeamsPerThread) + ") {\n"
      "localWeights[itemLocal] = weights[itemGlobal];\n"
      "itemLocal += " + isa::utils::toString(nrSamplesPerBlock * nrBeamsPerBlock) + ";\n"
      "itemGlobal += " + isa::utils::toString(nrSamplesPerBlock * nrBeamsPerBlock) + ";\n"
      "}\n"
      "itemGlobal = (channel * " + isa::utils::toString(observation.getNrStations() * observation.getNrSamplesPerPaddedSecond()) + ") + (station * " + isa::utils::toString(observation.getNrSamplesPerPaddedSecond()) + ") + (get_group_id(0) * " + isa::utils::toString(nrSamplesPerBlock * nrSamplesPerThread) + ") + (get_local_id(1) * " + isa::utils::toString(nrSamplesPerBlock) + ") + get_local_id(0);\n"
      "itemLocal = (get_local_id(1) * " + isa::utils::toString(nrSamplesPerBlock) + ") + get_local_id(0);\n"
      "while ( itemLocal < " + isa::utils::toString(nrSamplesPerBlock * nrSamplesPerThread) + ") {\n"
      "sample = samples[itemGlobal];\n"
      "localSamples[itemLocal] = sample.x;\n"
      "localSamples[(" + isa::utils::toString(isa::utils::pad(nrSamplesPerBlock * nrSamplesPerThread, observation.getPadding())) + ") + itemLocal] = sample.y;\n"
      "localSamples[(" + isa::utils::toString(isa::utils::pad(nrSamplesPerBlock * nrSamplesPerThread, observation.getPadding()) * 2) + ") + itemLocal] = sample.z;\n"
      "localSamples[(" + isa::utils::toString(isa::utils::pad(nrSamplesPerBlock * nrSamplesPerThread, observation.getPadding()) * 3) + ") + itemLocal] = sample.w;\n"
      "itemLocal += " + isa::utils::toString(nrSamplesPerBlock * nrBeamsPerBlock) + ";\n"
      "itemGlobal += " + isa::utils::toString(nrSamplesPerBlock * nrBeamsPerBlock) + ";\n"
      "}\n"
      "barrier(CLK_LOCAL_MEM_FENCE);\n";
  }
  *code += "<%LOAD_COMPUTE%>"
    "}\n"
    "<%AVERAGE%>"
    "<%STORE%>"
    "}\n";
  std::string defSamplesTemplate = "const unsigned int sample<%SNUM%> = (get_group_id(0) * " + isa::utils::toString(nrSamplesPerBlock * nrSamplesPerThread) + ") + get_local_id(0) + <%OFFSET%>;\n";
  std::string defSumsTemplate = dataType + "4 beam<%BNUM%>s<%SNUM%> = (" + dataType + "4)(0);\n";
  std::string loadComputeTemplate;
  if ( local ) {
   loadComputeTemplate += "sample.x = localSamples[get_local_id(0) + <%OFFSET%>];\n"
     "sample.y = localSamples[(" + isa::utils::toString(isa::utils::pad(nrSamplesPerBlock * nrSamplesPerThread, observation.getPadding())) + ") + get_local_id(0) + <%OFFSET%>];\n"
     "sample.z = localSamples[(" + isa::utils::toString(isa::utils::pad(nrSamplesPerBlock * nrSamplesPerThread, observation.getPadding()) * 2) + ") + get_local_id(0) + <%OFFSET%>];\n"
     "sample.w = localSamples[(" + isa::utils::toString(isa::utils::pad(nrSamplesPerBlock * nrSamplesPerThread, observation.getPadding()) * 3) + ") + get_local_id(0) + <%OFFSET%>];\n";
  } else {
   loadComputeTemplate += "sample = samples[(channel * " + isa::utils::toString(observation.getNrStations() * observation.getNrSamplesPerPaddedSecond()) + ") + (station * " + isa::utils::toString(observation.getNrSamplesPerPaddedSecond()) + ") + sample<%SNUM%>];\n";
  }
  loadComputeTemplate += "<%SUMS%>";
  std::string sumsTemplate;
  if ( local ) {
    sumsTemplate += "weight = localWeights[(get_local_id(1) * " + isa::utils::toString(nrBeamsPerThread) + ") + <%BNUM%>];\n";
  } else {
    sumsTemplate += "weight = weights[(channel * " + isa::utils::toString(observation.getNrStations() * observation.getNrPaddedBeams()) + ") + (station * " + isa::utils::toString(observation.getNrPaddedBeams()) + ") + beam + <%BNUM%>];\n";
  }
  sumsTemplate += "beam<%BNUM%>s<%SNUM%>.x += (sample.x * weight.x) - (sample.y * weight.y);\n"
    "beam<%BNUM%>s<%SNUM%>.y += (sample.x * weight.y) + (sample.y * weight.x);\n"
    "beam<%BNUM%>s<%SNUM%>.z += (sample.z * weight.x) - (sample.w * weight.y);\n"
    "beam<%BNUM%>s<%SNUM%>.w += (sample.z * weight.y) + (sample.w * weight.x);\n";
  std::string averageTemplate = "beam<%BNUM%>s<%SNUM%> *= " + isa::utils::toString(1.0f / observation.getNrStations()) + "f;\n";
  std::string storeTemplate = "output[((beam + <%BNUM%>) * " + isa::utils::toString(observation.getNrChannels() * observation.getNrSamplesPerPaddedSecond()) + ") + (channel * " + isa::utils::toString(observation.getNrSamplesPerPaddedSecond()) + ") + sample<%SNUM%>] = beam<%BNUM%>s<%SNUM%>;\n";
  // End kernel's template

  std::string * defSamples_s = new std::string();
  std::string * defSums_s = new std::string();
  std::string * loadCompute_s = new std::string();
  std::string * average_s = new std::string();
  std::string * store_s = new std::string();

  for ( unsigned int sample = 0; sample < nrSamplesPerThread; sample++ ) {
    std::string sample_s = isa::utils::toString(sample);
    std::string offset_s = isa::utils::toString(sample * nrSamplesPerBlock);
    std::string * sums_s = new std::string();
    std::string * temp_s = 0;

    temp_s = isa::utils::replace(&defSamplesTemplate, "<%SNUM%>", sample_s);
    temp_s = isa::utils::replace(temp_s, "<%OFFSET%>", offset_s, true);
    defSamples_s->append(*temp_s);
    delete temp_s;

    for ( unsigned int beam = 0; beam < nrBeamsPerThread; beam++ ) {
      std::string beam_s = isa::utils::toString(beam);
      std::string * temp_s = 0;

      temp_s = isa::utils::replace(&defSumsTemplate, "<%BNUM%>", beam_s);
      defSums_s->append(*temp_s);
      delete temp_s;
      temp_s = isa::utils::replace(&sumsTemplate, "<%BNUM%>", beam_s);
      sums_s->append(*temp_s);
      delete temp_s;
      temp_s = isa::utils::replace(&averageTemplate, "<%BNUM%>", beam_s);
      average_s->append(*temp_s);
      delete temp_s;
      temp_s = isa::utils::replace(&storeTemplate, "<%BNUM%>", beam_s);
      store_s->append(*temp_s);
      delete temp_s;
    }
    defSums_s = isa::utils::replace(defSums_s, "<%SNUM%>", sample_s, true);
    temp_s = isa::utils::replace(&loadComputeTemplate, "<%SNUM%>", sample_s);
    temp_s = isa::utils::replace(temp_s, "<%OFFSET%>", offset_s, true);
    temp_s = isa::utils::replace(temp_s, "<%SUMS%>", *sums_s, true);
    temp_s = isa::utils::replace(temp_s, "<%SNUM%>", sample_s, true);
    loadCompute_s->append(*temp_s);
    delete temp_s;
    average_s = isa::utils::replace(average_s, "<%SNUM%>", sample_s, true);
    store_s = isa::utils::replace(store_s, "<%SNUM%>", sample_s, true);
  }

  code = isa::utils::replace(code, "<%DEF_SAMPLES%>", *defSamples_s, true);
  code = isa::utils::replace(code, "<%DEF_SUMS%>", *defSums_s, true);
  code = isa::utils::replace(code, "<%LOAD_COMPUTE%>", *loadCompute_s, true);
  code = isa::utils::replace(code, "<%AVERAGE%>", *average_s, true);
  code = isa::utils::replace(code, "<%STORE%>", *store_s, true);

  return code;
}

} // RadioAstronomy

#endif // BEAM_FORMER_HPP

<commit_msg>Using constant instead of local memory for the weights.<commit_after>// Copyright 2014 Alessio Sclocco <a.sclocco@vu.nl>
//
// 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 <string>

#include <utils.hpp>
#include <Observation.hpp>

#ifndef BEAM_FORMER_HPP
#define BEAM_FORMER_HPP

namespace RadioAstronomy {

// Sequential beam forming algorithm
template< typename T > void beamFormer(const AstroData::Observation & observation, std::vector< T > & samples, std::vector< T > & output, std::vector< float > & weights);
// OpenCL beam forming algorithm
std::string * getBeamFormerOpenCL(const bool local, const unsigned int nrSamplesPerBlock, const unsigned int nrBeamsPerBlock, const unsigned int nrSamplesPerThread, const unsigned int nrBeamsPerThread, const std::string & dataType, const AstroData::Observation & observation);

// Implementations
template< typename T > void beamFormer(const AstroData::Observation & observation, std::vector< T > & samples, std::vector< T > & output, std::vector< float > & weights) {
  for ( unsigned int channel = 0; channel < observation.getNrChannels(); channel++ ) {
    for ( unsigned int sample = 0; sample < observation.getNrSamplesPerSecond(); sample++ ) {
      for ( unsigned int beam = 0; beam < observation.getNrBeams(); beam++ ) {
        T beamP0_r = 0;
        T beamP0_i = 0;
        T beamP1_r = 0;
        T beamP1_i = 0;

        for ( unsigned int station = 0; station < observation.getNrStations(); station++ ) {
          T * samplePointer = &(samples.data()[(channel * observation.getNrStations() * observation.getNrSamplesPerPaddedSecond() * 4) + (station * observation.getNrSamplesPerPaddedSecond() * 4) + (sample * 4)]);
          float * weightPointer = &(weights.data()[(channel * observation.getNrStations() * observation.getNrPaddedBeams() * 2) + (station * observation.getNrPaddedBeams() * 2) + (beam * 2)]);

          beamP0_r += (samplePointer[0] * weightPointer[0]) - (samplePointer[1] * weightPointer[1]);
          beamP0_i += (samplePointer[0] * weightPointer[1]) + (samplePointer[1] * weightPointer[0]);
          beamP1_r += (samplePointer[2] * weightPointer[0]) - (samplePointer[3] * weightPointer[1]);
          beamP1_i += (samplePointer[2] * weightPointer[1]) + (samplePointer[3] * weightPointer[0]);
        }
        beamP0_r /= observation.getNrStations();
        beamP0_i /= observation.getNrStations();
        beamP1_r /= observation.getNrStations();
        beamP1_i /= observation.getNrStations();
        output[(beam * observation.getNrChannels() * observation.getNrSamplesPerPaddedSecond() * 4) + (channel * observation.getNrSamplesPerPaddedSecond() * 4) + (sample * 4)] = beamP0_r;
        output[(beam * observation.getNrChannels() * observation.getNrSamplesPerPaddedSecond() * 4) + (channel * observation.getNrSamplesPerPaddedSecond() * 4) + (sample * 4) + 1] = beamP0_i;
        output[(beam * observation.getNrChannels() * observation.getNrSamplesPerPaddedSecond() * 4) + (channel * observation.getNrSamplesPerPaddedSecond() * 4) + (sample * 4) + 2] = beamP1_r;
        output[(beam * observation.getNrChannels() * observation.getNrSamplesPerPaddedSecond() * 4) + (channel * observation.getNrSamplesPerPaddedSecond() * 4) + (sample * 4) + 3] = beamP1_i;
      }
    }
  }
}

std::string * getBeamFormerOpenCL(const bool local, const unsigned int nrSamplesPerBlock, const unsigned int nrBeamsPerBlock, const unsigned int nrSamplesPerThread, const unsigned int nrBeamsPerThread, const std::string & dataType, const AstroData::Observation & observation) {
  std::string * code = new std::string();

  // Begin kernel's template
  *code = "__kernel void beamFormer(__global const " + dataType + "4 * restrict const samples, __global " + dataType + "4 * restrict const output, __constant const float2 * restrict const weights) {\n"
    "const unsigned int channel = get_group_id(2);\n"
    "const unsigned int beam = (get_group_id(1) * " + isa::utils::toString(nrBeamsPerBlock * nrBeamsPerThread) + ") + (get_local_id(1) * " + isa::utils::toString(nrBeamsPerThread) + ");\n"
    "<%DEF_SAMPLES%>"
    "<%DEF_SUMS%>"
    + dataType + "4 sample = (" + dataType + "4)(0);\n";
  if ( local ) {
    *code += "__local float localSamples[" + isa::utils::toString(isa::utils::pad(nrSamplesPerBlock * nrSamplesPerThread, observation.getPadding()) * 4) + "];\n";
  }
  *code += "float2 weight = (float2)(0);\n"
    "\n"
    "for ( unsigned int station = 0; station < " + isa::utils::toString(observation.getNrStations()) + "; station++ ) {\n";
  if ( local ) {
    *code += "itemGlobal = (channel * " + isa::utils::toString(observation.getNrStations() * observation.getNrSamplesPerPaddedSecond()) + ") + (station * " + isa::utils::toString(observation.getNrSamplesPerPaddedSecond()) + ") + (get_group_id(0) * " + isa::utils::toString(nrSamplesPerBlock * nrSamplesPerThread) + ") + (get_local_id(1) * " + isa::utils::toString(nrSamplesPerBlock) + ") + get_local_id(0);\n"
      "itemLocal = (get_local_id(1) * " + isa::utils::toString(nrSamplesPerBlock) + ") + get_local_id(0);\n"
      "while ( itemLocal < " + isa::utils::toString(nrSamplesPerBlock * nrSamplesPerThread) + ") {\n"
      "sample = samples[itemGlobal];\n"
      "localSamples[itemLocal] = sample.x;\n"
      "localSamples[(" + isa::utils::toString(isa::utils::pad(nrSamplesPerBlock * nrSamplesPerThread, observation.getPadding())) + ") + itemLocal] = sample.y;\n"
      "localSamples[(" + isa::utils::toString(isa::utils::pad(nrSamplesPerBlock * nrSamplesPerThread, observation.getPadding()) * 2) + ") + itemLocal] = sample.z;\n"
      "localSamples[(" + isa::utils::toString(isa::utils::pad(nrSamplesPerBlock * nrSamplesPerThread, observation.getPadding()) * 3) + ") + itemLocal] = sample.w;\n"
      "itemLocal += " + isa::utils::toString(nrSamplesPerBlock * nrBeamsPerBlock) + ";\n"
      "itemGlobal += " + isa::utils::toString(nrSamplesPerBlock * nrBeamsPerBlock) + ";\n"
      "}\n"
      "barrier(CLK_LOCAL_MEM_FENCE);\n";
  }
  *code += "<%LOAD_COMPUTE%>"
    "}\n"
    "<%AVERAGE%>"
    "<%STORE%>"
    "}\n";
  std::string defSamplesTemplate = "const unsigned int sample<%SNUM%> = (get_group_id(0) * " + isa::utils::toString(nrSamplesPerBlock * nrSamplesPerThread) + ") + get_local_id(0) + <%OFFSET%>;\n";
  std::string defSumsTemplate = dataType + "4 beam<%BNUM%>s<%SNUM%> = (" + dataType + "4)(0);\n";
  std::string loadComputeTemplate;
  if ( local ) {
   loadComputeTemplate += "sample.x = localSamples[get_local_id(0) + <%OFFSET%>];\n"
     "sample.y = localSamples[(" + isa::utils::toString(isa::utils::pad(nrSamplesPerBlock * nrSamplesPerThread, observation.getPadding())) + ") + get_local_id(0) + <%OFFSET%>];\n"
     "sample.z = localSamples[(" + isa::utils::toString(isa::utils::pad(nrSamplesPerBlock * nrSamplesPerThread, observation.getPadding()) * 2) + ") + get_local_id(0) + <%OFFSET%>];\n"
     "sample.w = localSamples[(" + isa::utils::toString(isa::utils::pad(nrSamplesPerBlock * nrSamplesPerThread, observation.getPadding()) * 3) + ") + get_local_id(0) + <%OFFSET%>];\n";
  } else {
   loadComputeTemplate += "sample = samples[(channel * " + isa::utils::toString(observation.getNrStations() * observation.getNrSamplesPerPaddedSecond()) + ") + (station * " + isa::utils::toString(observation.getNrSamplesPerPaddedSecond()) + ") + sample<%SNUM%>];\n";
  }
  loadComputeTemplate += "<%SUMS%>";
  std::string sumsTemplate = "weight = weights[(channel * " + isa::utils::toString(observation.getNrStations() * observation.getNrPaddedBeams()) + ") + (station * " + isa::utils::toString(observation.getNrPaddedBeams()) + ") + beam + <%BNUM%>];\n"
    "beam<%BNUM%>s<%SNUM%>.x += (sample.x * weight.x) - (sample.y * weight.y);\n"
    "beam<%BNUM%>s<%SNUM%>.y += (sample.x * weight.y) + (sample.y * weight.x);\n"
    "beam<%BNUM%>s<%SNUM%>.z += (sample.z * weight.x) - (sample.w * weight.y);\n"
    "beam<%BNUM%>s<%SNUM%>.w += (sample.z * weight.y) + (sample.w * weight.x);\n";
  std::string averageTemplate = "beam<%BNUM%>s<%SNUM%> *= " + isa::utils::toString(1.0f / observation.getNrStations()) + "f;\n";
  std::string storeTemplate = "output[((beam + <%BNUM%>) * " + isa::utils::toString(observation.getNrChannels() * observation.getNrSamplesPerPaddedSecond()) + ") + (channel * " + isa::utils::toString(observation.getNrSamplesPerPaddedSecond()) + ") + sample<%SNUM%>] = beam<%BNUM%>s<%SNUM%>;\n";
  // End kernel's template

  std::string * defSamples_s = new std::string();
  std::string * defSums_s = new std::string();
  std::string * loadCompute_s = new std::string();
  std::string * average_s = new std::string();
  std::string * store_s = new std::string();

  for ( unsigned int sample = 0; sample < nrSamplesPerThread; sample++ ) {
    std::string sample_s = isa::utils::toString(sample);
    std::string offset_s = isa::utils::toString(sample * nrSamplesPerBlock);
    std::string * sums_s = new std::string();
    std::string * temp_s = 0;

    temp_s = isa::utils::replace(&defSamplesTemplate, "<%SNUM%>", sample_s);
    temp_s = isa::utils::replace(temp_s, "<%OFFSET%>", offset_s, true);
    defSamples_s->append(*temp_s);
    delete temp_s;

    for ( unsigned int beam = 0; beam < nrBeamsPerThread; beam++ ) {
      std::string beam_s = isa::utils::toString(beam);
      std::string * temp_s = 0;

      temp_s = isa::utils::replace(&defSumsTemplate, "<%BNUM%>", beam_s);
      defSums_s->append(*temp_s);
      delete temp_s;
      temp_s = isa::utils::replace(&sumsTemplate, "<%BNUM%>", beam_s);
      sums_s->append(*temp_s);
      delete temp_s;
      temp_s = isa::utils::replace(&averageTemplate, "<%BNUM%>", beam_s);
      average_s->append(*temp_s);
      delete temp_s;
      temp_s = isa::utils::replace(&storeTemplate, "<%BNUM%>", beam_s);
      store_s->append(*temp_s);
      delete temp_s;
    }
    defSums_s = isa::utils::replace(defSums_s, "<%SNUM%>", sample_s, true);
    temp_s = isa::utils::replace(&loadComputeTemplate, "<%SNUM%>", sample_s);
    temp_s = isa::utils::replace(temp_s, "<%OFFSET%>", offset_s, true);
    temp_s = isa::utils::replace(temp_s, "<%SUMS%>", *sums_s, true);
    temp_s = isa::utils::replace(temp_s, "<%SNUM%>", sample_s, true);
    loadCompute_s->append(*temp_s);
    delete temp_s;
    average_s = isa::utils::replace(average_s, "<%SNUM%>", sample_s, true);
    store_s = isa::utils::replace(store_s, "<%SNUM%>", sample_s, true);
  }

  code = isa::utils::replace(code, "<%DEF_SAMPLES%>", *defSamples_s, true);
  code = isa::utils::replace(code, "<%DEF_SUMS%>", *defSums_s, true);
  code = isa::utils::replace(code, "<%LOAD_COMPUTE%>", *loadCompute_s, true);
  code = isa::utils::replace(code, "<%AVERAGE%>", *average_s, true);
  code = isa::utils::replace(code, "<%STORE%>", *store_s, true);

  return code;
}

} // RadioAstronomy

#endif // BEAM_FORMER_HPP

<|endoftext|>


<commit_before>#ifndef K3_RUNTIME_BUILTINS_H
#define K3_RUNTIME_BUILTINS_H

#ifdef CACHEPROFILE
#include <cpucounters.h>
#endif

#ifdef MEMPROFILE
#include "gperftools/heap-profiler.h"
#endif

#ifdef JEMALLOC
#include "jemalloc/jemalloc.h"
#endif

#include <ctime>
#include <chrono>
#include <climits>
#include <fstream>
#include <sstream>
#include <string>
#include <climits>
#include <functional>

#include "re2/re2.h"

#include "BaseTypes.hpp"
#include "BaseString.hpp"
#include "Common.hpp"
#include "dataspace/Dataspace.hpp"

// Hashing:
namespace boost {

template<>
struct hash<boost::asio::ip::address> {
  size_t operator()(boost::asio::ip::address const& v) const {
    if (v.is_v4()) {
      return v.to_v4().to_ulong();
    }
    if (v.is_v6()) {
      auto const& range = v.to_v6().to_bytes();
      return hash_range(range.begin(), range.end());
    }
    if (v.is_unspecified()) {
      return 0x4751301174351161ul;
    }
    return hash_value(v.to_string());
  }
};

} // boost

namespace K3 {

  template <class C1, class C, class F>
  void read_records(C1& paths, C& container, F read_record) {

    for (auto rec : paths) {
      std::ifstream in;
      in.open(rec.path);
      std::string tmp_buffer;
      while (!in.eof()) {
        container.insert(read_record(in, tmp_buffer));
        in >> std::ws;
      }
    }

    return;
  }

  class __pcm_context {
    #ifdef CACHEPROFILE
    protected:
      PCM *instance;
      std::shared_ptr<SystemCounterState> initial_state;
    #endif

    public:
      __pcm_context();
      ~__pcm_context();
      unit_t cacheProfilerStart(unit_t);
      unit_t cacheProfilerStop(unit_t);
  };

  class __tcmalloc_context {
    public:
      unit_t heapProfilerStart(const string_impl&);
      unit_t heapProfilerStop(unit_t);
  };

  class __jemalloc_context {
    public:
      unit_t jemallocStart(unit_t);
      unit_t jemallocStop(unit_t);
  };
  template <class C1, class C, class F>
  void read_records_with_resize(int size, C1& paths, C& container, F read_record) {

    if (size == 0) {
      return read_records(paths, container, read_record);
    }
    else {
      container.getContainer().resize(size);
    }

    int i = 0;
    for (auto rec : paths) {
      std::ifstream in;
      in.open(rec.path);

      std::string tmp_buffer;
      while (!in.eof()) {
        if (i >= container.size(unit_t {}) ) {
          throw std::runtime_error("Cannot read records, container size is too small");
        }
        container.getContainer()[i++] = read_record(in, tmp_buffer);
        in >> std::ws;
      }
    }

    return;
  }

  // Standard context for common builtins that use a handle to the engine (via inheritance)
  class __standard_context : public __k3_context {
    public:
    __standard_context(Engine&);

    unit_t openBuiltin(string_impl ch_id, string_impl builtin_ch_id, string_impl fmt);
    unit_t openFile(string_impl ch_id, string_impl path, string_impl fmt, string_impl mode);
    unit_t openSocket(string_impl ch_id, Address a, string_impl fmt, string_impl mode);

    bool hasRead(string_impl ch_id);
    template<typename T> T doRead(string_impl ch_id);
    template<typename T> Collection<R_elem<T>> doReadBlock(string_impl ch_id, int block_size);

    bool hasWrite(string_impl ch_id);
    template<typename T> unit_t doWrite(string_impl ch_id, T& val);

    unit_t close(string_impl chan_id);

    int random(int n);

    double randomFraction(unit_t);

    template <class T>
    int hash(const T& x) {
      // We implement hash_value for all of our types.
      // for ordered containers, so we may as well delegate to that.
      return static_cast<int>(hash_value(x));
    }

    template <class T>
    string_impl toJson(const T& in) {
      return K3::serialization::json::encode<T>(in);
    }

    template <class T>
    T range(int i) {
      T result;
      for (int j = 0; j < i; j++) {
        result.insert(j);
      }
      return result;
    }

    int truncate(double n) { return (int)n; }

    double real_of_int(int n) { return (double)n; }

    int get_max_int(unit_t) { return INT_MAX; }

    unit_t print(string_impl message);

    // TODO, implement, sharing code with prettify()
    template <class T>
    string_impl show(T t) {
      return string_impl("TODO: implement show()");
    }

    template <class T>
    T error(unit_t) {
      throw std::runtime_error("Error. Terminating");
      return T();
    }

    template <class T>
    unit_t ignore(T t) {
      return unit_t();
    }

    // TODO add a member to base_string, call that instead
    int strcomp(const string_impl& s1,const string_impl& s2) {
      const char* c1 = s1.c_str();
      const char* c2 = s2.c_str();
      if (c1 && c2) {
        return strcmp(c1,c2);
      }
      else if(c1) {
        return 1;
      }
      else if(c2) {
        return -1;
      }
      else {
        return 0;
      }

    }

    unit_t haltEngine(unit_t);

    unit_t drainEngine(unit_t);

    unit_t sleep(int n);

    template <template <class> class M, template <class> class C,
              template <typename...> class R>
    unit_t loadGraph(string_impl filepath, M<R<int, C<R_elem<int>>>>& c) {
      std::string tmp_buffer;
      std::ifstream in(filepath);

      int source;
      std::size_t position;
      while (!in.eof()) {
        C<R_elem<int>> edge_list;

        std::size_t start = 0;
        std::size_t end = start;
        std::getline(in, tmp_buffer);

        end = tmp_buffer.find(",", start);
        source = std::atoi(tmp_buffer.substr(start, end - start).c_str());

        start = end + 1;

        while (end != std::string::npos) {
          end = tmp_buffer.find(",", start);
          edge_list.insert(R_elem<int>(
              std::atoi(tmp_buffer.substr(start, end - start).c_str())));
          start = end + 1;
        }

        c.insert(R<int, C<R_elem<int>>>{source, std::move(edge_list)});
        in >> std::ws;
      }

      return unit_t{};
    }

    // TODO move to seperate context
    template <class C1>
    unit_t loadRKQ3(const C1& paths, K3::Map<R_key_value<string_impl, int>>& c)
    {
      for (auto r : paths) {
        // Buffers
        std::string tmp_buffer;
        R_key_value<string_impl, int> rec;
        // Infile
        std::ifstream in;
        in.open(r.path);

        // Parse by line
        while (!in.eof()) {
          std::getline(in, tmp_buffer, ',');
          rec.key = tmp_buffer;
          std::getline(in, tmp_buffer, ',');
          rec.value = std::atoi(tmp_buffer.c_str());
          // ignore last value
          std::getline(in, tmp_buffer);
          c.insert(rec);
        }
      }

      return unit_t{};
    }

   int lineCountFile(const string_impl& filepath) {
     std::cout << "LCF: " << filepath << std::endl;
     std::ifstream _in;
     _in.open(filepath);
     std::string tmp_buffer;
     std::getline(_in, tmp_buffer);
     std::cout << "LCF read: " << tmp_buffer << std::endl;
     return std::atoi(tmp_buffer.c_str());
   }

   template <class C1, template <class> class C, template <typename ...> class R>
   unit_t loadQ1(const C1& paths, C<R<int, string_impl>>& c) {
        K3::read_records(paths, c, [] (std::istream& in, std::string& tmp_buffer)   {
          R<int, string_impl> record;
          // Get pageURL
          std::getline(in, tmp_buffer, ',');
          record.pageURL = tmp_buffer;
          // Get pageRank
          std::getline(in, tmp_buffer, ',');
          record.pageRank = std::atoi(tmp_buffer.c_str());
          // Ignore avgDuration
          std::getline(in, tmp_buffer);
          //record.avgDuration = std::atoi(tmp_buffer.c_str());
          return record;
        });
        return unit_t {};
   }

   template <class C1, template<typename S> class C, template <typename ...> class R>
   unit_t loadQ2(const C1& paths, C<R<double, string_impl>>& c) {
        K3::read_records(paths, c, [] (std::istream& in, std::string& tmp_buffer)   {
          R<double, string_impl> record;
          // Get sourceIP
          std::getline(in, tmp_buffer, ',');
          record.sourceIP = tmp_buffer;

          // Ignore until adRevenue
          std::getline(in, tmp_buffer, ',');
          //record.destURL = tmp_buffer;
          std::getline(in, tmp_buffer, ',');
          //record.visitDate = tmp_buffer;

          // Get adRevenue
          std::getline(in, tmp_buffer, ',');
          record.adRevenue = std::atof(tmp_buffer.c_str());

          // Ignore the rest
          std::getline(in, tmp_buffer, ',');
          //record.userAgent = tmp_buffer;
          std::getline(in, tmp_buffer, ',');
          //record.countryCode = tmp_buffer;
          std::getline(in, tmp_buffer, ',');
          //record.languageCode = tmp_buffer;
          std::getline(in, tmp_buffer, ',');
          //record.searchWord = tmp_buffer;
          std::getline(in, tmp_buffer);
          //record.duration = std::atoi(tmp_buffer.c_str());
          return record;
        });
        return unit_t {};
   }

   template <class C1, template<typename S> class C, template <typename ...> class R>
   unit_t loadUVQ3(const C1& paths, C<R<double, string_impl, string_impl, string_impl>>& c) {
        K3::read_records(paths, c, [] (std::istream& in, std::string& tmp_buffer)   {
          R<double, string_impl, string_impl, string_impl> record;
          std::getline(in, tmp_buffer, ',');
          record.sourceIP = tmp_buffer;
          std::getline(in, tmp_buffer, ',');
          record.destURL = tmp_buffer;
          std::getline(in, tmp_buffer, ',');
          record.visitDate = tmp_buffer;
          std::getline(in, tmp_buffer, ',');
          record.adRevenue = std::atof(tmp_buffer.c_str());
          std::getline(in, tmp_buffer, ',');
          //record.userAgent = tmp_buffer;
          std::getline(in, tmp_buffer, ',');
          //record.countryCode = tmp_buffer;
          std::getline(in, tmp_buffer, ',');
          //record.languageCode = tmp_buffer;
          std::getline(in, tmp_buffer, ',');
          //record.searchWord = tmp_buffer;
          std::getline(in, tmp_buffer);
          //record.duration = std::atoi(tmp_buffer.c_str());
          return record;
        });
        return unit_t {};
   }

   template <class C, class F>
   unit_t logHelper(string_impl filepath, const C& c, F f, string_impl sep) {
     std::ofstream outfile;
     outfile.open(filepath);
     auto& container = c.getConstContainer();
     for (auto& elem : container) {
       f(outfile, elem, sep);
       outfile << "\n";
     }
     outfile.close();
     return unit_t();

   }
    Vector<R_elem<double>> zeroVector(int i);
    Vector<R_elem<double>> randomVector(int i);

    template <template <typename S> class C, class R>
    unit_t loadStrings(string_impl filepath, C<R>& c) {
      std::string line;
      std::ifstream infile(filepath);
      while (std::getline(infile, line)) {
        c.insert(R{line});
      }
      return unit_t{};
    }

    template <template<typename S> class C, class V>
    unit_t loadVector(string_impl filepath, C<R_elem<V>>& c) {
      std::string line;
      std::ifstream infile(filepath);
      char *saveptr;

      while (std::getline(infile, line)){
        char * pch;
        pch = strtok_r(&line[0],",", &saveptr);
        V v;
        while (pch) {
          R_elem<double> rec;
          rec.elem = std::atof(pch);
          v.insert(rec);
          pch = strtok_r(NULL,",", &saveptr);
        }
        R_elem<V> rec2 {v};
        c.insert(rec2);
      }
      return unit_t();
    }

    template <template <typename S> class C, template <typename...> class R, class V>
    unit_t loadVectorLabel(int dims, string_impl filepath, C<R<double, V>>& c) {
      // Buffers
      std::string tmp_buffer;
      R<double, V> rec;
      // Infile
      std::ifstream in;
      in.open(filepath);
      char* saveptr;

      // Parse by line
      while (!in.eof()) {
        V v;
        R_elem<double> r;
        for (int j = 0; j < dims; j++) {
          std::getline(in, tmp_buffer, ',');
          r.elem = std::atof(tmp_buffer.c_str());
          v.insert(r);
        }
        std::getline(in, tmp_buffer, ',');
        rec.class_label = std::atof(tmp_buffer.c_str());
        rec.elem = v;
        c.insert(rec);

        in >> std::ws;
      }

      return unit_t{};
    }
  };

  // Utilities:

  // Time:
  class __time_context {
    public:
    __time_context();
    int now_int(unit_t);
  };

  // String operations:

  class __string_context {
    public:
    shared_ptr<RE2> pattern;
    __string_context();

    string_impl itos(int i);

    string_impl rtos(double d);

    string_impl atos(Address a);

    F<Collection<R_elem<string_impl>>(const string_impl &)> regex_matcher(const string_impl&);
    Collection<R_elem<string_impl>> regex_matcher_q4(const string_impl&);

    template <class S> S slice_string(const S& s, int x, int y) {
      return s.substr(x, y);
    }

    // Split a string by substrings
    Seq<R_elem<string_impl>> splitString(string_impl, const string_impl&);
    string_impl takeUntil(const string_impl& s, const string_impl& splitter);
    int countChar(const string_impl& s, const string_impl& splitter);
    int tpch_date(const string_impl& s);
    string_impl tpch_date_to_string(const int& date);
  };

} // namespace K3

#endif /* K3_RUNTIME_BUILTINS_H */
<commit_msg>Fix graphLoader<commit_after>#ifndef K3_RUNTIME_BUILTINS_H
#define K3_RUNTIME_BUILTINS_H

#ifdef CACHEPROFILE
#include <cpucounters.h>
#endif

#ifdef MEMPROFILE
#include "gperftools/heap-profiler.h"
#endif

#ifdef JEMALLOC
#include "jemalloc/jemalloc.h"
#endif

#include <ctime>
#include <chrono>
#include <climits>
#include <fstream>
#include <sstream>
#include <string>
#include <climits>
#include <functional>

#include "re2/re2.h"

#include "BaseTypes.hpp"
#include "BaseString.hpp"
#include "Common.hpp"
#include "dataspace/Dataspace.hpp"

// Hashing:
namespace boost {

template<>
struct hash<boost::asio::ip::address> {
  size_t operator()(boost::asio::ip::address const& v) const {
    if (v.is_v4()) {
      return v.to_v4().to_ulong();
    }
    if (v.is_v6()) {
      auto const& range = v.to_v6().to_bytes();
      return hash_range(range.begin(), range.end());
    }
    if (v.is_unspecified()) {
      return 0x4751301174351161ul;
    }
    return hash_value(v.to_string());
  }
};

} // boost

namespace K3 {

  template <class C1, class C, class F>
  void read_records(C1& paths, C& container, F read_record) {

    for (auto rec : paths) {
      std::ifstream in;
      in.open(rec.path);
      std::string tmp_buffer;
      while (!in.eof()) {
        container.insert(read_record(in, tmp_buffer));
        in >> std::ws;
      }
    }

    return;
  }

  class __pcm_context {
    #ifdef CACHEPROFILE
    protected:
      PCM *instance;
      std::shared_ptr<SystemCounterState> initial_state;
    #endif

    public:
      __pcm_context();
      ~__pcm_context();
      unit_t cacheProfilerStart(unit_t);
      unit_t cacheProfilerStop(unit_t);
  };

  class __tcmalloc_context {
    public:
      unit_t heapProfilerStart(const string_impl&);
      unit_t heapProfilerStop(unit_t);
  };

  class __jemalloc_context {
    public:
      unit_t jemallocStart(unit_t);
      unit_t jemallocStop(unit_t);
  };
  template <class C1, class C, class F>
  void read_records_with_resize(int size, C1& paths, C& container, F read_record) {

    if (size == 0) {
      return read_records(paths, container, read_record);
    }
    else {
      container.getContainer().resize(size);
    }

    int i = 0;
    for (auto rec : paths) {
      std::ifstream in;
      in.open(rec.path);

      std::string tmp_buffer;
      while (!in.eof()) {
        if (i >= container.size(unit_t {}) ) {
          throw std::runtime_error("Cannot read records, container size is too small");
        }
        container.getContainer()[i++] = read_record(in, tmp_buffer);
        in >> std::ws;
      }
    }

    return;
  }

  // Standard context for common builtins that use a handle to the engine (via inheritance)
  class __standard_context : public __k3_context {
    public:
    __standard_context(Engine&);

    unit_t openBuiltin(string_impl ch_id, string_impl builtin_ch_id, string_impl fmt);
    unit_t openFile(string_impl ch_id, string_impl path, string_impl fmt, string_impl mode);
    unit_t openSocket(string_impl ch_id, Address a, string_impl fmt, string_impl mode);

    bool hasRead(string_impl ch_id);
    template<typename T> T doRead(string_impl ch_id);
    template<typename T> Collection<R_elem<T>> doReadBlock(string_impl ch_id, int block_size);

    bool hasWrite(string_impl ch_id);
    template<typename T> unit_t doWrite(string_impl ch_id, T& val);

    unit_t close(string_impl chan_id);

    int random(int n);

    double randomFraction(unit_t);

    template <class T>
    int hash(const T& x) {
      // We implement hash_value for all of our types.
      // for ordered containers, so we may as well delegate to that.
      return static_cast<int>(hash_value(x));
    }

    template <class T>
    string_impl toJson(const T& in) {
      return K3::serialization::json::encode<T>(in);
    }

    template <class T>
    T range(int i) {
      T result;
      for (int j = 0; j < i; j++) {
        result.insert(j);
      }
      return result;
    }

    int truncate(double n) { return (int)n; }

    double real_of_int(int n) { return (double)n; }

    int get_max_int(unit_t) { return INT_MAX; }

    unit_t print(string_impl message);

    // TODO, implement, sharing code with prettify()
    template <class T>
    string_impl show(T t) {
      return string_impl("TODO: implement show()");
    }

    template <class T>
    T error(unit_t) {
      throw std::runtime_error("Error. Terminating");
      return T();
    }

    template <class T>
    unit_t ignore(T t) {
      return unit_t();
    }

    // TODO add a member to base_string, call that instead
    int strcomp(const string_impl& s1,const string_impl& s2) {
      const char* c1 = s1.c_str();
      const char* c2 = s2.c_str();
      if (c1 && c2) {
        return strcmp(c1,c2);
      }
      else if(c1) {
        return 1;
      }
      else if(c2) {
        return -1;
      }
      else {
        return 0;
      }

    }

    unit_t haltEngine(unit_t);

    unit_t drainEngine(unit_t);

    unit_t sleep(int n);

    template <template <class> class M, template <class> class C,
              template <typename...> class R, class C2>
    unit_t loadGraph(const C2& filepaths, M<R<int, C<R_elem<int>>>>& c) {
      for (const auto& filepath : filepaths) {
        std::string tmp_buffer;
        std::ifstream in(filepath.path);

        int source;
        std::size_t position;
        while (!in.eof()) {
          C<R_elem<int>> edge_list;

          std::size_t start = 0;
          std::size_t end = start;
          std::getline(in, tmp_buffer);

          end = tmp_buffer.find(",", start);
          source = std::atoi(tmp_buffer.substr(start, end - start).c_str());

          start = end + 1;

          while (end != std::string::npos) {
            end = tmp_buffer.find(",", start);
            edge_list.insert(R_elem<int>(
                std::atoi(tmp_buffer.substr(start, end - start).c_str())));
            start = end + 1;
          }

          c.insert(R<int, C<R_elem<int>>>{source, std::move(edge_list)});
          in >> std::ws;
        }
      }
      return unit_t{};
    }

    // TODO move to seperate context
    template <class C1>
    unit_t loadRKQ3(const C1& paths, K3::Map<R_key_value<string_impl, int>>& c)
    {
      for (auto r : paths) {
        // Buffers
        std::string tmp_buffer;
        R_key_value<string_impl, int> rec;
        // Infile
        std::ifstream in;
        in.open(r.path);

        // Parse by line
        while (!in.eof()) {
          std::getline(in, tmp_buffer, ',');
          rec.key = tmp_buffer;
          std::getline(in, tmp_buffer, ',');
          rec.value = std::atoi(tmp_buffer.c_str());
          // ignore last value
          std::getline(in, tmp_buffer);
          c.insert(rec);
        }
      }

      return unit_t{};
    }

   int lineCountFile(const string_impl& filepath) {
     std::cout << "LCF: " << filepath << std::endl;
     std::ifstream _in;
     _in.open(filepath);
     std::string tmp_buffer;
     std::getline(_in, tmp_buffer);
     std::cout << "LCF read: " << tmp_buffer << std::endl;
     return std::atoi(tmp_buffer.c_str());
   }

   template <class C1, template <class> class C, template <typename ...> class R>
   unit_t loadQ1(const C1& paths, C<R<int, string_impl>>& c) {
        K3::read_records(paths, c, [] (std::istream& in, std::string& tmp_buffer)   {
          R<int, string_impl> record;
          // Get pageURL
          std::getline(in, tmp_buffer, ',');
          record.pageURL = tmp_buffer;
          // Get pageRank
          std::getline(in, tmp_buffer, ',');
          record.pageRank = std::atoi(tmp_buffer.c_str());
          // Ignore avgDuration
          std::getline(in, tmp_buffer);
          //record.avgDuration = std::atoi(tmp_buffer.c_str());
          return record;
        });
        return unit_t {};
   }

   template <class C1, template<typename S> class C, template <typename ...> class R>
   unit_t loadQ2(const C1& paths, C<R<double, string_impl>>& c) {
        K3::read_records(paths, c, [] (std::istream& in, std::string& tmp_buffer)   {
          R<double, string_impl> record;
          // Get sourceIP
          std::getline(in, tmp_buffer, ',');
          record.sourceIP = tmp_buffer;

          // Ignore until adRevenue
          std::getline(in, tmp_buffer, ',');
          //record.destURL = tmp_buffer;
          std::getline(in, tmp_buffer, ',');
          //record.visitDate = tmp_buffer;

          // Get adRevenue
          std::getline(in, tmp_buffer, ',');
          record.adRevenue = std::atof(tmp_buffer.c_str());

          // Ignore the rest
          std::getline(in, tmp_buffer, ',');
          //record.userAgent = tmp_buffer;
          std::getline(in, tmp_buffer, ',');
          //record.countryCode = tmp_buffer;
          std::getline(in, tmp_buffer, ',');
          //record.languageCode = tmp_buffer;
          std::getline(in, tmp_buffer, ',');
          //record.searchWord = tmp_buffer;
          std::getline(in, tmp_buffer);
          //record.duration = std::atoi(tmp_buffer.c_str());
          return record;
        });
        return unit_t {};
   }

   template <class C1, template<typename S> class C, template <typename ...> class R>
   unit_t loadUVQ3(const C1& paths, C<R<double, string_impl, string_impl, string_impl>>& c) {
        K3::read_records(paths, c, [] (std::istream& in, std::string& tmp_buffer)   {
          R<double, string_impl, string_impl, string_impl> record;
          std::getline(in, tmp_buffer, ',');
          record.sourceIP = tmp_buffer;
          std::getline(in, tmp_buffer, ',');
          record.destURL = tmp_buffer;
          std::getline(in, tmp_buffer, ',');
          record.visitDate = tmp_buffer;
          std::getline(in, tmp_buffer, ',');
          record.adRevenue = std::atof(tmp_buffer.c_str());
          std::getline(in, tmp_buffer, ',');
          //record.userAgent = tmp_buffer;
          std::getline(in, tmp_buffer, ',');
          //record.countryCode = tmp_buffer;
          std::getline(in, tmp_buffer, ',');
          //record.languageCode = tmp_buffer;
          std::getline(in, tmp_buffer, ',');
          //record.searchWord = tmp_buffer;
          std::getline(in, tmp_buffer);
          //record.duration = std::atoi(tmp_buffer.c_str());
          return record;
        });
        return unit_t {};
   }

   template <class C, class F>
   unit_t logHelper(string_impl filepath, const C& c, F f, string_impl sep) {
     std::ofstream outfile;
     outfile.open(filepath);
     auto& container = c.getConstContainer();
     for (auto& elem : container) {
       f(outfile, elem, sep);
       outfile << "\n";
     }
     outfile.close();
     return unit_t();

   }
    Vector<R_elem<double>> zeroVector(int i);
    Vector<R_elem<double>> randomVector(int i);

    template <template <typename S> class C, class R>
    unit_t loadStrings(string_impl filepath, C<R>& c) {
      std::string line;
      std::ifstream infile(filepath);
      while (std::getline(infile, line)) {
        c.insert(R{line});
      }
      return unit_t{};
    }

    template <template<typename S> class C, class V>
    unit_t loadVector(string_impl filepath, C<R_elem<V>>& c) {
      std::string line;
      std::ifstream infile(filepath);
      char *saveptr;

      while (std::getline(infile, line)){
        char * pch;
        pch = strtok_r(&line[0],",", &saveptr);
        V v;
        while (pch) {
          R_elem<double> rec;
          rec.elem = std::atof(pch);
          v.insert(rec);
          pch = strtok_r(NULL,",", &saveptr);
        }
        R_elem<V> rec2 {v};
        c.insert(rec2);
      }
      return unit_t();
    }

    template <template <typename S> class C, template <typename...> class R, class V>
    unit_t loadVectorLabel(int dims, string_impl filepath, C<R<double, V>>& c) {
      // Buffers
      std::string tmp_buffer;
      R<double, V> rec;
      // Infile
      std::ifstream in;
      in.open(filepath);
      char* saveptr;

      // Parse by line
      while (!in.eof()) {
        V v;
        R_elem<double> r;
        for (int j = 0; j < dims; j++) {
          std::getline(in, tmp_buffer, ',');
          r.elem = std::atof(tmp_buffer.c_str());
          v.insert(r);
        }
        std::getline(in, tmp_buffer, ',');
        rec.class_label = std::atof(tmp_buffer.c_str());
        rec.elem = v;
        c.insert(rec);

        in >> std::ws;
      }

      return unit_t{};
    }
  };

  // Utilities:

  // Time:
  class __time_context {
    public:
    __time_context();
    int now_int(unit_t);
  };

  // String operations:

  class __string_context {
    public:
    shared_ptr<RE2> pattern;
    __string_context();

    string_impl itos(int i);

    string_impl rtos(double d);

    string_impl atos(Address a);

    F<Collection<R_elem<string_impl>>(const string_impl &)> regex_matcher(const string_impl&);
    Collection<R_elem<string_impl>> regex_matcher_q4(const string_impl&);

    template <class S> S slice_string(const S& s, int x, int y) {
      return s.substr(x, y);
    }

    // Split a string by substrings
    Seq<R_elem<string_impl>> splitString(string_impl, const string_impl&);
    string_impl takeUntil(const string_impl& s, const string_impl& splitter);
    int countChar(const string_impl& s, const string_impl& splitter);
    int tpch_date(const string_impl& s);
    string_impl tpch_date_to_string(const int& date);
  };

} // namespace K3

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


<commit_before>#include "catch.hpp"
#include <toolbox/alloc/stack_allocator.hpp>
#include <iostream>

TEST_CASE("simple functionality", "[alloc][stack_allocator]")
{
	toolbox::alloc::stack_allocator<int, 10> alloc;

	int *p1 = alloc.allocate(1);
	int *p2 = alloc.allocate(1);
	int *p3 = alloc.allocate(8);

	REQUIRE(p2 - p1 == 1);
	REQUIRE(p3 != nullptr);
	REQUIRE(alloc.allocate(1) == nullptr);
	REQUIRE(alloc.available() == 0);

	alloc.deallocate(p1, 1);
	REQUIRE(alloc.available() == 0);

	alloc.deallocate(p3, 8);
	REQUIRE(alloc.available() == 8);
}

TEST_CASE("test with vector", "[alloc][stack_allocator][vector]")
{
	toolbox::alloc::stack_allocator<float, 5> alloc;
	std::vector<float, decltype(alloc)> vec(alloc);

	vec.push_back(1);
	vec.push_back(1);
}
<commit_msg>tests: check for exception in allocator tests<commit_after>#include "catch.hpp"
#include <toolbox/alloc/stack_allocator.hpp>
#include <iostream>

TEST_CASE("simple functionality", "[alloc][stack_allocator]")
{
	toolbox::alloc::stack_allocator<int, 10> alloc;

	int *p1 = alloc.allocate(1);
	int *p2 = alloc.allocate(1);
	int *p3 = alloc.allocate(8);

	REQUIRE(p2 - p1 == 1);
	REQUIRE(p3 != nullptr);
	REQUIRE_THROWS(alloc.allocate(1));
	REQUIRE(alloc.available() == 0);

	alloc.deallocate(p1, 1);
	REQUIRE(alloc.available() == 0);

	alloc.deallocate(p3, 8);
	REQUIRE(alloc.available() == 8);
}

TEST_CASE("test with vector", "[alloc][stack_allocator][vector]")
{
	toolbox::alloc::stack_allocator<float, 5> alloc;
	std::vector<float, decltype(alloc)> vec(alloc);

	vec.push_back(1);
	vec.push_back(1);
}
<|endoftext|>


<commit_before>#ifndef DIMENTIONS_HPP
#define DIMENTIONS_HPP

struct Dimentions
{
    int W;
    int H;
    Dimentions(int w, int h) : W(w), H(h) {}
};

#endif // DIMENTIONS_HPP
<commit_msg>Add comparison operators for Dimention class.<commit_after>#ifndef DIMENTIONS_HPP
#define DIMENTIONS_HPP

struct Dimentions
{
    int W;
    int H;
    Dimentions(int w, int h) : W(w), H(h) {}
};

inline bool operator==(const Dimentions& lhs, const Dimentions& rhs)
{
    return lhs.W == rhs.W && lhs.H == rhs.H;
}

inline bool operator!=(const Dimentions& lhs, const Dimentions& rhs)
{
    return !(lhs == rhs);
}

#endif // DIMENTIONS_HPP
<|endoftext|>


<commit_before>// -------------------------------------------------------------------------
//    @FileName      :    NFCEventProcessModule.cpp
//    @Author           :    LvSheng.Huang
//    @Date             :    2012-12-15
//    @Module           :    NFCEventProcessModule
//
// -------------------------------------------------------------------------

#include "NFCEventProcessModule.h"
#include "NFComm/NFPluginModule/NFIPluginManager.h"
#include "NFComm/NFPluginModule/NFIActorManager.h"

NFCEventProcessModule::NFCEventProcessModule(NFIPluginManager* p)
{
    pPluginManager = p;
}

NFCEventProcessModule::~NFCEventProcessModule()
{
	mRemoveObjectListEx.ClearAll();
	mRemoveEventListEx.ClearAll();

	mxClassEventInfoEx.ClearAll();
	mObjectEventInfoMapEx.ClearAll();
}

bool NFCEventProcessModule::Init()
{
    return true;
}

bool NFCEventProcessModule::Shut()
{
    mxClassEventInfoEx.ClearAll();
    mRemoveEventListEx.ClearAll();
    mObjectEventInfoMapEx.ClearAll();

    return true;
}

void NFCEventProcessModule::OnReload(const char* strModuleName, NFILogicModule* pModule)
{
}

bool NFCEventProcessModule::AddEventCallBack(const NFIDENTID& objectID, const int nEventID, const EVENT_PROCESS_FUNCTOR_PTR& cb)
{
    NF_SHARE_PTR<NFCObjectEventInfo> pObjectEventInfo = mObjectEventInfoMapEx.GetElement(objectID);
    if (nullptr != pObjectEventInfo)
    {
        pObjectEventInfo = NF_SHARE_PTR<NFCObjectEventInfo>(NF_NEW NFCObjectEventInfo());
        mObjectEventInfoMapEx.AddElement(objectID, pObjectEventInfo);
    }

    assert(nullptr != pObjectEventInfo);

    NF_SHARE_PTR<NFEventList> pEventInfo = pObjectEventInfo->GetElement(nEventID);
    if (nullptr != pEventInfo)
    {
        pEventInfo = NF_SHARE_PTR<NFEventList>(NF_NEW NFEventList());
        pObjectEventInfo->AddElement(nEventID, pEventInfo);
    }

    assert(nullptr != pEventInfo);

    pEventInfo->Add(cb);

    return true;
}

bool NFCEventProcessModule::Execute(const float fLasFrametime, const float fStartedTime)
{
    NFIDENTID ident;

    NF_SHARE_PTR<NFList<int>> pList = mRemoveEventListEx.First(ident);
    while (nullptr != pList)
    {
        NF_SHARE_PTR<NFCObjectEventInfo> pObjectEventInfo = mObjectEventInfoMapEx.GetElement(ident);
        if (pObjectEventInfo)
        {
            int nEvent = 0;
            bool bRet = pList->First(nEvent);
            while (bRet)
            {
                pObjectEventInfo->RemoveElement(nEvent);

                bRet = pList->Next(nEvent);
            }
        }

        pList = NULL;
        pList = mRemoveEventListEx.Next();
    }

    mRemoveEventListEx.ClearAll();

    //////////////////////////////////////////////////////////////////////////
    //删除事件对象
    bool bRet = mRemoveObjectListEx.First(ident);
    while (bRet)
    {
        mObjectEventInfoMapEx.RemoveElement(ident);

        bRet = mRemoveObjectListEx.Next(ident);
    }

    mRemoveObjectListEx.ClearAll();

    return true;
}

bool NFCEventProcessModule::RemoveEvent(const NFIDENTID& objectID)
{
    return mRemoveObjectListEx.Add(objectID);
}

bool NFCEventProcessModule::RemoveEventCallBack(const NFIDENTID& objectID, const int nEventID/*, const EVENT_PROCESS_FUNCTOR_PTR& cb*/)
{
    NF_SHARE_PTR<NFCObjectEventInfo> pObjectEventInfo = mObjectEventInfoMapEx.GetElement(objectID);
    if (nullptr != pObjectEventInfo)
    {
        NF_SHARE_PTR<NFEventList> pEventInfo = pObjectEventInfo->GetElement(nEventID);
        if (nullptr != pEventInfo)
        {
            NF_SHARE_PTR<NFList<int>> pList = mRemoveEventListEx.GetElement(objectID);
            if (nullptr != pList)
            {
                pList = NF_SHARE_PTR<NFList<int>>(NF_NEW NFList<int>());
                mRemoveEventListEx.AddElement(objectID, pList);
            }

            pList->Add(nEventID);
            return true;
        }
    }

    return false;
}

bool NFCEventProcessModule::DoEvent(const NFIDENTID& objectID, const int nEventID, const NFIDataList& valueList)
{
	NF_SHARE_PTR<NFCObjectEventInfo> pObjectEventInfo = mObjectEventInfoMapEx.GetElement(objectID);
	if (nullptr == pObjectEventInfo)
	{
		return false;
	}

	NF_SHARE_PTR<NFEventList> pEventInfo = pObjectEventInfo->GetElement(nEventID);
	if (nullptr == pEventInfo)
	{
		return false;
	}

	EVENT_PROCESS_FUNCTOR_PTR cb;
	bool bRet = pEventInfo->First(cb);
	while (bRet)
	{
		cb->operator()(objectID, nEventID,  valueList);

		bRet = pEventInfo->Next(cb);
	}

    return true;
}

bool NFCEventProcessModule::DoEvent(const NFIDENTID& objectID, const std::string& strClassName, const CLASS_OBJECT_EVENT eClassEvent, const NFIDataList& valueList)
{
    NF_SHARE_PTR<NFCClassEventList> pEventList = mxClassEventInfoEx.GetElement(strClassName);
    if (nullptr != pEventList)
    {
        CLASS_EVENT_FUNCTOR_PTR cb;
        bool bRet = pEventList->First(cb);
        while (bRet)
        {
            cb->operator()(objectID, strClassName, eClassEvent,  valueList);

            bRet = pEventList->Next(cb);
        }
    }

    return false;
}

bool NFCEventProcessModule::HasEventCallBack(const NFIDENTID& objectID, const int nEventID)
{
    NF_SHARE_PTR<NFCObjectEventInfo> pObjectEventInfo = mObjectEventInfoMapEx.GetElement(objectID);
    if (nullptr != pObjectEventInfo)
    {
        NF_SHARE_PTR<NFEventList> pEventInfo = pObjectEventInfo->GetElement(nEventID);
        if (nullptr != pEventInfo)
        {
            return true;
        }
    }

    return false;
}

bool NFCEventProcessModule::AddClassCallBack(const std::string& strClassName, const CLASS_EVENT_FUNCTOR_PTR& cb)
{
    NF_SHARE_PTR<NFCClassEventList> pEventList = mxClassEventInfoEx.GetElement(strClassName);
    if (nullptr == pEventList)
    {
        pEventList = NF_SHARE_PTR<NFCClassEventList>(NF_NEW NFCClassEventList());
        mxClassEventInfoEx.AddElement(strClassName, pEventList);
    }

    assert(NULL != pEventList);

    pEventList->Add(cb);

    return true;
}<commit_msg>fixed dump for  event system<commit_after>// -------------------------------------------------------------------------
//    @FileName      :    NFCEventProcessModule.cpp
//    @Author           :    LvSheng.Huang
//    @Date             :    2012-12-15
//    @Module           :    NFCEventProcessModule
//
// -------------------------------------------------------------------------

#include "NFCEventProcessModule.h"
#include "NFComm/NFPluginModule/NFIPluginManager.h"
#include "NFComm/NFPluginModule/NFIActorManager.h"

NFCEventProcessModule::NFCEventProcessModule(NFIPluginManager* p)
{
    pPluginManager = p;
}

NFCEventProcessModule::~NFCEventProcessModule()
{
	mRemoveObjectListEx.ClearAll();
	mRemoveEventListEx.ClearAll();

	mxClassEventInfoEx.ClearAll();
	mObjectEventInfoMapEx.ClearAll();
}

bool NFCEventProcessModule::Init()
{
    return true;
}

bool NFCEventProcessModule::Shut()
{
    mxClassEventInfoEx.ClearAll();
    mRemoveEventListEx.ClearAll();
    mObjectEventInfoMapEx.ClearAll();

    return true;
}

void NFCEventProcessModule::OnReload(const char* strModuleName, NFILogicModule* pModule)
{
}

bool NFCEventProcessModule::AddEventCallBack(const NFIDENTID& objectID, const int nEventID, const EVENT_PROCESS_FUNCTOR_PTR& cb)
{
    NF_SHARE_PTR<NFCObjectEventInfo> pObjectEventInfo = mObjectEventInfoMapEx.GetElement(objectID);
    if (nullptr == pObjectEventInfo)
    {
        pObjectEventInfo = NF_SHARE_PTR<NFCObjectEventInfo>(NF_NEW NFCObjectEventInfo());
        mObjectEventInfoMapEx.AddElement(objectID, pObjectEventInfo);
    }

    assert(nullptr != pObjectEventInfo);

    NF_SHARE_PTR<NFEventList> pEventInfo = pObjectEventInfo->GetElement(nEventID);
    if (nullptr == pEventInfo)
    {
        pEventInfo = NF_SHARE_PTR<NFEventList>(NF_NEW NFEventList());
        pObjectEventInfo->AddElement(nEventID, pEventInfo);
    }

    assert(nullptr != pEventInfo);

    pEventInfo->Add(cb);

    return true;
}

bool NFCEventProcessModule::Execute(const float fLasFrametime, const float fStartedTime)
{
    NFIDENTID ident;

    NF_SHARE_PTR<NFList<int>> pList = mRemoveEventListEx.First(ident);
    while (nullptr != pList)
    {
        NF_SHARE_PTR<NFCObjectEventInfo> pObjectEventInfo = mObjectEventInfoMapEx.GetElement(ident);
        if (pObjectEventInfo)
        {
            int nEvent = 0;
            bool bRet = pList->First(nEvent);
            while (bRet)
            {
                pObjectEventInfo->RemoveElement(nEvent);

                bRet = pList->Next(nEvent);
            }
        }

        pList = NULL;
        pList = mRemoveEventListEx.Next();
    }

    mRemoveEventListEx.ClearAll();

    //////////////////////////////////////////////////////////////////////////
    //删除事件对象
    bool bRet = mRemoveObjectListEx.First(ident);
    while (bRet)
    {
        mObjectEventInfoMapEx.RemoveElement(ident);

        bRet = mRemoveObjectListEx.Next(ident);
    }

    mRemoveObjectListEx.ClearAll();

    return true;
}

bool NFCEventProcessModule::RemoveEvent(const NFIDENTID& objectID)
{
    return mRemoveObjectListEx.Add(objectID);
}

bool NFCEventProcessModule::RemoveEventCallBack(const NFIDENTID& objectID, const int nEventID/*, const EVENT_PROCESS_FUNCTOR_PTR& cb*/)
{
    NF_SHARE_PTR<NFCObjectEventInfo> pObjectEventInfo = mObjectEventInfoMapEx.GetElement(objectID);
    if (nullptr != pObjectEventInfo)
    {
        NF_SHARE_PTR<NFEventList> pEventInfo = pObjectEventInfo->GetElement(nEventID);
        if (nullptr != pEventInfo)
        {
            NF_SHARE_PTR<NFList<int>> pList = mRemoveEventListEx.GetElement(objectID);
            if (nullptr != pList)
            {
                pList = NF_SHARE_PTR<NFList<int>>(NF_NEW NFList<int>());
                mRemoveEventListEx.AddElement(objectID, pList);
            }

            pList->Add(nEventID);
            return true;
        }
    }

    return false;
}

bool NFCEventProcessModule::DoEvent(const NFIDENTID& objectID, const int nEventID, const NFIDataList& valueList)
{
	NF_SHARE_PTR<NFCObjectEventInfo> pObjectEventInfo = mObjectEventInfoMapEx.GetElement(objectID);
	if (nullptr == pObjectEventInfo)
	{
		return false;
	}

	NF_SHARE_PTR<NFEventList> pEventInfo = pObjectEventInfo->GetElement(nEventID);
	if (nullptr == pEventInfo)
	{
		return false;
	}

	EVENT_PROCESS_FUNCTOR_PTR cb;
	bool bRet = pEventInfo->First(cb);
	while (bRet)
	{
		cb->operator()(objectID, nEventID,  valueList);

		bRet = pEventInfo->Next(cb);
	}

    return true;
}

bool NFCEventProcessModule::DoEvent(const NFIDENTID& objectID, const std::string& strClassName, const CLASS_OBJECT_EVENT eClassEvent, const NFIDataList& valueList)
{
    NF_SHARE_PTR<NFCClassEventList> pEventList = mxClassEventInfoEx.GetElement(strClassName);
    if (nullptr != pEventList)
    {
        CLASS_EVENT_FUNCTOR_PTR cb;
        bool bRet = pEventList->First(cb);
        while (bRet)
        {
            cb->operator()(objectID, strClassName, eClassEvent,  valueList);

            bRet = pEventList->Next(cb);
        }
    }

    return false;
}

bool NFCEventProcessModule::HasEventCallBack(const NFIDENTID& objectID, const int nEventID)
{
    NF_SHARE_PTR<NFCObjectEventInfo> pObjectEventInfo = mObjectEventInfoMapEx.GetElement(objectID);
    if (nullptr != pObjectEventInfo)
    {
        NF_SHARE_PTR<NFEventList> pEventInfo = pObjectEventInfo->GetElement(nEventID);
        if (nullptr != pEventInfo)
        {
            return true;
        }
    }

    return false;
}

bool NFCEventProcessModule::AddClassCallBack(const std::string& strClassName, const CLASS_EVENT_FUNCTOR_PTR& cb)
{
    NF_SHARE_PTR<NFCClassEventList> pEventList = mxClassEventInfoEx.GetElement(strClassName);
    if (nullptr == pEventList)
    {
        pEventList = NF_SHARE_PTR<NFCClassEventList>(NF_NEW NFCClassEventList());
        mxClassEventInfoEx.AddElement(strClassName, pEventList);
    }

    assert(NULL != pEventList);

    pEventList->Add(cb);

    return true;
}<|endoftext|>