Skip to content

Commit

Permalink
Merge pull request #47 from joaoleal/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
joaoleal authored Feb 25, 2020
2 parents c97c024 + 9650a00 commit ba1632f
Show file tree
Hide file tree
Showing 20 changed files with 585 additions and 357 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ ENDIF()
# ----------------------------------------------------------------------------
# Check if it is possible to use LLVM/Clang for JIT
# ----------------------------------------------------------------------------
SET(CPPADCG_LLVM_LINK_LIB "3.2|3.6|3.8|4.0|5.0|6.0|7.0|8.0")
SET(CPPADCG_LLVM_LINK_LIB "3.2|3.6|3.8|4.0|5.0|6.0|7.0|8.0|9.0")

IF((("${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}" MATCHES "^(${CPPADCG_LLVM_LINK_LIB})$") AND CLANG_FOUND)
OR
Expand Down
2 changes: 1 addition & 1 deletion cmake/FindLLVM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ UNSET(LLVM_MODULE_LIBS CACHE)

MACRO(find_llvm_iteratively)
IF(NOT LLVM_CONFIG AND NOT LLVM_FIND_VERSION_EXACT)
SET(_LLVM_KNOWN_VERSIONS ${LLVM_ADDITIONAL_VERSIONS} "8" "7" "6.0" "5.0" "4.0" "3.8" "3.7" "3.6" "3.5" "3.4" "3.3" "3.2")
SET(_LLVM_KNOWN_VERSIONS ${LLVM_ADDITIONAL_VERSIONS} "9" "8" "7" "6.0" "5.0" "4.0" "3.8" "3.7" "3.6" "3.5" "3.4" "3.3" "3.2")

# Select acceptable versions.
FOREACH(version ${_LLVM_KNOWN_VERSIONS})
Expand Down
4 changes: 2 additions & 2 deletions include/cppad/cg/lang/mathml/language_mathml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ class LanguageMathML : public Language<Base> {
const std::vector<FuncArgument>& indArg = _nameGen->getIndependent();
const std::vector<FuncArgument>& depArg = _nameGen->getDependent();
const std::vector<FuncArgument>& tmpArg = _nameGen->getTemporary();
CPPADCG_ASSERT_KNOWN(!indArg.empty() && depArg.size() > 0,
CPPADCG_ASSERT_KNOWN(!indArg.empty() && !depArg.empty(),
"There must be at least one dependent and one independent argument")
CPPADCG_ASSERT_KNOWN(tmpArg.size() == 3,
"There must be three temporary variables")
Expand Down Expand Up @@ -1906,7 +1906,7 @@ class LanguageMathML : public Language<Base> {
CPPADCG_ASSERT_KNOWN(node.getOperationType() == CGOpCode::IndexAssign, "Invalid node type")
CPPADCG_ASSERT_KNOWN(node.getArguments().size() > 0, "Invalid number of arguments for an index assignment operation")

IndexAssignOperationNode<Base>& inode = static_cast<IndexAssignOperationNode<Base>&> (node);
auto& inode = static_cast<IndexAssignOperationNode<Base>&> (node);

const IndexPattern& ip = inode.getIndexPattern();
_code << _startEq
Expand Down
2 changes: 2 additions & 0 deletions include/cppad/cg/model/llvm/llvm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include <cppad/cg/model/llvm/v7_0/llvm7_0.hpp>
#elif LLVM_VERSION_MAJOR==8 && LLVM_VERSION_MINOR==0
#include <cppad/cg/model/llvm/v8_0/llvm8_0.hpp>
#elif LLVM_VERSION_MAJOR==9 && LLVM_VERSION_MINOR==0
#include <cppad/cg/model/llvm/v9_0/llvm9_0.hpp>
#endif

#endif
Expand Down
92 changes: 92 additions & 0 deletions include/cppad/cg/model/llvm/v9_0/llvm9_0.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#ifndef CPPAD_CG_LLVM9_0_INCLUDED
#define CPPAD_CG_LLVM9_0_INCLUDED
/* --------------------------------------------------------------------------
* CppADCodeGen: C++ Algorithmic Differentiation with Source Code Generation:
* Copyright (C) 2019 Joao Leal
*
* CppADCodeGen is distributed under multiple licenses:
*
* - Eclipse Public License Version 1.0 (EPL1), and
* - GNU General Public License Version 3 (GPL3).
*
* EPL1 terms and conditions can be found in the file "epl-v10.txt", while
* terms and conditions for the GPL3 can be found in the file "gpl3.txt".
* ----------------------------------------------------------------------------
* Author: Joao Leal
*/

/**
* LLVM requires the use of it own flags which can make it difficult to compile
* libraries not using NDEBUG often required by LLVM.
* The define LLVM_CPPFLAG_NDEBUG can be used to apply NDEBUG only to LLVM
* headers.
*/
#ifdef LLVM_WITH_NDEBUG

// save the original NDEBUG definition
#ifdef NDEBUG
#define _OUTER_NDEBUG_DEFINED
#endif

#if LLVM_WITH_NDEBUG == 1
#define NDEBUG
#else
#undef NDEBUG
#endif

#endif

#include <clang/CodeGen/CodeGenAction.h>
#include <clang/Basic/DiagnosticOptions.h>
#include <clang/Basic/TargetInfo.h>
#include <clang/Basic/SourceManager.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/CompilerInvocation.h>
#include <clang/Frontend/FrontendDiagnostic.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <clang/Frontend/Utils.h>
#include <clang/Parse/ParseAST.h>
#include <clang/Lex/Preprocessor.h>
#include <clang/Lex/PreprocessorOptions.h>

#include <llvm/Analysis/Passes.h>
#include <llvm/IR/Verifier.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ExecutionEngine/SectionMemoryManager.h>
//#include <llvm/ExecutionEngine/JIT.h>
#include <llvm/IR/LegacyPassManager.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/Pass.h>
#include <llvm/Transforms/IPO/PassManagerBuilder.h>
#include <llvm/Bitcode/BitcodeReader.h>
#include <llvm/Bitcode/BitcodeWriter.h>
#include <llvm/Support/ManagedStatic.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Support/raw_os_ostream.h>
//#include <llvm/Support/system_error.h>
#include <llvm/Linker/Linker.h>
#include <llvm/Support/Program.h>

#ifdef LLVM_WITH_NDEBUG

// recover the original NDEBUG
#ifdef _OUTER_NDEBUG_DEFINED
#define NDEBUG
#else
#undef NDEBUG
#endif

// no need for this anymore
#undef _OUTER_NDEBUG_DEFINED

#endif

#include <cppad/cg/model/compiler/clang_compiler.hpp>
#include <cppad/cg/model/llvm/llvm_model_library.hpp>
#include <cppad/cg/model/llvm/llvm_model.hpp>
#include <cppad/cg/model/llvm/v5_0/llvm_model_library_impl.hpp> // yes, this is from version 5.0
#include <cppad/cg/model/llvm/v9_0/llvm_model_library_processor.hpp>

#endif
54 changes: 54 additions & 0 deletions include/cppad/cg/model/llvm/v9_0/llvm_model_library_processor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#ifndef CPPAD_CG_LLVM_MODEL_LIBRARY_PROCESSOR_INCLUDED
#define CPPAD_CG_LLVM_MODEL_LIBRARY_PROCESSOR_INCLUDED
/* --------------------------------------------------------------------------
* CppADCodeGen: C++ Algorithmic Differentiation with Source Code Generation:
* Copyright (C) 2020 Joao Leal
*
* CppADCodeGen is distributed under multiple licenses:
*
* - Eclipse Public License Version 1.0 (EPL1), and
* - GNU General Public License Version 3 (GPL3).
*
* EPL1 terms and conditions can be found in the file "epl-v10.txt", while
* terms and conditions for the GPL3 can be found in the file "gpl3.txt".
* ----------------------------------------------------------------------------
* Author: Joao Leal
*/

#include <cppad/cg/model/llvm/v5_0/llvm_base_model_library_processor_impl.hpp>

namespace CppAD {
namespace cg {

/**
* Useful class for generating a JIT evaluated model library (LLVM 9.0).
*
* @author Joao Leal
*/
template<class Base>
class LlvmModelLibraryProcessor : public LlvmBaseModelLibraryProcessorImpl<Base> {
public:

/**
* Creates a LLVM model library processor.
*
* @param librarySourceGen
*/
LlvmModelLibraryProcessor(ModelLibraryCSourceGen<Base>& librarySourceGen) :
LlvmBaseModelLibraryProcessorImpl<Base>(librarySourceGen, "9") {
}

virtual ~LlvmModelLibraryProcessor() = default;

using LlvmBaseModelLibraryProcessorImpl<Base>::create;

static inline std::unique_ptr<LlvmModelLibrary<Base>> create(ModelLibraryCSourceGen<Base>& modelLibraryHelper) {
LlvmModelLibraryProcessor<Base> p(modelLibraryHelper);
return p.create();
}
};

} // END cg namespace
} // END CppAD namespace

#endif
16 changes: 13 additions & 3 deletions include/cppad/cg/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ inline void addMatrixSparsity(const VectorSet& a,
template<class VectorSet, class VectorSet2>
inline void addMatrixSparsity(const VectorSet& a,
VectorSet2& result) {
CPPADCG_ASSERT_UNKNOWN(result.size() == a.size());
CPPADCG_ASSERT_UNKNOWN(result.size() == a.size())

addMatrixSparsity<VectorSet, VectorSet2>(a, a.size(), result);
}
Expand Down Expand Up @@ -197,7 +197,7 @@ inline void multMatrixMatrixSparsity(const VectorSet& a,

for (size_t jj = 0; jj < q; jj++) { //loop columns of b
const std::set<size_t>& colB = bt[jj];
if (colB.size() > 0) {
if (!colB.empty()) {
for (size_t i = 0; i < m; i++) {
const std::set<size_t>& rowA = a[i];
for (size_t rowb : colB) {
Expand Down Expand Up @@ -264,7 +264,7 @@ inline void multMatrixTransMatrixSparsity(const VectorSet& a,

for (size_t jj = 0; jj < q; jj++) { //loop columns of b
const std::set<size_t>& colB = bt[jj];
if (colB.size() > 0) {
if (!colB.empty()) {
for (size_t i = 0; i < n; i++) {
const std::set<size_t>& rowAt = at[i];
if (!rowAt.empty()) {
Expand Down Expand Up @@ -805,6 +805,16 @@ inline std::string implode(const std::vector<std::string>& text,
}
}

std::string readStringFromFile(const std::string& path) {
std::ifstream iStream;
iStream.open(path);

std::stringstream strStream;
strStream << iStream.rdbuf();

return strStream.str();
}

} // END cg namespace
} // END CppAD namespace

Expand Down
13 changes: 13 additions & 0 deletions test/CppADCGTest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ class CppADCGTest : public ::testing::Test {

protected:

static inline ::testing::AssertionResult compareValues(const std::vector<Base>& depCGen,
const std::vector<CppAD::cg::CG<Base> >& dep,
double epsilonR = 1e-14, double epsilonA = 1e-14) {

std::vector<double> depd(dep.size());

for (size_t i = 0; i < depd.size(); i++) {
depd[i] = dep[i].getValue();
}

return compareValues<Base>(depCGen, depd, epsilonR, epsilonA);
}

template<class T>
static inline ::testing::AssertionResult compareValues(const std::vector<std::vector<T> >& depCGen,
const std::vector<std::vector<T> >& dep,
Expand Down
Loading

0 comments on commit ba1632f

Please sign in to comment.