Open MPI logo

Open MPI User's Mailing List Archives

  |   Home   |   Support   |   FAQ   |   all Open MPI User's mailing list

Subject: Re: [OMPI users] MPI_AllGather null terminator character
From: Jeff Squyres (jsquyres_at_[hidden])
Date: 2012-01-26 16:52:04


I'm not sure what you're asking.

The entire contents of hostname[] will be sent -- from position 0 to position (MAX_STRING_LEN-1). If there's a \0 in there, it will be sent. If the \0 occurs after that, then it won't.

Be aware that get_hostname(buf, size) will not put a \0 in the buffer if the hostname is exactly "size" bytes. So you might want to double check that your get_hostname() is returning a \0-terminated string.

Does that make sense?

Here's a sample I wrote to verify this:

#include <stdio.h>
#include <string.h>
#include <mpi.h>
#include <stdlib.h>

#define MAX_LEN 64

static void where_null(char *ptr, int len, int rank)
{
    int i;

    for (i = 0; i < len; ++i) {
        if ('\0' == ptr[i]) {
            printf("Rank %d: Null found at position %d (string: %s)\n",
                   rank, i, ptr);
            return;
        }
    }

    printf("Rank %d: Null not found! (string: ", rank);
    for (i = 0; i < len; ++i) putc(ptr[i], stdout);
    putc('\n', stdout);
}

int main()
{
    int i;
    char hostname[MAX_LEN];
    char *hostname_recv_buf;
    int rank, size;

    MPI_Init(NULL, NULL);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    gethostname(hostname, MAX_LEN - 1);
    where_null(hostname, MAX_LEN, rank);

    hostname_recv_buf = calloc(size * (MAX_LEN), (sizeof(char)));
    MPI_Allgather(hostname, MAX_LEN, MPI_CHAR,
                  hostname_recv_buf, MAX_LEN, MPI_CHAR, MPI_COMM_WORLD);
    for (i = 0; i < size; ++i) {
        where_null(hostname_recv_buf + i * MAX_LEN, MAX_LEN, rank);
    }

    MPI_Finalize();
    return 0;
}

On Jan 13, 2012, at 2:32 AM, Gabriele Fatigati wrote:

> Dear OpenMPI,
>
> using MPI_Allgather with MPI_CHAR type, I have a doubt about null-terminated character. Imaging I want to spawn node names where my program is running on:
>
>
> ----------------------------------------
>
> char hostname[MAX_LEN];
>
> char* hostname_recv_buf=(char*)calloc(num_procs*(MAX_STRING_LEN),(sizeof(char)));
>
> MPI_Allgather(hostname, MAX_STRING_LEN, MPI_CHAR, hostname_recv_buf, MAX_STRING_LEN, MPI_CHAR, MPI_COMM_WORLD);
>
> ----------------------------------------
>
>
> Now, is the null-terminated character of each local string included? Or I have to send and receive in MPI_Allgather MAX_STRING_LEN+1 elements?
>
> Using Valgrind, in a subsequent simple strcmp:
>
> for( i= 0; i< num_procs; i++){
> if(strcmp(&hostname_recv_buf[MAX_STRING_LEN*i], local_hostname)==0){
> ... doing something....
> }
> }
>
> raise a warning:
>
> Conditional jump or move depends on uninitialised value(s)
> ==19931== at 0x4A06E5C: strcmp (mc_replace_strmem.c:412)
>
> The same warning is not present if I use MAX_STRING_LEN+1 in MPI_Allgather.
>
>
> Thanks in forward.
>
> --
> Ing. Gabriele Fatigati
>
> HPC specialist
>
> SuperComputing Applications and Innovation Department
>
> Via Magnanelli 6/3, Casalecchio di Reno (BO) Italy
>
> www.cineca.it Tel: +39 051 6171722
>
> g.fatigati [AT] cineca.it
> _______________________________________________
> users mailing list
> users_at_[hidden]
> http://www.open-mpi.org/mailman/listinfo.cgi/users

-- 
Jeff Squyres
jsquyres_at_[hidden]
For corporate legal information go to:
http://www.cisco.com/web/about/doing_business/legal/cri/