kspaceFirstOrder3D-CUDA  1.1
The CUDA/C++ implementation of the k-wave toolbox for the time-domain simulation of acoustic wave fields in 3D
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CommandLineParameters.cpp
Go to the documentation of this file.
1 /**
2  * @file CommandLineParameters.cpp
3  *
4  * @author Jiri Jaros \n
5  * Faculty of Information Technology \n
6  * Brno University of Technology \n
7  * jarosjir@fit.vutbr.cz
8  *
9  * @brief The implementation file containing the command line parameters.
10  *
11  * @version kspaceFirstOrder3D 3.4
12  *
13  * @date 29 August 2012, 11:25 (created) \n
14  * 10 August 2016, 12:54 (revised)
15  *
16  * @section License
17  * This file is part of the C++ extension of the k-Wave Toolbox
18  * (http://www.k-wave.org).\n Copyright (C) 2016 Jiri Jaros and Bradley Treeby.
19  *
20  * This file is part of the k-Wave. k-Wave is free software: you can redistribute it and/or modify
21  * it under the terms of the GNU Lesser General Public License as published by the Free Software
22  * Foundation, either version 3 of the License, or (at your option) any later version.
23  *
24  * k-Wave is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
25  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
26  * General Public License for more details.
27  *
28  * You should have received a copy of the GNU Lesser General Public License along with k-Wave.
29  * If not, see http://www.gnu.org/licenses/.
30  */
31 
32 //Linux build
33 #ifdef __linux__
34  #include <getopt.h>
35 #endif
36 
37 //Windows build
38 #ifdef _WIN64
39  #include <GetoptWin64/Getopt.h>
40 #endif
41 
42 #ifdef _OPENMP
43  #include <omp.h>
44 #endif
45 
46 
47 #include <stdexcept>
48 
49 #include <Logger/Logger.h>
51 #include <HDF5/HDF5_File.h>
52 
53 using std::string;
54 
55 //------------------------------------------------------------------------------------------------//
56 //------------------------------------------ Constants -------------------------------------------//
57 //------------------------------------------------------------------------------------------------//
58 
59 
60 
61 //------------------------------------------------------------------------------------------------//
62 //--------------------------------------- Public methods -----------------------------------------//
63 //------------------------------------------------------------------------------------------------//
64 
65 /**
66  * Constructor.
67  */
69  inputFileName(""), outputFileName (""), checkpointFileName(""),
70  #ifdef _OPENMP
71  numberOfThreads(omp_get_num_procs()),
72  #else
73  numberOfThreads(1),
74  #endif
75 
76  cudaDeviceIdx(-1), // default is undefined -1
77 
78  progressPrintInterval(DEFAULT_PROGRESS_PRINT_INTERVAL),
79  compressionLevel(DEFAULT_COMPRESSION_LEVEL),
80  benchmarkFlag (false), benchmarkTimeStepCount(0),
81  checkpointInterval(0),
82  printVersion (false),
83 
84  store_p_raw(false), store_p_rms(false), store_p_max(false), store_p_min(false),
85  store_p_max_all(false), store_p_min_all(false), store_p_final(false),
86  store_u_raw(false), store_u_non_staggered_raw(false),
87  store_u_rms(false), store_u_max(false), store_u_min(false),
88  store_u_max_all(false), store_u_min_all(false), store_u_final(false),
89  copySensorMask(false),
90  startTimeStep(0)
91 {
92 
93 }// end of TCommandLineParameters
94 //--------------------------------------------------------------------------------------------------
95 
96 /**
97  * Print usage.
98  */
100 {
102 
103  #ifdef _OPENMP
106  omp_get_num_procs());
107  #endif
108 
113 }// end of PrintUsage
114 //--------------------------------------------------------------------------------------------------
115 
116 /**
117  * Print out commandline parameters.
118  */
120 {
122 
126  15).c_str());
127 
131  15).c_str());
132 
133  if (IsCheckpointEnabled())
134  {
138  15).c_str());
139 
141 
143  }
144  else
145  {
147  }
148 
149 
151 
153 
154  if (benchmarkFlag)
155  {
157  }
158 
160 
161 
162  string sampledQuantitiesList = "";
163  // Sampled p quantities
164 
165  if (store_p_raw)
166  {
167  sampledQuantitiesList += "p_raw, ";
168  }
169  if (store_p_rms)
170  {
171  sampledQuantitiesList += "p_rms, ";
172  }
173  if (store_p_max)
174  {
175  sampledQuantitiesList += "p_max, ";
176  }
177  if (store_p_min)
178  {
179  sampledQuantitiesList += "p_min, ";
180  }
181  if (store_p_max_all)
182  {
183  sampledQuantitiesList += "p_max_all, ";
184  }
185  if (store_p_min_all)
186  {
187  sampledQuantitiesList += "p_min_all, ";
188  }
189  if (store_p_final)
190  {
191  sampledQuantitiesList += "p_final, ";
192  }
193 
194  // Sampled u quantities
195  if (store_u_raw)
196  {
197  sampledQuantitiesList += "u_raw, ";
198  }
199  if (store_u_rms)
200  {
201  sampledQuantitiesList += "u_rms, ";
202  }
203  if (store_u_max)
204  {
205  sampledQuantitiesList += "u_max, ";
206  }
207  if (store_u_min)
208  {
209  sampledQuantitiesList += "u_min, ";
210  }
211  if (store_u_max_all)
212  {
213  sampledQuantitiesList += "u_max_all, ";
214  }
215  if (store_u_min_all)
216  {
217  sampledQuantitiesList += "u_min_all, ";
218  }
219  if (store_u_final)
220  {
221  sampledQuantitiesList += "u_final, ";
222  }
223 
225  {
226  sampledQuantitiesList += "u_non_staggered_raw, ";
227  }
228 
229  // remove comma and space symbols
230  if (sampledQuantitiesList.length() > 0)
231  {
232  sampledQuantitiesList.pop_back();
233  sampledQuantitiesList.pop_back();
234  }
235 
237  TLogger::WordWrapString(sampledQuantitiesList,
238  " ",2).c_str());
239 
241 
243 
244  if (copySensorMask)
245  {
247  }
248 }// end of PrintComandlineParamers
249 //--------------------------------------------------------------------------------------------------
250 
251 /**
252  * Parse command line.
253  * @param [in, out] argc
254  * @param [in, out] argv
255  */
256 void TCommandLineParameters::ParseCommandLine(int argc, char** argv)
257 {
258  char c;
259  int longIndex = -1;
260  bool checkpointFlag = false;
261 
262  const int errorLineIndentation = 9;
263 
264  // all optional arguments are in fact requested. This was chosen to prevent
265  // getopt error messages and provide custom error handling.
266  #ifdef _OPENMP
267  const char* shortOpts = "i:o:r:c:t:g:puhs:";
268  #else
269  const char* shortOpts = "i:o:r:c:g:puhs:";
270  #endif
271 
272  const struct option longOpts[] =
273  {
274  { "benchmark", required_argument, NULL, 1 },
275  { "copy_sensor_mask", no_argument, NULL, 2 },
276  { "checkpoint_file" , required_argument, NULL, 3 },
277  { "checkpoint_interval", required_argument, NULL, 4 },
278  { "help", no_argument, NULL, 'h'},
279  { "verbose", required_argument, NULL, 5 },
280  { "version", no_argument, NULL, 6 },
281 
282  { "p_raw", no_argument, NULL,'p' },
283  { "p_rms", no_argument, NULL, 10 },
284  { "p_max", no_argument, NULL, 11 },
285  { "p_min", no_argument, NULL, 12 },
286  { "p_max_all", no_argument, NULL, 13 },
287  { "p_min_all", no_argument, NULL, 14 },
288  { "p_final", no_argument, NULL, 15 },
289 
290  { "u_raw", no_argument, NULL,'u' },
291  { "u_rms", no_argument, NULL, 20},
292  { "u_max", no_argument, NULL, 21},
293  { "u_min", no_argument, NULL, 22},
294  { "u_max_all", no_argument, NULL, 23},
295  { "u_min_all", no_argument, NULL, 24},
296  { "u_final", no_argument, NULL, 25},
297  { "u_non_staggered_raw", no_argument, NULL, 26},
298 
299  { NULL, no_argument, NULL, 0}
300  };
301 
302  // all optional arguments are in fact requested. This was chosen to prevent
303  // getopt error messages and provide custom error handling.
304  opterr = 0;
305 
306  // Short parameters //
307  while ((c = getopt_long (argc, argv, shortOpts, longOpts, &longIndex )) != -1)
308  {
309  switch (c)
310  {
311  case 'i':
312  {
313  // test if the wile was correctly entered (if not, getopt could eat
314  // the following parameter)
315  if ((optarg != NULL) &&
316  ((strlen(optarg) > 0) && (optarg[0] != '-')))
317  {
318  inputFileName = optarg;
319  }
320  else
321  {
322  PrintUsage();
324  " ",
325  errorLineIndentation).c_str());
326  }
327  break;
328  }
329 
330  case 'o':
331  {
332  // test if the wile was correctly entered (if not, getopt could eat
333  // the following parameter)
334  if ((optarg != NULL) &&
335  ((strlen(optarg) > 0) && (optarg[0] != '-')))
336  {
337  outputFileName = optarg;
338  }
339  else
340  {
341  PrintUsage();
343  " ",
344  errorLineIndentation).c_str());
345  }
346  break;
347  }
348 
349  case 'r':
350  {
351  try
352  {
353  int convertedValue = std::stoi(optarg);
354  if ((convertedValue < 1) || (convertedValue > 100))
355  {
356  throw std::invalid_argument("-r");
357  }
358  progressPrintInterval = std::stoll(optarg);
359  }
360  catch (...)
361  {
362  PrintUsage();
364  " ",
365  errorLineIndentation).c_str());
366  }
367  break;
368  }
369 
370  case 't':
371  {
372  try
373  {
374  if (std::stoi(optarg) < 1)
375  {
376  throw std::invalid_argument("-t");
377  }
378  numberOfThreads = std::stoll(optarg);
379  }
380  catch (...)
381  {
382  PrintUsage();
384  " ",
385  errorLineIndentation).c_str());
386  }
387  break;
388  }
389 
390  case 'g':
391  {
392  try
393  {
394  cudaDeviceIdx = std::stoi(optarg);
395  if (cudaDeviceIdx < 0)
396  {
397  throw std::invalid_argument("-g");
398  }
399  }
400  catch (...)
401  {
402  PrintUsage();
404  " ",
405  errorLineIndentation).c_str());
406  }
407  break;
408  }
409 
410  case 'c':
411  {
412  try
413  {
414  int covertedValue = std::stoi(optarg);
415  if ((covertedValue < 0) || (covertedValue > 9))
416  {
417  throw std::invalid_argument("-c");
418  }
419  compressionLevel = std::stoll(optarg);
420  }
421  catch (...)
422  {
423  PrintUsage();
425  " ",
426  errorLineIndentation).c_str());
427  }
428  break;
429  }
430 
431  case 'h':
432  {
433  PrintUsage();
434  exit(EXIT_SUCCESS);
435  }
436 
437  case 's':
438  {
439  try
440  {
441  if (std::stoll(optarg) < 1)
442  {
443  throw std::invalid_argument("-s");
444  }
445  startTimeStep = std::stoll(optarg) - 1;
446  }
447  catch (...)
448  {
449  PrintUsage();
451  " ",
452  errorLineIndentation).c_str());
453  }
454  break;
455  }
456 
457  case 1: // benchmark
458  {
459  try
460  {
461  benchmarkFlag = true;
462  if (std::stoll(optarg) <= 0)
463  {
464  throw std::invalid_argument("benchmark");
465  }
466  benchmarkTimeStepCount = std::stoll(optarg);
467  }
468  catch (...)
469  {
470  PrintUsage();
472  " ",
473  errorLineIndentation).c_str());
474  }
475  break;
476  }
477 
478  case 2: // copy_sensor_mask
479  {
480  copySensorMask = true;
481  break;
482  }
483 
484  case 3: // checkpoint_file
485  {
486  checkpointFlag = true;
487  // test if the wile was correctly entered (if not, getopt could eat
488  // the following parameter)
489  if ((optarg != NULL) &&
490  ((strlen(optarg) > 0) && (optarg[0] != '-')))
491  {
492  checkpointFileName = optarg;
493  }
494  else
495  {
496  PrintUsage();
498  " ",
499  errorLineIndentation).c_str());
500  }
501  break;
502  }
503 
504  case 4: // checkpoint_interval
505  {
506  try
507  {
508  checkpointFlag = true;
509  if (std::stoll(optarg) <= 0)
510  {
511  throw std::invalid_argument("checkpoint_interval");
512  }
513  checkpointInterval = std::stoll(optarg);
514  }
515  catch (...)
516  {
517  PrintUsage();
519  " ",
520  errorLineIndentation).c_str());
521  }
522  break;
523  }
524 
525  case 5: // verbose
526  {
527  try
528  {
529  int verboseLevel = std::stoi(optarg);
530  if ((verboseLevel < 0) || (verboseLevel > 2))
531  {
532  throw std::invalid_argument("verbose");
533  }
534  TLogger::SetLevel(static_cast<TLogger::TLogLevel> (verboseLevel));
535  }
536  catch (...)
537  {
538  PrintUsage();
540  " ",
541  errorLineIndentation).c_str());
542  }
543  break;
544  }
545 
546  case 6: // version
547  {
548  printVersion = true;
549  break;
550  }
551 
552  case 'p':
553  {
554  store_p_raw = true;
555  break;
556  }
557 
558  case 10: // p_rms
559  {
560  store_p_rms = true;
561  break;
562  }
563 
564  case 11: // p_max
565  {
566  store_p_max = true;
567  break;
568  }
569 
570  case 12: // p_min
571  {
572  store_p_min = true;
573  break;
574  }
575 
576  case 13: // p_max_all
577  {
578  store_p_max_all = true;
579  break;
580  }
581 
582  case 14: // p_min_all
583  {
584  store_p_min_all = true;
585  break;
586  }
587 
588  case 15: // p_final
589  {
590  store_p_final = true;
591  break;
592  }
593 
594  case 'u':
595  {
596  store_u_raw = true;
597  break;
598  }
599 
600  case 20: // u_rms
601  {
602  store_u_rms = true;
603  break;
604  }
605 
606  case 21: // u_max
607  {
608  store_u_max = true;
609  break;
610  }
611 
612  case 22: // u_min
613  {
614  store_u_min = true;
615  break;
616  }
617 
618  case 23: // u_max_all
619  {
620  store_u_max_all = true;
621  break;
622  }
623 
624  case 24: // u_min_all
625  {
626  store_u_min_all = true;
627  break;
628  }
629 
630  case 25: // u_final
631  {
632  store_u_final = true;
633  break;
634  }
635 
636  case 26: // u_non_staggered_raw
637  {
639  break;
640  }
641 
642  // unknown parameter or a missing mandatory argument
643  case ':':
644  case '?':
645  {
646  switch (optopt)
647  {
648  case 'i':
649  {
650  PrintUsage();
652  " ",
653  errorLineIndentation).c_str());
654  break;
655  }
656  case 'o':
657  {
658  PrintUsage();
660  " ",
661  errorLineIndentation).c_str());
662  break;
663  }
664 
665  case 'r':
666  {
667  PrintUsage();
669  " ",
670  errorLineIndentation).c_str());
671  break;
672  }
673 
674  case 'c':
675  {
676  PrintUsage();
678  " ",
679  errorLineIndentation).c_str());
680  break;
681  }
682 
683  case 't':
684  {
685  PrintUsage();
687  " ",
688  errorLineIndentation).c_str());
689  break;
690  }
691 
692  case 'g':
693  {
694  PrintUsage();
696  " ",
697  errorLineIndentation).c_str());
698  break;
699  }
700 
701  case 's':
702  {
703  PrintUsage();
705  " ",
706  errorLineIndentation).c_str());
707  break;
708  }
709 
710  case 1: // benchmark
711  {
712  PrintUsage();
714  " ",
715  errorLineIndentation).c_str());
716  break;
717  }
718 
719  case 3: // checkpoint_file
720  {
721  PrintUsage();
723  " ",
724  errorLineIndentation).c_str());
725  break;
726  }
727 
728  case 4: // checkpoint_interval
729  {
730  PrintUsage();
732  " ",
733  errorLineIndentation).c_str());
734  break;
735  }
736 
737  case 5: // verbose
738  {
739  PrintUsage();
741  " ",
742  errorLineIndentation).c_str());
743  break;
744  }
745 
746  default :
747  {
748  PrintUsage();
750  " ",
751  errorLineIndentation).c_str());
752  break;
753  }
754  }
755  }
756 
757  default:
758  {
759  PrintUsage();
761  " ",
762  errorLineIndentation).c_str());
763  break;
764  }
765  }
766  }
767 
768  if (printVersion) return;
769 
770  //-- Post checks --//
771  if (inputFileName == "")
772  {
773  PrintUsage();
775  " ",
776  errorLineIndentation).c_str());
777  }
778 
779  if (outputFileName == "")
780  {
781  PrintUsage();
783  " ",
784  errorLineIndentation).c_str());
785  }
786 
787  if (checkpointFlag)
788  {
789  if (checkpointFileName == "")
790  {
791  PrintUsage();
793  " ",
794  errorLineIndentation).c_str());
795  }
796 
797  if (checkpointInterval <= 0)
798  {
799  PrintUsage();
801  " ",
802  errorLineIndentation).c_str());
803  }
804  }
805 
806  // set a default flag if necessary
812  {
813  store_p_raw = true;
814  }
815 }// end of ParseCommandLine
816 //--------------------------------------------------------------------------------------------------
bool store_u_final
Store_u_final value.
bool store_p_max
Store_p_max value.
static std::string WordWrapString(const std::string &inputString, const std::string &delimiters, const int indentation=0, const int lineSize=65)
Wrap the line based on logger conventions.
Definition: Logger.cpp:130
TOutputMessage OUT_FMT_USAGE_PART_1
Usage massage.
TErrorMessage ERR_FMT_NO_BENCHMARK_STEP_SET
Command line parameters error message.
TErrorMessage ERR_FMT_PATH_DELIMITERS
delimiters for linux paths
Definition: ErrorMessages.h:52
bool copySensorMask
Copy sensor mask to the output file.
bool store_p_final
Store_p_final value.
TOutputMessage OUT_FMT_PRINT_PROGRESS_INTERVAL
Output message.
size_t numberOfThreads
Number of CPU threads value.
bool store_p_raw
Store_p_raw value.
bool store_p_min_all
Store_p_min_all value.
size_t checkpointInterval
Checkpoint interval in seconds.
bool store_u_non_staggered_raw
Store_u_non_staggered_raw value.
TOutputMessage OUT_FMT_CHECKPOINT_INTERVAL
Output message.
TErrorMessage ERR_FMT_NO_COMPRESSION_LEVEL
Command line parameters error message.
TOutputMessage OUT_FMT_USAGE_PART_2
Usage massage.
static void SetLevel(const TLogLevel actualLogLevel)
Set the log level.
Definition: Logger.cpp:53
TErrorMessage ERR_FMT_UNKNOW_PARAMETER_OR_ARGUMENT
Command line parameter error message.
void PrintComandlineParamers()
Print setup.
TErrorMessage ERR_FMT_NO_CHECKPOINT_FILE
Command line parameters error message.
bool benchmarkFlag
BenchmarkFlag value.
TErrorMessage ERR_FMT_NO_INPUT_FILE
Error message - input file was not specified.
int cudaDeviceIdx
Id of selected GPU devices.
TOutputMessage OUT_FMT_COMPRESSION_LEVEL
Output message.
static void ErrorAndTerminate(const std::string &errorMessage)
Log an error and terminate the execution.
Definition: Logger.cpp:94
bool store_p_min
Store_p_min value.
The header file containing the HDF5 related classes.
TOutputMessage OUT_FMT_SAMPLING_BEGINS_AT
Output message.
bool store_u_min_all
Store_u_min_all value.
Full level of verbosity.
Definition: Logger.h:67
bool printVersion
print version of the code and exit.
TOutputMessage OUT_FMT_OUTPUT_FILE
Output message.
bool IsCheckpointEnabled() const
Is checkpoint enabled?
TErrorMessage ERR_FMT_NO_THREAD_NUMBER
Command line parameters error message.
static const size_t DEFAULT_PROGRESS_PRINT_INTERVAL
Default progress print interval.
size_t progressPrintInterval
ProgressInterval value.
The header file containing a class responsible for printing out info and error messages (stdout...
TOutputMessage OUT_FMT_BENCHMARK_TIME_STEP
Output message.
std::string inputFileName
Input file name.
TOutputMessage OUT_FMT_USAGE_THREADS
Usage massage.
bool store_p_max_all
Store_p_max_all value.
bool store_u_max
Store_u_max value.
TErrorMessage FMT_NO_PROGRESS_PRINT_INTERVAL
Command line parameters error message.
std::string outputFileName
Output file name.
bool store_u_raw
Store_u_raw value.
size_t benchmarkTimeStepCount
BenchmarkTimeStepsCount value.
Basic (default) level of verbosity.
Definition: Logger.h:63
TCommandLineParameters()
Default constructor - only friend class can create an instance.
TErrorMessage ERR_FMT_NO_VERBOSE_LEVEL
Command line parameters error message.
bool store_u_max_all
Store_u_max_all value.
TErrorMessage ERR_FMT_NO_CHECKPOINT_INTERVAL
Command line parameters error message.
TErrorMessage ERR_FMT_NO_START_TIME_STEP
Command line parameters error message.
void PrintUsage()
Print usage..
bool store_p_rms
Store_p_rms value.
std::string checkpointFileName
Checkpoint file name.
TErrorMessage ERR_FMT_NO_OUTPUT_FILE
Command line parameters error message.
The header file containing the command line parameters.
Advanced level of verbosity.
Definition: Logger.h:65
TErrorMessage ERR_FMT_UNKNOWN_PARAMETER
Command line parameter error message.
static const size_t DEFAULT_COMPRESSION_LEVEL
Default compression level.
bool store_u_min
Store_u_min value.
static void Log(const TLogLevel queryLevel, const std::string &format, Args...args)
Log desired activity for a given log level, version with string format.
Definition: Logger.h:85
TOutputMessage OUT_FMT_SEPARATOR
Output message - separator.
TOutputMessage OUT_FMT_INPUT_FILE
Output message.
size_t startTimeStep
StartTimeStep value.
TErrorMessage ERR_FMT_NO_GPU_NUMBER
Command line parameters error message.
TOutputMessage OUT_FMT_SAMPLING_FLAGS
Output message.
TOutputMessage OUT_FMT_CHECKPOINT_FILE
Output message.
TOutputMessage OUT_FMT_COPY_SENSOR_MASK
Output message.
size_t compressionLevel
CompressionLevel value.
bool store_u_rms
Store_u_rms value.
void ParseCommandLine(int argc, char **argv)
Parse command line.