Actual source code: pdisplay.c
2: #include petsc.h
3: #include petscsys.h
4: #include <stdarg.h>
5: #if defined(PETSC_HAVE_STDLIB_H)
6: #include <stdlib.h>
7: #endif
8: #include "petscfix.h"
12: /*@C
13: PetscOptionsGetenv - Gets an environmental variable, broadcasts to all
14: processors in communicator from first.
16: Collective on MPI_Comm
18: Input Parameters:
19: + comm - communicator to share variable
20: . name - name of environmental variable
21: - len - amount of space allocated to hold variable
23: Output Parameters:
24: + flag - if not PETSC_NULL tells if variable found or not
25: - env - value of variable
27: Level: advanced
29: Notes:
30: You can also "set" the environmental variable by setting the options database value
31: -name "stringvalue" (with name in lower case). If name begins with PETSC_ this is
32: discarded before checking the database. For example, PETSC_VIEWER_SOCKET_PORT would
33: be given as -viewer_socket_port 9000
35: If comm does not contain the 0th process in the MPIRUN it is likely on
36: many systems that the environmental variable will not be set unless you
37: put it in a universal location like a .chsrc file
39: @*/
40: PetscErrorCode PetscOptionsGetenv(MPI_Comm comm,const char name[],char env[],size_t len,PetscTruth *flag)
41: {
43: PetscMPIInt rank;
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[256];
91: PetscErrorCode PetscSetDisplay(void)
92: {
94: PetscMPIInt size,rank;
95: PetscTruth flag;
96: char *str,display[256];
99: PetscMemzero(display,256*sizeof(char));
100: PetscOptionsGetString(PETSC_NULL,"-display",PetscDisplay,256,&flag);
101: if (flag) return(0);
103: MPI_Comm_size(PETSC_COMM_WORLD,&size);
104: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
105: if (!rank) {
106: str = getenv("DISPLAY");
107: if (!str || (str[0] == ':' && size > 1)) {
108: PetscGetHostName(display,124);
109: PetscStrcat(display,":0.0");
110: } else {
111: PetscStrncpy(display,str,256);
112: }
113: }
114: MPI_Bcast(display,256,MPI_CHAR,0,PETSC_COMM_WORLD);
115: if (rank) {
116: str = getenv("DISPLAY");
117: /* assume that ssh port forwarding is working */
118: if (str && (str[0] != ':')) {
119: PetscStrcpy(display,str);
120: }
121: }
122: PetscStrcpy(PetscDisplay,display);
123: return(0);
124: }
128: /*
129: PetscGetDisplay - Gets the display variable for all processors.
131: Input Parameters:
132: . n - length of string display
134: Output Parameters:
135: . display - the display string
137: */
138: PetscErrorCode PetscGetDisplay(char display[],size_t n)
139: {
143: PetscStrncpy(display,PetscDisplay,n);
144: return(0);
145: }