I have a couple of questions. First, when using malloc to dynamically create an array, the new array is still contiguous in memory correct?
Second, I'm trying to send a C structure in MPI in which one of the members is an array. If I specify the array size at compile time, everything works wonderful. The problem comes when I dynamically create the array, the information does not get passed. Here's an example of some simplified code I'm working on.
The program still runs with no errors, but Node 5 never gets the data from the array, but it gets all the other members of the structure just fine.Code:#include<stdio.h>
#include<stdlib.h>
#include "mpi.h"
//Structure I'm Trying to Send
struct ListNode
{
double* a;
double b;
int n;
};
typedef struct ListNode Node;
//Function to build derived MPI datatype
void Build_derived_type(Node* myNode, MPI_Datatype* mesg_mpi_t_ptr);
int main(int argc, char* argv[])
{
int nodeRank=0, numNodes,i;
MPI_Datatype MPI_NODE; //New Datatype to send structure
MPI_Status status;
Node myNode;
MPI_Init(&argc,&argv); //Initializes MPI
MPI_Comm_size(MPI_COMM_WORLD,&numNodes); //find out how many nodes there are total
MPI_Comm_rank(MPI_COMM_WORLD,&nodeRank); //Finds out rank of this node
if(nodeRank == 0)
{
myNode.a = malloc(sizeof(double)*5); //Dynamically create array
//Fill array
myNode.a[0] = 1.1;
myNode.a[1] = 2.2;
myNode.a[2] = 3.3;
myNode.a[3] = 4.4;
myNode.a[4] = 5.5;
myNode.b = 4.5;
myNode.n = 6;
}
Build_derived_type(&myNode, &MPI_NODE); //Builds derived type
//Send structure from node 0 to node 5
if(nodeRank == 0)
{
MPI_Send(&myNode,1,MPI_NODE,5,1,MPI_COMM_WORLD);
}
//Node 5 recieves structure and displays data
if(nodeRank == 5)
{
MPI_Recv(&myNode,1,MPI_NODE,0,1,MPI_COMM_WORLD,&status);
printf("Noderank = %d\n",nodeRank);
for(i=0;i<5;i++)
printf("a[%d] = %f\n",i,myNode.a[i]);
printf("b = %f\nn = %d\n",myNode.b,myNode.n);
}
MPI_Finalize();
return 0;
}
void Build_derived_type(Node* myNode, MPI_Datatype* MPI_NODE_ptr)
{
int block_lengths[3];
MPI_Aint displacements[3];
MPI_Datatype typelists[3];
MPI_Aint start_address;
MPI_Aint address;
//fill block lengths
block_lengths[0] = 5;
block_lengths[1] = block_lengths[2] = 1;
//Fill Typelists
typelists[0] = MPI_DOUBLE;
typelists[1] = MPI_DOUBLE;
typelists[2] = MPI_INT;
//Calculate Displacements
displacements[0] = 0;
MPI_Get_address(&((*myNode).a),&start_address);
MPI_Get_address(&((*myNode).b),&address);
displacements[1] = address - start_address;
MPI_Address(&((*myNode).n),&address);
displacements[2] = address - start_address;
//Create and Commit new mpi type
MPI_Type_create_struct(3,block_lengths,displacements,typelists,MPI_NODE_ptr);
MPI_Type_commit(MPI_NODE_ptr);
}
any help appreciated