【问题标题】:Matrix-Vector Multiplication on MPI - ERROR Compiled codeMPI 上的矩阵向量乘法 - 错误编译代码
【发布时间】:2020-08-01 04:09:09
【问题描述】:

我需要帮助来解决以下代码中的错误:

#include <iostream>
#include <mpi.h>
using namespace std;
//matrix in two dimension in memory!!
int main(int argc, char** argv)
{
    const int WIDTH = 100;
    const int HEIGHT = 100;
    int id, P;
    double tempValue = 0;
    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &P);
    MPI_Comm_rank(MPI_COMM_WORLD, &id);
    double A[WIDTH][HEIGHT];
    double x[HEIGHT], b[WIDTH];
    int upperBound, lowerBound = 0;
    // Master controls worksharing..
    if (id == 0)
    {
        // Init A & x
        for (int i = 0; i < WIDTH; i++)
            for (int j = 0; j < HEIGHT; j++)
                A[i][j] = 1;
        for (int j = 0; j < HEIGHT; j++)
            x[j] = 2;
        // Send to each node its portion of A to be processed
        int portionSize = WIDTH / P;
        for (int i = 0; i < P; i++)
        {
            lowerBound = i * portionSize;
            upperBound = (i + 1) * portionSize;
            // let the last node process the remainder
            if (i == (P - 1))
                upperBound += (HEIGHT - portionSize * P);
            if (i > 0)// Do not send to master node!!
            {
                // Send to node i the lower & upper bounds the A portion
                //and complete vector x
                MPI_Send(&lowerBound, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
                MPI_Send(&upperBound, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
                MPI_Send(&A[lowerBound][0], (upperBound - lowerBound) * HEIGHT,
                    MPI_DOUBLE, i, 0, MPI_COMM_WORLD);
                MPI_Send(&x[0], HEIGHT, MPI_DOUBLE, i, 0, MPI_COMM_WORLD);
            }
        }
        // master perform part of the job...
        for (int i = 0; i < portionSize; i++)
        {
            tempValue = 0;
            for (int j = 0; j < HEIGHT; j++)
                tempValue += A[i][j] * x[j];
            b[i] = tempValue;
        }
        //Get the results in order, each node would send their boundaries and data part
        for (int i = 1; i < P; i++)
        {
            MPI_Recv(&lowerBound, 1, MPI_INT, i, 0, MPI_COMM_WORLD, MPI_STATUSES_IGNORE);
            MPI_Recv(&upperBound, 1, MPI_INT, i, 0, MPI_COMM_WORLD, MPI_STATUSES_IGNORE);
            MPI_Recv(&P[lowerBound], (upperBound - lowerBound), MPI_DOUBLE, i, 0,
                MPI_COMM_WORLD, MPI_STATUSES_IGNORE);
        }
        // Print the first 2 values to check..
        cout << "b[0]=" << b[0] << " b[Width-1]=" << b[WIDTH - 1] << endl;
    }
    else // the rest of the workers do their parts
    {
        //Receive the inputs
        MPI_Recv(&lowerBound, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUSES_IGNORE);
        MPI_Recv(&upperBound, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUSES_IGNORE);
        MPI_Recv(&A[lowerBound][0], (upperBound - lowerBound) * WIDTH, MPI_DOUBLE, 0, 0,
            MPI_COMM_WORLD, MPI_STATUSES_IGNORE);
        MPI_Recv(&x, HEIGHT, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUSES_IGNORE);
        cout << "Node:" << id << " Received from:" << lowerBound << " to " << upperBound - 1
            << endl;
        double* result = new double[upperBound - lowerBound];
        //Do the job
        for (int i = lowerBound, resultCounter = 0; i < upperBound; i++, resultCounter++)
        {
            tempValue = 0;
            for (int j = 0; j < HEIGHT; j++)
                tempValue += A[i][j] * x[j];
            result[resultCounter] = tempValue;
        }
        //send the results
        MPI_Send(&lowerBound, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
        MPI_Send(&upperBound, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
        MPI_Send(&result[0], upperBound - lowerBound, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
        delete[] result;
    }
    MPI_Finalize();
    return 0;
}

当我在 Microsoft Visual Studio 2019 中编译代码时,我收到以下错误消息:

错误(活动)E0142 表达式必须具有指向对象类型的指针 ConsoleApplication9 C:\Users\m_swe\Desktop\Assignments\Assignments\PrjP2P\MatMPI\MatMPI\Source.cpp 59
错误 C2109 下标需要数组或指针类型 ConsoleApplication9 C:\Users\m_swe\Desktop\Assignments\Assignments\PrjP2P\MatMPI\MatMPI\Source.cpp 59

【问题讨论】:

    标签: c++ visual-studio mpi


    【解决方案1】:

    我认为问题出在 line: 59

           MPI_Recv(&P[lowerBound], (upperBound - lowerBound), MPI_DOUBLE, i, 0,
    

    MPI_Recv 接收一个指向缓冲区的指针(第一个参数),您将在其中接收和存储传入数据。在这种情况下,它可能位于您可以在 for 循环中定义的某个变量中,如下所示:

    int receivedValues[ WIDTH * HEIGHT ];
    for (int i = 1; i < P; i++)
            {
                MPI_Recv(&lowerBound, 1, MPI_INT, i, 0, MPI_COMM_WORLD, MPI_STATUSES_IGNORE);
                MPI_Recv(&upperBound, 1, MPI_INT, i, 0, MPI_COMM_WORLD, MPI_STATUSES_IGNORE);
                MPI_Recv(&receivedValues[0], (upperBound - lowerBound), MPI_DOUBLE, i, 0,
                    MPI_COMM_WORLD, MPI_STATUSES_IGNORE);
    
               // do your computation here with receivedValues
            }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-01-23
      • 2015-06-30
      • 2017-07-18
      • 2013-12-18
      • 2015-11-19
      • 1970-01-01
      • 2012-11-24
      • 2010-10-19
      • 2013-12-23
      相关资源
      最近更新 更多