CudaSteps/capter14/ReadMe.md

99 lines
2.8 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# cuda 标准库的使用
+ Thrust 类型 c++ 的标准模板库;
+ cuBLAS基本线性代数子程序
+ cuFFT快速傅里叶变换
+ cuSPARSE稀疏矩阵
+ cuRAND随机数生成器
+ cuSolver稠密矩阵和稀疏矩阵计算库
+ cuDNN深度神经网络。
------
## Thrust
**Thrust**一个实现了众多基本并行算法的c++模板库类似c++的标准库stl。
[Thrust官方资料](https://github.com/NVIDIA/thrust)
1. 数据结构
Thrust 中的数据结构主要是矢量容器,类似 stl 中的 std::vector:
+ 存储于主机的容器 `thrust::host_vector<typename>`;
+ 存储于设备的容器 `thrust::device__vector<typename>`;
容器的使用也类似于 stl
```cuda
// 包含头文件
#include<thrust/host_vector.h>
#include<thrust/device_vector.h>
// 定义并初始化主机内存
thrust::host_vector<double> arr(12, 0.0);
```
Thrust 函数可以直接调用设备上的矢量容器。
2. 算法
Thrust 提供5类常用算法变换归约前缀和排序于搜索选择性复制、替换、移除、分区等重排操作。
Thrust 函数的参数必须都来自于主机容器或者都来自于设备容器thrust::copy 除外。
如果程序中大量使用了 thrust 库,使用设备矢量较为合适;如果只是偶尔使用 Thrust 库,
则使用设备内存指针更为合适。
------
## cuBLAS
**cuBLAS**,一个基本线性代数子程序,提供三层功能函数:
+ 处理矢量之间的计算,如矢量之间的内积;
+ 处理矩阵和矢量之间的运算,如相乘;
+ 处理矩阵之间的运算,如相乘。
CUBLAS 中矩阵采用 **列主序**,即矩阵数据按列存储。
[cuBLAS官方资料](https://docs.nvidia.com/cuda/cublas/index.html)
------
## cuSolver
**cuSolver**:稠密矩阵和稀疏矩阵计算库。
cuSolver 相比于 cuBLAS专注于一些比较高级的线性代数计算并由3个子库组成
+ cuSolverDN处理稠密矩阵线性代数计算
+ cuSolverSP处理稀疏矩阵线性代数计算
+ cuSolverRF处理稀疏矩阵分解。
cuSolver 库函数倾向于使用异步执行。为例保证一个 cuSolver 函数的工作已完成,
可以使用 `cudaDeviceSynchronize()` 函数进行同步。
cuSolver 中矩阵同样采用 **列主序**
[cuSolver官方资料](https://docs.nvidia.com/cuda/cusolver/index.html)
------
## cuRAND
**cuRAND**:随机数生成器。
cuRAND 库中提供两种 API 主机API 和 设备API。以主机API为例使用方式
```cuda
#include <curand.h>
编译时指定链接选项 `-lcurand`
```
同时主机API 分为两种使用方式:
+ 使用设备产生伪随机数并存于设备数组;
+ 使用主机产生伪随机数并存于主机数组。
[cuRAND官方资料](https://docs.nvidia.com/cuda/curand/index.html)
------