Actual source code: send.c

  1: #define PETSC_DLL
 2:  #include petsc.h
 3:  #include petscsys.h

  5: #if defined(PETSC_NEEDS_UTYPE_TYPEDEFS)
  6: /* Some systems have inconsistent include files that use but do not
  7:    ensure that the following definitions are made */
  8: typedef unsigned char   u_char;
  9: typedef unsigned short  u_short;
 10: typedef unsigned short  ushort;
 11: typedef unsigned int    u_int;
 12: typedef unsigned long   u_long;
 13: #endif

 15: #include <errno.h>
 16: #if defined(PETSC_HAVE_STDLIB_H)
 17: #include <stdlib.h>
 18: #endif
 19: #include <sys/types.h>
 20: #include <ctype.h>
 21: #if defined(PETSC_HAVE_MACHINE_ENDIAN_H)
 22: #include <machine/endian.h>
 23: #endif
 24: #if defined(PETSC_HAVE_UNISTD_H)
 25: #include <unistd.h>
 26: #endif
 27: #if defined(PETSC_HAVE_SYS_SOCKET_H)
 28: #include <sys/socket.h>
 29: #endif
 30: #if defined(PETSC_HAVE_SYS_WAIT_H)
 31: #include <sys/wait.h>
 32: #endif
 33: #if defined(PETSC_HAVE_NETINET_IN_H)
 34: #include <netinet/in.h>
 35: #endif
 36: #if defined(PETSC_HAVE_NETDB_H)
 37: #include <netdb.h>
 38: #endif
 39: #if defined(PETSC_HAVE_FCNTL_H)
 40: #include <fcntl.h>
 41: #endif
 42: #if defined(PETSC_HAVE_IO_H)
 43: #include <io.h>
 44: #endif
 45: #if defined(PETSC_HAVE_WINSOCK2_H)
 46: #include <Winsock2.h>
 47: #endif

 49:  #include src/sys/viewer/impls/socket/socket.h
 50: #include "petscfix.h"

 53: #if defined(PETSC_NEED_CLOSE_PROTO)
 55: #endif
 56: #if defined(PETSC_NEED_SOCKET_PROTO)
 58: #endif
 59: #if defined(PETSC_NEED_SLEEP_PROTO)
 61: #endif
 62: #if defined(PETSC_NEED_CONNECT_PROTO)
 64: #endif

 67: /*--------------------------------------------------------------*/
 70: static PetscErrorCode PetscViewerDestroy_Socket(PetscViewer viewer)
 71: {
 72:   PetscViewer_Socket *vmatlab = (PetscViewer_Socket*)viewer->data;
 73:   PetscErrorCode     ierr;

 76:   if (vmatlab->port) {
 77: #if defined(PETSC_HAVE_CLOSESOCKET)
 78:     closesocket(vmatlab->port);
 79: #else
 80:     close(vmatlab->port);
 81: #endif
 82:     if (ierr) SETERRQ(PETSC_ERR_LIB,"System error closing socket");
 83:   }
 84:   PetscFree(vmatlab);
 85:   return(0);
 86: }

 88: /*--------------------------------------------------------------*/
 91: PetscErrorCode  SOCKCall_Private(char *hostname,int portnum,int *t)
 92: {
 93:   struct sockaddr_in sa;
 94:   struct hostent     *hp;
 95:   int                s = 0;
 96:   PetscErrorCode     ierr;
 97:   PetscTruth         flg = PETSC_TRUE;

100:   if (!(hp=gethostbyname(hostname))) {
101:     perror("SEND: error gethostbyname: ");
102:     SETERRQ1(PETSC_ERR_LIB,"system error open connection to %s",hostname);
103:   }
104:   PetscMemzero(&sa,sizeof(sa));
105:   PetscMemcpy(&sa.sin_addr,hp->h_addr,hp->h_length);

107:   sa.sin_family = hp->h_addrtype;
108:   sa.sin_port = htons((u_short) portnum);
109:   while (flg) {
110:     if ((s=socket(hp->h_addrtype,SOCK_STREAM,0)) < 0) {
111:       perror("SEND: error socket");  SETERRQ(PETSC_ERR_LIB,"system error");
112:     }
113:     if (connect(s,(struct sockaddr*)&sa,sizeof(sa)) < 0) {
114: #if defined(PETSC_HAVE_WSAGETLASTERROR)
115:       WSAGetLastError();
116:       if (ierr == WSAEADDRINUSE) {
117:         (*PetscErrorPrintf)("SEND: address is in use\n");
118:       } else if (ierr == WSAEALREADY) {
119:         (*PetscErrorPrintf)("SEND: socket is non-blocking \n");
120:       } else if (ierr == WSAEISCONN) {
121:         (*PetscErrorPrintf)("SEND: socket already connected\n");
122:         Sleep((unsigned) 1);
123:       } else if (ierr == WSAECONNREFUSED) {
124:         /* (*PetscErrorPrintf)("SEND: forcefully rejected\n"); */
125:         Sleep((unsigned) 1);
126:       } else {
127:         perror(NULL); SETERRQ(PETSC_ERR_LIB,"system error");
128:       }
129: #else
130:       if (errno == EADDRINUSE) {
131:         (*PetscErrorPrintf)("SEND: address is in use\n");
132:       } else if (errno == EALREADY) {
133:         (*PetscErrorPrintf)("SEND: socket is non-blocking \n");
134:       } else if (errno == EISCONN) {
135:         (*PetscErrorPrintf)("SEND: socket already connected\n");
136:         sleep((unsigned) 1);
137:       } else if (errno == ECONNREFUSED) {
138:         /* (*PetscErrorPrintf)("SEND: forcefully rejected\n"); */
139:         sleep((unsigned) 1);
140:       } else {
141:         perror(NULL); SETERRQ(PETSC_ERR_LIB,"system error");
142:       }
143: #endif
144:       flg = PETSC_TRUE;
145: #if defined(PETSC_HAVE_CLOSESOCKET)
146:       closesocket(s);
147: #else
148:       close(s);
149: #endif
150:     }
151:     else flg = PETSC_FALSE;
152:   }
153:   *t = s;
154:   return(0);
155: }

159: /*@C
160:    PetscViewerSocketOpen - Opens a connection to a Matlab or other socket
161:         based server.

163:    Collective on MPI_Comm

165:    Input Parameters:
166: +  comm - the MPI communicator
167: .  machine - the machine the server is running on
168: -  port - the port to connect to, use PETSC_DEFAULT for the default

170:    Output Parameter:
171: .  lab - a context to use when communicating with the server

173:    Level: intermediate

175:    Notes:
176:    Most users should employ the following commands to access the 
177:    Matlab PetscViewers
178: $
179: $    PetscViewerSocketOpen(MPI_Comm comm, char *machine,int port,PetscViewer &viewer)
180: $    MatView(Mat matrix,PetscViewer viewer)
181: $
182: $                or
183: $
184: $    PetscViewerSocketOpen(MPI_Comm comm,char *machine,int port,PetscViewer &viewer)
185: $    VecView(Vec vector,PetscViewer viewer)

187:    Options Database Keys:
188:    For use with  PETSC_VIEWER_SOCKET_WORLD, PETSC_VIEWER_SOCKET_SELF,
189:    PETSC_VIEWER_SOCKET_() or if 
190:     PETSC_NULL is passed for machine or PETSC_DEFAULT is passed for port
191: $    -viewer_socket_machine <machine>
192: $    -viewer_socket_port <port>

194:    Environmental variables:
195: +   PETSC_VIEWER_SOCKET_PORT portnumber
196: -   PETSC_VIEWER_SOCKET_MACHINE machine name

198:      Currently the only socket client available is Matlab. See 
199:      src/dm/da/examples/tests/ex12.c and ex12.m for an example of usage.

201:    Concepts: Matlab^sending data
202:    Concepts: sockets^sending data

204: .seealso: MatView(), VecView(), PetscViewerDestroy(), PetscViewerCreate(), PetscViewerSetType(),
205:           PetscViewerSocketSetConnection(), PETSC_VIEWER_SOCKET_, PETSC_VIEWER_SOCKET_WORLD, 
206:           PETSC_VIEWER_SOCKET_SELF
207: @*/
208: PetscErrorCode  PetscViewerSocketOpen(MPI_Comm comm,const char machine[],int port,PetscViewer *lab)
209: {

213:   PetscViewerCreate(comm,lab);
214:   PetscViewerSetType(*lab,PETSC_VIEWER_SOCKET);
215:   PetscViewerSocketSetConnection(*lab,machine,port);
216:   return(0);
217: }

221: PetscErrorCode PetscViewerSetFromOptions_Socket(PetscViewer v)
222: {
224:   PetscInt       def = -1;
225:   char           sdef[256];
226:   PetscTruth     tflg;

229:   /*
230:        These options are not processed here, they are processed in PetscViewerSocketSetConnection(), they
231:     are listed here for the GUI to display
232:   */
233:   PetscOptionsHead("Socket PetscViewer Options");
234:     PetscOptionsGetenv(v->comm,"PETSC_VIEWER_SOCKET_PORT",sdef,16,&tflg);
235:     if (tflg) {
236:       PetscOptionsAtoi(sdef,&def);
237:     } else {
238:       def = DEFAULTPORT;
239:     }
240:     PetscOptionsInt("-viewer_socket_port","Port number to use for socket","PetscViewerSocketSetConnection",def,0,0);

242:     PetscOptionsString("-viewer_socket_machine","Machine to use for socket","PetscViewerSocketSetConnection",sdef,0,0,0);
243:     PetscOptionsGetenv(v->comm,"PETSC_VIEWER_SOCKET_MACHINE",sdef,256,&tflg);
244:     if (!tflg) {
245:       PetscGetHostName(sdef,256);
246:     }
247:   PetscOptionsTail();
248:   return(0);
249: }

254: PetscErrorCode  PetscViewerCreate_Socket(PetscViewer v)
255: {
256:   PetscViewer_Socket *vmatlab;
257:   PetscErrorCode     ierr;

260:   PetscNew(PetscViewer_Socket,&vmatlab);
261:   vmatlab->port          = 0;
262:   v->data                = (void*)vmatlab;
263:   v->ops->destroy        = PetscViewerDestroy_Socket;
264:   v->ops->flush          = 0;
265:   v->ops->setfromoptions = PetscViewerSetFromOptions_Socket;

267:   /* lie and say this is a binary viewer; then all the XXXView_Binary() methods will work correctly on it */
268:   PetscObjectChangeTypeName((PetscObject)v,PETSC_VIEWER_BINARY);
269:   return(0);
270: }

275: /*@C
276:       PetscViewerSocketSetConnection - Sets the machine and port that a PETSc socket 
277:              viewer is to use

279:   Collective on PetscViewer

281:   Input Parameters:
282: +   v - viewer to connect
283: .   machine - host to connect to
284: -   port - the port on the machine one is connecting to

286:     Level: advanced

288: .seealso: PetscViewerSocketOpen()
289: @*/
290: PetscErrorCode  PetscViewerSocketSetConnection(PetscViewer v,const char machine[],PetscInt port)
291: {
292:   PetscErrorCode     ierr;
293:   PetscMPIInt        rank;
294:   char               mach[256];
295:   PetscTruth         tflg;
296:   PetscViewer_Socket *vmatlab = (PetscViewer_Socket *)v->data;

299:   if (port <= 0) {
300:     char portn[16];
301:     PetscOptionsGetenv(v->comm,"PETSC_VIEWER_SOCKET_PORT",portn,16,&tflg);
302:     if (tflg) {
303:       PetscOptionsAtoi(portn,&port);
304:     } else {
305:       port = DEFAULTPORT;
306:     }
307:   }
308:   if (!machine) {
309:     PetscOptionsGetenv(v->comm,"PETSC_VIEWER_SOCKET_MACHINE",mach,256,&tflg);
310:     if (!tflg) {
311:       PetscGetHostName(mach,256);
312:     }
313:   } else {
314:     PetscStrncpy(mach,machine,256);
315:   }

317:   MPI_Comm_rank(v->comm,&rank);
318:   if (!rank) {
319:     PetscInfo2(0,"Connecting to socket process on port %D machine %s\n",port,mach);
320:     SOCKCall_Private(mach,(int)port,&vmatlab->port);
321:   }
322:   return(0);
323: }

325: /* ---------------------------------------------------------------------*/
326: /*
327:     The variable Petsc_Viewer_Socket_keyval is used to indicate an MPI attribute that
328:   is attached to a communicator, in this case the attribute is a PetscViewer.
329: */
330: static PetscMPIInt Petsc_Viewer_Socket_keyval = MPI_KEYVAL_INVALID;


335: /*@C
336:      PETSC_VIEWER_SOCKET_ - Creates a socket viewer shared by all processors 
337:                      in a communicator.

339:      Collective on MPI_Comm

341:      Input Parameter:
342: .    comm - the MPI communicator to share the socket PetscViewer

344:      Level: intermediate

346:    Options Database Keys:
347:    For use with the default Matlab PetscViewer, PETSC_VIEWER_SOCKET_WORLD or if 
348:     PETSC_NULL is passed for machine or PETSC_DEFAULT is passed for port
349: $    -viewer_socket_machine <machine>
350: $    -viewer_socket_port <port>

352:    Environmental variables:
353: +   PETSC_VIEWER_SOCKET_PORT portnumber
354: -   PETSC_VIEWER_SOCKET_MACHINE machine name

356:      Notes:
357:      Unlike almost all other PETSc routines, PetscViewer_SOCKET_ does not return 
358:      an error code.  The socket PetscViewer is usually used in the form
359: $       XXXView(XXX object,PETSC_VIEWER_SOCKET_(comm));

361:      Currently the only socket client available is Matlab. See 
362:      src/dm/da/examples/tests/ex12.c and ex12.m for an example of usage.

364:      Connects to a waiting socket and stays connected until PetscViewerDestroy() is called.

366: .seealso: PETSC_VIEWER_SOCKET_WORLD, PETSC_VIEWER_SOCKET_SELF, PetscViewerSocketOpen(), PetscViewerCreate(),
367:           PetscViewerSocketSetConnection(), PetscViewerDestroy(), PETSC_VIEWER_SOCKET_()
368: @*/
369: PetscViewer  PETSC_VIEWER_SOCKET_(MPI_Comm comm)
370: {
372:   PetscTruth     flg;
373:   PetscViewer    viewer;

376:   if (Petsc_Viewer_Socket_keyval == MPI_KEYVAL_INVALID) {
377:     MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Socket_keyval,0);
378:     if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_SOCKET_",__FILE__,__SDIR__,1,1," ");return(0);}
379:   }
380:   MPI_Attr_get(comm,Petsc_Viewer_Socket_keyval,(void **)&viewer,(int*)&flg);
381:   if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_SOCKET_",__FILE__,__SDIR__,1,1," ");return(0);}
382:   if (!flg) { /* PetscViewer not yet created */
383:     PetscViewerSocketOpen(comm,0,0,&viewer);
384:     if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_SOCKET_",__FILE__,__SDIR__,1,1," ");return(0);}
385:     PetscObjectRegisterDestroy((PetscObject)viewer);
386:     if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_SOCKET_",__FILE__,__SDIR__,1,1," ");return(0);}
387:     MPI_Attr_put(comm,Petsc_Viewer_Socket_keyval,(void*)viewer);
388:     if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_SOCKET_",__FILE__,__SDIR__,1,1," ");return(0);}
389:   }
390:   PetscFunctionReturn(viewer);
391: }