Skip to content

Commit cb95ba4

Browse files
authored
Refactor codebase (#15)
* Change functions in `array.h` and `array.hpp` to inline. * Much more inline * Update---`static inline` * Make `bsp_read_matrix` statically linked instead of header-only. * Make `write_matrix` separately compiled
1 parent be5ac98 commit cb95ba4

23 files changed

+539
-460
lines changed

Diff for: CMakeLists.txt

+11-4
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,18 @@ set(CMAKE_CXX_STANDARD 20)
1313

1414
set(CMAKE_C_FLAGS "-O3 -march=native")
1515

16+
add_library(binsparse-rc STATIC)
17+
1618
add_subdirectory(include)
19+
add_subdirectory(src)
20+
21+
# 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.
1724

1825
find_package(HDF5 REQUIRED COMPONENTS C)
19-
target_link_libraries(binsparse-rc INTERFACE ${HDF5_C_LIBRARIES})
20-
target_include_directories(binsparse-rc INTERFACE . ${HDF5_INCLUDE_DIRS})
26+
target_link_libraries(binsparse-rc PUBLIC ${HDF5_C_LIBRARIES})
27+
target_include_directories(binsparse-rc PUBLIC . ${HDF5_INCLUDE_DIRS})
2128

2229
include(FetchContent)
2330
FetchContent_Declare(
@@ -28,8 +35,8 @@ FetchContent_Declare(
2835
FetchContent_MakeAvailable(cJSON)
2936

3037
configure_file(${cJSON_SOURCE_DIR}/cJSON.h ${CMAKE_BINARY_DIR}/include/cJSON/cJSON.h)
31-
target_include_directories(${PROJECT_NAME} INTERFACE ${CMAKE_BINARY_DIR}/include)
32-
target_link_libraries(${PROJECT_NAME} INTERFACE cjson)
38+
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_BINARY_DIR}/include)
39+
target_link_libraries(${PROJECT_NAME} PUBLIC cjson)
3340

3441
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
3542
add_subdirectory(examples)

Diff for: include/CMakeLists.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
#
33
# SPDX-License-Identifier: BSD-3-Clause
44

5-
add_library(binsparse-rc INTERFACE)
6-
target_include_directories(binsparse-rc INTERFACE .)
5+
target_include_directories(binsparse-rc PUBLIC .)

Diff for: include/binsparse/array.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ typedef struct bsp_array_t {
2020
bsp_allocator_t allocator;
2121
} bsp_array_t;
2222

23-
bsp_array_t bsp_construct_default_array_t() {
23+
static inline bsp_array_t bsp_construct_default_array_t() {
2424
bsp_array_t array;
2525
array.data = NULL;
2626
array.size = 0;
2727
array.allocator = bsp_default_allocator;
2828
return array;
2929
}
3030

31-
bsp_array_t bsp_construct_array_t(size_t size, bsp_type_t type) {
31+
static inline bsp_array_t bsp_construct_array_t(size_t size, bsp_type_t type) {
3232
size_t byte_size = size * bsp_type_size(type);
3333

3434
bsp_array_t array;
@@ -41,14 +41,14 @@ bsp_array_t bsp_construct_array_t(size_t size, bsp_type_t type) {
4141
return array;
4242
}
4343

44-
bsp_array_t bsp_copy_construct_array_t(bsp_array_t other) {
44+
static inline bsp_array_t bsp_copy_construct_array_t(bsp_array_t other) {
4545
bsp_array_t array = bsp_construct_array_t(other.size, other.type);
4646
memcpy(array.data, other.data, other.size * bsp_type_size(other.type));
4747

4848
return array;
4949
}
5050

51-
bsp_array_t bsp_complex_array_to_fp(bsp_array_t other) {
51+
static inline bsp_array_t bsp_complex_array_to_fp(bsp_array_t other) {
5252
assert(other.type == BSP_COMPLEX_FLOAT32 ||
5353
other.type == BSP_COMPLEX_FLOAT64);
5454

@@ -66,7 +66,7 @@ bsp_array_t bsp_complex_array_to_fp(bsp_array_t other) {
6666
return array;
6767
}
6868

69-
bsp_array_t bsp_fp_array_to_complex(bsp_array_t other) {
69+
static inline bsp_array_t bsp_fp_array_to_complex(bsp_array_t other) {
7070
assert(other.type == BSP_FLOAT32 || other.type == BSP_FLOAT64);
7171

7272
bsp_array_t array;
@@ -83,11 +83,11 @@ bsp_array_t bsp_fp_array_to_complex(bsp_array_t other) {
8383
return array;
8484
}
8585

86-
void bsp_destroy_array_t(bsp_array_t array) {
86+
static inline void bsp_destroy_array_t(bsp_array_t array) {
8787
array.allocator.free(array.data);
8888
}
8989

90-
bool bsp_array_equal(bsp_array_t x, bsp_array_t y) {
90+
static inline bool bsp_array_equal(bsp_array_t x, bsp_array_t y) {
9191
if (x.size != y.size) {
9292
return false;
9393
}

Diff for: include/binsparse/convert_matrix.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#include <assert.h>
1010
#include <binsparse/matrix.h>
1111

12-
bsp_matrix_t bsp_convert_matrix(bsp_matrix_t matrix,
13-
bsp_matrix_format_t format) {
12+
static inline bsp_matrix_t bsp_convert_matrix(bsp_matrix_t matrix,
13+
bsp_matrix_format_t format) {
1414
// Throw an error if matrix already in desired format.
1515
if (matrix.format == format) {
1616
assert(false);

Diff for: include/binsparse/detail/cpp/array.hpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ using array_ptr_variant_t =
1919
int32_t*, int64_t*, float*, double*, float _Complex*,
2020
double _Complex*>;
2121

22-
array_ptr_variant_t get_typed_ptr(bsp_array_t array) {
22+
inline array_ptr_variant_t get_typed_ptr(bsp_array_t array) {
2323
if (array.type == BSP_UINT8) {
2424
uint8_t* data = (uint8_t*) array.data;
2525
return data;
@@ -69,7 +69,7 @@ array_ptr_variant_t get_typed_ptr(bsp_array_t array) {
6969

7070
// value = array[index]
7171
template <typename T>
72-
void bsp_array_read(bsp_array_t array, size_t index, T& value) {
72+
inline void bsp_array_read(bsp_array_t array, size_t index, T& value) {
7373
auto variant_ptr = binsparse::__detail::get_typed_ptr(array);
7474

7575
std::visit(
@@ -85,7 +85,7 @@ void bsp_array_read(bsp_array_t array, size_t index, T& value) {
8585

8686
// array[index] = value
8787
template <typename U>
88-
void bsp_array_write(bsp_array_t array, size_t index, U value) {
88+
inline void bsp_array_write(bsp_array_t array, size_t index, U value) {
8989
auto variant_ptr = binsparse::__detail::get_typed_ptr(array);
9090

9191
std::visit(
@@ -100,8 +100,8 @@ void bsp_array_write(bsp_array_t array, size_t index, U value) {
100100
}
101101

102102
// array_0[index_0] = array_1[index_1]
103-
void bsp_array_awrite(bsp_array_t array_0, size_t index_0, bsp_array_t array_1,
104-
size_t index_1) {
103+
inline void bsp_array_awrite(bsp_array_t array_0, size_t index_0,
104+
bsp_array_t array_1, size_t index_1) {
105105
auto variant_ptr_0 = binsparse::__detail::get_typed_ptr(array_0);
106106
auto variant_ptr_1 = binsparse::__detail::get_typed_ptr(array_1);
107107

Diff for: include/binsparse/detail/declamp_values.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <binsparse/matrix.h>
1010
#include <math.h>
1111

12-
double bsp_suitesparse_declamp_impl_(double value) {
12+
static inline double bsp_suitesparse_declamp_impl_(double value) {
1313
const double HUGE_DOUBLE = 1e308;
1414
if (value >= HUGE_DOUBLE) {
1515
return INFINITY;
@@ -26,7 +26,7 @@ double bsp_suitesparse_declamp_impl_(double value) {
2626
// Here, we "declamp" these values to restore them to infinity.
2727
// This allows the `bsp_matrix_minimize_values` to properly minimize
2828
// matrices that have infinity values.
29-
void bsp_matrix_declamp_values(bsp_matrix_t matrix) {
29+
static inline void bsp_matrix_declamp_values(bsp_matrix_t matrix) {
3030
if (matrix.values.type == BSP_FLOAT64) {
3131
double* values = (double*) matrix.values.data;
3232

Diff for: include/binsparse/detail/parse_dataset.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ typedef struct {
1313
char* dataset;
1414
} bsp_fdataset_info_t;
1515

16-
bsp_fdataset_info_t bsp_parse_fdataset_string(char* str) {
16+
static inline bsp_fdataset_info_t bsp_parse_fdataset_string(char* str) {
1717
size_t len = strlen(str);
1818

1919
int split = -1;
@@ -41,7 +41,7 @@ bsp_fdataset_info_t bsp_parse_fdataset_string(char* str) {
4141
}
4242
}
4343

44-
const char* bsp_get_file_extension(const char* file_name) {
44+
static inline const char* bsp_get_file_extension(const char* file_name) {
4545
int64_t len = strlen(file_name);
4646
for (int64_t i = len - 1; i >= 0; i--) {
4747
if (file_name[i] == '.') {

Diff for: include/binsparse/detail/shm_tools.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ typedef struct {
2020
size_t size;
2121
} bsp_shm_t;
2222

23-
bsp_shm_t bsp_shm_new(size_t size) {
23+
static inline bsp_shm_t bsp_shm_new(size_t size) {
2424
bsp_shm_t shm;
2525
shm.size = size;
2626

@@ -32,11 +32,11 @@ bsp_shm_t bsp_shm_new(size_t size) {
3232
return shm;
3333
}
3434

35-
void bsp_shm_delete(bsp_shm_t shm) {
35+
static inline void bsp_shm_delete(bsp_shm_t shm) {
3636
shmctl(shm.id, IPC_RMID, 0);
3737
}
3838

39-
void* bsp_shm_attach(bsp_shm_t shm) {
39+
static inline void* bsp_shm_attach(bsp_shm_t shm) {
4040
void* data;
4141

4242
if ((data = shmat(shm.id, NULL, 0)) == (void*) -1) {
@@ -46,11 +46,11 @@ void* bsp_shm_attach(bsp_shm_t shm) {
4646
return data;
4747
}
4848

49-
void bsp_shm_detach(void* data) {
49+
static inline void bsp_shm_detach(void* data) {
5050
shmdt(data);
5151
}
5252

53-
void* bsp_shm_malloc(size_t size) {
53+
static inline void* bsp_shm_malloc(size_t size) {
5454
bsp_shm_t shm_id = bsp_shm_new(size);
5555

5656
void* ptr = bsp_shm_attach(shm_id);
@@ -60,7 +60,7 @@ void* bsp_shm_malloc(size_t size) {
6060
return ptr;
6161
}
6262

63-
void bsp_shm_free(void* ptr) {
63+
static inline void bsp_shm_free(void* ptr) {
6464
bsp_shm_detach(ptr);
6565
}
6666

Diff for: include/binsparse/generate.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include <binsparse/matrix.h>
1010

11-
void bsp_array_fill_random(bsp_array_t array, size_t bound) {
11+
static inline void bsp_array_fill_random(bsp_array_t array, size_t bound) {
1212
if (array.type == BSP_UINT8) {
1313
uint8_t* values = (uint8_t*) array.data;
1414
for (size_t i = 0; i < array.size; i++) {
@@ -67,8 +67,9 @@ void bsp_array_fill_random(bsp_array_t array, size_t bound) {
6767
}
6868
}
6969

70-
bsp_matrix_t bsp_generate_coo(size_t m, size_t n, size_t nnz,
71-
bsp_type_t value_type, bsp_type_t index_type) {
70+
static inline bsp_matrix_t bsp_generate_coo(size_t m, size_t n, size_t nnz,
71+
bsp_type_t value_type,
72+
bsp_type_t index_type) {
7273
bsp_matrix_t matrix = bsp_construct_default_matrix_t();
7374
matrix.nrows = m;
7475
matrix.ncols = n;

Diff for: include/binsparse/hdf5_wrapper.h

+8-7
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121

2222
// Write an array to a dataset / file
2323
// Returns 0 on success, nonzero on error.
24-
int bsp_write_array(hid_t f, const char* label, bsp_array_t array,
25-
int compression_level) {
24+
static inline int bsp_write_array(hid_t f, const char* label, bsp_array_t array,
25+
int compression_level) {
2626
if (array.type == BSP_COMPLEX_FLOAT32 || array.type == BSP_COMPLEX_FLOAT64) {
2727
array = bsp_complex_array_to_fp(array);
2828
}
@@ -76,8 +76,8 @@ int bsp_write_array(hid_t f, const char* label, bsp_array_t array,
7676
}
7777

7878
#if __STDC_VERSION__ >= 201112L
79-
bsp_array_t bsp_read_array_parallel(hid_t f, const char* label,
80-
int num_threads) {
79+
static inline bsp_array_t bsp_read_array_parallel(hid_t f, const char* label,
80+
int num_threads) {
8181
hid_t dset = H5Dopen2(f, label, H5P_DEFAULT);
8282

8383
if (dset == H5I_INVALID_HID) {
@@ -173,7 +173,7 @@ bsp_array_t bsp_read_array_parallel(hid_t f, const char* label,
173173
}
174174
#endif
175175

176-
bsp_array_t bsp_read_array(hid_t f, const char* label) {
176+
static inline bsp_array_t bsp_read_array(hid_t f, const char* label) {
177177
hid_t dset = H5Dopen2(f, label, H5P_DEFAULT);
178178

179179
if (dset == H5I_INVALID_HID) {
@@ -212,7 +212,8 @@ bsp_array_t bsp_read_array(hid_t f, const char* label) {
212212
return array;
213213
}
214214

215-
void bsp_write_attribute(hid_t f, const char* label, const char* string) {
215+
static inline void bsp_write_attribute(hid_t f, const char* label,
216+
const char* string) {
216217
hid_t strtype = H5Tcopy(H5T_C_S1);
217218
H5Tset_size(strtype, strlen(string));
218219
H5Tset_cset(strtype, H5T_CSET_UTF8);
@@ -228,7 +229,7 @@ void bsp_write_attribute(hid_t f, const char* label, const char* string) {
228229
H5Sclose(dataspace);
229230
}
230231

231-
char* bsp_read_attribute(hid_t f, const char* label) {
232+
static inline char* bsp_read_attribute(hid_t f, const char* label) {
232233
hid_t attribute = H5Aopen(f, label, H5P_DEFAULT);
233234
hid_t strtype = H5Aget_type(attribute);
234235

Diff for: include/binsparse/matrix.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ typedef struct bsp_matrix_t {
2626
bsp_structure_t structure;
2727
} bsp_matrix_t;
2828

29-
bsp_matrix_t bsp_construct_default_matrix_t() {
29+
static inline bsp_matrix_t bsp_construct_default_matrix_t() {
3030
bsp_matrix_t mat;
3131
mat.values = bsp_construct_default_array_t();
3232
mat.indices_0 = bsp_construct_default_array_t();
@@ -38,14 +38,14 @@ bsp_matrix_t bsp_construct_default_matrix_t() {
3838
return mat;
3939
}
4040

41-
void bsp_destroy_matrix_t(bsp_matrix_t matrix) {
41+
static inline void bsp_destroy_matrix_t(bsp_matrix_t matrix) {
4242
bsp_destroy_array_t(matrix.values);
4343
bsp_destroy_array_t(matrix.indices_0);
4444
bsp_destroy_array_t(matrix.indices_1);
4545
bsp_destroy_array_t(matrix.pointers_to_1);
4646
}
4747

48-
size_t bsp_matrix_nbytes(bsp_matrix_t mat) {
48+
static inline size_t bsp_matrix_nbytes(bsp_matrix_t mat) {
4949
size_t nbytes = 0;
5050
if (mat.values.size > 0) {
5151
nbytes += mat.values.size * bsp_type_size(mat.values.type);
@@ -66,7 +66,7 @@ size_t bsp_matrix_nbytes(bsp_matrix_t mat) {
6666
return nbytes;
6767
}
6868

69-
void bsp_print_matrix_info(bsp_matrix_t matrix) {
69+
static inline void bsp_print_matrix_info(bsp_matrix_t matrix) {
7070
printf("%lu x %lu matrix with %lu nnz.\n", matrix.nrows, matrix.ncols,
7171
matrix.nnz);
7272
printf("%s format with %s structure\n",

Diff for: include/binsparse/matrix_formats.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ typedef enum bsp_matrix_format_t {
2424
BSP_INVALID_FORMAT = 21
2525
} bsp_matrix_format_t;
2626

27-
char* bsp_get_matrix_format_string(bsp_matrix_format_t format) {
27+
static inline char* bsp_get_matrix_format_string(bsp_matrix_format_t format) {
2828
if (format == BSP_DVEC) {
2929
return (char*) "DVEC";
3030
} else if (format == BSP_DMAT) {
@@ -48,7 +48,7 @@ char* bsp_get_matrix_format_string(bsp_matrix_format_t format) {
4848
}
4949
}
5050

51-
bsp_matrix_format_t bsp_get_matrix_format(char* format) {
51+
static inline bsp_matrix_format_t bsp_get_matrix_format(char* format) {
5252
if (strcmp("DVEC", format) == 0) {
5353
return BSP_DVEC;
5454
} else if (strcmp("DMAT", format) == 0) {

Diff for: include/binsparse/matrix_market/matrix_market_inspector.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ typedef struct bsp_mm_metadata {
3232
char* comments;
3333
} bsp_mm_metadata;
3434

35-
bsp_mm_metadata bsp_mmread_metadata(const char* file_path) {
35+
static inline bsp_mm_metadata bsp_mmread_metadata(const char* file_path) {
3636
FILE* f = fopen(file_path, "r");
3737

3838
assert(f != NULL);

0 commit comments

Comments
 (0)