Discussion:
[OMPI users] Memory management and data structures with OpenMPI.
Pranav Sumanth
2017-05-22 23:02:01 UTC
Permalink
Greetings,

I need help understanding how I can better manage memory in my code. sizeof(x)=8 (file main.cpp) and this is always equal to the number of processes spawned. How do I ensure that sizeof(x) = 1 and that it is a 1D array of size num (from main.cpp).

I would like to import utils.h as a static library import. How best should I structure my code? I would also be very grateful if I am pointed to suitable references to be better aware of using ND arrays, vectors and data structures with OpenMPI.

Thanks,
Best Regards,
Pranav



My main.cpp is:

#include <iostream>
#include <utils.h>
#include <mpi.h>

using namespace std;

int main(int argc, const char * argv[]) {


MPI_Init(NULL, NULL);

float start = 0., end = 1.;
int num = 100;

double *x = linspace(start, end, num);


for(int i=0;i<num;i++){
cout << *(x + i) <<endl;
}


MPI_Finalize();


return 0;

}



My utils.h is:

#ifndef utils_h
#define utils_h


#endif /* utils_h */

#include <iostream>
#include <vector>
#include <cassert>
#include <mpi.h>

using namespace std;


double * linspace(double start_in, double end_in, unsigned long int num_in)
{
/* This function generates equally spaced elements and returns
an array with the results */


int size, rank;

double start = static_cast<double>(start_in);
double end = static_cast<double>(end_in);
unsigned long int num = static_cast<unsigned long int>(num_in);

double * linspaced;
linspaced = new double[num];

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

assert(num!=0);

if (num == 1)
{
*(linspaced) = start_in;
return linspaced;
}


double delta = (end - start) / (num - 1);


for(int i=rank; i < num - 1; i += size)
{

// cout << "i = " << i << endl;
*( linspaced + i) = start + delta * i;
}
linspaced[num-1] = end; // I want to ensure that start and end
// are exactly the same as the input


cout << "size-of-linspaced : " << sizeof(linspaced) << endl;
return linspaced;

}

Loading...