Thread: Error on sending argv

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

    Error on sending argv

    Hi all,
    I write a simple MPI program to send a text message to another process. The code is below.
    (test.c)
    Code:
    #include "mpi.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char* argv[]) {
        int dest, noProcesses, processId;
        MPI_Status status;
        
        char* buffer;
        
        char* text = "ABCDEF";
        
        MPI_Init(&argc, &argv);
        MPI_Comm_size(MPI_COMM_WORLD, &noProcesses);
        MPI_Comm_rank(MPI_COMM_WORLD, &processId);
        
        buffer = (char*) malloc(256 * sizeof(char));
        
        if (processId == 0) {
          fprintf(stdout, "Master: sending %s to %d\n", text, 1);
          MPI_Send((void *)&text, strlen(text) + 1, MPI_CHAR, 1, 0, MPI_COMM_WORLD);
        } else {
          MPI_Recv(&buffer, 128, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
          fprintf(stdout, "Slave: received %s from %d\n", buffer, status.MPI_SOURCE);
        }
        MPI_Finalize();
        return 0;
    }
    After compiling and executing it I get the following output:
    Code:
    [root@cluster Desktop]# mpicc -o test test.c
    [root@cluster Desktop]# mpirun -np 2 test
    Master: sending ABCDEF to 1
    Slave: received ABCDEF from 0
    In the source code above, I replace
    Code:
    char* text = "ABCDEF";
    by
    Code:
    char* text = argv[1];
    then compile and execute it again with the following commands:
    Code:
    [root@cluster Desktop]# mpicc -o test test.c
    [root@cluster Desktop]# mpirun -np 2 test ABCDEF
    Then I get the following output:
    Code:
    Master: sending ABCDEF to 1
    [cluster:03917] *** Process received signal ***
    [cluster:03917] Signal: Segmentation fault (11)
    [cluster:03917] Signal code: Address not mapped (1)
    [cluster:03917] Failing at address: 0xbfa445a2
    [cluster:03917] [ 0] [0x959440]
    [cluster:03917] [ 1] /lib/libc.so.6(_IO_fprintf+0x22) [0x76be02]
    [cluster:03917] [ 2] test(main+0x143) [0x80488b7]
    [cluster:03917] [ 3] /lib/libc.so.6(__libc_start_main+0xdc) [0x73be8c]
    [cluster:03917] [ 4] test [0x80486c1]
    [cluster:03917] *** End of error message ***
    --------------------------------------------------------------------------
    mpirun noticed that process rank 1 with PID 3917 on node cluster.hpc.org exited on signal 11 (Segmentation fault).
    --------------------------------------------------------------------------
    I’m very confused because the only difference between the two source codes is the difference between
    Code:
    char* text = "ABCDEF";
    and
    Code:
    char* text = argv[1];
    Can any one help me why the results are so different? How can I send argv[i] to another process?
    Thank you very much!
    Last edited by awiles; 04-20-2010 at 02:30 AM. Reason: Added [code][/code] tags

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Drop the & from these two lines?

    MPI_Send((void *)&text, strlen(text) + 1, MPI_CHAR, 1, 0, MPI_COMM_WORLD);

    MPI_Recv(&buffer, 128, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    Thanks for your reply but I encounter segmentation fault error again.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by awiles View Post
    Thanks for your reply but I encounter segmentation fault error again.
    Probably because argv[1] for test is NULL. Ensure that "ABCDEF" is indeed being passed to your program test.
    Last edited by itCbitC; 04-19-2010 at 06:40 PM.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    Quote Originally Posted by itCbitC View Post
    Probably because argv[1] for test is NULL. Ensure that "ABCDEF" is indeed being passed to your program test.
    The line
    Code:
    fprintf(stdout, "Master: sending %s to %d\n", text, 1);
    produces the following output:
    Code:
    Master: sending ABCDEF to 1
    which means argv[1] cannot be NULL.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. argc, argv
    By Martas in forum C Programming
    Replies: 6
    Last Post: 11-19-2009, 09:39 AM
  2. Sending data - line by line?
    By tuckker in forum C Programming
    Replies: 0
    Last Post: 02-21-2009, 09:31 PM
  3. argv
    By taurus in forum C Programming
    Replies: 15
    Last Post: 10-14-2007, 08:57 AM
  4. Using argc and argv data
    By Rad_Turnip in forum C Programming
    Replies: 4
    Last Post: 03-31-2006, 06:09 AM
  5. more argv and argc
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 09-08-2001, 11:04 PM