Sorry, this is the right code.
2012/1/27 Gabriele Fatigati <g.fatig...@cineca.it> > Hi Jeff, > > The problem is when I use strcmp on ALLGather buffer and Valgrind that > raise a warning. > > Please check if the attached code is right, where size(local_hostname) is > very small. > > Valgrind is used as: > > mpirun valgrind --leak-check=full --tool=memcheck ./all_gather > > and openmpi/1.4.4 compiled with "-O0 -g" > > Thanks! > > 2012/1/26 Jeff Squyres <jsquy...@cisco.com> > >> 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 >> > us...@open-mpi.org >> > http://www.open-mpi.org/mailman/listinfo.cgi/users >> >> >> -- >> Jeff Squyres >> jsquy...@cisco.com >> For corporate legal information go to: >> http://www.cisco.com/web/about/doing_business/legal/cri/ >> >> >> _______________________________________________ >> users mailing list >> us...@open-mpi.org >> http://www.open-mpi.org/mailman/listinfo.cgi/users >> > > > > -- > 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 > -- 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
#include <stdlib.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <mpi.h> int main(int argc, char *argv[]){ MPI_Init(&argc,&argv); const int max_name_len = 255; char* local_hostname; char* hostname_recv_buf; char** hostname_list_final; int i, num_procs, rank; MPI_Comm_size(MPI_COMM_WORLD, &num_procs); MPI_Comm_rank(MPI_COMM_WORLD, &rank); local_hostname = (char*) malloc(max_name_len*sizeof(char)); hostname_recv_buf=(char*)malloc(num_procs*(max_name_len+1)*(sizeof(char))); hostname_list_final=(char**)malloc(num_procs*(sizeof(char*))); for (i=0; i< num_procs; i++) hostname_list_final[i] = (char*)malloc((max_name_len+1)*sizeof(char)); gethostname(local_hostname, sizeof(local_hostname) ); // MPI_Alltoall(hostname_send_buf, max_name_len, MPI_CHAR, hostname_recv_buf, max_name_len, MPI_CHAR, MPI_COMM_WORLD); MPI_Allgather(local_hostname, 255, MPI_CHAR, hostname_recv_buf, 255, MPI_CHAR, MPI_COMM_WORLD); for (i=0; i< num_procs; i++){ strcpy (hostname_list_final[i], &hostname_recv_buf[max_name_len*i],1); // hostname_list_final[i][255] = '\0'; } if( rank==0){ for (i=0; i< num_procs; i++) printf( "i--: %d hostname_list[i]: %s \n", i, hostname_list_final[i]); } // return hostname_list_final; free(local_hostname); free(hostname_recv_buf); MPI_Finalize(); return 0; }