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 #ifdef WITH_NETCDF
27 #include <netcdf.h>
28 #endif
29 
30 extern void fstr_main();
31 
35 struct option_rec {
36  char *option_name;
37  void (*func)(char *);
38 };
39 
41 #ifndef HECMW_SERIAL
42  int proc;
43  MPI_Comm_size(MPI_COMM_WORLD, &proc);
44  return proc;
45 #else
46  return 1;
47 #endif
48 }
49 
51 #ifdef _OPENMP
52  return omp_get_max_threads();
53 #else
54  return 1;
55 #endif /* _OPENMP */
56 }
57 
58 #ifdef _OPENMP
62 void set_num_threads(char *arg) {
63  int exec_threads;
64 
65  if (arg == NULL) {
66  fprintf(stderr, "Error : specify number of OpenMP threads.\n");
67  fprintf(stderr, "Format: -t <n>\n");
68  exit(1);
69  }
70 
71  exec_threads = atoi(arg);
72 
73  if (exec_threads == 0) {
74  fprintf(stderr, "Error : specify 1 or more OpenMP threads.\n");
75  exit(1);
76  }
77  omp_set_num_threads(exec_threads);
78 #ifdef WITH_MKL
79  mkl_set_num_threads(exec_threads);
80 #endif
81 
82 }
83 #endif /* _OPENMP */
84 
88 void help(char *arg) {
89  printf("usage: [ mpirun -np <mpiprocs> ] fistr1 [options] \n");
90  printf(" -h: Show this help message.\n");
91  printf(" -v: Show version.\n");
92 #ifdef _OPENMP
93  printf(" -t <n>: Set number of OpenMP threads\n");
94 #endif
95  printf(" -c <Path of control file>: Use this control file. Default "
96  "./hecmw_ctrl.dat\n");
97  printf("--debug: Show debug messages.\n");
98  exit(0);
99 }
100 
104 void print_buildinfo(int log_level) {
105  int rank;
106 #ifndef HECMW_SERIAL
107  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
108  if (rank!=0) return;
109 #endif
110  printf("##################################################################\n");
111  printf("# FrontISTR #\n");
112  printf("##################################################################\n");
113  printf("---\n");
114  if (VERSION_PATCH == 0){
115  printf("version: %d.%d\n", VERSION_MAJOR, VERSION_MINOR);
116  }else{
117  printf("version: %d.%d.%d\n", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH);
118  }
119  printf("git_hash: %s\n", GIT_HASH );
120  printf("build:\n");
121  printf(" date: %s\n", BUILD_DATE );
122 #ifdef WITH_MPI
123  printf(" MPI: \"%d.%d", MPI_VERSION, MPI_SUBVERSION);
124 #if defined(MVAPITCH2_VERSION)
125  printf(", MVAPITCH %s", MVAPITCH2_VERSION);
126 #elif defined(I_MPI_VERSION)
127  printf(", Intel MPI %s", I_MPI_VERSION);
128 #elif defined(MSMPI_VER)
129  printf(", Microsoft MPI");
130 #elif defined(MPI_NEC_MODE_GETPUTALIGNED)
131  printf(", NEC MPI");
132 #elif defined(MPICH_VERSION)
133  printf(", MPICH %s", MPICH_VERSION);
134 #elif defined(OMPI_MAJOR_VERSION)
135  printf(", Open MPI %d.%d.%d", OMPI_MAJOR_VERSION, OMPI_MINOR_VERSION, OMPI_RELEASE_VERSION);
136 #endif
137  printf("\"\n");
138 #else
139  printf(" MPI: disabled\n");
140 #endif
141 #ifdef _OPENMP
142  printf(" OpenMP: %d\n", _OPENMP);
143 #else
144  printf(" OpenMP: disabled\n");
145 #endif
146  printf(" option: ");
147  printf("\"");
148 #ifdef WITH_MPI
149  printf("-p ");
150 #endif
151 #ifdef WITH_TOOLS
152  printf("--with-tools ");
153 #endif
154 #ifdef WITH_REFINER
155  printf("--with-refiner ");
156 #endif
157 #ifdef WITH_METIS
158  printf("--with-metis ");
159 #endif
160 #ifdef WITH_MUMPS
161  printf("--with-mumps ");
162 #endif
163 #ifdef WITH_LAPACK
164  printf("--with-lapack ");
165 #endif
166 #ifdef WITH_ML
167  printf("--with-ml ");
168 #endif
169 #ifdef WITH_PARMETIS
170  printf("--with-parmetis ");
171 #endif
172 #ifdef WITH_MKL
173  printf("--with-mkl ");
174 #endif
175 #ifdef WITH_NETCDF
176  printf("--with-netcdf ");
177 #endif
178  printf("\"");
179  printf("\n");
180 #ifdef HECMW_METIS_VER
181  printf(" HECMW_METIS_VER: %d\n", HECMW_METIS_VER);
182 #endif
183 }
184 
188 void print_executeinfo(int log_level) {
189  int rank=0;
190  int proc, i, len, mpi_ver, mpi_subver;
191  char *p;
192  char date[32];
193  time_t t;
194  int d;
195 #ifndef HECMW_SERIAL
196  char hostname[MPI_MAX_PROCESSOR_NAME];
197  char mpilibver[MPI_MAX_LIBRARY_VERSION_STRING];
198  MPI_Status status;
199  MPI_Comm_size(MPI_COMM_WORLD, &proc);
200  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
201  MPI_Get_version(&mpi_ver, &mpi_subver);
202  MPI_Get_library_version(mpilibver,&len);
203  /* mpich returns too long string, clip 1st line. */
204  while ((p = strchr(mpilibver, '\n')) != NULL) *p = '\0';
205  MPI_Get_processor_name(hostname, &len);
206 #else
207  char hostname[128];
208 #endif
209 
210  if (rank==0){
211  printf("execute: \n");
212  t=time(NULL);
213  /* for windows compatibility */
214  d=(int)difftime(t,mktime(gmtime(&t)));
215  strftime(date, sizeof(date), "%Y-%m-%dT%H:%M:%S", localtime(&t));
216  printf(" date: %s", date);
217  if (abs(d)<86400) printf("%+05d", (int)(d/3600)*100+(int)(d/60)%60);
218  printf("\n");
219  printf(" processes: %d\n", get_procs_num());
220  printf(" threads: %d\n", get_threads_num());
221  printf(" cores: %d\n", get_threads_num()*get_procs_num());
222 #ifndef HECMW_SERIAL
223  printf(" MPI: \"%d.%d, %.128s\"\n", mpi_ver, mpi_subver, mpilibver);
224 #endif
225  printf(" host:\n");
226  }
227 #ifndef HECMW_SERIAL
228 
229  if (rank == 0){
230  printf(" %d: %s\n",0,hostname);
231  for (i=1;i<proc;i++){
232  MPI_Recv(&hostname, sizeof(hostname), MPI_CHAR, i, 0, MPI_COMM_WORLD, &status);
233  printf(" %d: %s\n",i,hostname);
234  }
235  }else{
236  MPI_Send(&hostname, len, MPI_CHAR, 0, 0, MPI_COMM_WORLD);
237  }
238 #else
239  gethostname(hostname, sizeof(hostname));
240  printf(" %d: %s\n",0,hostname);
241 #endif
242  if (rank==0) printf("---\n");
243 }
244 
248 void version(char *arg) {
249  int rank=0;
250 #ifndef HECMW_SERIAL
251  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
252 #endif
253  print_buildinfo(9);
254  if (rank==0) printf("---\n");
255  exit(0);
256 }
257 
261 void load_hecmw_ctrl(char *arg) {
262  fprintf(stderr, "Sorry this option cannot work yet. (-c)\n");
263  fprintf(stderr, "%s\n", arg);
264  exit(0);
265 }
266 
270 void set_loglevel_debug(char *arg) {
272 }
273 
278 struct option_rec options[] = {
279  {"-h", help},
280  {"-H", help},
281  {"-v", version},
282  {"-V", version},
283 #ifdef _OPENMP
284  {"-t", set_num_threads},
285  {"-T", set_num_threads},
286 #endif /* _OPENMP */
287  {"-c", load_hecmw_ctrl},
288  {"-C", load_hecmw_ctrl},
289  {"--debug", set_loglevel_debug},
290  {NULL, NULL}
291 };
292 
296 int main(int argc, char *argv[])
297 {
298  struct option_rec *p;
299  unsigned int i;
300 
301 #ifndef HECMW_SERIAL
302  MPI_Init(&argc, &argv);
303 #endif
304  for (i = 0; i < argc; i++) {
305  for (p = options; p->option_name != NULL; p++) {
306  if (strncmp(p->option_name, argv[i], strlen(p->option_name)) == 0) {
307  p->func(argv[i + 1]);
308  }
309  }
310  }
311  print_buildinfo(1);
313 #ifndef HECMW_SERIAL
314  MPI_Barrier( MPI_COMM_WORLD );
315 #endif
316  fstr_main();
317  return 0;
318 }
#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:296
void help(char *arg)
show available command line option
Definition: main.c:88
void print_executeinfo(int log_level)
show execute environment information
Definition: main.c:188
void set_loglevel_debug(char *arg)
set log level to HECMW_LOG_DEBUG
Definition: main.c:270
int get_threads_num()
Definition: main.c:50
void fstr_main()
Startup routine for FrontISTR.
int get_procs_num()
Definition: main.c:40
void version(char *arg)
show version and revision of FrontISTR
Definition: main.c:248
struct option_rec options[]
specify command line option name and executing function name.
Definition: main.c:278
void load_hecmw_ctrl(char *arg)
load hecmw_ctrl.dat from specified place
Definition: main.c:261
void print_buildinfo(int log_level)
show build information
Definition: main.c:104
struct of command-line option
Definition: main.c:35
char * option_name
Definition: main.c:36
void(* func)(char *)
Definition: main.c:37