-
Notifications
You must be signed in to change notification settings - Fork 20
Added Sympiler Wrapper #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 10 commits
9436ff4
afa30dd
3045d06
f062fae
d888656
d4f790f
9a0ce05
bd64223
8b50984
85dafbe
45feb17
feed5f1
b335e40
7a0e47a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Source: https://github.com/cpm-cmake/CPM.cmake/blob/master/cmake/get_cpm.cmake | ||
# SPDX-License-Identifier: MIT | ||
|
||
set(CPM_DOWNLOAD_VERSION 0.38.1) | ||
|
||
if(CPM_SOURCE_CACHE) | ||
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake") | ||
elseif(DEFINED ENV{CPM_SOURCE_CACHE}) | ||
set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake") | ||
else() | ||
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake") | ||
endif() | ||
|
||
# Expand relative path. This is important if the provided path contains a tilde (~) | ||
get_filename_component(CPM_DOWNLOAD_LOCATION ${CPM_DOWNLOAD_LOCATION} ABSOLUTE) | ||
|
||
function(download_cpm) | ||
message(STATUS "Downloading CPM.cmake to ${CPM_DOWNLOAD_LOCATION}") | ||
file(DOWNLOAD | ||
https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake | ||
${CPM_DOWNLOAD_LOCATION} | ||
) | ||
endfunction() | ||
|
||
if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION})) | ||
download_cpm() | ||
else() | ||
# resume download if it previously failed | ||
file(READ ${CPM_DOWNLOAD_LOCATION} check) | ||
if("${check}" STREQUAL "") | ||
download_cpm() | ||
endif() | ||
unset(check) | ||
endif() | ||
|
||
include(${CPM_DOWNLOAD_LOCATION}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# | ||
# Copyright 2020 Adobe. All rights reserved. | ||
# This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. You may obtain a copy | ||
# of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software distributed under | ||
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
# OF ANY KIND, either express or implied. See the License for the specific language | ||
# governing permissions and limitations under the License. | ||
# | ||
if(TARGET sympiler::sym_sparse_blas) | ||
return() | ||
endif() | ||
|
||
message(STATUS "Third-party (external): creating target 'sympiler::sym_sparse_blas'") | ||
|
||
if("${CMAKE_SYSTEM_PROCESSOR}" MATCHES "arm64" OR "${CMAKE_OSX_ARCHITECTURES}" MATCHES "arm64") | ||
# apple M1 | ||
set(SYMPILER_BLAS_BACKEND "Apple" CACHE STRING "BLAS implementation for SYMPILER to use") | ||
else() | ||
# windows, linux, apple intel | ||
set(SYMPILER_BLAS_BACKEND "MKL" CACHE STRING "BLAS implementation for SYMPILER to use") | ||
endif() | ||
|
||
if(SYMPILER_BLAS_BACKEND STREQUAL "MKL") | ||
message("INCLUDING MKL") | ||
include(mkl) | ||
if(NOT TARGET MKL::MKL) | ||
add_library(MKL::MKL ALIAS mkl::mkl) | ||
endif() | ||
endif() | ||
|
||
include(CPM) | ||
CPMAddPackage( | ||
NAME sympiler | ||
GITHUB_REPOSITORY ryansynk/sympiler | ||
GIT_TAG 2fe1ef1d72b551acedd848d64b51fa5e90251804 | ||
) | ||
|
||
add_library(sympiler::sym_sparse_blas ALIAS sym_sparse_blas) | ||
|
||
include(GNUInstallDirs) | ||
target_include_directories(sym_sparse_blas INTERFACE | ||
"$<BUILD_INTERFACE:${sympiler_SOURCE_DIR}>/include" | ||
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>" | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#ifdef POLYSOLVE_WITH_SYMPILER | ||
|
||
#include <polysolve/LinearSolverSympiler.hpp> | ||
#include <Eigen/Core> | ||
|
||
namespace polysolve | ||
{ | ||
void LinearSolverSympiler::setSolverMode(int _solver_mode) | ||
{ | ||
solver_mode = _solver_mode; | ||
} | ||
|
||
void LinearSolverSympiler::updateCSC() | ||
{ | ||
m_A_csc->nzmax = m_A_copy.nonZeros(); | ||
m_A_csc->ncol = m_A_csc->nrow = m_A_copy.rows(); | ||
m_A_csc->p = m_A_copy.outerIndexPtr(); | ||
m_A_csc->i = m_A_copy.innerIndexPtr(); | ||
|
||
m_A_csc->x = m_A_copy.valuePtr(); | ||
m_A_csc->stype = -1; | ||
m_A_csc->packed = 1; | ||
m_A_csc->nz = nullptr; | ||
} | ||
|
||
void LinearSolverSympiler::setParameters(const json ¶ms) | ||
{ | ||
if (params.contains("Sympiler")) | ||
{ | ||
if (params["Sympiler"].contains("solver_mode")) | ||
{ | ||
setSolverMode(params["Sympiler"]["mtype"].get<int>()); | ||
} | ||
} | ||
} | ||
|
||
void LinearSolverSympiler::getInfo(json ¶ms) const | ||
{ | ||
if (factorize_status == 1) | ||
{ | ||
params["factorize_info"] = "Success"; | ||
} | ||
else | ||
{ | ||
params["factorize_info"] = "Failure"; | ||
} | ||
} | ||
|
||
void LinearSolverSympiler::analyzePattern(const StiffnessMatrix &A, const int precond_num) | ||
{ | ||
m_A_copy = StiffnessMatrix(A); | ||
updateCSC(); | ||
m_solver = std::make_unique<sym_lib::parsy::SolverSettings>(m_A_csc.get()); | ||
m_solver->symbolic_analysis(); | ||
} | ||
|
||
// Factorize system matrix | ||
void LinearSolverSympiler::factorize(const StiffnessMatrix &A) | ||
{ | ||
// Only copy when matrix changes | ||
if (!m_A_copy.isApprox(A)) | ||
{ | ||
m_A_copy = StiffnessMatrix(A); | ||
updateCSC(); | ||
} | ||
factorize_status = m_solver->numerical_factorization(m_A_csc.get()); | ||
} | ||
|
||
// Solve the linear system Ax = b | ||
void LinearSolverSympiler::solve(const Ref<const Eigen::VectorXd> b, Ref<Eigen::VectorXd> x) | ||
{ | ||
double *x_ptr = m_solver->solve_only(b.data()); | ||
x = Eigen::Map<Eigen::VectorXd>(x_ptr, x.rows(), x.cols()); | ||
} | ||
|
||
} // namespace polysolve | ||
|
||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#ifdef POLYSOLVE_WITH_SYMPILER | ||
|
||
#include <polysolve/LinearSolver.hpp> | ||
#include <sympiler/parsy/cholesky_solver.h> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's move this header to the .cpp. This will require moving the definition of the constructor/destructor to the .cpp of this file as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this possible given the member variables of type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I took a crack at it -- let me know how it looks |
||
|
||
namespace polysolve | ||
{ | ||
class LinearSolverSympiler : public LinearSolver | ||
{ | ||
public: | ||
LinearSolverSympiler() | ||
: m_solver(nullptr), m_A_csc(std::make_unique<sym_lib::parsy::CSC>()) {} | ||
virtual ~LinearSolverSympiler() = default; | ||
|
||
private: | ||
POLYSOLVE_DELETE_MOVE_COPY(LinearSolverSympiler) | ||
|
||
protected: | ||
void setSolverMode(int _solver_mode); | ||
void updateCSC(); | ||
|
||
public: | ||
////////////////////// | ||
// Public interface // | ||
////////////////////// | ||
|
||
// Set solver parameters | ||
virtual void setParameters(const json ¶ms) override; | ||
|
||
// Retrieve status (success/failure) of factorization | ||
virtual void getInfo(json ¶ms) const override; | ||
|
||
// Analyze sparsity pattern | ||
virtual void analyzePattern(const StiffnessMatrix &A, const int precond_num) override; | ||
|
||
// Factorize system matrix | ||
virtual void factorize(const StiffnessMatrix &A) override; | ||
|
||
// Solve the linear system Ax = b | ||
virtual void solve(const Ref<const VectorXd> b, Ref<VectorXd> x) override; | ||
|
||
// Name of the solver type (for debugging purposes) | ||
virtual std::string name() const override { return "Sympiler"; } | ||
|
||
private: | ||
std::unique_ptr<sym_lib::parsy::SolverSettings> m_solver; | ||
std::unique_ptr<sym_lib::parsy::CSC> m_A_csc; | ||
polysolve::StiffnessMatrix m_A_copy; | ||
|
||
protected: | ||
//////////////////// | ||
// Sympiler stuff // | ||
//////////////////// | ||
int solver_mode = 0; // 0 is normal solve, 1 is row/col addition | ||
int factorize_status = -1; | ||
Comment on lines
+51
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd vote for using |
||
}; | ||
} // namespace polysolve | ||
|
||
#endif |
Uh oh!
There was an error while loading. Please reload this page.