From 6998e4253c12d14854a20c393f12e719d0cd72d0 Mon Sep 17 00:00:00 2001 From: Manuel Drehwald Date: Mon, 28 Aug 2023 17:30:22 -0400 Subject: [PATCH] Make Eigen blas usage more robust trough -Xclang Definition --- CMakeLists.txt | 21 ++++++++++++--- multisource.c | 48 --------------------------------- multisource.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 51 deletions(-) delete mode 100644 multisource.c create mode 100644 multisource.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 771256b..7abc72b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,16 +9,31 @@ message("Found LLVM at: " ${Enzyme_LLVM_BINARY_DIR}) set(CMAKE_C_COMPILER "${Enzyme_LLVM_BINARY_DIR}/bin/clang") set(CMAKE_CXX_COMPILER "${Enzyme_LLVM_BINARY_DIR}/bin/clang++") -project(EnzymeExample) +set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --save-temps -Xclang -D -Xclang EIGEN_USE_BLAS -Xclang -no-opaque-pointers") +set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --save-temps -Xclang -D -Xclang EIGEN_USE_BLAS -Xclang -no-opaque-pointers") + +project(EnzymeExample CXX) + +set(BLA_VENDER OpenBLAS) +find_package(BLAS REQUIRED) +if(BLAS_FOUND) + message("OpenBLAS found.") + #include_directories(/opt/OpenBLAS/include/) + #target_link_libraries(main.exe ${BLAS_LIBRARIES}) +endif(BLAS_FOUND) message("found dir ${Enzyme_DIR}") message("found ${Enzyme_FOUND}") get_property(importTargetsAfter DIRECTORY "${CMAKE_SOURCE_DIR}" PROPERTY IMPORTED_TARGETS) message("imported targets ${importTargetsAfter}") + +find_package (Eigen3 3.3 REQUIRED NO_MODULE) + + add_executable(example - multisource.c + multisource.cpp myblas.c myblas.h ) -target_link_libraries(example PUBLIC LLDEnzymeFlags) +target_link_libraries(example PUBLIC LLDEnzymeFlags Eigen3::Eigen ${BLAS_LIBRARIES}) diff --git a/multisource.c b/multisource.c deleted file mode 100644 index a90cb41..0000000 --- a/multisource.c +++ /dev/null @@ -1,48 +0,0 @@ -#include -#include -#include -#include -#include "myblas.h" - -double dotabs(struct complex* alpha, struct complex* beta, int n) { - struct complex prod = myblas_cdot(alpha, beta, n); - return myblas_cabs(prod); -} - -void __enzyme_autodiff(void*, ...); -int enzyme_const, enzyme_dup, enzyme_out; - -int main(int argc, char *argv[]) { - int n = 3; - if (argc > 1) { - n = atoi(argv[1]); - } - - - struct complex *A = (struct complex*)malloc(sizeof(struct complex) * n); - assert(A != 0); - for(int i=0; i +#include +#include +#include +#include + +// #define EIGEN_USE_BLAS + +#include + +double dotabs(struct complex* alpha, struct complex* beta, int n) { + struct complex prod = myblas_cdot(alpha, beta, n); + return myblas_cabs(prod); +} + +void __enzyme_autodiff(void*, ...); +int enzyme_const, enzyme_dup, enzyme_out; + +using Eigen::MatrixXd; +using Eigen::VectorXd; + +void foo(MatrixXd *m, VectorXd *v) { *v = *m * *v; } + +int main(int argc, char *argv[]) { + // int size = 50; + //// VectorXf is a vector of floats, with dynamic size. + // Eigen::VectorXf u(size), v(size), w(size); + // u = v + w; + + MatrixXd m = MatrixXd::Random(30, 30); + MatrixXd dm = MatrixXd::Random(30, 30); + m = (m + MatrixXd::Constant(30, 30, 1.2)) * 50; + std::cout << "m =" << std::endl << m << std::endl; + VectorXd v = VectorXd::Random(30); + VectorXd dv = VectorXd::Random(30); + // v << 1, 2, 3; + // std::cout << "m * v =" << std::endl << m * v << std::endl; + + __enzyme_autodiff((void *)foo, &m, &dm, &v, &dv); + std::cout << "dm, dv: =" << std::endl << dm << std::endl << dv << std::endl; + + // int n = 3; + // if (argc > 1) { + // n = atoi(argv[1]); + // } + + // struct complex *A = (struct complex*)malloc(sizeof(struct complex) * n); + // assert(A != 0); + // for(int i=0; i