Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add factory class to create instances of OscProbCalcerBase objects #31

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions OscProbCalcer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
set(HEADERS OscProbCalcerBase.h)
set(HEADERS OscProbCalcerBase.h OscProbCalcerFactory.h)

add_library(OscProbCalcer SHARED OscProbCalcerBase.cpp)
add_library(OscProbCalcer SHARED OscProbCalcerBase.cpp OscProbCalcerFactory.cpp)

target_link_libraries(OscProbCalcer yaml-cpp NuOscillatorCompilerOptions)

Expand All @@ -22,7 +22,7 @@ install(DIRECTORY ${yaml-cpp_SOURCE_DIR}/include/
if(${UseCUDAProb3} EQUAL 1)
target_sources(OscProbCalcer PRIVATE OscProbCalcer_CUDAProb3.cpp)
if(${UseGPU} EQUAL 1)
set_source_files_properties(OscProbCalcer_CUDAProb3.cpp PROPERTIES LANGUAGE CUDA)
set_source_files_properties(OscProbCalcer_CUDAProb3.cpp PROPERTIES LANGUAGE CUDA)
endif()
target_include_directories(OscProbCalcer PRIVATE ${CUDAProb3_SOURCE_DIR})
list(APPEND HEADERS OscProbCalcer_CUDAProb3.h)
Expand Down
96 changes: 96 additions & 0 deletions OscProbCalcer/OscProbCalcerFactory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include "OscProbCalcerFactory.h"

#if UseCUDAProb3==1
#include "OscProbCalcer/OscProbCalcer_CUDAProb3.h"
#endif

#if UseCUDAProb3Linear==1
#include "OscProbCalcer/OscProbCalcer_CUDAProb3Linear.h"
#endif

#if UseProb3ppLinear==1
#include "OscProbCalcer/OscProbCalcer_Prob3ppLinear.h"
#endif

#if UseProbGPULinear==1
#include "OscProbCalcer/OscProbCalcer_ProbGPULinear.h"
#endif

#if UseNuFASTLinear==1
#include "OscProbCalcer/OscProbCalcer_NuFASTLinear.h"
#endif

#include <iostream>

OscProbCalcerFactory::OscProbCalcerFactory() {
}

OscProbCalcerFactory::~OscProbCalcerFactory() {
}

OscProbCalcerBase* OscProbCalcerFactory::CreateOscProbCalcer(std::string OscProbCalcerImplementationToCreate, std::string OscProbCalcerConfigName, int Instance, int Verbose) {
OscProbCalcerBase* Calcer = NULL;

if (OscProbCalcerImplementationToCreate == "CUDAProb3") {
#if UseCUDAProb3==1
OscProbCalcerCUDAProb3* CUDAProb3 = new OscProbCalcerCUDAProb3(OscProbCalcerConfigName,Instance);
Calcer = (OscProbCalcerBase*)CUDAProb3;
if (Verbose >= INFO) {std::cout << "Initalised OscProbCalcer Implementation:" << Calcer->ReturnImplementationName() << " in OscillatorBase object" << std::endl;}
#else
std::cerr << "Oscillator was requsted to create " << OscProbCalcerImplementationToCreate << " OscProbCalcer but Use" << OscProbCalcerImplementationToCreate << " is undefined. Indicates problem in setup" << std::endl;
throw;
#endif
}

else if (OscProbCalcerImplementationToCreate == "CUDAProb3Linear") {
#if UseCUDAProb3Linear==1
OscProbCalcerCUDAProb3Linear* CUDAProb3Linear = new OscProbCalcerCUDAProb3Linear(OscProbCalcerConfigName,Instance);
Calcer = (OscProbCalcerBase*)CUDAProb3Linear;
if (Verbose >= INFO) {std::cout << "Initalised OscProbCalcer Implementation:" << Calcer->ReturnImplementationName() << " in OscillatorBase object" << std::endl;}
#else
std::cerr << "Oscillator was requsted to create " << OscProbCalcerImplementationToCreate << " OscProbCalcer but Use" << OscProbCalcerImplementationToCreate << " is undefined. Indicates problem in setup" << std::endl;
throw;
#endif
}

else if (OscProbCalcerImplementationToCreate == "Prob3ppLinear") {
#if UseProb3ppLinear==1
OscProbCalcerProb3ppLinear* Prob3ppLinear = new OscProbCalcerProb3ppLinear(OscProbCalcerConfigName,Instance);
Calcer = (OscProbCalcerBase*)Prob3ppLinear;
if (Verbose >= INFO) {std::cout << "Initalised OscProbCalcer Implementation:" << Calcer->ReturnImplementationName() << " in OscillatorBase object" << std::endl;}
#else
std::cerr << "Oscillator was requsted to create " << OscProbCalcerImplementationToCreate << " OscProbCalcer but Use" << OscProbCalcerImplementationToCreate << " is undefined. Indicates problem in setup" << std::endl;
throw;
#endif
}

else if (OscProbCalcerImplementationToCreate == "ProbGPULinear") {
#if UseProbGPULinear==1
OscProbCalcerProbGPULinear* ProbGPULinear = new OscProbCalcerProbGPULinear(OscProbCalcerConfigName,Instance);
Calcer = (OscProbCalcerBase*)ProbGPULinear;
if (Verbose >= INFO) {std::cout << "Initalised OscProbCalcer Implementation:" << Calcer->ReturnImplementationName() << " in OscillatorBase object" << std::endl;}
#else
std::cerr << "Oscillator was requsted to create " << OscProbCalcerImplementationToCreate << " OscProbCalcer but Use" << OscProbCalcerImplementationToCreate << " is undefined. Indicates problem in setup" << std::endl;
throw;
#endif
}

else if (OscProbCalcerImplementationToCreate == "NuFASTLinear") {
#if UseNuFASTLinear==1
OscProbCalcerNuFASTLinear* NuFASTLinear = new OscProbCalcerNuFASTLinear(OscProbCalcerConfigName,Instance);
Calcer = (OscProbCalcerBase*)NuFASTLinear;
if (Verbose >= INFO) {std::cout << "Initalised OscProbCalcer Implementation:" << Calcer->ReturnImplementationName() << " in OscillatorBase object" << std::endl;}
#else
std::cerr << "Oscillator was requsted to create " << OscProbCalcerImplementationToCreate << " OscProbCalcer but Use" << OscProbCalcerImplementationToCreate << " is undefined. Indicates problem in setup" << std::endl;
throw;
#endif
}

else {
std::cerr << "Oscillator was requsted to create " << OscProbCalcerImplementationToCreate << " OscProbCalcer but this is not implemented within " << __FILE__ << std::endl;
std::cerr << "Please correct the mistake or implement the new OscProbCalcer" << std::endl;
throw;
}

return Calcer;
}
40 changes: 40 additions & 0 deletions OscProbCalcer/OscProbCalcerFactory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef __OSCPROBCALCERFACTORY_H__
#define __OSCPROBCALCERFACTORY_H__

#include "OscProbCalcerBase.h"

/**
* @file OscProbCalcerFactory.h
*
* @class OscProbCalcerFactory
*
* @brief Factory method for creating instances of OscProbCalcerBase::OscProbCalcerBase() obejcts
*/
class OscProbCalcerFactory {
public:

/**
* @brief Default constructor
*/
OscProbCalcerFactory();

/**
* @brief Destructor
*/
virtual ~OscProbCalcerFactory();

/**
* @brief Create an instance of OscProbCalcerBase::OscProbCalcerBase() objects from a YAML config.
*
* @param ConfigName_ Path to YAML config file
*
* @return OscillatorBase::OscillatorBase() object
*/
OscProbCalcerBase* CreateOscProbCalcer(std::string OscProbCalcerImplementationToCreate, std::string OscProbCalcerConfigName_, int Instance, int Verbose=INFO);

protected:

private:
};

#endif
85 changes: 3 additions & 82 deletions Oscillator/OscillatorBase.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
#include "Oscillator/OscillatorBase.h"

#if UseCUDAProb3==1
#include "OscProbCalcer/OscProbCalcer_CUDAProb3.h"
#endif

#if UseCUDAProb3Linear==1
#include "OscProbCalcer/OscProbCalcer_CUDAProb3Linear.h"
#endif

#if UseProb3ppLinear==1
#include "OscProbCalcer/OscProbCalcer_Prob3ppLinear.h"
#endif

#if UseProbGPULinear==1
#include "OscProbCalcer/OscProbCalcer_ProbGPULinear.h"
#endif

#if UseNuFASTLinear==1
#include "OscProbCalcer/OscProbCalcer_NuFASTLinear.h"
#endif
#include "OscProbCalcer/OscProbCalcerFactory.h"

#include <iostream>

Expand Down Expand Up @@ -125,69 +107,8 @@ void OscillatorBase::InitialiseOscProbCalcers() {
}

OscProbCalcerBase* OscillatorBase::InitialiseOscProbCalcer(std::string OscProbCalcerImplementationToCreate, std::string OscProbCalcerConfigname, int Instance) {
OscProbCalcerBase* Calcer;

if (OscProbCalcerImplementationToCreate == "CUDAProb3") {
#if UseCUDAProb3==1
OscProbCalcerCUDAProb3* CUDAProb3 = new OscProbCalcerCUDAProb3(OscProbCalcerConfigname,Instance);
Calcer = (OscProbCalcerBase*)CUDAProb3;
if (fVerbose >= INFO) {std::cout << "Initalised OscProbCalcer Implementation:" << Calcer->ReturnImplementationName() << " in OscillatorBase object" << std::endl;}
#else
std::cerr << "Oscillator was requsted to create " << OscProbCalcerImplementationToCreate << " OscProbCalcer but Use" << OscProbCalcerImplementationToCreate << " is undefined. Indicates problem in setup" << std::endl;
throw;
#endif
}

else if (OscProbCalcerImplementationToCreate == "CUDAProb3Linear") {
#if UseCUDAProb3Linear==1
OscProbCalcerCUDAProb3Linear* CUDAProb3Linear = new OscProbCalcerCUDAProb3Linear(OscProbCalcerConfigname,Instance);
Calcer = (OscProbCalcerBase*)CUDAProb3Linear;
if (fVerbose >= INFO) {std::cout << "Initalised OscProbCalcer Implementation:" << Calcer->ReturnImplementationName() << " in OscillatorBase object" << std::endl;}
#else
std::cerr << "Oscillator was requsted to create " << OscProbCalcerImplementationToCreate << " OscProbCalcer but Use" << OscProbCalcerImplementationToCreate << " is undefined. Indicates problem in setup" << std::endl;
throw;
#endif
}

else if (OscProbCalcerImplementationToCreate == "Prob3ppLinear") {
#if UseProb3ppLinear==1
OscProbCalcerProb3ppLinear* Prob3ppLinear = new OscProbCalcerProb3ppLinear(OscProbCalcerConfigname,Instance);
Calcer = (OscProbCalcerBase*)Prob3ppLinear;
if (fVerbose >= INFO) {std::cout << "Initalised OscProbCalcer Implementation:" << Calcer->ReturnImplementationName() << " in OscillatorBase object" << std::endl;}
#else
std::cerr << "Oscillator was requsted to create " << OscProbCalcerImplementationToCreate << " OscProbCalcer but Use" << OscProbCalcerImplementationToCreate << " is undefined. Indicates problem in setup" << std::endl;
throw;
#endif
}

else if (OscProbCalcerImplementationToCreate == "ProbGPULinear") {
#if UseProbGPULinear==1
OscProbCalcerProbGPULinear* ProbGPULinear = new OscProbCalcerProbGPULinear(OscProbCalcerConfigname,Instance);
Calcer = (OscProbCalcerBase*)ProbGPULinear;
if (fVerbose >= INFO) {std::cout << "Initalised OscProbCalcer Implementation:" << Calcer->ReturnImplementationName() << " in OscillatorBase object" << std::endl;}
#else
std::cerr << "Oscillator was requsted to create " << OscProbCalcerImplementationToCreate << " OscProbCalcer but Use" << OscProbCalcerImplementationToCreate << " is undefined. Indicates problem in setup" << std::endl;
throw;
#endif
}

else if (OscProbCalcerImplementationToCreate == "NuFASTLinear") {
#if UseNuFASTLinear==1
OscProbCalcerNuFASTLinear* NuFASTLinear = new OscProbCalcerNuFASTLinear(OscProbCalcerConfigname,Instance);
Calcer = (OscProbCalcerBase*)NuFASTLinear;
if (fVerbose >= INFO) {std::cout << "Initalised OscProbCalcer Implementation:" << Calcer->ReturnImplementationName() << " in OscillatorBase object" << std::endl;}
#else
std::cerr << "Oscillator was requsted to create " << OscProbCalcerImplementationToCreate << " OscProbCalcer but Use" << OscProbCalcerImplementationToCreate << " is undefined. Indicates problem in setup" << std::endl;
throw;
#endif
}

else {
std::cerr << "Oscillator was requsted to create " << OscProbCalcerImplementationToCreate << " OscProbCalcer but this is not implemented within " << __FILE__ << std::endl;
std::cerr << "Please correct the mistake or implement the new OscProbCalcer" << std::endl;
throw;
}

OscProbCalcerFactory* OscProbCalcFactory = new OscProbCalcerFactory();
OscProbCalcerBase* Calcer = OscProbCalcFactory->CreateOscProbCalcer(OscProbCalcerImplementationToCreate,OscProbCalcerConfigname,Instance,fVerbose);
return Calcer;
}

Expand Down