Skip to content
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

suitesparse-cholmod: new recipe #23553

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Reproduces the CUDA dependencies added by https://github.com/DrTimothyAldenDavis/SuiteSparse/blob/v7.7.0/CHOLMOD/GPU/CMakeLists.txt

find_dependency(CUDAToolkit REQUIRED)

target_link_libraries(SuiteSparse::CHOLMOD INTERFACE CUDA::nvrtc CUDA::cudart_static CUDA::cublas)
target_compile_definitions(SuiteSparse::CHOLMOD INTERFACE "CHOLMOD_HAS_CUDA")
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"
147 changes: 147 additions & 0 deletions recipes/suitesparse-cholmod/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import os

from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
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, save

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],
"cuda": [True, False],
"build_matrixops": [True, False],
"build_modify": [True, False],
"build_partition": [True, False],
"build_supernodal": [True, False],
}
default_options = {
"shared": False,
"fPIC": True,
"cuda": False,
"build_matrixops": True,
"build_modify": True,
"build_partition": True,
"build_supernodal": True,
}

def export_sources(self):
copy(self, "cholmod-conan-cuda-support.cmake", self.recipe_folder, self.export_sources_folder)

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

@property
def _license_is_gpl(self):
return self.options.cuda or self.options.build_matrixops or self.options.build_modify or self.options.build_supernodal

def configure(self):
if self.options.shared:
self.options.rm_safe("fPIC")
self.settings.rm_safe("compiler.cppstd")
self.settings.rm_safe("compiler.libcxx")
if not self._license_is_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 ConanInvalidConfiguration("CUDA acceleration requires GPL-licensed modules. Set suitesparse-cholmod/*:gpl=True.")
if self.options.build_supernodal and not self.dependencies["openblas"].options.build_lapack:
raise ConanInvalidConfiguration("-o openblas/*:build_lapack=True is required when build_supernodal=True")

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._license_is_gpl
tc.variables["CHOLMOD_MATRIXOPS"] = self.options.build_matrixops
tc.variables["CHOLMOD_MODIFY"] = self.options.build_modify
tc.variables["CHOLMOD_PARTITION"] = self.options.build_partition
tc.variables["CHOLMOD_SUPERNODAL"] = self.options.build_supernodal
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
# FIXME: Find a way to not hardcode this. The system BLAS gets used otherwise.
tc.variables["BLAS_LIBRARIES"] = "OpenBLAS::OpenBLAS"
tc.variables["LAPACK_LIBRARIES"] = "OpenBLAS::OpenBLAS"
tc.variables["LAPACK_FOUND"] = True
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)
if self.options.cuda:
copy(self, "cholmod-conan-cuda-support.cmake", self.export_sources_folder, os.path.join(self.package_folder, "lib", "cmake"))

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")

if self.options.cuda:
self.cpp_info.builddirs.append(os.path.join("lib", "cmake"))
self.cpp_info.set_property("cmake_build_modules", [os.path.join("lib", "cmake", "cholmod-conan-cuda-support.cmake")])

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._license_is_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