SUNphi  1.0
SUNphi.cpp
Go to the documentation of this file.
1 #ifdef HAVE_CONFIG_H
2  #include "config.hpp"
3 #endif
4 
5 /// \file SUNphi.cpp
6 ///
7 /// \brief Implements the parts of code which require dedicated compilation units
8 
9 #include <cstdarg>
10 #include <cstdio>
11 
12 #include <gitInfo.hpp>
13 
14 #include <Threads.hpp>
15 #include <debug/Crash.hpp>
16 #include <ios/Logger.hpp>
17 #include <ios/MinimalLogger.hpp>
18 #include <ios/TextFormat.hpp>
19 #include <random/TrueRandomGenerator.hpp>
20 #include <system/Memory.hpp>
21 #include <system/Mpi.hpp>
22 #include <system/Timer.hpp>
23 #include <utility/Aliver.hpp>
24 #include <utility/SingleInstance.hpp>
25 
26 #ifndef CONFIG_TIME
27  /// Null time
28  #define CONFIG_TIME
29  ""
30 #endif
31 
32 #ifndef CONFIG_FLAGS
33  /// Null flags
34  #define CONFIG_FLAGS
35  ""
36 #endif
37 
38 namespace SUNphi
39 {
40  void minimalLogger(Logger& logger,
41  const char* format,
42  ...)
43  {
44  /// Starts the variadic arguments
45  va_list ap;
46  va_start(ap,format);
47 
48  logger.printVariadicMessage(format,ap);
49 
50  va_end(ap);
51  }
52 
53  void minimalCrash(const char* path,
54  const int line,
55  const char* funcName,
56  const char* format,
57  ...)
58  {
59  /// Starts the variadic arguments
60  va_list ap;
61  va_start(ap,format);
62 
63  (runLog()<<Crasher(path,line,funcName)).printVariadicMessage(format,ap);
64 
65  va_end(ap);
66  }
67 
68 #ifdef USE_THREADS
69  void ThreadPool::fill(const pthread_attr_t* attr)
70  {
71  {
73 
74  runLog()<<"Filling the thread pool with "<<nThreads<<" threads";
75 
76  // Checks that the pool is not filled
77  if(isFilled)
78  MINIMAL_CRASH("Cannot fill again the pool!");
79 
80  // Resize the pool to contain all threads
81  pool.resize(nThreads,0);
82 
83  // Marks the pool as filled, even if we are still filling it, this will keep the threads swimming
84  isFilled=
85  true;
86 
87  for(int threadId=1;threadId<nThreads;threadId++)
88  {
89  //runLog()<<"thread of id "<<threadId<<" spwawned\n";
90 
91  // Allocates the parameters of the thread
92  ThreadPars* pars=
93  new ThreadPars{this,threadId};
94 
95  if(pthread_create(&pool[threadId],attr,threadPoolSwim,pars)!=0)
96  MINIMAL_CRASH_STDLIBERR("creating the thread");
97  }
98 
99  waitPoolToBeFilled(masterThreadId);
100  }
101 
102  // Marks the pool is waiting for job to be done
103  isWaitingForWork=
104  true;
105  }
106 #endif // USE_THREADS
107 
108  int aliverHelper()
109  {
110  return
111  0;
112  }
113 
114  /// Class used to provocate initialization of Mpi
115  class Aliver : public SingleInstance<Aliver>
116  {
117 
118  /// Prints the banner
119  void printBanner()
120  const
121  {
122  runLog()<<"";
123  runLog()<<TextColor::BROWN<<" ▄▄ ▄█▄ ▄▄ \t"<<TextColor::PURPLE<< " ▄█▄ ";
124  runLog()<<TextColor::BROWN<<" █░█ █░█ █░█ \t"<<TextColor::PURPLE<< " █░█ ";
125  runLog()<<TextColor::BROWN<<" ▄▄ █░█ █░█ █░█ ▄▄ \t"<<TextColor::PURPLE<< " █░█ ";
126  runLog()<<TextColor::BROWN<<" █░█ █░█ █░█ █░█ █░█ \t"<<TextColor::PURPLE<< " █░█ ";
127  runLog()<<TextColor::BROWN<<" █░█ █░█ ███████ █░█ █░█ \t"<<TextColor::PURPLE<< " ███████ ";
128  runLog()<<TextColor::BROWN<<" █░█ █████░░░░░█████ █░█ \t"<<TextColor::PURPLE<< " █████░█░█░█████ ";
129  runLog()<<TextColor::BROWN<<" █░█ ██░░░░░░░░░░░░░██ █░█ \t"<<TextColor::PURPLE<< " ██░░░░░█░█░░░░░██ ";
130  runLog()<<TextColor::BROWN<<" █░██░░░░░░░░░░░░░░░░░██░█ \t"<<TextColor::PURPLE<< " ██░░░░░░░█░█░░░░░░░██ ";
131  runLog()<<TextColor::BROWN<<" ▄▄▄▄▄▄███████████░███████████▄▄▄▄▄▄ \t" <<TextColor::PURPLE<< " ██░░░░░░░░█░█░░░░░░░░██ ";
132  runLog()<<TextColor::BROWN<<" █░░░░░░█░████████░░░████████░█░░░░░░█ \t"<<TextColor::PURPLE<< " █░░░░░░░░░█░█░░░░░░░░░█ ";
133  runLog()<<TextColor::BROWN<<" ▀▀▀▀▀▀█░░░████░░░░░░░████░░░█▀▀▀▀▀▀ \t" <<TextColor::PURPLE<< " █░░░░░░░░░█░█░░░░░░░░░█ ";
134  runLog()<<TextColor::BROWN<<" ██░░░░░░░░░░░░░░░░░░░░█ \t"<<TextColor::PURPLE<< " ██░░░░░░░░█░█░░░░░░░░░█ ";
135  runLog()<<TextColor::BROWN<<" █░██░░░░░███████░░░░░░█░█ \t"<<TextColor::PURPLE<< " ██░░░░░░░█░█░░░░░░░░█ ";
136  runLog()<<TextColor::BROWN<<" █░█ █░░░░░░░░░░░░░░░██ █░█ \t"<<TextColor::PURPLE<< " █░░░░░░█░█░░░░░░██ ";
137  runLog()<<TextColor::BROWN<<" █░█ ██░░░░░░░░░░░██ █░█ \t"<<TextColor::PURPLE<< " ██░░░░█░█░░░░██ ";
138  runLog()<<TextColor::BROWN<<" █░█ █░███████████░█ █░█ \t"<<TextColor::PURPLE<< " ███████████ ";
139  runLog()<<TextColor::BROWN<<" █░█ █░█ █░█ █░█ █░█ \t"<<TextColor::PURPLE<< " █░█ ";
140  runLog()<<TextColor::BROWN<<" ▀▀ █░█ █░█ █░█ ▀▀ \t" <<TextColor::PURPLE<< " █░█ ";
141  runLog()<<TextColor::BROWN<<" █░█ █░█ █░█ \t" <<TextColor::PURPLE<< " █░█ ";
142  runLog()<<TextColor::BROWN<<" █░█ █░█ █░█ \t" <<TextColor::PURPLE<< " █░█ ";
143  runLog()<<TextColor::BROWN<<" ▀▀ ▀█▀ ▀▀ \t" <<TextColor::PURPLE<< " ▀█▀ ";
144  runLog()<< "";
145  }
146 
147  /// Prints the version, and contacts
149  const
150  {
151  runLog()<<"\nInitializing "<<PACKAGE_NAME<<" library, v"<<PACKAGE_VERSION<<", send bug report to <"<<PACKAGE_BUGREPORT<<">";
152  }
153 
154  /// Prints the git info
156  const
157  {
158  runLog()<<"Commit "<<GIT_HASH<<" made at "<<GIT_TIME<<" by "<<GIT_COMMITTER<<" with message: \""<<GIT_LOG<<"\"";
159  }
160 
161  /// Prints configure info
163  const
164  {
165  runLog()<<"Configured at "<<CONFIG_TIME<<" with flags: "<<CONFIG_FLAGS<<"";
166  }
167 
168  /// Says bye bye
170  const
171  {
172  runLog()<<"\n Ciao!\n";
173  }
174 
175  public:
176 
177  /// Creates
179  {
184 
185  threads.workOn([](const int threadID){runLog()<<"ANNA";});
186 
187  {
190 
191  threads.workOn([](const int threadID){runLog()<<"ANNA";});
192  }
193  // threads.loopSplit(0,10,[](const int& rank,const int& i){printf("Rank %d prints again %d\n",rank,i);});
194  }
195 
196  /// Destroyer
198  {
200  }
201  };
202 
203  /// Global timings
204  Timer timings("Total time",Timer::NO_FATHER,Timer::UNSTOPPABLE);
205 
206  Logger Logger::fakeLogger("/dev/null");
207 
208  /// Global logger
209  Logger runLog("/dev/stdout");
210 
211  /// Global MPI
212  Mpi mpi;
213 
214  /// Global thrads
215  ThreadPool threads;
216 
217  /// Global true random generator
218  TrueRandomGenerator trueRandomGenerator;
219 
220  /// Memory manager
221  Memory memory;
222 
223  /// Presentation of the library
224  Aliver aliver;
225 }
#define MINIMAL_CRASH(...)
Initialize the minimal crasher.
Mpi mpi
Gloabl MPI.
Definition: SUNphi.cpp:212
#define GIT_COMMITTER
Definition: gitInfo.hpp:6
LoggerLine printVariadicMessage(const char *format, va_list ap)
Print a C-style variadic message.
Definition: Logger.hpp:386
void minimalCrash(const char *path, const int line, const char *funcName, const char *format,...)
Definition: SUNphi.cpp:53
#define PACKAGE_NAME
Definition: config.hpp:68
static Logger fakeLogger
Fake logger, printing to /dev/null.
Definition: Logger.hpp:39
#define GIT_LOG
Definition: gitInfo.hpp:7
void printBailout() const
Says bye bye.
Definition: SUNphi.cpp:169
void minimalLogger(Logger &logger, const char *format,...)
Definition: SUNphi.cpp:40
void printVersionContacts() const
Prints the version, and contacts.
Definition: SUNphi.cpp:148
#define GIT_TIME
Definition: gitInfo.hpp:5
#define ALLOWS_ALL_THREADS_TO_PRINT_FOR_THIS_SCOPE(LOGGER)
Makes all thread print for current scope.
Definition: Thread.hpp:32
decltype(auto) father(Ts &&...ts)
Definition: Timer.hpp:151
Logger(const char *path, const bool &prependTime=true)
Create with a path.
Definition: Logger.hpp:394
Aliver()
Creates.
Definition: SUNphi.cpp:178
void printBanner() const
Prints the banner.
Definition: SUNphi.cpp:119
~Aliver()
Destroyer.
Definition: SUNphi.cpp:197
#define GIT_HASH
Definition: gitInfo.hpp:4
#define MINIMAL_CRASH_STDLIBERR(STRING)
Minimal crash with stdlib error.
Class used to provocate initialization of Mpi.
Definition: SUNphi.cpp:115
constexpr int masterThreadId
Thread id of master thread.
Definition: Thread.hpp:24
Logger runLog("/dev/stdout")
Global logger.
#define PACKAGE_BUGREPORT
Definition: config.hpp:65
void printGitInfo() const
Prints the git info.
Definition: SUNphi.cpp:155
decltype(auto) file(Ts &&...ts)
Definition: Logger.hpp:49
void printConfigurePars() const
Prints configure info.
Definition: SUNphi.cpp:162
#define ALLOWS_ALL_RANKS_TO_PRINT_FOR_THIS_SCOPE(LOGGER)
Makes all thread print for current scope.
Definition: Mpi.hpp:27
#define CONFIG_TIME
Null time.
Definition: SUNphi.cpp:28
#define CONFIG_FLAGS
Null flags.
Definition: SUNphi.cpp:34
Timer timings("Total time", Timer::NO_FATHER, Timer::UNSTOPPABLE)
Global timings.
void broadcast(T &&val, int root=MASTER_RANK) const
Definition: Mpi.hpp:295
ThreadPool threads
Global thread pool.
Definition: SUNphi.cpp:215
#define PACKAGE_VERSION
Definition: config.hpp:80