Actual source code: pdisplay.c

      1: #define PETSC_DLL

     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"

     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: PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetenv(MPI_Comm comm,const char name[],char env[],size_t len,PetscTruth *flag)
     42: {
     44:   PetscMPIInt    rank;
     45:   char           *str,work[256];
     46:   PetscTruth     flg = PETSC_FALSE,spetsc;
     47:

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

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

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

     87: */
     88: static char PetscDisplay[256];

     92: PetscErrorCode PETSC_DLLEXPORT PetscSetDisplay(void)
     93: {
     95:   PetscMPIInt    size,rank;
     96:   PetscTruth     flag;
     97:   char           *str,display[256];

    100:   PetscMemzero(display,256*sizeof(char));
    101:   PetscOptionsGetString(PETSC_NULL,"-display",PetscDisplay,256,&flag);
    102:   if (flag) return(0);

    104:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
    105:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
    106:   if (!rank) {
    107:     str = getenv("DISPLAY");
    108:     if (!str || (str[0] == ':' && size > 1)) {
    109:       PetscGetHostName(display,255);
    110:       PetscStrcat(display,":0.0");
    111:     } else {
    112:       PetscStrncpy(display,str,256);
    113:     }
    114:   }
    115:   MPI_Bcast(display,256,MPI_CHAR,0,PETSC_COMM_WORLD);
    116:   if (rank) {
    117:     str = getenv("DISPLAY");
    118:     /* assume that ssh port forwarding is working */
    119:     if (str && (str[0] != ':')) {
    120:       PetscStrcpy(display,str);
    121:     }
    122:   }
    123:   PetscStrcpy(PetscDisplay,display);
    124:   return(0);
    125: }

    129: /*
    130:      PetscGetDisplay - Gets the display variable for all processors.

    132:   Input Parameters:
    133: .   n - length of string display

    135:   Output Parameters:
    136: .   display - the display string

    138: */
    139: PetscErrorCode PETSC_DLLEXPORT PetscGetDisplay(char display[],size_t n)
    140: {

    144:   PetscStrncpy(display,PetscDisplay,n);
    145:   return(0);
    146: }