Actual source code: pdisplay.c

petsc-3.7.3 2016-08-01
Report Typos and Errors
  2: #include <petscsys.h>

  6: /*@C
  7:      PetscOptionsGetenv - Gets an environmental variable, broadcasts to all
  8:           processors in communicator from first.

 10:      Collective on MPI_Comm

 12:    Input Parameters:
 13: +    comm - communicator to share variable
 14: .    name - name of environmental variable
 15: -    len - amount of space allocated to hold variable

 17:    Output Parameters:
 18: +    flag - if not NULL tells if variable found or not
 19: -    env - value of variable

 21:   Level: advanced

 23:    Notes:
 24:     You can also "set" the environmental variable by setting the options database value
 25:     -name "stringvalue" (with name in lower case). If name begins with PETSC_ this is
 26:     discarded before checking the database. For example, PETSC_VIEWER_SOCKET_PORT would
 27:     be given as -viewer_socket_port 9000

 29:     If comm does not contain the 0th process in the MPIEXEC it is likely on
 30:     many systems that the environmental variable will not be set unless you
 31:     put it in a universal location like a .chsrc file

 33: @*/
 34: PetscErrorCode  PetscOptionsGetenv(MPI_Comm comm,const char name[],char env[],size_t len,PetscBool  *flag)
 35: {
 37:   PetscMPIInt    rank;
 38:   char           *str,work[256];
 39:   PetscBool      flg = PETSC_FALSE,spetsc;

 42:   /* first check options database */
 43:   PetscStrncmp(name,"PETSC_",6,&spetsc);

 45:   PetscStrcpy(work,"-");
 46:   if (spetsc) {
 47:     PetscStrcat(work,name+6);
 48:   } else {
 49:     PetscStrcat(work,name);
 50:   }
 51:   PetscStrtolower(work);
 52:   if (env) {
 53:     PetscOptionsGetString(NULL,NULL,work,env,len,&flg);
 54:     if (flg) {
 55:       if (flag) *flag = PETSC_TRUE;
 56:     } else { /* now check environment */
 57:       PetscMemzero(env,len*sizeof(char));

 59:       MPI_Comm_rank(comm,&rank);
 60:       if (!rank) {
 61:         str = getenv(name);
 62:         if (str) flg = PETSC_TRUE;
 63:         if (str && env) {PetscStrncpy(env,str,len);}
 64:       }
 65:       MPI_Bcast(&flg,1,MPIU_BOOL,0,comm);
 66:       MPI_Bcast(env,len,MPI_CHAR,0,comm);
 67:       if (flag) *flag = flg;
 68:     }
 69:   } else {
 70:     PetscOptionsHasName(NULL,NULL,work,flag);
 71:   }
 72:   return(0);
 73: }

 75: /*
 76:      PetscSetDisplay - Tries to set the X windows display variable for all processors.
 77:                        The variable PetscDisplay contains the X windows display variable.

 79: */
 80: static char PetscDisplay[256];

 84: static PetscErrorCode PetscWorldIsSingleHost(PetscBool  *onehost)
 85: {
 87:   char           hostname[256],roothostname[256];
 88:   PetscMPIInt    localmatch,allmatch;
 89:   PetscBool      flag;

 92:   PetscGetHostName(hostname,256);
 93:   PetscMemcpy(roothostname,hostname,256);
 94:   MPI_Bcast(roothostname,256,MPI_CHAR,0,PETSC_COMM_WORLD);
 95:   PetscStrcmp(hostname,roothostname,&flag);

 97:   localmatch = (PetscMPIInt)flag;

 99:   MPIU_Allreduce(&localmatch,&allmatch,1,MPI_INT,MPI_LAND,PETSC_COMM_WORLD);

101:   *onehost = (PetscBool)allmatch;
102:   return(0);
103: }


108: PetscErrorCode  PetscSetDisplay(void)
109: {
111:   PetscMPIInt    size,rank;
112:   PetscBool      flag,singlehost=PETSC_FALSE;
113:   char           display[sizeof(PetscDisplay)];
114:   const char     *str;

117:   PetscOptionsGetString(NULL,NULL,"-display",PetscDisplay,sizeof(PetscDisplay),&flag);
118:   if (flag) return(0);

120:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
121:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

123:   PetscWorldIsSingleHost(&singlehost);

125:   str = getenv("DISPLAY");
126:   if (!str) str = ":0.0";
127: #if defined(PETSC_HAVE_X)
128:   flag = PETSC_FALSE;
129:   PetscOptionsGetBool(NULL,NULL,"-x_virtual",&flag,NULL);
130:   if (flag) {
131:     /*  this is a crude hack, but better than nothing */
132:     PetscPOpen(PETSC_COMM_WORLD,NULL,"pkill -9 Xvfb","r",NULL);
133:     PetscSleep(1);
134:     PetscPOpen(PETSC_COMM_WORLD,NULL,"Xvfb :15 -screen 0 1600x1200x24","r",NULL);
135:     PetscSleep(5);
136:     str  = ":15";
137:   }
138: #endif
139:   if (str[0] != ':' || singlehost) {
140:     PetscStrncpy(display,str,sizeof(display));
141:   } else if (!rank) {
142:     size_t len;
143:     PetscGetHostName(display,sizeof(display));
144:     PetscStrlen(display,&len);
145:     PetscStrncat(display,str,sizeof(display)-len-1);
146:   }
147:   MPI_Bcast(display,sizeof(display),MPI_CHAR,0,PETSC_COMM_WORLD);
148:   PetscMemcpy(PetscDisplay,display,sizeof(PetscDisplay));

150:   PetscDisplay[sizeof(PetscDisplay)-1] = 0;
151:   return(0);
152: }

156: /*
157:      PetscGetDisplay - Gets the display variable for all processors.

159:   Input Parameters:
160: .   n - length of string display

162:   Output Parameters:
163: .   display - the display string

165:   Options Database:
166: +  -display <display> - sets the display to use
167: -  -x_virtual - forces use of a X virtual display Xvfb that will not display anything but -draw_save will still work. Xvfb is automatically
168:                 started up in PetscSetDisplay() with this option

170: */
171: PetscErrorCode  PetscGetDisplay(char display[],size_t n)
172: {

176:   PetscStrncpy(display,PetscDisplay,n);
177:   return(0);
178: }