xref: /petsc/src/sys/utils/pdisplay.c (revision a58c3bc3391eee32bc3fd94ac7edeea38fe57aae)
1 #define PETSC_DLL
2 
3 #include "petsc.h"
4 #include "petscsys.h"             /*I    "petscsys.h"   I*/
5 #include <stdarg.h>
6 #if defined(PETSC_HAVE_STDLIB_H)
7 #include <stdlib.h>
8 #endif
9 #include "petscfix.h"
10 
11 #undef __FUNCT__
12 #define __FUNCT__ "PetscOptionsGetenv"
13 /*@C
14      PetscOptionsGetenv - Gets an environmental variable, broadcasts to all
15           processors in communicator from first.
16 
17      Collective on MPI_Comm
18 
19    Input Parameters:
20 +    comm - communicator to share variable
21 .    name - name of environmental variable
22 -    len - amount of space allocated to hold variable
23 
24    Output Parameters:
25 +    flag - if not PETSC_NULL tells if variable found or not
26 -    env - value of variable
27 
28   Level: advanced
29 
30    Notes:
31     You can also "set" the environmental variable by setting the options database value
32     -name "stringvalue" (with name in lower case). If name begins with PETSC_ this is
33     discarded before checking the database. For example, PETSC_VIEWER_SOCKET_PORT would
34     be given as -viewer_socket_port 9000
35 
36     If comm does not contain the 0th process in the MPIEXEC it is likely on
37     many systems that the environmental variable will not be set unless you
38     put it in a universal location like a .chsrc file
39 
40 @*/
41 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetenv(MPI_Comm comm,const char name[],char env[],size_t len,PetscTruth *flag)
42 {
43   PetscErrorCode ierr;
44   PetscMPIInt    rank;
45   char           *str,work[256];
46   PetscTruth     flg = PETSC_FALSE,spetsc;
47 
48   PetscFunctionBegin;
49 
50   /* first check options database */
51   ierr = PetscStrncmp(name,"PETSC_",6,&spetsc);CHKERRQ(ierr);
52 
53   ierr = PetscStrcpy(work,"-");CHKERRQ(ierr);
54   if (spetsc) {
55     ierr = PetscStrcat(work,name+6);CHKERRQ(ierr);
56   } else {
57     ierr = PetscStrcat(work,name);CHKERRQ(ierr);
58   }
59   ierr = PetscStrtolower(work);CHKERRQ(ierr);
60   if (env) {
61     ierr = PetscOptionsGetString(PETSC_NULL,work,env,len,&flg);CHKERRQ(ierr);
62     if (flg) {
63       if (flag) *flag = PETSC_TRUE;
64     } else { /* now check environment */
65       ierr = PetscMemzero(env,len*sizeof(char));CHKERRQ(ierr);
66 
67       ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
68       if (!rank) {
69 	str = getenv(name);
70 	if (str) flg = PETSC_TRUE;
71 	if (str && env) {ierr = PetscStrncpy(env,str,len);CHKERRQ(ierr);}
72       }
73       ierr = MPI_Bcast(&flg,1,MPI_INT,0,comm);CHKERRQ(ierr);
74       ierr = MPI_Bcast(env,len,MPI_CHAR,0,comm);CHKERRQ(ierr);
75       if (flag) *flag = flg;
76     }
77   } else {
78     ierr = PetscOptionsHasName(PETSC_NULL,work,flag);CHKERRQ(ierr);
79   }
80   PetscFunctionReturn(0);
81 }
82 
83 /*
84      PetscSetDisplay - Tries to set the X windows display variable for all processors.
85                        The variable PetscDisplay contains the X windows display variable.
86 
87 */
88 static char PetscDisplay[256];
89 
90 #undef __FUNCT__
91 #define __FUNCT__ "PetscSetDisplay"
92 PetscErrorCode PETSC_DLLEXPORT PetscSetDisplay(void)
93 {
94   PetscErrorCode ierr;
95   PetscMPIInt    size,rank;
96   PetscTruth     flag;
97   char           *str,display[256];
98 
99   PetscFunctionBegin;
100   ierr = PetscMemzero(display,256*sizeof(char));CHKERRQ(ierr);
101   ierr = PetscOptionsGetString(PETSC_NULL,"-display",PetscDisplay,256,&flag);CHKERRQ(ierr);
102   if (flag) PetscFunctionReturn(0);
103 
104   ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
105   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
106   if (!rank) {
107     str = getenv("DISPLAY");
108     if (!str || (str[0] == ':' && size > 1)) {
109       ierr = PetscGetHostName(display,255);CHKERRQ(ierr);
110       ierr = PetscStrcat(display,":0.0");CHKERRQ(ierr);
111     } else {
112       ierr = PetscStrncpy(display,str,256);CHKERRQ(ierr);
113     }
114   }
115   ierr = MPI_Bcast(display,256,MPI_CHAR,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
116   if (rank) {
117     str = getenv("DISPLAY");
118     /* assume that ssh port forwarding is working */
119     if (str && (str[0] != ':')) {
120       ierr = PetscStrcpy(display,str);CHKERRQ(ierr);
121     }
122   }
123   ierr = PetscStrcpy(PetscDisplay,display);CHKERRQ(ierr);
124   PetscFunctionReturn(0);
125 }
126 
127 #undef __FUNCT__
128 #define __FUNCT__ "PetscGetDisplay"
129 /*
130      PetscGetDisplay - Gets the display variable for all processors.
131 
132   Input Parameters:
133 .   n - length of string display
134 
135   Output Parameters:
136 .   display - the display string
137 
138 */
139 PetscErrorCode PETSC_DLLEXPORT PetscGetDisplay(char display[],size_t n)
140 {
141   PetscErrorCode ierr;
142 
143   PetscFunctionBegin;
144   ierr = PetscStrncpy(display,PetscDisplay,n);CHKERRQ(ierr);
145   PetscFunctionReturn(0);
146 }
147