MiscTools.hpp
1 #ifndef __DMRG_MISCTOOLS_HPP__
2 #define __DMRG_MISCTOOLS_HPP__
3 
4 #include <petscsys.h>
5 #include <slepceps.h>
6 
7 #include <map>
8 #include <string>
9 #include <fstream>
10 #include <iostream>
11 
17 // #define DMRG_KRON_TIMINGS
18 
19 #if defined(DMRG_KRON_TIMINGS)
20  #include <petsctime.h>
21  #define TIMINGS_NEWLINE() \
22  if(!mpi_rank) printf("\n");
23  #define FUNCTION_TIMINGS_BEGIN() \
24  PetscLogDouble tstart = 0.0, tend = 0.0; \
25  if(!mpi_rank) PetscTime(&tstart);
26  #define FUNCTION_TIMINGS_END() \
27  if(!mpi_rank){ \
28  PetscTime(&tend); \
29  printf(" %-28s %-12.6f s\n", __FUNCTION__, tend - tstart); \
30  }
31  #define FUNCTION_TIMINGS_PRINT_SPACE() if(!mpi_rank) printf("\n");
32  #define INTERVAL_TIMINGS_SETUP() PetscLogDouble itstart = 0.0, itend = 0.0;
33  #define INTERVAL_TIMINGS_BEGIN() if(!mpi_rank) PetscTime(&itstart);
34  #define INTERVAL_TIMINGS_END(LABEL) \
35  if(!mpi_rank){ \
36  PetscTime(&itend); \
37  printf(" %-28s %-12.6f s\n", LABEL, itend - itstart); \
38  }
39  #define ACCUM_TIMINGS_SETUP(LABEL) PetscLogDouble ts_##LABEL = 0.0, te_##LABEL = 0.0, tot_##LABEL = 0.0;
40  #define ACCUM_TIMINGS_BEGIN(LABEL) if(!mpi_rank){ PetscTime(&ts_##LABEL); }
41  #define ACCUM_TIMINGS_END(LABEL) if(!mpi_rank){ PetscTime(&te_##LABEL); \
42  tot_##LABEL += (te_##LABEL - ts_##LABEL); }
43  #define ACCUM_TIMINGS_PRINT(LABEL, TEXT) \
44  if(!mpi_rank){ \
45  printf(" %-28s %-12.6f s\n", TEXT, tot_##LABEL); \
46  }
47 #else
48  #define TIMINGS_NEWLINE()
49  #define FUNCTION_TIMINGS_BEGIN()
50  #define FUNCTION_TIMINGS_END()
51  #define FUNCTION_TIMINGS_PRINT_SPACE()
52  #define INTERVAL_TIMINGS_SETUP()
53  #define INTERVAL_TIMINGS_BEGIN()
54  #define INTERVAL_TIMINGS_END(LABEL)
55  #define ACCUM_TIMINGS_SETUP(LABEL)
56  #define ACCUM_TIMINGS_BEGIN(LABEL)
57  #define ACCUM_TIMINGS_END(LABEL)
58  #define ACCUM_TIMINGS_PRINT(LABEL, TEXT)
59 #endif
60 
65 PetscErrorCode PreSplitOwnership(const MPI_Comm comm, const PetscInt N, PetscInt& locrows, PetscInt& Istart);
66 
67 PetscErrorCode SplitOwnership(
68  const PetscMPIInt& rank,
69  const PetscMPIInt& nprocs ,
70  const PetscInt N,
71  PetscInt& locrows,
72  PetscInt& Istart);
73 
78 template< typename T >
79 PetscErrorCode RetrieveInfoFile(
80  const MPI_Comm& mpi_comm,
81  const std::string& filename,
82  std::map< std::string, T >& infomap
83  )
84 {
85  PetscErrorCode ierr;
86  PetscMPIInt mpi_rank;
87  ierr = MPI_Comm_rank(mpi_comm, &mpi_rank); CHKERRQ(ierr);
88 
89  /* Read the file and build a key-value pair */
90  PetscBool flg;
91  std::string opt_string;
92  PetscInt opt_string_length;
93 
94  if(!mpi_rank) {
95  ierr = PetscTestFile(filename.c_str(), 'r', &flg); CHKERRQ(ierr);
96  if(!flg) SETERRQ1(PETSC_COMM_SELF,1,"File %s unaccessible.", filename.c_str());
97  std::ifstream infofile(filename.c_str());
98  opt_string = std::string(
99  (std::istreambuf_iterator<char>(infofile)),
100  std::istreambuf_iterator<char>());
101  infofile.close();
102  opt_string_length = opt_string.size();
103  }
104 
105  ierr = MPI_Bcast(&opt_string_length, 1, MPIU_INT, 0, mpi_comm); CHKERRQ(ierr);
106  opt_string.resize(opt_string_length);
107  ierr = MPI_Bcast(&opt_string[0], opt_string_length, MPI_CHAR, 0, mpi_comm); CHKERRQ(ierr);
108 
109  std::stringstream ss(opt_string);
110  std::string line;
111  infomap.clear();
112  while(std::getline(ss, line)) {
113  std::string key;
114  T val;
115  std::istringstream line_ss(line);
116  line_ss >> key >> val;
117  infomap[key] = val;
118  }
119 
120  return(0);
121 }
122 
131 PetscErrorCode SetOptionsFromFile(
132  MPI_Comm& mpi_comm,
133  const std::string& filename
134  );
135 
140 #endif // __DMRG_MISCTOOLS_HPP__
PetscErrorCode SetOptionsFromFile(MPI_Comm &mpi_comm, const std::string &filename)
Reads a file containing keys and values and sets them as command line arguments.
Definition: MiscTools.cpp:329
PetscErrorCode RetrieveInfoFile(const MPI_Comm &mpi_comm, const std::string &filename, std::map< std::string, T > &infomap)
Utility to retrieve the contents of a key-value data file where the keys are strings and the values a...
Definition: MiscTools.hpp:79
This site was generated by Sphinx using Doxygen with a customized theme from doxygen-bootstrapped.