-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtensor.c
91 lines (80 loc) · 2.55 KB
/
tensor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* Copyright © 2024 Austin Berrio
*
* @file tensor.c
*
* @brief A simple and easy to use Tensor API.
*
* Only use pure C.
* Only use libraries when absolutely necessary.
*
* @note Prefixing related objects, functions, etc. assists with autocomplete.
*/
#include "tensor.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Tensor operations
tensor_t* tensor_create(size_t columns, size_t rows, size_t layers) {
// Allocate memory for the tensor structure
tensor_t* tensor = (tensor_t*) malloc(sizeof(tensor_t));
if (NULL == tensor) {
fprintf(stderr, "Failed to allocate memory for tensor_t.\n");
return NULL;
}
tensor->columns = columns;
tensor->rows = rows;
tensor->layers = layers;
// Allocate memory for the 3D array
tensor->elements = (float***) malloc(layers * sizeof(float**));
if (NULL == tensor->elements) {
fprintf(stderr, "Failed to allocate memory for tensor elements.\n");
free(tensor);
return NULL;
}
for (size_t d = 0; d < layers; ++d) {
tensor->elements[d] = (float**) malloc(rows * sizeof(float*));
if (NULL == tensor->elements[d]) {
fprintf(stderr, "Failed to allocate memory for tensor rows.\n");
for (size_t i = 0; i < d; ++i) {
free(tensor->elements[i]);
}
free(tensor->elements);
free(tensor);
return NULL;
}
for (size_t r = 0; r < rows; ++r) {
tensor->elements[d][r] = (float*) calloc(columns, sizeof(float));
if (NULL == tensor->elements[d][r]) {
fprintf(stderr, "Failed to allocate memory for tensor columns.\n");
for (size_t j = 0; j < r; ++j) {
free(tensor->elements[d][j]);
}
free(tensor->elements[d]);
for (size_t i = 0; i < d; ++i) {
for (size_t j = 0; j < rows; ++j) {
free(tensor->elements[i][j]);
}
free(tensor->elements[i]);
}
free(tensor->elements);
free(tensor);
return NULL;
}
}
}
return tensor;
}
void tensor_free(tensor_t* tensor) {
if (NULL == tensor) {
return;
}
for (size_t d = 0; d < tensor->layers; ++d) {
for (size_t r = 0; r < tensor->rows; ++r) {
free(tensor->elements[d][r]);
}
free(tensor->elements[d]);
}
free(tensor->elements);
free(tensor);
}