简体   繁体   中英

open mp three for loops with reduction

i need to multiply two 10x10 matrices using open mp. I decided to split the rows of one matrice into groups of 3rows,3 rows and 4 rows. how do i fix this code for the first three rows ?

#pragma omg parallel for reduction(+:m[p][q])
        {
            for (p = 0; p < 3; p++)
                for (q = 0; q < 10; q++)
                    for (k = 0; k < 10; ++k)
                    {
                        m[p][q] += l[p][k] * o[k][q];
                    }
        }

For a start - don't split the matrix yourself, but let OpenMP take care of sharing the work in the loops, eg

#pragma omg parallel for
{
    for (p = 0; p < 10; p++)
        for (q = 0; q < 10; q++)
            for (k = 0; k < 10; ++k)
            {
                m[p][q] += l[p][k] * o[k][q];
            }
}

In this code there is no need for a reduction because all concurrent write operations happen to different elements of m . Even if you collapse(2) the first two loops, you are still fine in that regard.

That said, optimizing matrix multiplication is an immensely complex topic on modern hardware. Parallelizing it even more so. If you want to get performance, use a BLAS implementation that is optimized for your architecture. If you want to learn - I suggest you start with the serial implementation and then go on parallelizing it. There plenty of educational material available for either.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM