aboutsummaryrefslogtreecommitdiff
path: root/src/HostNames.c
blob: 961c7d530b77f83616bd74b59e24bfdd8b35b96a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
 /*@@
   @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 <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif /* HAVE_SYS_TIME_H */
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif /* HAVE_SYS_TYPES_H */
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif /* HAVE_SYS_SOCKET_H */
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif /* HAVE_NETINET_IN_H */
#ifdef HAVE_NETDB_H
#include <netdb.h>
#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 */