0%

lab0-cpp primer

开始 CMU 15-445 的学习,首先是 lab0。

lab0 主要是 搭建环境 和 熟悉 C++。

project0 C++ primer

P0 的主要任务是,实现 Matrix、RowMatrix 和 RowMatrixOperations 三个 class。

Matrix 是虚基类,RowMatrix 是 Matrix 的一个实现。RowMatrixOperations 是矩阵加法、乘法等的封装。

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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//===----------------------------------------------------------------------===//
//
// BusTub
//
// p0_starter.h
//
// Identification: src/include/primer/p0_starter.h
//
// Copyright (c) 2015-2020, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//

#pragma once

#include <memory>
#include <stdexcept>
#include <vector>

#include "common/exception.h"

namespace bustub {

/**
* The Matrix type defines a common
* interface for matrix operations.
*/
template <typename T>
class Matrix {
protected:
/**
* TODO(P0): Add implementation
*
* Construct a new Matrix instance.
* @param rows The number of rows
* @param cols The number of columns
*
*/
Matrix(int rows, int cols) : rows_(rows), cols_(cols) { this->linear_ = new T[rows * cols]; }

/** The number of rows in the matrix */
int rows_;
/** The number of columns in the matrix */
int cols_;

/**
* TODO(P0): Allocate the array in the constructor.
* TODO(P0): Deallocate the array in the destructor.
* A flattened array containing the elements of the matrix.
*/
T *linear_;

public:
/** @return The number of rows in the matrix */
virtual int GetRowCount() const = 0;

/** @return The number of columns in the matrix */
virtual int GetColumnCount() const = 0;

/**
* Get the (i,j)th matrix element.
*
* Throw OUT_OF_RANGE if either index is out of range.
*
* @param i The row index
* @param j The column index
* @return The (i,j)th matrix element
* @throws OUT_OF_RANGE if either index is out of range
*/
virtual T GetElement(int i, int j) const = 0;

/**
* Set the (i,j)th matrix element.
*
* Throw OUT_OF_RANGE if either index is out of range.
*
* @param i The row index
* @param j The column index
* @param val The value to insert
* @throws OUT_OF_RANGE if either index is out of range
*/
virtual void SetElement(int i, int j, T val) = 0;

/**
* Fill the elements of the matrix from `source`.
*
* Throw OUT_OF_RANGE in the event that `source`
* does not contain the required number of elements.
*
* @param source The source container
* @throws OUT_OF_RANGE if `source` is incorrect size
*/
virtual void FillFrom(const std::vector<T> &source) = 0;

/**
* Destroy a matrix instance.
* TODO(P0): Add implementation
*/
virtual ~Matrix() { delete []this->linear_; }
};

/**
* The RowMatrix type is a concrete matrix implementation.
* It implements the interface defined by the Matrix type.
*/
template <typename T>
class RowMatrix : public Matrix<T> {
public:
/**
* TODO(P0): Add implementation
*
* Construct a new RowMatrix instance.
* @param rows The number of rows
* @param cols The number of columns
*/
RowMatrix(int rows, int cols) : Matrix<T>(rows, cols) {
this->data_ = new T *[rows];
for (int i = 0; i < rows; i++) {
this->data_[i] = Matrix<T>::linear_ + i * cols;
}
}

/**
* TODO(P0): Add implementation
* @return The number of rows in the matrix
*/
int GetRowCount() const override { return Matrix<T>::rows_; }

/**
* TODO(P0): Add implementation
* @return The number of columns in the matrix
*/
int GetColumnCount() const override { return Matrix<T>::cols_; }

/**
* TODO(P0): Add implementation
*
* Get the (i,j)th matrix element.
*
* Throw OUT_OF_RANGE if either index is out of range.
*
* @param i The row index
* @param j The column index
* @return The (i,j)th matrix element
* @throws OUT_OF_RANGE if either index is out of range
*/
T GetElement(int i, int j) const override {
if (i < 0 || i >= GetRowCount() || j < 0 || j >= GetColumnCount()) {
throw Exception(ExceptionType::OUT_OF_RANGE, "RowMatrix::GetElement() out of range.");
}

return this->data_[i][j];
}

/**
* Set the (i,j)th matrix element.
*
* Throw OUT_OF_RANGE if either index is out of range.
*
* @param i The row index
* @param j The column index
* @param val The value to insert
* @throws OUT_OF_RANGE if either index is out of range
*/
void SetElement(int i, int j, T val) override {
if (i < 0 || i >= GetRowCount() || j < 0 || j >= GetColumnCount()) {
throw Exception(ExceptionType::OUT_OF_RANGE, "RowMatrix::SetElement() out of range.");
}

this->data_[i][j] = val;
}

/**
* TODO(P0): Add implementation
*
* Fill the elements of the matrix from `source`.
*
* Throw OUT_OF_RANGE in the event that `source`
* does not contain the required number of elements.
*
* @param source The source container
* @throws OUT_OF_RANGE if `source` is incorrect size
*/
void FillFrom(const std::vector<T> &source) override {
if (static_cast<int>(source.size()) != Matrix<T>::rows_ * Matrix<T>::cols_) {
throw Exception(ExceptionType::OUT_OF_RANGE, "RowMatrix::FillForm() has incorrect size");
}
int idx = 0;
for (int i = 0; i < Matrix<T>::rows_; i++) {
for (int j = 0; j < Matrix<T>::cols_; j++) {
this->data_[i][j] = source[idx++];
}
}
}

/**
* TODO(P0): Add implementation
*
* Destroy a RowMatrix instance.
*/
~RowMatrix() override { delete []this->data_; }

private:
/**
* A 2D array containing the elements of the matrix in row-major format.
*
* TODO(P0):
* - Allocate the array of row pointers in the constructor.
* - Use these pointers to point to corresponding elements of the `linear` array.
* - Don't forget to deallocate the array in the destructor.
*/
T **data_;
};

/**
* The RowMatrixOperations class defines operations
* that may be performed on instances of `RowMatrix`.
*/
template <typename T>
class RowMatrixOperations {
public:
/**
* Compute (`matrixA` + `matrixB`) and return the result.
* Return `nullptr` if dimensions mismatch for input matrices.
* @param matrixA Input matrix
* @param matrixB Input matrix
* @return The result of matrix addition
*/
static std::unique_ptr<RowMatrix<T>> Add(const RowMatrix<T> *matrixA, const RowMatrix<T> *matrixB) {
// TODO(P0): Add implementation
if (matrixA->GetColumnCount() != matrixB->GetColumnCount() || matrixA->GetRowCount() != matrixB->GetRowCount()) {
return std::unique_ptr<RowMatrix<T>>(nullptr);
}
int row = matrixA->GetRowCount();
int col = matrixA->GetColumnCount();

std::unique_ptr<RowMatrix<T>> matrix_c(new RowMatrix<T>(row, col));
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
matrix_c->SetElement(i, j, matrixA->GetElement(i, j) + matrixB->GetElement(i, j));
}
}

return matrix_c;
}

/**
* Compute the matrix multiplication (`matrixA` * `matrixB` and return the result.
* Return `nullptr` if dimensions mismatch for input matrices.
* @param matrixA Input matrix
* @param matrixB Input matrix
* @return The result of matrix multiplication
*/
static std::unique_ptr<RowMatrix<T>> Multiply(const RowMatrix<T> *matrixA, const RowMatrix<T> *matrixB) {
// TODO(P0): Add implementation
if (matrixA->GetColumnCount() != matrixB->GetRowCount()) {
return std::unique_ptr<RowMatrix<T>>(nullptr);
}
int row = matrixA->GetRowCount();
int col = matrixB->GetColumnCount();
std::unique_ptr<RowMatrix<T>> matrix_c(new RowMatrix<T>(row, col));

for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
T elem = 0;
for (int ptr = 0; ptr < matrixA->GetColumnCount(); ptr++) {
elem += matrixA->GetElement(i, ptr) * matrixB->GetElement(ptr, j);
}
matrix_c->SetElement(i, j, elem);
}
}
return matrix_c;
}

/**
* Simplified General Matrix Multiply operation. Compute (`matrixA` * `matrixB` + `matrixC`).
* Return `nullptr` if dimensions mismatch for input matrices.
* @param matrixA Input matrix
* @param matrixB Input matrix
* @param matrixC Input matrix
* @return The result of general matrix multiply
*/
static std::unique_ptr<RowMatrix<T>> GEMM(const RowMatrix<T> *matrixA, const RowMatrix<T> *matrixB,
const RowMatrix<T> *matrixC) {
auto matrix_temp = Multiply(matrixA, matrixB);
if (matrix_temp == nullptr) {
return matrix_temp;
}
return Add(matrix_temp.get(), matrixC);
}
};
} // namespace bustub

在提交之前,可以先用本地的 gtest 测试一下。

提交前,需要用代码格式化工具和代码静态检查工具检查一下代码风格。

最后将代码提交到 gradescope,需要注册,注册码在官网 QA 页面。

实验结果:

dbp0_0