【问题标题】:Create multiple Instances of Caffe - C++创建多个 Caffe 实例 - C++
【发布时间】:2017-11-15 12:04:37
【问题描述】:

我想在单个 C++ 程序中创建多个 Caffe 实例,以便可以利用多个 GPU,但 Caffe 似乎是一个单例实例。我可以运行使用 Caffe 的单独程序,并为每个程序分配一个唯一的 GPU。多程序方法不像我从一个程序中运行它们那样容易管理。

【问题讨论】:

  • 运行多个进程的效率低于单个进程并不是一个既定事实。
  • @JesperJuhl,我是说管理运行多个程序比管理一个程序更难。我会澄清一下,谢谢。

标签: c++ machine-learning computer-vision gpu caffe


【解决方案1】:

@Apexdev,我正在处理类似的问题。我想在同一个数据集上测试多个模型。以前我使用不同的脚本进行测试,现在我可以使用相同的脚本调用所有脚本。 这是对我有用的解决方案... 头文件:

#include <pthread.h>

第 1 步:定义我们将传递给线程参数的结构

Ex. struct thread_Arguments {
    int GPU_id;
    std::string Model;
    std::vector<std::string> list;
};

第 2 步:创建一个可以执行您想要的任何操作的函数

 void *MultiscriptInvoke(void *threadarg) {
    struct thread_Arguments *Newthread;
    Newthread = (struct thread_Arguments *) threadarg;
    std::string modelFile = Newthread->Model;
    int GPU_ID = Newthread->GPU_ID;
    // Terminate thread using
    pthread_exit(NULL);
}

第 3 步:定义线程创建

pthread_t threads[NUM_THREADS];
pthread_attr_t attr;
void *status;

第四步:初始化线程

pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
struct thread_Arguments td[NUM_THREADS];

第 5 步:创建线程

for (i = 0; i < NUM_THREADS; i++) {
    td[i].GPU_ID = GPU1[i];
    td[i].Model = ModelName[i];
    rc = pthread_create(&threads[i], NULL, MultiscriptInvoke, (void *)i );
    if (rc) {
         cout << "Error:unable to create thread," << rc << endl;
         exit(-1);
     }
}

第 6 步:等待线程完成

   pthread_attr_destroy(&attr);
   for( i = 0; i < NUM_THREADS; i++ ) {
      rc = pthread_join(threads[i], &status);
      if (rc) {
         cout << "Error:unable to join," << rc << endl;
         exit(-1);
      }
   }
   pthread_exit(NULL); 

我希望这会对你有所帮助。根据需要修改结构并创建多个实例。 参考:感谢tutorialspoint

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-30
    • 2018-07-11
    • 2020-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多