Actual source code: pdisplay.c

petsc-3.4.4 2014-03-13
  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,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,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:   MPI_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,"-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 (str[0] != ':' || singlehost) {
128:     PetscStrncpy(display,str,sizeof(display));
129:   } else if (!rank) {
130:     size_t len;
131:     PetscGetHostName(display,sizeof(display));
132:     PetscStrlen(display,&len);
133:     PetscStrncat(display,str,sizeof(display)-len-1);
134:   }
135:   MPI_Bcast(display,sizeof(display),MPI_CHAR,0,PETSC_COMM_WORLD);
136:   PetscMemcpy(PetscDisplay,display,sizeof(PetscDisplay));

138:   PetscDisplay[sizeof(PetscDisplay)-1] = 0;
139:   return(0);
140: }

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

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

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

153: */
154: PetscErrorCode  PetscGetDisplay(char display[],size_t n)
155: {

159:   PetscStrncpy(display,PetscDisplay,n);
160:   return(0);
161: }