[matrix/utils] adding resize_fill_zeros

This commit is contained in:
Al
2017-03-21 01:37:08 -04:00
parent 2bda741fa9
commit 7fe84e6247
2 changed files with 355 additions and 319 deletions

View File

@@ -19,325 +19,343 @@
#warning "No CLBAS"
#endif
#define MATRIX_INIT(name, type, type_name, array_type) \
typedef struct { \
size_t m, n; \
type *values; \
} name##_t; \
\
static name##_t *name##_new(size_t m, size_t n) { \
name##_t *matrix = malloc(sizeof(name##_t)); \
\
if (matrix == NULL) { \
return NULL; \
} \
\
matrix->m = m; \
matrix->n = n; \
\
matrix->values = malloc(sizeof(type) * m * n); \
if (matrix->values == NULL) { \
free(matrix); \
return NULL; \
} \
\
return matrix; \
\
} \
\
static name##_t *name##_new_aligned(size_t m, size_t n, size_t alignment) { \
name##_t *matrix = malloc(sizeof(name##_t)); \
\
if (matrix == NULL) { \
return NULL; \
} \
\
matrix->m = m; \
matrix->n = n; \
\
matrix->values = _aligned_malloc(sizeof(type) * m * n, alignment); \
if (matrix->values == NULL) { \
free(matrix); \
return NULL; \
} \
\
return matrix; \
\
} \
\
static void name##_destroy(name##_t *self) { \
if (self == NULL) return; \
\
if (self->values != NULL) { \
free(self->values); \
} \
\
free(self); \
} \
\
static void name##_destroy_aligned(name##_t *self) { \
if (self == NULL) return; \
\
if (self->values != NULL) { \
_aligned_free(self->values); \
} \
\
free(self); \
} \
\
static inline void name##_zero(name##_t *self) { \
memset(self->values, 0, self->m * self->n * sizeof(type)); \
} \
\
\
static inline bool name##_resize(name##_t *self, size_t m, size_t n) { \
if (self == NULL) return false; \
\
if (m * n > (self->m * self->n)) { \
type *ptr = realloc(self->values, sizeof(type) * m * n); \
if (ptr == NULL) { \
return false; \
} \
self->values = ptr; \
} \
\
self->m = m; \
self->n = n; \
\
return true; \
} \
\
static inline bool name##_resize_aligned(name##_t *self, size_t m, size_t n, size_t alignment) { \
if (self == NULL) return false; \
\
if (m * n > (self->m * self->n)) { \
type *ptr = _aligned_realloc(self->values, sizeof(type) * m * n, alignment); \
if (ptr == NULL) { \
return false; \
} \
self->values = ptr; \
} \
\
self->m = m; \
self->n = n; \
\
return true; \
} \
\
static inline name##_t *name##_new_copy(name##_t *self) { \
name##_t *cpy = name##_new(self->m, self->n); \
size_t num_values = self->m * self->n; \
memcpy(cpy->values, self->values, num_values); \
\
return cpy; \
} \
\
static inline bool name##_copy(name##_t *self, name##_t *other) { \
if (self->m != other->m || self->n != other->n) { \
return false; \
} \
size_t num_values = self->m * self->n; \
\
memcpy(other->values, self->values, num_values * sizeof(type)); \
return true; \
} \
\
static inline void name##_init_values(name##_t *self, type *values) { \
size_t num_values = self->m * self->n; \
memcpy(self->values, values, num_values * sizeof(type)); \
} \
\
static inline void name##_set(name##_t *self, type value) { \
array_type##_set(self->values, value, self->m * self->n); \
} \
\
static inline void name##_set_row(name##_t *self, size_t index, type *row) { \
size_t offset = index * self->n; \
type *values = self->values; \
size_t n = self->n; \
memcpy(values + offset, row, n * sizeof(type)); \
} \
\
static inline void name##_set_scalar(name##_t *self, size_t row_index, size_t col_index, type value) { \
size_t offset = row_index * self->n + col_index; \
self->values[offset] = value; \
} \
\
static inline void name##_add_scalar(name##_t *self, size_t row_index, size_t col_index, type value) { \
size_t offset = row_index * self->n + col_index; \
self->values[offset] += value; \
} \
\
static inline void name##_sub_scalar(name##_t *self, size_t row_index, size_t col_index, type value) { \
size_t offset = row_index * self->n + col_index; \
self->values[offset] -= value; \
} \
\
static inline void name##_mul_scalar(name##_t *self, size_t row_index, size_t col_index, type value) { \
size_t offset = row_index * self->n + col_index; \
self->values[offset] *= value; \
} \
\
static inline void name##_div_scalar(name##_t *self, size_t row_index, size_t col_index, type value) { \
size_t offset = row_index * self->n + col_index; \
self->values[offset] /= value; \
} \
\
static inline type name##_get(name##_t *self, size_t row_index, size_t col_index) { \
size_t index = row_index * self->n + col_index; \
return self->values[index]; \
} \
\
static inline type *name##_get_row(name##_t *self, size_t row_index) { \
size_t index = row_index * self->n; \
return self->values + index; \
} \
\
static inline name##_t *name##_new_value(size_t m, size_t n, type value) { \
name##_t *matrix = name##_new(m, n); \
name##_set(matrix, value); \
return matrix; \
} \
\
static inline name##_t *name##_new_zeros(size_t m, size_t n) { \
name##_t *matrix = name##_new(m, n); \
name##_zero(matrix); \
return matrix; \
} \
\
static inline name##_t *name##_new_ones(size_t m, size_t n) { \
return name##_new_value(m, n, (type)1); \
} \
\
static inline name##_t *name##_new_values(size_t m, size_t n, type *values) { \
name##_t *matrix = name##_new(m, n); \
memcpy(matrix->values, values, m * n * sizeof(type)); \
return matrix; \
} \
\
static inline void name##_div(name##_t *self, type value) { \
array_type##_div(self->values, value, self->m * self->n); \
} \
\
static inline bool name##_div_matrix(name##_t *self, name##_t *other) { \
if (self->m != other->m || self->n != other->n) return false; \
array_type##_div_array(self->values, other->values, self->m * self->n); \
return true; \
} \
\
static inline bool name##_div_matrix_times_scalar(name##_t *self, name##_t *other, type v) { \
if (self->m != other->m || self->n != other->n) return false; \
array_type##_div_array_times_scalar(self->values, other->values, v, self->m * self->n); \
return true; \
} \
\
static inline void name##_mul(name##_t *self, type value) { \
array_type##_mul(self->values, value, self->m * self->n); \
} \
\
static inline bool name##_mul_matrix(name##_t *self, name##_t *other) { \
if (self->m != other->m || self->n != other->n) return false; \
array_type##_mul_array(self->values, other->values, self->m * self->n); \
return true; \
} \
\
static inline bool name##_mul_matrix_times_scalar(name##_t *self, name##_t *other, type v) { \
if (self->m != other->m || self->n != other->n) return false; \
array_type##_mul_array_times_scalar(self->values, other->values, v, self->m * self->n); \
return true; \
} \
\
static inline void name##_add(name##_t *self, type value) { \
array_type##_add(self->values, self->m * self->n, value); \
} \
\
\
static inline bool name##_add_matrix(name##_t *self, name##_t *other) { \
if (self->m != other->m || self->n != other->n) return false; \
array_type##_add_array(self->values, other->values, self->m * self->n); \
return true; \
} \
\
static inline bool name##_add_matrix_times_scalar(name##_t *self, name##_t *other, type v) { \
if (self->m != other->m || self->n != other->n) return false; \
array_type##_add_array_times_scalar(self->values, other->values, v, self->m * self->n); \
return true; \
} \
\
static inline void name##_sub(name##_t *self, type value) { \
array_type##_sub(self->values, value, self->m * self->n); \
} \
\
static inline bool name##_sub_matrix(name##_t *self, name##_t *other) { \
if (self->m != other->m || self->n != other->n) return false; \
array_type##_sub_array(self->values, other->values, self->m * self->n); \
return true; \
} \
\
static inline bool name##_sub_matrix_times_scalar(name##_t *self, name##_t *other, type v) { \
if (self->m != other->m || self->n != other->n) return false; \
array_type##_sub_array_times_scalar(self->values, other->values, v, self->m * self->n); \
return true; \
} \
\
static name##_t *name##_read(FILE *f) { \
name##_t *mat = malloc(sizeof(name##_t)); \
if (mat == NULL) return NULL; \
\
mat->values = NULL; \
\
uint64_t m = 0; \
uint64_t n = 0; \
\
if (!file_read_uint64(f, &m) || \
!file_read_uint64(f, &n)) { \
goto exit_##name##_allocated; \
} \
\
mat->m = (size_t)m; \
mat->n = (size_t)n; \
\
size_t len_data = mat->m * mat->n; \
\
type *data = malloc(len_data * sizeof(type)); \
if (data == NULL) { \
log_error("error in data malloc\n"); \
goto exit_##name##_allocated; \
} \
\
if (!file_read_##array_type(f, data, len_data)) { \
free(data); \
goto exit_##name##_allocated; \
} \
\
mat->values = data; \
\
return mat; \
\
exit_##name##_allocated: \
name##_destroy(mat); \
return NULL; \
} \
\
static bool name##_write(name##_t *self, FILE *f) { \
if (self == NULL || self->values == NULL) { \
return false; \
} \
\
if (!file_write_uint64(f, (uint64_t)self->m) || \
!file_write_uint64(f, (uint64_t)self->n)) { \
return false; \
} \
\
uint64_t len_data = (uint64_t)self->m * (uint64_t)self->n; \
\
for (uint64_t i = 0; i < len_data; i++) { \
if (!file_write_##type_name(f, self->values[i])) { \
return false; \
} \
} \
\
return true; \
#define MATRIX_INIT(name, type, type_name, array_type) \
typedef struct { \
size_t m, n; \
type *values; \
} name##_t; \
\
static name##_t *name##_new(size_t m, size_t n) { \
name##_t *matrix = malloc(sizeof(name##_t)); \
\
if (matrix == NULL) { \
return NULL; \
} \
\
matrix->m = m; \
matrix->n = n; \
\
matrix->values = malloc(sizeof(type) * m * n); \
if (matrix->values == NULL) { \
free(matrix); \
return NULL; \
} \
\
return matrix; \
\
} \
\
static name##_t *name##_new_aligned(size_t m, size_t n, size_t alignment) { \
name##_t *matrix = malloc(sizeof(name##_t)); \
\
if (matrix == NULL) { \
return NULL; \
} \
\
matrix->m = m; \
matrix->n = n; \
\
matrix->values = _aligned_malloc(sizeof(type) * m * n, alignment); \
if (matrix->values == NULL) { \
free(matrix); \
return NULL; \
} \
\
return matrix; \
\
} \
\
static void name##_destroy(name##_t *self) { \
if (self == NULL) return; \
\
if (self->values != NULL) { \
free(self->values); \
} \
\
free(self); \
} \
\
static void name##_destroy_aligned(name##_t *self) { \
if (self == NULL) return; \
\
if (self->values != NULL) { \
_aligned_free(self->values); \
} \
\
free(self); \
} \
\
static inline void name##_zero(name##_t *self) { \
memset(self->values, 0, self->m * self->n * sizeof(type)); \
} \
\
\
static inline bool name##_resize(name##_t *self, size_t m, size_t n) { \
if (self == NULL) return false; \
\
if (m * n > (self->m * self->n)) { \
type *ptr = realloc(self->values, sizeof(type) * m * n); \
if (ptr == NULL) { \
return false; \
} \
self->values = ptr; \
} \
\
self->m = m; \
self->n = n; \
\
return true; \
} \
\
static inline bool name##_resize_aligned(name##_t *self, size_t m, size_t n, size_t alignment) { \
if (self == NULL) return false; \
\
if (m * n > (self->m * self->n)) { \
type *ptr = _aligned_realloc(self->values, sizeof(type) * m * n, alignment); \
if (ptr == NULL) { \
return false; \
} \
self->values = ptr; \
} \
\
self->m = m; \
self->n = n; \
\
return true; \
} \
\
static inline bool name##_resize_fill_zeros(name##_t *self, size_t m, size_t n) { \
size_t old_m = self->m; \
bool ret = name##_resize(self, m, n); \
if (ret && m > old_m) { \
memset(self->values + old_m, 0, (m - old_m) * self->n * sizeof(type)); \
} \
return ret; \
} \
\
static inline bool name##_resize_aligned_fill_zeros(name##_t *self, size_t m, size_t n, size_t alignment) { \
size_t old_m = self->m; \
bool ret = name##_resize_aligned(self, m, n, alignment); \
if (ret && m > old_m) { \
memset(self->values + old_m, 0, (m - old_m) * self->n * sizeof(type)); \
} \
return ret; \
} \
\
static inline name##_t *name##_new_copy(name##_t *self) { \
name##_t *cpy = name##_new(self->m, self->n); \
size_t num_values = self->m * self->n; \
memcpy(cpy->values, self->values, num_values); \
\
return cpy; \
} \
\
static inline bool name##_copy(name##_t *self, name##_t *other) { \
if (self->m != other->m || self->n != other->n) { \
return false; \
} \
size_t num_values = self->m * self->n; \
\
memcpy(other->values, self->values, num_values * sizeof(type)); \
return true; \
} \
\
static inline void name##_init_values(name##_t *self, type *values) { \
size_t num_values = self->m * self->n; \
memcpy(self->values, values, num_values * sizeof(type)); \
} \
\
static inline void name##_set(name##_t *self, type value) { \
array_type##_set(self->values, value, self->m * self->n); \
} \
\
static inline void name##_set_row(name##_t *self, size_t index, type *row) { \
size_t offset = index * self->n; \
type *values = self->values; \
size_t n = self->n; \
memcpy(values + offset, row, n * sizeof(type)); \
} \
\
static inline void name##_set_scalar(name##_t *self, size_t row_index, size_t col_index, type value) { \
size_t offset = row_index * self->n + col_index; \
self->values[offset] = value; \
} \
\
static inline void name##_add_scalar(name##_t *self, size_t row_index, size_t col_index, type value) { \
size_t offset = row_index * self->n + col_index; \
self->values[offset] += value; \
} \
\
static inline void name##_sub_scalar(name##_t *self, size_t row_index, size_t col_index, type value) { \
size_t offset = row_index * self->n + col_index; \
self->values[offset] -= value; \
} \
\
static inline void name##_mul_scalar(name##_t *self, size_t row_index, size_t col_index, type value) { \
size_t offset = row_index * self->n + col_index; \
self->values[offset] *= value; \
} \
\
static inline void name##_div_scalar(name##_t *self, size_t row_index, size_t col_index, type value) { \
size_t offset = row_index * self->n + col_index; \
self->values[offset] /= value; \
} \
\
static inline type name##_get(name##_t *self, size_t row_index, size_t col_index) { \
size_t index = row_index * self->n + col_index; \
return self->values[index]; \
} \
\
static inline type *name##_get_row(name##_t *self, size_t row_index) { \
size_t index = row_index * self->n; \
return self->values + index; \
} \
\
static inline name##_t *name##_new_value(size_t m, size_t n, type value) { \
name##_t *matrix = name##_new(m, n); \
name##_set(matrix, value); \
return matrix; \
} \
\
static inline name##_t *name##_new_zeros(size_t m, size_t n) { \
name##_t *matrix = name##_new(m, n); \
name##_zero(matrix); \
return matrix; \
} \
\
static inline name##_t *name##_new_ones(size_t m, size_t n) { \
return name##_new_value(m, n, (type)1); \
} \
\
static inline name##_t *name##_new_values(size_t m, size_t n, type *values) { \
name##_t *matrix = name##_new(m, n); \
memcpy(matrix->values, values, m * n * sizeof(type)); \
return matrix; \
} \
\
static inline void name##_div(name##_t *self, type value) { \
array_type##_div(self->values, value, self->m * self->n); \
} \
\
static inline bool name##_div_matrix(name##_t *self, name##_t *other) { \
if (self->m != other->m || self->n != other->n) return false; \
array_type##_div_array(self->values, other->values, self->m * self->n); \
return true; \
} \
\
static inline bool name##_div_matrix_times_scalar(name##_t *self, name##_t *other, type v) { \
if (self->m != other->m || self->n != other->n) return false; \
array_type##_div_array_times_scalar(self->values, other->values, v, self->m * self->n); \
return true; \
} \
\
static inline void name##_mul(name##_t *self, type value) { \
array_type##_mul(self->values, value, self->m * self->n); \
} \
\
static inline bool name##_mul_matrix(name##_t *self, name##_t *other) { \
if (self->m != other->m || self->n != other->n) return false; \
array_type##_mul_array(self->values, other->values, self->m * self->n); \
return true; \
} \
\
static inline bool name##_mul_matrix_times_scalar(name##_t *self, name##_t *other, type v) { \
if (self->m != other->m || self->n != other->n) return false; \
array_type##_mul_array_times_scalar(self->values, other->values, v, self->m * self->n); \
return true; \
} \
\
static inline void name##_add(name##_t *self, type value) { \
array_type##_add(self->values, self->m * self->n, value); \
} \
\
\
static inline bool name##_add_matrix(name##_t *self, name##_t *other) { \
if (self->m != other->m || self->n != other->n) return false; \
array_type##_add_array(self->values, other->values, self->m * self->n); \
return true; \
} \
\
static inline bool name##_add_matrix_times_scalar(name##_t *self, name##_t *other, type v) { \
if (self->m != other->m || self->n != other->n) return false; \
array_type##_add_array_times_scalar(self->values, other->values, v, self->m * self->n); \
return true; \
} \
\
static inline void name##_sub(name##_t *self, type value) { \
array_type##_sub(self->values, value, self->m * self->n); \
} \
\
static inline bool name##_sub_matrix(name##_t *self, name##_t *other) { \
if (self->m != other->m || self->n != other->n) return false; \
array_type##_sub_array(self->values, other->values, self->m * self->n); \
return true; \
} \
\
static inline bool name##_sub_matrix_times_scalar(name##_t *self, name##_t *other, type v) { \
if (self->m != other->m || self->n != other->n) return false; \
array_type##_sub_array_times_scalar(self->values, other->values, v, self->m * self->n); \
return true; \
} \
\
static name##_t *name##_read(FILE *f) { \
name##_t *mat = malloc(sizeof(name##_t)); \
if (mat == NULL) return NULL; \
\
mat->values = NULL; \
\
uint64_t m = 0; \
uint64_t n = 0; \
\
if (!file_read_uint64(f, &m) || \
!file_read_uint64(f, &n)) { \
goto exit_##name##_allocated; \
} \
\
mat->m = (size_t)m; \
mat->n = (size_t)n; \
\
size_t len_data = mat->m * mat->n; \
\
type *data = malloc(len_data * sizeof(type)); \
if (data == NULL) { \
log_error("error in data malloc\n"); \
goto exit_##name##_allocated; \
} \
\
if (!file_read_##array_type(f, data, len_data)) { \
free(data); \
goto exit_##name##_allocated; \
} \
\
mat->values = data; \
\
return mat; \
\
exit_##name##_allocated: \
name##_destroy(mat); \
return NULL; \
} \
\
static bool name##_write(name##_t *self, FILE *f) { \
if (self == NULL || self->values == NULL) { \
return false; \
} \
\
if (!file_write_uint64(f, (uint64_t)self->m) || \
!file_write_uint64(f, (uint64_t)self->n)) { \
return false; \
} \
\
uint64_t len_data = (uint64_t)self->m * (uint64_t)self->n; \
\
for (uint64_t i = 0; i < len_data; i++) { \
if (!file_write_##type_name(f, self->values[i])) { \
return false; \
} \
} \
\
return true; \
}

View File

@@ -65,6 +65,24 @@
return vector; \
} \
\
static inline bool name##_resize_fill_zeros(name *self, size_t n) { \
size_t old_n = self->n; \
bool ret = name##_resize(self, n); \
if (ret && n > old_n) { \
memset(self->a + old_n, 0, (n - old_n) * sizeof(type)); \
} \
return ret; \
} \
\
static inline bool name##_resize_aligned_fill_zeros(name *self, size_t n, size_t alignment) { \
size_t old_n = self->n; \
bool ret = name##_resize_aligned(self, n, alignment); \
if (ret && n > old_n) { \
memset(self->a + old_n, 0, (n - old_n) * sizeof(type)); \
} \
return ret; \
} \
\
static inline type name##_max(type *array, size_t n) { \
if (n < 1) return (type) 0; \
type val = array[0]; \