profile.txx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #include SCTL_INCLUDE(comm.hpp)
  2. #include <omp.h>
  3. #include <iostream>
  4. #include <sstream>
  5. #include <iomanip>
  6. #include <cassert>
  7. #include <cstdlib>
  8. namespace SCTL_NAMESPACE {
  9. #if SCTL_PROFILE >= 0
  10. inline Long Profile::Add_MEM(Long inc) {
  11. std::vector<Long>& max_mem = ProfData().max_mem;
  12. Long& MEM = ProfData().MEM;
  13. Long orig_val = MEM;
  14. #pragma omp atomic update
  15. MEM += inc;
  16. for (Integer i = max_mem.size() - 1; i >= 0 && max_mem[i] < MEM; i--) max_mem[i] = MEM;
  17. return orig_val;
  18. }
  19. inline Long Profile::Add_FLOP(Long inc) {
  20. Long& FLOP = ProfData().FLOP;
  21. Long orig_val = FLOP;
  22. #pragma omp atomic update
  23. FLOP += inc;
  24. return orig_val;
  25. }
  26. inline bool Profile::Enable(bool state) {
  27. bool& enable_state = ProfData().enable_state;
  28. bool orig_val = enable_state;
  29. enable_state = state;
  30. return orig_val;
  31. }
  32. inline void Profile::Tic(const char* name_, const Comm* comm_, bool sync_, Integer verbose) {
  33. ProfileData& prof = ProfData();
  34. if (!prof.enable_state) return;
  35. // sync_=true;
  36. if (verbose <= SCTL_PROFILE && (Integer)prof.verb_level.size() == prof.enable_depth) {
  37. if (comm_ != nullptr && sync_) comm_->Barrier();
  38. #ifdef SCTL_VERBOSE
  39. Integer rank = 0;
  40. if (comm_ != nullptr) rank = comm_->Rank();
  41. if (!rank) {
  42. for (size_t i = 0; i < prof.name.size(); i++) std::cout << " ";
  43. std::cout << "\033[1;31m" << name_ << "\033[0m {\n";
  44. }
  45. #endif
  46. prof.name.push(name_);
  47. prof.comm.push(comm_);
  48. prof.sync.push(sync_);
  49. prof.max_mem.push_back(prof.MEM);
  50. prof.e_log.push_back(true);
  51. prof.s_log.push_back(sync_);
  52. prof.n_log.push_back(prof.name.top());
  53. prof.t_log.push_back(omp_get_wtime());
  54. prof.f_log.push_back(prof.FLOP);
  55. prof.m_log.push_back(prof.MEM);
  56. prof.max_m_log.push_back(prof.MEM);
  57. prof.enable_depth++;
  58. }
  59. prof.verb_level.push(verbose);
  60. }
  61. inline void Profile::Toc() {
  62. ProfileData& prof = ProfData();
  63. if (!prof.enable_state) return;
  64. SCTL_ASSERT_MSG(!prof.verb_level.empty(), "Unbalanced extra Toc()");
  65. if (prof.verb_level.top() <= SCTL_PROFILE && (Integer)prof.verb_level.size() == prof.enable_depth) {
  66. SCTL_ASSERT_MSG(!prof.name.empty() && !prof.comm.empty() && !prof.sync.empty() && !prof.max_mem.empty(), "Unbalanced extra Toc()");
  67. std::string name_ = prof.name.top();
  68. const Comm* comm_ = prof.comm.top();
  69. bool sync_ = prof.sync.top();
  70. // sync_=true;
  71. prof.e_log.push_back(false);
  72. prof.s_log.push_back(sync_);
  73. prof.n_log.push_back(name_);
  74. prof.t_log.push_back(omp_get_wtime());
  75. prof.f_log.push_back(prof.FLOP);
  76. prof.m_log.push_back(prof.MEM);
  77. prof.max_m_log.push_back(prof.max_mem.back());
  78. #ifndef NDEBUG
  79. if (comm_ != nullptr && sync_) comm_->Barrier();
  80. #endif
  81. prof.name.pop();
  82. prof.comm.pop();
  83. prof.sync.pop();
  84. prof.max_mem.pop_back();
  85. #ifdef SCTL_VERBOSE
  86. Integer rank = 0;
  87. if (comm_ != nullptr) rank = comm_->Rank();
  88. if (!rank) {
  89. for (size_t i = 0; i < prof.name.size(); i++) std::cout << " ";
  90. std::cout << "}\n";
  91. }
  92. #endif
  93. prof.enable_depth--;
  94. }
  95. prof.verb_level.pop();
  96. }
  97. inline void Profile::print(const Comm* comm_) {
  98. ProfileData& prof = ProfData();
  99. SCTL_ASSERT_MSG(prof.name.empty(), "Missing balancing Toc()");
  100. Comm c_self = Comm::Self();
  101. if (comm_ == nullptr) comm_ = &c_self;
  102. comm_->Barrier();
  103. Integer np, rank;
  104. np = comm_->Size();
  105. rank = comm_->Rank();
  106. std::stack<double> tt;
  107. std::stack<Long> ff;
  108. std::stack<Long> mm;
  109. Integer width = 10;
  110. size_t level = 0;
  111. if (!rank && prof.e_log.size() > 0) {
  112. std::cout << "\n" << std::setw(width * 3 - 2 * level) << " ";
  113. if (np == 1) {
  114. std::cout << " " << std::setw(width) << "t";
  115. std::cout << " " << std::setw(width) << "f";
  116. std::cout << " " << std::setw(width) << "f/s";
  117. } else {
  118. std::cout << " " << std::setw(width) << "t_min";
  119. std::cout << " " << std::setw(width) << "t_avg";
  120. std::cout << " " << std::setw(width) << "t_max";
  121. std::cout << " " << std::setw(width) << "f_min";
  122. std::cout << " " << std::setw(width) << "f_avg";
  123. std::cout << " " << std::setw(width) << "f_max";
  124. std::cout << " " << std::setw(width) << "f/s_min";
  125. std::cout << " " << std::setw(width) << "f/s_max";
  126. std::cout << " " << std::setw(width) << "f/s_total";
  127. }
  128. std::cout << " " << std::setw(width) << "m_init";
  129. std::cout << " " << std::setw(width) << "m_max";
  130. std::cout << " " << std::setw(width) << "m_final" << '\n';
  131. }
  132. std::stack<std::string> out_stack;
  133. std::string s;
  134. out_stack.push(s);
  135. for (size_t i = 0; i < prof.e_log.size(); i++) {
  136. if (prof.e_log[i]) {
  137. level++;
  138. tt.push(prof.t_log[i]);
  139. ff.push(prof.f_log[i]);
  140. mm.push(prof.m_log[i]);
  141. std::string ss;
  142. out_stack.push(ss);
  143. } else {
  144. double t0 = prof.t_log[i] - tt.top();
  145. tt.pop();
  146. double f0 = (double)(prof.f_log[i] - ff.top()) * 1e-9;
  147. ff.pop();
  148. double fs0 = f0 / t0;
  149. double t_max, t_min, t_sum, t_avg;
  150. double f_max, f_min, f_sum, f_avg;
  151. double fs_max, fs_min, fs_sum; //, fs_avg;
  152. double m_init, m_max, m_final;
  153. comm_->Allreduce(Ptr2ConstItr<double>(&t0, 1), Ptr2Itr<double>(&t_max, 1), 1, Comm::CommOp::MAX);
  154. comm_->Allreduce(Ptr2ConstItr<double>(&f0, 1), Ptr2Itr<double>(&f_max, 1), 1, Comm::CommOp::MAX);
  155. comm_->Allreduce(Ptr2ConstItr<double>(&fs0, 1), Ptr2Itr<double>(&fs_max, 1), 1, Comm::CommOp::MAX);
  156. comm_->Allreduce(Ptr2ConstItr<double>(&t0, 1), Ptr2Itr<double>(&t_min, 1), 1, Comm::CommOp::MIN);
  157. comm_->Allreduce(Ptr2ConstItr<double>(&f0, 1), Ptr2Itr<double>(&f_min, 1), 1, Comm::CommOp::MIN);
  158. comm_->Allreduce(Ptr2ConstItr<double>(&fs0, 1), Ptr2Itr<double>(&fs_min, 1), 1, Comm::CommOp::MIN);
  159. comm_->Allreduce(Ptr2ConstItr<double>(&t0, 1), Ptr2Itr<double>(&t_sum, 1), 1, Comm::CommOp::SUM);
  160. comm_->Allreduce(Ptr2ConstItr<double>(&f0, 1), Ptr2Itr<double>(&f_sum, 1), 1, Comm::CommOp::SUM);
  161. m_final = (double)prof.m_log[i] * 1e-9;
  162. m_init = (double)mm.top() * 1e-9;
  163. mm.pop();
  164. m_max = (double)prof.max_m_log[i] * 1e-9;
  165. t_avg = t_sum / np;
  166. f_avg = f_sum / np;
  167. // fs_avg=f_avg/t_max;
  168. fs_sum = f_sum / t_max;
  169. if (!rank) {
  170. std::string s0 = out_stack.top();
  171. out_stack.pop();
  172. std::string s1 = out_stack.top();
  173. out_stack.pop();
  174. std::stringstream ss(std::stringstream::in | std::stringstream::out);
  175. ss << setiosflags(std::ios::fixed) << std::setprecision(4) << std::setiosflags(std::ios::left);
  176. for (size_t j = 0; j < level - 1; j++) {
  177. size_t l = i + 1;
  178. size_t k = level - 1;
  179. while (k > j && l < prof.e_log.size()) {
  180. k += (prof.e_log[l] ? 1 : -1);
  181. l++;
  182. }
  183. if (l < prof.e_log.size() ? prof.e_log[l] : false)
  184. ss << "| ";
  185. else
  186. ss << " ";
  187. }
  188. ss << "+-";
  189. ss << std::setw(width * 3 - 2 * level) << prof.n_log[i];
  190. ss << std::setiosflags(std::ios::right);
  191. if (np == 1) {
  192. ss << " " << std::setw(width) << t_avg;
  193. ss << " " << std::setw(width) << f_avg;
  194. ss << " " << std::setw(width) << fs_sum;
  195. } else {
  196. ss << " " << std::setw(width) << t_min;
  197. ss << " " << std::setw(width) << t_avg;
  198. ss << " " << std::setw(width) << t_max;
  199. ss << " " << std::setw(width) << f_min;
  200. ss << " " << std::setw(width) << f_avg;
  201. ss << " " << std::setw(width) << f_max;
  202. ss << " " << std::setw(width) << fs_min;
  203. // ss<<" "<<std::setw(width)<<fs_avg;
  204. ss << " " << std::setw(width) << fs_max;
  205. ss << " " << std::setw(width) << fs_sum;
  206. }
  207. ss << " " << std::setw(width) << m_init;
  208. ss << " " << std::setw(width) << m_max;
  209. ss << " " << std::setw(width) << m_final << '\n';
  210. s1 += ss.str() + s0;
  211. if (!s0.empty() && (i + 1 < prof.e_log.size() ? prof.e_log[i + 1] : false)) {
  212. for (size_t j = 0; j < level; j++) {
  213. size_t l = i + 1;
  214. size_t k = level - 1;
  215. while (k > j && l < prof.e_log.size()) {
  216. k += (prof.e_log[l] ? 1 : -1);
  217. l++;
  218. }
  219. if (l < prof.e_log.size() ? prof.e_log[l] : false)
  220. s1 += "| ";
  221. else
  222. s1 += " ";
  223. }
  224. s1 += "\n";
  225. } // */
  226. out_stack.push(s1);
  227. }
  228. level--;
  229. }
  230. }
  231. if (!rank) std::cout << out_stack.top() << '\n';
  232. reset();
  233. }
  234. inline void Profile::reset() {
  235. ProfileData& prof = ProfData();
  236. prof.MEM = 0;
  237. prof.FLOP = 0;
  238. while (!prof.sync.empty()) prof.sync.pop();
  239. while (!prof.name.empty()) prof.name.pop();
  240. while (!prof.comm.empty()) prof.comm.pop();
  241. prof.e_log.clear();
  242. prof.s_log.clear();
  243. prof.n_log.clear();
  244. prof.t_log.clear();
  245. prof.f_log.clear();
  246. prof.m_log.clear();
  247. prof.max_m_log.clear();
  248. }
  249. #else
  250. inline Long Profile::Add_FLOP(Long inc) { return 0; }
  251. inline Long Profile::Add_MEM(Long inc) { return 0; }
  252. inline bool Profile::Enable(bool state) { return false; }
  253. inline void Profile::Tic(const char* name_, const Comm* comm_, bool sync_, Integer verbose) { }
  254. inline void Profile::Toc() { }
  255. inline void Profile::print(const Comm* comm_) { }
  256. inline void Profile::reset() { }
  257. #endif
  258. } // end namespace