Skip to content

Commit

Permalink
Driver methods (#877)
Browse files Browse the repository at this point in the history
Add drivers for each gtclang and dawn component and uses these in the executables. This does add code, but that is mostly in separating options, and more code will be eliminated once only these drivers are used (follow-up PR).
  • Loading branch information
jdahm authored Apr 1, 2020
1 parent cee0e3d commit cf3e289
Show file tree
Hide file tree
Showing 34 changed files with 681 additions and 270 deletions.
3 changes: 3 additions & 0 deletions dawn/src/dawn/CodeGen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ add_library(DawnCodeGen
Cuda/ASTStencilFunctionParamVisitor.h
Cuda/MSCodeGen.cpp
Cuda/MSCodeGen.h
Driver.cpp
Driver.h
GridTools/ASTStencilBody.cpp
GridTools/ASTStencilBody.h
GridTools/ASTStencilDesc.cpp
Expand All @@ -58,6 +60,7 @@ add_library(DawnCodeGen
GridTools/CodeGenUtils.h
GridTools/GTCodeGen.cpp
GridTools/GTCodeGen.h
Options.h
Options.inc
StencilFunctionAsBCGenerator.cpp
StencilFunctionAsBCGenerator.h
Expand Down
17 changes: 16 additions & 1 deletion dawn/src/dawn/CodeGen/CXXNaive-ico/CXXNaiveCodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,22 @@ std::string makeKLoop(bool isBackward, iir::Interval const& interval) {
}
} // namespace

CXXNaiveIcoCodeGen::CXXNaiveIcoCodeGen(const stencilInstantiationContext& ctx,
std::unique_ptr<TranslationUnit>
run(const std::map<std::string, std::shared_ptr<iir::StencilInstantiation>>&
stencilInstantiationMap,
const Options& options) {
DiagnosticsEngine diagnostics;
CXXNaiveIcoCodeGen CG(stencilInstantiationMap, diagnostics, options.MaxHaloSize);
if(diagnostics.hasDiags()) {
for(const auto& diag : diagnostics.getQueue())
DAWN_LOG(INFO) << diag->getMessage();
throw std::runtime_error("An error occured in code generation");
}

return CG.generateCode();
}

CXXNaiveIcoCodeGen::CXXNaiveIcoCodeGen(const StencilInstantiationContext& ctx,
DiagnosticsEngine& engine, int maxHaloPoint)
: CodeGen(ctx, engine, maxHaloPoint) {}

Expand Down
9 changes: 8 additions & 1 deletion dawn/src/dawn/CodeGen/CXXNaive-ico/CXXNaiveCodeGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "dawn/CodeGen/CodeGen.h"
#include "dawn/CodeGen/CodeGenProperties.h"
#include "dawn/CodeGen/Options.h"
#include "dawn/IIR/Interval.h"
#include "dawn/Support/IndexRange.h"
#include <set>
Expand All @@ -32,12 +33,18 @@ class StencilInstantiation;
namespace codegen {
namespace cxxnaiveico {

/// @brief Run the cxx-naive-ico code generation
std::unique_ptr<TranslationUnit>
run(const std::map<std::string, std::shared_ptr<iir::StencilInstantiation>>&
stencilInstantiationMap,
const Options& options = {});

/// @brief GridTools C++ code generation for the gtclang DSL
/// @ingroup cxxnaiveico
class CXXNaiveIcoCodeGen : public CodeGen {
public:
///@brief constructor
CXXNaiveIcoCodeGen(const stencilInstantiationContext& ctx, DiagnosticsEngine& engine,
CXXNaiveIcoCodeGen(const StencilInstantiationContext& ctx, DiagnosticsEngine& engine,
int maxHaloPoint);
virtual ~CXXNaiveIcoCodeGen();
virtual std::unique_ptr<TranslationUnit> generateCode() override;
Expand Down
17 changes: 16 additions & 1 deletion dawn/src/dawn/CodeGen/CXXNaive/CXXNaiveCodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,22 @@ std::string makeKLoop(bool isBackward, iir::Interval const& interval) {
}
} // namespace

CXXNaiveCodeGen::CXXNaiveCodeGen(const stencilInstantiationContext& ctx, DiagnosticsEngine& engine,
std::unique_ptr<TranslationUnit>
run(const std::map<std::string, std::shared_ptr<iir::StencilInstantiation>>&
stencilInstantiationMap,
const Options& options) {
DiagnosticsEngine diagnostics;
CXXNaiveCodeGen CG(stencilInstantiationMap, diagnostics, options.MaxHaloSize);
if(diagnostics.hasDiags()) {
for(const auto& diag : diagnostics.getQueue())
DAWN_LOG(INFO) << diag->getMessage();
throw std::runtime_error("An error occured in code generation");
}

return CG.generateCode();
}

CXXNaiveCodeGen::CXXNaiveCodeGen(const StencilInstantiationContext& ctx, DiagnosticsEngine& engine,
int maxHaloPoint)
: CodeGen(ctx, engine, maxHaloPoint) {}

Expand Down
9 changes: 8 additions & 1 deletion dawn/src/dawn/CodeGen/CXXNaive/CXXNaiveCodeGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "dawn/CodeGen/CodeGen.h"
#include "dawn/CodeGen/CodeGenProperties.h"
#include "dawn/CodeGen/Options.h"
#include "dawn/IIR/Interval.h"
#include "dawn/Support/IndexRange.h"
#include <set>
Expand All @@ -32,12 +33,18 @@ class StencilInstantiation;
namespace codegen {
namespace cxxnaive {

/// @brief Run the cxx-naive code generation
std::unique_ptr<TranslationUnit>
run(const std::map<std::string, std::shared_ptr<iir::StencilInstantiation>>&
stencilInstantiationMap,
const Options& options = {});

/// @brief GridTools C++ code generation for the gtclang DSL
/// @ingroup cxxnaive
class CXXNaiveCodeGen : public CodeGen {
public:
///@brief constructor
CXXNaiveCodeGen(const stencilInstantiationContext& ctx, DiagnosticsEngine& engine,
CXXNaiveCodeGen(const StencilInstantiationContext& ctx, DiagnosticsEngine& engine,
int maxHaloPoint);
virtual ~CXXNaiveCodeGen();
virtual std::unique_ptr<TranslationUnit> generateCode() override;
Expand Down
22 changes: 18 additions & 4 deletions dawn/src/dawn/CodeGen/CodeGen.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
//===--------------------------------------------------------------------------------*- C++ -*-===//
// _
// | |
// __| | __ ___ ___ ___
// / _` |/ _` \ \ /\ / / '_ |
// | (_| | (_| |\ V V /| | | |
// \__,_|\__,_| \_/\_/ |_| |_| - Compiler Toolchain
//
//
// This file is distributed under the MIT License (MIT).
// See LICENSE.txt for details.
//
//===------------------------------------------------------------------------------------------===//

#include "dawn/CodeGen/CodeGen.h"
#include "dawn/CodeGen/StencilFunctionAsBCGenerator.h"
#include "dawn/IIR/Extents.h"
Expand All @@ -6,7 +20,7 @@
namespace dawn {
namespace codegen {

CodeGen::CodeGen(const stencilInstantiationContext& ctx, DiagnosticsEngine& engine,
CodeGen::CodeGen(const StencilInstantiationContext& ctx, DiagnosticsEngine& engine,
int maxHaloPoints)
: context_(ctx), diagEngine(engine), codeGenOptions{maxHaloPoints} {}

Expand All @@ -30,7 +44,7 @@ size_t CodeGen::getVerticalTmpHaloSizeForMultipleStencils(
return fullIntervals ? std::max(fullIntervals->overEnd(), fullIntervals->belowBegin()) : 0;
}

std::string CodeGen::generateGlobals(const stencilInstantiationContext& context,
std::string CodeGen::generateGlobals(const StencilInstantiationContext& context,
std::string outer_namespace_, std::string inner_namespace_) {

std::stringstream ss;
Expand All @@ -43,7 +57,7 @@ std::string CodeGen::generateGlobals(const stencilInstantiationContext& context,
return ss.str();
}

std::string CodeGen::generateGlobals(const stencilInstantiationContext& context,
std::string CodeGen::generateGlobals(const StencilInstantiationContext& context,
std::string namespace_) {
if(context.size() > 0) {
const auto& globalsMap = context.begin()->second->getIIR()->getGlobalVariableMap();
Expand Down Expand Up @@ -382,7 +396,7 @@ void CodeGen::addMplIfdefs(std::vector<std::string>& ppDefines, int mplContainer
makeIfNotDefinedString("BOOST_MPL_LIMIT_VECTOR_SIZE", "GT_VECTOR_LIMIT_SIZE"));
}

std::string CodeGen::generateFileName(const stencilInstantiationContext& context) const {
std::string CodeGen::generateFileName(const StencilInstantiationContext& context) const {
if(context.size() > 0) {
return context_.begin()->second->getMetaData().getFileName();
}
Expand Down
12 changes: 6 additions & 6 deletions dawn/src/dawn/CodeGen/CodeGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
namespace dawn {
namespace codegen {

using stencilInstantiationContext =
using StencilInstantiationContext =
std::map<std::string, std::shared_ptr<iir::StencilInstantiation>>;

/// @brief Interface of the backend code generation
/// @ingroup codegen
class CodeGen {
protected:
const stencilInstantiationContext& context_;
const StencilInstantiationContext& context_;
DiagnosticsEngine& diagEngine;
struct codeGenOption {
int MaxHaloPoints;
Expand Down Expand Up @@ -78,7 +78,7 @@ class CodeGen {
const std::string bigWrapperMetadata_ = "m_meta_data";

public:
CodeGen(const stencilInstantiationContext& ctx, DiagnosticsEngine& engine, int maxHaloPoints);
CodeGen(const StencilInstantiationContext& ctx, DiagnosticsEngine& engine, int maxHaloPoints);
virtual ~CodeGen() {}

/// @brief Generate code
Expand All @@ -99,16 +99,16 @@ class CodeGen {
Class& stencilWrapperClass,
const sir::GlobalVariableMap& globalsMap,
const CodeGenProperties& codeGenProperties) const;
virtual std::string generateGlobals(const stencilInstantiationContext& context,
virtual std::string generateGlobals(const StencilInstantiationContext& context,
std::string namespace_);
virtual std::string generateGlobals(const stencilInstantiationContext& context,
virtual std::string generateGlobals(const StencilInstantiationContext& context,
std::string outer_namespace_, std::string inner_namespace_);
virtual std::string generateGlobals(const sir::GlobalVariableMap& globalsMaps,
std::string namespace_) const;

void generateBCHeaders(std::vector<std::string>& ppDefines) const;

std::string generateFileName(const stencilInstantiationContext& context) const;
std::string generateFileName(const StencilInstantiationContext& context) const;
};

} // namespace codegen
Expand Down
19 changes: 18 additions & 1 deletion dawn/src/dawn/CodeGen/Cuda/CudaCodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,24 @@ std::string makeIntervalBoundExplicit(std::string dim, const iir::Interval& inte
}
} // namespace

CudaCodeGen::CudaCodeGen(const stencilInstantiationContext& ctx, DiagnosticsEngine& engine,
std::unique_ptr<TranslationUnit>
run(const std::map<std::string, std::shared_ptr<iir::StencilInstantiation>>&
stencilInstantiationMap,
const Options& options) {
DiagnosticsEngine diagnostics;
const Array3i domain_size{options.DomainSizeI, options.DomainSizeJ, options.DomainSizeK};
CudaCodeGen CG(stencilInstantiationMap, diagnostics, options.MaxHaloSize, options.nsms,
options.MaxBlocksPerSM, domain_size);
if(diagnostics.hasDiags()) {
for(const auto& diag : diagnostics.getQueue())
DAWN_LOG(INFO) << diag->getMessage();
throw std::runtime_error("An error occured in code generation");
}

return CG.generateCode();
}

CudaCodeGen::CudaCodeGen(const StencilInstantiationContext& ctx, DiagnosticsEngine& engine,
int maxHaloPoints, int nsms, int maxBlocksPerSM, const Array3i& domainSize)
: CodeGen(ctx, engine, maxHaloPoints), codeGenOptions_{nsms, maxBlocksPerSM, domainSize} {}

Expand Down
10 changes: 9 additions & 1 deletion dawn/src/dawn/CodeGen/Cuda/CudaCodeGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "dawn/CodeGen/CodeGen.h"
#include "dawn/CodeGen/CodeGenProperties.h"
#include "dawn/CodeGen/Cuda/CacheProperties.h"
#include "dawn/CodeGen/Options.h"
#include "dawn/Support/Array.h"
#include "dawn/Support/IndexRange.h"
#include <unordered_map>
Expand All @@ -30,14 +31,20 @@ class StencilInstantiation;
namespace codegen {
namespace cuda {

/// @brief Run the Cuda code generation
std::unique_ptr<TranslationUnit>
run(const std::map<std::string, std::shared_ptr<iir::StencilInstantiation>>&
stencilInstantiationMap,
const Options& options = {});

/// @brief CUDA code generation for cartesian grids
/// @ingroup cxxnaive cartesian
class CudaCodeGen : public CodeGen {
std::unordered_map<int, CacheProperties> cachePropertyMap_;

public:
///@brief constructor
CudaCodeGen(const stencilInstantiationContext& ctx, DiagnosticsEngine& engine, int maxHaloPoints,
CudaCodeGen(const StencilInstantiationContext& ctx, DiagnosticsEngine& engine, int maxHaloPoints,
int nsms, int maxBlocksPerSM, const Array3i& domainSize);
virtual ~CudaCodeGen();
virtual std::unique_ptr<TranslationUnit> generateCode() override;
Expand Down Expand Up @@ -119,6 +126,7 @@ class CudaCodeGen : public CodeGen {
CudaCodeGenOptions codeGenOptions_;
bool iterationSpaceSet_;
};

} // namespace cuda
} // namespace codegen
} // namespace dawn
Expand Down
71 changes: 71 additions & 0 deletions dawn/src/dawn/CodeGen/Driver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//===--------------------------------------------------------------------------------*- C++ -*-===//
// _
// | |
// __| | __ ___ ___ ___
// / _` |/ _` \ \ /\ / / '_ |
// | (_| | (_| |\ V V /| | | |
// \__,_|\__,_| \_/\_/ |_| |_| - Compiler Toolchain
//
//
// This file is distributed under the MIT License (MIT).
// See LICENSE.txt for details.
//
//===------------------------------------------------------------------------------------------===//

#include "dawn/CodeGen/Driver.h"
#include "dawn/CodeGen/CXXNaive-ico/CXXNaiveCodeGen.h"
#include "dawn/CodeGen/CXXNaive/CXXNaiveCodeGen.h"
#include "dawn/CodeGen/Cuda/CudaCodeGen.h"
#include "dawn/CodeGen/GridTools/GTCodeGen.h"
#include <stdexcept>

namespace dawn {
namespace codegen {

std::string generate(const std::unique_ptr<TranslationUnit>& translationUnit) {
std::string code;
for(const auto& p : translationUnit->getPPDefines())
code += p + "\n";

code += translationUnit->getGlobals() + "\n\n";
for(const auto& p : translationUnit->getStencils())
code += p.second;

return code;
}

codegen::Backend parseBackendString(const std::string& backendStr) {
if(backendStr == "gt" || backendStr == "gridtools") {
return codegen::Backend::GridTools;
} else if(backendStr == "naive" || backendStr == "cxxnaive" || backendStr == "c++-naive") {
return codegen::Backend::CXXNaive;
} else if(backendStr == "ico" || backendStr == "naive-ico" || backendStr == "c++-naive-ico") {
return codegen::Backend::CXXNaiveIco;
} else if(backendStr == "cuda" || backendStr == "CUDA") {
return codegen::Backend::CUDA;
} else {
throw std::invalid_argument("Backend not supported");
}
}

std::unique_ptr<TranslationUnit>
run(const std::map<std::string, std::shared_ptr<iir::StencilInstantiation>>& context,
Backend backend, const Options& options) {
switch(backend) {
case Backend::CUDA:
return cuda::run(context, options);
case Backend::CXXNaive:
return cxxnaive::run(context, options);
case Backend::CXXNaiveIco:
return cxxnaiveico::run(context, options);
case Backend::GridTools:
return gt::run(context, options);
case Backend::CXXOpt:
throw std::invalid_argument("Backend not supported");
}
// This line should not be needed but the compiler seems to complain if it is not present.
return nullptr;
}

} // namespace codegen
} // namespace dawn
42 changes: 42 additions & 0 deletions dawn/src/dawn/CodeGen/Driver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//===--------------------------------------------------------------------------------*- C++ -*-===//
// _
// | |
// __| | __ ___ ___ ___
// / _` |/ _` \ \ /\ / / '_ |
// | (_| | (_| |\ V V /| | | |
// \__,_|\__,_| \_/\_/ |_| |_| - Compiler Toolchain
//
//
// This file is distributed under the MIT License (MIT).
// See LICENSE.txt for details.
//
//===------------------------------------------------------------------------------------------===//

#ifndef DAWN_CODEGEN_DRIVER_H
#define DAWN_CODEGEN_DRIVER_H

#include "dawn/CodeGen/Options.h"
#include "dawn/CodeGen/TranslationUnit.h"
#include "dawn/IIR/StencilInstantiation.h"
#include <map>
#include <memory>
#include <string>

namespace dawn {
namespace codegen {

/// @brief Parse the backend string to enumeration
Backend parseBackendString(const std::string& backendStr);

/// @brief Run the code generation
std::unique_ptr<TranslationUnit>
run(const std::map<std::string, std::shared_ptr<iir::StencilInstantiation>>& context,
Backend backend, const Options& options = {});

/// @brief Shortcut to generate code from a translation unit
std::string generate(const std::unique_ptr<TranslationUnit>& translationUnit);

} // namespace codegen
} // namespace dawn

#endif
Loading

0 comments on commit cf3e289

Please sign in to comment.