blob中每一个元素是按照float类型来存储的,占4个byte
这里面定义了几个类,先看
template <typename Dtype> //模板类
class SolverRegistry {
public://定义了两个成员变量
//名为Creator的函数指针,参数为SolverParameter&类型的引用,返回值为一个Solver类型的指针
typedef Solver<Dtype>* (*Creator)(const SolverParameter&);
//将一个键值对为“字符串-函数指针”的容器命名为CreatorRegistry
typedef std::map<string, Creator> CreatorRegistry;
//静态成员方法Registry(),为一个CreatorRegistry类型的变量分配内存,返回变量的指针
static CreatorRegistry& Registry() {
//定义指针变量g_registry,只想一块由CreatorRegistry()分配的内存
//那么CreatorRegistry()方法在哪里定义的?
static CreatorRegistry* g_registry_ = new CreatorRegistry();
//返回变量的引用
return *g_registry_;
}
// Adds a creator.向CreatorRegistry中增加键值对
static void AddCreator(const string& type, Creator creator) {
//生成一个键值对容器
CreatorRegistry& registry = Registry();
CHECK_EQ(registry.count(type), 0)
<< "Solver type " << type << " already registered.";
//根据type添加一个Creator
registry[type] = creator;
}
// Get a solver using a SolverParameter.根据参数param生成一个求解器Solver,返回指针
static Solver<Dtype>* CreateSolver(const SolverParameter& param) {
const string& type = param.type();
CreatorRegistry& registry = Registry();
CHECK_EQ(registry.count(type), 1) << "Unknown solver type: " << type
<< " (known types: " << SolverTypeListString() << ")";
//这里尤其值得注意,registry[type]代表一个函数指针,通过参数param,执行函数,返回函数执行的结果,应该是生成一个Solver
return registry[type](param);
}
//向一个vector<string>变量中迭代插入求解器的名字,也就是type
static vector<string> SolverTypeList() {
CreatorRegistry& registry = Registry();
vector<string> solver_types;
for (typename CreatorRegistry::iterator iter = registry.begin();
iter != registry.end(); ++iter) {
solver_types.push_back(iter->first);
}
return solver_types;
}
private:
// Solver registry should never be instantiated - everything is done with its
// static variables.
SolverRegistry() {}
//作用是输出所有的求解器类型
static string SolverTypeListString() {
vector<string> solver_types = SolverTypeList();
string solver_types_str;
for (vector<string>::iterator iter = solver_types.begin();
iter != solver_types.end(); ++iter) {
if (iter != solver_types.begin()) {
solver_types_str += ", ";
}
solver_types_str += *iter;
}
return solver_types_str;
}
};
看第二个类
//仅仅实现打印出solver的类型和添加键值对?
template <typename Dtype>
class SolverRegisterer {
public:
SolverRegisterer(const string& type,
Solver<Dtype>* (*creator)(const SolverParameter&)) {
// LOG(INFO) << "Registering solver type: " << type;
SolverRegistry<Dtype>::AddCreator(type, creator);
}
};
下面这个宏定义的函数比较重要
//
#define REGISTER_SOLVER_CLASS(type) \
template <typename Dtype> \
Solver<Dtype>* Creator_##type##Solver( \ const SolverParameter& param) \
{ \
return new type##Solver<Dtype>(param); \
} \
REGISTER_SOLVER_CREATOR(type, Creator_##type##Solver)