title: C-MPI Send created typedef struct with array of chars
body: <p>Hello I have a file containing that kind of data lines:</p>
  
  <pre><code>AsfAGHM5om  00000000000000000000000000000000  0000222200002222000022220000222200002222000000001111
  </code></pre>
  
  <p>I want to read this kind of data and send them over using C and MPI. So I've reached the following C code:</p>
  
  <pre><code>    #include &lt;mpi.h&gt;
      #include &lt;stdio.h&gt;
      #include &lt;stdlib.h&gt;
      #include &lt;string.h&gt;
      #include &lt;stddef.h&gt; // used for offsetof
      typedef struct tuple_str{
          char *key;
          char *index;
          char *value;
      } tuple;
  
      int main(int argc, char** argv) {
  
          // Initialize the MPI environment
          MPI_Init(&amp;argc, &amp;argv);
  
          // Initialize file pointer
          FILE *fp = fopen("tuples","r");
  
          // define original structure that stores file and temp used by each process
          tuple A[10000],B[10000];
          // mpi structure name
          MPI_Datatype mpi_tuples_str;
          // number of structure members
          const int nitems = 3;
          // array of structure member sizes
          int blocklengths[3];
          blocklengths[0] = sizeof(A-&gt;key);
          blocklengths[1] = sizeof(A-&gt;index);
          blocklengths[2] = sizeof(A-&gt;value);
          // structure member types
          MPI_Datatype types[3] = {MPI_CHAR,MPI_CHAR,MPI_CHAR};
          // status
          MPI_Status status;
          // offset of structure members
          MPI_Aint offsets[3];
          offsets[0] = offsetof(tuple,key);
          offsets[1] = offsetof(tuple,index);
          offsets[2] = offsetof(tuple,value);
  
          // create mpi struct
          MPI_Type_create_struct(nitems,blocklengths, offsets, types, &amp;mpi_tuples_str);
          MPI_Type_commit(&amp;mpi_tuples_str);
  
          // Get the number of processes
          int size;
          MPI_Comm_size(MPI_COMM_WORLD, &amp;size);
  
          // Get the rank of the process
          int my_rank;
          MPI_Comm_rank(MPI_COMM_WORLD, &amp;my_rank);
  
          int index = 0;
          int i;
          int local_A_size = (10000%size == 0) ? 10000/size : 0;
  
          if ( my_rank == 0){
            char text[10000];
            char *p;
            p=strtok(NULL," ");
            // node0 reads file form hard drive and saves file to struct
            while( fgets(text,10000,fp)!=NULL){
              p = strtok (text," ");
              char *temp[3];
              temp[0]=p;
              A[index].key=temp[0];
              p = strtok (NULL, " ");
              temp[1] = p;
              A[index].index=temp[1];
              p = strtok (NULL, " ");
              temp[2] = p;
              A[index].value=temp[2];
              // printf("%s ",A[index].key);
              // printf("%s ",A[index].index);
              // printf("%s\n",A[index].value);
              index++;
            }
            fclose(fp);
          }
  
          if ( local_A_size != 0){
            if (my_rank == 0) {
              printf("File saved to memory of process %d!\n",my_rank);
              printf("Process %d sending struct data to others...\n",my_rank);
            }
  
            // send struct to all processes
            MPI_Scatter(&amp;A,local_A_size,mpi_tuples_str,B,local_A_size,mpi_tuples_str,0,MPI_COMM_WORLD);
            // MPI_Bcast(&amp;A,index,mpi_tuples_str,0,MPI_COMM_WORLD);
  
            for(i=0;i&lt;=local_A_size;i++)
             printf("I'm process %d and my result is: %s\n",my_rank,B[i].key);
  
            if (my_rank == 0) printf("Data sent from process %d to others...\n",my_rank);
          }
          else
          {
            if (my_rank == 0) printf("Number of processes must be an exact divisor of %d, %d in not %ds divisor\n",index,size,index);
          }
            // free memory used by mpi_tuples_str
            MPI_Type_free(&amp;mpi_tuples_str);
            // Finalize
            MPI_Finalize();
          return 0;
      }
  </code></pre>
  
  <p>So the problem here is, as far as I can understand_ the creation and allocation of memory of my struct at first and the packing and sending of it at second.
  As you can see I've tried both <code>MPI_Scatter</code> &amp; <code>MPI_Bcast</code> but none of them helped me.
  The result is that, as it supposed to, process 0 that reads the file has data but all others not. Also I'm getting this weird message of</p>
  
  <pre><code>=   BAD TERMINATION OF ONE OF YOUR APPLICATION PROCESSES
  =   EXIT CODE: 11
  =   CLEANING UP REMAINING PROCESSES
  =   YOU CAN IGNORE THE BELOW CLEANUP MESSAGES
  </code></pre>
  
  <p>I'll be so grateful if someone can enlighten me!!</p>
  
  <p>Alright I 've changed my code to the following:</p>
  
  <pre><code>`#include &lt;mpi.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stddef.h&gt; // used for offsetof
  typedef struct tuple_str{
      char key[10];
      char index[12];
      char value[52];
  } tuple;
  
  int main(int argc, char** argv) {
  
      // Initialize the MPI environment
      MPI_Init(NULL, NULL);
  
      // Initialize file pointer
      FILE *fp = fopen("tuples_mini","r");
  
      // define original structure that stores file and temp used by each process
      tuple A[10000],B[10000];
      // mpi structure name
      MPI_Datatype mpi_tuples_str;
      // number of structure members
      const int nitems = 3;
      // array of structure member sizes
      int blocklengths[3];
      blocklengths[0] = sizeof(10);
      blocklengths[1] = sizeof(12);
      blocklengths[2] = sizeof(52);
      // structure member types
      MPI_Datatype types[3] = {MPI_CHAR,MPI_CHAR,MPI_CHAR};
      // status
      MPI_Status status;
      // offset of structure members
      MPI_Aint offsets[3];
      offsets[0] = offsetof(tuple,key);
      offsets[1] = offsetof(tuple,index);
      offsets[2] = offsetof(tuple,value);
  
      // create mpi struct
      MPI_Type_create_struct(nitems,blocklengths, offsets, types, &amp;mpi_tuples_str);
      MPI_Type_commit(&amp;mpi_tuples_str);
  
      // Get the number of processes
      int size;
      MPI_Comm_size(MPI_COMM_WORLD, &amp;size);
  
      // Get the rank of the process
      int my_rank;
      MPI_Comm_rank(MPI_COMM_WORLD, &amp;my_rank);
  
      int index = 0;
      int i;
      int local_A_size = (10000%size == 0) ? 10000/size : 0;
      char *tmp[10000],*b[10000];
  
      if ( my_rank == 0){
        char text[10000];
        char *p;
        p=strtok(NULL," ");
        // node0 reads file form hard drive and saves file to struct
        while( fgets(text,10000,fp)!=NULL){
          p = strtok (text," ");
          char *temp[3];
          temp[0]=p;
          strcpy(A[index].key,temp[0]);
          p = strtok (NULL, " ");
          temp[1] = p;
          strcpy(A[index].index,temp[1]);
          p = strtok (NULL, " ");
          temp[2] = p;
          strcpy(A[index].value,temp[2]);
          printf("%s ",A[index].key);
          printf("%s ",A[index].index);
          printf("%s\n",A[index].value);
          index++;
        }
        fclose(fp);
      }
  
      if ( local_A_size != 0){
        if (my_rank == 0) {
          printf("File saved to memory of process %d!\n",my_rank);
          printf("Process %d sending struct data to others...\n",my_rank);
        }
  
        // send struct to all processes
        MPI_Scatter(&amp;A,index,mpi_tuples_str,B,index,mpi_tuples_str,0,MPI_COMM_WORLD);
        // MPI_Bcast(&amp;tmp,index,MPI_CHAR,0,MPI_COMM_WORLD);
  
        for(i=0;i&lt;=local_A_size;i++){
            // MPI_Recv(&amp;tmp,index,MPI_CHAR,0,10,MPI_COMM_WORLD,&amp;status);
            printf("I'm process %d and my result is: %s\n",my_rank,B[i].key);
        }
  
        if (my_rank == 0) printf("Data sent from process %d to others...\n",my_rank);
      }
      else
      {
        if (my_rank == 0) printf("Number of processes must be an exact divisor of %d, %d in not %ds divisor\n",index,size,index);
      }
        // free memory used by mpi_tuples_str
        MPI_Type_free(&amp;mpi_tuples_str);
        // Finalize
        MPI_Finalize();
      return 0;
  }`
  </code></pre>
  
  <p>but that lead me to new error:</p>
  
  <pre><code>    ==============================================================================    =====
  =   BAD TERMINATION OF ONE OF YOUR APPLICATION PROCESSES
  =   EXIT CODE: 6
  =   CLEANING UP REMAINING PROCESSES
  =   YOU CAN IGNORE THE BELOW CLEANUP MESSAGES
  ==============================================================================    =====
  YOUR APPLICATION TERMINATED WITH THE EXIT STRING: Aborted (signal 6)
  This typically refers to a problem with your application.
  Please see the FAQ page for debugging suggestions
  </code></pre>
  
  <p>after last suggestions!</p>
  
  <pre><code>#include &lt;mpi.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stddef.h&gt; // used for offsetof
  typedef struct tuple_str{
      char key[10];
      char index[12];
      char value[52];
  } tuple;
  int main(int argc, char** argv) {
    // Initialize the MPI environment
    MPI_Init(NULL, NULL);
  
    // Initialize file pointer
    FILE *fp = fopen("tuples_mini","r");
  
    // define original structure that stores file and temp used by each process
   tuple A[10000],B[10000];
   // mpi structure name
   MPI_Datatype mpi_tuples_str;
   // number of structure members
   const int nitems = 3;
   // array of structure member sizes
   int blocklengths[3];
   blocklengths[0] = 11;
   blocklengths[1] = 33;
   blocklengths[2] = 53;
   // structure member types
   MPI_Datatype types[3] = {MPI_CHAR,MPI_CHAR,MPI_CHAR};
   // status
   MPI_Status status;
   // offset of structure members
   MPI_Aint offsets[3];
   offsets[0] = offsetof(tuple,key);
   offsets[1] = offsetof(tuple,index);
   offsets[2] = offsetof(tuple,value);
  
   // create mpi struct
   MPI_Type_create_struct(nitems,blocklengths, offsets, types, &amp;mpi_tuples_str);
   MPI_Type_commit(&amp;mpi_tuples_str);
  
   // Get the number of processes
   int size;
   MPI_Comm_size(MPI_COMM_WORLD, &amp;size);
  
  // Get the rank of the process
  int my_rank;
  MPI_Comm_rank(MPI_COMM_WORLD, &amp;my_rank);
  
  int index = 0;
  int i;
  int local_A_size = (10000%size == 0) ? 10000/size : 0;
  char *tmp[10000],*b[10000];
  
  if ( my_rank == 0){
    char text[10000];
    char *p;
    // p=strtok(NULL," ");
    // node0 reads file form hard drive and saves file to struct
    while( fgets(text,10000,fp) != NULL &amp;&amp; fp != NULL){
      p = strtok (text," ");
      char *temp[3];
      temp[0]=p;
      strcpy(A[index].key,temp[0]);
      p = strtok (NULL, " ");
      temp[1] = p;
      strcpy(A[index].index,temp[1]);
      p = strtok (NULL, " ");
      temp[2] = p;
      strcpy(A[index].value,temp[2]);
      printf("%s ",A[index].key);
      printf("%s ",A[index].index);
      printf("%s\n",A[index].value);
      tmp[index] = temp[0];
      // printf("%s\n",tmp[index]);
      index++;
    }
    fclose(fp);
  }
  
  if ( local_A_size != 0){
    if (my_rank == 0) {
      printf("File saved to memory of process %d!\n",my_rank);
      printf("Process %d sending struct data to others...\n",my_rank);
      // MPI_Send(&amp;A,index,mpi_tuples_str,0,10,MPI_COMM_WORLD);
    }
  
    // send struct to all processes
       MPI_Scatter(&amp;A,index,mpi_tuples_str,B,index,mpi_tuples_str,0,MPI_COMM_WORLD);
    // MPI_Bcast(&amp;tmp,index,MPI_CHAR,0,MPI_COMM_WORLD);
    // MPI_Bcast(&amp;A,index,mpi_tuples_str,0,MPI_COMM_WORLD);
  
    for(i=0;i&lt;=local_A_size;i++){
        // MPI_Recv(&amp;tmp,index,MPI_CHAR,0,10,MPI_COMM_WORLD,&amp;status);
        // MPI_Recv(&amp;A,index,mpi_tuples_str,0,10,MPI_COMM_WORLD,&amp;status);
        printf("I'm process %d and my result is: %s\n",my_rank,B[i].key);
    }
  
    if (my_rank == 0) printf("Data sent from process %d to others...\n",my_rank);
  }
  else
  {
    if (my_rank == 0) printf("Number of processes must be an exact divisor of %d, %d in not %ds divisor\n",index,size,index);
  }
    // free memory used by mpi_tuples_str
    MPI_Type_free(&amp;mpi_tuples_str);
    // Finalize
    MPI_Finalize();
  return 0;
  }
  </code></pre>