Thanks for the quick reply. I knew it had to be something with the memory, I just was unable to pin it down. It makes a lot more sense now. With that being said, I tried implementing what you suggested, but the program is aborting when it hits the recv. It aborts even when I don't dynamically allocate the memory. I've looked over my code, but I'm not sure where I'm going wrong. If you could look over my code again it would be a great help. I'm sure it is just something simple that I missed.
thanks
Code:
#include<stdio.h>
#include<stdlib.h>
#include "mpi.h"
//Structure I'm Trying to Send
struct ListNode
{
double a[1];
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, 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
myNode = malloc(sizeof(myNode) + sizeof(double)*(5-1)); //Dynamically create array
if(nodeRank == 0)
{
//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_Get_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);
}
Also, should the malloc statement be contain "sizeof(myNode)" or "sizeof(*myNode)"?
Thanks again
EDIT: moved malloc statement out of if statement. The code will now run but with interesting out put. Here is what I get.
Code:
Noderank = 5
a[0] = 1.1000000
a[1] = 4.5000000
a[2] = 3.2999999
a[3] = 4.4000000
a[5] = 5.5000000
b = 4.5000000
n = 6