#ifndef TENSOR_H #define TENSOR_H #include "alignedmem.h" using namespace std; template class Tensor { private: void alloc_buff(); void free_buff(); int mem_size; public: T *buff; int size[4]; int buff_size; Tensor(Tensor *in); Tensor(int a); Tensor(int a, int b); Tensor(int a, int b, int c); Tensor(int a, int b, int c, int d); ~Tensor(); void zeros(); void shape(); void disp(); void dump(const char *mode); void concat(Tensor *din, int dim); void resize(int a, int b, int c, int d); void add(float coe, Tensor *in); void add(Tensor *in); void add(Tensor *in1, Tensor *in2); void reload(Tensor *in); }; template Tensor::Tensor(int a) : size{1, 1, 1, a} { alloc_buff(); } template Tensor::Tensor(int a, int b) : size{1, 1, a, b} { alloc_buff(); } template Tensor::Tensor(int a, int b, int c) : size{1, a, b, c} { alloc_buff(); } template Tensor::Tensor(int a, int b, int c, int d) : size{a, b, c, d} { alloc_buff(); } template Tensor::Tensor(Tensor *in) { memcpy(size, in->size, 4 * sizeof(int)); alloc_buff(); memcpy(buff, in->buff, in->buff_size * sizeof(T)); } template Tensor::~Tensor() { free_buff(); } template void Tensor::alloc_buff() { buff_size = size[0] * size[1] * size[2] * size[3]; mem_size = buff_size; buff = (T *)aligned_malloc(32, buff_size * sizeof(T)); } template void Tensor::free_buff() { aligned_free(buff); } template void Tensor::zeros() { memset(buff, 0, buff_size * sizeof(T)); } template void Tensor::shape() { printf("(%d,%d,%d,%d)\n", size[0], size[1], size[2], size[3]); } // TODO:: fix it!!!! template void Tensor::concat(Tensor *din, int dim) { memcpy(buff + buff_size, din->buff, din->buff_size * sizeof(T)); buff_size += din->buff_size; size[dim] += din->size[dim]; } // TODO:: fix it!!!! template void Tensor::resize(int a, int b, int c, int d) { size[0] = a; size[1] = b; size[2] = c; size[3] = d; buff_size = size[0] * size[1] * size[2] * size[3]; } template void Tensor::add(float coe, Tensor *in) { int i; for (i = 0; i < buff_size; i++) { buff[i] = buff[i] + coe * in->buff[i]; } } template void Tensor::add(Tensor *in) { int i; for (i = 0; i < buff_size; i++) { buff[i] = buff[i] + in->buff[i]; } } template void Tensor::add(Tensor *in1, Tensor *in2) { int i; for (i = 0; i < buff_size; i++) { buff[i] = buff[i] + in1->buff[i] + in2->buff[i]; } } template void Tensor::reload(Tensor *in) { memcpy(buff, in->buff, in->buff_size * sizeof(T)); } template void Tensor::disp() { int i; for (i = 0; i < buff_size; i++) { cout << buff[i] << " "; } cout << endl; } template void Tensor::dump(const char *mode) { FILE *fp; fp = fopen("tmp.bin", mode); fwrite(buff, 1, buff_size * sizeof(T), fp); fclose(fp); } #endif