From d5fb98f124b2f3a0e6eb72f6d7b5d3995d18ddd1 Mon Sep 17 00:00:00 2001 From: Yash Agrawal Date: Mon, 27 May 2024 19:56:32 +0530 Subject: [PATCH 1/5] Eigen package is optional (support for removed eigen) --- CMakeLists.txt | 10 ++- README.md | 2 +- src/FlowAware.cpp | 7 +- src/include/VectorSolver.h | 116 ++++++++++++++++++++------------ src/include/VectorSolverEigen.h | 63 +++++++++++++++++ 5 files changed, 151 insertions(+), 47 deletions(-) create mode 100644 src/include/VectorSolverEigen.h diff --git a/CMakeLists.txt b/CMakeLists.txt index cf098afa..8b620bfb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,8 +8,14 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2") set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) -find_package (Eigen3 3.3.7 REQUIRED NO_MODULE) -message(STATUS "Found Eigen3 in: ${Eigen3_DIR}") +find_package (Eigen3 3.3.7 QUIET NO_MODULE) + +if (Eigen3_FOUND) + # Eigen3 found, include the directory + include_directories(${Eigen3_DIR}) + message(STATUS "Found Eigen3: ${Eigen3_DIR}") + add_definitions(-DEIGEN_FOUND) +endif() set(CMAKE_CXX_STANDARD 17 CACHE STRING "") diff --git a/README.md b/README.md index 681ea482..4b21cce3 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ If you're a C++ developer and require low-level control, optimization, or integr * GNU Make (4.2.1) * LLVM (17.0.6) - [src](https://github.com/llvm/llvm-project/tree/release/17.x), [release](https://releases.llvm.org/download.html#17.0.6) * Support for latest LLVM versions would be added soon -* Eigen library (3.3.7) +* Eigen library (3.3.7) (Optional) * Python (3.6.7) * Other python requirements * For training the vocabulary are available in [seed_embeddings/OpenKE/requirements.txt](./seed_embeddings/OpenKE/requirements.txt), and diff --git a/src/FlowAware.cpp b/src/FlowAware.cpp index 48cddcfe..9161e1d2 100644 --- a/src/FlowAware.cpp +++ b/src/FlowAware.cpp @@ -5,8 +5,11 @@ // file in the top-level directory for more details. // #include "FlowAware.h" -#include "VectorSolver.h" - +#ifdef EIGEN_FOUND + #include "VectorSolverEigen.h" +#else + #include "VectorSolver.h" +#endif #include "llvm/ADT/DepthFirstIterator.h" #include "llvm/ADT/MapVector.h" #include "llvm/ADT/PostOrderIterator.h" diff --git a/src/include/VectorSolver.h b/src/include/VectorSolver.h index 4ae901e2..1d7d5615 100644 --- a/src/include/VectorSolver.h +++ b/src/include/VectorSolver.h @@ -6,58 +6,90 @@ // #ifndef __VECTOR_SOLVER_H__ #define __VECTOR_SOLVER_H__ -#define EIGEN_MPL2_ONLY -#include "Eigen/LU" -#include "Eigen/QR" -#include "llvm/ADT/SmallVector.h" +#include +#include +#include +#include #include +using namespace std; +typedef std::vector> matrix; +// Function to swap rows in a matrix +void swapRows(std::vector &row1, std::vector &row2) { + std::swap(row1, row2); +} -using namespace Eigen; -using namespace llvm; +const double EPS = 1e-9; -typedef std::vector> matrix; +void gaussJordan(matrix a, int k, matrix &ans) { + int n = (int)a.size(); + int m = (int)a[0].size() - k; -MatrixXd calculate(MatrixXd A, MatrixXd B) { - if (A.determinant() != 0) { - return A.fullPivHouseholderQr().solve(B); - } else { - // To-Do: perturb probabilities - llvm_unreachable("inconsistent/infinitely many solutions"); + vector where(m, -1); + for (int col = 0, row = 0; col < m && row < n; ++col) { + int sel = row; + for (int i = row; i < n; ++i) + if (abs(a[i][col]) > abs(a[sel][col])) + sel = i; + if (abs(a[sel][col]) < EPS) + continue; + for (int i = 0; i < m + k; ++i) + swap(a[sel][i], a[row][i]); + where[col] = row; + + for (int i = 0; i < n; ++i) + if (i != row) { + double c = a[i][col] / a[row][col]; + for (int j = col; j < m + k; ++j) + a[i][j] -= a[row][j] * c; + } + ++row; } -} -MatrixXd formMatrix(std::vector> a, int r, int l) { - MatrixXd M(r, l); - for (int i = 0; i < r; i++) - M.row(i) = VectorXd::Map(&a[i][0], a[i].size()); + ans.assign(m, vector(k, 0)); + for (int i = 0; i < m; ++i) + if (where[i] != -1) + for (int j = 0; j < k; ++j) + ans[i][j] = a[where[i]][m + j] / a[where[i]][i]; - return M; + for (int i = 0; i < n; ++i) { + for (int j = 0; j < k; ++j) { + double sum = 0; + for (int l = 0; l < m; ++l) + sum += ans[l][j] * a[i][l]; + if (abs(sum - a[i][m + j]) > EPS) + return; + } + } } +matrix solve(matrix &A, matrix &B) { + int m = A.size(); + int n = B[0].size(); + + // Check if dimensions are compatible (m rows in A, same m rows in B) + if (m != B.size()) { + throw std::invalid_argument( + "Matrix dimensions are not compatible for solving AX=B"); + } -matrix solve(matrix A, matrix B) { - int r = A.size(); - int c = A[0].size(); - MatrixXd mA(r, c); - mA = formMatrix(A, r, c); - - r = B.size(); - c = B[0].size(); - MatrixXd mB(r, c); - mB = formMatrix(B, r, c); - - r = A.size(); - MatrixXd x(r, c); - x = calculate(mA, mB); - std::vector> raw_data; - // raw_data.resize(x.rows()); - for (unsigned i = 0; i < x.rows(); i++) { - std::vector tmp; - tmp.resize(x.cols()); - VectorXd::Map(&tmp[0], x.cols()) = x.row(i); - raw_data.push_back(tmp); + matrix augmented(m, std::vector(m + n)); + for (int i = 0; i < m; ++i) { + for (int j = 0; j < m; ++j) { + augmented[i][j] = A[i][j]; + } + for (int j = 0; j < n; ++j) { + augmented[i][m + j] = B[i][j]; + } } - return raw_data; + gaussJordan(augmented, B[0].size(), B); + matrix X(m, std::vector(n)); + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + X[i][j] = B[i][j]; + } + } + + return X; } -#endif +#endif \ No newline at end of file diff --git a/src/include/VectorSolverEigen.h b/src/include/VectorSolverEigen.h new file mode 100644 index 00000000..4ae901e2 --- /dev/null +++ b/src/include/VectorSolverEigen.h @@ -0,0 +1,63 @@ +// Copyright (c) 2021, S. VenkataKeerthy, Rohit Aggarwal +// Department of Computer Science and Engineering, IIT Hyderabad +// +// This software is available under the BSD 4-Clause License. Please see LICENSE +// file in the top-level directory for more details. +// +#ifndef __VECTOR_SOLVER_H__ +#define __VECTOR_SOLVER_H__ +#define EIGEN_MPL2_ONLY + +#include "Eigen/LU" +#include "Eigen/QR" +#include "llvm/ADT/SmallVector.h" +#include + +using namespace Eigen; +using namespace llvm; + +typedef std::vector> matrix; + +MatrixXd calculate(MatrixXd A, MatrixXd B) { + if (A.determinant() != 0) { + return A.fullPivHouseholderQr().solve(B); + } else { + // To-Do: perturb probabilities + llvm_unreachable("inconsistent/infinitely many solutions"); + } +} + +MatrixXd formMatrix(std::vector> a, int r, int l) { + MatrixXd M(r, l); + for (int i = 0; i < r; i++) + M.row(i) = VectorXd::Map(&a[i][0], a[i].size()); + + return M; +} + +matrix solve(matrix A, matrix B) { + int r = A.size(); + int c = A[0].size(); + MatrixXd mA(r, c); + mA = formMatrix(A, r, c); + + r = B.size(); + c = B[0].size(); + MatrixXd mB(r, c); + mB = formMatrix(B, r, c); + + r = A.size(); + MatrixXd x(r, c); + x = calculate(mA, mB); + std::vector> raw_data; + // raw_data.resize(x.rows()); + for (unsigned i = 0; i < x.rows(); i++) { + std::vector tmp; + tmp.resize(x.cols()); + VectorXd::Map(&tmp[0], x.cols()) = x.row(i); + raw_data.push_back(tmp); + } + return raw_data; +} + +#endif From f372487f2af88b75ce5ac1c5d96371ae64173567 Mon Sep 17 00:00:00 2001 From: Yash Agrawal Date: Tue, 28 May 2024 10:30:10 +0530 Subject: [PATCH 2/5] minor updates in CMakeLists.txt --- CMakeLists.txt | 2 -- src/CMakeLists.txt | 10 ++++++++-- src/FlowAware.cpp | 1 + 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8b620bfb..16f5100d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,8 +11,6 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) find_package (Eigen3 3.3.7 QUIET NO_MODULE) if (Eigen3_FOUND) - # Eigen3 found, include the directory - include_directories(${Eigen3_DIR}) message(STATUS "Found Eigen3: ${Eigen3_DIR}") add_definitions(-DEIGEN_FOUND) endif() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e8f5521a..37a1415b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -30,7 +30,10 @@ if(NOT LLVM_IR2VEC) add_library(objlib OBJECT ${libsrc}) set_property(TARGET objlib PROPERTY POSITION_INDEPENDENT_CODE 1) - target_link_libraries (objlib Eigen3::Eigen) + find_package(Eigen3 QUIET) + if(Eigen3_FOUND) + target_link_libraries (objlib Eigen3::Eigen) + endif() add_library(${IR2VEC_LIB} SHARED $) add_library(${IR2VEC_LIB_STATIC} STATIC $) @@ -71,7 +74,10 @@ else() intrinsics_gen ) - target_link_libraries(LLVMIR2Vec PRIVATE Eigen3::Eigen) + find_package(Eigen3 QUIET) + if(Eigen3_FOUND) + target_link_libraries(LLVMIR2Vec PRIVATE Eigen3::Eigen) + endif() target_include_directories(LLVMIR2Vec PRIVATE ${LLVM_MAIN_INCLUDE_DIR}) target_include_directories(LLVMIR2Vec PRIVATE .) diff --git a/src/FlowAware.cpp b/src/FlowAware.cpp index 9161e1d2..84f9cc8a 100644 --- a/src/FlowAware.cpp +++ b/src/FlowAware.cpp @@ -10,6 +10,7 @@ #else #include "VectorSolver.h" #endif + #include "llvm/ADT/DepthFirstIterator.h" #include "llvm/ADT/MapVector.h" #include "llvm/ADT/PostOrderIterator.h" From b2ea3ce8ae81e4a7ffcf93c66a295b036a340b06 Mon Sep 17 00:00:00 2001 From: Yash Agrawal Date: Tue, 28 May 2024 10:32:13 +0530 Subject: [PATCH 3/5] added new line at the end of the file --- src/include/VectorSolver.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/include/VectorSolver.h b/src/include/VectorSolver.h index 1d7d5615..bac000db 100644 --- a/src/include/VectorSolver.h +++ b/src/include/VectorSolver.h @@ -92,4 +92,4 @@ matrix solve(matrix &A, matrix &B) { return X; } -#endif \ No newline at end of file +#endif From 4b05fa0f1e5018b15f0227544708e9a4bcd1e245 Mon Sep 17 00:00:00 2001 From: Yash Agrawal Date: Tue, 28 May 2024 10:44:07 +0530 Subject: [PATCH 4/5] minor update in flowAware file --- src/FlowAware.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/FlowAware.cpp b/src/FlowAware.cpp index 84f9cc8a..a37a91f7 100644 --- a/src/FlowAware.cpp +++ b/src/FlowAware.cpp @@ -6,9 +6,9 @@ // #include "FlowAware.h" #ifdef EIGEN_FOUND - #include "VectorSolverEigen.h" +#include "VectorSolverEigen.h" #else - #include "VectorSolver.h" +#include "VectorSolver.h" #endif #include "llvm/ADT/DepthFirstIterator.h" From eafffb5ba52cd32ba5e089e63820243d69ebec3b Mon Sep 17 00:00:00 2001 From: Yash Agrawal Date: Tue, 28 May 2024 11:59:56 +0530 Subject: [PATCH 5/5] minor change in CMakeLists.txt --- src/CMakeLists.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 37a1415b..e4c7adaa 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -30,7 +30,6 @@ if(NOT LLVM_IR2VEC) add_library(objlib OBJECT ${libsrc}) set_property(TARGET objlib PROPERTY POSITION_INDEPENDENT_CODE 1) - find_package(Eigen3 QUIET) if(Eigen3_FOUND) target_link_libraries (objlib Eigen3::Eigen) endif() @@ -74,7 +73,6 @@ else() intrinsics_gen ) - find_package(Eigen3 QUIET) if(Eigen3_FOUND) target_link_libraries(LLVMIR2Vec PRIVATE Eigen3::Eigen) endif()