【tensorflow 解析】-【1】

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调用是如何实现的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值