Actual source code: pdisplay.c

  1: /*$Id: pdisplay.c,v 1.24 2001/06/21 21:15:41 bsmith Exp $*/

 3:  #include petsc.h
 4:  #include petscsys.h
  5: #include <stdarg.h>
  6: #if defined(PETSC_HAVE_STDLIB_H)
  7: #include <stdlib.h>
  8: #endif
  9: #include "petscfix.h"

 11: #undef __FUNCT__  
 13: /*@C
 14:      PetscOptionsGetenv - Gets an environmental variable, broadcasts to all
 15:           processors in communicator from first.

 17:      Collective on MPI_Comm

 19:    Input Parameters:
 20: +    comm - communicator to share variable
 21: .    name - name of environmental variable
 22: -    len - amount of space allocated to hold variable

 24:    Output Parameters:
 25: +    flag - if not PETSC_NULL tells if variable found or not
 26: -    env - value of variable

 28:   Level: advanced

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

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

 40: @*/
 41: int PetscOptionsGetenv(MPI_Comm comm,const char *name,char env[],int len,PetscTruth *flag)
 42: {
 43:   int        rank,ierr;
 44:   char       *str,work[256];
 45:   PetscTruth flg = PETSC_FALSE,spetsc;
 46: 

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

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

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

 86: */
 87: static char PetscDisplay[128];

 89: #undef __FUNCT__  
 91: int PetscSetDisplay(void)
 92: {
 93:   int        size,rank,len,ierr;
 94:   PetscTruth flag;
 95:   char       *str,display[128];

 98:   PetscOptionsGetString(PETSC_NULL,"-display",PetscDisplay,128,&flag);
 99:   if (flag) return(0);

101:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
102:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
103:   if (!rank) {
104:     str = getenv("DISPLAY");
105:     if (!str || (str[0] == ':' && size > 1)) {
106:       PetscGetHostName(display,124);
107:       PetscStrcat(display,":0.0");
108:     } else {
109:       PetscStrncpy(display,str,128);
110:     }
111:     PetscStrlen(display,&len);
112:   }
113:   MPI_Bcast(&len,1,MPI_INT,0,PETSC_COMM_WORLD);
114:   MPI_Bcast(display,len,MPI_CHAR,0,PETSC_COMM_WORLD);
115:   display[len] = 0;
116:   PetscStrcpy(PetscDisplay,display);
117:   return(0);
118: }

120: #undef __FUNCT__  
122: /*
123:      PetscGetDisplay - Gets the display variable for all processors.

125:   Input Parameters:
126: .   n - length of string display

128:   Output Parameters:
129: .   display - the display string

131: */
132: int PetscGetDisplay(char display[],int n)
133: {

137:   PetscStrncpy(display,PetscDisplay,n);
138:   return(0);
139: }