mercredi 29 mars 2017

Strange behavior multithreading openMP visual studio 2015

I encountered weird behavior using visual studio 2015 due to multithreading, code does not spawn threads, even when explicete ask to.

    std::vector<std::thread> Pool;
    Pool.reserve(std::thread::hardware_concurrency());

    int max = ImageB_spline.rows() % std::thread::hardware_concurrency();
    max = ImageB_spline.rows() - max;
    int step = max / std::thread::hardware_concurrency();

    for (int thread = 0; thread < std::thread::hardware_concurrency() - 1; ++thread) {
        Pool.push_back(std::thread([](Eigen::Array<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor >& _ImageB_spline, Eigen::Matrix<std::complex<double>, 1, Eigen::Dynamic, Eigen::RowMajor> _X_Freq_kernel, int start, int stop) {
            //work
        }, std::ref(ImageB_spline), X_Freq_kernel, step *thread, step *thread + 1));
    }

    Pool.push_back(std::thread([](Eigen::Array<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor >& _ImageB_spline, Eigen::Matrix<std::complex<double>, 1, Eigen::Dynamic, Eigen::RowMajor> _X_Freq_kernel, int start, int stop) {
        //work
    }, std::ref(ImageB_spline), X_Freq_kernel, std::thread::hardware_concurrency() - 1, ImageB_spline.rows()));

    for (int iter = 0; iter < Pool.size(); ++iter) {
        Pool[iter].join();
    }

it create 8 threads with different id but it isn't executed int parallel, same as using openMP ( in language settings I enable openMP, and in command line flag is present). Still no parallelism.

#pragma openmp parallel for firstprivate(fft,X_Freq_kernel,X_Freq_Row_Conv) num_threads(4)
    for (int iI = 0; iI < ImageB_spline.rows(); ++iI)
    {
        //work
    }

I resolve to use

        std::thread F1([](Eigen::Array<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor >& _ImageB_spline, Eigen::Matrix<std::complex<double>, 1, Eigen::Dynamic, Eigen::RowMajor> _X_Freq_kernel, int start, int stop) {
        //work
    }, std::ref(ImageB_spline), X_Freq_kernel, 0, 999);

    std::thread F2([](Eigen::Array<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor >& _ImageB_spline, Eigen::Matrix<std::complex<double>, 1, Eigen::Dynamic, Eigen::RowMajor> _X_Freq_kernel, int start, int stop) {
        //work
    }, std::ref(ImageB_spline), X_Freq_kernel, 1000, 1999);


    std::thread F3([](Eigen::Array<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor >& _ImageB_spline, Eigen::Matrix<std::complex<double>, 1, Eigen::Dynamic, Eigen::RowMajor> _X_Freq_kernel, int start, int stop) {
        //work
    }, std::ref(ImageB_spline), X_Freq_kernel, 2000, 2500);


    F1.join();
    F2.join();
    F3.join();

And now its executing in parallel, and i don't see reason why...

Aucun commentaire:

Enregistrer un commentaire