diff --git a/high_performance_operators/gemm/version1.cpp b/high_performance_operators/gemm/version1.cpp index e650d3d..2b9a068 100644 --- a/high_performance_operators/gemm/version1.cpp +++ b/high_performance_operators/gemm/version1.cpp @@ -9,33 +9,58 @@ template struct matrix_2d { - matrix_2d(std::vector> &data) { - this->data = data; - h = data.size(); - w = data[0].size(); + +public: + matrix_2d(std::vector> data) { + h = data.size(); + w = data[0].size(); + stride[0] = w; + stride[1] = 1; + + this->data = new type[h * w]; + for (int i = 0; i < h; ++i) { + for (int j = 0; j < w; ++j) { + (this->data)[i * stride[0] + j * stride[1]] = data[i][j]; + } + } } matrix_2d(int high, int width, type fill = 0) { - data.resize(high, std::vector(width, fill)); - h = high; - w = width; + h = high; + w = width; + stride[0] = w; + stride[1] = 1; + + this->data = new type[h * w]; + std::fill(this->data, this->data + h * w, fill); + } + + ~matrix_2d() { + delete data; } - std::vector & operator[](int i) { - return data[i]; + type * data_ptr() { + return data; + } + + type * operator[](int i) { + return (this->data) + i * stride[0]; } friend std::ostream & operator<<(std::ostream & o, matrix_2d & m) { - for (int i = 0; i < m.h; ++i) { - for (int j = 0; j < m.w; ++j) o << m[i][j] << " "; - o << std::endl; - } - return o; + for (int i = 0; i < m.h; ++i) { + for (int j = 0; j < m.w; ++j) o << m[i][j] << " "; + o << std::endl; + } + return o; } int h; int w; - std::vector> data; + +private: + int stride[2]; + type * data; }; template