Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vector Usage in MPI(C++)

I am new to MPI programming,stiil learning , i was successful till creating the Derived data-types by defining the structures . Now i want to include Vector in my structure and want to send the data across the Process. for ex:

struct Structure{

//Constructor 
Structure(): X(nodes),mass(nodes),ac(nodes) {

//code to calculate the mass and accelerations
}
//Destructor
Structure() {}

//Variables
double radius;
double volume;
vector<double> mass;
vector<double> area;

//and some other variables

//Methods to calculate some physical properties

Now using MPI i want to sent the data in the structure across the processes. Is it possible for me to create the MPI_type_struct vectors included and send the data?

I tried reading through forums, but i am not able to get the clear picture from the responses given there. Hope i would be able to get a clear idea or approach to send the data

PS: i can send the data individually , but its an overhead of sending the data using may MPI_Send/Recieve if we consider the domain very large(say 10000*10000)

like image 499
lsk1985 Avatar asked Mar 30 '10 15:03

lsk1985


People also ask

Why do we use vector in C?

In C++, vectors are used to store elements of similar data types. However, unlike arrays, the size of a vector can grow dynamically. That is, we can change the size of the vector during the execution of a program as per our requirements. Vectors are part of the C++ Standard Template Library.

How can we use vector in C?

In this tutorial, we will be discussing a program to understand how vectors work in C/C++. A vector data structure is an enhancement over the standard arrays. Unlike arrays, which have their size fixed when they are defined; vectors can be resized easily according to the requirement of the user.

What does a vector do in CPP?

Vectors in C++ are sequence containers representing arrays that can change their size during runtime. They use contiguous storage locations for their elements just as efficiently as in arrays, which means that their elements can also be accessed using offsets on regular pointers to its elements.

Should I use std::vector?

If you need a "dynamic" array, then std::vector is the natural solution. It should in general be the default container for everything. But if you want a statically sized array created at time of compilation (like a C-style array is) but wrapped in a nice C++ object then std::array might be a better choice.


1 Answers

Defining structures in MPI is a pain. I think an easier approach would be to use the fact that STL vectors are guaranteed to have contiguously allocated memory. That is, you can treat them like C arrays by getting a pointer to the first element.

std::vector<float> data;
// ... add elements to your vector ...
MPI_Send(&data.front(), data.size(), MPI_FLOAT, 0, 1, MPI_COMM_WORLD);
like image 64
E.M. Avatar answered Oct 21 '22 13:10

E.M.