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