Debian Codegraph @

debian/external_deps/fetk/maloc/src/psh/psh.c

raw
1 /*
2 * ***************************************************************************
3 * MALOC = < Minimal Abstraction Layer for Object-oriented C >
4 * Copyright (C) 1994-- Michael Holst
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * rcsid="$Id: psh.c,v 1.41 2010/08/12 05:40:20 fetk Exp $"
21 * ***************************************************************************
22 */
23
24 /*
25 * ***************************************************************************
26 * File: psh.c
27 *
28 * Purpose: Vsh_pshell w/ infrastructure (parallel extension of Vsh_shell).
29 *
30 * Author: Michael Holst
31 * ***************************************************************************
32 */
33
34 #include "psh_p.h"
35
36 VEMBED(rcsid="$Id: psh.c,v 1.41 2010/08/12 05:40:20 fetk Exp $")
37
38 /* parallel shell commands */
39 typedef enum PSH_command {
40 pshcom_none,
41 pshcom_ignore,
42 pshcom_set,
43 pshcom_help,
44 pshcom_vmp_snd,
45 pshcom_vmp_rcv,
46 pshcom_vmp_bar
47 } PSH_command;
48
49 /* local variables we need (THIS CODE IS NOT REENTRANT!) */
50 VPRIVATE Vmp *theeVMP = VNULL;
51 VPRIVATE Vsh *theePSH = VNULL;
52 VPRIVATE int (*theeFunc)(void *thee, int argc, char **argv) = VNULL;
53
54 /*
55 * ***************************************************************************
56 * Routine: Vsh_publishVars
57 *
58 * Purpose: Publish environment variables.
59 *
60 * Author: Michael Holst
61 * ***************************************************************************
62 */
63 VPRIVATE void PSH_publishVars(Vsh *thee)
64 {
65 int i, numVars = 4;
66 typedef struct vshVars {
67 char envi[VMAX_ARGLEN];
68 char valu[VMAX_ARGLEN];
69 char info[VMAX_ARGLEN];
70 } vshVars;
71 vshVars envVars[] = {
72 /* -------- ----- ----------- */
73 /* VARIABLE VALUE EXPLANATION */
74 /* -------- ----- ----------- */
75 /* ===[ VMP=4 ]=== */
76 { "VMP_I", "0",
77 "VMP id (my VMP process number)" },
78 { "VMP_N", "1",
79 "VMP nproc (number of VMP processes in this execution)" },
80 { "VMP_P", "0",
81 "VMP send/recv partner (current VMP send/recv partner)" },
82 { "VMP_F", "-1",
83 "VMP cmd effect (-1=all,0=proc0,1=proc1,...,N=procN)" }
84 };
85
86 /* publish remaining variables */
87 for (i=0; i<numVars; i++) {
88 VASSERT( Vsh_putenv( thee, envVars[i].envi, envVars[i].valu )
89 && Vsh_putenvInfo( thee, envVars[i].envi, envVars[i].info ) );
90 }
91 }
92
93 /*
94 * ***************************************************************************
95 * Routine: PSH_getCmd
96 *
97 * Purpose: Decode the input string into a legal command.
98 *
99 * Author: Michael Holst
100 * ***************************************************************************
101 */
102 VPRIVATE PSH_command PSH_getCmd(int argc, char **argv)
103 {
104 PSH_command theCmd = pshcom_none;
105 if (!strcmp(argv[0],"")) {
106 theCmd = pshcom_none;
107 } else if (!strcmp(argv[0],"set")) {
108 theCmd = pshcom_set;
109 } else if (!strcmp(argv[0],"help")) {
110 theCmd = pshcom_help;
111 } else if (!strcmp(argv[0],"vmp_snd")) {
112 theCmd = pshcom_vmp_snd;
113 } else if (!strcmp(argv[0],"vmp_rcv")) {
114 theCmd = pshcom_vmp_rcv;
115 } else if (!strcmp(argv[0],"vmp_bar")) {
116 theCmd = pshcom_vmp_bar;
117 } else {
118 theCmd = pshcom_none;
119 }
120 return theCmd;
121 }
122
123 /*
124 * ***************************************************************************
125 * Routine: PSH_builtin
126 *
127 * Purpose: Parallel extensions to the vsh.
128 *
129 * Return codes (required):
130 *
131 * rc=0 --> Psh does not know about this command
132 * rc=1 --> Psh handled this command sucessfully
133 * rc=2 --> Psh handled this command sucessfully and wants to exit!
134 *
135 * Author: Michael Holst
136 * ***************************************************************************
137 */
138 VPRIVATE int PSH_builtin(void *pthee, int argc, char **argv)
139 {
140 int rc, me, src, des, bufLen;
141 unsigned int bufLenMessage;
142 char *bufPtr;
143 PSH_command theCmd;
144
145 static int init=0;
146 static char vmp[VMAX_BUFSIZE], vmp_min[VMAX_BUFSIZE];
147 const char *stmp;
148
149 /* one-time intialization */
150 if (!init) {
151 init=1;
152
153 /* make the minimal vmp message (%s slots = 2) */
154 stmp = "%s: pVsh-layer Help Menu: \n"
155 " help vmp --> Help on %s communication commands\n";
156 sprintf(vmp_min,stmp,theePSH->PR,theePSH->PR);
157
158 /* make the full vmp message (%s slots = 1) */
159 stmp = "%s: Parallel shell extensions: \n"
160 " vmp_snd --> VMP send local buffer to selected proc\n"
161 " vmp_rcv --> VMP recv into local buffer\n"
162 " vmp_bar --> VMP synchronization barrier\n";
163 sprintf(vmp,stmp,theePSH->PR);
164
165 /* publish VMP variables we need */
166 PSH_publishVars(theePSH);
167
168 /* initialize VMP variables */
169 if (theeVMP != VNULL) {
170 Vsh_putenvInt(theePSH,"VMP_I",Vmp_rank(theeVMP));
171 Vsh_putenvInt(theePSH,"VMP_N",Vmp_size(theeVMP));
172 } else {
173 Vsh_putenvInt(theePSH,"VMP_I",0);
174 Vsh_putenvInt(theePSH,"VMP_N",1);
175 }
176 }
177
178 /* get the command */
179 theCmd = PSH_getCmd(argc, argv);
180
181 /*
182 * see if we are supposed to execute the command
183 * parallel mode: we execute the command in only three situations:
184 * (1) everyone has the focus (VMP_F==-1)
185 * (2) we alone have the focus (VMP_F==VMP_I)
186 * (3) we are setting the focus (set VMP_F X)
187 */
188 if ( Vsh_getenvInt(theePSH,"VMP_F") == -1 ) {
189 /*
190 * everyone is supposed to execute it
191 */
192 rc = 0;
193 } else if ( Vsh_getenvInt(theePSH,"VMP_F")
194 == Vsh_getenvInt(theePSH,"VMP_I") ) {
195 /*
196 * only we are supposed to execute it
197 */
198 rc = 0;
199 } else if ( (theCmd == pshcom_set)
200 && (!strcmp(argv[1],"VMP_F"))
201 && (argc == 3) ) {
202 /*
203 * OK, it is a legal "set VMP_F X" command.
204 * SO, we ARE supposed to execute it.
205 * NOTE: we won't recognize "set" as one of our
206 * legal commands, so it will fall through
207 * to the base VSH builtin which will deal with it.
208 */
209 rc = 0;
210 } else {
211 /*
212 * we are supposed to ignore it
213 */
214 theCmd = pshcom_ignore;
215 rc = 1;
216 }
217
218 /* have a look at the command if we are not ignoring it */
219 if (theCmd != pshcom_ignore) {
220
221 /* the normal vsh shell gets first shot at the command */
222 if (theeFunc != VNULL) {
223 rc = (*(theeFunc))(pthee,argc,argv);
224 if (rc != 0) return rc;
225 }
226
227 /* decode and execute the command */
228 switch (theCmd) {
229
230 case pshcom_help:
231 if (argc==1) {
232 Vnm_print(1,"%s",vmp_min);
233 rc = 0; /* pretend we didn't see it so subshell can help too */
234 } else if ((argc==2) && (!strcmp(argv[1],"vmp"))) {
235 Vnm_print(1,"%s",vmp);
236 rc = 1;
237 } else {
238 rc = 0; /* pretend we didn't see it so subshell can help too */
239 }
240 break;
241
242 case pshcom_vmp_snd:
243 me = Vsh_getenvInt(theePSH,"VMP_I");
244 des = Vsh_getenvInt(theePSH,"VMP_P");
245 bufLen = theePSH->bufsize;
246 bufPtr = theePSH->buf;
247
248 Vnm_print(2,"Vsh_builtIn: [%d --> %d] sending mesg size=<%d>\n",
249 me, des, bufLen);
250 bufLenMessage = (unsigned int)bufLen;
251 Vmp_send(theeVMP, des, (char*)&bufLenMessage, 4);
252
253 Vnm_print(2,"Vsh_builtIn: [%d --> %d] sending the real mesg.\n",
254 me, des);
255 Vmp_send(theeVMP, des, bufPtr, bufLen);
256 rc = 1;
257 break;
258
259 case pshcom_vmp_rcv:
260 me = Vsh_getenvInt(theePSH,"VMP_I");
261 src = Vsh_getenvInt(theePSH,"VMP_P");
262
263 Vmp_recv(theeVMP, src, (char*)&bufLenMessage, 4);
264 bufLen = (int)bufLenMessage;
265 Vnm_print(2,"Vsh_builtIn: [%d <-- %d] received mesg size=<%d>\n",
266 me, src, bufLen);
267
268 /* bufPtr = Vmem_malloc( theePSH->vmem, bufLen, sizeof(char) ); */
269 bufPtr = calloc( bufLen, sizeof(char) );
270
271 Vmp_recv(theeVMP, src, bufPtr, bufLen);
272 Vnm_print(2,"Vsh_builtIn: [%d <-- %d] received the real mesg.\n",
273 me, src);
274 theePSH->bufsize = bufLen;
275 theePSH->buf = bufPtr;
276 rc = 1;
277 break;
278
279 case pshcom_vmp_bar:
280 Vmp_barr(theeVMP);
281 rc = 1;
282 break;
283
284 case pshcom_ignore:
285 /* truly ignore; keep subsequent layers from trying to execute! */
286 rc = 1;
287 break;
288
289 default:
290 rc = 0;
291 break;
292 }
293 }
294
295 return rc;
296 }
297
298 /*
299 * ***************************************************************************
300 * Routine: Vsh_pshell
301 *
302 * Purpose: Drop-in replacement for <Vsh_shell> giving parallel extensions.
303 *
304 * Author: Michael Holst
305 * ***************************************************************************
306 */
307 VPUBLIC int Vsh_pshell(Vsh *thee, char *pPR, void *pthee,
308 int (*builtin)(void *thee, int argc, char **argv))
309 {
310 int rc;
311
312 /* don't let shell process args (e.g. MPI wants to use argc/argv itself) */
313 thee->processArgs = 0;
314
315 /* save the function pointer to be called by our parallel layer */
316 theeFunc = builtin;
317 theePSH = thee;
318
319 /* create communication object (do first so file i/o gets tagged) */
320 theeVMP = Vmp_ctor();
321
322 /* start the vsh, slipping our extra parallel layer in */
323 rc = Vsh_shell(thee, pPR, pthee, &PSH_builtin);
324
325 /* destroy communication object */
326 Vmp_dtor( &(theeVMP) );
327
328 /* return */
329 return rc;
330 }
331