[CMU 15-445/645] Project#0 C++ Primer

[CMU 15-445/645] Project#0 C++ Primer

[CMU 15-445/645] Project#0 C++ Primer

Project地址:C++ PRIMER

概览

All the programming projects this semester will be written on the BusTub database management system. This system is written in C++. To ensure that you have the necessary C++ background, we are requiring everyone to complete a simple programming assignment to assess your knowledge of basic C++ features. You will not be given a grade for this project, but you are required to complete the project with a perfect score before being allowed to proceed in the course. Any student that is unable to complete this assignment before the deadline will be asked to drop the course.

All of the code in this programming assignment must be written in C++ (specifically C++ 17). It is generally sufficient to know C++ 11. If you have not used C++ before, here is a short tutorial on the language. More detailed documentation of language internals is available on cppreference. A Tour of C++ and Effective Modern C++ are also digitally available from the CMU library.

这里是说这些项目是基于一个叫做BusTub的数据库管理系统,使用C++ 编写,Project#0是用来测试你的C++ 水平,通过了才能继续完成之后的project。使用的C++ 17,C++ 11也行。需要注意的是该实验是在linux下进行的,windows下可以利用wsl或者虚拟机来进行实验,之后有时间把配置过程补上。

There are many tutorials available to teach you how to use gdb effectively. Here are some that we have found useful:

这些是gdb调试的资料

项目说明

In this project, you will implement three classes: Matrix, RowMatrix, and RowMatrixOperations. These matrices are simple two-dimensional matrices that must support addition, matrix multiplication, and a simplified General Matrix Multiply (GEMM) operations.

You will only need to modify a single file: p0_starter.h You can find the file in the BusTub repository at src/include/primer/p0_starter.h.

In this header file, we have defined the three classes that you will need to implement. The Matrix abstract class defines the common functions for the derived class RowMatrix. The RowMatrixOperations class will use RowMatrix objects to achieve the operations mentioned in overview. The function prototypes and member variables are specified in the file. The project requires you to fill in the implementations of all the constructors, deconstructors, and member functions. Do not add any additional function prototypes or member variables. You only need to modify the defined functions that we provide you.

The instructor and TAs will not provide any assistance on C++ questions. You can freely use Google or StackOverflow to learn about C++ and any errors you encounter.

需要完成三个矩阵相关的类,两个矩阵类一个矩阵操作类,内容就是一维数组转二维数组并利用二维数组进行矩阵运算。

实验环境搭建

创建你的仓库

If the below git concepts (e.g., repository, merge, pull, fork) do not make sense to you, please spend some time learning git first.

Follow the instruction to setup your own PRIVATE repository and your own development branch. If you have previuosly forked the repository through the GitHub UI (by clicking Fork), PLEASE DO NOT PUSH ANY CODE TO YOUR PUBLIC FORKED REPOSITORY! Make sure your repository is PRIVATE before you git push any of your code.

就是在github上创建你的仓库,注意的不要公开你的代码,请创建为私有仓库,根据instruction创建好就可以了

配置环境

进入实验文件夹后,首先安装需要的依赖,README说明了不同操作系统的配置方法。

sudo ./build_support/packages.sh

之后执行以下命令

$ mkdir build
$ cd build
$ cmake ..
$ make

测试

测试文件为

  • Starter: test/primer/starter_test.cpp

在build文件夹里使用

$ make starter_test
$ ./test/starter_test

You can also run make check-tests to run ALL of the test cases. Note that some tests are disabled as you have not implemented future projects. You can disable tests in GTest by adding a DISABLED_ prefix to the test name.

These tests are only a subset of the all the tests that we will use to evaluate and grade your project. You should write additional test cases on your own to check the complete functionality of your implementation.

注意如果要进行所有测试需要将测试文件里的 DISABLED_ 前缀全部删除

格式化

Your code must follow the Google C++ Style Guide. We use Clang to automatically check the quality of your source code. Your project grade will be zero if your submission fails any of these checks.

Execute the following commands to check your syntax. The format target will automatically correct your code. The check-lint and check-clang-tidy targets will print errors and instruct you how to fix it to conform to our style guide.

使用Google的C++ style,并使用clang来自动化检查代码

使用以下命令来测试代码格式,需要通过以下三个检查才能得分。

$ make format
$ make check-lint
$ make check-clang-tidy

开发技巧

Instead of using printf statements for debugging, use the LOG_* macros for logging information like this:

LOG_INFO("# Pages: %d", num_pages);
LOG_DEBUG("Fetching page %d", page_id);

To enable logging in your project, you will need to reconfigure it like this:

$ mkdir build
$ cd build
$ cmake -DCMAKE_BUILD_TYPE=DEBUG ..
$ make

The different logging levels are defined in src/include/common/logger.h. After enabling logging, the logging level defaults to LOG_LEVEL_INFO. Any logging method with a level that is equal to or higher than LOG_LEVEL_INFO (e.g., LOG_INFO, LOG_WARN, LOG_ERROR) will emit logging information. Note that you will need to add #include "common/logger.h" to any file that you want to use the logging infrastructure.

使用宏函数来替代printf进行日志信息。

提交

可以在gradescope网站进行在线测试

提交的文件需要完整路径

  • src/include/primer/p0_starter.h

实验

这些都是一些基本的C++ 语法,矩阵运算也很简单,主要就是一些C++的特性用的比较多。

Matrix类

需要做的就是实现他的构造函数以及析构函数。构造函数需要动态申请一个一维数组。析构函数需要释放掉。

RowMatrix类

这里继承了Matrix类,有一个二维指针,其思想就是一维数组转二维数组。

RowMatrixOperations类

这里就是利用RowMatrix做矩阵的加法乘法,以及加法乘法的综合应用。

需要注意的点在于unique_ptr

unique_ptr.h中,有

unique_ptr(const unique_ptr&) = delete;

unique_ptr 代表这个指针指向的资源只属于一个owner,因此不能直接赋值给其他变量,注意,直接作为函数参数,也是一种赋值,因此必须使用std::move来进行赋值。不能直接赋值。

编写完在本地测试。首先检查style以及正确性

$ make format 
$ make check-lint 
$ make check-clang-tidy

接着跑本地测试,测试之前需要将测试文件中的 DISABLED_ 前缀删除,不然会有些测试不会run

  • test/primer/starter_test.cpp

测试结果

  • 本地
    image.png
  • Gradescope
    打包脚本
zip -r p0_submission.zip src/include/primer/p0_starter.h

image.png