[Beowulf] Stupid MPI programming question
Jakob Oestergaard
jakob at unthought.net
Thu Sep 28 00:42:42 PDT 2006
On Thu, Sep 28, 2006 at 01:47:17AM +0100, Clements, Brent M (SAIC) wrote:
> Hey Guys,
...
>
> This is what I have in my code(some things have been removed)
>
> #define MASTER_RANK 0
>
> char* mystring;
So mystring is a pointer to character(s).
>
> mystring = "some test";
Now mystring points to a statically allocated string "some test".
You must not overwrite "some test" with other data - the data area is
assumed to be constant and compilers are even free to merge such
strings. For example:
char *a = "some fancy test";
char *b = "fancy test";
This can result in the following memory layout
"some fancy test"
^ ^
a |
b
Imagine what would happen to *a if you changed the contents of *b...
I don't know if your compiler does this - but it is just one example of
what *could* go wrong.
My current gcc will place the strings in a read-only part of the program
memory. This means, the following code will segfault:
---
char *foo = "my test"; // 8 bytes in possibly read-only memory
...
foo[0] = ' '; // <- blamo! segfault
---
What you probably want to do is:
---
char foo[1024]; // 1KiB on the stack - writable
strncpy(foo, sizeof foo, "my test"); // Assign contents by copying
...
foo[0] = ' '; // <- fine
---
>
> MPI_Bcast(&mystring, sizeof(basedir), MPI_CHAR, MASTER_RANK, MPI_COMM_WORLD);
It's really odd to me that MPI_Bcast with MPI_CHAR takes the *address*
of the *pointer* rather than the address of the data that needs to be
sent... But I looked up another example and it does look like that's
really what you need to do. Odd...
So, anyway, I think what you want to do is:
char foo[1024];
strncpy(foo, sizeof foo, "my test");
MPI_Bcast(&foo, sizeof foo, MPI_CHAR, ...);
...
>
> Am I doing something wrong? Or if someone has some sample code they can point me to on how to send a string to a slave mpi process and have that slave mpi process print out the string..that would be even better. That way..I relearn MPI basics 101
Looks like the problem is with C not MPI - unless of course I've completely missed something ;)
--
/ jakob
More information about the Beowulf
mailing list