1 | /** \file opt.c
|
---|
2 | * Option Retrieving and debugger.
|
---|
3 | * This file contains routines that read options from command line GetOptions(),
|
---|
4 | * set the verbosity level SetOutGroup1() and individually for each process
|
---|
5 | * SetOutGroup2() and starting the various debuggers Start_cvd(), Start_dbx(),
|
---|
6 | * Start_ddd(), Start_gdb(), Start_totalview(), Start_ups(), StartDebugger().
|
---|
7 | * And deallocating the debug string FreeDebugStr().
|
---|
8 | *
|
---|
9 | * \date 1998-2000
|
---|
10 | *
|
---|
11 | * $Id: opt.c,v 1.15 2007-02-12 09:44:56 foo Exp $
|
---|
12 | */
|
---|
13 |
|
---|
14 | #include<stdio.h>
|
---|
15 | #include<stdlib.h>
|
---|
16 | #include<string.h>
|
---|
17 | #include<unistd.h>
|
---|
18 | #include<sys/utsname.h>
|
---|
19 | #include"data.h"
|
---|
20 | #include"helpers.h"
|
---|
21 | #include"opt.h"
|
---|
22 |
|
---|
23 | static char *DebugString = NULL; //!< command line option
|
---|
24 | static char *program_name = NULL;//<! copy of argv[0]
|
---|
25 | static int debugger_started = 0; //!< "debugger been started?"-flag (0 - no, 1 - yes)
|
---|
26 |
|
---|
27 | /** Free debug string memory .
|
---|
28 | * Frees memory for DebugString and program_name
|
---|
29 | */
|
---|
30 | static void FreeDebugStr(void) {
|
---|
31 | if (DebugString) Free(DebugString, "FreeDebugStr: DebugString");
|
---|
32 | if (program_name) Free(program_name, "FreeDebugStr: program_name");
|
---|
33 | }
|
---|
34 |
|
---|
35 | /*extern char *optarg;
|
---|
36 | extern int optind;*/
|
---|
37 |
|
---|
38 | /** Reads options from commandline and fills \ref CallOptions.
|
---|
39 | * uses getopt in order to retrieve options from command line, including
|
---|
40 | * most importantly the main paramter file. The rest is just verbosity,
|
---|
41 | * debugging, alarm timer and source file for restarting calculations.\n
|
---|
42 | * Also usage is printed on request
|
---|
43 | *
|
---|
44 | * \param *Call \ref CallOptions structure to be filled
|
---|
45 | * \param argc argument count and ...
|
---|
46 | * \param argv[] .. argument const char array right from command line
|
---|
47 | */
|
---|
48 | void GetOptions(struct CallOptions *Call, int argc, char *const argv[]) {
|
---|
49 | /* parsen der Kommandozeile */
|
---|
50 | int c, errflg = 0;
|
---|
51 |
|
---|
52 | /* defaults setzen */
|
---|
53 | Call->MainParameterFile = NULL;
|
---|
54 | Call->debug = 0;
|
---|
55 | Call->nicelevel = 0;
|
---|
56 | Call->Out = 0;
|
---|
57 | Call->alarm = 0;
|
---|
58 | Call->proc[PEPsi] = 0;
|
---|
59 | Call->proc[PEGamma] = 0;
|
---|
60 | Call->ReadSrcFiles = 0;
|
---|
61 | Call->WriteSrcFiles = 0;
|
---|
62 | Call->AddNFactor = 1;
|
---|
63 |
|
---|
64 | while ((c = getopt(argc, argv, "a:d:D:hn:o:p:m:rvwx")) != -1) {
|
---|
65 | switch (c) {
|
---|
66 | case 'a':
|
---|
67 | Call->alarm = abs(atoi(optarg));
|
---|
68 | break;
|
---|
69 | case 'd':
|
---|
70 | if (DebugString) Free(DebugString, "GetOptions: DebugString");
|
---|
71 | Call->debug = 1;
|
---|
72 | DebugString = (char *)Malloc(strlen(optarg)+1, "GetOptions");
|
---|
73 | strcpy(DebugString, optarg);
|
---|
74 | break;
|
---|
75 | case 'D':
|
---|
76 | if (DebugString) Free(DebugString, "GetOptions: DebugString");
|
---|
77 | DebugString = (char *)Malloc(strlen(optarg)+1, "GetOptions");
|
---|
78 | strcpy(DebugString, optarg);
|
---|
79 | Call->debug = 2;
|
---|
80 | break;
|
---|
81 | case 'h':
|
---|
82 | printf("Usage: %s [-hvxrw ] [-a alarmtime] [-d debugstr|-D debugstr] [-n nicelevel] [-m addNfactor]\n","pcp");
|
---|
83 | printf(" [-o verbosity] mainparameterfile\n");
|
---|
84 | printf(" -a alarmtime Sets alarm to alarmtime seconds. Code will finish after\n");
|
---|
85 | printf(" next timestep, writing out the current state.\n");
|
---|
86 | printf(" -d debugstr Starts debugger right away.\n");
|
---|
87 | printf(" -D debugstr Starts debugger when encountering an error.\n");
|
---|
88 | printf(" Valid syntax for debugstr:\n");
|
---|
89 | printf(" 0 sleep for a minute, no debugger\n");
|
---|
90 | printf(" [host[:display][,debugger]] start debugger xterm on host:display\n");
|
---|
91 | printf(" (if DISPLAY is set or for local connection set host to local).\n");
|
---|
92 | printf(" Valid values for debugger are: gdb, dbx, ddd, cvd, totalview, ups.\n");
|
---|
93 | printf(" -h Displays this help page and exits successfully.\n");
|
---|
94 | printf(" -n nicelevel Decreases priority of process.\n");
|
---|
95 | printf(" -o verbosity Sets verbosity level. Deprecated, use -v instead.\n");
|
---|
96 | printf(" Possible values include: 0-5.\n");
|
---|
97 | printf(" -v Increases verbosity level by one. Default value: 0.\n");
|
---|
98 | printf(" -p n,m procs per Gamma-point, procs per psi\n");
|
---|
99 | printf(" No.of PE = n * m\n");
|
---|
100 | printf(" -r Read old src files.\n");
|
---|
101 | printf(" -w Write src files.\n");
|
---|
102 | printf(" -m f Additional NFactor - to fix read srcpsi\n");
|
---|
103 | printf("For parallel codes it is necessary to specify the main parameter file\n");
|
---|
104 | printf("with an absolute path.\n");
|
---|
105 | exit(EXIT_SUCCESS);
|
---|
106 | case 'n':
|
---|
107 | Call->nicelevel = abs(atoi(optarg));
|
---|
108 | break;
|
---|
109 | case 'o':
|
---|
110 | Call->Out = abs(atoi(optarg));
|
---|
111 | break;
|
---|
112 | case 'p':
|
---|
113 | if (sscanf(optarg, "%d,%d", &Call->proc[PEPsi], &Call->proc[PEGamma]) < 2)
|
---|
114 | errflg++;
|
---|
115 | if (Call->proc[PEPsi] < 1 || Call->proc[PEGamma] < 1) {
|
---|
116 | fprintf(stderr, "proc[i] must be > 0\n");
|
---|
117 | errflg++;
|
---|
118 | }
|
---|
119 | break;
|
---|
120 | case 'r':
|
---|
121 | Call->ReadSrcFiles++;
|
---|
122 | break;
|
---|
123 | case 'v':
|
---|
124 | Call->Out++;
|
---|
125 | break;
|
---|
126 | case 'w':
|
---|
127 | Call->WriteSrcFiles++;
|
---|
128 | break;
|
---|
129 | case 'm':
|
---|
130 | if (sscanf(optarg, "%d", &Call->AddNFactor) < 1)
|
---|
131 | errflg++;
|
---|
132 | if (Call->AddNFactor <= 1) {
|
---|
133 | fprintf(stderr, "AddNFactor must be > 0\n");
|
---|
134 | errflg++;
|
---|
135 | }
|
---|
136 | break;
|
---|
137 | case '?':
|
---|
138 | errflg++;
|
---|
139 | break;
|
---|
140 | default:
|
---|
141 | fprintf(stderr, "GetOptions: getopt() returned character code O%o !?\n", c);
|
---|
142 | errflg++;
|
---|
143 | }
|
---|
144 | }
|
---|
145 | if (errflg) {
|
---|
146 | fprintf(stderr,"Usage: %s [ OPTIONS ] mainparameterfile\nTry '%s -h' for more information.\n", argv[0], argv[0]);
|
---|
147 | exit(EXIT_FAILURE);
|
---|
148 | }
|
---|
149 | if ( optind < argc - 1)
|
---|
150 | fprintf(stderr, "Warning: more than one file specified\n");
|
---|
151 | for ( ; optind < argc ; optind++) {
|
---|
152 | Call->MainParameterFile = (char *)Malloc(strlen(argv[optind])+1, "GetOptions: MainParameterFile");
|
---|
153 | strcpy(Call->MainParameterFile, argv[optind]);
|
---|
154 | }
|
---|
155 |
|
---|
156 | /* nun auswerten */
|
---|
157 | program_name = (char *)Malloc(strlen(argv[0]) + 1, "GetOptions");
|
---|
158 | strcpy(program_name, argv[0]);
|
---|
159 | if (Call->alarm) alarm(Call->alarm);
|
---|
160 | if (Call->nicelevel) nice(Call->nicelevel);
|
---|
161 | if (!Call->MainParameterFile) {
|
---|
162 | fprintf(stderr, "Did not specify a main parameter file.\n");
|
---|
163 | exit(EXIT_FAILURE);
|
---|
164 | }
|
---|
165 | atexit(FreeDebugStr); /* nachher aufraeumen */
|
---|
166 | }
|
---|
167 |
|
---|
168 | /** Starts DDD Debugger.
|
---|
169 | * Via a system call the debugger is started with certain paramaters
|
---|
170 | * \param *Host hostname (if not specified, then launched via \a pid)
|
---|
171 | * \param *program program name
|
---|
172 | * \param pid Process ID of te program
|
---|
173 | * \return return value of launched debugger
|
---|
174 | */
|
---|
175 | static int Start_ddd(char *Host, char *program, pid_t pid) {
|
---|
176 | char s[MAXDUMMYSTRING];
|
---|
177 | if (Host) sprintf(s, "ddd -display %s %s %ld &", Host, program, (long int)pid);
|
---|
178 | else sprintf(s, "ddd %s %ld &", program, (long int)pid);
|
---|
179 | return system(s);
|
---|
180 | }
|
---|
181 |
|
---|
182 | /** Starts GDB Debugger.
|
---|
183 | * Via a system call the debugger is started with certain paramaters
|
---|
184 | * \param *Host hostname (if not specified, then launched via \a pid)
|
---|
185 | * \param *program program name
|
---|
186 | * \param pid Process ID of te program
|
---|
187 | * \return return value of launched debugger
|
---|
188 | */
|
---|
189 | static int Start_gdb(char *Host, char *program, pid_t pid) {
|
---|
190 | char s[MAXDUMMYSTRING];
|
---|
191 | if (Host) sprintf(s, "xterm -display %s -e gdb %s %ld &", Host, program, (long int)pid);
|
---|
192 | else sprintf(s, "xterm -e gdb %s %ld &", program, (long int)pid);
|
---|
193 | return system(s);
|
---|
194 | }
|
---|
195 |
|
---|
196 | /** Starts DBX Debugger.
|
---|
197 | * Via a system call the debugger is started with certain paramaters
|
---|
198 | * \param *Host hostname (if not specified, then launched via \a pid)
|
---|
199 | * \param *program program name
|
---|
200 | * \param pid Process ID of te program
|
---|
201 | * \return return value of launched debugger
|
---|
202 | */
|
---|
203 | static int Start_dbx(char *Host, char *program, pid_t pid) {
|
---|
204 | char s[MAXDUMMYSTRING];
|
---|
205 | program = program;
|
---|
206 | if (Host) sprintf(s, "xterm -display %s -e dbx -p %ld &", Host, (long int)pid);
|
---|
207 | else sprintf(s, "xterm -e dbx -p %ld &", (long int)pid);
|
---|
208 | return system(s);
|
---|
209 | }
|
---|
210 |
|
---|
211 | /** Starts CVD Debugger.
|
---|
212 | * Via a system call the debugger is started with certain paramaters
|
---|
213 | * \param *Host hostname (if not specified, then launched via \a pid)
|
---|
214 | * \param *program program name
|
---|
215 | * \param pid Process ID of te program
|
---|
216 | * \return return value of launched debugger
|
---|
217 | */
|
---|
218 | static int Start_cvd(char *Host, char *program, pid_t pid) {
|
---|
219 | char s[MAXDUMMYSTRING];
|
---|
220 | program = program;
|
---|
221 | if (Host) sprintf(s, "cvd -pid %ld -display %s &", (long int)pid, Host);
|
---|
222 | else sprintf(s, "cvd -pid %ld &", (long int)pid);
|
---|
223 | return system(s);
|
---|
224 | }
|
---|
225 |
|
---|
226 | /** Starts Totalview Debugger.
|
---|
227 | * Via a system call the debugger is started with certain paramaters
|
---|
228 | * \param *Host hostname (if not specified, then launched via \a pid)
|
---|
229 | * \param *program program name
|
---|
230 | * \param pid Process ID of te program
|
---|
231 | * \return return value of launched debugger
|
---|
232 | */
|
---|
233 | static int Start_totalview(char *Host, char *program, pid_t pid) {
|
---|
234 | char s[MAXDUMMYSTRING];
|
---|
235 | int myrank = 0;
|
---|
236 | MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
|
---|
237 | if (myrank) return 0; /* nur einmal totalview aufrufen */
|
---|
238 | if (Host) sprintf(s, "totalview -display %s -p %ld %s &", Host, (long int)pid, program);
|
---|
239 | else sprintf(s, "totalview -p %ld %s &", (long int)pid, program);
|
---|
240 | return system(s);
|
---|
241 | }
|
---|
242 |
|
---|
243 | /** Starts UPS Debugger.
|
---|
244 | * Via a system call the debugger is started with certain paramaters
|
---|
245 | * \param *Host hostname (if not specified, then launched via \a pid)
|
---|
246 | * \param *program program name
|
---|
247 | * \param pid Process ID of te program
|
---|
248 | * \return return value of launched debugger
|
---|
249 | */
|
---|
250 | static int Start_ups(char *Host, char *program, pid_t pid) {
|
---|
251 | char s[MAXDUMMYSTRING];
|
---|
252 | if (Host) sprintf(s, "ups -display %s %s %ld &", Host, program, (long int)pid);
|
---|
253 | else sprintf(s, "ups %s %ld &", program, (long int)pid);
|
---|
254 | return system(s);
|
---|
255 | }
|
---|
256 |
|
---|
257 | /** Starts debugger.
|
---|
258 | * Tries to start a certain debugger, specified either via command line
|
---|
259 | * or educatedly guessed (having a list of typical ones), waits a minute
|
---|
260 | * in order to let the debugger have time for its pre-activities
|
---|
261 | *
|
---|
262 | * \warning no mallocs in this routine, no \ref Error
|
---|
263 | */
|
---|
264 | void StartDebugger(void) {
|
---|
265 | char Host[MAXDUMMYSTRING] = "", Debugger[MAXDUMMYSTRING] = "";
|
---|
266 | char *host_ptr = Host;
|
---|
267 | int (*Debugger_call) (char *, char *, pid_t);
|
---|
268 | struct utsname op;
|
---|
269 |
|
---|
270 | if (debugger_started || !DebugString)
|
---|
271 | return; /* nicht starten, falls kein -d oder -D, nicht zweimal starten */
|
---|
272 | debugger_started = 1;
|
---|
273 |
|
---|
274 | if (strcmp(DebugString, "0")) {
|
---|
275 | /* debugger soll aus Programm heraus gestartet werden */
|
---|
276 | if (!system(NULL)) {
|
---|
277 | fprintf(stderr,"StartDebugger: No shell available\n");
|
---|
278 | exit(EXIT_FAILURE);
|
---|
279 | }
|
---|
280 | switch (sscanf(DebugString, " %197[a-zA-Z0-9._:-],%199s", Host, Debugger)) {
|
---|
281 | case 2:
|
---|
282 | if (!strcmp(Debugger, "ddd")) Debugger_call = Start_ddd;
|
---|
283 | else if (!strcmp(Debugger, "gdb")) Debugger_call = Start_gdb;
|
---|
284 | else if (!strcmp(Debugger, "dbx")) Debugger_call = Start_dbx;
|
---|
285 | else if (!strcmp(Debugger, "cvd")) Debugger_call = Start_cvd;
|
---|
286 | else if (!strcmp(Debugger, "totalview")) Debugger_call = Start_totalview;
|
---|
287 | else if (!strcmp(Debugger, "ups")) Debugger_call = Start_ups;
|
---|
288 | else {
|
---|
289 | fprintf(stderr, "StartDebugger: debugger %s not supported.\n", Debugger);
|
---|
290 | exit(EXIT_FAILURE);
|
---|
291 | }
|
---|
292 | break;
|
---|
293 | case 0:
|
---|
294 | host_ptr = NULL; /* no -display */
|
---|
295 | case 1:
|
---|
296 | /* try to set debugger smart ;-) */
|
---|
297 | if (uname(&op) == -1) {
|
---|
298 | perror("StartDebugger: uname failed");
|
---|
299 | exit(EXIT_FAILURE);
|
---|
300 | }
|
---|
301 | if (!strncmp(op.sysname, "Linux", 5) || !strncmp(op.sysname, "linux", 5)) Debugger_call = Start_gdb;
|
---|
302 | else if (!strncmp(op.sysname, "IRIX", 4)) Debugger_call = Start_cvd;
|
---|
303 | else if (!strncmp(op.sysname, "sn", 2)) Debugger_call = Start_totalview;
|
---|
304 | else Debugger_call = Start_gdb;
|
---|
305 | break;
|
---|
306 | default:
|
---|
307 | fprintf(stderr, "StartDebugger: could not read debugstring.\n");
|
---|
308 | exit(EXIT_FAILURE);
|
---|
309 | }
|
---|
310 | if (!strcmp(Host, "local")) host_ptr = NULL; /* no -display */
|
---|
311 | else if (!strchr(Host, ':')) strcat(Host, ":0"); /* kein :0 gesetzt */
|
---|
312 | if (Debugger_call(host_ptr, program_name, getpid())) {
|
---|
313 | fprintf(stderr, "StartDebugger: could not start %s on %s\n", Debugger, Host);
|
---|
314 | exit(EXIT_FAILURE);
|
---|
315 | }
|
---|
316 | }
|
---|
317 | sleep(60); /* danach sollte der Debugger gestartet sein */
|
---|
318 | }
|
---|
319 |
|
---|
320 | /** Sets verbosity individually for each group.
|
---|
321 | * Depending on the specified general verbosity \ref CallOptions#Out the array \ref CallOptions#out
|
---|
322 | * is set accordingly
|
---|
323 | *
|
---|
324 | * \sa SetOutGroup2()
|
---|
325 | * \param *Call \ref CallOptions structure containing \ref CallOptions#Out and \ref CallOptions#out
|
---|
326 | */
|
---|
327 | void SetOutGroup1(struct CallOptions *Call) {
|
---|
328 | int i, me;
|
---|
329 | MPI_Comm_rank(MPI_COMM_WORLD, &me); // me is the process number of this one
|
---|
330 | for (i=0; i < MaxOutGroup; i++) Call->out[i] = 0; // resetting all levels
|
---|
331 | if (me == 0) Call->out[MinOut] = 1; // generally (0) does some output
|
---|
332 | switch (Call->Out) {
|
---|
333 | case OutNone:
|
---|
334 | break;
|
---|
335 | case OutNormal:
|
---|
336 | if (me == 0) Call->out[MinOut] = 1;
|
---|
337 | if (me == 0) Call->out[NormalOut] = 1;
|
---|
338 | if (me == 0) Call->out[ValueOut] = 1;
|
---|
339 | break;
|
---|
340 | case OutNormalP:
|
---|
341 | Call->out[MinOut] = 1;
|
---|
342 | Call->out[NormalOut] = 1;
|
---|
343 | if (me == 0) Call->out[ValueOut] = 1;
|
---|
344 | break;
|
---|
345 | case OutMore:
|
---|
346 | if (me == 0) Call->out[MinOut] = 1;
|
---|
347 | if (me == 0) Call->out[NormalOut] = 1;
|
---|
348 | if (me == 0) Call->out[ReadOut] = 1;
|
---|
349 | if (me == 0) Call->out[ValueOut] = 1;
|
---|
350 | break;
|
---|
351 | case OutMoreP:
|
---|
352 | Call->out[MinOut] = 1;
|
---|
353 | Call->out[NormalOut] = 1;
|
---|
354 | Call->out[ReadOut] = 1;
|
---|
355 | Call->out[ValueOut] = 1;
|
---|
356 | break;
|
---|
357 | case OutAll:
|
---|
358 | break;
|
---|
359 | case OutAllP:
|
---|
360 | default:
|
---|
361 | ;
|
---|
362 | }
|
---|
363 | }
|
---|
364 |
|
---|
365 | /** Set the output of various MPI communicating groups.
|
---|
366 | * Groups such as SpinDouble and for fft receive equal output levels
|
---|
367 | * regarding their calculations. Pattern is: "Me being part of the
|
---|
368 | * group? (..==0) Then set level to blabla"
|
---|
369 | *
|
---|
370 | * \sa SetOutGroup1()
|
---|
371 | * \param *P \ref Problem contains the communicators (aka groups)
|
---|
372 | * \param *Call \ref CallOptions contains verbosity level
|
---|
373 | */
|
---|
374 | void SetOutGroup2(struct Problem *P, struct CallOptions *Call) {
|
---|
375 | int i;
|
---|
376 | switch (Call->Out) {
|
---|
377 | case OutNone:
|
---|
378 | break;
|
---|
379 | case OutNormal:
|
---|
380 | if (P->Par.me_comm_ST_Psi == 0) Call->out[LeaderOut] = 1;
|
---|
381 | break;
|
---|
382 | case OutNormalP:
|
---|
383 | if (P->Par.me_comm_ST_Psi == 0) Call->out[LeaderOut] = 1;
|
---|
384 | if (P->Par.me_comm_ST == 0) Call->out[StepLeaderOut] = 1;
|
---|
385 | break;
|
---|
386 | case OutMore:
|
---|
387 | if (P->Par.me_comm_ST_Psi == 0) Call->out[LeaderOut] = 1;
|
---|
388 | if (P->Par.me_comm_ST == 0) Call->out[PsiOut] = 1;
|
---|
389 | if (P->Par.me_comm_ST_Psi == 0) Call->out[StepLeaderOut] = 1;
|
---|
390 | break;
|
---|
391 | case OutMoreP:
|
---|
392 | if (P->Par.my_color_comm_ST_Psi == 0) Call->out[LeaderOut] = 1;
|
---|
393 | if (P->Par.my_color_comm_ST_Psi == 0) Call->out[PsiOut] = 1;
|
---|
394 | if (P->Par.my_color_comm_ST_Psi == 0) Call->out[StepLeaderOut] = 1;
|
---|
395 | break;
|
---|
396 | case OutAll:
|
---|
397 | for (i=0; i < MaxOutGroup; i++) Call->out[i] = 1;
|
---|
398 | break;
|
---|
399 | case OutAllP:
|
---|
400 | default:
|
---|
401 | for (i=0; i < MaxOutGroup; i++) Call->out[i] = 1;
|
---|
402 | }
|
---|
403 | }
|
---|