Skip to content

Commit 0167f9e

Browse files
committed
Implement "declamping" for SuiteSparse Matrices, fix formatting for
C-style casts
1 parent 47b4029 commit 0167f9e

File tree

12 files changed

+67
-26
lines changed

12 files changed

+67
-26
lines changed

.clang-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ PointerAlignment: Left
44
ColumnLimit: 80
55
AlwaysBreakTemplateDeclarations: Yes
66
AllowShortFunctionsOnASingleLine: Empty
7+
SpaceAfterCStyleCast: true
78
---

examples/check_equivalence.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,16 @@ bsp_fdataset_info_t bsp_parse_fdataset_string(char* str) {
9898

9999
if (split == -1) {
100100
bsp_fdataset_info_t info;
101-
info.fname = (char*)malloc(sizeof(char) * (len + 1));
101+
info.fname = (char*) malloc(sizeof(char) * (len + 1));
102102
strcpy(info.fname, str);
103103
info.dataset = NULL;
104104
return info;
105105
} else {
106106
bsp_fdataset_info_t info;
107-
info.fname = (char*)malloc(sizeof(char) * (split + 1));
107+
info.fname = (char*) malloc(sizeof(char) * (split + 1));
108108
strncpy(info.fname, str, split);
109109
info.fname[split] = '\0';
110-
info.dataset = (char*)malloc(sizeof(char) * (len - split));
110+
info.dataset = (char*) malloc(sizeof(char) * (len - split));
111111
strcpy(info.dataset, &str[split + 1]);
112112
return info;
113113
}

examples/mtx2bsp.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ int main(int argc, char** argv) {
99
return 1;
1010
}
1111

12+
// NOTE: this binary automatically performs "declamping" of floating point
13+
// values greater/less than 1e308/-1e308 in order to restore inf values
14+
// in SuiteSparse Matrix Collection matrices.
15+
bool perform_suitesparse_declamping = true;
16+
1217
char* input_fname = argv[1];
1318
char* output_fname = argv[2];
1419

@@ -41,6 +46,10 @@ int main(int argc, char** argv) {
4146
bsp_matrix_t matrix = bsp_mmread(input_fname);
4247
printf(" === Done reading. ===\n");
4348

49+
if (perform_suitesparse_declamping) {
50+
bsp_matrix_declamp_values(matrix);
51+
}
52+
4453
matrix = bsp_matrix_minimize_values(matrix);
4554

4655
bsp_print_matrix_info(matrix);

include/binsparse/array.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void bsp_destroy_array_t(bsp_array_t array) {
108108
data[index] = value; \
109109
} else if (array.type == BSP_BINT8) { \
110110
int8_t* data = array.data; \
111-
data[index] = ((size_t)value) % 2; \
111+
data[index] = ((size_t) value) % 2; \
112112
} else if (array.type == BSP_COMPLEX_FLOAT32) { \
113113
float _Complex* data = array.data; \
114114
data[index] = value; \

include/binsparse/binsparse.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#define BINSPARSE_VERSION "0.1"
44

55
#include <binsparse/array.h>
6+
#include <binsparse/detail/detail.h>
67
#include <binsparse/generate.h>
78
#include <binsparse/hdf5_wrapper.h>
89
#include <binsparse/matrix.h>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#pragma once
2+
3+
#include <binsparse/matrix.h>
4+
#include <math.h>
5+
6+
// "Declamp" a matrix from the SuiteSparse Matrix Collection.
7+
// SuiteSparse Matrix Collection clamps values at 1e308 and -1e308
8+
// for printing. These are almost all infinities or negative infinites.
9+
// Here, we "declamp" these values to restore them to infinity.
10+
// This allows the `bsp_matrix_minimize_values` to properly minimize
11+
// matrices that have infinity values.
12+
void bsp_matrix_declamp_values(bsp_matrix_t matrix) {
13+
const double HUGE_DOUBLE = 1e308;
14+
if (matrix.values.type == BSP_FLOAT64) {
15+
double* values = (double*) matrix.values.data;
16+
17+
for (size_t i = 0; i < matrix.values.size; i++) {
18+
if (values[i] >= HUGE_DOUBLE) {
19+
values[i] = INFINITY;
20+
} else if (values[i] <= -HUGE_DOUBLE) {
21+
values[i] = -INFINITY;
22+
}
23+
}
24+
}
25+
}

include/binsparse/detail/detail.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
#include <binsparse/detail/declamp_values.h>

include/binsparse/matrix_market/coo_sort_tools.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ static int bsp_compare_int_impl_(size_t x, size_t y) {
2121

2222
static int bsp_coo_comparison_row_sort_operator_impl_(const void* x,
2323
const void* y) {
24-
size_t x_index = *((const size_t*)x);
25-
size_t y_index = *((const size_t*)y);
24+
size_t x_index = *((const size_t*) x);
25+
size_t y_index = *((const size_t*) y);
2626

2727
size_t x_i, x_j;
2828
size_t y_i, y_j;

include/binsparse/matrix_market/matrix_market_read.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,11 @@ bsp_matrix_t bsp_mmread(char* file_path) {
289289
size_t max_dim =
290290
(metadata.nrows > metadata.ncols) ? metadata.nrows : metadata.ncols;
291291

292-
if (max_dim < (size_t)UINT8_MAX) {
292+
if (max_dim < (size_t) UINT8_MAX) {
293293
index_type = BSP_UINT8;
294-
} else if (max_dim < (size_t)UINT16_MAX) {
294+
} else if (max_dim < (size_t) UINT16_MAX) {
295295
index_type = BSP_UINT16;
296-
} else if (max_dim < (size_t)UINT32_MAX) {
296+
} else if (max_dim < (size_t) UINT32_MAX) {
297297
index_type = BSP_UINT32;
298298
} else {
299299
index_type = BSP_UINT64;

include/binsparse/matrix_market/matrix_market_write.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ void bsp_mmwrite(char* file_path, bsp_matrix_t matrix) {
6969
bsp_array_read(matrix.indices_0, count, i);
7070
bsp_array_read(matrix.indices_1, count, j);
7171
bsp_array_read(matrix.values, count, value);
72-
fprintf(f, "%zu %zu %lld\n", i + 1, j + 1, (long long)value);
72+
fprintf(f, "%zu %zu %lld\n", i + 1, j + 1, (long long) value);
7373
} else if (mm_type == BSP_MM_REAL) {
7474
size_t i, j;
7575
double value;

0 commit comments

Comments
 (0)