跳转至

学习笔记

C&C++

  • C++ 语言新机制(如闭包,for_each)
  • C++ STL 使用
    • 容器
    • 算法库
  • 常见算法记录

CUDA 架构

疑问

  • ampere 架构 SM 中有 4 个 process block,process block 对应一个 warp?意思是可以有 4 个 warp 同时执行?
    • Femi 架构没有 process block,SM 就是最小单元?
  • The threads of a thread block execute concurrently on one multiprocessor, and multiple thread blocks can execute concurrently on one multiprocessor.
    • 这样岂不是若干 thread block 抢一个 SM 上的 shared memory?
    • 不同 threadblock 的 warp 并发执行,如何隐藏延迟
  • cuda 分块大小对性能影响很大,那么如何确定分块大小呢?
    • 穷举
    • 分析模型?

WSL 记录

概述

发现使用 wsl + docker + vs code(remote wsl + remote container 插件) 可以让开发、部署、运行整个流程变得更加方便。因此想写一篇文章记录接触 wsl 后了解到的一些知识以及遇到的一些问题以及解决方案(长期更新)。

字体分类

概述

目前还没有一种统一的字体分类方法。因此本文从易于理解的角度,使用 4 个维度对字体进行分类。

简单来说,确定了大小、粗细、样式和字体系列后便确定了一种字体。

font family

字体系列(font family) 表示符合同一种设计风格的字体的集合。比如大名鼎鼎的 Helvetica 字体就因醒目,清晰而广泛应用于广告,宣传语等场合。Time New Roman 也是一种使用非常广泛的字体。

一种字体系列包含多种变体字体,如 Times 包含 TimeIt(倾斜), TimeBd(加粗) 等。我们口语中说的某一种字体,其实通常说的是一种字体系列。

而对于现有的五花八门的字体系列,可以根据一些特点分成 5 类,也叫通用字体系列(generic font families)。如 Helvetica 就属于无衬线 (sans serif) 字体。

以下是字体系列的 5 种类别

Use dlib in C++

use dlib in c++

从方法 1.1 到 1.3 都不必预编译 dlib 库,而是在使用 dlib 的项目中编译。

方法 1.4 介绍了将 dlib 预编译成静态库然后使用时会遇到的一些问题。

with CMake(officially recommend)

dlib/example/CMakeLists.txt

cmake_minimum_required(VERSION 2.8.12)

project(examples)

# Tell cmake we will need dlib.  This command will pull in dlib and compile it
# into your project.  Note that you don't need to compile or install dlib.  All
# cmake needs is the dlib source code folder and it will take care of everything.
add_subdirectory(./dlib dlib_build)

macro(add_example name)
   add_executable(${name} ${name}.cpp)
   target_link_libraries(${name} dlib::dlib )
endmacro()

# if an example requires GUI, call this macro to check DLIB_NO_GUI_SUPPORT to include or exclude
macro(add_gui_example name)
   if (DLIB_NO_GUI_SUPPORT)
      message("No GUI support, so we won't build the ${name} example.")
   else()
      add_example(${name})
   endif()
endmacro()

# 
add_gui_example(3d_point_cloud_ex)

命令行执行:

mkdir build
cd build
cmake .. -G "Visual Studio 14 2015 Win64" -T host=x64 #-T host=x64告诉CMake生成64bit的可执行文件。其实安装了最新的VisualStudio后,-G -T都不用指定,默认使用最新的VisualStudio,默认64位。
cmake --build . --config Release

使用 CMake GUI 可以设置一些选项,configure, generate 之后可以使用命令行执行最后一步 (不用打开 VisualStudio)

with GCC in terminal

g++ -std=c++11 -O3 -I.. ../dlib/all/source.cpp -lpthread -lX11 example_program_name.cpp

windows 下还需gdi32, comctl32, user32, winmm, ws2_32, or imm32

with VisualStudio

  • 把 dlib 的父目录添加到 include search path

    You should NOT add the dlib folder itself to your compiler's include path. Doing so will cause the build to fail because of name collisions (such as dlib/string.h and string.h from the standard library). Instead you should add the folder that contains the dlib folder to your include search path and then use include statements of the form #include or

    include "dlib/queue.h". This will ensure that everything builds correctly.

  • 把 dlib/all/source.cpp 添加到源文件

    If you are using Visual Studio you add .cpp files to your application using the solution explorer window. Specifically, right click on Source Files, then select Add -> Existing Item and select the .cpp files you want to add.

  • 如果需要 libjpeg 等,把 dlib/external 文件夹下的源文件添加到 project,并 define the DLIB_PNG_SUPPORT and DLIB_JPEG_SUPPORT preprocessor directives.

Installing dlib as a precompiled library

dlib 的 CMake 脚本包含 INSTALL target,因此可以像其它 C++ 库一样将 dlib 作为一个静态或动态库安装到系统上。

  • 调用 dlib 时出现 USER_ERROR inconsistent_build_configuration see_dlib_faq_2 错误。 需要将 build/dlib/config.h 文件拷贝到源码目录 dlib-19.17/dlib 进行覆盖。config.h 文件内有其说明。

配置 3d_face_reconstruction 总结

1. OpenCV

  1. windows 下载预编译版 (只含有 msvc 编译的版本)

  2. CMake 直接使用 find_packages 即可。(需添加环境变量,以便找到 OpenCV 的 config 文件)

2. dlib

  1. 官方推荐将代码直接包含到项目中编译(好处是没有 ABI 一致性问题)

    CMake 中 add_subdirectory(/path/to/dlib/top/dir) 即可。(甚至可以自动从网上下载)

3. boost

  1. 分为只含有头文件的库和需要独立编译的库。
  2. 官方编译方法为:
    1. 先 build 出 Boost.Build(可以看作 Boost 的一个 build 工具)
    2. 然后调用 b2 编译指定模块。(可以添加参数指定编译的库的路径)
    3. 我的编译目录是 stage/(库位于 stage/lib/下)
  3. CMake find_packages 暂时是失败的。

4. eos

  1. 只需要包含头文件即可 (header only),include 和 3rdparty
  2. 顶层目录下的 CMakeLists.txt 默认勾选了编译 example 下的示例(需要 boost 的 system, filesystem, program_options 和 openCV 的 core, higui, imgproc
  3. CMake 直接 add_subdirectory(/path/to/eos) 比较方便。

5. Qt

  1. 官方介绍了如何使用 CMake find_packages,自动 tic, moc 等。

TODO

  1. 学习 Qt,能自己写界面。
  2. 明白 eos 那个示例程序输入输出是什么
  3. 最后,自己来改写 eos 的程序,自己写 Qt 程序,最后展示出来。

安装使用 OpenCV(windows)

Install

安装预编译版

opencv.org上提供了源代码以及各个操作系统的预编译版。windows 预编译版为一个 installer 程序,运行后会将各种文件 extract 到安装目录。安装完成后,/path/to/opencv/包含两个目录——build, source。source 为源代码目录 (和直接下载源代码解压一模一样),build 下包含了许多其它文件。其中:

  • include/为调用 OpenCV 时需要包含的头文件 (源代码目录下也有)
  • x64/vcxx/下包含 bin/和 lib/,为预编译的库。vcxx 代表使用 MSVC xx 编译的,运行时需要对应的 runtime 库。

    IDE 编译器
    VS2017 VC15
    VS2015 VC14
    VS2013 VC12
    VS2012 VC11
    VS2010 VC10
    VS2008 VC9
    VS2005 VC8
  • etc/文件夹中是 OpenCV 某些算法所依赖的数据集。
  • OpenCVConfig.cmake文件在使用 CMake 编译项目时可使用 find_package 自动搜索头文件、库文件路径
  • 其它文件暂时不管

从源文件编译 OpenCV

Usage

通过 VisualStudio 编译 OpenCV 程序

  1. 菜单栏 project->properities。(或在右侧的 Solution Explorer 下找到一个 project(solution 和 project 的关系)。鼠标右键->Properities)

  2. C/C++/AdditionalIncludeDirectories。编译器从该文件夹下查找头文件。

    (p.s. #include "opencv2/opencv.hpp"#include "opencv.hpp"对应不同的设置)

  3. Linker/Input/AdditionalDependencies 添加opencv_world420.lib

  4. Linker/General/AdditionalLibraryDirectories 添加opencv_world420.lib所在目录。(或直接在 3 中输入绝对路径)

  5. 设置环境变量,把opencv_world420.dll所在目录添加到 Path 变量。(否则运行时报错:无法找到opencv_world420.dll)。不添加环境变量的方法是直接把opencv_world420.dll拷贝到 exe 所在目录。

    p.s. 貌似opencv_world420.lib表面上是一个静态链接库,但实际上是一个动态链接库,因而运行时需要opencv_world420.dll。而第 3 步如若改成添加opencv_world420.dll,则编译失败。

    LNK1107 could also occur if you attempt to pass a module (.dll or .netmodule extension created with /clr:noAssembly or /NOASSEMBLY) to the linker; pass the .obj file instead.

通过 CMake 编译 OpenCV 程序

参考:Using OpenCV with gcc and CMake

通过 CMake 编译 OpenCV 程序有几点好处:

  • 在 Windows 和 Linux 之间移植程序不用更改设置。
  • 自动搜索 include,lib 路径。(通过OpenCVConfig.cmake文件,需添加到 Path 环境变量)
  • 写代码的方式比 VisualStudio 点击按钮然后输入方便,配置的文件可以保留下来用于其它项目。

缺点:

步骤:

  1. 添加opencv/build/到 Path(OpenCVConfig.cmake所在目录)。

  2. 编写CMakeLists.txt。示例:

    cmake_minimum_required(VERSION 2.8)
    project( DisplayImage )
    find_package( OpenCV REQUIRED ) //
    add_executable( DisplayImage DisplayImage.cpp )
    target_link_libraries( DisplayImage ${OpenCV_LIBS} ) //
    
  3. 使用 CMake GUI,点击 configure(可以先 File->Delete Cache,删除掉之前的配置)

    选择 VS 的编译器。

  4. 在中间面板修改一些变量的值。(重新 configure)

  5. Generate,在 build 目录下生成 VS 的 sln 文件,双击打开。

  6. 在 ALL_BUILD 上右键 build。

运行 HPL

编译 BLAS/CBLAS

BLAS 为 fortran 接口的库,CBLAS 为 C 语言接口的库

 tar zxf blas-3.8.0.tgz 
 tar zxf cblas.tgz
 cd BLAS-3.8.0
 make #生成blas_LINUX.a
 cp blas_LINUX.a ../CBLAS
 cd ../CBLAS
 vi Makefile.in
 #修改
 # BLLIB = ../blas_LINUX.a
 make #在lib/下生成cblas_LINUX.a

编译 HPL

tar zxf hpl-2.3.tar.gz 
cd hpl-2.3
cp setup/Make.Linux_PII_CBLAS ./Make.<arch> #在top-level 文件夹下
vi Make.<arch> #修改
make <arch> #在bin/<arch>/下生成HPL.dat xhpl

Make.示例

OpenMPI+CBLAS
ARCH         = <arch>

TOPdir       = /home/mpiuser/hpl-2.3

MPdir        = /path/to/OpenMPI
MPinc        = -I$(MPdir)/include
MPlib        = $(MPdir)/lib/libmpi.so

LAdir        = 
LAinc        = 
LAlib        = path/to/cblas_LINUX.a path/to/blas_LINUX.a #顺序不能改变,cblas依赖blas

HPL_OPTS     = -DHPL_CALL_CBLAS 

CC           = mpicc
LINKER       = mpif77

HPL 优化

配置参数

计算次数:

\[ \#Ns \times \#NBs \times \#(Ps, Qs) \times \#PFACTs \times \#NBMINs \times \#BCASTs \times \#DEPTHs \]
HPLinpack benchmark input file
Innovative Computing Laboratory, University of Tennessee
HPL.out      output file name (if any)
6            device out (6=stdout,7=stderr,file)
1            # of problems sizes (N)
8192         Ns
1            # of NBs
128          NBs
0            PMAP process mapping (0=Row-,1=Column-major)
3            # of process grids (P x Q)
2 1 4        Ps
2 4 1        Qs
16.0         threshold
3            # of panel fact
0 1 2        PFACTs (0=left, 1=Crout, 2=Right)
2            # of recursive stopping criterium
2 4          NBMINs (>= 1)
2            # of panels in recursion
2            NDIVs
3            # of recursive panel fact.
0 1 2        RFACTs (0=left, 1=Crout, 2=Right)
1            # of broadcast
0            BCASTs (0=1rg,1=1rM,2=2rg,3=2rM,4=Lng,5=LnM)
1            # of lookahead depth
0            DEPTHs (>=0)
2            SWAP (0=bin-exch,1=long,2=mix)
64           swapping threshold
0            L1 in (0=transposed,1=no-transposed) form
0            U  in (0=transposed,1=no-transposed) form
1            Equilibration (0=no,1=yes)
8            memory alignment in double (> 0)