简体   繁体   中英

How to dynamically allocate memory using MPI in C++ instead of C malloc/calloc?

I am trying to write a parallel code in C++ employing MPI, however I only know how to allocate memory using the C commands, as malloc/calloc. The aim is to work with the identity matrix and decompose it among the MPI processes.

The identity matrix is created on the local workspace and then sent from local to rank 0 to be printed.

The pieces of codes that I have tried are:

Allocating memory using C language:

  • calloc
// N is the matrix size (N x N)
int* A=( int* )calloc( local_N*N, sizeof(int) );
  • malloc
typedef int row[N];
row *mat;

/* Dividing the number of rows to each processor*/

int n = N / size;

mat = (row *) malloc(n * sizeof(row));

Allocating memory using C++ language:

 int**  matrix = new int *[row];

I succeed to run on C programming language, how ever I would like to rewrite the code for C++.

It is quite handy to have contiguous array working with MPI specially it is much easier to code with contiguous data eg to make derived data types . My recommendation is using vector and flattening your data:

const int N = 100;
const int M = 20;
const int size = 4;
int n = N / size;
std::vector<int> mat(n*M); // each process has a mat with length of n * M
for(uint32_t i = 0; i < n; ++i)
    for(uint32_t j = 0; j < M; ++j)
        mat.at(i * M + j) = j; // this is equivalent to mat[i][j] if mat was 2D

You can also use smart pointer s:

using ManagedInt = std::unique_ptr<int[]> ;
auto managedMat = std::unique_ptr<ManagedInt[]>(new ManagedInt[n]);
for (size_t i = 0; i < n; ++i)
    managedMat[i] = ManagedInt(new int[M]);

for(uint32_t i = 0; i < n; ++i)
    for(uint32_t j = 0; j < M; ++j)
       managedMat[i][j] = j;

The bad way is:

WARNING you are about to enter ***programmer land.

// allocate mat
int **mat = new int *[n];
for (int i = 0; i < n; i++) {
    mat[i] = new int [M];
}

// now you can use mat[i][j]

// delete mat
for (int i = 0; i < n; i++) {
    delete[] mat[i];
}
delete[] mat;

and the godbolt

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