Caffe源码解读2--syncedmem.hpp
来源:互联网 发布:什么是淘宝黑搜 编辑:程序博客网 时间:2024/04/30 14:27
#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_
0 0
- Caffe源码解读2--syncedmem.hpp
- caffe源码阅读2-syncedmem.hpp+.cpp
- Caffe源码解析2:SyncedMem
- caffe源码追踪--syncedmem
- caffe源码深入学习3:更底层的数据信息存取与交换代码:syncedmem.hpp和syncedmem.cpp
- Caffe源码解读1--blob.hpp
- Caffe源码解读3--layer.hpp
- Caffe源码中syncedmem文件分析
- caffe1源码解析从入门到放弃1):内存管理syncedmem.hpp / syncedmem.cpp
- Caffe 源码阅读笔记 [基本模块] Syncedmem & Blob
- Caffe代码解读(一):blob.hpp
- Caffe代码解读(二):base_data_layer.hpp
- caffe源码深入学习2:blob.hpp+blob.cpp
- caffe源码分析:layer.hpp分析
- caffe源码分析:blob.hpp分析
- caffe源码阅读1-blob.hpp
- caffe源码阅读4-layer.hpp
- caffe源码vision_layers.hpp+各cpp
- iOS开发常用类库
- LINUX下MYSQL的启动及相关配置
- 安装cassandra作为kairosdb的数据存储
- Android:屏幕旋转时,禁止Activity重新加载
- POJ 2236 Wireless Network(并查集)
- Caffe源码解读2--syncedmem.hpp
- fragment中弹出PopupWindow窗体,点击外界屏幕不消失问题解决
- numpy各函数简介之生成数组函数(Array creation routines)
- myeclipse下SSH整合环境搭建
- SVN和Git的比较
- 50. Pow(x, n)
- 安装mysql
- 观察者模式及Java实现例子
- Powershell check server patch status