solver_factory.hpp的理解

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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值