Actual source code: pdisplay.c


  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:     PetscStrlcat(work,name+6,sizeof(work));
 46:   } else {
 47:     PetscStrlcat(work,name,sizeof(work));
 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 == 0) {
 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: }

101: PetscErrorCode  PetscSetDisplay(void)
102: {
104:   PetscMPIInt    size,rank;
105:   PetscBool      flag,singlehost=PETSC_FALSE;
106:   char           display[sizeof(PetscDisplay)];
107:   const char     *str;

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

113:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
114:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

116:   PetscWorldIsSingleHost(&singlehost);

118:   str = getenv("DISPLAY");
119:   if (!str) str = ":0.0";
120: #if defined(PETSC_HAVE_X)
121:   flag = PETSC_FALSE;
122:   PetscOptionsGetBool(NULL,NULL,"-x_virtual",&flag,NULL);
123:   if (flag) {
124:     /*  this is a crude hack, but better than nothing */
125:     PetscPOpen(PETSC_COMM_WORLD,NULL,"pkill -9 Xvfb","r",NULL);
126:     PetscSleep(1);
127:     PetscPOpen(PETSC_COMM_WORLD,NULL,"Xvfb :15 -screen 0 1600x1200x24","r",NULL);
128:     PetscSleep(5);
129:     str  = ":15";
130:   }
131: #endif
132:   if (str[0] != ':' || singlehost) {
133:     PetscStrncpy(display,str,sizeof(display));
134:   } else if (rank == 0) {
135:     PetscGetHostName(display,sizeof(display));
136:     PetscStrlcat(display,str,sizeof(display));
137:   }
138:   MPI_Bcast(display,sizeof(display),MPI_CHAR,0,PETSC_COMM_WORLD);
139:   PetscMemcpy(PetscDisplay,display,sizeof(PetscDisplay));

141:   PetscDisplay[sizeof(PetscDisplay)-1] = 0;
142:   return(0);
143: }

145: /*
146:      PetscGetDisplay - Gets the display variable for all processors.

148:   Input Parameters:
149: .   n - length of string display

151:   Output Parameters:
152: .   display - the display string

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

159: */
160: PetscErrorCode  PetscGetDisplay(char display[],size_t n)
161: {

165:   PetscStrncpy(display,PetscDisplay,n);
166:   return(0);
167: }