Skip to content
Snippets Groups Projects
Commit 53202173 authored by Chris Cantwell's avatar Chris Cantwell
Browse files

Separation of C++ & CUDA with conditional compile.

parent 02ef9bb8
No related branches found
No related tags found
No related merge requests found
build
cmake_minimum_required(VERSION 3.0)
project(Redesign CXX)
option(NEKTAR_USE_CUDA "Enable CUDA support" FALSE)
set(SRC main.cpp Operator.cpp)
if (NEKTAR_USE_CUDA)
enable_language(CUDA)
set(SRC ${SRC} Operator.cu)
endif()
add_executable(main ${SRC})
\ No newline at end of file
///////////////////////////////////////////////////////////////////////////////
//
// File ErrorUtil.hpp
//
// For more information, please see: http://www.nektar.info
//
// The MIT License
//
// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
// Department of Aeronautics, Imperial College London (UK), and Scientific
// Computing and Imaging Institute, University of Utah (USA).
//
// 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.
//
// Description: error related utilities
//
///////////////////////////////////////////////////////////////////////////////
#ifndef ERRORUTIL_HPP
#define ERRORUTIL_HPP
#include <iostream>
#include <stdexcept>
#include <string>
#include <boost/core/ignore_unused.hpp>
//#include <LibUtilities/LibUtilitiesDeclspec.h>
#define LIB_UTILITIES_EXPORT
#if defined(NEKTAR_USE_MPI)
#include <mpi.h>
#endif
#ifndef _WIN32
#include <execinfo.h>
#endif
namespace Nektar
{
class ErrorUtil
{
public:
class NekError : public std::runtime_error
{
public:
NekError(const std::string& message) : std::runtime_error(message)
{
}
};
enum ErrType
{
efatal,
ewarning
};
inline static void SetErrorStream(std::ostream& o)
{
m_outStream = &o;
}
inline static void SetPrintBacktrace(bool b)
{
m_printBacktrace = b;
}
inline static bool HasCustomErrorStream()
{
return m_outStream != &std::cerr;
}
inline static void Error(ErrType type,
const char *routine,
int lineNumber,
const char *msg,
unsigned int level,
bool DoComm = false)
{
boost::ignore_unused(DoComm);
// The user of outStream is primarily for the unit tests. The unit
// tests often generate errors on purpose to make sure invalid usage is
// flagged appropriately. Printing the error messages to cerr made the
// unit test output hard to parse.
std::string baseMsg = "Level " + std::to_string(level) +
" assertion violation\n";
#if defined(NEKTAR_DEBUG) || defined(NEKTAR_FULLDEBUG)
baseMsg += "Where : " + std::string(routine) + "[" +
std::to_string(lineNumber) + "]\nMessage : ";
#else
boost::ignore_unused(routine, lineNumber);
#endif
baseMsg += std::string(msg);
// Default rank is zero. If MPI used and initialised, populate with
// the correct rank. Messages are only printed on rank zero.
int rank = 0;
#if defined(NEKTAR_USE_MPI) && !defined(NEKTAR_USE_CWIPI)
int flag = 0;
if(DoComm)
{
MPI_Initialized(&flag);
if(flag)
{
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
}
}
#else
boost::ignore_unused(DoComm);
#endif
std::string btMessage("");
#if defined(NEKTAR_FULLDEBUG)
#ifndef _WIN32
if (m_printBacktrace)
{
void *btArray[40];
int btSize;
char **btStrings;
btSize = backtrace(btArray, 40);
btStrings = backtrace_symbols(btArray, btSize);
for (int i = 0 ; i < btSize ; ++i)
{
btMessage += std::string(btStrings[i]) + "\n";
}
free(btStrings);
}
#endif
#endif
switch (type)
{
case efatal:
if (!rank)
{
if (m_printBacktrace)
{
(*m_outStream) << btMessage;
}
(*m_outStream) << "Fatal : " << baseMsg << std::endl;
}
#if defined(NEKTAR_USE_MPI) && !defined(NEKTAR_USE_CWIPI)
if(DoComm)
{
if (flag)
{
MPI_Barrier(MPI_COMM_WORLD);
}
}
#endif
throw NekError(baseMsg);
break;
case ewarning:
if (!rank)
{
if (m_printBacktrace)
{
(*m_outStream) << btMessage;
}
(*m_outStream) << "Warning : " << baseMsg << std::endl;
}
break;
default:
(*m_outStream) << "Unknown warning type: " << baseMsg << std::endl;
}
}
inline static void Error(ErrType type, const char *routine, int lineNumber, const std::string& msg, unsigned int level)
{
Error(type, routine, lineNumber, msg.c_str(), level);
}
inline static void Error(ErrType type, const char *routine, int lineNumber, const char *msg)
{
Error(type, routine, lineNumber, msg, 0);
}
private:
LIB_UTILITIES_EXPORT static std::ostream *m_outStream;
LIB_UTILITIES_EXPORT static bool m_printBacktrace;
};
/// Assert Level 0 -- Fundamental assert which
/// is used whether in FULLDEBUG, DEBUG or OPT
/// compilation mode. This level assert is
/// considered code critical, even under
/// optimized compilation.
#define NEKERROR(type, msg) \
Nektar::ErrorUtil::Error(type, __FILE__, __LINE__, msg, 0);
#define ROOTONLY_NEKERROR(type, msg) \
Nektar::ErrorUtil::Error(type, __FILE__, __LINE__, msg, 0, true);
#define ASSERTL0(condition,msg) \
if(!(condition)) \
{ \
Nektar::ErrorUtil::Error( \
Nektar::ErrorUtil::efatal, __FILE__, __LINE__, msg, 0); \
}
#define WARNINGL0(condition,msg) \
if(!(condition)) \
{ \
Nektar::ErrorUtil::Error( \
Nektar::ErrorUtil::ewarning, __FILE__, __LINE__, msg, 0); \
}
/// Assert Level 1 -- Debugging which is used whether in FULLDEBUG or
/// DEBUG compilation mode. This level assert is designed for aiding
/// in standard debug (-g) mode
#if defined(NEKTAR_DEBUG) || defined(NEKTAR_FULLDEBUG)
#define ASSERTL1(condition,msg) \
if(!(condition)) \
{ \
Nektar::ErrorUtil::Error( \
Nektar::ErrorUtil::efatal, __FILE__, __LINE__, msg, 1); \
}
#define WARNINGL1(condition,msg) \
if(!(condition)) \
{ \
Nektar::ErrorUtil::Error( \
Nektar::ErrorUtil::ewarning, __FILE__, __LINE__, msg, 1); \
}
#else //defined(NEKTAR_DEBUG) || defined(NEKTAR_FULLDEBUG)
#define ASSERTL1(condition,msg)
#define WARNINGL1(condition,msg)
#endif //defined(NEKTAR_DEBUG) || defined(NEKTAR_FULLDEBUG)
/// Assert Level 2 -- Debugging which is used FULLDEBUG compilation
/// mode. This level assert is designed to provide addition safety
/// checks within the code (such as bounds checking, etc.).
#ifdef NEKTAR_FULLDEBUG
#define ASSERTL2(condition,msg) \
if(!(condition)) \
{ \
Nektar::ErrorUtil::Error( \
Nektar::ErrorUtil::efatal, __FILE__, __LINE__, msg, 2); \
}
#define WARNINGL2(condition,msg) \
if(!(condition)) \
{ \
Nektar::ErrorUtil::Error( \
Nektar::ErrorUtil::ewarning, __FILE__, __LINE__, msg, 2); \
}
#else //NEKTAR_FULLDEBUG
#define ASSERTL2(condition,msg)
#define WARNINGL2(condition,msg)
#endif //NEKTAR_FULLDEBUG
}
#endif //ERRORUTIL_HPP
///////////////////////////////////////////////////////////////////////////////
//
// File: NekFactory.hpp
//
// For more information, please see: http://www.nektar.info
//
// The MIT License
//
// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
// Department of Aeronautics, Imperial College London (UK), and Scientific
// Computing and Imaging Institute, University of Utah (USA).
//
// 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.
//
// Description: Factory pattern class for Nektar
//
///////////////////////////////////////////////////////////////////////////////
#ifndef NEKTAR_LIBUTILITIES_BASICUTILS_NEKFACTORY
#define NEKTAR_LIBUTILITIES_BASICUTILS_NEKFACTORY
#include <iostream>
#include <map>
#include <string>
#include <sstream>
#include <memory>
#include <functional>
#ifdef NEKTAR_USE_THREAD_SAFETY
#include <boost/thread/shared_mutex.hpp>
#include <boost/thread/locks.hpp>
#endif
#include "ErrorUtil.hpp"
namespace Nektar
{
namespace LibUtilities
{
#ifdef NEKTAR_USE_THREAD_SAFETY
// Generate parameter typenames with default type of 'none'
typedef boost::unique_lock<boost::shared_mutex> WriteLock;
typedef boost::shared_lock<boost::shared_mutex> ReadLock;
#endif
/**
* @class NekFactory
*
* @brief Provides a generic Factory class.
*
* Implements a generic object factory. Class-types which use an arbitrary
* number of parameters may be used via C++ variadic templating.
*
* To allow a class to be instantiated by the factory, the following are
* required in each class definition (in the case of a single parameter):
*
* \code
* static [baseclass]* create([paramtype1] &P) {
* return new [derivedclass](P);
* }
* static std::string className;
* \endcode
*
* and outside the class definition in the implementation:
*
* \code
* std::string [derivedclass]::className
* = Factory<std::string,[baseclass],[paramtype1]>::
* RegisterCreatorFunction("[derivedclass]",
* [derivedclass]::create,"Description");
* \endcode
*
* The assignment of the static variable className is done through the call to
* RegisterCreatorFunction, which registers the class with the factory prior to
* the start of the main() routine.
*
* To create an instance of a derived class, for instance:
* \code
* [baseclass]* var_name =
* Factory<std::string,[baseclass],[paramtype1]>
* ::CreateInstance("[derivedclass]",Param1);
* \endcode
*/
template <typename tKey, // reference tag (e.g. string, int)
typename tBase, // base class
typename... tParam>
class NekFactory
{
public:
/// Comparison predicator of key
typedef std::less<tKey> tPredicator;
/// Shared pointer to an object of baseclass type.
typedef std::unique_ptr<tBase> tBaseUniquePtr;
/// CreatorFunction type which takes parameter and returns base class shared
/// pointer.
typedef std::function<tBaseUniquePtr(tParam...)> CreatorFunction;
/// Define a struct to hold the information about a module.
struct ModuleEntry
{
ModuleEntry(CreatorFunction pFunc, const std::string pDesc)
: m_func(pFunc),
m_desc(pDesc)
{
}
/// Function used to create instance of class.
CreatorFunction m_func;
/// Description of class for use in listing available classes.
std::string m_desc;
};
/// Factory map between key and module data.
typedef std::map<tKey, ModuleEntry, tPredicator> TMapFactory;
public:
NekFactory() = default;
/**
* @brief Create an instance of the class referred to by \c idKey.
*
* Searches the factory's map for the given key and returns a shared
* base class pointer to a new instance of the associated class.
* @param idKey Key of class to create.
* @param x Parameter to pass to class constructor.
* @returns Base class pointer to new instance.
*/
tBaseUniquePtr CreateInstance(tKey idKey, tParam... args)
{
#ifdef NEKTAR_USE_THREAD_SAFETY
ReadLock vReadLock(m_mutex);
#endif
// Now try and find the key in the map.
auto it = getMapFactory()->find(idKey);
// If successful, check the CreatorFunction is defined and
// create a new instance of the class.
if (it != getMapFactory()->end())
{
ModuleEntry *tmp = &(it->second);
#ifdef NEKTAR_USE_THREAD_SAFETY
vReadLock.unlock();
#endif
if (tmp->m_func)
{
try
{
return tmp->m_func(args...);
}
catch (const std::string& s)
{
std::stringstream errstr;
errstr << "Unable to create module: " << idKey << "\n";
errstr << s;
NEKERROR(ErrorUtil::efatal, errstr.str());
}
}
}
// If we get this far, the key doesn't exist, so throw an error.
std::stringstream errstr;
errstr << "No such module: " << idKey << std::endl;
PrintAvailableClasses(errstr);
NEKERROR(ErrorUtil::efatal, errstr.str());
return tBaseUniquePtr();
}
/**
* @brief Register a class with the factory.
*
* This function is called by each class in a static context (prior
* to the execution of main()) and creates an entry for the class
* in the factory's map.
* @param idKey Key used to reference the class.
* @param classCreator Function to call to create an instance
* of this class.
* @param pDesc Optional description of class.
* @returns The given key \c idKey.
*/
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator,
std::string pDesc = "")
{
#ifdef NEKTAR_USE_THREAD_SAFETY
WriteLock vWriteLock(m_mutex);
#endif
ModuleEntry e(classCreator, pDesc);
getMapFactory()->insert(std::pair<tKey,ModuleEntry>(idKey, e));
return idKey;
}
/**
* @brief Checks if a particular module is available.
*/
bool ModuleExists(tKey idKey)
{
#ifdef NEKTAR_USE_THREAD_SAFETY
ReadLock vReadLock(m_mutex);
#endif
// Now try and find the key in the map.
auto it = getMapFactory()->find(idKey);
if (it != getMapFactory()->end())
{
return true;
}
return false;
}
/**
* @brief Prints the available classes to stdout.
*/
void PrintAvailableClasses(std::ostream& pOut = std::cout)
{
#ifdef NEKTAR_USE_THREAD_SAFETY
ReadLock vReadLock(m_mutex);
#endif
pOut << std::endl << "Available classes: " << std::endl;
for (auto &it : *getMapFactory())
{
pOut << " " << it.first;
if (it.second.m_desc != "")
{
pOut << ":" << std::endl << " "
<< it.second.m_desc << std::endl;
}
else
{
pOut << std::endl;
}
}
}
/**
* @brief Retrieves a key, given a description
*/
tKey GetKey(std::string pDesc)
{
#ifdef NEKTAR_USE_THREAD_SAFETY
ReadLock vReadLock(m_mutex);
#endif
for (auto &it : *getMapFactory())
{
if (it.second.m_desc == pDesc)
{
return it.first;
}
}
std::string errstr = "Module '" + pDesc + "' is not known.";
ASSERTL0(false, errstr);
}
/**
* @brief Returns the description of a class
*/
std::string GetClassDescription(tKey idKey)
{
#ifdef NEKTAR_USE_THREAD_SAFETY
ReadLock vReadLock(m_mutex);
#endif
// Now try and find the key in the map.
auto it = getMapFactory()->find(idKey);
std::stringstream errstr;
errstr << "No such module: " << idKey << std::endl;
ASSERTL0 (it != getMapFactory()->end(), errstr.str());
return it->second.m_desc;
}
protected:
/**
* @brief Ensure the factory's map is created.
* @returns The factory's map.
*/
TMapFactory* getMapFactory()
{
return &mMapFactory;
}
private:
NekFactory(const NekFactory& rhs);
NekFactory& operator=(const NekFactory& rhs);
TMapFactory mMapFactory;
#ifdef NEKTAR_USE_THREAD_SAFETY
boost::shared_mutex m_mutex;
#endif
};
}
}
#endif
#include "OperatorBwdTrans.hpp"
template<typename TData>
OperatorFactory<TData> &GetOperatorFactory() {
static OperatorFactory<TData> instance;
return instance;
}
std::string OpBwdTransLocMatCPU =
GetOperatorFactory<double>().RegisterCreatorFunction(
"BwdTransLocMatCPU",
Operator<double, OpBwdTrans, MethodLocMat, DeviceCPU>::create);
\ No newline at end of file
#include <memory>
#include <iostream>
#include "NekFactory.hpp"
#include "Field.hpp"
typedef double NekDouble;
......@@ -12,7 +13,7 @@ struct OpPhysDeriv;
struct MethodLocMat;
struct MethodSumFac;
struct MethodMatFree;
using DefaultMethod = MethodSumFac;
using DefaultMethod = MethodLocMat;
// Base class
template<typename TData>
......@@ -22,6 +23,14 @@ class OperatorBase
virtual void apply(TData in, TData out) = 0;
};
using namespace Nektar::LibUtilities;
template<typename TData>
using OperatorFactory = NekFactory<std::string, OperatorBase<TData>>;
template<typename TData>
OperatorFactory<TData> &GetOperatorFactory();
// Templated Operator
template<typename TData,
typename TOp,
......@@ -30,39 +39,3 @@ template<typename TData,
class Operator;
template<typename TData>
class Operator<TData, OpBwdTrans, MethodLocMat, DeviceCPU>
: public OperatorBase<TData>
{
public:
using ClassType = Operator<TData, OpBwdTrans, MethodLocMat, DeviceCPU>;
static std::unique_ptr<ClassType> create()
{
return std::unique_ptr<ClassType>(new ClassType());
}
void apply(TData in, TData out) override
{
std::cout << "Perform BwdTrans op with LocMat" << std::endl;
}
};
template<typename TData>
class Operator<TData, OpBwdTrans, MethodMatFree, DeviceCPU>
: public OperatorBase<TData>
{
public:
using ClassType = Operator<TData, OpBwdTrans, MethodMatFree, DeviceCPU>;
static std::unique_ptr<ClassType> create()
{
return std::unique_ptr<ClassType>(new ClassType());
}
void apply(TData in, TData out)
{
std::cout << "Perform BwdTrans op with MatFree" << std::endl;
}
};
// Specialisations for CUDA-based BwdTrans implementations
#include "Operator.hpp"
// BwdTrans mat-free operator
template<typename TData>
class Operator<TData, OpBwdTrans, MethodMatFree, DeviceCUDA>
: public OperatorBase<TData>
{
public:
using ClassType = Operator<TData, OpBwdTrans, MethodMatFree, DeviceCUDA>;
static std::unique_ptr<ClassType> create()
{
return std::unique_ptr<ClassType>(new ClassType());
}
void apply(TData in, TData out)
{
std::cout << "Perform BwdTrans op with CUDA" << std::endl;
}
};
\ No newline at end of file
// Specialisations for CPU-based BwdTrans implementations
#include "Operator.hpp"
// BwdTrans local matrix operator
template<typename TData>
class Operator<TData, OpBwdTrans, MethodLocMat, DeviceCPU>
: public OperatorBase<TData>
{
public:
using ClassType = Operator<TData, OpBwdTrans, MethodLocMat, DeviceCPU>;
static std::unique_ptr<ClassType> create()
{
return std::unique_ptr<ClassType>(new ClassType());
}
void apply(TData in, TData out) override
{
std::cout << "Perform BwdTrans op with LocMat" << std::endl;
}
};
// BwdTrans mat-free operator
template<typename TData>
class Operator<TData, OpBwdTrans, MethodMatFree, DeviceCPU>
: public OperatorBase<TData>
{
public:
using ClassType = Operator<TData, OpBwdTrans, MethodMatFree, DeviceCPU>;
static std::unique_ptr<ClassType> create()
{
return std::unique_ptr<ClassType>(new ClassType());
}
void apply(TData in, TData out)
{
std::cout << "Perform BwdTrans op with MatFree" << std::endl;
// Quad
// size_t nElmt = 1000;
// size_t nVW = 4;
// size_t nGrpsQ = nElmt / nVW;
// auto o = OpKernel<TData, OpBwdTrans, MethodLocMat, DeviceCPU, NQ0, NQ1, NP0, NP1>
// for (int i = 0; i < nGrpsQ; ++i) {
// }
}
};
......@@ -4,7 +4,7 @@
#include <typeinfo>
using namespace std;
#include "Operator.hpp"
#include "OperatorBwdTrans.hpp"
int main() {
using DefaultMethod = MethodLocMat;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment