1 tensorflow GPU 调用架构
如图:
从上图我们可以看到,Tensorflow提供两种方式调用NVIDIA的方式,而NVIDIA的GPU调用方式主要依靠的CUDA的并行计算框架.
2 Stream Executor
StreamExecutor 是一个子项目,是一个google开源的数学并行运算库,是基于CUDA API、OpenCL API管理各种GPU设备的统一API,这种统一的GPU封装适用于需要与GPU设备通信的库,而在Tensorflow上只提供了对CUDA的支持。位置是tensorflow\stream_executor
文件夹下。stream.h
的部分内容:
......
// Represents a stream of dependent computations on a GPU device.
//
// The operations within a stream execute linearly and asynchronously until
// BlockHostUntilDone() is invoked, which synchronously joins host code with
// the execution of the stream.
//
// If any given operation fails when entraining work for the stream, ok() will
// indicate that an error has occurred. After initialization, once a stream is
// !ok(), it will never be ok().
//
// Thread-safe post-initialization.
class Stream {
public:
// Instantiate a stream tied to parent as a platform executor. Work
// entrained onto this stream will be launched/managed on that
// StreamExecutor's platform.
explicit Stream(StreamExecutor *parent);
// Test only. Use an externally-populated value (like a mock) for the
// platform-specific stream implementation.
Stream(StreamExecutor *parent, internal::StreamInterface *implementation);
// Deallocates any stream resources that the parent StreamExecutor has
// bestowed
// upon this object.
~Stream();
// Returns whether any errors have occurred while entraining work for this
// stream.
bool ok() const { return !InErrorState(); }
......
}
StreamExecutor的主要功能:
- 抽象化底层平台,对开发者不需要考虑底层的GPU的平台
- 流式的管理模式
- 封装了主机和GPU之间的数据移动
在StreamExecutor里封装了几个常见的基本的核心运算:
- BLAS: 基本线性代数
- DNN: 深层神经网络
- FFT: 快速傅里叶变换
- RNG: 随机数生成
3 Stream 接口
-
算子直接通过Stream的API的调用,在Tensorflow里Stream executor 只支持4个核心算法
-
每个算法都提供Support的类,进行多态的支持,比如CUDA, OpenCL
-
通过Support,官方tensorflow 只提供了CUDA支持,如果要支持OpenCL,可以参考开源(点击打开链接)
-
对CUDA的支持使用了基于CUDA平台的第三方开发库,没有直接使用CUDA编