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: /*@C
 12:      PetscOptionsGetenv - Gets an environmental variable, broadcasts to all
 13:           processors in communicator from first.

 15:      Collective on MPI_Comm

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

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

 26:   Level: advanced

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

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

 38: @*/
 39: int PetscOptionsGetenv(MPI_Comm comm,const char *name,char env[],int len,PetscTruth *flag)
 40: {
 41:   int        rank,ierr;
 42:   char       *str,work[256];
 43:   PetscTruth 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[128];

 87: int PetscSetDisplay(void)
 88: {
 89:   int        size,rank,len,ierr;
 90:   PetscTruth flag;
 91:   char       *str,display[128];

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

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

116: /*
117:      PetscGetDisplay - Gets the display variable for all processors.

119:   Input Parameters:
120: .   n - length of string display

122:   Output Parameters:
123: .   display - the display string

125: */
126: int PetscGetDisplay(char display[],int n)
127: {

131:   PetscStrncpy(display,PetscDisplay,n);
132:   return(0);
133: }