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

Refactor codebase #15

Merged
merged 5 commits into from
Oct 30, 2024
Merged
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
15 changes: 11 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ set(CMAKE_CXX_STANDARD 20)

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

add_library(binsparse-rc STATIC)

add_subdirectory(include)
add_subdirectory(src)

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

find_package(HDF5 REQUIRED COMPONENTS C)
target_link_libraries(binsparse-rc INTERFACE ${HDF5_C_LIBRARIES})
target_include_directories(binsparse-rc INTERFACE . ${HDF5_INCLUDE_DIRS})
target_link_libraries(binsparse-rc PUBLIC ${HDF5_C_LIBRARIES})
target_include_directories(binsparse-rc PUBLIC . ${HDF5_INCLUDE_DIRS})

include(FetchContent)
FetchContent_Declare(
Expand All @@ -28,8 +35,8 @@ FetchContent_Declare(
FetchContent_MakeAvailable(cJSON)

configure_file(${cJSON_SOURCE_DIR}/cJSON.h ${CMAKE_BINARY_DIR}/include/cJSON/cJSON.h)
target_include_directories(${PROJECT_NAME} INTERFACE ${CMAKE_BINARY_DIR}/include)
target_link_libraries(${PROJECT_NAME} INTERFACE cjson)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_BINARY_DIR}/include)
target_link_libraries(${PROJECT_NAME} PUBLIC cjson)

if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
add_subdirectory(examples)
Expand Down
3 changes: 1 addition & 2 deletions include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
#
# SPDX-License-Identifier: BSD-3-Clause

add_library(binsparse-rc INTERFACE)
target_include_directories(binsparse-rc INTERFACE .)
target_include_directories(binsparse-rc PUBLIC .)
14 changes: 7 additions & 7 deletions include/binsparse/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ typedef struct bsp_array_t {
bsp_allocator_t allocator;
} bsp_array_t;

bsp_array_t bsp_construct_default_array_t() {
static inline bsp_array_t bsp_construct_default_array_t() {
bsp_array_t array;
array.data = NULL;
array.size = 0;
array.allocator = bsp_default_allocator;
return array;
}

bsp_array_t bsp_construct_array_t(size_t size, bsp_type_t type) {
static inline bsp_array_t bsp_construct_array_t(size_t size, bsp_type_t type) {
size_t byte_size = size * bsp_type_size(type);

bsp_array_t array;
Expand All @@ -41,14 +41,14 @@ bsp_array_t bsp_construct_array_t(size_t size, bsp_type_t type) {
return array;
}

bsp_array_t bsp_copy_construct_array_t(bsp_array_t other) {
static inline bsp_array_t bsp_copy_construct_array_t(bsp_array_t other) {
bsp_array_t array = bsp_construct_array_t(other.size, other.type);
memcpy(array.data, other.data, other.size * bsp_type_size(other.type));

return array;
}

bsp_array_t bsp_complex_array_to_fp(bsp_array_t other) {
static inline bsp_array_t bsp_complex_array_to_fp(bsp_array_t other) {
assert(other.type == BSP_COMPLEX_FLOAT32 ||
other.type == BSP_COMPLEX_FLOAT64);

Expand All @@ -66,7 +66,7 @@ bsp_array_t bsp_complex_array_to_fp(bsp_array_t other) {
return array;
}

bsp_array_t bsp_fp_array_to_complex(bsp_array_t other) {
static inline bsp_array_t bsp_fp_array_to_complex(bsp_array_t other) {
assert(other.type == BSP_FLOAT32 || other.type == BSP_FLOAT64);

bsp_array_t array;
Expand All @@ -83,11 +83,11 @@ bsp_array_t bsp_fp_array_to_complex(bsp_array_t other) {
return array;
}

void bsp_destroy_array_t(bsp_array_t array) {
static inline void bsp_destroy_array_t(bsp_array_t array) {
array.allocator.free(array.data);
}

bool bsp_array_equal(bsp_array_t x, bsp_array_t y) {
static inline bool bsp_array_equal(bsp_array_t x, bsp_array_t y) {
if (x.size != y.size) {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions include/binsparse/convert_matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#include <assert.h>
#include <binsparse/matrix.h>

bsp_matrix_t bsp_convert_matrix(bsp_matrix_t matrix,
bsp_matrix_format_t format) {
static inline bsp_matrix_t bsp_convert_matrix(bsp_matrix_t matrix,
bsp_matrix_format_t format) {
// Throw an error if matrix already in desired format.
if (matrix.format == format) {
assert(false);
Expand Down
10 changes: 5 additions & 5 deletions include/binsparse/detail/cpp/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ using array_ptr_variant_t =
int32_t*, int64_t*, float*, double*, float _Complex*,
double _Complex*>;

array_ptr_variant_t get_typed_ptr(bsp_array_t array) {
inline array_ptr_variant_t get_typed_ptr(bsp_array_t array) {
if (array.type == BSP_UINT8) {
uint8_t* data = (uint8_t*) array.data;
return data;
Expand Down Expand Up @@ -69,7 +69,7 @@ array_ptr_variant_t get_typed_ptr(bsp_array_t array) {

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

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

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

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

// array_0[index_0] = array_1[index_1]
void bsp_array_awrite(bsp_array_t array_0, size_t index_0, bsp_array_t array_1,
size_t index_1) {
inline void bsp_array_awrite(bsp_array_t array_0, size_t index_0,
bsp_array_t array_1, size_t index_1) {
auto variant_ptr_0 = binsparse::__detail::get_typed_ptr(array_0);
auto variant_ptr_1 = binsparse::__detail::get_typed_ptr(array_1);

Expand Down
4 changes: 2 additions & 2 deletions include/binsparse/detail/declamp_values.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <binsparse/matrix.h>
#include <math.h>

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

Expand Down
4 changes: 2 additions & 2 deletions include/binsparse/detail/parse_dataset.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ typedef struct {
char* dataset;
} bsp_fdataset_info_t;

bsp_fdataset_info_t bsp_parse_fdataset_string(char* str) {
static inline bsp_fdataset_info_t bsp_parse_fdataset_string(char* str) {
size_t len = strlen(str);

int split = -1;
Expand Down Expand Up @@ -41,7 +41,7 @@ bsp_fdataset_info_t bsp_parse_fdataset_string(char* str) {
}
}

const char* bsp_get_file_extension(const char* file_name) {
static inline const char* bsp_get_file_extension(const char* file_name) {
int64_t len = strlen(file_name);
for (int64_t i = len - 1; i >= 0; i--) {
if (file_name[i] == '.') {
Expand Down
12 changes: 6 additions & 6 deletions include/binsparse/detail/shm_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ typedef struct {
size_t size;
} bsp_shm_t;

bsp_shm_t bsp_shm_new(size_t size) {
static inline bsp_shm_t bsp_shm_new(size_t size) {
bsp_shm_t shm;
shm.size = size;

Expand All @@ -32,11 +32,11 @@ bsp_shm_t bsp_shm_new(size_t size) {
return shm;
}

void bsp_shm_delete(bsp_shm_t shm) {
static inline void bsp_shm_delete(bsp_shm_t shm) {
shmctl(shm.id, IPC_RMID, 0);
}

void* bsp_shm_attach(bsp_shm_t shm) {
static inline void* bsp_shm_attach(bsp_shm_t shm) {
void* data;

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

void bsp_shm_detach(void* data) {
static inline void bsp_shm_detach(void* data) {
shmdt(data);
}

void* bsp_shm_malloc(size_t size) {
static inline void* bsp_shm_malloc(size_t size) {
bsp_shm_t shm_id = bsp_shm_new(size);

void* ptr = bsp_shm_attach(shm_id);
Expand All @@ -60,7 +60,7 @@ void* bsp_shm_malloc(size_t size) {
return ptr;
}

void bsp_shm_free(void* ptr) {
static inline void bsp_shm_free(void* ptr) {
bsp_shm_detach(ptr);
}

Expand Down
7 changes: 4 additions & 3 deletions include/binsparse/generate.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include <binsparse/matrix.h>

void bsp_array_fill_random(bsp_array_t array, size_t bound) {
static inline void bsp_array_fill_random(bsp_array_t array, size_t bound) {
if (array.type == BSP_UINT8) {
uint8_t* values = (uint8_t*) array.data;
for (size_t i = 0; i < array.size; i++) {
Expand Down Expand Up @@ -67,8 +67,9 @@ void bsp_array_fill_random(bsp_array_t array, size_t bound) {
}
}

bsp_matrix_t bsp_generate_coo(size_t m, size_t n, size_t nnz,
bsp_type_t value_type, bsp_type_t index_type) {
static inline bsp_matrix_t bsp_generate_coo(size_t m, size_t n, size_t nnz,
bsp_type_t value_type,
bsp_type_t index_type) {
bsp_matrix_t matrix = bsp_construct_default_matrix_t();
matrix.nrows = m;
matrix.ncols = n;
Expand Down
15 changes: 8 additions & 7 deletions include/binsparse/hdf5_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

// Write an array to a dataset / file
// Returns 0 on success, nonzero on error.
int bsp_write_array(hid_t f, const char* label, bsp_array_t array,
int compression_level) {
static inline int bsp_write_array(hid_t f, const char* label, bsp_array_t array,
int compression_level) {
if (array.type == BSP_COMPLEX_FLOAT32 || array.type == BSP_COMPLEX_FLOAT64) {
array = bsp_complex_array_to_fp(array);
}
Expand Down Expand Up @@ -76,8 +76,8 @@ int bsp_write_array(hid_t f, const char* label, bsp_array_t array,
}

#if __STDC_VERSION__ >= 201112L
bsp_array_t bsp_read_array_parallel(hid_t f, const char* label,
int num_threads) {
static inline bsp_array_t bsp_read_array_parallel(hid_t f, const char* label,
int num_threads) {
hid_t dset = H5Dopen2(f, label, H5P_DEFAULT);

if (dset == H5I_INVALID_HID) {
Expand Down Expand Up @@ -173,7 +173,7 @@ bsp_array_t bsp_read_array_parallel(hid_t f, const char* label,
}
#endif

bsp_array_t bsp_read_array(hid_t f, const char* label) {
static inline bsp_array_t bsp_read_array(hid_t f, const char* label) {
hid_t dset = H5Dopen2(f, label, H5P_DEFAULT);

if (dset == H5I_INVALID_HID) {
Expand Down Expand Up @@ -212,7 +212,8 @@ bsp_array_t bsp_read_array(hid_t f, const char* label) {
return array;
}

void bsp_write_attribute(hid_t f, const char* label, const char* string) {
static inline void bsp_write_attribute(hid_t f, const char* label,
const char* string) {
hid_t strtype = H5Tcopy(H5T_C_S1);
H5Tset_size(strtype, strlen(string));
H5Tset_cset(strtype, H5T_CSET_UTF8);
Expand All @@ -228,7 +229,7 @@ void bsp_write_attribute(hid_t f, const char* label, const char* string) {
H5Sclose(dataspace);
}

char* bsp_read_attribute(hid_t f, const char* label) {
static inline char* bsp_read_attribute(hid_t f, const char* label) {
hid_t attribute = H5Aopen(f, label, H5P_DEFAULT);
hid_t strtype = H5Aget_type(attribute);

Expand Down
8 changes: 4 additions & 4 deletions include/binsparse/matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ typedef struct bsp_matrix_t {
bsp_structure_t structure;
} bsp_matrix_t;

bsp_matrix_t bsp_construct_default_matrix_t() {
static inline bsp_matrix_t bsp_construct_default_matrix_t() {
bsp_matrix_t mat;
mat.values = bsp_construct_default_array_t();
mat.indices_0 = bsp_construct_default_array_t();
Expand All @@ -38,14 +38,14 @@ bsp_matrix_t bsp_construct_default_matrix_t() {
return mat;
}

void bsp_destroy_matrix_t(bsp_matrix_t matrix) {
static inline void bsp_destroy_matrix_t(bsp_matrix_t matrix) {
bsp_destroy_array_t(matrix.values);
bsp_destroy_array_t(matrix.indices_0);
bsp_destroy_array_t(matrix.indices_1);
bsp_destroy_array_t(matrix.pointers_to_1);
}

size_t bsp_matrix_nbytes(bsp_matrix_t mat) {
static inline size_t bsp_matrix_nbytes(bsp_matrix_t mat) {
size_t nbytes = 0;
if (mat.values.size > 0) {
nbytes += mat.values.size * bsp_type_size(mat.values.type);
Expand All @@ -66,7 +66,7 @@ size_t bsp_matrix_nbytes(bsp_matrix_t mat) {
return nbytes;
}

void bsp_print_matrix_info(bsp_matrix_t matrix) {
static inline void bsp_print_matrix_info(bsp_matrix_t matrix) {
printf("%lu x %lu matrix with %lu nnz.\n", matrix.nrows, matrix.ncols,
matrix.nnz);
printf("%s format with %s structure\n",
Expand Down
4 changes: 2 additions & 2 deletions include/binsparse/matrix_formats.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ typedef enum bsp_matrix_format_t {
BSP_INVALID_FORMAT = 21
} bsp_matrix_format_t;

char* bsp_get_matrix_format_string(bsp_matrix_format_t format) {
static inline char* bsp_get_matrix_format_string(bsp_matrix_format_t format) {
if (format == BSP_DVEC) {
return (char*) "DVEC";
} else if (format == BSP_DMAT) {
Expand All @@ -48,7 +48,7 @@ char* bsp_get_matrix_format_string(bsp_matrix_format_t format) {
}
}

bsp_matrix_format_t bsp_get_matrix_format(char* format) {
static inline bsp_matrix_format_t bsp_get_matrix_format(char* format) {
if (strcmp("DVEC", format) == 0) {
return BSP_DVEC;
} else if (strcmp("DMAT", format) == 0) {
Expand Down
2 changes: 1 addition & 1 deletion include/binsparse/matrix_market/matrix_market_inspector.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ typedef struct bsp_mm_metadata {
char* comments;
} bsp_mm_metadata;

bsp_mm_metadata bsp_mmread_metadata(const char* file_path) {
static inline bsp_mm_metadata bsp_mmread_metadata(const char* file_path) {
FILE* f = fopen(file_path, "r");

assert(f != NULL);
Expand Down
Loading
Loading