00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "psh_p.h"
00035
00036 VEMBED(rcsid="$Id: psh.c,v 1.40 2008/03/12 05:13:58 fetk Exp $")
00037
00038
00039 typedef enum PSH_command {
00040 pshcom_none,
00041 pshcom_ignore,
00042 pshcom_set,
00043 pshcom_help,
00044 pshcom_vmp_snd,
00045 pshcom_vmp_rcv,
00046 pshcom_vmp_bar
00047 } PSH_command;
00048
00049
00050 VPRIVATE Vmp *theeVMP = VNULL;
00051 VPRIVATE Vsh *theePSH = VNULL;
00052 VPRIVATE int (*theeFunc)(void *thee, int argc, char **argv) = VNULL;
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 VPRIVATE void PSH_publishVars(Vsh *thee)
00064 {
00065 int i, numVars = 4;
00066 typedef struct vshVars {
00067 char envi[VMAX_ARGLEN];
00068 char valu[VMAX_ARGLEN];
00069 char info[VMAX_ARGLEN];
00070 } vshVars;
00071 vshVars envVars[] = {
00072
00073
00074
00075
00076 { "VMP_I", "0",
00077 "VMP id (my VMP process number)" },
00078 { "VMP_N", "1",
00079 "VMP nproc (number of VMP processes in this execution)" },
00080 { "VMP_P", "0",
00081 "VMP send/recv partner (current VMP send/recv partner)" },
00082 { "VMP_F", "-1",
00083 "VMP cmd effect (-1=all,0=proc0,1=proc1,...,N=procN)" }
00084 };
00085
00086
00087 for (i=0; i<numVars; i++) {
00088 VASSERT( Vsh_putenv( thee, envVars[i].envi, envVars[i].valu )
00089 && Vsh_putenvInfo( thee, envVars[i].envi, envVars[i].info ) );
00090 }
00091 }
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102 VPRIVATE PSH_command PSH_getCmd(int argc, char **argv)
00103 {
00104 PSH_command theCmd = pshcom_none;
00105 if (!strcmp(argv[0],"")) {
00106 theCmd = pshcom_none;
00107 } else if (!strcmp(argv[0],"set")) {
00108 theCmd = pshcom_set;
00109 } else if (!strcmp(argv[0],"help")) {
00110 theCmd = pshcom_help;
00111 } else if (!strcmp(argv[0],"vmp_snd")) {
00112 theCmd = pshcom_vmp_snd;
00113 } else if (!strcmp(argv[0],"vmp_rcv")) {
00114 theCmd = pshcom_vmp_rcv;
00115 } else if (!strcmp(argv[0],"vmp_bar")) {
00116 theCmd = pshcom_vmp_bar;
00117 } else {
00118 theCmd = pshcom_none;
00119 }
00120 return theCmd;
00121 }
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 VPRIVATE int PSH_builtin(void *pthee, int argc, char **argv)
00139 {
00140 int rc, me, src, des, bufLen;
00141 unsigned int bufLenMessage;
00142 char *bufPtr;
00143 PSH_command theCmd;
00144
00145 static int init=0;
00146 static char vmp[VMAX_BUFSIZE], vmp_min[VMAX_BUFSIZE];
00147 const char *stmp;
00148
00149
00150 if (!init) {
00151 init=1;
00152
00153
00154 stmp = "%s: pVsh-layer Help Menu: \n"
00155 " help vmp --> Help on %s communication commands\n";
00156 sprintf(vmp_min,stmp,theePSH->PR,theePSH->PR);
00157
00158
00159 stmp = "%s: Parallel shell extensions: \n"
00160 " vmp_snd --> VMP send local buffer to selected proc\n"
00161 " vmp_rcv --> VMP recv into local buffer\n"
00162 " vmp_bar --> VMP synchronization barrier\n";
00163 sprintf(vmp,stmp,theePSH->PR);
00164
00165
00166 PSH_publishVars(theePSH);
00167
00168
00169 if (theeVMP != VNULL) {
00170 Vsh_putenvInt(theePSH,"VMP_I",Vmp_rank(theeVMP));
00171 Vsh_putenvInt(theePSH,"VMP_N",Vmp_size(theeVMP));
00172 } else {
00173 Vsh_putenvInt(theePSH,"VMP_I",0);
00174 Vsh_putenvInt(theePSH,"VMP_N",1);
00175 }
00176 }
00177
00178
00179 theCmd = PSH_getCmd(argc, argv);
00180
00181
00182
00183
00184
00185
00186
00187
00188 if ( Vsh_getenvInt(theePSH,"VMP_F") == -1 ) {
00189
00190
00191
00192 rc = 0;
00193 } else if ( Vsh_getenvInt(theePSH,"VMP_F")
00194 == Vsh_getenvInt(theePSH,"VMP_I") ) {
00195
00196
00197
00198 rc = 0;
00199 } else if ( (theCmd == pshcom_set)
00200 && (!strcmp(argv[1],"VMP_F"))
00201 && (argc == 3) ) {
00202
00203
00204
00205
00206
00207
00208
00209 rc = 0;
00210 } else {
00211
00212
00213
00214 theCmd = pshcom_ignore;
00215 rc = 1;
00216 }
00217
00218
00219 if (theCmd != pshcom_ignore) {
00220
00221
00222 if (theeFunc != VNULL) {
00223 rc = (*(theeFunc))(pthee,argc,argv);
00224 if (rc != 0) return rc;
00225 }
00226
00227
00228 switch (theCmd) {
00229
00230 case pshcom_help:
00231 if (argc==1) {
00232 Vnm_print(1,"%s",vmp_min);
00233 rc = 0;
00234 } else if ((argc==2) && (!strcmp(argv[1],"vmp"))) {
00235 Vnm_print(1,"%s",vmp);
00236 rc = 1;
00237 } else {
00238 rc = 0;
00239 }
00240 break;
00241
00242 case pshcom_vmp_snd:
00243 me = Vsh_getenvInt(theePSH,"VMP_I");
00244 des = Vsh_getenvInt(theePSH,"VMP_P");
00245 bufLen = theePSH->bufsize;
00246 bufPtr = theePSH->buf;
00247
00248 Vnm_print(2,"Vsh_builtIn: [%d --> %d] sending mesg size=<%d>\n",
00249 me, des, bufLen);
00250 bufLenMessage = (unsigned int)bufLen;
00251 Vmp_send(theeVMP, des, (char*)&bufLenMessage, 4);
00252
00253 Vnm_print(2,"Vsh_builtIn: [%d --> %d] sending the real mesg.\n",
00254 me, des);
00255 Vmp_send(theeVMP, des, bufPtr, bufLen);
00256 rc = 1;
00257 break;
00258
00259 case pshcom_vmp_rcv:
00260 me = Vsh_getenvInt(theePSH,"VMP_I");
00261 src = Vsh_getenvInt(theePSH,"VMP_P");
00262
00263 Vmp_recv(theeVMP, src, (char*)&bufLenMessage, 4);
00264 bufLen = (int)bufLenMessage;
00265 Vnm_print(2,"Vsh_builtIn: [%d <-- %d] received mesg size=<%d>\n",
00266 me, src, bufLen);
00267
00268
00269 bufPtr = calloc( bufLen, sizeof(char) );
00270
00271 Vmp_recv(theeVMP, src, bufPtr, bufLen);
00272 Vnm_print(2,"Vsh_builtIn: [%d <-- %d] received the real mesg.\n",
00273 me, src);
00274 theePSH->bufsize = bufLen;
00275 theePSH->buf = bufPtr;
00276 rc = 1;
00277 break;
00278
00279 case pshcom_vmp_bar:
00280 Vmp_barr(theeVMP);
00281 rc = 1;
00282 break;
00283
00284 case pshcom_ignore:
00285
00286 rc = 1;
00287 break;
00288
00289 default:
00290 rc = 0;
00291 break;
00292 }
00293 }
00294
00295 return rc;
00296 }
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307 VPUBLIC int Vsh_pshell(Vsh *thee, char *pPR, void *pthee,
00308 int (*builtin)(void *thee, int argc, char **argv))
00309 {
00310 int rc;
00311
00312
00313 thee->processArgs = 0;
00314
00315
00316 theeFunc = builtin;
00317 theePSH = thee;
00318
00319
00320 theeVMP = Vmp_ctor();
00321
00322
00323 rc = Vsh_shell(thee, pPR, pthee, &PSH_builtin);
00324
00325
00326 Vmp_dtor( &(theeVMP) );
00327
00328
00329 return rc;
00330 }
00331