Actual source code: pdisplay.c
1: #define PETSC_DLL
3: #include petscsys.h
4: #include <stdarg.h>
5: #if defined(PETSC_HAVE_STDLIB_H)
6: #include <stdlib.h>
7: #endif
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 MPIEXEC 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: PetscErrorCode PetscOptionsGetenv(MPI_Comm comm,const char name[],char env[],size_t len,PetscTruth *flag)
40: {
42: PetscMPIInt rank;
43: char *str,work[256];
44: PetscTruth flg = PETSC_FALSE,spetsc;
45:
48: /* first check options database */
49: PetscStrncmp(name,"PETSC_",6,&spetsc);
50:
51: PetscStrcpy(work,"-");
52: if (spetsc) {
53: PetscStrcat(work,name+6);
54: } else {
55: PetscStrcat(work,name);
56: }
57: PetscStrtolower(work);
58: if (env) {
59: PetscOptionsGetString(PETSC_NULL,work,env,len,&flg);
60: if (flg) {
61: if (flag) *flag = PETSC_TRUE;
62: } else { /* now check environment */
63: PetscMemzero(env,len*sizeof(char));
65: MPI_Comm_rank(comm,&rank);
66: if (!rank) {
67: str = getenv(name);
68: if (str) flg = PETSC_TRUE;
69: if (str && env) {PetscStrncpy(env,str,len);}
70: }
71: MPI_Bcast(&flg,1,MPI_INT,0,comm);
72: MPI_Bcast(env,len,MPI_CHAR,0,comm);
73: if (flag) *flag = flg;
74: }
75: } else {
76: PetscOptionsHasName(PETSC_NULL,work,flag);
77: }
78: return(0);
79: }
81: /*
82: PetscSetDisplay - Tries to set the X windows display variable for all processors.
83: The variable PetscDisplay contains the X windows display variable.
85: */
86: static char PetscDisplay[256];
90: PetscErrorCode PetscSetDisplay(void)
91: {
93: PetscMPIInt size,rank;
94: PetscTruth flag;
95: char *str,display[256];
98: PetscMemzero(display,256*sizeof(char));
99: PetscOptionsGetString(PETSC_NULL,"-display",PetscDisplay,256,&flag);
100: if (flag) return(0);
102: MPI_Comm_size(PETSC_COMM_WORLD,&size);
103: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
104: if (!rank) {
105: str = getenv("DISPLAY");
106: if (!str || (str[0] == ':' && size > 1)) {
107: PetscGetHostName(display,255);
108: PetscStrcat(display,":0.0");
109: } else {
110: PetscStrncpy(display,str,256);
111: }
112: }
113: MPI_Bcast(display,256,MPI_CHAR,0,PETSC_COMM_WORLD);
114: if (rank) {
115: str = getenv("DISPLAY");
116: /* assume that ssh port forwarding is working */
117: if (str && (str[0] != ':')) {
118: PetscStrcpy(display,str);
119: }
120: }
121: PetscStrcpy(PetscDisplay,display);
122: return(0);
123: }
127: /*
128: PetscGetDisplay - Gets the display variable for all processors.
130: Input Parameters:
131: . n - length of string display
133: Output Parameters:
134: . display - the display string
136: */
137: PetscErrorCode PetscGetDisplay(char display[],size_t n)
138: {
142: PetscStrncpy(display,PetscDisplay,n);
143: return(0);
144: }