Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.
Announcements
This community is designed for sharing of public information. Please do not share Intel or third-party confidential information here.
2421 Discussions

compilation error when invoking parallel_for()

gajendra_g_
Beginner
227 Views

I am trying to create a 1D array and emulate a 2D matrix multiplication and parallelize it using the tbb. In my previous post I got an error that was due  missing the namespace tbb for the tbb constructs.

However  I further extened my code to create a parallelMatrixMultiply that calls the MatrixMultiply2D and i get a half page terminal error message a part of which is like this:

usr/include/tbb/parallel_for.h:102:37: error: no match for call to ‘(const MatrixMultiply2D) (tbb::blocked_range2d<long unsigned int>&)’
         void run_body( Range &r ) { my_body( r ); }
                                     ^
./tbbMatrix.cpp:36:14: note: candidate: void MatrixMultiply2D::operator()(const tbb::blocked_range2d<long unsigned int>&) <near match>
         void operator()(const blocked_range2d<size_t>& rnge )
              ^
./tbbMatrix.cpp:36:14: note:   passing ‘const MatrixMultiply2D*’ as ‘this’ argument discards qualifiers

 

Here's my code:

#include <iostream>
#include "tbb/tbb.h"
#include "tbb/parallel_for.h"
#include "tbb/blocked_range2d.h"

using namespace tbb;

const size_t m_rows = 256;
const size_t m_columns = 256;
const size_t m_k = 256;

class MatrixMultiply2D
{
    private:
        double *m_A;
        double *m_B;
        double *m_C;

    public:
        void operator()(const blocked_range2d<size_t>& rnge )
        {
            double *usrA = m_A;
            double *usrB = m_B;
            double *usrC = m_C;

            for(size_t row=rnge.rows().begin(); row<rnge.rows().end(); ++row)
            {
                for(size_t col=rnge.cols().begin(); col <rnge.cols().end(); ++col)
                {
                    double sum = 0.0;
                
                     for(size_t k=0; k<m_k; ++k)
                     {
                        usrC[row*(rnge.cols().end()-rnge.cols().begin()) + col]  += usrA[row*m_k + k]*usrB[k*(rnge.cols().end()-rnge.cols().begin()) + col];     
                    }
                }
            }
        }

        MatrixMultiply2D(double *A, double *B, double *C):
        m_A(A), m_B(B), m_C(C)
        {       }
};

// this line spawns error, if commented the code compiles fine!
void parallelMatrixMultiply(double *A, double *B, double *C, size_t rows, size_t columns, size_t k)
{
    parallel_for( blocked_range2d<size_t>(0, rows, 16, 0, columns, 32), MatrixMultiply2D(A,B,C) );
}

int main(int argc, char **argv)
{
    double *A = new double[m_rows*m_k];
    double *B = new double[m_k*m_columns];
    double *C = new double[m_rows*m_columns];

  


/***************************************************************************/
/********************* CALLING TBB PARALLEL MATRIX *************************/

    parallelMatrixMultiply(A, B, C, m_rows, m_columns, m_k);

  
    
/********************* END OF PARALLEL MATRIX *********************************/
/******************************************************************************/

    delete[] A;
    delete[] B;
    delete[] C;
   
    return 0;
}

 

Any suggestions is greatly appreciated.

0 Kudos
1 Solution
Vladimir_P_Intel2
227 Views

Hello, 

documentation for parallel_for requires operator() to be the const member function: https://software.intel.com/en-us/node/506153.

So change line 20 from "void operator()(const blocked_range2d<size_t>& rnge )" to "void operator()(const blocked_range2d<size_t>& rnge ) const"

--Vladimir

View solution in original post

1 Reply
Vladimir_P_Intel2
228 Views

Hello, 

documentation for parallel_for requires operator() to be the const member function: https://software.intel.com/en-us/node/506153.

So change line 20 from "void operator()(const blocked_range2d<size_t>& rnge )" to "void operator()(const blocked_range2d<size_t>& rnge ) const"

--Vladimir

Reply