Running CUDA Samples Based on GPGPU-Sim
CUDA Samples
CUDA Samples是示范CUDA编程概念的软件包。自CUDA 4.1开始,CUDA Toolkit搭载了CUDA Samples安装包。CUDA Samples的目录结构如下:
- 0_Simple
- 1_Utilities
- 2_Graphics
- 3_Imaging
- 4_Finance
- 5_Simulations
- 6_Advanced
- 7_CUDALibraries
- EULA.txt
- Makefile
- bin
- common
Incompatible GCC Version
如果你安装的是CUDA Toolkit 9.0, 它所兼容的GCC Version最高到 gcc-6. 如果你的环境是Ubuntu 19.10,那么在编译CUDA Samples之前,你需要降级GCC Version到6或以下,方法如下:
1 | sudo apt install gcc-4.8 |
update-alternatives 更新/etc/alternatives目录下的gcc链接指向/usr/bin/gcc-4.8, 它的最后的参数10是指priority.
NOTE: 安装CUDA Toolkit时会进行GCC Compatibility检查,但是可以通过下面选项忽略这个检查:
1 | sudo sh cuda_9.0.176_384.81_linux.run --override |
Build Samples
构建CUDA Samples可以在顶层目录构建所有Samples, 也可以到单个目录下构建一个Sample. 为了能够让可执行文件能够链接到GPGPU-Sim的libcudart.so,需要这样运行构建命令:
1 | make NVCCFLAGS="--cudart shared" |
Run Samples
在 Run Samples 之前,需要配置GPGPU-Sim的仿真参数和libcudart.so的路径:
1 | cp /path/to/gpgpu-sim/configs/tested-cfgs/SM2_GTX480/* /path/to/executables |
现在可以让CUDA Samples运行在GPGPU-Sim仿真器上了。
Documents
1 | find /usr/local/cuda-9.0 -name '*.pdf' -printf '%f\n' |
通过上面的命令可以找到介绍如何编写Samples以及相关的图形学原理的文档,亦包括CUDA编程的相关文档,可供初学者参考。
References:
vim 速查手册
Visual Block Selection
-
选择
{}(curly braces) 之间的行(包括{})v%
-
选择
{}(curly braces) 之间的行(不包括{})vi{
注意:光标必须放在 { 或者 }
Copy & Paste
Copy to clipboard
- Normal 模式
"+y
Paste from clipboard
X11 window system 有 3 个 selections:
- PRIMARY 表示当前的可见 selection
- SECONDARY
- CLIPBOARD 通常所说的剪贴板,用来完成剪贴,复制和粘贴操作
Vim 有两个专门的寄存器分别与 PRIMARY selection 和 CLIPBOARD selection 对应
"*(quotestar) PRIMARY"+(quoteplus) CLIPBOARD
如果要在 Vim 内部剪贴,复制/粘贴就使用 quotestar, 如果要将内容剪贴,复制/粘贴到系统剪贴板,就使用 quoteplus
举个例子,在 WSL Ubuntu 里如果想粘贴 Windows 剪贴板里的内容到 vim (最好是 neovim, 因为 vim 可能未使能 clipboard)
1 | echo "Hello, world!" | clip.exe |
- Normal 模式
"+p
如果想粘贴 Ubuntu 剪贴板里的内容
1 | echo "Hello, world!" | xclip |
- Normal 模式
"*p
Copy from above or below
在 Insert 模式下
<ctrl-y>copies the character from the line above<ctrl-e>copies the character from the line below (本行就是这样输入的)
Search & Replace
删除多行 C-style 注释
-
s;/\*\_.\{-}\*/;;-
;因为要匹配/, 为了省去转义/的麻烦,将 search 命令的分隔符由/改为; -
/\*匹配开始的/* -
\_.匹配任意字符,包括\n, 所以常用在多行匹配 -
\{-}指非贪婪 (non-greedy) 匹配,即匹配最短的字串,默认是贪婪匹配,匹配最长的字串 -
\*/匹配结尾的*/ -
;;指使用空替换匹配结果,即删除 -
以下是 vim 中在单行中使用的通配符 (wildcard), 这些通配符一般不能匹配换行
\n.匹配除了\n的任意字符^行首,即锚点 (anchor)$行尾,即锚点\s匹配 space, tab, 但不匹配\n
-
以上通配符加上
\_后可以在多行匹配中使用,即也可以匹配\n\_.匹配任意字符,包括\n\_^多行中第一行行首\_$多行中最后一行行尾\_s匹配 space, tab 和\n
-
删除空白行
-
g/^\s*$/dg指所有行搜索,而不是默认的光标所在行^\s*$匹配行首到行尾之间任意个空白字符,即空白行d删除命令 (delete)
搜索不匹配某模式的行,即反向搜索
-
/^\(\(^# .*$\)\@!.\)*$/查找命令 (search) 的提示符^\(\(The_Regex\)\@!.\)*$反向查找的命令固定模式,本例中The_Regex是^# .*$即以#开头的行,所以整个表达式匹配的就是不以#开头的所有行
-
:v/^# .*$/p- 与上面的反向查找命令功能相同, 但会将匹配结果显示在 Visual 模式下
大小写转换
-
s/\(^# .*$\)/\L\1/\(The_Regex\)为了向前索引,即后面\1所指的部分,本例中The_Regex是^# .*$, 意思同上\L\1\L指 Lowercase, 即将\1匹配的结果中的所有字母都换成小写
-
s/\(^# .*$\)/\U\1/- 与上面的表达式功能相反,即将
\1匹配的结果中的所有字母都换成大写 (Uppercase)
- 与上面的表达式功能相反,即将
expression register
假如你在 markdown 文件里编辑一个 Table, table 的第1列是 find 的输出:一个很长的文件列表,且文件名长短不一(假如最长的文件名是 100 个字符),你如何在 100 列插入 vertical bar (|)?
-
N,Ms/.*/\=printf('%-101s|', submatch(0))/N,M指定替换的范围\=表示使用 expression register, 这里相当于把printf()函数的输出导入到这里
TOhtml
vim 有一个内置的扩展 TOhtml, 可以将 vim 的 buffer 自动转成 html 文件,这个功能对于 vimdiff 时尤其有用。
Debugging X11 with tcpdump
X11 is designed as client-server mode. The communication between the X client and server complies with TCP protocol. Recently I have a Windows X server VcXsrv installed on my Windows 10 and I debug an OpenGL demo glxgears on the WSL2 with tcpdump.
Environment
WSL2 is equipped with its own networking interface like a virtual machine.
1 | $ ifconfig |
1 | $ /mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -C "ipconfig" |
After starting up the X server VcXsrv, you need to export the environmnet variable DISPLAY on the WSL. In case of the vEthernet configuration changed after rebooting you’d better do it like this:
1 | export DISPLAY=$(grep 'nameserver' /etc/resolv.conf | awk '{print $2}'):0 |
NOTE: The Firewall between the host and WSL2 must be disabled or your X client can not connect VcXsrv.
Debugging
I trace the demo glxgears using gdb and tcpdump at the same time.
- gdb
1 | gdb -q -tui glxgears |
- tcpdump
1 | sudo tcpdump -vvX not icmp and not arp and not udp and portrange 37900-37999 -w x110224.pcap |
- vv: verboser than -v
- X: show the packet’s content
- not icmp: filter out icmp packets
- not arp: filter out arp packets
- not udp: filter out udp packets
- portrange 37900-37999: listening on the ports from 37900 to 37999
- w x110224.pcap: save the packet captures into the file
1 | tcpdump -X -r x110224.pcap |
1 | reading from file /home/luc/github/x110224.pcap, link-type EN10MB (Ethernet) |
what codes sends and receives these packets? The first two twenty-byted segments are IP header (20 bytes without option) and TCP header (20 bytes without option) separately in these packets.
The source code snippet:
1 | Bool |
The gdb log:
1 | Starting program: /mnt/c/Users/lulu/Documents/github/demos/src/xdemos/glxgears |
$2 is Request packet content to VcXsrv, $3 is Reply packet content from VcXsrv. Even that we can notice the three-way handshake of TCP from the zero-lengthed packet in x110224.pcap.
Notes for Mesa
Overview
Mesa 包含了各种 GPU/CPU 的 OpenGL, OpenCL, Vulkan 实现(Usermode Driver), 也包括 GLX, EGL, GBM 等协议的实现。
- Vulkan 驱动
| codename | directories | platforms |
|---|---|---|
| anv | src/intel/vulkan, src/intel/vulkan_hasvk | Alder Lake-P |
| asahi | src/asahi/vulkan | Apple M1, M2 |
| lpv | src/gallium/drivers/llvmpipe | CPU |
| panvk | src/panfrost/vulkan | RK3399 |
| v3dv | src/broadcom/vulkan | Raspberry Pi |
Build
Build Dependencies (since mesa-25.0.0)
| dep | apt-get | version required | yet another install | |
|---|---|---|---|---|
| /usr/bin/glslangValidator | glslang-tools | https://github.com/KhronosGroup/glslang | ||
| /usr/bin/rustc | rustc | 1.78.0 or newer | `curl --proto ‘=https’ --tlsv1.2 -sSf https://sh.rustup.rs | |
| bindgen (rust package) | cargo | 0.65 or newer | cargo install bindgen-cli |
|
| libclc-dev | libclc-17-dev | not required if -Dmesa-clc=auto | ||
| + | libdrm | libdrm-dev | 2.4.121(120 ok2.0) | https://gitlab.freedesktop.org/mesa/drm |
| llvm | llvm-17-dev | (17.0.6 ok2.0) | https://github.com/llvm/llvm-project | |
| libLLVMSPIRVLib.so.17 | llvm-spirv-17 | (17.0.0 ok2.0) | https://github.com/KhronosGroup/SPIRV-LLVM-Translator | |
| + | LLVMSPIRVLib.h | llvmspirvlib-17-dev | (absent ok2.0) | https://github.com/KhronosGroup/SPIRV-LLVM-Translator |
| clang-cpp | libclang-cpp17-dev | (17.0.6 ok2.0) | not required if -Dmesa-clc=auto | |
| * | clang-dev | libclang-17-dev | Debian package issue | |
| xshmfence | libxshmfence-dev | (1.3 ok2.0) | required if -Dplatforms=x11 | |
| xxf86vm | libxxf86vm-dev | (1.1.4 ok2.0) | required since -Dglx-direct=true by default | |
| xrandr | libxrandr-dev | (1.5.2 ok2.0) | required since -Dxlib-lease=true | |
| cbindgen (rust package) | cargo | 0.28.0 | cargo install cbindgen; required if -Dgallium-drivers=nouveau |
NOTE:
-
(*) 表示本来不需要的依赖
-
(+) 在 OpenKylin 2.0 的源里没有,需要源码构建
-
Ubuntu 22.04
书到用时方恨少,包要装时不好找
1 | sudo apt install -y cmake ninja-build bison flex g++ git pkg-config python3-setuptools python3-lz4 \ |
1 | meson build -Dprefix=/usr/local -Dlibdir=lib/x86_64-linux-gnu -Dplatforms=x11 -Dgallium-drivers=nouveau |
-Dprefix=/usr/local避免覆盖系统原来的libGL*-Dlibdir=lib/x86_64-linux-gnu设置库的安装路径为/etc/ld.so.conf.d/x86_64-linux-gnu.conf中搜索第一顺位的路径- 其他默认就好, 出现的依赖在 OpenKylin 2.0 上 apt-get 基本都能解决
1 | Build targets in project: 437 |