OpenMp For-Loop并行化

时间:2017-01-04 22:38:47

标签: c++ c parallel-processing openmp parallel-for

我尝试使用OpenMP并行化以下代码。

#pragma omp parallel for collapse(2)
{
    for (int mNdx = 0; mNdx < M; ++mNdx)
    {
        for (int nNdx = mNdx; nNdx < N; ++nNdx)
        {
            for (int elemNdx = mNdx; elemNdx <= nNdx; ++elemNdx)
            {
                result[mNdx * N + nNdx] += matrixOne[mNdx * N + elemNdx] * matrixTwo[elemNdx * N + nNdx];
            }
        }
    }
}

但SpeedUp总是大约1.0(+ - 0.05)。我尝试了所有可能的调度(自动,动态,静态和引导),不同的块大小,没有崩溃和崩溃(2)。 运行时根本不会改变......

任何人都能解释我为什么或在哪里犯错?
可能是由于自动化的for循环编译器并行化?

提前感谢任何建议/提示!

更新

根据需要提供可验证的示例。

#include <omp.h>
#include <iostream>

void initMatrix(float* mat, const int M, const int N);
void initResMatrix(float* mat, const int M, const int N);

double matMulUpperTriangular_C(float* matrixOne, float* matrixTwo, float* result, const int M, const int N);
double matMulUpperTriangular_Omp(float* matrixOne, float* matrixTwo, float* result, const int M, const int N);

int main()
{
    const int M = 2048, N = 2048;
    float* matOne = (float*)malloc(M * N * sizeof(float));
    float* matTwo = (float*)malloc(M * N * sizeof(float));
    float* res = (float*)malloc(M * N * sizeof(float));

    initMatrix(matOne, M, N);
    initMatrix(matTwo, M, N);
    initResMatrix(res, M, N);

    double timeConsumption[2] = { 0.0, 0.0 };

    timeConsumption[0] = matMulUpperTriangular_C(matOne, matTwo, res, M, N);
    timeConsumption[1] = matMulUpperTriangular_Omp(matOne, matTwo, res, M, N);

    std::cout << "Runtime C:\t\t" << timeConsumption[0] << "s" << std::endl;
    std::cout << "Runtime Omp:\t\t" << timeConsumption[1] << "s";
    std::cout << " | SpeedUp: " << timeConsumption[0] / timeConsumption[1] << std::endl;

    system("PAUSE");
    return 0;
}

void initMatrix(float* mat, const int M, const int N)
{
    for (int mNdx = 0; mNdx < M; ++mNdx)
    {
        for (int nNdx = 0; nNdx < mNdx; ++nNdx)
        {
            mat[mNdx * N + nNdx] = 0;
        }
        for (int nNdx = mNdx; nNdx < N; ++nNdx)
        {
            mat[mNdx * N + nNdx] = ((mNdx + nNdx) % 5 + 1) * 0.1f;
        }
    }
}

void initResMatrix(float* mat, const int M, const int N)
{
    for (int mNdx = 0; mNdx < M; ++mNdx)
    {
        for (int nNdx = 0; nNdx < N; ++nNdx)
        {
            mat[mNdx * N + nNdx] = 0.0f;
        }
    }
}

double matMulUpperTriangular_C(float* matrixOne, float* matrixTwo, float* result, const int M, const int N)
{
    double startTime = omp_get_wtime();

    for (int mNdx = 0; mNdx < M; ++mNdx)
    {
        for (int nNdx = mNdx; nNdx < N; ++nNdx)
        {
            for (int elemNdx = mNdx; elemNdx <= nNdx; ++elemNdx)
            {
                result[mNdx * N + nNdx] += matrixOne[mNdx * N + elemNdx] * matrixTwo[elemNdx * N + nNdx];
            }
        }
    }

    double endTime = omp_get_wtime();

    return endTime - startTime;
}

double matMulUpperTriangular_Omp(float* matrixOne, float* matrixTwo, float* result, const int M, const int N)
{
    double startTime = omp_get_wtime();

    #pragma omp parallel for collapse(2)
    {
        for (int mNdx = 0; mNdx < M; ++mNdx)
        {
            for (int nNdx = mNdx; nNdx < N; ++nNdx)
            {
                for (int elemNdx = mNdx; elemNdx <= nNdx; ++elemNdx)
                {
                    result[mNdx * N + nNdx] += matrixOne[mNdx * N + elemNdx] * matrixTwo[elemNdx * N + nNdx];
                }
            }
        }
    }

    double endTime = omp_get_wtime();

    return endTime - startTime;
}

解决:

愚蠢的错误...... 已设置/ MP-Flag(使用多个进程构建)而不是/ openmp。 现在SpeedUp大概是2.0:)

很抱歉,谢谢大家的帮助!

0 个答案:

没有答案