Making multiple instances of Caffe - C ++

I want to create multiple Caffe instances in the same C ++ program, so I can use multiple GPUs, but Caffe seems to be a singleton instance. I can run separate programs that use Caffe and assign a unique GPU to each program. The method of multiple programs is not as easily manageable as if I were running them all from one program.

+3


source to share


1 answer


@Apexdev, I was working on a similar problem. I wanted to test multiple models on one dataset. I used to use for testing using different scripts like yours and now I can call all scripts using the same script. Here is the solution that worked for me ... Header file:

#include <pthread.h>

      

Step 1: Define the structure we will pass to the stream arguments

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

      

Step 2. Create a function that will perform whatever action you want

 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);
}

      

Step 3: defining thread creation

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

      



Step 4: initializing threads

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

      

Step 5: creating streams

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);
     }
}

      

Step 6: Wait for the threads to complete.

   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); 

      

Hope this helps you. Change the structure to suit your needs and create multiple instances. ref: Thanks to tutorialspoint

+3


source







All Articles