Skip to content

Commit

Permalink
Added destructor funcs for Matrix, Row and NeuralNet
Browse files Browse the repository at this point in the history
  • Loading branch information
iWas-Coder committed Nov 9, 2024
1 parent 3e76536 commit 1f6d595
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
3 changes: 3 additions & 0 deletions carbon.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ CARBON_API f32 carbon_math_vec3_dot(CBN_Vec3 u, CBN_Vec3 v);
CARBON_API CBN_Vec3 carbon_math_vec3_cross(CBN_Vec3 u, CBN_Vec3 v);
CARBON_API CBN_Vec2 carbon_math_vec2_rotate(CBN_Vec2 v, f32 angle);
CARBON_API CBN_Matrix carbon_math_mat_create(usz rows, usz cols);
CARBON_API void carbon_math_mat_destroy(CBN_Matrix *m);
CARBON_API void carbon_math_mat_fill(CBN_Matrix m, f32 x);
CARBON_API void carbon_math_mat_rand(CBN_Matrix m, f32 min, f32 max);
CARBON_API CBN_Row carbon_math_mat_row(CBN_Matrix m, usz row);
Expand All @@ -282,6 +283,7 @@ CARBON_API void carbon_math_mat_dot(CBN_Matrix dst, CBN_Matrix a, CBN_Matrix b);
CARBON_API void carbon_math_mat_map(CBN_Matrix m, f32 (*f)(f32));
CARBON_API void carbon_math_mat_print(CBN_Matrix m, const char *name);
CARBON_API CBN_Row carbon_math_row_create(usz cols);
CARBON_API void carbon_math_row_destroy(CBN_Row *r);
CARBON_API CBN_Matrix carbon_math_row_to_mat(CBN_Row r);
CARBON_API CBN_Row carbon_math_row_slice(CBN_Row r, usz i, usz cols);
CARBON_API void carbon_math_row_fill(CBN_Row r, f32 x);
Expand Down Expand Up @@ -524,6 +526,7 @@ typedef struct {
} CBN_NeuralNet;

CARBON_API CBN_NeuralNet carbon_nn_create(usz *arch, usz arch_count);
CARBON_API void carbon_nn_destroy(CBN_NeuralNet *nn);
CARBON_API void carbon_nn_fill(CBN_NeuralNet nn, f32 x);
CARBON_API void carbon_nn_zero(CBN_NeuralNet nn);
CARBON_API void carbon_nn_rand(CBN_NeuralNet nn, f32 min, f32 max);
Expand Down
20 changes: 20 additions & 0 deletions src/carbon_math.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ CBN_Matrix carbon_math_mat_create(usz rows, usz cols) {
return m;
}

void carbon_math_mat_destroy(CBN_Matrix *m) {
if (!m) {
CARBON_WARNING("`m` is not a valid pointer, skipping destruction");
return;
}
CARBON_FREE(m->items);
memset(m, 0, sizeof(CBN_Matrix));
m = 0;
}

void carbon_math_mat_fill(CBN_Matrix m, f32 x) {
for (usz i = 0; i < m.rows; ++i) {
for (usz j = 0; j < m.cols; ++j) {
Expand Down Expand Up @@ -243,6 +253,16 @@ CBN_Row carbon_math_row_create(usz cols) {
return carbon_math_mat_row(carbon_math_mat_create(1, cols), 0);
}

void carbon_math_row_destroy(CBN_Row *r) {
if (!r) {
CARBON_WARNING("`r` is not a valid pointer, skipping destruction");
return;
}
CARBON_FREE(r->items);
memset(r, 0, sizeof(CBN_Row));
r = 0;
}

CBN_Matrix carbon_math_row_to_mat(CBN_Row r) {
return (CBN_Matrix) {
.items = r.items,
Expand Down
28 changes: 23 additions & 5 deletions src/carbon_nn.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,33 @@ CBN_NeuralNet carbon_nn_create(usz *arch, usz arch_count) {
CARBON_ERROR("failed to allocate memory");
memset(&nn, 0, sizeof(CBN_NeuralNet));
}
nn.as[0] = carbon_math_row_create(arch[0]);
for (usz i = 1; i < arch_count; ++i) {
nn.ws[i - 1] = carbon_math_mat_create(nn.as[i - 1].cols, arch[i]);
nn.bs[i - 1] = carbon_math_row_create(arch[i]);
nn.as[i] = carbon_math_row_create(arch[i]);
nn.as[0] = carbon_math_row_create(nn.arch[0]);
for (usz i = 1; i < nn.arch_count; ++i) {
nn.ws[i - 1] = carbon_math_mat_create(nn.as[i - 1].cols, nn.arch[i]);
nn.bs[i - 1] = carbon_math_row_create(nn.arch[i]);
nn.as[i] = carbon_math_row_create(nn.arch[i]);
}
return nn;
}

void carbon_nn_destroy(CBN_NeuralNet *nn) {
if (!nn) {
CARBON_WARNING("`nn` is not a valid pointer, skipping destruction");
return;
}
carbon_math_row_destroy(&nn->as[0]);
for (usz i = 1; i < nn->arch_count; ++i) {
carbon_math_mat_destroy(&nn->ws[i - 1]);
carbon_math_row_destroy(&nn->bs[i - 1]);
carbon_math_row_destroy(&nn->as[i]);
}
CARBON_FREE(nn->ws);
CARBON_FREE(nn->bs);
CARBON_FREE(nn->as);
memset(nn, 0, sizeof(CBN_NeuralNet));
nn = 0;
}

void carbon_nn_fill(CBN_NeuralNet nn, f32 x) {
for (usz i = 0; i < nn.arch_count - 1; ++i) {
carbon_math_mat_fill(nn.ws[i], x);
Expand Down

0 comments on commit 1f6d595

Please sign in to comment.