Multithreading Problem

asked 2020-07-06 10:47:24 -0600

Horst gravatar image

updated 2020-07-06 10:53:09 -0600

Hey guys,

I'm facing a problem: I want to generate multiple oversegmented images (images with multiple superpixels). To reduce the time consumption, I want to do this in parallel. My code looks like the following:

int main(){
//Initialization Stuff
pFileHandler = new cFileHandler();
pPreProcessing = new cPreProcessing();
pSegmentation = new cSegmentation();
//Read Images
pFileHandler->m_read_images();
//Start Threads
for (int i = 0; i < number_of_threads; i++)
{
    thread_arr.push_back(new boost::thread(threadFunc, i));
}
//Wait for all threads to finish
for (int i = 0; i < number_of_threads; i++)
{
    thread_arr.at(i)->join();
}
   }

void threadFunc(int thread_ID){

std::cout << "ID: " << thread_ID << std::endl;
pSegmentation->segment_image(thread_ID, pFileHandler->getRGB(), segmented_images_arr);
    }

In the moment all segmentation processes are identical and each image is segmented by one thread. Problem: The more threads I use, the more execution time it needs. E.g. only using one thread (segment one image) needs about 3 seconds, using 2 threads needs about 4 seconds and using 4 threads about 7 seconds. I am very confused, because I expect that it wont make such a different in using 1,2,3,4 or 6 threads. Does anybody know why this happens? For clarity I am using the SLIC algorithm from the opencv extra modules. I appreciate any hint.

Thanks in advance

Horsti

edit retag flag offensive close merge delete

Comments

you are trying to put your own naive task parallel implementation on top of opencv's already existing data parallel approach - not much to gain this way (look at the SLIC src, there's tons of parallel_for's in it already)

berak gravatar imageberak ( 2020-07-06 13:22:53 -0600 )edit