Skip to content

Commit 21a906f

Browse files
committed
Merge branch 'main' into junikimm717/binsparse-pr
2 parents 5ac75e6 + 00eb7cc commit 21a906f

File tree

13 files changed

+139
-34
lines changed

13 files changed

+139
-34
lines changed

.github/workflows/ci.yml

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,33 @@ jobs:
2626

2727
gcc:
2828
runs-on: 'ubuntu-latest'
29-
strategy:
30-
matrix:
31-
cxx: [gcc, clang]
32-
name: ${{ matrix.cxx }}
3329
env:
34-
CXX: ${{ matrix.cxx }}
30+
CXX: g++-12
31+
CC: gcc-12
3532
steps:
3633
- uses: actions/checkout@v4
3734
- name: CMake
3835
run: |
3936
sudo apt-get update
40-
sudo apt-get install libhdf5-dev g++-12
41-
CXX=g++-12 cmake -B build
37+
sudo apt-get install libhdf5-dev $CXX $CC
38+
cmake -B build
39+
- name: Build
40+
run: VERBOSE=true make -C build -j `nproc`
41+
- name: Test
42+
run: ctest --test-dir ./build/test/bash
43+
44+
clang:
45+
runs-on: 'ubuntu-latest'
46+
env:
47+
CXX: clang++
48+
CC: clang
49+
steps:
50+
- uses: actions/checkout@v4
51+
- name: CMake
52+
run: |
53+
sudo apt-get update
54+
sudo apt-get install libhdf5-dev clang
55+
cmake -B build
4256
- name: Build
4357
run: VERBOSE=true make -C build -j `nproc`
4458
- name: Test

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
scripts
66
venv
7-
build
7+
build*
88
._*
99
tensor_test_files

CMakeLists.txt

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,88 @@
22
#
33
# SPDX-License-Identifier: BSD-3-Clause
44

5-
cmake_minimum_required(VERSION 3.5)
6-
project(binsparse-rc)
5+
cmake_minimum_required(VERSION 3.20)
6+
project(binsparse VERSION 1.0.0)
77

88
cmake_policy(SET CMP0079 NEW)
99

1010
set(CMAKE_C_STANDARD 11)
11-
1211
set(CMAKE_CXX_STANDARD 20)
13-
1412
set(CMAKE_C_FLAGS "-O3 -march=native")
1513

16-
add_library(binsparse-rc SHARED)
14+
include(GNUInstallDirs)
15+
16+
add_library(binsparse STATIC)
1717

1818
add_subdirectory(include)
1919
add_subdirectory(src)
2020

2121
# NOTE: For now, both HDF5 and cJSON are `PUBLIC`, meaning that anything that
22-
# depends on `binsparse-rc` will also link/include HDF5 and cJSON. We can change
23-
# these to `PRIVATE` to use them only when building binsparse-rc.
22+
# depends on `binsparse` will also link/include HDF5 and cJSON. We can change
23+
# these to `PRIVATE` to use them only when building binsparse.
2424

2525
find_package(HDF5 REQUIRED COMPONENTS C)
26-
target_link_libraries(binsparse-rc PUBLIC ${HDF5_C_LIBRARIES})
27-
target_include_directories(binsparse-rc PUBLIC . ${HDF5_INCLUDE_DIRS})
26+
target_link_libraries(binsparse PUBLIC ${HDF5_C_LIBRARIES})
2827

2928
include(FetchContent)
3029
FetchContent_Declare(
3130
cJSON
32-
GIT_REPOSITORY https://github.com/DaveGamble/cJSON.git
33-
GIT_TAG v1.7.17
31+
# GIT_REPOSITORY https://github.com/DaveGamble/cJSON.git
32+
GIT_REPOSITORY https://github.com/p1k0chu/cJSON.git
33+
GIT_TAG 887642c0a93bd8a6616bf90daacac0ea7d4b095e
3434
)
3535
FetchContent_MakeAvailable(cJSON)
3636

3737
configure_file(${cJSON_SOURCE_DIR}/cJSON.h ${CMAKE_BINARY_DIR}/include/cJSON/cJSON.h)
38-
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_BINARY_DIR}/include)
3938
target_link_libraries(${PROJECT_NAME} PUBLIC cjson)
4039

40+
# Set up include directories properly for both build and install
41+
target_include_directories(${PROJECT_NAME}
42+
PUBLIC
43+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
44+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
45+
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/include>
46+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
47+
${HDF5_INCLUDE_DIRS})
48+
4149
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
4250
add_subdirectory(examples)
4351
add_subdirectory(test)
4452
endif()
53+
54+
# Installation rules - these are always needed when the library is built
55+
install(TARGETS binsparse
56+
EXPORT binsparse-targets
57+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
58+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
59+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
60+
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
61+
62+
# Install headers
63+
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/binsparse
64+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
65+
FILES_MATCHING PATTERN "*.h"
66+
PATTERN "*.hpp")
67+
68+
# Export targets
69+
install(EXPORT binsparse-targets
70+
FILE binsparse-targets.cmake
71+
NAMESPACE binsparse::
72+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/binsparse)
73+
74+
# Create and install package config files
75+
include(CMakePackageConfigHelpers)
76+
write_basic_package_version_file(
77+
"${CMAKE_CURRENT_BINARY_DIR}/binsparse-config-version.cmake"
78+
VERSION ${PROJECT_VERSION}
79+
COMPATIBILITY SameMajorVersion)
80+
81+
configure_package_config_file(
82+
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/binsparse-config.cmake.in"
83+
"${CMAKE_CURRENT_BINARY_DIR}/binsparse-config.cmake"
84+
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/binsparse)
85+
86+
install(FILES
87+
"${CMAKE_CURRENT_BINARY_DIR}/binsparse-config.cmake"
88+
"${CMAKE_CURRENT_BINARY_DIR}/binsparse-config-version.cmake"
89+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/binsparse)

cmake/binsparse-config.cmake.in

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# SPDX-FileCopyrightText: 2024 Binsparse Developers
2+
#
3+
# SPDX-License-Identifier: BSD-3-Clause
4+
5+
@PACKAGE_INIT@
6+
7+
include("${CMAKE_CURRENT_LIST_DIR}/binsparse-targets.cmake")
8+
check_required_components(binsparse)
9+
10+
# Include dependencies
11+
include(CMakeFindDependencyMacro)
12+
find_dependency(HDF5 COMPONENTS C)
13+
14+
# cJSON is bundled with binsparse, no need to find it externally

examples/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
function(add_example example_name)
66
add_executable(${example_name} ${example_name}.c)
7-
target_link_libraries(${example_name} binsparse-rc)
7+
target_link_libraries(${example_name} binsparse)
88
endfunction()
99

1010
add_example(simple_matrix_write)

examples/bsp2mtx.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ int main(int argc, char** argv) {
2121
printf(" === Reading file... ===\n");
2222
bsp_matrix_t matrix = bsp_read_matrix(input_fname, NULL);
2323
printf(" === Done writing. ===\n");
24+
if (matrix.format != BSP_COO) {
25+
matrix = bsp_convert_matrix(matrix, BSP_COO);
26+
}
2427

2528
printf(" === Writing to %s... ===\n", output_fname);
2629
bsp_mmwrite(output_fname, matrix);

examples/cpp/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
function(add_example example_name)
66
add_executable(${example_name}-cpp ${example_name}.cpp)
7-
target_link_libraries(${example_name}-cpp binsparse-rc)
7+
target_link_libraries(${example_name}-cpp binsparse)
88
endfunction()
99

1010
add_example(simple_matrix_write)

include/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
#
33
# SPDX-License-Identifier: BSD-3-Clause
44

5-
target_include_directories(binsparse-rc PUBLIC .)
5+
# No need for target_include_directories here - moved to main CMakeLists.txt

include/binsparse/detail/cpp/array.hpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ inline array_ptr_variant_t get_typed_ptr(bsp_array_t array) {
6363
return {};
6464
}
6565

66+
template <typename T>
67+
constexpr static bool is_c_complex_v =
68+
std::is_same_v<std::remove_cvref_t<T>, _Complex double> ||
69+
std::is_same_v<std::remove_cvref_t<T>, _Complex float>;
70+
71+
template <typename T, typename U>
72+
constexpr static bool mixed_c_complex_v =
73+
(is_c_complex_v<T> && !is_c_complex_v<U>) ||
74+
(!is_c_complex_v<T> && is_c_complex_v<U>);
75+
6676
} // namespace __detail
6777

6878
} // namespace binsparse
@@ -76,7 +86,8 @@ inline void bsp_array_read(bsp_array_t array, size_t index, T& value) {
7686
[&](auto* ptr) {
7787
using U = std::remove_pointer_t<decltype(ptr)>;
7888

79-
if constexpr (std::is_assignable_v<T&, U>) {
89+
if constexpr (std::is_convertible_v<U, T> &&
90+
!binsparse::__detail::mixed_c_complex_v<T, U>) {
8091
value = ptr[index];
8192
}
8293
},
@@ -92,7 +103,8 @@ inline void bsp_array_write(bsp_array_t array, size_t index, U value) {
92103
[&](auto* ptr) {
93104
using T = std::remove_pointer_t<decltype(ptr)>;
94105

95-
if constexpr (std::is_assignable_v<T&, U>) {
106+
if constexpr (std::is_convertible_v<U, T> &&
107+
!binsparse::__detail::mixed_c_complex_v<T, U>) {
96108
ptr[index] = value;
97109
}
98110
},
@@ -110,7 +122,8 @@ inline void bsp_array_awrite(bsp_array_t array_0, size_t index_0,
110122
using T = std::remove_pointer_t<decltype(ptr_0)>;
111123
using U = std::remove_pointer_t<decltype(ptr_1)>;
112124

113-
if constexpr (std::is_assignable_v<T&, U>) {
125+
if constexpr (std::is_convertible_v<U, T> &&
126+
!binsparse::__detail::mixed_c_complex_v<T, U>) {
114127
ptr_0[index_0] = ptr_1[index_1];
115128
}
116129
},

include/binsparse/detail/declamp_values.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ static inline void bsp_matrix_declamp_values(bsp_matrix_t matrix) {
4242
real_value = bsp_suitesparse_declamp_impl_(real_value);
4343
imaginary_value = bsp_suitesparse_declamp_impl_(imaginary_value);
4444

45-
values[i] = real_value + 1j * imaginary_value;
45+
double _Complex complex_value;
46+
47+
((double*) &complex_value)[0] = real_value;
48+
((double*) &complex_value)[1] = imaginary_value;
49+
50+
values[i] = complex_value;
4651
}
4752
}
4853
}

0 commit comments

Comments
 (0)