Caffe源码解读2--syncedmem.hpp
2016-05-13 15:03
405 查看
#ifndef CAFFE_SYNCEDMEM_HPP_
#define CAFFE_SYNCEDMEM_HPP_
#include <cstdlib>
#include "caffe/common.hpp"
#include "caffe/util/math_functions.hpp"
namespace caffe {
// Theoretically, CaffeMallocHost and CaffeFreeHost should simply call the
// cudaMallocHost and cudaFree functions in order to create pinned memory.
// However, those codes rely on the existence of a cuda GPU (I don't know
// why that is a must since allocating memory should not be accessing the
// GPU resource, but it just creates an error as of Cuda 5.0) and will cause
// problem when running on a machine without GPU. Thus, we simply define
// these two functions for safety and possible future change if the problem
// of calling cuda functions disappears in a future version.
//
// In practice, although we are creating unpinned memory here, as long as we
// are constantly accessing them the memory pages almost always stays in
// the physical memory (assuming we have large enough memory installed), and
// does not seem to create a memory bottleneck here.
//SynceMem:做内存同步的操作。
//两个全局的内联函数。
//机器支持GPU并且安装了cuda,通过cudaMallocHost分配的hostmemory将会被pinned(内存不会被paged out),
//注:内存有页作为基本的管理单元,分配的内存可以常住在内存空间中对提高效率有帮助,空间不会被别的进程抢占。
//内存越大,能背分配的pinned内存自然也越大。对于单一的GPU提升不显著,但对多个GPU可以显著提高稳定性。
inline void CaffeMallocHost(void** ptr, size_t size) {
*ptr = malloc(size);
CHECK(*ptr) << "host allocation of size " << size << " failed";
}
inline void CaffeFreeHost(void* ptr) {
free(ptr);
}
/**
* @brief Manages memory allocation and synchronization between the host (CPU)
* and device (GPU).
*
* TODO(dox): more thorough description.
*/
class SyncedMemory {//SyncedMemory类,首先是构造函数和析构函数。
public:
SyncedMemory()//参数构造函数:负责初始化
: cpu_ptr_(NULL), gpu_ptr_(NULL), size_(0), head_(UNINITIALIZED),
own_cpu_data_(false) {}
explicit SyncedMemory(size_t size)//带explicit关键字:单个参数构造函数,explicit禁止单参数构造函数的隐式转换
: cpu_ptr_(NULL), gpu_ptr_(NULL), size_(size), head_(UNINITIALIZED),
own_cpu_data_(false) {}
~SyncedMemory();//其在析构时,也调用CaffeFreeHost
const void* cpu_data();//获得cpu上data的地址
void set_cpu_data(void* data);//将cpu的data指针指向一个新的区域由data指针传入,并且将原来申请的内存释放。
const void* gpu_data();//获得gpu数据地址//获得set gpu的数据地址
void* mutable_cpu_data();//返回cpu上data的指针
void* mutable_gpu_data();//返回gpu上data指针
enum SyncedHead { UNINITIALIZED, HEAD_AT_CPU, HEAD_AT_GPU, SYNCED };//SyncedHead是个枚举类型,用来设定head_状态,
SyncedHead head() { return head_; }//head()函数即返回相应的数据状态,
size_t size() { return size_; }//size()函数返回数据大小
private:
void to_cpu();
void to_gpu();
void* cpu_ptr_;
void* gpu_ptr_;
size_t size_;
SyncedHead head_;
bool own_cpu_data_;
//head未被初始化的状态:首先需要分配内存,根据cpu和gpu的情况而定,再将数据从cpu或gpu拷贝到另一处。
//之后函数会重新标记Head状态,数据是否在cpu或者在gpu中。
//cpu_ptr和gpu_ptr分别是cpu和gpu中的数据指针
DISABLE_COPY_AND_ASSIGN(SyncedMemory);//禁止该类的拷贝与赋值
}; // class SyncedMemory
} // namespace caffe
#endif // CAFFE_SYNCEDMEM_HPP_
#define CAFFE_SYNCEDMEM_HPP_
#include <cstdlib>
#include "caffe/common.hpp"
#include "caffe/util/math_functions.hpp"
namespace caffe {
// Theoretically, CaffeMallocHost and CaffeFreeHost should simply call the
// cudaMallocHost and cudaFree functions in order to create pinned memory.
// However, those codes rely on the existence of a cuda GPU (I don't know
// why that is a must since allocating memory should not be accessing the
// GPU resource, but it just creates an error as of Cuda 5.0) and will cause
// problem when running on a machine without GPU. Thus, we simply define
// these two functions for safety and possible future change if the problem
// of calling cuda functions disappears in a future version.
//
// In practice, although we are creating unpinned memory here, as long as we
// are constantly accessing them the memory pages almost always stays in
// the physical memory (assuming we have large enough memory installed), and
// does not seem to create a memory bottleneck here.
//SynceMem:做内存同步的操作。
//两个全局的内联函数。
//机器支持GPU并且安装了cuda,通过cudaMallocHost分配的hostmemory将会被pinned(内存不会被paged out),
//注:内存有页作为基本的管理单元,分配的内存可以常住在内存空间中对提高效率有帮助,空间不会被别的进程抢占。
//内存越大,能背分配的pinned内存自然也越大。对于单一的GPU提升不显著,但对多个GPU可以显著提高稳定性。
inline void CaffeMallocHost(void** ptr, size_t size) {
*ptr = malloc(size);
CHECK(*ptr) << "host allocation of size " << size << " failed";
}
inline void CaffeFreeHost(void* ptr) {
free(ptr);
}
/**
* @brief Manages memory allocation and synchronization between the host (CPU)
* and device (GPU).
*
* TODO(dox): more thorough description.
*/
class SyncedMemory {//SyncedMemory类,首先是构造函数和析构函数。
public:
SyncedMemory()//参数构造函数:负责初始化
: cpu_ptr_(NULL), gpu_ptr_(NULL), size_(0), head_(UNINITIALIZED),
own_cpu_data_(false) {}
explicit SyncedMemory(size_t size)//带explicit关键字:单个参数构造函数,explicit禁止单参数构造函数的隐式转换
: cpu_ptr_(NULL), gpu_ptr_(NULL), size_(size), head_(UNINITIALIZED),
own_cpu_data_(false) {}
~SyncedMemory();//其在析构时,也调用CaffeFreeHost
const void* cpu_data();//获得cpu上data的地址
void set_cpu_data(void* data);//将cpu的data指针指向一个新的区域由data指针传入,并且将原来申请的内存释放。
const void* gpu_data();//获得gpu数据地址//获得set gpu的数据地址
void* mutable_cpu_data();//返回cpu上data的指针
void* mutable_gpu_data();//返回gpu上data指针
enum SyncedHead { UNINITIALIZED, HEAD_AT_CPU, HEAD_AT_GPU, SYNCED };//SyncedHead是个枚举类型,用来设定head_状态,
SyncedHead head() { return head_; }//head()函数即返回相应的数据状态,
size_t size() { return size_; }//size()函数返回数据大小
private:
void to_cpu();
void to_gpu();
void* cpu_ptr_;
void* gpu_ptr_;
size_t size_;
SyncedHead head_;
bool own_cpu_data_;
//head未被初始化的状态:首先需要分配内存,根据cpu和gpu的情况而定,再将数据从cpu或gpu拷贝到另一处。
//之后函数会重新标记Head状态,数据是否在cpu或者在gpu中。
//cpu_ptr和gpu_ptr分别是cpu和gpu中的数据指针
DISABLE_COPY_AND_ASSIGN(SyncedMemory);//禁止该类的拷贝与赋值
}; // class SyncedMemory
} // namespace caffe
#endif // CAFFE_SYNCEDMEM_HPP_
相关文章推荐
- 丰趣海淘:跨境电商平台的前端性能优化实践
- Improving neural networks by preventing co-adaptation of feature detectors阅读
- Swap Nodes in Pairs Java实现
- ck-reset css(2016/5/13)
- JS中Null与Undefined的区别
- WPS之替换样式
- Bootstrap 中的插件的学习
- jQuery Easyui 验证两次密码输入是否相等
- 利用jsp+uploadify插件实现附件下载
- CSS学习之列表 - CSS: The Missing Manual
- react native学习笔记
- 20160513--js 弹出窗口带有iframe控件 备忘
- JavaScript 跨域:谈谈跨域之 JSONP
- JavaScript内的类型转换
- 前端精选文摘:BFC 神奇背后的原理
- 深入理解CSS height属性设置元素的高度
- HTML5拖放
- HTML5中的验证功能
- 浅析javascript异步执行函数导致的变量变化问题解决思路
- mysql Sort aborted: Out of sort memory, consider increasing server sort buffer size的解决方法