Thread: MPI send message

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    3

    Question MPI send message

    I am trying to send an array of character but when i receive it in the other processor i receive it with garbage !!! any suggestion?
    Code:
    #include "stdafx.h"
    #include <stdio.h>
    #include "mpi.h"
    
    int main (int argc, char *argv[])
    {  
    	MPI_Status s;
       int size, rank;
       char line [128]; 
       //char* str="hjfjh";
       static const char filename[] = "file.txt";
       MPI_Init (&argc, &argv);
       MPI_Comm_size (MPI_COMM_WORLD, &size);
       MPI_Comm_rank (MPI_COMM_WORLD, &rank);
       
       if (rank == 0) // Master process
       { 
     
       FILE *file = fopen ( filename, "r" );
       if ( file != NULL )
       {
         /* or other suitable maximum line size */
     
          while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
          {
             //fputs ( line, stdout ); /* write the line */
     
    		  MPI_Send ((void *)&line, 1, MPI_CHAR, 1, 0xACE5, MPI_COMM_WORLD);     
    
    	  }
          fclose ( file );
       }
       else
       {
          perror ( filename ); /* why didn't the file open? */
       }
      
       }
    
       else
       { 
    	    printf ("Receiving data . . .\n");
    		printf("\n\n line %s\n",line);
       
          {
    		  MPI_Recv (&line, 1, MPI_CHAR, 0, 0xACE5, MPI_COMM_WORLD, &s);
              printf ("[%d] sent %s\n", 0, line);
          }
       }
     
       MPI_Finalize();
       return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If my reading of the manual is correct, the 1 just means you're sending the first character, not the entire line?

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    yes i want to send any thing from the file without garbage !!!

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, let's see some data. Or better yet, I'll provide some you can test with. How's that work? What do you get if you use the following as your .txt file:
    Code:
    The times that try men's souls
    have come and gone and come back again
    in a whirligig of emotion, a maelstrom of temper
    stringing us out to dry
      And yet, and yet
    it is not the end of the road
    so much is still yet to be done
      And yet, and yet
    no decisions are set in stone for eternity
    or forever out of bounds
      And yet, and yet
    eternity is coming soon
    replacing all of our comfort
    replacing it all with fear
    out of touch with bedrock
    replacing it with quicksand
    If you can, try that and tell me what answer you get.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    thanks for your replay but i fix my error through your notation
    my solution is: i should set the length in send && receive to be 128 instead of 1
    to be like MPI_Send ((void *)&line, 128, MPI_CHAR, 1, 0xACE5, MPI_COMM_WORLD);

    in order to read the whole line !!
    thanks again

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    1
    Hi M. Samy
    my problem is how to send for each Task 3 or 4 works like :
    1- helloTimwhereareyou
    2- helloTimwhereyoulivethesedays
    3- SilaDodohelloTimwhereyoulivethesedays
    4- HihelloTimwhereyoulivethesedays
    as you without space at a same time
    then in each task add for each
    add to each word any char at end like '#'
    then in master recieve them back with new modification

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. User-defined message problem in MFC dialog-based app
    By jrohde in forum Windows Programming
    Replies: 1
    Last Post: 01-17-2010, 10:08 PM
  2. Send() not "sending" whole message
    By Martin_T in forum C Programming
    Replies: 9
    Last Post: 11-18-2009, 07:37 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Making a script language?
    By Blackroot in forum Game Programming
    Replies: 10
    Last Post: 02-16-2006, 02:22 AM
  5. Sending CChildView a Message :: MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 04-06-2002, 03:00 PM