Changed int type to uint_8/uint_32
This commit is contained in:
94
tensor.c
94
tensor.c
@@ -1,5 +1,4 @@
|
|||||||
#include "tensor.h"
|
#include "tensor.h"
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
/* A helper for f.e. the tensor_add_scalar function */
|
/* A helper for f.e. the tensor_add_scalar function */
|
||||||
dtype _tensor_scalar_wise_helper;
|
dtype _tensor_scalar_wise_helper;
|
||||||
@@ -20,17 +19,17 @@ void tensor_destroy(tensor t)
|
|||||||
free(t);
|
free(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
int tensor_is_empty(const tensor t)
|
uint8_t tensor_is_empty(const tensor t)
|
||||||
{
|
{
|
||||||
return t->elements == NULL || t->size == NULL;
|
return t->elements == NULL || t->size == NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int tensor_is_equal(const tensor t1, const tensor t2)
|
uint8_t tensor_is_equal(const tensor t1, const tensor t2)
|
||||||
{
|
{
|
||||||
assert(!tensor_is_empty(t1));
|
assert(!tensor_is_empty(t1));
|
||||||
assert(!tensor_is_empty(t2));
|
assert(!tensor_is_empty(t2));
|
||||||
|
|
||||||
int i;
|
uint32_t i;
|
||||||
if (t1->rank != t2->rank) return 0;
|
if (t1->rank != t2->rank) return 0;
|
||||||
for (i = 0; i < t1->rank; i++) {
|
for (i = 0; i < t1->rank; i++) {
|
||||||
if (t1->size[i] != t2->size[i]) return 0;
|
if (t1->size[i] != t2->size[i]) return 0;
|
||||||
@@ -41,37 +40,38 @@ int tensor_is_equal(const tensor t1, const tensor t2)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int _tensor_check_size(const int *size, int dim)
|
uint8_t _tensor_check_size(const uint32_t *size, uint8_t rank)
|
||||||
{
|
{
|
||||||
int i;
|
uint8_t i;
|
||||||
if(dim < 0) return 0;
|
if(rank < 0) return 0;
|
||||||
for(i = 0; i < dim; i++) {
|
for(i = 0; i < rank; i++) {
|
||||||
if(size[i] < 1) return 0;
|
if(size[i] < 1) return 0;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int _tensor_set_size(tensor t, const int *size, int dim)
|
uint8_t _tensor_set_size(tensor t, const uint32_t *size, uint8_t rank)
|
||||||
{
|
{
|
||||||
/* Sets the size of a Tensor. During this process all data in the tensor t is lost. */
|
/* Sets the size of a Tensor. During this process all data in the tensor t is lost. */
|
||||||
|
|
||||||
int *temp1;
|
uint32_t *temp_size;
|
||||||
int *temp2;
|
uint32_t *temp_index_offset;
|
||||||
dtype *temp3;
|
dtype *temp_elements;
|
||||||
int i, j, num_elem = 1;
|
uint8_t i, j;
|
||||||
|
uint32_t num_elem = 1;
|
||||||
|
|
||||||
if(!_tensor_check_size(size, dim)) return 0;
|
if(!_tensor_check_size(size, rank)) return 0;
|
||||||
|
|
||||||
/* Try allocating memory for the size/ index_offset array of the tensor */
|
/* Try allocating memory for the size/ index_offset array of the tensor */
|
||||||
for(i = 0; i < dim; i++) {
|
for(i = 0; i < rank; i++) {
|
||||||
num_elem *= size[i];
|
num_elem *= size[i];
|
||||||
}
|
}
|
||||||
temp1 = malloc(dim * sizeof(int));
|
temp_size = malloc(rank * sizeof(uint32_t));
|
||||||
temp2 = malloc(dim * sizeof(int));
|
temp_index_offset = malloc(rank * sizeof(uint32_t));
|
||||||
temp3 = malloc(num_elem * sizeof(dtype));
|
temp_elements = malloc(num_elem * sizeof(dtype));
|
||||||
if((temp1 == NULL && dim != 0) || (temp2 == NULL && dim != 0) || temp3 == NULL) {
|
if((temp_size == NULL && rank != 0) || (temp_index_offset == NULL && rank != 0) || temp_elements == NULL) {
|
||||||
free(temp1);
|
free(temp_size);
|
||||||
free(temp2);
|
free(temp_index_offset);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,11 +81,11 @@ int _tensor_set_size(tensor t, const int *size, int dim)
|
|||||||
free(t->elements);
|
free(t->elements);
|
||||||
|
|
||||||
/* Setting the size array */
|
/* Setting the size array */
|
||||||
t->size = temp1;
|
t->size = temp_size;
|
||||||
if(dim != 0) memcpy(t->size, size, dim * sizeof(int));
|
if(rank != 0) memcpy(t->size, size, rank * sizeof(uint32_t));
|
||||||
t->rank = dim;
|
t->rank = rank;
|
||||||
/* Setting the index_offset array */
|
/* Setting the index_offset array */
|
||||||
t->index_offsets = temp2;
|
t->index_offsets = temp_index_offset;
|
||||||
for(i = 0; i < t->rank; i++) {
|
for(i = 0; i < t->rank; i++) {
|
||||||
t->index_offsets[i] = 1;
|
t->index_offsets[i] = 1;
|
||||||
for(j = i + 1; j < t->rank; j++) {
|
for(j = i + 1; j < t->rank; j++) {
|
||||||
@@ -93,17 +93,18 @@ int _tensor_set_size(tensor t, const int *size, int dim)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* Setting the elements pointer and memory usage */
|
/* Setting the elements pointer and memory usage */
|
||||||
t->elements = temp3;
|
t->elements = temp_elements;
|
||||||
t->num_elem = num_elem;
|
t->num_elem = num_elem;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int tensor_set(tensor t, const int *index, dtype val)
|
uint8_t tensor_set(tensor t, const uint32_t *index, dtype val)
|
||||||
{
|
{
|
||||||
assert(!tensor_is_empty(t));
|
assert(!tensor_is_empty(t));
|
||||||
|
|
||||||
int i, offset = 0;
|
uint8_t i;
|
||||||
|
uint32_t offset = 0;
|
||||||
|
|
||||||
if(t->rank == 0) {
|
if(t->rank == 0) {
|
||||||
t->elements[0] = val;
|
t->elements[0] = val;
|
||||||
@@ -119,11 +120,12 @@ int tensor_set(tensor t, const int *index, dtype val)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
dtype tensor_get(const tensor t, const int *index, int *success)
|
dtype tensor_get(const tensor t, const uint32_t *index, uint8_t *success)
|
||||||
{
|
{
|
||||||
assert(!tensor_is_empty(t));
|
assert(!tensor_is_empty(t));
|
||||||
|
|
||||||
int i, offset = 0;
|
uint8_t i;
|
||||||
|
uint32_t offset = 0;
|
||||||
|
|
||||||
if(t->rank == 0) return t->elements[0];
|
if(t->rank == 0) return t->elements[0];
|
||||||
|
|
||||||
@@ -139,9 +141,9 @@ dtype tensor_get(const tensor t, const int *index, int *success)
|
|||||||
return t->elements[offset];
|
return t->elements[offset];
|
||||||
}
|
}
|
||||||
|
|
||||||
int tensor_init_one(tensor t, int rank, const int *size)
|
uint8_t tensor_init_one(tensor t, uint8_t rank, const uint32_t *size)
|
||||||
{
|
{
|
||||||
int i;
|
uint32_t i;
|
||||||
|
|
||||||
if(!_tensor_set_size(t, size, rank)) return 0;
|
if(!_tensor_set_size(t, size, rank)) return 0;
|
||||||
for(i = 0; i < t->num_elem; i++) {
|
for(i = 0; i < t->num_elem; i++) {
|
||||||
@@ -150,9 +152,9 @@ int tensor_init_one(tensor t, int rank, const int *size)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int tensor_init_zero(tensor t, int rank, const int *size)
|
uint8_t tensor_init_zero(tensor t, uint8_t rank, const uint32_t *size)
|
||||||
{
|
{
|
||||||
int i;
|
uint32_t i;
|
||||||
|
|
||||||
if(!_tensor_set_size(t, size, rank)) return 0;
|
if(!_tensor_set_size(t, size, rank)) return 0;
|
||||||
for(i = 0; i < t->num_elem; i++) {
|
for(i = 0; i < t->num_elem; i++) {
|
||||||
@@ -161,10 +163,10 @@ int tensor_init_zero(tensor t, int rank, const int *size)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int tensor_init_rand(tensor t, int rank, const int *size, dtype max)
|
uint8_t tensor_init_rand(tensor t, uint8_t rank, const uint32_t *size, dtype max)
|
||||||
{
|
{
|
||||||
int i;
|
uint32_t i;
|
||||||
static int last_seed;
|
static long last_seed;
|
||||||
last_seed += time(NULL) * 200 + rand();
|
last_seed += time(NULL) * 200 + rand();
|
||||||
srand(last_seed);
|
srand(last_seed);
|
||||||
|
|
||||||
@@ -175,11 +177,11 @@ int tensor_init_rand(tensor t, int rank, const int *size, dtype max)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int tensor_cpy(tensor t1, const tensor t2)
|
uint8_t tensor_cpy(tensor t1, const tensor t2)
|
||||||
{
|
{
|
||||||
assert(!tensor_is_empty(t2));
|
assert(!tensor_is_empty(t2));
|
||||||
|
|
||||||
int i;
|
uint32_t i;
|
||||||
if(!_tensor_set_size(t1, t2->size, t2->rank)) return 0;
|
if(!_tensor_set_size(t1, t2->size, t2->rank)) return 0;
|
||||||
for(i = 0; i < t2->num_elem; i++) {
|
for(i = 0; i < t2->num_elem; i++) {
|
||||||
t1->elements[i] = t2->elements[i];
|
t1->elements[i] = t2->elements[i];
|
||||||
@@ -224,12 +226,12 @@ void tensor_div_scalar(tensor t, dtype n)
|
|||||||
tensor_map(t, &_tensor_dtype_div_helper);
|
tensor_map(t, &_tensor_dtype_div_helper);
|
||||||
}
|
}
|
||||||
|
|
||||||
int tensor_add_inplace(tensor t1, const tensor t2)
|
uint8_t tensor_add_inplace(tensor t1, const tensor t2)
|
||||||
{
|
{
|
||||||
assert(!tensor_is_empty(t1));
|
assert(!tensor_is_empty(t1));
|
||||||
assert(!tensor_is_empty(t2));
|
assert(!tensor_is_empty(t2));
|
||||||
|
|
||||||
int i;
|
uint32_t i;
|
||||||
if(t1->rank != t2->rank) return 0;
|
if(t1->rank != t2->rank) return 0;
|
||||||
for(i = 0; i < t1->rank; i++) {
|
for(i = 0; i < t1->rank; i++) {
|
||||||
if(t1->size[i] != t2->size[i]) return 0;
|
if(t1->size[i] != t2->size[i]) return 0;
|
||||||
@@ -240,12 +242,12 @@ int tensor_add_inplace(tensor t1, const tensor t2)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int tensor_sub_inplace(tensor t1, const tensor t2)
|
uint8_t tensor_sub_inplace(tensor t1, const tensor t2)
|
||||||
{
|
{
|
||||||
assert(!tensor_is_empty(t1));
|
assert(!tensor_is_empty(t1));
|
||||||
assert(!tensor_is_empty(t2));
|
assert(!tensor_is_empty(t2));
|
||||||
|
|
||||||
int i;
|
uint32_t i;
|
||||||
if(t1->rank != t2->rank) return 0;
|
if(t1->rank != t2->rank) return 0;
|
||||||
for(i = 0; i < t1->rank; i++) {
|
for(i = 0; i < t1->rank; i++) {
|
||||||
if(t1->size[i] != t2->size[i]) return 0;
|
if(t1->size[i] != t2->size[i]) return 0;
|
||||||
@@ -285,7 +287,7 @@ void tensor_map(tensor t, dtype (*func)(dtype))
|
|||||||
{
|
{
|
||||||
assert(!tensor_is_empty(t));
|
assert(!tensor_is_empty(t));
|
||||||
|
|
||||||
int i;
|
uint32_t i;
|
||||||
for(i = 0; i < t->num_elem; i++) {
|
for(i = 0; i < t->num_elem; i++) {
|
||||||
t->elements[i] = func(t->elements[i]);
|
t->elements[i] = func(t->elements[i]);
|
||||||
}
|
}
|
||||||
@@ -293,8 +295,8 @@ void tensor_map(tensor t, dtype (*func)(dtype))
|
|||||||
|
|
||||||
void tensor_print(const tensor t)
|
void tensor_print(const tensor t)
|
||||||
{
|
{
|
||||||
int i, j;
|
uint32_t i, j;
|
||||||
int *indx;
|
uint32_t *indx;
|
||||||
|
|
||||||
if(tensor_is_empty(t)){
|
if(tensor_is_empty(t)){
|
||||||
printf("<empty tensor>\n");
|
printf("<empty tensor>\n");
|
||||||
|
|||||||
37
tensor.h
37
tensor.h
@@ -1,12 +1,15 @@
|
|||||||
#ifndef TENSOR_H_INCLUDED
|
#ifndef TENSOR_H_INCLUDED
|
||||||
#define TENSOR_H_INCLUDED
|
#define TENSOR_H_INCLUDED
|
||||||
|
|
||||||
|
#include <_types/_uint32_t.h>
|
||||||
|
#include <_types/_uint8_t.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
/* Defining the datatype of the tensor */
|
/* Defining the datatype of the tensor */
|
||||||
typedef float dtype;
|
typedef float dtype;
|
||||||
@@ -21,43 +24,43 @@ typedef float dtype;
|
|||||||
/* one and zero, */
|
/* one and zero, */
|
||||||
#define DTYPE_ONE 1.0
|
#define DTYPE_ONE 1.0
|
||||||
#define DTYPE_ZERO 0.0
|
#define DTYPE_ZERO 0.0
|
||||||
/* and a random and print function */
|
/* and a random and pruint8_t function */
|
||||||
#define DTYPE_RAND(max) ((float) rand() / RAND_MAX * (max))
|
#define DTYPE_RAND(max) ((float) rand() / RAND_MAX * (max))
|
||||||
#define DTYPE_PRINT(a) (printf(" %4.1f ", (a)))
|
#define DTYPE_PRINT(a) (printf(" %4.1f ", (a)))
|
||||||
|
|
||||||
|
|
||||||
typedef struct _tensor {
|
typedef struct _tensor {
|
||||||
dtype *elements;
|
dtype *elements;
|
||||||
int rank;
|
uint8_t rank;
|
||||||
int *size;
|
uint32_t *size;
|
||||||
int *index_offsets;
|
uint32_t *index_offsets;
|
||||||
int num_elem;
|
uint32_t num_elem;
|
||||||
} *tensor;
|
} *tensor;
|
||||||
|
|
||||||
|
|
||||||
tensor tensor_new(void);
|
tensor tensor_new(void);
|
||||||
void tensor_destroy(tensor t);
|
void tensor_destroy(tensor t);
|
||||||
|
|
||||||
int tensor_is_empty(const tensor t);
|
uint8_t tensor_is_empty(const tensor t);
|
||||||
int tensor_is_equal(const tensor t1, const tensor t2);
|
uint8_t tensor_is_equal(const tensor t1, const tensor t2);
|
||||||
|
|
||||||
int _tensor_check_size(const int *size, int dim);
|
uint8_t _tensor_check_size(const uint32_t *size, uint8_t rank);
|
||||||
int _tensor_set_size(tensor t, const int *size, int dim);
|
uint8_t _tensor_set_size(tensor t, const uint32_t *size, uint8_t rank);
|
||||||
|
|
||||||
int tensor_set(tensor t, const int *index, dtype val);
|
uint8_t tensor_set(tensor t, const uint32_t *index, dtype val);
|
||||||
dtype tensor_get(const tensor t, const int *index, int *success);
|
dtype tensor_get(const tensor t, const uint32_t *index, uint8_t *success);
|
||||||
|
|
||||||
int tensor_init_one(tensor t, int rank, const int *size);
|
uint8_t tensor_init_one(tensor t, uint8_t rank, const uint32_t *size);
|
||||||
int tensor_init_zero(tensor t, int rank, const int *size);
|
uint8_t tensor_init_zero(tensor t, uint8_t rank, const uint32_t *size);
|
||||||
int tensor_init_rand(tensor t, int rank, const int *size, dtype max);
|
uint8_t tensor_init_rand(tensor t, uint8_t rank, const uint32_t *size, dtype max);
|
||||||
int tensor_cpy(tensor t1, const tensor t2);
|
uint8_t tensor_cpy(tensor t1, const tensor t2);
|
||||||
|
|
||||||
void tensor_add_scalar(tensor t, dtype n);
|
void tensor_add_scalar(tensor t, dtype n);
|
||||||
void tensor_sub_scalar(tensor t, dtype n);
|
void tensor_sub_scalar(tensor t, dtype n);
|
||||||
void tensor_mul_scalar(tensor t, dtype n);
|
void tensor_mul_scalar(tensor t, dtype n);
|
||||||
void tensor_div_scalar(tensor t, dtype n);
|
void tensor_div_scalar(tensor t, dtype n);
|
||||||
int tensor_add_inplace(tensor t1, const tensor t2);
|
uint8_t tensor_add_inplace(tensor t1, const tensor t2);
|
||||||
int tensor_sub_inplace(tensor t1, const tensor t2);
|
uint8_t tensor_sub_inplace(tensor t1, const tensor t2);
|
||||||
tensor tensor_add(const tensor t1, const tensor t2);
|
tensor tensor_add(const tensor t1, const tensor t2);
|
||||||
tensor tensor_sub(const tensor t1, const tensor t2);
|
tensor tensor_sub(const tensor t1, const tensor t2);
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "test_tensor.h"
|
#include "test_tensor.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
void test_run_all(void)
|
void test_run_all(void)
|
||||||
{
|
{
|
||||||
@@ -19,7 +20,7 @@ void test_run_all(void)
|
|||||||
void test_tensor_is_equal(void)
|
void test_tensor_is_equal(void)
|
||||||
{
|
{
|
||||||
/* Depends on tensor_cpy, tensor_is_equal, tensor_init_zero, tensor_init_rand */
|
/* Depends on tensor_cpy, tensor_is_equal, tensor_init_zero, tensor_init_rand */
|
||||||
int s[4] = {2, 5, 3, 7};
|
uint32_t s[4] = {2, 5, 3, 7};
|
||||||
tensor t1 = tensor_new();
|
tensor t1 = tensor_new();
|
||||||
tensor t2 = tensor_new();
|
tensor t2 = tensor_new();
|
||||||
|
|
||||||
@@ -38,8 +39,8 @@ void test_tensor_is_equal(void)
|
|||||||
void test_tensor_set(void)
|
void test_tensor_set(void)
|
||||||
{
|
{
|
||||||
/* Depends on tensor_is_equal, tensor_init_zero, tensor_init_rand */
|
/* Depends on tensor_is_equal, tensor_init_zero, tensor_init_rand */
|
||||||
int s[4] = {2, 5, 3, 7};
|
uint32_t s[4] = {2, 5, 3, 7};
|
||||||
int index[4] = {0, 0, 0, 0};
|
uint32_t index[4] = {0, 0, 0, 0};
|
||||||
tensor t1 = tensor_new();
|
tensor t1 = tensor_new();
|
||||||
tensor t2 = tensor_new();
|
tensor t2 = tensor_new();
|
||||||
|
|
||||||
@@ -63,9 +64,9 @@ void test_tensor_set(void)
|
|||||||
void test_tensor_get(void)
|
void test_tensor_get(void)
|
||||||
{
|
{
|
||||||
/* Depends on tensor_is_equal, tensor_set, tensor_init_zero, tensor_init_rand */
|
/* Depends on tensor_is_equal, tensor_set, tensor_init_zero, tensor_init_rand */
|
||||||
int status;
|
uint8_t status;
|
||||||
int s[4] = {2, 5, 3, 7};
|
uint32_t s[4] = {2, 5, 3, 7};
|
||||||
int index[4] = {0, 0, 0, 0};
|
uint32_t index[4] = {0, 0, 0, 0};
|
||||||
tensor t1 = tensor_new();
|
tensor t1 = tensor_new();
|
||||||
tensor t2 = tensor_new();
|
tensor t2 = tensor_new();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user