Actual source code: pdisplay.c

petsc-3.3-p7 2013-05-11
  2: #include <petscsys.h>        
  3: #include <stdarg.h>
  4: #if defined(PETSC_HAVE_STDLIB_H)
  5: #include <stdlib.h>
  6: #endif

 10: /*@C
 11:      PetscOptionsGetenv - Gets an environmental variable, broadcasts to all
 12:           processors in communicator from first.

 14:      Collective on MPI_Comm

 16:    Input Parameters:
 17: +    comm - communicator to share variable
 18: .    name - name of environmental variable
 19: -    len - amount of space allocated to hold variable

 21:    Output Parameters:
 22: +    flag - if not PETSC_NULL tells if variable found or not
 23: -    env - value of variable

 25:   Level: advanced

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

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

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

 47:   /* first check options database */
 48:   PetscStrncmp(name,"PETSC_",6,&spetsc);
 49: 
 50:   PetscStrcpy(work,"-");
 51:   if (spetsc) {
 52:     PetscStrcat(work,name+6);
 53:   } else {
 54:     PetscStrcat(work,name);
 55:   }
 56:   PetscStrtolower(work);
 57:   if (env) {
 58:     PetscOptionsGetString(PETSC_NULL,work,env,len,&flg);
 59:     if (flg) {
 60:       if (flag) *flag = PETSC_TRUE;
 61:     } else { /* now check environment */
 62:       PetscMemzero(env,len*sizeof(char));

 64:       MPI_Comm_rank(comm,&rank);
 65:       if (!rank) {
 66:         str = getenv(name);
 67:         if (str) flg = PETSC_TRUE;
 68:         if (str && env) {PetscStrncpy(env,str,len);}
 69:       }
 70:       MPI_Bcast(&flg,1,MPI_INT,0,comm);
 71:       MPI_Bcast(env,len,MPI_CHAR,0,comm);
 72:       if (flag) *flag = flg;
 73:     }
 74:   } else {
 75:     PetscOptionsHasName(PETSC_NULL,work,flag);
 76:   }
 77:   return(0);
 78: }

 80: /*
 81:      PetscSetDisplay - Tries to set the X windows display variable for all processors.
 82:                        The variable PetscDisplay contains the X windows display variable.

 84: */
 85: static char PetscDisplay[256];

 89: static PetscErrorCode PetscWorldIsSingleHost(PetscBool  *onehost)
 90: {
 92:   char           hostname[256],roothostname[256];
 93:   PetscMPIInt    localmatch,allmatch;
 94:   PetscBool      flag;

 97:   PetscGetHostName(hostname,256);
 98:   PetscMemcpy(roothostname,hostname,256);
 99:   MPI_Bcast(roothostname,256,MPI_CHAR,0,PETSC_COMM_WORLD);
100:   PetscStrcmp(hostname,roothostname,&flag);
101:   localmatch = (PetscMPIInt)flag;
102:   MPI_Allreduce(&localmatch,&allmatch,1,MPI_INT,MPI_LAND,PETSC_COMM_WORLD);
103:   *onehost = (PetscBool)allmatch;
104:   return(0);
105: }


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

119:   PetscOptionsGetString(PETSC_NULL,"-display",PetscDisplay,sizeof PetscDisplay,&flag);
120:   if (flag) return(0);

122:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
123:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

125:   PetscWorldIsSingleHost(&singlehost);

127:   str = getenv("DISPLAY");
128:   if (!str) str = ":0.0";
129:   if (str[0] != ':' || singlehost) {
130:     PetscStrncpy(display,str,sizeof display);
131:   } else {
132:     if (!rank) {
133:       size_t len;
134:       PetscGetHostName(display,sizeof display);
135:       PetscStrlen(display,&len);
136:       PetscStrncat(display,str,sizeof display-len-1);
137:     }
138:   }
139:   MPI_Bcast(display,sizeof display,MPI_CHAR,0,PETSC_COMM_WORLD);
140:   PetscMemcpy(PetscDisplay,display,sizeof PetscDisplay);
141:   PetscDisplay[sizeof PetscDisplay-1] = 0;
142:   return(0);
143: }

147: /*
148:      PetscGetDisplay - Gets the display variable for all processors.

150:   Input Parameters:
151: .   n - length of string display

153:   Output Parameters:
154: .   display - the display string

156: */
157: PetscErrorCode  PetscGetDisplay(char display[],size_t n)
158: {

162:   PetscStrncpy(display,PetscDisplay,n);
163:   return(0);
164: }