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