Skip to content

Commit 8c1e724

Browse files
authored
Merge pull request #7 from GraphBLAS/cpp-fixes
Add fixes to compile with C++
2 parents 2f44f36 + b8aee43 commit 8c1e724

File tree

13 files changed

+291
-159
lines changed

13 files changed

+291
-159
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.20)
1+
cmake_minimum_required(VERSION 3.5)
22
project(binsparse-rc)
33

44
set(CMAKE_C_STANDARD 11)

include/binsparse/array.h

Lines changed: 86 additions & 78 deletions
Large diffs are not rendered by default.
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#pragma once
2+
3+
#ifdef __cplusplus
4+
5+
#include <type_traits>
6+
#include <variant>
7+
8+
namespace binsparse {
9+
10+
namespace __detail {
11+
12+
using array_ptr_variant_t =
13+
std::variant<uint8_t*, uint16_t*, uint32_t*, uint64_t*, int8_t*, int16_t*,
14+
int32_t*, int64_t*, float*, double*, float _Complex*,
15+
double _Complex*>;
16+
17+
array_ptr_variant_t get_typed_ptr(bsp_array_t array) {
18+
if (array.type == BSP_UINT8) {
19+
uint8_t* data = (uint8_t*) array.data;
20+
return data;
21+
} else if (array.type == BSP_UINT16) {
22+
uint16_t* data = (uint16_t*) array.data;
23+
return data;
24+
} else if (array.type == BSP_UINT32) {
25+
uint32_t* data = (uint32_t*) array.data;
26+
return data;
27+
} else if (array.type == BSP_UINT64) {
28+
uint64_t* data = (uint64_t*) array.data;
29+
return data;
30+
} else if (array.type == BSP_INT8) {
31+
int8_t* data = (int8_t*) array.data;
32+
return data;
33+
} else if (array.type == BSP_INT16) {
34+
int16_t* data = (int16_t*) array.data;
35+
return data;
36+
} else if (array.type == BSP_INT32) {
37+
int32_t* data = (int32_t*) array.data;
38+
return data;
39+
} else if (array.type == BSP_INT64) {
40+
int64_t* data = (int64_t*) array.data;
41+
return data;
42+
} else if (array.type == BSP_FLOAT32) {
43+
float* data = (float*) array.data;
44+
return data;
45+
} else if (array.type == BSP_FLOAT64) {
46+
double* data = (double*) array.data;
47+
return data;
48+
} else if (array.type == BSP_BINT8) {
49+
int8_t* data = (int8_t*) array.data;
50+
return data;
51+
} else if (array.type == BSP_COMPLEX_FLOAT32) {
52+
float _Complex* data = (float _Complex*) array.data;
53+
return data;
54+
} else if (array.type == BSP_COMPLEX_FLOAT64) {
55+
double _Complex* data = (double _Complex*) array.data;
56+
return data;
57+
}
58+
return {};
59+
}
60+
61+
} // namespace __detail
62+
63+
} // namespace binsparse
64+
65+
// value = array[index]
66+
template <typename T>
67+
void bsp_array_read(bsp_array_t array, size_t index, T& value) {
68+
auto variant_ptr = binsparse::__detail::get_typed_ptr(array);
69+
70+
std::visit(
71+
[&](auto* ptr) {
72+
using U = std::remove_pointer_t<decltype(ptr)>;
73+
74+
if constexpr (std::is_assignable_v<T, U>) {
75+
value = ptr[index];
76+
}
77+
},
78+
variant_ptr);
79+
}
80+
81+
// array[index] = value
82+
template <typename U>
83+
void bsp_array_write(bsp_array_t array, size_t index, U value) {
84+
auto variant_ptr = binsparse::__detail::get_typed_ptr(array);
85+
86+
std::visit(
87+
[&](auto* ptr) {
88+
using T = std::remove_pointer_t<decltype(ptr)>;
89+
90+
if constexpr (std::is_assignable_v<T, U>) {
91+
ptr[index] = value;
92+
}
93+
},
94+
variant_ptr);
95+
}
96+
97+
// array_0[index_0] = array_1[index_1]
98+
void bsp_array_awrite(bsp_array_t array_0, size_t index_0, bsp_array_t array_1,
99+
size_t index_1) {
100+
auto variant_ptr_0 = binsparse::__detail::get_typed_ptr(array_0);
101+
auto variant_ptr_1 = binsparse::__detail::get_typed_ptr(array_1);
102+
103+
std::visit(
104+
[&](auto* ptr_0, auto* ptr_1) {
105+
using T = std::remove_pointer_t<decltype(ptr_0)>;
106+
using U = std::remove_pointer_t<decltype(ptr_1)>;
107+
108+
if constexpr (std::is_assignable_v<T, U>) {
109+
ptr_0[index_0] = ptr_1[index_1];
110+
}
111+
},
112+
variant_ptr_0, variant_ptr_1);
113+
}
114+
115+
#endif

include/binsparse/generate.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,57 @@
44

55
void bsp_array_fill_random(bsp_array_t array, size_t bound) {
66
if (array.type == BSP_UINT8) {
7-
uint8_t* values = array.data;
7+
uint8_t* values = (uint8_t*) array.data;
88
for (size_t i = 0; i < array.size; i++) {
99
values[i] = lrand48() % bound;
1010
}
1111
} else if (array.type == BSP_UINT16) {
12-
uint16_t* values = array.data;
12+
uint16_t* values = (uint16_t*) array.data;
1313
for (size_t i = 0; i < array.size; i++) {
1414
values[i] = lrand48() % bound;
1515
}
1616
} else if (array.type == BSP_UINT32) {
17-
uint32_t* values = array.data;
17+
uint32_t* values = (uint32_t*) array.data;
1818
for (size_t i = 0; i < array.size; i++) {
1919
values[i] = lrand48() % bound;
2020
}
2121
} else if (array.type == BSP_UINT64) {
22-
uint64_t* values = array.data;
22+
uint64_t* values = (uint64_t*) array.data;
2323
for (size_t i = 0; i < array.size; i++) {
2424
values[i] = lrand48() % bound;
2525
}
2626
} else if (array.type == BSP_INT8) {
27-
int8_t* values = array.data;
27+
int8_t* values = (int8_t*) array.data;
2828
for (size_t i = 0; i < array.size; i++) {
2929
values[i] = lrand48() % bound;
3030
}
3131
} else if (array.type == BSP_INT16) {
32-
int16_t* values = array.data;
32+
int16_t* values = (int16_t*) array.data;
3333
for (size_t i = 0; i < array.size; i++) {
3434
values[i] = lrand48() % bound;
3535
}
3636
} else if (array.type == BSP_INT32) {
37-
int32_t* values = array.data;
37+
int32_t* values = (int32_t*) array.data;
3838
for (size_t i = 0; i < array.size; i++) {
3939
values[i] = lrand48() % bound;
4040
}
4141
} else if (array.type == BSP_INT64) {
42-
int64_t* values = array.data;
42+
int64_t* values = (int64_t*) array.data;
4343
for (size_t i = 0; i < array.size; i++) {
4444
values[i] = lrand48() % bound;
4545
}
4646
} else if (array.type == BSP_FLOAT32) {
47-
float* values = array.data;
47+
float* values = (float*) array.data;
4848
for (size_t i = 0; i < array.size; i++) {
4949
values[i] = drand48() * bound;
5050
}
5151
} else if (array.type == BSP_FLOAT64) {
52-
double* values = array.data;
52+
double* values = (double*) array.data;
5353
for (size_t i = 0; i < array.size; i++) {
5454
values[i] = drand48() * bound;
5555
}
5656
} else if (array.type == BSP_BINT8) {
57-
int8_t* values = array.data;
57+
int8_t* values = (int8_t*) array.data;
5858
for (size_t i = 0; i < array.size; i++) {
5959
values[i] = lrand48() % 2;
6060
}

include/binsparse/hdf5_wrapper.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ int bsp_write_array(hid_t f, char* label, bsp_array_t array,
1313
array = bsp_complex_array_to_fp(array);
1414
}
1515

16+
hsize_t hsize[1];
17+
18+
hsize[0] = array.size;
19+
1620
hid_t hdf5_standard_type = bsp_get_hdf5_standard_type(array.type);
17-
hid_t fspace = H5Screate_simple(1, (hsize_t[]){array.size}, NULL);
21+
hid_t fspace = H5Screate_simple(1, hsize, NULL);
1822
hid_t lcpl = H5Pcreate(H5P_LINK_CREATE);
1923

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

30-
H5Pset_chunk(dcpl, 1, (hsize_t[]){chunk_size});
34+
hsize[0] = chunk_size;
35+
H5Pset_chunk(dcpl, 1, hsize);
3136

3237
if (compression_level > 0) {
3338
H5Pset_deflate(dcpl, compression_level);
@@ -120,7 +125,7 @@ char* bsp_read_attribute(hid_t f, char* label) {
120125

121126
size_t size = H5Tget_size(strtype);
122127

123-
char* string = malloc(size + 1);
128+
char* string = (char*) malloc(size + 1);
124129

125130
H5Aread(attribute, strtype, string);
126131

include/binsparse/matrix_formats.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,31 @@ typedef enum bsp_matrix_format_t {
1414
BSP_DCSC = 18,
1515
BSP_COO = 19,
1616
BSP_COOR = 19,
17-
BSP_COOC = 20
17+
BSP_COOC = 20,
18+
BSP_INVALID_FORMAT = 21
1819
} bsp_matrix_format_t;
1920

2021
char* bsp_get_matrix_format_string(bsp_matrix_format_t format) {
2122
if (format == BSP_DVEC) {
22-
return "DVEC";
23+
return (char*) "DVEC";
2324
} else if (format == BSP_DMAT) {
24-
return "DMAT";
25+
return (char*) "DMAT";
2526
} else if (format == BSP_DMATC) {
26-
return "DMATC";
27+
return (char*) "DMATC";
2728
} else if (format == BSP_CVEC) {
28-
return "CVEC";
29+
return (char*) "CVEC";
2930
} else if (format == BSP_CSR) {
30-
return "CSR";
31+
return (char*) "CSR";
3132
} else if (format == BSP_DCSR) {
32-
return "DCSR";
33+
return (char*) "DCSR";
3334
} else if (format == BSP_DCSC) {
34-
return "DCSC";
35+
return (char*) "DCSC";
3536
} else if (format == BSP_COO) {
36-
return "COO";
37+
return (char*) "COO";
3738
} else if (format == BSP_COOC) {
38-
return "COOC";
39+
return (char*) "COOC";
3940
} else {
40-
return "";
41+
return (char*) "";
4142
}
4243
}
4344

@@ -67,6 +68,6 @@ bsp_matrix_format_t bsp_get_matrix_format(char* format) {
6768
} else if (strcmp("COOC", format) == 0) {
6869
return BSP_COOC;
6970
} else {
70-
return 0;
71+
return BSP_INVALID_FORMAT;
7172
}
7273
}

include/binsparse/matrix_market/matrix_market_inspector.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ bsp_mm_metadata bsp_mmread_metadata(char* file_path) {
4545

4646
size_t comments_capacity = 2048;
4747
size_t comments_size = 0;
48-
char* comments = malloc(sizeof(char) * comments_capacity);
48+
char* comments = (char*) malloc(sizeof(char) * comments_capacity);
4949

5050
while (!outOfComments) {
5151
char* line = fgets(buf, 2048, f);
@@ -60,7 +60,7 @@ bsp_mm_metadata bsp_mmread_metadata(char* file_path) {
6060
while (comments_size + strlen(line) > comments_capacity) {
6161
comments_capacity <<= 1;
6262
}
63-
comments = realloc(comments, sizeof(char) * comments_capacity);
63+
comments = (char*) realloc(comments, sizeof(char) * comments_capacity);
6464
}
6565

6666
memcpy(comments + comments_size, line, strlen(line));

include/binsparse/matrix_market/matrix_market_read.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ bsp_matrix_t bsp_mmread_explicit_coordinate(char* file_path,
209209
count++;
210210
}
211211

212-
size_t* indices = malloc(sizeof(size_t) * matrix.nnz);
212+
size_t* indices = (size_t*) malloc(sizeof(size_t) * matrix.nnz);
213213

214214
for (size_t i = 0; i < matrix.nnz; i++) {
215215
indices[i] = i;

include/binsparse/matrix_market/matrix_market_write.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ void bsp_mmwrite(char* file_path, bsp_matrix_t matrix) {
1313

1414
char* structure = NULL;
1515
if (matrix.structure == BSP_GENERAL) {
16-
structure = "general";
16+
structure = (char*) "general";
1717
} else if (matrix.structure == BSP_SYMMETRIC) {
18-
structure = "symmetric";
18+
structure = (char*) "symmetric";
1919
} else if (matrix.structure == BSP_HERMITIAN) {
20-
structure = "hermitian";
20+
structure = (char*) "hermitian";
2121
} else if (matrix.structure == BSP_SKEW_SYMMETRIC) {
22-
structure = "skew-symmetric";
22+
structure = (char*) "skew-symmetric";
2323
} else {
2424
assert(false);
2525
}
@@ -33,20 +33,20 @@ void bsp_mmwrite(char* file_path, bsp_matrix_t matrix) {
3333

3434
if (matrix.is_iso == true) {
3535
mm_type = BSP_MM_PATTERN;
36-
type = "pattern";
36+
type = (char*) "pattern";
3737
} else if ((matrix.values.type >= BSP_UINT8 &&
3838
matrix.values.type <= BSP_INT64) ||
3939
matrix.values.type == BSP_BINT8) {
4040
mm_type = BSP_MM_INTEGER;
41-
type = "integer";
41+
type = (char*) "integer";
4242
} else if (matrix.values.type >= BSP_FLOAT32 &&
4343
matrix.values.type <= BSP_FLOAT64) {
4444
mm_type = BSP_MM_REAL;
45-
type = "real";
45+
type = (char*) "real";
4646
} else if (matrix.values.type == BSP_COMPLEX_FLOAT32 ||
4747
matrix.values.type == BSP_COMPLEX_FLOAT64) {
4848
mm_type = BSP_MM_COMPLEX;
49-
type = "complex";
49+
type = (char*) "complex";
5050
} else {
5151
assert(false);
5252
}
@@ -85,8 +85,8 @@ void bsp_mmwrite(char* file_path, bsp_matrix_t matrix) {
8585
bsp_array_read(matrix.indices_0, count, i);
8686
bsp_array_read(matrix.indices_1, count, j);
8787
bsp_array_read(matrix.values, count, value);
88-
double real_value = 1.0 * value;
89-
double complex_value = 1j * value;
88+
double real_value = __real__ value;
89+
double complex_value = __imag__ value;
9090
fprintf(f, "%zu %zu %.17lg %.17lg\n", i + 1, j + 1, real_value,
9191
complex_value);
9292
} else {

include/binsparse/read_matrix.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
bsp_matrix_t bsp_read_matrix_from_group(hid_t f) {
88
bsp_matrix_t matrix = bsp_construct_default_matrix_t();
99

10-
char* json_string = bsp_read_attribute(f, "binsparse");
10+
char* json_string = bsp_read_attribute(f, (char*) "binsparse");
1111

1212
cJSON* j = cJSON_Parse(json_string);
1313

@@ -65,7 +65,7 @@ bsp_matrix_t bsp_read_matrix_from_group(hid_t f) {
6565
assert(data_types_ != NULL);
6666

6767
if (cJSON_HasObjectItem(data_types_, "values")) {
68-
matrix.values = bsp_read_array(f, "values");
68+
matrix.values = bsp_read_array(f, (char*) "values");
6969

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

8383
if (cJSON_HasObjectItem(data_types_, "indices_0")) {
84-
matrix.indices_0 = bsp_read_array(f, "indices_0");
84+
matrix.indices_0 = bsp_read_array(f, (char*) "indices_0");
8585
}
8686

8787
if (cJSON_HasObjectItem(data_types_, "indices_1")) {
88-
matrix.indices_1 = bsp_read_array(f, "indices_1");
88+
matrix.indices_1 = bsp_read_array(f, (char*) "indices_1");
8989
}
9090

9191
if (cJSON_HasObjectItem(data_types_, "pointers_to_1")) {
92-
matrix.pointers_to_1 = bsp_read_array(f, "pointers_to_1");
92+
matrix.pointers_to_1 = bsp_read_array(f, (char*) "pointers_to_1");
9393
}
9494

9595
if (cJSON_HasObjectItem(binsparse, "structure")) {

0 commit comments

Comments
 (0)