mercredi 17 octobre 2018

multithreading multiple short tasks in C++ 11 slows down the process?

I'm not really experienced when it comes down to multithreading. I have a facial landmark detector that detects 68 landmarks around the facial components. For every single landmark HoG features around it need to be extracted and appended to the previous landmark features to create a giant vector before passing it to the regressor.

Currently, all the features are getting extracted in serial one after another and I'm trying to extract them in Parallel to speed up the process.

Extracting the features around all the landmarks IN SERIAL takes about 2.5ms on my system. When I try to parallelize it using 68 threads, it takes about 8.5ms extracting features around all the landmarks. So it actually slows down the process and I'm guessing this is probably because of the threads initializing time.

The following is the original code in serial

for(int i = 0; i < 68; i++){   // for each landmark

    fx = shape[i];       // x position
    fy = shape[i + 68];  // y position

    extract_features(image, fx, fy, &features[i]);
}

Now this is what I have done to parallelize it

vector<std::thread> threads;

for(int i = 0; i < 68; i++){   // for each landmark

    fx = shape[i];       // x position
    fy = shape[i + 68];  // y position

    threads.emplace_back( 
        [image, fx, fy, &] () { extract_features(image, fx, fy, &features[i]); } 
    );  
}

for(int x  = 0; x < 68; x++)
    threads[x].join();

I should be doing something wrong which is slowing down the process instead of speeding it up. My best guess is, initializing a thread the way I'm doing it is more time consuming that the task itself. If that's the case, is there a way I can initialize the threads already and just run them in the for loop?

I would very much appreciate your help in guiding me through finding the right approach to this project.

Thanks,

Aucun commentaire:

Enregistrer un commentaire