Матрично-векторное умножение на MPI - ERROR Скомпилированный код - PullRequest
0 голосов
/ 18 апреля 2020

Мне нужна помощь для устранения ошибки в следующем коде:

#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

1 Ответ

0 голосов
/ 18 апреля 2020

Я думаю, что проблема в строке: 59

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

MPI_Recv принимает указатель на буфер (первый аргумент), где вы собираются получать и хранить входящие данные. В этом случае это может быть некоторая переменная, которую вы можете определить внутри для l oop, например:

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
        }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...