Hi,<div><br></div><div>I had some small fragmented discussions with Gilles, and Johannes about using OpenMP to speed up the batch face detection.</div><div>Here is part of the conversation I just had:</div><div><br></div><div>
"</div><div><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; color: rgb(68, 68, 68); "><div class="im" style="color: rgb(51, 51, 51); ">Aditya Bhatt wrote:<br>
> In my latest bunch of commits, I have added a face scanner to digiKam, which<br>> detects faces in all images in every album. The relevant code is in<br>> utilities/batch/batchfacedetector.cpp.<br>><br>> It works, but due to the nature of the algorithm, the CPU usage is very high<br>
> and digiKam UI slows down to a crawl. (Press the "rescan all images" button<br>> at the top of the people sidebar at the left).<br><br></div>This is definitely a bug. Sounds like you are running the detection in<br>
the main user interface thread. Instead it must be executed in (a)<br>separate thread(s).<br><div class="im" style="color: rgb(51, 51, 51); "><br>> Gilles says that using OpenMP would be a good idea for this.<br>> I don't know anything about OpenMP, so I'd like it if you can have a look at<br>
> the code and suggest how to parallelize it.<br><br></div>OpenMP is one way to parallelize things but it's something that was<br>designed to work deeply in the algorithm code, e.g. by parallelizing<br>single loops etc. For your task I'd suspect that a more higher level<br>
approach is better suitable because you simply can run the algorithm as<br>it is on several faces in parallel. This is normally done with a<br>task-based approach. You first create list of tasks, which would be one<br>face to scan here, and then give these tasks to an executor. I don't<br>
know if Qt provides a thread pool, but this would be the most classical<br>versions to create such a service. You have a synchronized queue of<br>tasks that is filled from the one end with new faces to recognize and<br>that is dispatched by the executor to several parallel working threads<br>
that perform the recognition algorithm.<br><br>For C++ there is one especially notable library that implements a task<br>pattern in a very versatile way: Intel's Threading Building Blocks. We<br>don't use them yet in digikam but I think this is definitely the way to<br>
go if there is nothing similar in Qt.<br><br>The main statement should be: don't manage threads on your own whenever<br>you can. Instead focus on tasks that can be executed in parallel. If you<br>want to improve a single algorithm, then OpenMP would be a solution but<br>
not for these high-level tasks. Also, Intel's TBB include a solution to<br>parallelize loops in a way that's more or less equal to OpenMP.<br><br>If you want to have more insight on task-based approaches you can have a<br>
look at the java.util.concurrent package from standard platform.<br>Executor and ExecutorService etc. are a very well designed implementation.<br>"<br></span><br></div><div>Also, I'm not very knowledgeable about TBB or even OpenMP for that matter.<br>
So I've started the thread so that Alex and Marcel can join in...
</div><div><br></div><div>Aditya</div>