[Beowulf] Stupid MPI programming question

Robert G. Brown rgb at phy.duke.edu
Wed Sep 27 20:44:10 PDT 2006


On Wed, 27 Sep 2006, Joe Landman wrote:

> Hi Brent
>
> Clements, Brent M (SAIC) wrote:
>> Hey Guys,
>>
>> I've been sitting here working for the past 48 hours and I'm fighting
>> a stupid bug in some mpi code I'm working on
>>
>> How do I broadcast a char string to my slave mpi processes? And how
>> do I receive that char string and print it out on my slave mpi
>> process.
>>
>>
>> This is what I have in my code(some things have been removed)
>>
>> #define MASTER_RANK 0
>>
>> char* mystring;
>
> Hmmm....  This makes *mystring a string (ok, a character array), and
> mystring a pointer to a character array.
>
>>
>> mystring = "some test";
>
> ok ... I haven't played with "strings" recently in C, though I seem to
> remember needing a "\0" at the end.  RGB is more likely a C language
> guru/lawyer than I am, I don't remember off the top of my head if this
> is strictly necessary.

The data string "some test" is automatically so terminated (and should
have length 10 to accomodate the termination), so that shouldn't be a
problem.  So basically mystring is allocated at compile time and its
address is loaded into the pointer mystring, which should be OK.

>>
>> MPI_Bcast(&mystring, sizeof(basedir), MPI_CHAR, MASTER_RANK,
>> MPI_COMM_WORLD);
>
> Lets put on our parser hat.  The first argument is
>
> 	pointer to (pointer to mystring)
>
> mystring is a pointer, *mystring is the (technically) first character in
> the array, and &mystring is a pointer to mystring, which is a pointer to
> a pointer to the first character in the array.

Ya, I think so to.  If you allocated:

char mystring[] = "some test";

then you should be able to use &mystring, &mystring[0], or just mystring
as synonyms.  However, char *mystring means it is ALREADY a pointer, so
assuming

  int MPI_Bcast ( void *buffer, int count, MPI_Datatype datatype,
    int root, MPI_Comm comm );

you don't want to pass the address of a POINTER, you want to pass the
POINTER ITSELF -- the contents of which are the address of the string.
I would therefore expect the call to look more like:

   MPI_Bcast(mystring,10,MPI_CHAR,MASTER_RANK,MPI_COMM_WORLD);

but mind you, I don't speak MPI per se.

    rgb

-- 
Robert G. Brown	                       http://www.phy.duke.edu/~rgb/
Duke University Dept. of Physics, Box 90305
Durham, N.C. 27708-0305
Phone: 1-919-660-2567  Fax: 919-660-2525     email:rgb at phy.duke.edu





More information about the Beowulf mailing list