/*@@ @file HostNames.c @date Tue Nov 7 17:36:35 2000 @author Tom Goodale @desc Routines to collect data about all hosts in a Ccatus job. @enddesc @version $Header$ @@*/ #ifndef TEST_HOSTNAMES #include "cctk.h" #endif #include #include #include #ifdef HAVE_UNISTD_H #include #endif /* HAVE_UNISTD_H */ #ifdef HAVE_SYS_TIME_H #include #endif /* HAVE_SYS_TIME_H */ #ifdef HAVE_SYS_TYPES_H #include #endif /* HAVE_SYS_TYPES_H */ #ifdef HAVE_SYS_SOCKET_H #include #endif /* HAVE_SYS_SOCKET_H */ #ifdef HAVE_NETINET_IN_H #include #endif /* HAVE_NETINET_IN_H */ #ifdef HAVE_NETDB_H #include #endif /* HAVE_NETDB_H */ #ifdef CCTK_MPI #include "mpi.h" #endif /* CCTK_MPI */ #include "httpextra_HostNames.h" static char *rcsid = "$Header$"; #ifdef CCTK_FILEVERSION CCTK_FILEVERSION(CactusConnect_HTTPDExtra_HostNames_c) #endif /******************************************************************** ********************* Local Data Types *********************** ********************************************************************/ #define HOSTDATALENGTH 255 /******************************************************************** ********************* Local Routine Prototypes ********************* ********************************************************************/ static void GetHostName(char *name, int length); /******************************************************************** ********************* Other Routine Prototypes ********************* ********************************************************************/ /******************************************************************** ********************* Local Data ***************************** ********************************************************************/ char *hostdata = NULL; /******************************************************************** ********************* External Routines ********************** ********************************************************************/ /*@@ @routine HTTPDExtra_CollateHostData @date Tue Nov 7 18:10:01 2000 @author Tom Goodale @desc Gets data about all hosts in the parallel job. @enddesc @calls @calledby @history @endhistory @@*/ void HTTPDExtra_CollateHostData(void) { int rank; int nprocs; char thisdata[HOSTDATALENGTH+1]; GetHostName(thisdata, HOSTDATALENGTH); thisdata[HOSTDATALENGTH] = 0; #ifdef CCTK_MPI /* Work out how many processes there are. */ MPI_Comm_size(MPI_COMM_WORLD, &nprocs); /* Work out if this is proc 0 or not. */ MPI_Comm_rank(MPI_COMM_WORLD, &rank); #else nprocs = 1; rank = 0; #endif if(rank == 0) { hostdata=(char *)malloc((HOSTDATALENGTH+1)*nprocs); if(!hostdata) { exit(999); } } #ifdef CCTK_MPI MPI_Gather(thisdata, HOSTDATALENGTH+1, MPI_BYTE, hostdata, HOSTDATALENGTH+1, MPI_BYTE, 0, MPI_COMM_WORLD); #else strncpy(hostdata, thisdata, HOSTDATALENGTH); #endif } /*@@ @routine HTTPDExtra_RemoteHostData @date Tue Nov 7 18:10:38 2000 @author Tom Goodale @desc Gets name of a remote host indexed by host process number. @enddesc @calls @calledby @history @endhistory @@*/ const char *HTTPDExtra_RemoteHostName(int host) { return hostdata+host*(HOSTDATALENGTH+1); } /******************************************************************** ********************* Local Routines ************************* ********************************************************************/ /*@@ @routine GetHostName @date Fri Oct 20 12:12:34 2000 @author Tom Goodale @desc Gets the fully qualified name of this host if possible. @enddesc @calls @calledby @history @endhistory @var name @vdesc character buffer to store name in @vtype char * @vio out @vcomment @endvar @var length @vdesc length of the character buffer @vtype int @vio in @vcomment @endvar @@*/ static void GetHostName(char *name, int length) { gethostname(name, length); /* Does the name include the domain. */ if(!strchr(name, '.')) { #ifdef HAVE_GETHOSTBYNAME struct hostent *thishostent; thishostent = gethostbyname(name); strncpy(name, thishostent->h_name, length); #endif } } #ifdef TEST_HOSTNAMES int main(int argc, char *argv[]) { int rank; int nprocs; #ifdef CCTK_MPI MPI_Init(&argc, &argv); /* Work out how many processes there are. */ MPI_Comm_size(MPI_COMM_WORLD, &nprocs); /* Work out if this is proc 0 or not. */ MPI_Comm_rank(MPI_COMM_WORLD, &rank); #else nprocs = 1; rank = 0; #endif HTTPDExtra_CollateHostData(); if(rank == 0) { for(rank = 0; rank < nprocs; rank++) { printf("Host %d is %s\n", rank, HTTPDExtra_RemoteHostName(rank)); } } #ifdef CCTK_MPI MPI_Finalize(); #endif return 0; } #endif /* TESTHOSTNAMES */