xref: /petsc/src/sys/utils/pdisplay.c (revision 89c53ad22474f0c850ece585b896cc28116c6d0b)
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__ "PetscWorldIsSingleHost"
90 static PetscErrorCode PetscWorldIsSingleHost(PetscTruth *onehost)
91 {
92   PetscErrorCode ierr;
93   char hostname[256],roothostname[256],localmatch,allmatch;
94   PetscTruth flag;
95 
96   PetscFunctionBegin;
97   ierr = PetscGetHostName(hostname,256);CHKERRQ(ierr);
98   ierr = PetscMemcpy(roothostname,hostname,256);CHKERRQ(ierr);
99   ierr = MPI_Bcast(roothostname,256,MPI_CHAR,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
100   ierr = PetscStrcmp(hostname,roothostname,&flag);CHKERRQ(ierr);
101   localmatch = (PetscMPIInt)flag;
102   ierr = MPI_Allreduce(&localmatch,&allmatch,1,MPI_INT,MPI_LAND,PETSC_COMM_WORLD);CHKERRQ(ierr);
103   *onehost = (PetscTruth)allmatch;
104   PetscFunctionReturn(0);
105 }
106 
107 
108 #undef __FUNCT__
109 #define __FUNCT__ "PetscSetDisplay"
110 PetscErrorCode PETSC_DLLEXPORT PetscSetDisplay(void)
111 {
112   PetscErrorCode ierr;
113   PetscMPIInt    size,rank;
114   PetscTruth     flag,singlehost;
115   char           display[256];
116   const char     *str;
117 
118   PetscFunctionBegin;
119   ierr = PetscMemzero(display,256*sizeof(char));CHKERRQ(ierr);
120   ierr = PetscOptionsGetString(PETSC_NULL,"-display",PetscDisplay,256,&flag);CHKERRQ(ierr);
121   if (flag) PetscFunctionReturn(0);
122 
123   ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
124   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
125 
126   ierr = PetscWorldIsSingleHost(&singlehost);CHKERRQ(ierr);
127 
128   str = getenv("DISPLAY");
129   if (!str) str = ":0.0";
130   if (str[0] != ':' || singlehost) {
131     ierr = PetscStrcpy(display,str);CHKERRQ(ierr);
132   } else {
133     if (!rank) {
134       size_t len1,len2;
135       ierr = PetscGetHostName(display,256);CHKERRQ(ierr);
136       ierr = PetscStrlen(display,&len1);CHKERRQ(ierr);
137       ierr = PetscStrlen(str,&len2);CHKERRQ(ierr);
138       ierr = PetscStrncat(display,str,255-(len1+len2));CHKERRQ(ierr);
139     }
140     ierr = MPI_Bcast(display,256,MPI_CHAR,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
141   }
142   ierr = PetscStrcpy(PetscDisplay,display);CHKERRQ(ierr);
143   PetscFunctionReturn(0);
144 }
145 
146 #undef __FUNCT__
147 #define __FUNCT__ "PetscGetDisplay"
148 /*
149      PetscGetDisplay - Gets the display variable for all processors.
150 
151   Input Parameters:
152 .   n - length of string display
153 
154   Output Parameters:
155 .   display - the display string
156 
157 */
158 PetscErrorCode PETSC_DLLEXPORT PetscGetDisplay(char display[],size_t n)
159 {
160   PetscErrorCode ierr;
161 
162   PetscFunctionBegin;
163   ierr = PetscStrncpy(display,PetscDisplay,n);CHKERRQ(ierr);
164   PetscFunctionReturn(0);
165 }
166