mirror of
				https://github.com/paboyle/Grid.git
				synced 2025-11-02 21:14:32 +00:00 
			
		
		
		
	Adding factories
This commit is contained in:
		
							
								
								
									
										100
									
								
								lib/qcd/modules/Factory.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										100
									
								
								lib/qcd/modules/Factory.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,100 @@
 | 
			
		||||
/*************************************************************************************
 | 
			
		||||
Grid physics library, www.github.com/paboyle/Grid 
 | 
			
		||||
Source file: extras/Hadrons/Factory.hpp
 | 
			
		||||
 | 
			
		||||
Copyright (C) 2015
 | 
			
		||||
Copyright (C) 2016
 | 
			
		||||
 | 
			
		||||
Author: Antonin Portelli <antonin.portelli@me.com>
 | 
			
		||||
 | 
			
		||||
This program is free software; you can redistribute it and/or modify
 | 
			
		||||
it under the terms of the GNU General Public License as published by
 | 
			
		||||
the Free Software Foundation; either version 2 of the License, or
 | 
			
		||||
(at your option) any later version.
 | 
			
		||||
This program 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 General Public License for more details.
 | 
			
		||||
You should have received a copy of the GNU General Public License along
 | 
			
		||||
with this program; if not, write to the Free Software Foundation, Inc.,
 | 
			
		||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 | 
			
		||||
See the full license in the file "LICENSE" in the top level distribution directory
 | 
			
		||||
*************************************************************************************/
 | 
			
		||||
/*  END LEGAL */
 | 
			
		||||
 | 
			
		||||
#ifndef Factory_hpp_
 | 
			
		||||
#define Factory_hpp_
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
namespace Grid{
 | 
			
		||||
 | 
			
		||||
/******************************************************************************
 | 
			
		||||
 *                        abstract factory class                              *
 | 
			
		||||
 ******************************************************************************/
 | 
			
		||||
template <typename T, typename ProductCreator>
 | 
			
		||||
class Factory
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
    typedef std::function< std::unique_ptr<T> (const ProductCreator&)> Func;
 | 
			
		||||
public:
 | 
			
		||||
    // constructor
 | 
			
		||||
    Factory(void) = default;
 | 
			
		||||
    // destructor
 | 
			
		||||
    virtual ~Factory(void) = default;
 | 
			
		||||
    // registration
 | 
			
		||||
    void registerBuilder(const std::string type, const Func &f);
 | 
			
		||||
    // get builder list
 | 
			
		||||
    std::vector<std::string> getBuilderList(void) const;
 | 
			
		||||
    // factory
 | 
			
		||||
    std::unique_ptr<T> create(const std::string type,
 | 
			
		||||
              const ProductCreator& name) const;
 | 
			
		||||
private:
 | 
			
		||||
    std::map<std::string, Func> builder_;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/******************************************************************************
 | 
			
		||||
 *                         template implementation                            *
 | 
			
		||||
 ******************************************************************************/
 | 
			
		||||
// registration ////////////////////////////////////////////////////////////////
 | 
			
		||||
template <typename T, typename ProductCreator>
 | 
			
		||||
void Factory<T, ProductCreator>::registerBuilder(const std::string type, const Func &f)
 | 
			
		||||
{
 | 
			
		||||
    builder_[type] = f;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// get module list /////////////////////////////////////////////////////////////
 | 
			
		||||
template <typename T, typename ProductCreator>
 | 
			
		||||
std::vector<std::string> Factory<T, ProductCreator>::getBuilderList(void) const
 | 
			
		||||
{
 | 
			
		||||
    std::vector<std::string> list;
 | 
			
		||||
    
 | 
			
		||||
    for (auto &b: builder_)
 | 
			
		||||
    {
 | 
			
		||||
        list.push_back(b.first);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    return list;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// factory /////////////////////////////////////////////////////////////////////
 | 
			
		||||
template <typename T, typename ProductCreator>
 | 
			
		||||
std::unique_ptr<T> Factory<T, ProductCreator>::create(const std::string type,
 | 
			
		||||
                                      const ProductCreator& name) const
 | 
			
		||||
{
 | 
			
		||||
    Func func;
 | 
			
		||||
    
 | 
			
		||||
    try
 | 
			
		||||
    {
 | 
			
		||||
        func = builder_.at(type);
 | 
			
		||||
    }
 | 
			
		||||
    catch (std::out_of_range &)
 | 
			
		||||
    {
 | 
			
		||||
        //HADRON_ERROR("object of type '" + type + "' unknown");
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    return func(name);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif // Factory_hpp_
 | 
			
		||||
							
								
								
									
										198
									
								
								lib/qcd/modules/Modules.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										198
									
								
								lib/qcd/modules/Modules.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,198 @@
 | 
			
		||||
/*************************************************************************************
 | 
			
		||||
 | 
			
		||||
Grid physics library, www.github.com/paboyle/Grid
 | 
			
		||||
 | 
			
		||||
Source file: ./lib/qcd/action/gauge/WilsonGaugeAction.h
 | 
			
		||||
 | 
			
		||||
Copyright (C) 2016
 | 
			
		||||
 | 
			
		||||
Author: Guido Cossu <guido.cossu@ed.ac.uk>
 | 
			
		||||
 | 
			
		||||
This program is free software; you can redistribute it and/or modify
 | 
			
		||||
it under the terms of the GNU General Public License as published by
 | 
			
		||||
the Free Software Foundation; either version 2 of the License, or
 | 
			
		||||
(at your option) any later version.
 | 
			
		||||
 | 
			
		||||
This program 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 General Public License for more details.
 | 
			
		||||
 | 
			
		||||
You should have received a copy of the GNU General Public License along
 | 
			
		||||
with this program; if not, write to the Free Software Foundation, Inc.,
 | 
			
		||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 | 
			
		||||
 | 
			
		||||
See the full license in the file "LICENSE" in the top level distribution
 | 
			
		||||
directory
 | 
			
		||||
*************************************************************************************/
 | 
			
		||||
/*  END LEGAL */
 | 
			
		||||
#ifndef HMC_MODULES_H
 | 
			
		||||
#define HMC_MODULES_H
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
Define loadable, serializable modules
 | 
			
		||||
for the HMC execution
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
namespace Grid {
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
Base class for modules with parameters
 | 
			
		||||
*/
 | 
			
		||||
template < class P >
 | 
			
		||||
class Parametrized{
 | 
			
		||||
public:
 | 
			
		||||
  typedef P Parameters;
 | 
			
		||||
 | 
			
		||||
  Parametrized(Parameters Par):Par_(Par){};
 | 
			
		||||
 | 
			
		||||
  template <class ReaderClass>
 | 
			
		||||
  Parametrized(Reader<ReaderClass> & Reader){
 | 
			
		||||
    read(Reader, section_name(), Par_);
 | 
			
		||||
  }
 | 
			
		||||
protected:
 | 
			
		||||
  Parameters Par_;
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
  // identifies the section name
 | 
			
		||||
  // override in derived classes if needed 
 | 
			
		||||
  virtual std::string section_name(){
 | 
			
		||||
    return std::string("parameters"); //default
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
Lowest level abstract module class
 | 
			
		||||
*/
 | 
			
		||||
template < class Prod >
 | 
			
		||||
class HMCModuleBase{
 | 
			
		||||
public:
 | 
			
		||||
  typedef Prod Product;
 | 
			
		||||
virtual Prod* getPtr() = 0;  
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
//////////////////////////////////////////////
 | 
			
		||||
//		Actions
 | 
			
		||||
//////////////////////////////////////////////
 | 
			
		||||
 | 
			
		||||
template <class ActionType, class APar>
 | 
			
		||||
class ActionModule
 | 
			
		||||
    : public Parametrized<APar>,
 | 
			
		||||
      public HMCModuleBase<QCD::Action<typename ActionType::GaugeField> > {
 | 
			
		||||
 public:
 | 
			
		||||
  typedef HMCModuleBase< QCD::Action<typename ActionType::GaugeField> > Base;
 | 
			
		||||
  typedef typename Base::Product Product;
 | 
			
		||||
 | 
			
		||||
  std::unique_ptr<ActionType> ActionPtr;
 | 
			
		||||
 | 
			
		||||
  ActionModule(APar Par) : Parametrized<APar>(Par) {}
 | 
			
		||||
 | 
			
		||||
  template <class ReaderClass>
 | 
			
		||||
  ActionModule(Reader<ReaderClass>& Reader) : Parametrized<APar>(Reader){};
 | 
			
		||||
 | 
			
		||||
  Product* getPtr() {
 | 
			
		||||
    if (!ActionPtr) initialize();
 | 
			
		||||
 | 
			
		||||
    return ActionPtr.get();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 private:
 | 
			
		||||
  virtual void initialize() = 0;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
namespace QCD{
 | 
			
		||||
 | 
			
		||||
class WilsonGaugeActionParameters : Serializable {
 | 
			
		||||
 public:
 | 
			
		||||
  GRID_SERIALIZABLE_CLASS_MEMBERS(WilsonGaugeActionParameters, 
 | 
			
		||||
    RealD, beta);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
template<class Impl>
 | 
			
		||||
class WilsonGModule: public ActionModule<WilsonGaugeAction<Impl>, WilsonGaugeActionParameters> {
 | 
			
		||||
  typedef ActionModule<WilsonGaugeAction<Impl>, WilsonGaugeActionParameters> ActionBase;
 | 
			
		||||
  using ActionBase::ActionBase;
 | 
			
		||||
 | 
			
		||||
  // acquire resource
 | 
			
		||||
  virtual void initialize(){
 | 
			
		||||
    ActionBase::ActionPtr.reset(new WilsonGaugeAction<Impl>(ActionBase::Par_.beta));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
typedef WilsonGModule<PeriodicGimplR> WilsonGMod;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}// QCD temporarily here
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// use the same classed defined by Antonin, does not make sense to rewrite
 | 
			
		||||
// Factory is perfectly fine
 | 
			
		||||
// Registar must be changed because I do not want to use the ModuleFactory
 | 
			
		||||
/*
 | 
			
		||||
define
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
typedef HMCModuleBase< QCD::Action< QCD::LatticeGaugeField > > HMCModBase;
 | 
			
		||||
 | 
			
		||||
template <class ReaderClass >
 | 
			
		||||
class HMCActionModuleFactory
 | 
			
		||||
    : public Factory < HMCModBase ,	Reader<ReaderClass> > {
 | 
			
		||||
 public:
 | 
			
		||||
 	typedef Reader<ReaderClass> TheReader;
 | 
			
		||||
 	// use SINGLETON FUNCTOR MACRO HERE
 | 
			
		||||
  HMCActionModuleFactory(const HMCActionModuleFactory& e) = delete;
 | 
			
		||||
  void operator=(const HMCActionModuleFactory& e) = delete;
 | 
			
		||||
  static HMCActionModuleFactory& getInstance(void) {
 | 
			
		||||
    static HMCActionModuleFactory e;
 | 
			
		||||
    return e;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 private:
 | 
			
		||||
  HMCActionModuleFactory(void) = default;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
then rewrite the registar
 | 
			
		||||
 | 
			
		||||
when this is done we have all the modules that contain the pointer to the objects
 | 
			
		||||
(actions, integrators, checkpointers, solvers)
 | 
			
		||||
 | 
			
		||||
factory will create only the modules and prepare the parameters
 | 
			
		||||
when needed a pointer is released
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
template <class T, class TheFactory>
 | 
			
		||||
class Registrar {
 | 
			
		||||
 public:
 | 
			
		||||
  Registrar(std::string className) {
 | 
			
		||||
    // register the class factory function
 | 
			
		||||
    TheFactory::getInstance().registerBuilder(className, [&](typename TheFactory::TheReader Reader)
 | 
			
		||||
        { return std::unique_ptr<T>(new T(Reader));});
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
Registrar<QCD::WilsonGMod, HMCActionModuleFactory<XmlReader> > __WGmodInit("WilsonGaugeAction"); 
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#endif //HMC_MODULES_H
 | 
			
		||||
							
								
								
									
										38
									
								
								lib/qcd/modules/mods.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								lib/qcd/modules/mods.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,38 @@
 | 
			
		||||
/*************************************************************************************
 | 
			
		||||
 | 
			
		||||
Grid physics library, www.github.com/paboyle/Grid
 | 
			
		||||
 | 
			
		||||
Source file: ./lib/qcd/action/gauge/WilsonGaugeAction.h
 | 
			
		||||
 | 
			
		||||
Copyright (C) 2016
 | 
			
		||||
 | 
			
		||||
Author: Guido Cossu <guido.cossu@ed.ac.uk>
 | 
			
		||||
 | 
			
		||||
This program is free software; you can redistribute it and/or modify
 | 
			
		||||
it under the terms of the GNU General Public License as published by
 | 
			
		||||
the Free Software Foundation; either version 2 of the License, or
 | 
			
		||||
(at your option) any later version.
 | 
			
		||||
 | 
			
		||||
This program 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 General Public License for more details.
 | 
			
		||||
 | 
			
		||||
You should have received a copy of the GNU General Public License along
 | 
			
		||||
with this program; if not, write to the Free Software Foundation, Inc.,
 | 
			
		||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 | 
			
		||||
 | 
			
		||||
See the full license in the file "LICENSE" in the top level distribution
 | 
			
		||||
directory
 | 
			
		||||
*************************************************************************************/
 | 
			
		||||
/*  END LEGAL */
 | 
			
		||||
#ifndef MODS_H
 | 
			
		||||
#define MODS_H
 | 
			
		||||
 | 
			
		||||
// Modules files
 | 
			
		||||
 | 
			
		||||
#include <Grid/qcd/modules/Factory.h>
 | 
			
		||||
#include <Grid/qcd/modules/Modules.h>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#endif //MODS_H
 | 
			
		||||
		Reference in New Issue
	
	Block a user