15c6c1daeSBarry Smith 25c6c1daeSBarry Smith #include <petscsys.h> 35c6c1daeSBarry Smith 45c6c1daeSBarry Smith #if defined(PETSC_NEEDS_UTYPE_TYPEDEFS) 55c6c1daeSBarry Smith /* Some systems have inconsistent include files that use but do not 65c6c1daeSBarry Smith ensure that the following definitions are made */ 75c6c1daeSBarry Smith typedef unsigned char u_char; 85c6c1daeSBarry Smith typedef unsigned short u_short; 95c6c1daeSBarry Smith typedef unsigned short ushort; 105c6c1daeSBarry Smith typedef unsigned int u_int; 115c6c1daeSBarry Smith typedef unsigned long u_long; 125c6c1daeSBarry Smith #endif 135c6c1daeSBarry Smith 145c6c1daeSBarry Smith #include <errno.h> 155c6c1daeSBarry Smith #include <ctype.h> 165c6c1daeSBarry Smith #if defined(PETSC_HAVE_MACHINE_ENDIAN_H) 175c6c1daeSBarry Smith #include <machine/endian.h> 185c6c1daeSBarry Smith #endif 195c6c1daeSBarry Smith #if defined(PETSC_HAVE_UNISTD_H) 205c6c1daeSBarry Smith #include <unistd.h> 215c6c1daeSBarry Smith #endif 225c6c1daeSBarry Smith #if defined(PETSC_HAVE_SYS_SOCKET_H) 235c6c1daeSBarry Smith #include <sys/socket.h> 245c6c1daeSBarry Smith #endif 255c6c1daeSBarry Smith #if defined(PETSC_HAVE_SYS_WAIT_H) 265c6c1daeSBarry Smith #include <sys/wait.h> 275c6c1daeSBarry Smith #endif 285c6c1daeSBarry Smith #if defined(PETSC_HAVE_NETINET_IN_H) 295c6c1daeSBarry Smith #include <netinet/in.h> 305c6c1daeSBarry Smith #endif 315c6c1daeSBarry Smith #if defined(PETSC_HAVE_NETDB_H) 325c6c1daeSBarry Smith #include <netdb.h> 335c6c1daeSBarry Smith #endif 345c6c1daeSBarry Smith #if defined(PETSC_HAVE_FCNTL_H) 355c6c1daeSBarry Smith #include <fcntl.h> 365c6c1daeSBarry Smith #endif 375c6c1daeSBarry Smith #if defined(PETSC_HAVE_IO_H) 385c6c1daeSBarry Smith #include <io.h> 395c6c1daeSBarry Smith #endif 405c6c1daeSBarry Smith #if defined(PETSC_HAVE_WINSOCK2_H) 415c6c1daeSBarry Smith #include <Winsock2.h> 425c6c1daeSBarry Smith #endif 435c6c1daeSBarry Smith #include <sys/stat.h> 445c6c1daeSBarry Smith #include <../src/sys/classes/viewer/impls/socket/socket.h> 455c6c1daeSBarry Smith 465c6c1daeSBarry Smith #if defined(PETSC_NEED_CLOSE_PROTO) 478cc058d9SJed Brown PETSC_EXTERN int close(int); 485c6c1daeSBarry Smith #endif 495c6c1daeSBarry Smith #if defined(PETSC_NEED_SOCKET_PROTO) 508cc058d9SJed Brown PETSC_EXTERN int socket(int, int, int); 515c6c1daeSBarry Smith #endif 525c6c1daeSBarry Smith #if defined(PETSC_NEED_SLEEP_PROTO) 538cc058d9SJed Brown PETSC_EXTERN int sleep(unsigned); 545c6c1daeSBarry Smith #endif 555c6c1daeSBarry Smith #if defined(PETSC_NEED_CONNECT_PROTO) 568cc058d9SJed Brown PETSC_EXTERN int connect(int, struct sockaddr *, int); 575c6c1daeSBarry Smith #endif 585c6c1daeSBarry Smith 595c6c1daeSBarry Smith /*--------------------------------------------------------------*/ 60d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscViewerDestroy_Socket(PetscViewer viewer) 61d71ae5a4SJacob Faibussowitsch { 625c6c1daeSBarry Smith PetscViewer_Socket *vmatlab = (PetscViewer_Socket *)viewer->data; 635c6c1daeSBarry Smith PetscErrorCode ierr; 645c6c1daeSBarry Smith 655c6c1daeSBarry Smith PetscFunctionBegin; 665c6c1daeSBarry Smith if (vmatlab->port) { 675c6c1daeSBarry Smith #if defined(PETSC_HAVE_CLOSESOCKET) 685c6c1daeSBarry Smith ierr = closesocket(vmatlab->port); 695c6c1daeSBarry Smith #else 705c6c1daeSBarry Smith ierr = close(vmatlab->port); 715c6c1daeSBarry Smith #endif 7228b400f6SJacob Faibussowitsch PetscCheck(!ierr, PETSC_COMM_SELF, PETSC_ERR_SYS, "System error closing socket"); 735c6c1daeSBarry Smith } 749566063dSJacob Faibussowitsch PetscCall(PetscFree(vmatlab)); 752e956fe4SStefano Zampini PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerBinarySetSkipHeader_C", NULL)); 762e956fe4SStefano Zampini PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerBinaryGetSkipHeader_C", NULL)); 772e956fe4SStefano Zampini PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerBinaryGetFlowControl_C", NULL)); 785c6c1daeSBarry Smith PetscFunctionReturn(0); 795c6c1daeSBarry Smith } 805c6c1daeSBarry Smith 815c6c1daeSBarry Smith /*--------------------------------------------------------------*/ 824683183fSBarry Smith /*@C 835c6c1daeSBarry Smith PetscSocketOpen - handles connected to an open port where someone is waiting. 845c6c1daeSBarry Smith 854683183fSBarry Smith Input Parameters: 864683183fSBarry Smith + url - for example www.mcs.anl.gov 874683183fSBarry Smith - portnum - for example 80 884683183fSBarry Smith 8901d2d390SJose E. Roman Output Parameter: 904683183fSBarry Smith . t - the socket number 914683183fSBarry Smith 9295452b02SPatrick Sanan Notes: 9395452b02SPatrick Sanan Use close() to close the socket connection 944683183fSBarry Smith 95811af0c4SBarry Smith Use read() or `PetscHTTPRequest()` to read from the socket 964683183fSBarry Smith 974683183fSBarry Smith Level: advanced 984683183fSBarry Smith 99db781477SPatrick Sanan .seealso: `PetscSocketListen()`, `PetscSocketEstablish()`, `PetscHTTPRequest()`, `PetscHTTPSConnect()` 1004683183fSBarry Smith @*/ 101d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscOpenSocket(const char hostname[], int portnum, int *t) 102d71ae5a4SJacob Faibussowitsch { 1035c6c1daeSBarry Smith struct sockaddr_in sa; 1045c6c1daeSBarry Smith struct hostent *hp; 1055c6c1daeSBarry Smith int s = 0; 1065c6c1daeSBarry Smith PetscBool flg = PETSC_TRUE; 1074a285bdaSBarry Smith static int refcnt = 0; 1085c6c1daeSBarry Smith 1095c6c1daeSBarry Smith PetscFunctionBegin; 1105c6c1daeSBarry Smith if (!(hp = gethostbyname(hostname))) { 1115c6c1daeSBarry Smith perror("SEND: error gethostbyname: "); 11298921bdaSJacob Faibussowitsch SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SYS, "system error open connection to %s", hostname); 1135c6c1daeSBarry Smith } 1149566063dSJacob Faibussowitsch PetscCall(PetscMemzero(&sa, sizeof(sa))); 1159566063dSJacob Faibussowitsch PetscCall(PetscMemcpy(&sa.sin_addr, hp->h_addr_list[0], hp->h_length)); 1165c6c1daeSBarry Smith 1175c6c1daeSBarry Smith sa.sin_family = hp->h_addrtype; 1185c6c1daeSBarry Smith sa.sin_port = htons((u_short)portnum); 1195c6c1daeSBarry Smith while (flg) { 1205c6c1daeSBarry Smith if ((s = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) { 1219371c9d4SSatish Balay perror("SEND: error socket"); 1229371c9d4SSatish Balay SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SYS, "system error"); 1235c6c1daeSBarry Smith } 1245c6c1daeSBarry Smith if (connect(s, (struct sockaddr *)&sa, sizeof(sa)) < 0) { 1255c6c1daeSBarry Smith #if defined(PETSC_HAVE_WSAGETLASTERROR) 1265c6c1daeSBarry Smith ierr = WSAGetLastError(); 127a297a907SKarl Rupp if (ierr == WSAEADDRINUSE) (*PetscErrorPrintf)("SEND: address is in use\n"); 128a297a907SKarl Rupp else if (ierr == WSAEALREADY) (*PetscErrorPrintf)("SEND: socket is non-blocking \n"); 129a297a907SKarl Rupp else if (ierr == WSAEISCONN) { 1305c6c1daeSBarry Smith (*PetscErrorPrintf)("SEND: socket already connected\n"); 1315c6c1daeSBarry Smith Sleep((unsigned)1); 1325c6c1daeSBarry Smith } else if (ierr == WSAECONNREFUSED) { 1335c6c1daeSBarry Smith /* (*PetscErrorPrintf)("SEND: forcefully rejected\n"); */ 1345c6c1daeSBarry Smith Sleep((unsigned)1); 1355c6c1daeSBarry Smith } else { 1369371c9d4SSatish Balay perror(NULL); 1379371c9d4SSatish Balay SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SYS, "system error"); 1385c6c1daeSBarry Smith } 1395c6c1daeSBarry Smith #else 140a297a907SKarl Rupp if (errno == EADDRINUSE) (*PetscErrorPrintf)("SEND: address is in use\n"); 141a297a907SKarl Rupp else if (errno == EALREADY) (*PetscErrorPrintf)("SEND: socket is non-blocking \n"); 142a297a907SKarl Rupp else if (errno == EISCONN) { 1435c6c1daeSBarry Smith (*PetscErrorPrintf)("SEND: socket already connected\n"); 1445c6c1daeSBarry Smith sleep((unsigned)1); 1455c6c1daeSBarry Smith } else if (errno == ECONNREFUSED) { 1464a285bdaSBarry Smith refcnt++; 14708401ef6SPierre Jolivet PetscCheck(refcnt <= 5, PETSC_COMM_SELF, PETSC_ERR_SYS, "Connection refused by remote host %s port %d", hostname, portnum); 1489566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL, "Connection refused in attaching socket, trying again\n")); 1495c6c1daeSBarry Smith sleep((unsigned)1); 1505c6c1daeSBarry Smith } else { 1519371c9d4SSatish Balay perror(NULL); 1529371c9d4SSatish Balay SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SYS, "system error"); 1535c6c1daeSBarry Smith } 1545c6c1daeSBarry Smith #endif 1555c6c1daeSBarry Smith flg = PETSC_TRUE; 1565c6c1daeSBarry Smith #if defined(PETSC_HAVE_CLOSESOCKET) 1575c6c1daeSBarry Smith closesocket(s); 1585c6c1daeSBarry Smith #else 1595c6c1daeSBarry Smith close(s); 1605c6c1daeSBarry Smith #endif 161a297a907SKarl Rupp } else flg = PETSC_FALSE; 1625c6c1daeSBarry Smith } 1635c6c1daeSBarry Smith *t = s; 1645c6c1daeSBarry Smith PetscFunctionReturn(0); 1655c6c1daeSBarry Smith } 1665c6c1daeSBarry Smith 1674683183fSBarry Smith /*@C 1685c6c1daeSBarry Smith PetscSocketEstablish - starts a listener on a socket 1695c6c1daeSBarry Smith 1704683183fSBarry Smith Input Parameters: 1714683183fSBarry Smith . portnumber - the port to wait at 1724683183fSBarry Smith 1734683183fSBarry Smith Output Parameters: 174811af0c4SBarry Smith . ss - the socket to be used with `PetscSocketListen()` 1754683183fSBarry Smith 1764683183fSBarry Smith Level: advanced 1774683183fSBarry Smith 178db781477SPatrick Sanan .seealso: `PetscSocketListen()`, `PetscOpenSocket()` 1794683183fSBarry Smith @*/ 180d71ae5a4SJacob Faibussowitsch PETSC_INTERN PetscErrorCode PetscSocketEstablish(int portnum, int *ss) 181d71ae5a4SJacob Faibussowitsch { 182589a23caSBarry Smith static size_t MAXHOSTNAME = 100; 1835c6c1daeSBarry Smith char myname[MAXHOSTNAME + 1]; 1845c6c1daeSBarry Smith int s; 1855c6c1daeSBarry Smith struct sockaddr_in sa; 1865c6c1daeSBarry Smith struct hostent *hp; 1875c6c1daeSBarry Smith 1885c6c1daeSBarry Smith PetscFunctionBegin; 1899566063dSJacob Faibussowitsch PetscCall(PetscGetHostName(myname, sizeof(myname))); 1905c6c1daeSBarry Smith 1919566063dSJacob Faibussowitsch PetscCall(PetscMemzero(&sa, sizeof(struct sockaddr_in))); 1925c6c1daeSBarry Smith 1935c6c1daeSBarry Smith hp = gethostbyname(myname); 19428b400f6SJacob Faibussowitsch PetscCheck(hp, PETSC_COMM_SELF, PETSC_ERR_SYS, "Unable to get hostent information from system"); 1955c6c1daeSBarry Smith 1965c6c1daeSBarry Smith sa.sin_family = hp->h_addrtype; 1975c6c1daeSBarry Smith sa.sin_port = htons((u_short)portnum); 1985c6c1daeSBarry Smith 199cc73adaaSBarry Smith PetscCheck((s = socket(AF_INET, SOCK_STREAM, 0)) >= 0, PETSC_COMM_SELF, PETSC_ERR_SYS, "Error running socket() command"); 2005c6c1daeSBarry Smith #if defined(PETSC_HAVE_SO_REUSEADDR) 2015c6c1daeSBarry Smith { 2025c6c1daeSBarry Smith int optval = 1; /* Turn on the option */ 2039566063dSJacob Faibussowitsch PetscCall(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&optval, sizeof(optval))); 2045c6c1daeSBarry Smith } 2055c6c1daeSBarry Smith #endif 2065c6c1daeSBarry Smith 2075c6c1daeSBarry Smith while (bind(s, (struct sockaddr *)&sa, sizeof(sa)) < 0) { 2085c6c1daeSBarry Smith #if defined(PETSC_HAVE_WSAGETLASTERROR) 2095c6c1daeSBarry Smith ierr = WSAGetLastError(); 2105c6c1daeSBarry Smith if (ierr != WSAEADDRINUSE) { 2115c6c1daeSBarry Smith #else 2125c6c1daeSBarry Smith if (errno != EADDRINUSE) { 2135c6c1daeSBarry Smith #endif 2145c6c1daeSBarry Smith close(s); 2155c6c1daeSBarry Smith SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SYS, "Error from bind()"); 2165c6c1daeSBarry Smith } 2175c6c1daeSBarry Smith } 2185c6c1daeSBarry Smith listen(s, 0); 2195c6c1daeSBarry Smith *ss = s; 2200654c89bSBarry Smith PetscFunctionReturn(0); 2215c6c1daeSBarry Smith } 2225c6c1daeSBarry Smith 2234683183fSBarry Smith /*@C 224811af0c4SBarry Smith PetscSocketListen - Listens at a socket created with `PetscSocketEstablish()` 2254683183fSBarry Smith 2264683183fSBarry Smith Input Parameter: 227811af0c4SBarry Smith . listenport - obtained with `PetscSocketEstablish()` 2284683183fSBarry Smith 2294683183fSBarry Smith Output Parameter: 2304683183fSBarry Smith . t - pass this to read() to read what is passed to this connection 2314683183fSBarry Smith 2324683183fSBarry Smith Level: advanced 2335c6c1daeSBarry Smith 234db781477SPatrick Sanan .seealso: `PetscSocketEstablish()` 2354683183fSBarry Smith @*/ 236d71ae5a4SJacob Faibussowitsch PETSC_INTERN PetscErrorCode PetscSocketListen(int listenport, int *t) 237d71ae5a4SJacob Faibussowitsch { 2385c6c1daeSBarry Smith struct sockaddr_in isa; 2395c6c1daeSBarry Smith #if defined(PETSC_HAVE_ACCEPT_SIZE_T) 2405c6c1daeSBarry Smith size_t i; 2415c6c1daeSBarry Smith #else 2425c6c1daeSBarry Smith int i; 2435c6c1daeSBarry Smith #endif 2445c6c1daeSBarry Smith 2455c6c1daeSBarry Smith PetscFunctionBegin; 2465c6c1daeSBarry Smith /* wait for someone to try to connect */ 2475c6c1daeSBarry Smith i = sizeof(struct sockaddr_in); 248cc73adaaSBarry Smith PetscCheck((*t = accept(listenport, (struct sockaddr *)&isa, (socklen_t *)&i)) >= 0, PETSC_COMM_SELF, PETSC_ERR_SYS, "error from accept()"); 2495c6c1daeSBarry Smith PetscFunctionReturn(0); 2505c6c1daeSBarry Smith } 2515c6c1daeSBarry Smith 2525c6c1daeSBarry Smith /*@C 253a30fc760SBarry Smith PetscViewerSocketOpen - Opens a connection to a MATLAB or other socket based server. 2545c6c1daeSBarry Smith 255d083f849SBarry Smith Collective 2565c6c1daeSBarry Smith 2575c6c1daeSBarry Smith Input Parameters: 2585c6c1daeSBarry Smith + comm - the MPI communicator 259750b007cSBarry Smith . machine - the machine the server is running on, use NULL for the local machine, use "server" to passively wait for 2605c6c1daeSBarry Smith a connection from elsewhere 261811af0c4SBarry Smith - port - the port to connect to, use `PETSC_DEFAULT` for the default 2625c6c1daeSBarry Smith 2635c6c1daeSBarry Smith Output Parameter: 2645c6c1daeSBarry Smith . lab - a context to use when communicating with the server 2655c6c1daeSBarry Smith 266811af0c4SBarry Smith Options Database Keys: 267811af0c4SBarry Smith For use with `PETSC_VIEWER_SOCKET_WORLD`, `PETSC_VIEWER_SOCKET_SELF`, 268811af0c4SBarry Smith `PETSC_VIEWER_SOCKET_()` or if 269811af0c4SBarry Smith NULL is passed for machine or PETSC_DEFAULT is passed for port 270811af0c4SBarry Smith $ -viewer_socket_machine <machine> 271811af0c4SBarry Smith $ -viewer_socket_port <port> 272811af0c4SBarry Smith 273811af0c4SBarry Smith Environmental variables: 274811af0c4SBarry Smith + `PETSC_VIEWER_SOCKET_PORT` - portnumber 275811af0c4SBarry Smith - `PETSC_VIEWER_SOCKET_MACHINE` - machine name 276811af0c4SBarry Smith 2775c6c1daeSBarry Smith Level: intermediate 2785c6c1daeSBarry Smith 2795c6c1daeSBarry Smith Notes: 2805c6c1daeSBarry Smith Most users should employ the following commands to access the 281811af0c4SBarry Smith MATLAB `PetscViewer` 2825c6c1daeSBarry Smith $ 2835c6c1daeSBarry Smith $ PetscViewerSocketOpen(MPI_Comm comm, char *machine,int port,PetscViewer &viewer) 2845c6c1daeSBarry Smith $ MatView(Mat matrix,PetscViewer viewer) 2855c6c1daeSBarry Smith $ 2865c6c1daeSBarry Smith $ or 2875c6c1daeSBarry Smith $ 2885c6c1daeSBarry Smith $ PetscViewerSocketOpen(MPI_Comm comm,char *machine,int port,PetscViewer &viewer) 2895c6c1daeSBarry Smith $ VecView(Vec vector,PetscViewer viewer) 2905c6c1daeSBarry Smith 291750b007cSBarry Smith Currently the only socket client available is MATLAB, PETSc must be configured with --with-matlab for this client. See 292c4762a1bSJed Brown src/dm/tests/ex12.c and ex12.m for an example of usage. 2935c6c1daeSBarry Smith 29495452b02SPatrick Sanan The socket viewer is in some sense a subclass of the binary viewer, to read and write to the socket 295811af0c4SBarry Smith use `PetscViewerBinaryRead()`, `PetscViewerBinaryWrite()`, `PetscViewerBinarWriteStringArray()`, `PetscViewerBinaryGetDescriptor()`. 296a30fc760SBarry Smith 297811af0c4SBarry Smith Use this for communicating with an interactive MATLAB session, see `PETSC_VIEWER_MATLAB_()` for writing output to a 298811af0c4SBarry Smith .mat file. Use `PetscMatlabEngineCreate()` or `PETSC_MATLAB_ENGINE_()`, `PETSC_MATLAB_ENGINE_SELF`, or `PETSC_MATLAB_ENGINE_WORLD` 299a30fc760SBarry Smith for communicating with a MATLAB Engine 3005c6c1daeSBarry Smith 301*d1f92df0SBarry Smith .seealso: [](sec_viewers), `PETSCVIEWERBINARY`, `PETSCVIEWERSOCKET`, `MatView()`, `VecView()`, `PetscViewerDestroy()`, `PetscViewerCreate()`, `PetscViewerSetType()`, 302db781477SPatrick Sanan `PetscViewerSocketSetConnection()`, `PETSC_VIEWER_SOCKET_`, `PETSC_VIEWER_SOCKET_WORLD`, 303db781477SPatrick Sanan `PETSC_VIEWER_SOCKET_SELF`, `PetscViewerBinaryWrite()`, `PetscViewerBinaryRead()`, `PetscViewerBinaryWriteStringArray()`, 304db781477SPatrick Sanan `PetscBinaryViewerGetDescriptor()`, `PetscMatlabEngineCreate()` 3055c6c1daeSBarry Smith @*/ 306d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerSocketOpen(MPI_Comm comm, const char machine[], int port, PetscViewer *lab) 307d71ae5a4SJacob Faibussowitsch { 3085c6c1daeSBarry Smith PetscFunctionBegin; 3099566063dSJacob Faibussowitsch PetscCall(PetscViewerCreate(comm, lab)); 3109566063dSJacob Faibussowitsch PetscCall(PetscViewerSetType(*lab, PETSCVIEWERSOCKET)); 3119566063dSJacob Faibussowitsch PetscCall(PetscViewerSocketSetConnection(*lab, machine, port)); 3125c6c1daeSBarry Smith PetscFunctionReturn(0); 3135c6c1daeSBarry Smith } 3145c6c1daeSBarry Smith 315d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscViewerSetFromOptions_Socket(PetscViewer v, PetscOptionItems *PetscOptionsObject) 316d71ae5a4SJacob Faibussowitsch { 3175c6c1daeSBarry Smith PetscInt def = -1; 3185c6c1daeSBarry Smith char sdef[256]; 3195c6c1daeSBarry Smith PetscBool tflg; 3205c6c1daeSBarry Smith 3215c6c1daeSBarry Smith PetscFunctionBegin; 3225c6c1daeSBarry Smith /* 3235c6c1daeSBarry Smith These options are not processed here, they are processed in PetscViewerSocketSetConnection(), they 3245c6c1daeSBarry Smith are listed here for the GUI to display 3255c6c1daeSBarry Smith */ 326d0609cedSBarry Smith PetscOptionsHeadBegin(PetscOptionsObject, "Socket PetscViewer Options"); 3279566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetenv(PetscObjectComm((PetscObject)v), "PETSC_VIEWER_SOCKET_PORT", sdef, 16, &tflg)); 3285c6c1daeSBarry Smith if (tflg) { 3299566063dSJacob Faibussowitsch PetscCall(PetscOptionsStringToInt(sdef, &def)); 330a297a907SKarl Rupp } else def = PETSCSOCKETDEFAULTPORT; 3319566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-viewer_socket_port", "Port number to use for socket", "PetscViewerSocketSetConnection", def, NULL, NULL)); 3325c6c1daeSBarry Smith 3339566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-viewer_socket_machine", "Machine to use for socket", "PetscViewerSocketSetConnection", sdef, NULL, sizeof(sdef), NULL)); 3349566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetenv(PetscObjectComm((PetscObject)v), "PETSC_VIEWER_SOCKET_MACHINE", sdef, sizeof(sdef), &tflg)); 33548a46eb9SPierre Jolivet if (!tflg) PetscCall(PetscGetHostName(sdef, sizeof(sdef))); 336d0609cedSBarry Smith PetscOptionsHeadEnd(); 3375c6c1daeSBarry Smith PetscFunctionReturn(0); 3385c6c1daeSBarry Smith } 3395c6c1daeSBarry Smith 340d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscViewerBinaryGetSkipHeader_Socket(PetscViewer viewer, PetscBool *skip) 341d71ae5a4SJacob Faibussowitsch { 3424c9f2355SSatish Balay PetscViewer_Socket *vsocket = (PetscViewer_Socket *)viewer->data; 3434c9f2355SSatish Balay 3444c9f2355SSatish Balay PetscFunctionBegin; 3454c9f2355SSatish Balay *skip = vsocket->skipheader; 3464c9f2355SSatish Balay PetscFunctionReturn(0); 3474c9f2355SSatish Balay } 3484c9f2355SSatish Balay 349d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscViewerBinarySetSkipHeader_Socket(PetscViewer viewer, PetscBool skip) 350d71ae5a4SJacob Faibussowitsch { 3514c9f2355SSatish Balay PetscViewer_Socket *vsocket = (PetscViewer_Socket *)viewer->data; 3524c9f2355SSatish Balay 3534c9f2355SSatish Balay PetscFunctionBegin; 3544c9f2355SSatish Balay vsocket->skipheader = skip; 3554c9f2355SSatish Balay PetscFunctionReturn(0); 3564c9f2355SSatish Balay } 3574c9f2355SSatish Balay 35876667918SBarry Smith PETSC_INTERN PetscErrorCode PetscViewerBinaryGetFlowControl_Binary(PetscViewer, PetscInt *); 3594c9f2355SSatish Balay 3608556b5ebSBarry Smith /*MC 3618556b5ebSBarry Smith PETSCVIEWERSOCKET - A viewer that writes to a Unix socket 3628556b5ebSBarry Smith 363811af0c4SBarry Smith Level: beginner 364811af0c4SBarry Smith 365*d1f92df0SBarry Smith .seealso: [](sec_viewers), `PETSC_VIEWERBINARY`, `PetscViewerSocketOpen()`, `PetscViewerDrawOpen()`, `PETSC_VIEWER_DRAW_()`, `PETSC_VIEWER_DRAW_SELF`, `PETSC_VIEWER_DRAW_WORLD`, 366db781477SPatrick Sanan `PetscViewerCreate()`, `PetscViewerASCIIOpen()`, `PetscViewerBinaryOpen()`, `PETSCVIEWERBINARY`, `PETSCVIEWERDRAW`, 367db781477SPatrick Sanan `PetscViewerMatlabOpen()`, `VecView()`, `DMView()`, `PetscViewerMatlabPutArray()`, `PETSCVIEWERASCII`, `PETSCVIEWERMATLAB`, 368db781477SPatrick Sanan `PetscViewerFileSetName()`, `PetscViewerFileSetMode()`, `PetscViewerFormat`, `PetscViewerType`, `PetscViewerSetType()` 3698556b5ebSBarry Smith M*/ 3708556b5ebSBarry Smith 371d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscViewerCreate_Socket(PetscViewer v) 372d71ae5a4SJacob Faibussowitsch { 3735c6c1daeSBarry Smith PetscViewer_Socket *vmatlab; 3745c6c1daeSBarry Smith 3755c6c1daeSBarry Smith PetscFunctionBegin; 3764dfa11a4SJacob Faibussowitsch PetscCall(PetscNew(&vmatlab)); 3775c6c1daeSBarry Smith vmatlab->port = 0; 37876667918SBarry Smith vmatlab->flowcontrol = 256; /* same default as in PetscViewerCreate_Binary() */ 3795c6c1daeSBarry Smith v->data = (void *)vmatlab; 3805c6c1daeSBarry Smith v->ops->destroy = PetscViewerDestroy_Socket; 38102c9f0b5SLisandro Dalcin v->ops->flush = NULL; 3825c6c1daeSBarry Smith v->ops->setfromoptions = PetscViewerSetFromOptions_Socket; 3835c6c1daeSBarry Smith 3845c6c1daeSBarry Smith /* lie and say this is a binary viewer; then all the XXXView_Binary() methods will work correctly on it */ 3859566063dSJacob Faibussowitsch PetscCall(PetscObjectChangeTypeName((PetscObject)v, PETSCVIEWERBINARY)); 3869566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerBinarySetSkipHeader_C", PetscViewerBinarySetSkipHeader_Socket)); 3879566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerBinaryGetSkipHeader_C", PetscViewerBinaryGetSkipHeader_Socket)); 38876667918SBarry Smith PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerBinaryGetFlowControl_C", PetscViewerBinaryGetFlowControl_Binary)); 3894c9f2355SSatish Balay 3905c6c1daeSBarry Smith PetscFunctionReturn(0); 3915c6c1daeSBarry Smith } 3925c6c1daeSBarry Smith 3935c6c1daeSBarry Smith /*@C 3945c6c1daeSBarry Smith PetscViewerSocketSetConnection - Sets the machine and port that a PETSc socket 3955c6c1daeSBarry Smith viewer is to use 3965c6c1daeSBarry Smith 397c3339decSBarry Smith Logically Collective 3985c6c1daeSBarry Smith 3995c6c1daeSBarry Smith Input Parameters: 4005c6c1daeSBarry Smith + v - viewer to connect 4010298fd71SBarry Smith . machine - host to connect to, use NULL for the local machine,use "server" to passively wait for 4025c6c1daeSBarry Smith a connection from elsewhere 403811af0c4SBarry Smith - port - the port on the machine one is connecting to, use `PETSC_DEFAULT` for default 4045c6c1daeSBarry Smith 4055c6c1daeSBarry Smith Level: advanced 4065c6c1daeSBarry Smith 407*d1f92df0SBarry Smith .seealso: [](sec_viewers), `PETSCVIEWERMATLAB`, `PETSCVIEWERSOCKET`, `PetscViewerSocketOpen()` 4085c6c1daeSBarry Smith @*/ 409d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerSocketSetConnection(PetscViewer v, const char machine[], int port) 410d71ae5a4SJacob Faibussowitsch { 4115c6c1daeSBarry Smith PetscMPIInt rank; 4125c6c1daeSBarry Smith char mach[256]; 4135c6c1daeSBarry Smith PetscBool tflg; 4143ca90d2dSJacob Faibussowitsch PetscViewer_Socket *vmatlab; 4155c6c1daeSBarry Smith 4165c6c1daeSBarry Smith PetscFunctionBegin; 4173ca90d2dSJacob Faibussowitsch PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 1); 4183ca90d2dSJacob Faibussowitsch if (machine) PetscValidCharPointer(machine, 2); 4193ca90d2dSJacob Faibussowitsch vmatlab = (PetscViewer_Socket *)v->data; 4205c6c1daeSBarry Smith /* PetscValidLogicalCollectiveInt(v,port,3); not a PetscInt */ 4215c6c1daeSBarry Smith if (port <= 0) { 4225c6c1daeSBarry Smith char portn[16]; 4239566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetenv(PetscObjectComm((PetscObject)v), "PETSC_VIEWER_SOCKET_PORT", portn, 16, &tflg)); 4245c6c1daeSBarry Smith if (tflg) { 4255c6c1daeSBarry Smith PetscInt pport; 4269566063dSJacob Faibussowitsch PetscCall(PetscOptionsStringToInt(portn, &pport)); 4275c6c1daeSBarry Smith port = (int)pport; 428a297a907SKarl Rupp } else port = PETSCSOCKETDEFAULTPORT; 4295c6c1daeSBarry Smith } 4305c6c1daeSBarry Smith if (!machine) { 4319566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetenv(PetscObjectComm((PetscObject)v), "PETSC_VIEWER_SOCKET_MACHINE", mach, sizeof(mach), &tflg)); 43248a46eb9SPierre Jolivet if (!tflg) PetscCall(PetscGetHostName(mach, sizeof(mach))); 4335c6c1daeSBarry Smith } else { 4349566063dSJacob Faibussowitsch PetscCall(PetscStrncpy(mach, machine, sizeof(mach))); 4355c6c1daeSBarry Smith } 4365c6c1daeSBarry Smith 4379566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)v), &rank)); 438dd400576SPatrick Sanan if (rank == 0) { 4399566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(mach, "server", &tflg)); 4405c6c1daeSBarry Smith if (tflg) { 4415c6c1daeSBarry Smith int listenport; 4429566063dSJacob Faibussowitsch PetscCall(PetscInfo(v, "Waiting for connection from socket process on port %d\n", port)); 4439566063dSJacob Faibussowitsch PetscCall(PetscSocketEstablish(port, &listenport)); 4449566063dSJacob Faibussowitsch PetscCall(PetscSocketListen(listenport, &vmatlab->port)); 4455c6c1daeSBarry Smith close(listenport); 4465c6c1daeSBarry Smith } else { 4479566063dSJacob Faibussowitsch PetscCall(PetscInfo(v, "Connecting to socket process on port %d machine %s\n", port, mach)); 4489566063dSJacob Faibussowitsch PetscCall(PetscOpenSocket(mach, port, &vmatlab->port)); 4495c6c1daeSBarry Smith } 4505c6c1daeSBarry Smith } 4515c6c1daeSBarry Smith PetscFunctionReturn(0); 4525c6c1daeSBarry Smith } 4535c6c1daeSBarry Smith 4545c6c1daeSBarry Smith /* ---------------------------------------------------------------------*/ 4555c6c1daeSBarry Smith /* 4565c6c1daeSBarry Smith The variable Petsc_Viewer_Socket_keyval is used to indicate an MPI attribute that 4575c6c1daeSBarry Smith is attached to a communicator, in this case the attribute is a PetscViewer. 4585c6c1daeSBarry Smith */ 459d4c7638eSBarry Smith PetscMPIInt Petsc_Viewer_Socket_keyval = MPI_KEYVAL_INVALID; 4605c6c1daeSBarry Smith 4615c6c1daeSBarry Smith /*@C 4625c6c1daeSBarry Smith PETSC_VIEWER_SOCKET_ - Creates a socket viewer shared by all processors in a communicator. 4635c6c1daeSBarry Smith 464d083f849SBarry Smith Collective 4655c6c1daeSBarry Smith 4665c6c1daeSBarry Smith Input Parameter: 467811af0c4SBarry Smith . comm - the MPI communicator to share the `PETSCVIEWERSOCKET` `PetscViewer` 4685c6c1daeSBarry Smith 4695c6c1daeSBarry Smith Level: intermediate 4705c6c1daeSBarry Smith 4715c6c1daeSBarry Smith Options Database Keys: 472811af0c4SBarry Smith For use with the default `PETSC_VIEWER_SOCKET_WORLD` or if 473811af0c4SBarry Smith NULL is passed for machine or `PETSC_DEFAULT` is passed for port 474*d1f92df0SBarry Smith + -viewer_socket_machine <machine> - machine to connect to 475*d1f92df0SBarry Smith - -viewer_socket_port <port> - port to connect to 4765c6c1daeSBarry Smith 4775c6c1daeSBarry Smith Environmental variables: 478811af0c4SBarry Smith + `PETSC_VIEWER_SOCKET_PORT` - portnumber 479811af0c4SBarry Smith - `PETSC_VIEWER_SOCKET_MACHINE` - machine name 4805c6c1daeSBarry Smith 4815c6c1daeSBarry Smith Notes: 482811af0c4SBarry Smith Unlike almost all other PETSc routines, `PETSC_VIEWER_SOCKET_()` does not return 483811af0c4SBarry Smith an error code, it returns NULL if it fails. The `PETSCVIEWERSOCKET` `PetscViewer` is usually used in the form 4845c6c1daeSBarry Smith $ XXXView(XXX object,PETSC_VIEWER_SOCKET_(comm)); 4855c6c1daeSBarry Smith 4865c6c1daeSBarry Smith Currently the only socket client available is MATLAB. See 487c4762a1bSJed Brown src/dm/tests/ex12.c and ex12.m for an example of usage. 4885c6c1daeSBarry Smith 489811af0c4SBarry Smith Connects to a waiting socket and stays connected until `PetscViewerDestroy()` is called. 4905c6c1daeSBarry Smith 491811af0c4SBarry Smith Use this for communicating with an interactive MATLAB session, see `PETSC_VIEWER_MATLAB_()` for writing output to a 492811af0c4SBarry Smith .mat file. Use `PetscMatlabEngineCreate()` or `PETSC_MATLAB_ENGINE_()`, `PETSC_MATLAB_ENGINE_SELF`, or `PETSC_MATLAB_ENGINE_WORLD` 493a30fc760SBarry Smith for communicating with a MATLAB Engine 4945c6c1daeSBarry Smith 495*d1f92df0SBarry Smith .seealso: [](sec_viewers), `PETSCVIEWERMATLAB`, `PETSCVIEWERSOCKET`, `PETSC_VIEWER_SOCKET_WORLD`, `PETSC_VIEWER_SOCKET_SELF`, `PetscViewerSocketOpen()`, `PetscViewerCreate()`, 496db781477SPatrick Sanan `PetscViewerSocketSetConnection()`, `PetscViewerDestroy()`, `PETSC_VIEWER_SOCKET_()`, `PetscViewerBinaryWrite()`, `PetscViewerBinaryRead()`, 497db781477SPatrick Sanan `PetscViewerBinaryWriteStringArray()`, `PetscViewerBinaryGetDescriptor()`, `PETSC_VIEWER_MATLAB_()` 4985c6c1daeSBarry Smith @*/ 499d71ae5a4SJacob Faibussowitsch PetscViewer PETSC_VIEWER_SOCKET_(MPI_Comm comm) 500d71ae5a4SJacob Faibussowitsch { 5015c6c1daeSBarry Smith PetscErrorCode ierr; 5025c6c1daeSBarry Smith PetscBool flg; 5035c6c1daeSBarry Smith PetscViewer viewer; 5045c6c1daeSBarry Smith MPI_Comm ncomm; 5055c6c1daeSBarry Smith 5065c6c1daeSBarry Smith PetscFunctionBegin; 5079371c9d4SSatish Balay ierr = PetscCommDuplicate(comm, &ncomm, NULL); 5089371c9d4SSatish Balay if (ierr) { 5099371c9d4SSatish Balay PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_SOCKET_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_INITIAL, " "); 5109371c9d4SSatish Balay PetscFunctionReturn(NULL); 5119371c9d4SSatish Balay } 5125c6c1daeSBarry Smith if (Petsc_Viewer_Socket_keyval == MPI_KEYVAL_INVALID) { 51302c9f0b5SLisandro Dalcin ierr = MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN, MPI_COMM_NULL_DELETE_FN, &Petsc_Viewer_Socket_keyval, NULL); 5149371c9d4SSatish Balay if (ierr) { 5159371c9d4SSatish Balay PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_SOCKET_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_INITIAL, " "); 5169371c9d4SSatish Balay PetscFunctionReturn(NULL); 5179371c9d4SSatish Balay } 5185c6c1daeSBarry Smith } 51947435625SJed Brown ierr = MPI_Comm_get_attr(ncomm, Petsc_Viewer_Socket_keyval, (void **)&viewer, (int *)&flg); 5209371c9d4SSatish Balay if (ierr) { 5219371c9d4SSatish Balay PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_SOCKET_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_INITIAL, " "); 5229371c9d4SSatish Balay PetscFunctionReturn(NULL); 5239371c9d4SSatish Balay } 5245c6c1daeSBarry Smith if (!flg) { /* PetscViewer not yet created */ 52502c9f0b5SLisandro Dalcin ierr = PetscViewerSocketOpen(ncomm, NULL, 0, &viewer); 5269371c9d4SSatish Balay if (ierr) { 5279371c9d4SSatish Balay PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_SOCKET_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_REPEAT, " "); 5289371c9d4SSatish Balay PetscFunctionReturn(NULL); 5299371c9d4SSatish Balay } 5305c6c1daeSBarry Smith ierr = PetscObjectRegisterDestroy((PetscObject)viewer); 5319371c9d4SSatish Balay if (ierr) { 5329371c9d4SSatish Balay PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_SOCKET_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_REPEAT, " "); 5339371c9d4SSatish Balay PetscFunctionReturn(NULL); 5349371c9d4SSatish Balay } 53547435625SJed Brown ierr = MPI_Comm_set_attr(ncomm, Petsc_Viewer_Socket_keyval, (void *)viewer); 5369371c9d4SSatish Balay if (ierr) { 5379371c9d4SSatish Balay PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_SOCKET_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_INITIAL, " "); 5389371c9d4SSatish Balay PetscFunctionReturn(NULL); 5399371c9d4SSatish Balay } 5405c6c1daeSBarry Smith } 5415c6c1daeSBarry Smith ierr = PetscCommDestroy(&ncomm); 5429371c9d4SSatish Balay if (ierr) { 5439371c9d4SSatish Balay PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_SOCKET_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_REPEAT, " "); 5449371c9d4SSatish Balay PetscFunctionReturn(NULL); 5459371c9d4SSatish Balay } 5465c6c1daeSBarry Smith PetscFunctionReturn(viewer); 5475c6c1daeSBarry Smith } 548