Thread: MPI in C

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    MPI in C

    Hi all

    I am using MPI and c programming. i want to collect strings from processors to root processor
    now i have writen the code as below but does not display the strings. Can anyone help me with this?
    The code is as below:
    Code:
    #include <mpi.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    int main(int argc, char *argv[]){
    int numProcs;
    int myRank, a, *data, i;
    char **DATA;
    char *b;
    //initialize MPI
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD,&myRank);
    MPI_Comm_size(MPI_COMM_WORLD, &numProcs);
    // do some computation and MPI communications
    a = 1000+myRank;
     b = "sdfsd";
    if (myRank == 1){
    data = (int *) malloc(numProcs*sizeof(int));
    DATA = (char **) calloc(numProcs, sizeof(char **));
    for (i=0;i<numProcs;i++){
    data[i] = 0;
    DATA[i] = "helloP";
     }
    printf("\n contents of data array before gather:\n");
    for (i=0;i<numProcs;i++)
    printf("%s\n", DATA[i]);
    
    }
    
    MPI_Gather(&b, 5, MPI_CHAR, DATA, 5,  MPI_CHAR,1, MPI_COMM_WORLD);
    
    printf("here are contents %s\n",DATA[1]); 
    if (myRank == 1){
    printf("\n contents of data array after gather:\n");
    for (i=0;i<numProcs;i++){
    	printf("%s\n", DATA[i]);
     /*printf("2. DATA[%d] = %s \n",i, DATA[i]);*/}}
    
    //shutdown MPI
    MPI_Finalize();
    return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Don't see anything DIRECTLY wrong. What are you actually receiving?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    Thanks matsp for quick response. Actually bellow is the message i receive:

    rank 2 in job 8 cslab117-002_50788 caused collective abort of all ranks
    exit status of rank 2: killed by signal 11
    rank 0 in job 8 cslab117-002_50788 caused collective abort of all ranks
    exit status of rank 0: killed by signal 11

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    signal 11 is a "seg-fault", which means your app tried to write to memory that doesn't exist.

    I suspect it is because your DATA is actually pointing to read-only memory. Maybe you would be better off allocating some memory for DATA[i] instead of setting it to "HELLO". I don't KNOW that this is the problem, but it seems reasonable that your processes can not write to read-only memory, and that would cause a Seg-fault signal.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    well, when i remove the line
    Code:
     DATA[i] = "helloP";
    i still get the message:
    rank 1 in job 11 cslab117-002_50788 caused collective abort of all ranks
    exit status of rank 1: killed by signal 11

    Clear allocation of memory is not a problem here. so im wondering exactly what i need to change. If you look at line:
    Code:
     MPI_Gather(&b, 5, MPI_CHAR, DATA, 5,  MPI_CHAR,1, MPI_COMM_WORLD);
    , can you pick something that might be causing this error, because i suspect that the error is there

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So where is DATA[i] pointing now? Just removing the assignment IS NOT ENOUGH. You need each process to have some space to write the data into.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Communication using MPI
    By Cell in forum Linux Programming
    Replies: 9
    Last Post: 08-13-2009, 02:28 AM
  2. MPI, which way will be best?
    By maverick_starst in forum C++ Programming
    Replies: 5
    Last Post: 06-30-2009, 02:17 PM
  3. Sorting whit MPI
    By isato in forum C Programming
    Replies: 0
    Last Post: 03-03-2009, 10:38 AM
  4. Malloc and MPI
    By moddinati in forum C Programming
    Replies: 17
    Last Post: 03-07-2008, 07:55 PM
  5. MPI programming
    By kris.c in forum Tech Board
    Replies: 1
    Last Post: 12-08-2006, 12:25 PM

Tags for this Thread