前言

这个实验主要用来测试大家对现代 C++ 的掌握程度,实验要求如下:

简单翻译一下上述要求,就是我们需要实现定义在 src/include/primer/p0_starter.h 中的三个类 MatrixRowMatrixRowMatrixOperations,其中 MatrixRowMatrix 的父类,RowMatrixOperations 定义了三个用于数组运算的成员函数:AddMultiplyGEMM(就是 \(\boldsymbol{A}*\boldsymbol{B} + \boldsymbol{C}\))。

代码实现

Matrix 类

抽象基类 Matrix 需要我们编写的代码很少,只要完成构造函数和析构函数即可,下面省略了一些不需要我们写的代码:

template <typename T>
class Matrix {
protected:
/**
*
* 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), linear_(new T[rows * cols]) {}
int rows_;
int cols_;
T *linear_; public:
/**
* Destroy a matrix instance.
* TODO(P0): Add implementation
*/
virtual ~Matrix() { delete[] linear_; }
};

linear_ 指向一个由二维矩阵展平而得的一维数组,里面共有 rows * cols 个类型为 T 的元素。由于我们在堆上分配数组的空间使用的是 new T[],所以删除的时候也得用 delete[]

RowMatrix 类

这个类用于表示二维矩阵,需要实现父类 Matrix 中的所有纯虚函数,为了方便访问数据元素,RowMatrix 多定义了一个指针数组 data_,里面的每个元素分别指向了二维矩阵每行首元素的地址:

template <typename T>
class RowMatrix : public Matrix<T> {
public:
/**
* 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) {
data_ = new T *[rows];
for (int i = 0; i < rows; ++i) {
data_[i] = &this->linear_[i * cols];
}
} /**
* @return The number of rows in the matrix
*/
auto GetRowCount() const -> int override { return this->rows_; } /**
* @return The number of columns in the matrix
*/
auto GetColumnCount() const -> int override { return this->cols_; } /**
* 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
*/
auto GetElement(int i, int j) const -> T override {
if (i < 0 || i >= GetRowCount() || j < 0 || j >= GetColumnCount()) {
throw Exception(ExceptionType::OUT_OF_RANGE, "The index out of range");
} return 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, "The index out of range");
} data_[i][j] = val;
} /**
* 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()) != GetRowCount() * GetColumnCount()) {
throw Exception(ExceptionType::OUT_OF_RANGE, "The number of elements of `source` is different from matrix");
} for (int i = 0; i < GetRowCount(); ++i) {
for (int j = 0; j < GetColumnCount(); ++j) {
data_[i][j] = source[i * GetColumnCount() + j];
}
}
} /**
* Destroy a RowMatrix instance.
*/
~RowMatrix() override { delete[] data_; } private:
T **data_;
};

需要注意的是,在 RowMatrix 中访问基类部分的成员(非虚函数)时需要加上 this 指针,不然编译时会报错说找不到指定的成员。

RowMatrixOperations 类

实现该类的三个成员函数之前应该检查数据维度是否匹配,不匹配就返回空指针,否则开个循环遍历二维矩阵完成相关操作即可:

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 auto Add(const RowMatrix<T> *matrixA, const RowMatrix<T> *matrixB) -> std::unique_ptr<RowMatrix<T>> {
if (matrixA->GetRowCount() != matrixB->GetRowCount() || matrixA->GetColumnCount() != matrixB->GetColumnCount()) {
return std::unique_ptr<RowMatrix<T>>(nullptr);
} auto rows = matrixA->GetRowCount();
auto cols = matrixA->GetColumnCount();
auto matrix = std::make_unique<RowMatrix<T>>(rows, cols);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
matrix->SetElement(i, j, matrixA->GetElement(i, j) + matrixB->GetElement(i, j));
}
} return matrix;
} /**
* 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 auto Multiply(const RowMatrix<T> *matrixA, const RowMatrix<T> *matrixB) -> std::unique_ptr<RowMatrix<T>> {
if (matrixA->GetColumnCount() != matrixB->GetRowCount()) {
return std::unique_ptr<RowMatrix<T>>(nullptr);
} auto rows = matrixA->GetRowCount();
auto cols = matrixB->GetColumnCount();
auto matrix = std::make_unique<RowMatrix<T>>(rows, cols);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
T sum = 0;
for (int k = 0; k < matrixA->GetColumnCount(); ++k) {
sum += matrixA->GetElement(i, k) * matrixB->GetElement(k, j);
}
matrix->SetElement(i, j, sum);
}
} return matrix;
} /**
* 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 auto GEMM(const RowMatrix<T> *matrixA, const RowMatrix<T> *matrixB, const RowMatrix<T> *matrixC)
-> std::unique_ptr<RowMatrix<T>> {
if (matrixA->GetColumnCount() != matrixB->GetRowCount()) {
return std::unique_ptr<RowMatrix<T>>(nullptr);
}
if (matrixA->GetRowCount() != matrixC->GetRowCount() || matrixB->GetColumnCount() != matrixC->GetColumnCount()) {
return std::unique_ptr<RowMatrix<T>>(nullptr);
} return Add(Multiply(matrixA, matrixB).get(), matrixC);
}
};

测试

打开 test/primer/starter_test.cpp,将各个测试用例里面的 DISABLED_ 前缀移除,比如 TEST(StarterTest, DISABLED_SampleTest) 改为 TEST(StarterTest, SampleTest),之后运行下述命令:

mkdir build
cd build
cmake ..
make starter_test
./test/starter_test

测试结果如下图所示:

总结

这次实验感觉比较简单,主要考察虚函数、模板和动态内存(包括智能指针)的知识,就是没搞明白为什么函数都用尾置返回类型,而且 Google 风格也让人很不习惯,缩进居然只有两格,函数居然开头大写。以上~~

CMU15445 之 Project#0 - C++ Primer 详解的更多相关文章

  1. QuartusII13.0使用教程详解(一个完整的工程建立)

    好久都没有发布自己的博客了,因为最近学校有比赛,从参加到现在都是一脸懵逼,幸亏有bingo大神的教程,让我慢慢走上了VIP之旅,bingo大神的无私奉献精神值得我们每一个业界人士学习,向bingo致敬 ...

  2. IIS7.0 Appcmd 命令详解和定时重启应用池及站点的设置

    IIS7.0 Appcmd 命令详解 废话不说!虽然有配置界面管理器!但是做安装包的时候命令创建是必不可少的!最近使用NSIS制作安装包仔细研究了一下Appcmd的命令,可谓是功能齐全. 上网查了些资 ...

  3. loadrunner11.0 安装破解详解使用教程

    loadrunner11.0 安装破解详解使用教程 来源:互联网 作者:佚名 时间:01-21 10:25:34 [大 中 小] 很多朋友下载了loadrunner11但不是很会使用,这里简单介绍下安 ...

  4. Apache2.2+Tomcat7.0整合配置详解

    一.简单介绍 Apache.Tomcat Apache HTTP Server(简称 Apache),是 Apache 软件基金协会的一个开放源码的网页服务器,可以在 Windows.Unix.Lin ...

  5. IIS7.0 Appcmd 命令详解

    原文 IIS7.0 Appcmd 命令详解 一:准备工作 APPcmd.exe 位于 C:\Windows\System32\inetsrv 目录 使用 Cd c:\Windows\System32\ ...

  6. Android EventBus 3.0 实例使用详解

    EventBus的使用和原理在网上有很多的博客了,其中泓洋大哥和启舰写的非常非常棒,我也是跟着他们的博客学会的EventBus,因为是第一次接触并使用EventBus,所以我写的更多是如何使用,源码解 ...

  7. RxJava2.0的使用详解

    RxJava2.0的使用详解 1,初识RxJava RxJava就是一种用Java语言实现的响应式编程,来创建基于事件的异步程序 RxJava是一个基于事件订阅的异步执行的一个类库,目前比较火的一些技 ...

  8. 转 OAuth 2.0授权协议详解

    http://www.jb51.net/article/54948.htm 作者:阮一峰 字体:[增加 减小] 类型:转载 时间:2014-09-10我要评论 这篇文章主要介绍了OAuth 2.0授权 ...

  9. 命令创建.net core3.0 web应用详解(超详细教程)

    原文:命令创建.net core3.0 web应用详解(超详细教程) 你是不是曾经膜拜那些敲几行代码就可以创建项目的大神,学习了命令创建项目你也可以成为大神,其实命令创建项目很简单. 1.cmd命令行 ...

随机推荐

  1. MySQL学习day3随笔

    索引在数据量不大的时候体现不出来,数据很多的时候区别明显 1 select * from app_user where `name`='用户9999';-- 0.053 sec 2 select * ...

  2. Dockerfile 命令详解及最佳实践

    Dockerfile 命令详解 FROM 指定基础镜像(必选) 所谓定制镜像,那一定是以一个镜像为基础,在其上进行定制.就像我们之前运行了一个 nginx 镜像的容器,再进行修改一样,基础镜像是必须指 ...

  3. switch 和 if else if else 有什么区别

    1.  一般情况下,它们两个语句可以相互替换 2.  switch..case语句通常处理case为比较确定值的情况,而if...else...语句更加灵活,常用于范围判断(大于.等于某个范围) 3. ...

  4. C#二次开发BIMFACE系列61 File Management文件管理服务接口二次开发及实战详解

    系列目录     [已更新最新开发文章,点击查看详细] 在我的博客<C#二次开发BIMFACE系列61 File Management文件管理服务接口二次开发及实战详解>最后列出了 Fil ...

  5. B+树能存多少数据?

    B+树能存多少数据? 图 MySQL B+树示意图 InnoDB页的大小默认是16KB: 假设一条记录大小为1KB,则一个数据页中可以存16条数据(忽略页中的其他数据结构) 假设主键为int,又指针大 ...

  6. 2.3 为什么建议使用虚拟机来安装Linux?

    笔者认为,通过虚拟机软件学习是初学者学习 Linux 的最佳方式. 在与部分读者的交流中,笔者发现,很多初学者都认为,学习 Linux 就必须将自己的电脑装成 Linux 系统或者必须要有真正的服务器 ...

  7. mmsegmentation中构造自己的数据集和数据加载部分,跑现有demo

    在mmsegmentation中训练自己的数据集 先在mmse/dataset下创建一个python文件,我的名字是my_thermal_dataset.py 在其中填写下面内容 这里要注意,在设置s ...

  8. web渗透学习目录

    一,基础学习 01.基础学习 [[编码总结]] [[JSON三种数据解析方法]] [[js加密,解密]] [[Internet保留地址和非保留地址.内网和公网.VNC和UltraVN]] 代理 [[S ...

  9. HamsterBear Linux Low Res ADC按键驱动的适配 + LVGL button移植

    HamsterBear lradc按键驱动的适配 平台 - F1C200s Linux版本 - 5.17.2 ADC按键 - 4 KEY tablet 驱动程序位于主线内核: drivers/inpu ...

  10. Git 后续——分支与协作

    Git 后续--分支与协作 本文写于 2020 年 9 月 1 日 之前一篇文章写了 Git 的基础用法,但那其实只是「单机模式」,Git 之所以在今天被如此广泛的运用,是脱不开分支系统这一概念的. ...