Skip to content
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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.20)
cmake_minimum_required(VERSION 3.5)
project(binsparse-rc)

set(CMAKE_C_STANDARD 11)
Expand Down
164 changes: 86 additions & 78 deletions include/binsparse/array.h

Large diffs are not rendered by default.

115 changes: 115 additions & 0 deletions include/binsparse/detail/cpp/array.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#pragma once

#ifdef __cplusplus

#include <type_traits>
#include <variant>

namespace binsparse {

namespace __detail {

using array_ptr_variant_t =
std::variant<uint8_t*, uint16_t*, uint32_t*, uint64_t*, int8_t*, int16_t*,
int32_t*, int64_t*, float*, double*, float _Complex*,
double _Complex*>;

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;
} else if (array.type == BSP_UINT16) {
uint16_t* data = (uint16_t*) array.data;
return data;
} else if (array.type == BSP_UINT32) {
uint32_t* data = (uint32_t*) array.data;
return data;
} else if (array.type == BSP_UINT64) {
uint64_t* data = (uint64_t*) array.data;
return data;
} else if (array.type == BSP_INT8) {
int8_t* data = (int8_t*) array.data;
return data;
} else if (array.type == BSP_INT16) {
int16_t* data = (int16_t*) array.data;
return data;
} else if (array.type == BSP_INT32) {
int32_t* data = (int32_t*) array.data;
return data;
} else if (array.type == BSP_INT64) {
int64_t* data = (int64_t*) array.data;
return data;
} else if (array.type == BSP_FLOAT32) {
float* data = (float*) array.data;
return data;
} else if (array.type == BSP_FLOAT64) {
double* data = (double*) array.data;
return data;
} else if (array.type == BSP_BINT8) {
int8_t* data = (int8_t*) array.data;
return data;
} else if (array.type == BSP_COMPLEX_FLOAT32) {
float _Complex* data = (float _Complex*) array.data;
return data;
} else if (array.type == BSP_COMPLEX_FLOAT64) {
double _Complex* data = (double _Complex*) array.data;
return data;
}
return {};
}

} // namespace __detail

} // namespace binsparse

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

std::visit(
[&](auto* ptr) {
using U = std::remove_pointer_t<decltype(ptr)>;

if constexpr (std::is_assignable_v<T, U>) {
value = ptr[index];
}
},
variant_ptr);
}

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

std::visit(
[&](auto* ptr) {
using T = std::remove_pointer_t<decltype(ptr)>;

if constexpr (std::is_assignable_v<T, U>) {
ptr[index] = value;
}
},
variant_ptr);
}

// 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) {
auto variant_ptr_0 = binsparse::__detail::get_typed_ptr(array_0);
auto variant_ptr_1 = binsparse::__detail::get_typed_ptr(array_1);

std::visit(
[&](auto* ptr_0, auto* ptr_1) {
using T = std::remove_pointer_t<decltype(ptr_0)>;
using U = std::remove_pointer_t<decltype(ptr_1)>;

if constexpr (std::is_assignable_v<T, U>) {
ptr_0[index_0] = ptr_1[index_1];
}
},
variant_ptr_0, variant_ptr_1);
}

#endif
22 changes: 11 additions & 11 deletions include/binsparse/generate.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,57 @@

void bsp_array_fill_random(bsp_array_t array, size_t bound) {
if (array.type == BSP_UINT8) {
uint8_t* values = array.data;
uint8_t* values = (uint8_t*) array.data;
for (size_t i = 0; i < array.size; i++) {
values[i] = lrand48() % bound;
}
} else if (array.type == BSP_UINT16) {
uint16_t* values = array.data;
uint16_t* values = (uint16_t*) array.data;
for (size_t i = 0; i < array.size; i++) {
values[i] = lrand48() % bound;
}
} else if (array.type == BSP_UINT32) {
uint32_t* values = array.data;
uint32_t* values = (uint32_t*) array.data;
for (size_t i = 0; i < array.size; i++) {
values[i] = lrand48() % bound;
}
} else if (array.type == BSP_UINT64) {
uint64_t* values = array.data;
uint64_t* values = (uint64_t*) array.data;
for (size_t i = 0; i < array.size; i++) {
values[i] = lrand48() % bound;
}
} else if (array.type == BSP_INT8) {
int8_t* values = array.data;
int8_t* values = (int8_t*) array.data;
for (size_t i = 0; i < array.size; i++) {
values[i] = lrand48() % bound;
}
} else if (array.type == BSP_INT16) {
int16_t* values = array.data;
int16_t* values = (int16_t*) array.data;
for (size_t i = 0; i < array.size; i++) {
values[i] = lrand48() % bound;
}
} else if (array.type == BSP_INT32) {
int32_t* values = array.data;
int32_t* values = (int32_t*) array.data;
for (size_t i = 0; i < array.size; i++) {
values[i] = lrand48() % bound;
}
} else if (array.type == BSP_INT64) {
int64_t* values = array.data;
int64_t* values = (int64_t*) array.data;
for (size_t i = 0; i < array.size; i++) {
values[i] = lrand48() % bound;
}
} else if (array.type == BSP_FLOAT32) {
float* values = array.data;
float* values = (float*) array.data;
for (size_t i = 0; i < array.size; i++) {
values[i] = drand48() * bound;
}
} else if (array.type == BSP_FLOAT64) {
double* values = array.data;
double* values = (double*) array.data;
for (size_t i = 0; i < array.size; i++) {
values[i] = drand48() * bound;
}
} else if (array.type == BSP_BINT8) {
int8_t* values = array.data;
int8_t* values = (int8_t*) array.data;
for (size_t i = 0; i < array.size; i++) {
values[i] = lrand48() % 2;
}
Expand Down
11 changes: 8 additions & 3 deletions include/binsparse/hdf5_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ int bsp_write_array(hid_t f, char* label, bsp_array_t array,
array = bsp_complex_array_to_fp(array);
}

hsize_t hsize[1];

hsize[0] = array.size;

hid_t hdf5_standard_type = bsp_get_hdf5_standard_type(array.type);
hid_t fspace = H5Screate_simple(1, (hsize_t[]){array.size}, NULL);
hid_t fspace = H5Screate_simple(1, hsize, NULL);
hid_t lcpl = H5Pcreate(H5P_LINK_CREATE);

hid_t dcpl = H5Pcreate(H5P_DATASET_CREATE);
Expand All @@ -27,7 +31,8 @@ int bsp_write_array(hid_t f, char* label, bsp_array_t array,
chunk_size = array.size;
}

H5Pset_chunk(dcpl, 1, (hsize_t[]){chunk_size});
hsize[0] = chunk_size;
H5Pset_chunk(dcpl, 1, hsize);

if (compression_level > 0) {
H5Pset_deflate(dcpl, compression_level);
Expand Down Expand Up @@ -120,7 +125,7 @@ char* bsp_read_attribute(hid_t f, char* label) {

size_t size = H5Tget_size(strtype);

char* string = malloc(size + 1);
char* string = (char*) malloc(size + 1);

H5Aread(attribute, strtype, string);

Expand Down
25 changes: 13 additions & 12 deletions include/binsparse/matrix_formats.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,31 @@ typedef enum bsp_matrix_format_t {
BSP_DCSC = 18,
BSP_COO = 19,
BSP_COOR = 19,
BSP_COOC = 20
BSP_COOC = 20,
BSP_INVALID_FORMAT = 21
} bsp_matrix_format_t;

char* bsp_get_matrix_format_string(bsp_matrix_format_t format) {
if (format == BSP_DVEC) {
return "DVEC";
return (char*) "DVEC";
} else if (format == BSP_DMAT) {
return "DMAT";
return (char*) "DMAT";
} else if (format == BSP_DMATC) {
return "DMATC";
return (char*) "DMATC";
} else if (format == BSP_CVEC) {
return "CVEC";
return (char*) "CVEC";
} else if (format == BSP_CSR) {
return "CSR";
return (char*) "CSR";
} else if (format == BSP_DCSR) {
return "DCSR";
return (char*) "DCSR";
} else if (format == BSP_DCSC) {
return "DCSC";
return (char*) "DCSC";
} else if (format == BSP_COO) {
return "COO";
return (char*) "COO";
} else if (format == BSP_COOC) {
return "COOC";
return (char*) "COOC";
} else {
return "";
return (char*) "";
}
}

Expand Down Expand Up @@ -67,6 +68,6 @@ bsp_matrix_format_t bsp_get_matrix_format(char* format) {
} else if (strcmp("COOC", format) == 0) {
return BSP_COOC;
} else {
return 0;
return BSP_INVALID_FORMAT;
}
}
4 changes: 2 additions & 2 deletions include/binsparse/matrix_market/matrix_market_inspector.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ bsp_mm_metadata bsp_mmread_metadata(char* file_path) {

size_t comments_capacity = 2048;
size_t comments_size = 0;
char* comments = malloc(sizeof(char) * comments_capacity);
char* comments = (char*) malloc(sizeof(char) * comments_capacity);

while (!outOfComments) {
char* line = fgets(buf, 2048, f);
Expand All @@ -60,7 +60,7 @@ bsp_mm_metadata bsp_mmread_metadata(char* file_path) {
while (comments_size + strlen(line) > comments_capacity) {
comments_capacity <<= 1;
}
comments = realloc(comments, sizeof(char) * comments_capacity);
comments = (char*) realloc(comments, sizeof(char) * comments_capacity);
}

memcpy(comments + comments_size, line, strlen(line));
Expand Down
2 changes: 1 addition & 1 deletion include/binsparse/matrix_market/matrix_market_read.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ bsp_matrix_t bsp_mmread_explicit_coordinate(char* file_path,
count++;
}

size_t* indices = malloc(sizeof(size_t) * matrix.nnz);
size_t* indices = (size_t*) malloc(sizeof(size_t) * matrix.nnz);

for (size_t i = 0; i < matrix.nnz; i++) {
indices[i] = i;
Expand Down
20 changes: 10 additions & 10 deletions include/binsparse/matrix_market/matrix_market_write.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ void bsp_mmwrite(char* file_path, bsp_matrix_t matrix) {

char* structure = NULL;
if (matrix.structure == BSP_GENERAL) {
structure = "general";
structure = (char*) "general";
} else if (matrix.structure == BSP_SYMMETRIC) {
structure = "symmetric";
structure = (char*) "symmetric";
} else if (matrix.structure == BSP_HERMITIAN) {
structure = "hermitian";
structure = (char*) "hermitian";
} else if (matrix.structure == BSP_SKEW_SYMMETRIC) {
structure = "skew-symmetric";
structure = (char*) "skew-symmetric";
} else {
assert(false);
}
Expand All @@ -33,20 +33,20 @@ void bsp_mmwrite(char* file_path, bsp_matrix_t matrix) {

if (matrix.is_iso == true) {
mm_type = BSP_MM_PATTERN;
type = "pattern";
type = (char*) "pattern";
} else if ((matrix.values.type >= BSP_UINT8 &&
matrix.values.type <= BSP_INT64) ||
matrix.values.type == BSP_BINT8) {
mm_type = BSP_MM_INTEGER;
type = "integer";
type = (char*) "integer";
} else if (matrix.values.type >= BSP_FLOAT32 &&
matrix.values.type <= BSP_FLOAT64) {
mm_type = BSP_MM_REAL;
type = "real";
type = (char*) "real";
} else if (matrix.values.type == BSP_COMPLEX_FLOAT32 ||
matrix.values.type == BSP_COMPLEX_FLOAT64) {
mm_type = BSP_MM_COMPLEX;
type = "complex";
type = (char*) "complex";
} else {
assert(false);
}
Expand Down Expand Up @@ -85,8 +85,8 @@ void bsp_mmwrite(char* file_path, bsp_matrix_t matrix) {
bsp_array_read(matrix.indices_0, count, i);
bsp_array_read(matrix.indices_1, count, j);
bsp_array_read(matrix.values, count, value);
double real_value = 1.0 * value;
double complex_value = 1j * value;
double real_value = __real__ value;
double complex_value = __imag__ value;
fprintf(f, "%zu %zu %.17lg %.17lg\n", i + 1, j + 1, real_value,
complex_value);
} else {
Expand Down
10 changes: 5 additions & 5 deletions include/binsparse/read_matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
bsp_matrix_t bsp_read_matrix_from_group(hid_t f) {
bsp_matrix_t matrix = bsp_construct_default_matrix_t();

char* json_string = bsp_read_attribute(f, "binsparse");
char* json_string = bsp_read_attribute(f, (char*) "binsparse");

cJSON* j = cJSON_Parse(json_string);

Expand Down Expand Up @@ -65,7 +65,7 @@ bsp_matrix_t bsp_read_matrix_from_group(hid_t f) {
assert(data_types_ != NULL);

if (cJSON_HasObjectItem(data_types_, "values")) {
matrix.values = bsp_read_array(f, "values");
matrix.values = bsp_read_array(f, (char*) "values");

cJSON* value_type = cJSON_GetObjectItemCaseSensitive(data_types_, "values");
char* type_string = cJSON_GetStringValue(value_type);
Expand All @@ -81,15 +81,15 @@ bsp_matrix_t bsp_read_matrix_from_group(hid_t f) {
}

if (cJSON_HasObjectItem(data_types_, "indices_0")) {
matrix.indices_0 = bsp_read_array(f, "indices_0");
matrix.indices_0 = bsp_read_array(f, (char*) "indices_0");
}

if (cJSON_HasObjectItem(data_types_, "indices_1")) {
matrix.indices_1 = bsp_read_array(f, "indices_1");
matrix.indices_1 = bsp_read_array(f, (char*) "indices_1");
}

if (cJSON_HasObjectItem(data_types_, "pointers_to_1")) {
matrix.pointers_to_1 = bsp_read_array(f, "pointers_to_1");
matrix.pointers_to_1 = bsp_read_array(f, (char*) "pointers_to_1");
}

if (cJSON_HasObjectItem(binsparse, "structure")) {
Expand Down
Loading