Хранение двумерного массива в векторе

В программе я хочу хранить матрицу как одномерный массив, но если при выполнении задавать размерность более 500 элементов, вылетает с ошибкой: ended prematurely and may have crashed. exit code 0xc0000005. Подскажите, пожалуйста, как быть, если нужно указывать большую размерность, например 10000?

#include <iostream>
#include <algorithm>
#include <ctime>
#include "mpi.h"

#define INF 1001
#define MASTER 0

using namespace std;

int main(int argc, char** argv)
{
    srand(time(NULL));

    MPI_Init(&argc, &argv);

    int rank, size;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    int countOfVertex;
    MPI_Request request;
    MPI_Status status;

    if (rank == MASTER)
    {
        cout << endl;
        cout << "Input value of countOfVertex -> "; cin >> countOfVertex;
        int *matrix;
        matrix = (int *)malloc(sizeof(int) * (countOfVertex * countOfVertex));

        for (int i = 0; i < countOfVertex; i++)
        {
            for (int j = 0; j < countOfVertex; j++)
            {
                matrix[i*countOfVertex + j] = rand() % 1002;

                if (i == j)
                {
                    matrix[i*countOfVertex + j] = 0;
                }
                else if ((i != j) && (matrix[i*countOfVertex + j] == 0))
                {
                    matrix[i*countOfVertex + j] = 1 + rand() % 1002;
                }
            }
        }

        cout << "Root process. Count of verts sent to other processes \n" << endl;
        MPI_Bcast(&countOfVertex, 1, MPI_INT, MASTER, MPI_COMM_WORLD);
        cout << "Root process. The first matrix: (print new matrix) \n" << endl;
        int workSize = size - 1; 
        int *countOfRows = (int*)malloc(sizeof(int) * workSize);
        for (int i = 0; i < workSize - 1; i++)
        {
            countOfRows[i] = countOfVertex / workSize;
        }
        if (countOfVertex % workSize == 0)
        {
            countOfRows[workSize - 1] = countOfVertex / workSize;
        }
        else
        {
            countOfRows[workSize - 1] = countOfVertex - ((countOfVertex / workSize) * (workSize - 1));
        }

        cout << "Root process. Count of rows sent to other processes." << endl;

        for (int i = 1; i < size; i++)
        {
            MPI_Send(&countOfRows[i - 1], 1, MPI_INT, i, 0, MPI_COMM_WORLD);
        }
        cout << "Root process. MAIN START." << endl;
        double start_time = MPI_Wtime();
        for (int k = 0; k < countOfVertex; k++)
        {
            for (int p = 1; p < size; p++)
            {
                MPI_Isend(&matrix[k*countOfVertex], countOfVertex, MPI_INT, p, 0, MPI_COMM_WORLD, &request);
            }
            int num = 0;//number of row
            for (int i = 0; i < workSize; i++)
            {
                for (int j = 0; j < countOfRows[i]; j++)
                {
                    MPI_Isend(&matrix[num*countOfVertex], countOfVertex, MPI_INT, i + 1, 0, MPI_COMM_WORLD, &request);
                    num++;
                }
            }
            num = 0; // number of row
            for (int i = 0; i < workSize; i++)
            {
                for (int j = 0; j < countOfRows[i]; j++)
                {
                    MPI_Recv(&matrix[num*countOfVertex], countOfVertex, MPI_INT, i + 1, 0, MPI_COMM_WORLD, &status);
                    num++;
                }
            }
        }
        double run_time = MPI_Wtime() - start_time;
        cout << "Root process. Show final matrix: (here will be final matrix) \n" << endl;
        //showMatrix(matrix, countOfVertex);
        cout << "RUNTIME -> " << run_time * 1000 << " ms ";
        cout << endl;
        free(matrix);
    }
    else
    {
        MPI_Bcast(&countOfVertex, 1, MPI_INT, MASTER, MPI_COMM_WORLD);
        int countOfRows_;
        MPI_Recv(&countOfRows_, 1, MPI_INT, MASTER, 0, MPI_COMM_WORLD, &status);
        cout << rank << " : " << countOfRows_ << endl;

        int *kRow = (int *)malloc(sizeof(int) * countOfVertex);
        int *rows = (int *)malloc(sizeof(int) * (countOfRows_ * countOfVertex));

        for (int k = 0; k < countOfVertex; k++)
        {
            MPI_Recv(kRow, countOfVertex, MPI_INT, MASTER, 0, MPI_COMM_WORLD, &status);

            for (int i = 0; i < countOfRows_; i++)
            {
                MPI_Recv(&rows[i*countOfVertex], countOfVertex, MPI_INT, MASTER, 0, MPI_COMM_WORLD, &status);
            }
            for (int i = 0; i < countOfRows_; i++)
            {
                for (int j = 0; j < countOfVertex; j++)
                {
                    rows[i*countOfVertex + j] = min(rows[i*countOfVertex + j], rows[i*countOfVertex + k] + kRow[j]);
                }
            }
            for (int i = 0; i < countOfRows_; i++)
            {
                MPI_Send(&rows[i*countOfVertex], countOfVertex, MPI_INT, MASTER, 0, MPI_COMM_WORLD);
            }
        }
        free(kRow);
        free(rows);
    }

    MPI_Finalize();
    return 0;
}

Если задаю динамически int *matrix = new int[countOfVertex*countOfVertex], ничего не меняется.


Ответы (0 шт):