FrontISTR  5.8.0
Large-scale structural analysis program with finit element method
main.c
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright (c) 2019 FrontISTR Commons
3  * This software is released under the MIT License, see LICENSE.txt
4  *****************************************************************************/
9 #include <stdio.h>
10 #include <time.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <FrontISTRConfig.h>
14 #include "hecmw_log.h"
15 #ifndef HECMW_SERIAL
16 #include "mpi.h"
17 #else
18 #include <unistd.h>
19 #endif
20 #ifdef _OPENMP
21 #include <omp.h>
22 #endif /* _OPENMP */
23 #ifdef WITH_MKL
24 #include <mkl.h>
25 #endif
26 
27 extern void fstr_main();
28 
32 struct option_rec {
33  char *option_name;
34  void (*func)(char *);
35 };
36 
38 #ifndef HECMW_SERIAL
39  int proc;
40  MPI_Comm_size(MPI_COMM_WORLD, &proc);
41  return proc;
42 #else
43  return 1;
44 #endif
45 }
46 
48 #ifdef _OPENMP
49  return omp_get_max_threads();
50 #else
51  return 1;
52 #endif /* _OPENMP */
53 }
54 
55 #ifdef _OPENMP
59 void set_num_threads(char *arg) {
60  int exec_threads;
61 
62  if (arg == NULL) {
63  fprintf(stderr, "Error : specify number of OpenMP threads.\n");
64  fprintf(stderr, "Format: -t <n>\n");
65  exit(1);
66  }
67 
68  exec_threads = atoi(arg);
69 
70  if (exec_threads == 0) {
71  fprintf(stderr, "Error : specify 1 or more OpenMP threads.\n");
72  exit(1);
73  }
74  omp_set_num_threads(exec_threads);
75 #ifdef WITH_MKL
76  mkl_set_num_threads(exec_threads);
77 #endif
78 
79 }
80 #endif /* _OPENMP */
81 
85 void help(char *arg) {
86  printf("usage: [ mpirun -np <mpiprocs> ] fistr1 [options] \n");
87  printf(" -h: Show this help message.\n");
88  printf(" -v: Show version.\n");
89 #ifdef _OPENMP
90  printf(" -t <n>: Set number of OpenMP threads\n");
91 #endif
92  printf(" -c <Path of control file>: Use this control file. Default "
93  "./hecmw_ctrl.dat\n");
94  printf("--debug: Show debug messages.\n");
95  exit(0);
96 }
97 
101 void print_buildinfo(int log_level) {
102  int rank;
103 #ifndef HECMW_SERIAL
104  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
105  if (rank!=0) return;
106 #endif
107  printf("##################################################################\n");
108  printf("# FrontISTR #\n");
109  printf("##################################################################\n");
110  printf("---\n");
111  if (VERSION_PATCH == 0){
112  printf("version: %d.%d\n", VERSION_MAJOR, VERSION_MINOR);
113  }else{
114  printf("version: %d.%d.%d\n", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH);
115  }
116  printf("git_hash: %s\n", GIT_HASH );
117  printf("build:\n");
118  printf(" date: %s\n", BUILD_DATE );
119 #ifdef WITH_MPI
120  printf(" MPI: \"%d.%d", MPI_VERSION, MPI_SUBVERSION);
121 #if defined(MVAPITCH2_VERSION)
122  printf(", MVAPITCH %s", MVAPITCH2_VERSION);
123 #elif defined(I_MPI_VERSION)
124  printf(", Intel MPI %s", I_MPI_VERSION);
125 #elif defined(MSMPI_VER)
126  printf(", Microsoft MPI");
127 #elif defined(MPI_NEC_MODE_GETPUTALIGNED)
128  printf(", NEC MPI");
129 #elif defined(MPICH_VERSION)
130  printf(", MPICH %s", MPICH_VERSION);
131 #elif defined(OMPI_MAJOR_VERSION)
132  printf(", Open MPI %d.%d.%d", OMPI_MAJOR_VERSION, OMPI_MINOR_VERSION, OMPI_RELEASE_VERSION);
133 #endif
134  printf("\"\n");
135 #else
136  printf(" MPI: disabled\n");
137 #endif
138 #ifdef _OPENMP
139  printf(" OpenMP: %d\n", _OPENMP);
140 #else
141  printf(" OpenMP: disabled\n");
142 #endif
143  printf(" option: ");
144  printf("\"");
145 #ifdef WITH_MPI
146  printf("-p ");
147 #endif
148 #ifdef WITH_TOOLS
149  printf("--with-tools ");
150 #endif
151 #ifdef WITH_REFINER
152  printf("--with-refiner ");
153 #endif
154 #ifdef WITH_METIS
155  printf("--with-metis ");
156 #endif
157 #ifdef WITH_MUMPS
158  printf("--with-mumps ");
159 #endif
160 #ifdef WITH_LAPACK
161  printf("--with-lapack ");
162 #endif
163 #ifdef WITH_ML
164  printf("--with-ml ");
165 #endif
166 #ifdef WITH_PARMETIS
167  printf("--with-parmetis ");
168 #endif
169 #ifdef WITH_MKL
170  printf("--with-mkl ");
171 #endif
172  printf("\"");
173  printf("\n");
174 #ifdef HECMW_METIS_VER
175  printf(" HECMW_METIS_VER: %d\n", HECMW_METIS_VER);
176 #endif
177 }
178 
182 void print_executeinfo(int log_level) {
183  int rank=0;
184  int proc, i, len, mpi_ver, mpi_subver;
185  char *p;
186  char date[32];
187  time_t t;
188  int d;
189 #ifndef HECMW_SERIAL
190  char hostname[MPI_MAX_PROCESSOR_NAME];
191  char mpilibver[MPI_MAX_LIBRARY_VERSION_STRING];
192  MPI_Status status;
193  MPI_Comm_size(MPI_COMM_WORLD, &proc);
194  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
195  MPI_Get_version(&mpi_ver, &mpi_subver);
196  MPI_Get_library_version(mpilibver,&len);
197  /* mpich returns too long string, clip 1st line. */
198  while ((p = strchr(mpilibver, '\n')) != NULL) *p = '\0';
199  MPI_Get_processor_name(hostname, &len);
200 #else
201  char hostname[128];
202 #endif
203 
204  if (rank==0){
205  printf("execute: \n");
206  t=time(NULL);
207  /* for windows compatibility */
208  d=(int)difftime(t,mktime(gmtime(&t)));
209  strftime(date, sizeof(date), "%Y-%m-%dT%H:%M:%S", localtime(&t));
210  printf(" date: %s", date);
211  if (abs(d)<86400) printf("%+05d", (int)(d/3600)*100+(int)(d/60)%60);
212  printf("\n");
213  printf(" processes: %d\n", get_procs_num());
214  printf(" threads: %d\n", get_threads_num());
215  printf(" cores: %d\n", get_threads_num()*get_procs_num());
216 #ifndef HECMW_SERIAL
217  printf(" MPI: \"%d.%d, %.128s\"\n", mpi_ver, mpi_subver, mpilibver);
218 #endif
219  printf(" host:\n");
220  }
221 #ifndef HECMW_SERIAL
222 
223  if (rank == 0){
224  printf(" %d: %s\n",0,hostname);
225  for (i=1;i<proc;i++){
226  MPI_Recv(&hostname, sizeof(hostname), MPI_CHAR, i, 0, MPI_COMM_WORLD, &status);
227  printf(" %d: %s\n",i,hostname);
228  }
229  }else{
230  MPI_Send(&hostname, len, MPI_CHAR, 0, 0, MPI_COMM_WORLD);
231  }
232 #else
233  gethostname(hostname, sizeof(hostname));
234  printf(" %d: %s\n",0,hostname);
235 #endif
236  if (rank==0) printf("---\n");
237 }
238 
242 void version(char *arg) {
243  int rank=0;
244 #ifndef HECMW_SERIAL
245  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
246 #endif
247  print_buildinfo(9);
248  if (rank==0) printf("---\n");
249  exit(0);
250 }
251 
255 void load_hecmw_ctrl(char *arg) {
256  fprintf(stderr, "Sorry this option cannot work yet. (-c)\n");
257  fprintf(stderr, "%s\n", arg);
258  exit(0);
259 }
260 
264 void set_loglevel_debug(char *arg) {
266 }
267 
272 struct option_rec options[] = {
273  {"-h", help},
274  {"-H", help},
275  {"-v", version},
276  {"-V", version},
277 #ifdef _OPENMP
278  {"-t", set_num_threads},
279  {"-T", set_num_threads},
280 #endif /* _OPENMP */
281  {"-c", load_hecmw_ctrl},
282  {"-C", load_hecmw_ctrl},
283  {"--debug", set_loglevel_debug},
284  {NULL, NULL}
285 };
286 
290 int main(int argc, char *argv[])
291 {
292  struct option_rec *p;
293  unsigned int i;
294 
295 #ifndef HECMW_SERIAL
296  MPI_Init(&argc, &argv);
297 #endif
298  for (i = 0; i < argc; i++) {
299  for (p = options; p->option_name != NULL; p++) {
300  if (strncmp(p->option_name, argv[i], strlen(p->option_name)) == 0) {
301  p->func(argv[i + 1]);
302  }
303  }
304  }
305  print_buildinfo(1);
307 #ifndef HECMW_SERIAL
308  MPI_Barrier( MPI_COMM_WORLD );
309 #endif
310  fstr_main();
311  return 0;
312 }
#define NULL
void HECMW_setloglv(int loglv)
Definition: hecmw_log.c:57
#define HECMW_LOG_DEBUG
Definition: hecmw_log.h:21
int main(int argc, char *argv[])
main function
Definition: main.c:290
void help(char *arg)
show available command line option
Definition: main.c:85
void print_executeinfo(int log_level)
show execute environment information
Definition: main.c:182
void set_loglevel_debug(char *arg)
set log level to HECMW_LOG_DEBUG
Definition: main.c:264
int get_threads_num()
Definition: main.c:47
void fstr_main()
Startup routine for FrontISTR.
int get_procs_num()
Definition: main.c:37
void version(char *arg)
show version and revision of FrontISTR
Definition: main.c:242
struct option_rec options[]
specify command line option name and executing function name.
Definition: main.c:272
void load_hecmw_ctrl(char *arg)
load hecmw_ctrl.dat from specified place
Definition: main.c:255
void print_buildinfo(int log_level)
show build information
Definition: main.c:101
struct of command-line option
Definition: main.c:32
char * option_name
Definition: main.c:33
void(* func)(char *)
Definition: main.c:34