Перші спроби багатопотокового програмування Qt5 + OpenMP +MS Visual Studio
Продовжуючи вивчати багатопотоковість з’ясував що бібліотека libraw має підтримку багатопотокового програмування - OpenMP.
Для використання потрібно дозволити компілятору використовувати OpenMP у випадку MS Visual Studio це вмикається так : OpenMP in Visual C++, /openmp (Enable OpenMP 2.0 Support)
Після чого директиви #pragma omp починають працювати. Таким чином я перекомпілював libraw.dll з підтримкою omp.
Для вмикання OpenMP у середовищі Qt5 для компілятора MS Visual Studio
у файлі проекту (.pro) потрібно додати:
QMAKE_CXXFLAGS += -openmp
Та пере компілювати проект.
Для тестування написав цей код:
#pragma omp parallel
{
qDebug("OpenMP parallel, thread=%d of %d",omp_get_thread_num(),omp_get_max_threads());
}
#pragma omp parallel for
for (int i=0;i<16;i++){
qDebug("1. OpenMP i=%d, thread=%d of %d",i,omp_get_thread_num(),omp_get_max_threads());
qDebug("2. OpenMP i=%d, thread=%d of %d",i,omp_get_thread_num(),omp_get_max_threads());
qDebug("3. OpenMP i=%d, thread=%d of %d",i,omp_get_thread_num(),omp_get_max_threads());
}
Отримав результат автоматичного розподілення операцій циклу у по різним потокам. Результат:
OpenMP parallel, thread=0 of 8
OpenMP parallel, thread=5 of 8
OpenMP parallel, thread=1 of 8
OpenMP parallel, thread=6 of 8
OpenMP parallel, thread=3 of 8
OpenMP parallel, thread=4 of 8
OpenMP parallel, thread=2 of 8
OpenMP parallel, thread=7 of 8
1. OpenMP i=1, thread=0 of 8
1. OpenMP i=0, thread=7 of 8
1. OpenMP i=2, thread=2 of 8
1. OpenMP i=3, thread=4 of 8
1. OpenMP i=4, thread=1 of 8
2. OpenMP i=1, thread=0 of 8
1. OpenMP i=6, thread=6 of 8
1. OpenMP i=7, thread=3 of 8
2. OpenMP i=0, thread=7 of 8
1. OpenMP i=5, thread=5 of 8
2. OpenMP i=2, thread=2 of 8
2. OpenMP i=3, thread=4 of 8
2. OpenMP i=4, thread=1 of 8
3. OpenMP i=1, thread=0 of 8
2. OpenMP i=6, thread=6 of 8
2. OpenMP i=7, thread=3 of 8
3. OpenMP i=0, thread=7 of 8
2. OpenMP i=5, thread=5 of 8
3. OpenMP i=2, thread=2 of 8
3. OpenMP i=3, thread=4 of 8
3. OpenMP i=4, thread=1 of 8
1. OpenMP i=8, thread=0 of 8
3. OpenMP i=6, thread=6 of 8
3. OpenMP i=7, thread=3 of 8
1. OpenMP i=9, thread=7 of 8
3. OpenMP i=5, thread=5 of 8
1. OpenMP i=10, thread=2 of 8
1. OpenMP i=11, thread=4 of 8
1. OpenMP i=12, thread=1 of 8
2. OpenMP i=8, thread=0 of 8
1. OpenMP i=13, thread=6 of 8
1. OpenMP i=14, thread=3 of 8
2. OpenMP i=9, thread=7 of 8
1. OpenMP i=15, thread=5 of 8
2. OpenMP i=10, thread=2 of 8
2. OpenMP i=11, thread=4 of 8
2. OpenMP i=12, thread=1 of 8
3. OpenMP i=8, thread=0 of 8
2. OpenMP i=13, thread=6 of 8
2. OpenMP i=14, thread=3 of 8
3. OpenMP i=9, thread=7 of 8
2. OpenMP i=15, thread=5 of 8
3. OpenMP i=10, thread=2 of 8
3. OpenMP i=11, thread=4 of 8
3. OpenMP i=12, thread=1 of 8
3. OpenMP i=13, thread=6 of 8
3. OpenMP i=14, thread=3 of 8
3. OpenMP i=15, thread=5 of 8
