Actual source code: pdisplay.c
petsc-3.12.5 2020-03-29
2: #include <petscsys.h>
4: /*@C
5: PetscOptionsGetenv - Gets an environmental variable, broadcasts to all
6: processors in communicator from first.
8: Collective
10: Input Parameters:
11: + comm - communicator to share variable
12: . name - name of environmental variable
13: - len - amount of space allocated to hold variable
15: Output Parameters:
16: + flag - if not NULL tells if variable found or not
17: - env - value of variable
19: Level: advanced
21: Notes:
22: You can also "set" the environmental variable by setting the options database value
23: -name "stringvalue" (with name in lower case). If name begins with PETSC_ this is
24: discarded before checking the database. For example, PETSC_VIEWER_SOCKET_PORT would
25: be given as -viewer_socket_port 9000
27: If comm does not contain the 0th process in the MPIEXEC it is likely on
28: many systems that the environmental variable will not be set unless you
29: put it in a universal location like a .chsrc file
31: @*/
32: PetscErrorCode PetscOptionsGetenv(MPI_Comm comm,const char name[],char env[],size_t len,PetscBool *flag)
33: {
35: PetscMPIInt rank;
36: char *str,work[256];
37: PetscBool flg = PETSC_FALSE,spetsc;
40: /* first check options database */
41: PetscStrncmp(name,"PETSC_",6,&spetsc);
43: PetscStrcpy(work,"-");
44: if (spetsc) {
45: PetscStrcat(work,name+6);
46: } else {
47: PetscStrcat(work,name);
48: }
49: PetscStrtolower(work);
50: if (env) {
51: PetscOptionsGetString(NULL,NULL,work,env,len,&flg);
52: if (flg) {
53: if (flag) *flag = PETSC_TRUE;
54: } else { /* now check environment */
55: PetscArrayzero(env,len);
57: MPI_Comm_rank(comm,&rank);
58: if (!rank) {
59: str = getenv(name);
60: if (str) flg = PETSC_TRUE;
61: if (str && env) {PetscStrncpy(env,str,len);}
62: }
63: MPI_Bcast(&flg,1,MPIU_BOOL,0,comm);
64: MPI_Bcast(env,len,MPI_CHAR,0,comm);
65: if (flag) *flag = flg;
66: }
67: } else {
68: PetscOptionsHasName(NULL,NULL,work,flag);
69: }
70: return(0);
71: }
73: /*
74: PetscSetDisplay - Tries to set the X windows display variable for all processors.
75: The variable PetscDisplay contains the X windows display variable.
77: */
78: static char PetscDisplay[256];
80: static PetscErrorCode PetscWorldIsSingleHost(PetscBool *onehost)
81: {
83: char hostname[256],roothostname[256];
84: PetscMPIInt localmatch,allmatch;
85: PetscBool flag;
88: PetscGetHostName(hostname,sizeof(hostname));
89: PetscMemcpy(roothostname,hostname,sizeof(hostname));
90: MPI_Bcast(roothostname,sizeof(roothostname),MPI_CHAR,0,PETSC_COMM_WORLD);
91: PetscStrcmp(hostname,roothostname,&flag);
93: localmatch = (PetscMPIInt)flag;
95: MPIU_Allreduce(&localmatch,&allmatch,1,MPI_INT,MPI_LAND,PETSC_COMM_WORLD);
97: *onehost = (PetscBool)allmatch;
98: return(0);
99: }
102: PetscErrorCode PetscSetDisplay(void)
103: {
105: PetscMPIInt size,rank;
106: PetscBool flag,singlehost=PETSC_FALSE;
107: char display[sizeof(PetscDisplay)];
108: const char *str;
111: PetscOptionsGetString(NULL,NULL,"-display",PetscDisplay,sizeof(PetscDisplay),&flag);
112: if (flag) return(0);
114: MPI_Comm_size(PETSC_COMM_WORLD,&size);
115: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
117: PetscWorldIsSingleHost(&singlehost);
119: str = getenv("DISPLAY");
120: if (!str) str = ":0.0";
121: #if defined(PETSC_HAVE_X)
122: flag = PETSC_FALSE;
123: PetscOptionsGetBool(NULL,NULL,"-x_virtual",&flag,NULL);
124: if (flag) {
125: /* this is a crude hack, but better than nothing */
126: PetscPOpen(PETSC_COMM_WORLD,NULL,"pkill -9 Xvfb","r",NULL);
127: PetscSleep(1);
128: PetscPOpen(PETSC_COMM_WORLD,NULL,"Xvfb :15 -screen 0 1600x1200x24","r",NULL);
129: PetscSleep(5);
130: str = ":15";
131: }
132: #endif
133: if (str[0] != ':' || singlehost) {
134: PetscStrncpy(display,str,sizeof(display));
135: } else if (!rank) {
136: PetscGetHostName(display,sizeof(display));
137: PetscStrlcat(display,str,sizeof(display));
138: }
139: MPI_Bcast(display,sizeof(display),MPI_CHAR,0,PETSC_COMM_WORLD);
140: PetscMemcpy(PetscDisplay,display,sizeof(PetscDisplay));
142: PetscDisplay[sizeof(PetscDisplay)-1] = 0;
143: return(0);
144: }
146: /*
147: PetscGetDisplay - Gets the display variable for all processors.
149: Input Parameters:
150: . n - length of string display
152: Output Parameters:
153: . display - the display string
155: Options Database:
156: + -display <display> - sets the display to use
157: - -x_virtual - forces use of a X virtual display Xvfb that will not display anything but -draw_save will still work. Xvfb is automatically
158: started up in PetscSetDisplay() with this option
160: */
161: PetscErrorCode PetscGetDisplay(char display[],size_t n)
162: {
166: PetscStrncpy(display,PetscDisplay,n);
167: return(0);
168: }