Skip to content

Commit e9fcce3

Browse files
committed
Some debugging/cleanup
1 parent 4b4d3f0 commit e9fcce3

6 files changed

Lines changed: 119 additions & 82 deletions

File tree

bindings/matlab/binsparse_from_ssmc.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,8 @@ void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {
333333

334334
bsp_matrix_t result = csc_matrix;
335335
if (target_format != BSP_CSC) {
336-
result = bsp_convert_matrix(csc_matrix, target_format);
336+
result = bsp_convert_matrix_allocator(csc_matrix, target_format,
337+
bsp_matlab_allocator);
337338
bsp_destroy_matrix_t(&csc_matrix);
338339

339340
if (result.format != target_format) {

bindings/matlab/binsparse_minimize_types.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ static void minimize_iso_values(bsp_matrix_t* matrix) {
4949
uint64_t current = 0;
5050
bsp_array_read((*values), i, current);
5151
if (current != first) {
52-
mexPrintf("%lu != %lu\n", current, first);
5352
all_equal = false;
5453
break;
5554
}
@@ -93,7 +92,6 @@ static void minimize_iso_values(bsp_matrix_t* matrix) {
9392
int64_t current = 0;
9493
bsp_array_read((*values), i, current);
9594
if (current != first) {
96-
mexPrintf("%lu != %lu\n", current, first);
9795
all_equal = false;
9896
break;
9997
}
@@ -133,7 +131,6 @@ static void minimize_iso_values(bsp_matrix_t* matrix) {
133131
float current = 0.0f;
134132
bsp_array_read((*values), i, current);
135133
if (current != first) {
136-
mexPrintf("%lu != %lu\n", current, first);
137134
all_equal = false;
138135
break;
139136
}
@@ -162,7 +159,6 @@ static void minimize_iso_values(bsp_matrix_t* matrix) {
162159
double current = 0.0;
163160
bsp_array_read((*values), i, current);
164161
if (current != first) {
165-
mexPrintf("%lu != %lu\n", current, first);
166162
all_equal = false;
167163
break;
168164
}
@@ -227,8 +223,6 @@ static void minimize_iso_values(bsp_matrix_t* matrix) {
227223
return;
228224
}
229225

230-
mexPrintf("All equal!\n");
231-
232226
bsp_array_t new_values;
233227
bsp_error_t error = bsp_construct_array_t_allocator(
234228
&new_values, 1, values->type, bsp_matlab_allocator);
@@ -495,7 +489,6 @@ static void minimize_index_array(bsp_array_t* array, const char* name) {
495489
}
496490

497491
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {
498-
mexPrintf("Running binsparse_minimize_types\n");
499492
if (nrhs != 1) {
500493
mexErrMsgIdAndTxt("BinSparse:InvalidArgs",
501494
"Usage: matrix = binsparse_minimize_types(matrix)");
@@ -523,7 +516,6 @@ void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {
523516
minimize_index_array(&matrix.indices_0, "indices_0");
524517
minimize_index_array(&matrix.indices_1, "indices_1");
525518
minimize_index_array(&matrix.pointers_to_1, "pointers_to_1");
526-
mexPrintf("Minimizing ISO values:\n");
527519
minimize_iso_values(&matrix);
528520

529521
plhs[0] = bsp_matrix_to_matlab_struct(&matrix);

bindings/matlab/binsparse_read.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,8 @@
1919
#include <binsparse/binsparse.h>
2020
#include <string.h>
2121

22-
static inline void* bsp_matlab_malloc(size_t size) {
23-
void* ptr = mxMalloc(size);
24-
mexMakeMemoryPersistent(ptr);
25-
return ptr;
26-
}
27-
28-
static const bsp_allocator_t bsp_matlab_allocator = {
29-
.malloc = bsp_matlab_malloc, .free = mxFree};
22+
static const bsp_allocator_t bsp_matlab_allocator = {.malloc = mxMalloc,
23+
.free = mxFree};
3024

3125
static inline mxClassID get_mxClassID(bsp_type_t type) {
3226
switch (type) {
@@ -71,15 +65,19 @@ static inline mxComplexity get_mxComplexity(bsp_type_t type) {
7165

7266
mxArray* bsp_array_to_matlab(bsp_array_t* array) {
7367
if (array->data == NULL || array->size == 0) {
74-
// Return empty array
75-
return mxCreateDoubleMatrix(1, 1, mxREAL);
68+
// Return empty array of the correct class
69+
mxClassID class_id = get_mxClassID(array->type);
70+
if (class_id == mxUNKNOWN_CLASS) {
71+
class_id = mxDOUBLE_CLASS;
72+
}
73+
return mxCreateNumericMatrix(0, 1, class_id, get_mxComplexity(array->type));
7674
}
7775

7876
if (get_mxClassID(array->type) == mxUNKNOWN_CLASS) {
7977
mexWarnMsgIdAndTxt("BinSparse:UnsupportedType",
8078
"Unsupported array type %d, returning empty array",
8179
(int) array->type);
82-
return mxCreateDoubleMatrix(1, 1, mxREAL);
80+
return mxCreateNumericMatrix(0, 1, mxDOUBLE_CLASS, mxREAL);
8381
}
8482

8583
mxArray* mx_array = NULL;

bindings/matlab/matlab_bsp_helpers.h

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ static inline int extract_matlab_csc(const mxArray* mx_matrix,
6464
static inline bsp_error_t
6565
matlab_to_bsp_array_allocator(const mxArray* mx_array, bsp_array_t* array,
6666
bsp_allocator_t allocator) {
67-
if (mxIsEmpty(mx_array)) {
68-
return bsp_construct_default_array_t_allocator(array, allocator);
69-
}
67+
bool is_empty = mxIsEmpty(mx_array);
7068

7169
size_t size = mxGetNumberOfElements(mx_array);
7270
mxClassID class_id = mxGetClassID(mx_array);
@@ -132,6 +130,14 @@ matlab_to_bsp_array_allocator(const mxArray* mx_array, bsp_array_t* array,
132130
}
133131
}
134132

133+
if (is_empty) {
134+
array->data = NULL;
135+
array->size = 0;
136+
array->type = bsp_type;
137+
array->allocator = allocator;
138+
return BSP_SUCCESS;
139+
}
140+
135141
bsp_error_t error =
136142
bsp_construct_array_t_allocator(array, size, bsp_type, allocator);
137143
if (error != BSP_SUCCESS) {
@@ -372,14 +378,24 @@ static inline mxComplexity get_mxComplexity(bsp_type_t type) {
372378

373379
static inline mxArray* bsp_array_to_matlab(bsp_array_t* array) {
374380
if (!array || array->data == NULL || array->size == 0) {
375-
return mxCreateDoubleMatrix(0, 0, mxREAL);
381+
bsp_type_t type = array ? array->type : BSP_FLOAT64;
382+
mxClassID class_id = get_mxClassID(type);
383+
if (class_id == mxUNKNOWN_CLASS) {
384+
class_id = mxDOUBLE_CLASS;
385+
type = BSP_FLOAT64;
386+
}
387+
mxArray* empty_array =
388+
mxCreateNumericMatrix(0, 1, class_id, get_mxComplexity(type));
389+
return empty_array;
376390
}
377391

378392
if (get_mxClassID(array->type) == mxUNKNOWN_CLASS) {
379393
mexWarnMsgIdAndTxt("BinSparse:UnsupportedType",
380394
"Unsupported array type %d, returning empty array",
381395
(int) array->type);
382-
return mxCreateDoubleMatrix(0, 0, mxREAL);
396+
mxArray* empty_array = mxCreateNumericMatrix(
397+
0, 1, get_mxClassID(array->type), get_mxComplexity(array->type));
398+
return empty_array;
383399
}
384400

385401
mxArray* mx_array = NULL;

include/binsparse/array.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ bsp_construct_default_array_t_allocator(bsp_array_t* array,
2525
bsp_allocator_t allocator) {
2626
array->data = NULL;
2727
array->size = 0;
28+
array->type = BSP_INVALID_TYPE;
2829
array->allocator = allocator;
2930
return BSP_SUCCESS;
3031
}

0 commit comments

Comments
 (0)