Tensor.h 3.22 KB
Newer Older
mayong's avatar
mayong committed
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#ifndef TENSOR_H
#define TENSOR_H

#include "alignedmem.h"

using namespace std;

template <typename T> class Tensor {
  private:
    void alloc_buff();
    void free_buff();
    int mem_size;

  public:
    T *buff;
    int size[4];
    int buff_size;
    Tensor(Tensor<T> *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<T> *din, int dim);
    void resize(int a, int b, int c, int d);
    void add(float coe, Tensor<T> *in);
    void add(Tensor<T> *in);
    void add(Tensor<T> *in1, Tensor<T> *in2);
    void reload(Tensor<T> *in);
};

template <typename T> Tensor<T>::Tensor(int a) : size{1, 1, 1, a}
{
    alloc_buff();
}

template <typename T> Tensor<T>::Tensor(int a, int b) : size{1, 1, a, b}
{
    alloc_buff();
}

template <typename T> Tensor<T>::Tensor(int a, int b, int c) : size{1, a, b, c}
{

    alloc_buff();
}

template <typename T>
Tensor<T>::Tensor(int a, int b, int c, int d) : size{a, b, c, d}
{
    alloc_buff();
}

template <typename T> Tensor<T>::Tensor(Tensor<T> *in)
{
    memcpy(size, in->size, 4 * sizeof(int));
    alloc_buff();
    memcpy(buff, in->buff, in->buff_size * sizeof(T));
}

template <typename T> Tensor<T>::~Tensor()
{
    free_buff();
}

template <typename T> void Tensor<T>::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 <typename T> void Tensor<T>::free_buff()
{
    aligned_free(buff);
}

template <typename T> void Tensor<T>::zeros()
{
    memset(buff, 0, buff_size * sizeof(T));
}

template <typename T> void Tensor<T>::shape()
{
    printf("(%d,%d,%d,%d)\n", size[0], size[1], size[2], size[3]);
}

// TODO:: fix it!!!!
template <typename T> void Tensor<T>::concat(Tensor<T> *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 <typename T> void Tensor<T>::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 <typename T> void Tensor<T>::add(float coe, Tensor<T> *in)
{
    int i;
    for (i = 0; i < buff_size; i++) {
        buff[i] = buff[i] + coe * in->buff[i];
    }
}

template <typename T> void Tensor<T>::add(Tensor<T> *in)
{
    int i;
    for (i = 0; i < buff_size; i++) {
        buff[i] = buff[i] + in->buff[i];
    }
}

template <typename T> void Tensor<T>::add(Tensor<T> *in1, Tensor<T> *in2)
{
    int i;
    for (i = 0; i < buff_size; i++) {
        buff[i] = buff[i] + in1->buff[i] + in2->buff[i];
    }
}

template <typename T> void Tensor<T>::reload(Tensor<T> *in)
{
    memcpy(buff, in->buff, in->buff_size * sizeof(T));
}

template <typename T> void Tensor<T>::disp()
{
    int i;
    for (i = 0; i < buff_size; i++) {
        cout << buff[i] << " ";
    }
    cout << endl;
}

template <typename T> void Tensor<T>::dump(const char *mode)
{
    FILE *fp;
    fp = fopen("tmp.bin", mode);
    fwrite(buff, 1, buff_size * sizeof(T), fp);
    fclose(fp);
}
#endif