【tensorflow 源码解析】-【1】

版权声明:欢迎转载。转载请注明地址:https://blog.csdn.net/weixin_32820767 https://blog.csdn.net/weixin_32820767/article/details/82259749

1 tensorflow GPU 调用架构
如图:
 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的主要功能:

  1. 抽象化底层平台,对开发者不需要考虑底层的GPU的平台
  2. 流式的管理模式
  3. 封装了主机和GPU之间的数据移动

在StreamExecutor里封装了几个常见的基本的核心运算:

  1. BLAS: 基本线性代数
  2. DNN: 深层神经网络
  3. FFT: 快速傅里叶变换
  4. RNG: 随机数生成

3 Stream 接口
Stream 接口

  1. 算子直接通过Stream的API的调用,在Tensorflow里Stream executor 只支持4个核心算法

  2. 每个算法都提供Support的类,进行多态的支持,比如CUDA, OpenCL

  3. 通过Support,官方tensorflow 只提供了CUDA支持,如果要支持OpenCL,可以参考开源(点击打开链接)

  4. 对CUDA的支持使用了基于CUDA平台的第三方开发库,没有直接使用CUDA编

参考 Tensorflow 源码分析-GPU调用是如何实现的

猜你喜欢

转载自blog.csdn.net/weixin_32820767/article/details/82259749