forked from jspark1105/tbb_test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.h
205 lines (178 loc) · 5.38 KB
/
Matrix.h
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#ifndef MATRIX_H
#define MATRIX_H
#include <cassert>
#include <iomanip>
#include <iostream>
//#define USE_HUGE_PAGE
#ifdef USE_HUGE_PAGE
#include <sys/mman.h>
#endif
#include <mkl.h>
#include "Rand.h"
/// Matrix class. Stored in row-major format with
/// leading dimension equal to number of culumns
template <typename T, int PAD = 0> class Matrix {
public:
~Matrix() {
free(data_);
}
/// nrows x ncols matrix (uninitialized)
Matrix(const int nrows, const int ncols, const std::string name = "")
: nrows_(nrows), ncols_(ncols), name_(name) {
assert(nrows_ > 0);
assert(ncols_ > 0);
posix_memalign(
(void **)&data_, 4096, sizeof(T) * (nrows_ + PAD) * (ncols_ + PAD));
assert(data_);
}
/// nrows x ncols matrix initialized with pre-allocated array
Matrix(const int nrows, const int ncols, T *ptr, const std::string name = "")
: nrows_(nrows), ncols_(ncols), name_(name) {
assert(nrows_ > 0);
assert(ncols_ > 0);
data_ = ptr;
}
/// matrix with the given shape with uniform random data if high > low
explicit Matrix(const std::vector<int> shape, const T low = T(0),
const T high = T(0), bool duplicates = true,
const std::string name = "")
: nrows_(shape[0]), ncols_(shape[1]), name_(name) {
assert(shape.size() == 2);
assert(nrows_ > 0);
assert(ncols_ > 0);
#ifdef USE_HUGE_PAGE
if (size() * sizeof(T) >= 2*1024*1024) {
//LOG(INFO) << nrows_ << " " << ncols_ << " " << size() * sizeof(T);
data_ = (T *)mmap64(
NULL, size() * sizeof(T), PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE | MAP_HUGETLB, -1, 0);
if (data_ == MAP_FAILED) {
perror("mmap");
assert(errno != EACCES);
assert(errno != EAGAIN);
assert(errno != EBADF);
assert(errno != EINVAL);
assert(errno != ENFILE);
assert(errno != ENODEV);
assert(errno != ENOMEM);
assert(0);
}
madvise(data_, size() * sizeof(T), MADV_HUGEPAGE);
}
else
#endif
{
posix_memalign(
(void **)&data_, 4096, sizeof(T) * (nrows_ + PAD) * (ncols_ + PAD));
assert(data_);
}
for (int i = 0; i < nrows_; ++i) {
for (int j = 0; j < ncols_; ++j) {
*rawData(i, j) = low;
}
}
// init randomly by sampling from the range [low, high]
if (low < high) {
randFill(low, high, duplicates);
}
}
/// reset to new set of values
void newInput(const T low = T(0), const T high = T(0),
bool duplicates = true) {
if (low < high) {
randFill(low, high, duplicates);
} else {
std::fill(data_, data_ + ((size_t)nrows_ + PAD) * (ncols_ * PAD), 0);
}
}
T &operator()(int row, int col) {
assert(row >= 0 && row < nrows_);
assert(col >= 0 && col < ncols_);
return data_[(size_t)row * (ncols_ + PAD) + col];
}
const T &operator()(int row, int col) const {
assert(row >= 0 && row < nrows_);
assert(col >= 0 && col < ncols_);
return data_[(size_t)row * (ncols_ + PAD) + col];
}
const T *rawData() const { return data_; }
T *rawData() { return data_; }
const T *rawData(int row, int col) const {
return data_ + (size_t)row * (ncols_ + PAD) + col;
}
T *rawData(int row, int col) {
return data_ + (size_t)row * (ncols_ + PAD) + col;
}
const size_t size() const { return (size_t)nrows_ * ncols_; }
void realloc(int nrows, int ncols) {
nrows_ = nrows;
ncols_ = ncols;
free(data_);
posix_memalign(
(void **)&data_, 4096, sizeof(T) * (nrows_ + PAD) * (ncols_ + PAD));
assert(data_);
}
void realloc(const std::vector<int>& shape) {
realloc(shape[0], shape[1]);
}
void assign(aligned_vector<T>& other, int nrows, int ncols) {
nrows_ = nrows;
ncols_ = ncols;
assert(nrows * ncols == other.size());
data_ = other.data();
}
const std::vector<int> shape() const { return {nrows_, ncols_}; }
const std::string name() const { return name_; }
const int nrows() const { return nrows_; }
const int ncols() const { return ncols_; }
int ld() const { return ncols_ + PAD; }
bool operator==(const Matrix &other) const {
bool eq = true;
double rtol = 1e-6;
for (auto i = 0; i < size(); i++) {
if (double(std::abs(rawData()[i] - other.rawData()[i])) >
rtol * double(std::abs(other.rawData()[i]))) {
std::cout << rawData()[i] << " " << other.rawData()[i] << "\n";
eq = false;
break;
}
}
return eq;
}
void randFill(T low, T high, bool duplicates = true) {
for (int i = 0; i < nrows_; ++i) {
::randFill(rawData(i, 0), (size_t)ncols_, low, high, duplicates);
}
}
T *data_;
protected:
int nrows_;
int ncols_;
std::string name_;
};
#define Const(x) (x)
#define From(x) (x)
#define To(x) (x)
inline const std::vector<int> shape(int nrows, int ncols) {
return {nrows, ncols};
}
template <typename T> T sum(Matrix<T> &m) {
T sum = T(0);
for (auto i = 0; i < m.size(); i++) {
sum += m.rawData()[i];
}
return sum;
}
template <typename T>
std::ostream &operator<<(std::ostream &os, const Matrix<T> &m) {
std::cout << "Matrix " << m.name() << ": " << m.nrows() << " x " << m.ncols()
<< "\n";
for (auto i = 0; i < m.nrows(); i++) {
for (auto j = 0; j < m.ncols(); j++) {
os << std::setprecision(9) << m(i, j) << " ";
}
os << "\n";
}
return os;
}
#endif