forked from mohuangrui/ArtraCFD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
program_entrance.c
198 lines (197 loc) · 7.89 KB
/
program_entrance.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/****************************************************************************
* ArtraCFD *
* <By Huangrui Mo> *
* Copyright (C) Huangrui Mo <[email protected]> *
* This file is part of ArtraCFD. *
* ArtraCFD is free software: you can redistribute it and/or modify it *
* under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
****************************************************************************/
/****************************************************************************
* Required Header Files
****************************************************************************/
#include "program_entrance.h"
#include <stdio.h> /* standard library for input and output */
#include <stdlib.h> /* dynamic memory allocation and exit */
#include <string.h> /* manipulating strings */
#include "calculator.h"
#include "case_generator.h"
#include "commons.h"
/****************************************************************************
* Static Function Declarations
****************************************************************************/
static void ConfigureProgram(Control *, Space *);
static void ShowPreamble(Control *);
static void ShowManual(void);
/****************************************************************************
* Function Definitions
****************************************************************************/
int EnterProgram(int argc, char *argv[], Control *control, Space *space)
{
/*
* Loop through command line to process options
* The procedure main takes two arguments: argc and argv. The parameter
* argc is the number of arguments on the command line (including the
* program name). The array argv contains the actual arguments.
* A standard command-line format has the form:
* command options file1 file2 file3 ...
* Options are preceded by a dash (-) and are usually a single letter.
* If the option takes a parameter, it follows the letter with a space.
* A while loop is used to cycle through the command-line options.
* One argument always exists: the program name. The expression
* (argc > 1) checks for additional arguments. The first one is
* numbered 1. The first character of the first argument is argv[1][0].
* If this is a dash, there is an option. The switch statement is used
* to decode the options.
*/
while ((1 < argc) && ('-' == argv[1][0])) { /* options present */
if (3 > argc) { /* not enough arguments */
ShowError("empty entry after: %s\n", argv[1]);
exit(EXIT_FAILURE);
}
switch (argv[1][1]) { /* argv[1][1] is the actual option character */
/* run mode: -m [gui], [serial], [omp], [mpi], [gpu] */
case 'm':
++argv;
--argc;
if (0 == strcmp(argv[1], "gui")) {
control->runMode = 'i';
break;
}
if (0 == strcmp(argv[1], "serial")) {
control->runMode = 's';
break;
}
if (0 == strcmp(argv[1], "omp")) {
control->runMode = 'o';
break;
}
if (0 == strcmp(argv[1], "mpi")) {
control->runMode = 'm';
break;
}
if (0 == strcmp(argv[1], "gpu")) {
control->runMode = 'g';
break;
}
ShowError("bad option: %s\n", argv[1]);
exit(EXIT_FAILURE);
/* number of processors: -n nx*ny*nz */
case 'n':
++argv;
--argc;
Sscanf(argv[1], 3, "%d*%d*%d", &(control->proc[X]),
&(control->proc[Y]), &(control->proc[Z]));
break;
default:
ShowError("bad option: %s\n", argv[1]);
exit(EXIT_FAILURE);
}
/* adjust argument list and count to consume an option */
++argv;
--argc;
}
/* check information left */
if (1 != argc) {
ShowWarning("unidentified arguments ignored: %s...\n", argv[1]);
}
/* configure program according to inputted options */
ConfigureProgram(control, space);
return 0;
}
static void ConfigureProgram(Control *control, Space *space)
{
Partition *const part = &(space->part);
switch (control->runMode) {
case 'i': /* gui mode */
ShowPreamble(control);
/* fall through */
case 's': /* serial mode */
part->proc[X] = 1;
part->proc[Y] = 1;
part->proc[Z] = 1;
part->procN = 1;
break;
case 'o': /* omp mode */
/* fall through */
case 'm': /* mpi mode */
part->proc[X] = control->proc[X];
part->proc[Y] = control->proc[Y];
part->proc[Z] = control->proc[Z];
part->procN = control->proc[X] *
control->proc[Y] * control->proc[Z];
break;
case 'g': /* gpu mode */
break;
default:
break;
}
return;
}
static void ShowPreamble(Control *control)
{
ShowInfo("Session");
ShowInfo("* ArtraCFD *\n");
ShowInfo("* <By Huangrui Mo> *\n");
ShowInfo("* Copyright (C) Huangrui Mo <[email protected]> *\n");
ShowInfo("Session");
ShowInfo("Enter 'help' for more information\n");
ShowInfo("Session");
String str = {'\0'}; /* store the current read line */
while (1) {
ShowInfo("\nArtraCFD << ");
ParseCommand(fgets(str, sizeof str, stdin));
ShowInfo("\n");
if (0 == strncmp(str, "help", sizeof str)) {
ShowInfo("Options in gui environment:\n");
ShowInfo("[help] show this information\n");
ShowInfo("[init] generate files for a sample case\n");
ShowInfo("[solve] solve current case in serial mode\n");
ShowInfo("[calc] access expression calculator\n");
ShowInfo("[manual] show user manual\n");
ShowInfo("[exit] exit program\n");
continue;
}
if (0 == strncmp(str, "init", sizeof str)) {
GenerateCaseFiles();
ShowInfo("a sample case generated successfully\n");
continue;
}
if (0 == strncmp(str, "calc", sizeof str)) {
RunCalculator();
continue;
}
if (0 == strncmp(str, "manual", sizeof str)) {
ShowManual();
continue;
}
if ('\0' == str[0]) {
continue;
}
if (0 == strncmp(str, "solve", sizeof str)) {
control->runMode = 's';
ShowInfo("Session");
return;
}
if (0 == strncmp(str, "exit", sizeof str)) {
ShowInfo("Session");
exit(EXIT_SUCCESS);
}
/* if non of above is true, then unknow commands */
ShowWarning("unknown command: %s\n", str);
}
}
static void ShowManual(void)
{
ShowInfo("\n ArtraCFD User Manual\n");
ShowInfo("SYNOPSIS:\n");
ShowInfo(" artracfd [-m runmode] [-n nprocessors]\n");
ShowInfo("OPTIONS:\n");
ShowInfo(" -m runmode run mode: gui, serial, omp, mpi, gpu\n");
ShowInfo(" -n nprocessors processors per dimension: nx*ny*nz\n");
ShowInfo("NOTES:\n");
ShowInfo(" default run mode is gui\n");
return;
}
/* a good practice: end file with a newline */