Skip to content

Commit

Permalink
suitesparse-cholmod: new recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
valgur committed May 6, 2024
1 parent 4522d7a commit 8dac0cc
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 0 deletions.
4 changes: 4 additions & 0 deletions recipes/suitesparse-cholmod/all/conandata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sources:
"5.2.1":
url: "https://github.com/DrTimothyAldenDavis/SuiteSparse/archive/refs/tags/v7.7.0.tar.gz"
sha256: "529b067f5d80981f45ddf6766627b8fc5af619822f068f342aab776e683df4f3"
123 changes: 123 additions & 0 deletions recipes/suitesparse-cholmod/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import os

from conan import ConanFile
from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout
from conan.tools.env import VirtualBuildEnv
from conan.tools.files import get, rm, rmdir, copy

required_conan_version = ">=1.53.0"


class SuiteSparseCholmodConan(ConanFile):
name = "suitesparse-cholmod"
description = "CHOLMOD: Routines for factorizing sparse symmetric positive definite matrices in SuiteSparse"
license = "LGPL-2.1-or-later AND GPL-2.0-or-later AND Apache-2.0"
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://people.engr.tamu.edu/davis/suitesparse.html"
topics = ("mathematics", "sparse-matrix", "linear-algebra", "matrix-factorization")

package_type = "library"
settings = "os", "arch", "compiler", "build_type"
options = {
"shared": [True, False],
"fPIC": [True, False],
"gpl": [True, False],
"cuda": [True, False],
}
default_options = {
"shared": False,
"fPIC": True,
"gpl": True,
"cuda": False,
}
options_description = {
"gpl": "Enable GPL-licensed modules",
"cuda": "Enable CUDA acceleration",
}

def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC

def configure(self):
if self.options.shared:
self.options.rm_safe("fPIC")
self.settings.rm_safe("compiler.cppstd")
self.settings.rm_safe("compiler.libcxx")

def package_id(self):
if not self.info.options.gpl:
self.license = "LGPL-2.1-or-later AND Apache-2.0"

def layout(self):
cmake_layout(self, src_folder="src")

def requirements(self):
# OpenBLAS and OpenMP are provided via suitesparse-config
self.requires("suitesparse-config/7.7.0", transitive_headers=True, transitive_libs=True)
self.requires("suitesparse-amd/3.3.2")
self.requires("suitesparse-camd/3.3.2")
self.requires("suitesparse-colamd/3.3.3")
self.requires("suitesparse-ccolamd/3.3.3")

# A modified vendored version of METIS v5.1.0 is included,
# but it has been modified to not conflict with the general version

def validate(self):
if self.options.cuda and not self.options.gpl:
raise ValueError("CUDA acceleration requires GPL-licensed modules")

def build_requirements(self):
self.tool_requires("cmake/[>=3.22 <4]")

def source(self):
get(self, **self.conan_data["sources"][self.version], strip_root=True)

def generate(self):
venv = VirtualBuildEnv(self)
venv.generate()

tc = CMakeToolchain(self)
tc.variables["BUILD_SHARED_LIBS"] = self.options.shared
tc.variables["BUILD_STATIC_LIBS"] = not self.options.shared
tc.variables["CHOLMOD_GPL"] = self.options.gpl
tc.variables["SUITESPARSE_USE_OPENMP"] = True
tc.variables["SUITESPARSE_USE_CUDA"] = self.options.cuda
tc.variables["SUITESPARSE_DEMOS"] = False
tc.variables["SUITESPARSE_USE_FORTRAN"] = False # Fortran sources are translated to C instead
tc.generate()

deps = CMakeDeps(self)
deps.generate()

def build(self):
cmake = CMake(self)
cmake.configure(build_script_folder=os.path.join(self.source_folder, "CHOLMOD"))
cmake.build()

def package(self):
copy(self, "License.txt", os.path.join(self.source_folder, "CHOLMOD", "Doc"), os.path.join(self.package_folder, "licenses"))
cmake = CMake(self)
cmake.install()
rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig"))
rmdir(self, os.path.join(self.package_folder, "lib", "cmake"))
rmdir(self, os.path.join(self.package_folder, "share"))
rm(self, "*.pdb", self.package_folder, recursive=True)

def package_info(self):
self.cpp_info.set_property("cmake_file_name", "CHOLMOD")
self.cpp_info.set_property("cmake_target_name", "SuiteSparse::CHOLMOD")
if not self.options.shared:
self.cpp_info.set_property("cmake_target_aliases", ["SuiteSparse::CHOLMOD_static"])
self.cpp_info.set_property("pkg_config_name", "CHOLMOD")

self.cpp_info.libs = ["cholmod"]
self.cpp_info.includedirs.append(os.path.join("include", "suitesparse"))

if self.settings.os in ["Linux", "FreeBSD"]:
self.cpp_info.system_libs.append("m")

if not self.options.gpl:
self.cpp_info.defines.append("NGPL")
if self.options.cuda:
self.cpp_info.defines.append("CHOLMOD_HAS_CUDA")
8 changes: 8 additions & 0 deletions recipes/suitesparse-cholmod/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.15)
project(test_package LANGUAGES C)

find_package(CHOLMOD REQUIRED CONFIG)

add_executable(${PROJECT_NAME} test_package.c)
target_link_libraries(${PROJECT_NAME} PRIVATE SuiteSparse::CHOLMOD)
target_compile_features(${PROJECT_NAME} PRIVATE c_std_11)
7 changes: 7 additions & 0 deletions recipes/suitesparse-cholmod/all/test_package/c.mtx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%%MatrixMarket matrix coordinate complex hermitian
3 3 5
1 1 1. 0.
3 1 2. -1.
2 2 1. 0.
3 2 3. 0.
3 3 42. 0.
27 changes: 27 additions & 0 deletions recipes/suitesparse-cholmod/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from conan import ConanFile
from conan.tools.build import can_run
from conan.tools.cmake import cmake_layout, CMake
import os


class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "CMakeDeps", "CMakeToolchain", "VirtualRunEnv"
test_type = "explicit"

def requirements(self):
self.requires(self.tested_reference_str)

def layout(self):
cmake_layout(self)

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if can_run(self):
bin_path = os.path.join(self.cpp.build.bindir, "test_package")
test_mtx = os.path.join(self.source_folder, "c.mtx")
self.run(f"{bin_path} < {test_mtx}", env="conanrun")
88 changes: 88 additions & 0 deletions recipes/suitesparse-cholmod/all/test_package/test_package.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// https://github.com/DrTimothyAldenDavis/SuiteSparse/blob/v7.7.0/CHOLMOD/Demo/cholmod_si_simple.c

//------------------------------------------------------------------------------
// CHOLMOD/Demo/cholmod_si_simple: simple demo program for CHOLMOD
//------------------------------------------------------------------------------

// CHOLMOD/Demo Module. Copyright (C) 2005-2023, Timothy A. Davis,
// All Rights Reserved.
// SPDX-License-Identifier: GPL-2.0+

//------------------------------------------------------------------------------

// Read in a real symmetric or complex Hermitian matrix from stdin in
// MatrixMarket format, solve Ax=b where b=[1 1 ... 1]', and print the residual.
//
// Usage: cholmod_si_simple < matrixfile
//
// There are four versions of this demo:
// cholmod_di_simple: double, int32
// cholmod_dl_simple: double, int64
// cholmod_si_simple: float, int32
// cholmod_sl_simple: float, int64

#include <cholmod.h>

#include <stdio.h>

int main (void)
{
cholmod_sparse *A ;
cholmod_dense *x, *b, *r ;
cholmod_factor *L ;
double one [2] = {1,0}, m1 [2] = {-1,0} ; // basic scalars
cholmod_common c ;
cholmod_start (&c) ; // start CHOLMOD
int dtype = CHOLMOD_SINGLE ; // use single precision
A = cholmod_read_sparse2 (stdin, dtype, &c) ; // read in a matrix
c.precise = true ;
c.print = (A->nrow > 5) ? 3 : 5 ;
cholmod_print_sparse (A, "A", &c) ; // print the matrix
if (A == NULL || A->stype == 0) // A must be symmetric
{
cholmod_free_sparse (&A, &c) ;
cholmod_finish (&c) ;
return (0) ;
}
b = cholmod_ones (A->nrow, 1, A->xtype + dtype, &c) ; // b = ones(n,1)

double t1 = SUITESPARSE_TIME ;
L = cholmod_analyze (A, &c) ; // analyze
t1 = SUITESPARSE_TIME - t1 ;
double t2 = SUITESPARSE_TIME ;
cholmod_factorize (A, L, &c) ; // factorize
t2 = SUITESPARSE_TIME - t2 ;
double t3 = SUITESPARSE_TIME ;
x = cholmod_solve (CHOLMOD_A, L, b, &c) ; // solve Ax=b
t3 = SUITESPARSE_TIME - t3 ;
printf ("analyze time: %10.3f sec\n", t1) ;
printf ("factorize time: %10.3f sec\n", t2) ;
printf ("solve time: %10.3f sec\n", t3) ;
printf ("total time: %10.3f sec\n", t1 + t2 + t3) ;

cholmod_print_factor (L, "L", &c) ; // print the factorization
cholmod_print_dense (x, "x", &c) ; // print the solution
r = cholmod_copy_dense (b, &c) ; // r = b
#ifndef NMATRIXOPS
cholmod_sdmult (A, 0, m1, one, x, r, &c) ; // r = r-Ax
double rnorm = cholmod_norm_dense (r, 0, &c) ; // compute inf-norm of r
double anorm = cholmod_norm_sparse (A, 0, &c) ; // compute inf-norm of A
printf ("\n%s precision results:\n", dtype ? "single" : "double") ;
printf ("norm(b-Ax) %8.1e\n", rnorm) ;
printf ("norm(A) %8.1e\n", anorm) ;
double relresid = rnorm / anorm ;
printf ("resid: norm(b-Ax)/norm(A) %8.1e\n", relresid) ;
fprintf (stderr, "resid: norm(b-Ax)/norm(A) %8.1e\n", relresid) ;
#else
printf ("residual norm not computed (requires CHOLMOD/MatrixOps)\n") ;
#endif
cholmod_free_factor (&L, &c) ; // free matrices
cholmod_free_sparse (&A, &c) ;
cholmod_free_dense (&r, &c) ;
cholmod_free_dense (&x, &c) ;
cholmod_free_dense (&b, &c) ;
cholmod_print_common ("common", &c) ;
cholmod_gpu_stats (&c) ;
cholmod_finish (&c) ; // finish CHOLMOD
return (0) ;
}
3 changes: 3 additions & 0 deletions recipes/suitesparse-cholmod/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
versions:
"5.2.1":
folder: all

0 comments on commit 8dac0cc

Please sign in to comment.