profile.txx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. SCTL_UNUSED(comm_);
  71. // sync_=true;
  72. prof.e_log.push_back(false);
  73. prof.s_log.push_back(sync_);
  74. prof.n_log.push_back(name_);
  75. prof.t_log.push_back(omp_get_wtime());
  76. prof.f_log.push_back(prof.FLOP);
  77. prof.m_log.push_back(prof.MEM);
  78. prof.max_m_log.push_back(prof.max_mem.back());
  79. #ifndef NDEBUG
  80. if (comm_ != nullptr && sync_) comm_->Barrier();
  81. #endif
  82. prof.name.pop();
  83. prof.comm.pop();
  84. prof.sync.pop();
  85. prof.max_mem.pop_back();
  86. #ifdef SCTL_VERBOSE
  87. Integer rank = 0;
  88. if (comm_ != nullptr) rank = comm_->Rank();
  89. if (!rank) {
  90. for (size_t i = 0; i < prof.name.size(); i++) std::cout << " ";
  91. std::cout << "}\n";
  92. }
  93. #endif
  94. prof.enable_depth--;
  95. }
  96. prof.verb_level.pop();
  97. }
  98. inline void Profile::print(const Comm* comm_) {
  99. ProfileData& prof = ProfData();
  100. SCTL_ASSERT_MSG(prof.name.empty(), "Missing balancing Toc()");
  101. Comm c_self = Comm::Self();
  102. if (comm_ == nullptr) comm_ = &c_self;
  103. comm_->Barrier();
  104. Integer np, rank;
  105. np = comm_->Size();
  106. rank = comm_->Rank();
  107. std::stack<double> tt;
  108. std::stack<Long> ff;
  109. std::stack<Long> mm;
  110. Integer width = 10;
  111. size_t level = 0;
  112. if (!rank && prof.e_log.size() > 0) {
  113. std::cout << "\n" << std::setw(width * 3 - 2 * level) << " ";
  114. if (np == 1) {
  115. std::cout << " " << std::setw(width) << "t";
  116. std::cout << " " << std::setw(width) << "f";
  117. std::cout << " " << std::setw(width) << "f/s";
  118. } else {
  119. std::cout << " " << std::setw(width) << "t_min";
  120. std::cout << " " << std::setw(width) << "t_avg";
  121. std::cout << " " << std::setw(width) << "t_max";
  122. std::cout << " " << std::setw(width) << "f_min";
  123. std::cout << " " << std::setw(width) << "f_avg";
  124. std::cout << " " << std::setw(width) << "f_max";
  125. std::cout << " " << std::setw(width) << "f/s_min";
  126. std::cout << " " << std::setw(width) << "f/s_max";
  127. std::cout << " " << std::setw(width) << "f/s_total";
  128. }
  129. std::cout << " " << std::setw(width) << "m_init";
  130. std::cout << " " << std::setw(width) << "m_max";
  131. std::cout << " " << std::setw(width) << "m_final" << '\n';
  132. }
  133. std::stack<std::string> out_stack;
  134. std::string s;
  135. out_stack.push(s);
  136. for (size_t i = 0; i < prof.e_log.size(); i++) {
  137. if (prof.e_log[i]) {
  138. level++;
  139. tt.push(prof.t_log[i]);
  140. ff.push(prof.f_log[i]);
  141. mm.push(prof.m_log[i]);
  142. std::string ss;
  143. out_stack.push(ss);
  144. } else {
  145. double t0 = prof.t_log[i] - tt.top();
  146. tt.pop();
  147. double f0 = (double)(prof.f_log[i] - ff.top()) * 1e-9;
  148. ff.pop();
  149. double fs0 = f0 / t0;
  150. double t_max, t_min, t_sum, t_avg;
  151. double f_max, f_min, f_sum, f_avg;
  152. double fs_max, fs_min, fs_sum; //, fs_avg;
  153. double m_init, m_max, m_final;
  154. comm_->Allreduce(Ptr2ConstItr<double>(&t0, 1), Ptr2Itr<double>(&t_max, 1), 1, Comm::CommOp::MAX);
  155. comm_->Allreduce(Ptr2ConstItr<double>(&f0, 1), Ptr2Itr<double>(&f_max, 1), 1, Comm::CommOp::MAX);
  156. comm_->Allreduce(Ptr2ConstItr<double>(&fs0, 1), Ptr2Itr<double>(&fs_max, 1), 1, Comm::CommOp::MAX);
  157. comm_->Allreduce(Ptr2ConstItr<double>(&t0, 1), Ptr2Itr<double>(&t_min, 1), 1, Comm::CommOp::MIN);
  158. comm_->Allreduce(Ptr2ConstItr<double>(&f0, 1), Ptr2Itr<double>(&f_min, 1), 1, Comm::CommOp::MIN);
  159. comm_->Allreduce(Ptr2ConstItr<double>(&fs0, 1), Ptr2Itr<double>(&fs_min, 1), 1, Comm::CommOp::MIN);
  160. comm_->Allreduce(Ptr2ConstItr<double>(&t0, 1), Ptr2Itr<double>(&t_sum, 1), 1, Comm::CommOp::SUM);
  161. comm_->Allreduce(Ptr2ConstItr<double>(&f0, 1), Ptr2Itr<double>(&f_sum, 1), 1, Comm::CommOp::SUM);
  162. m_final = (double)prof.m_log[i] * 1e-9;
  163. m_init = (double)mm.top() * 1e-9;
  164. mm.pop();
  165. m_max = (double)prof.max_m_log[i] * 1e-9;
  166. t_avg = t_sum / np;
  167. f_avg = f_sum / np;
  168. // fs_avg=f_avg/t_max;
  169. fs_sum = f_sum / t_max;
  170. if (!rank) {
  171. std::string s0 = out_stack.top();
  172. out_stack.pop();
  173. std::string s1 = out_stack.top();
  174. out_stack.pop();
  175. std::stringstream ss(std::stringstream::in | std::stringstream::out);
  176. ss << setiosflags(std::ios::fixed) << std::setprecision(4) << std::setiosflags(std::ios::left);
  177. for (size_t j = 0; j < level - 1; j++) {
  178. size_t l = i + 1;
  179. size_t k = level - 1;
  180. while (k > j && l < prof.e_log.size()) {
  181. k += (prof.e_log[l] ? 1 : -1);
  182. l++;
  183. }
  184. if (l < prof.e_log.size() ? prof.e_log[l] : false)
  185. ss << "| ";
  186. else
  187. ss << " ";
  188. }
  189. ss << "+-";
  190. ss << std::setw(width * 3 - 2 * level) << prof.n_log[i];
  191. ss << std::setiosflags(std::ios::right);
  192. if (np == 1) {
  193. ss << " " << std::setw(width) << t_avg;
  194. ss << " " << std::setw(width) << f_avg;
  195. ss << " " << std::setw(width) << fs_sum;
  196. } else {
  197. ss << " " << std::setw(width) << t_min;
  198. ss << " " << std::setw(width) << t_avg;
  199. ss << " " << std::setw(width) << t_max;
  200. ss << " " << std::setw(width) << f_min;
  201. ss << " " << std::setw(width) << f_avg;
  202. ss << " " << std::setw(width) << f_max;
  203. ss << " " << std::setw(width) << fs_min;
  204. // ss<<" "<<std::setw(width)<<fs_avg;
  205. ss << " " << std::setw(width) << fs_max;
  206. ss << " " << std::setw(width) << fs_sum;
  207. }
  208. ss << " " << std::setw(width) << m_init;
  209. ss << " " << std::setw(width) << m_max;
  210. ss << " " << std::setw(width) << m_final << '\n';
  211. s1 += ss.str() + s0;
  212. if (!s0.empty() && (i + 1 < prof.e_log.size() ? prof.e_log[i + 1] : false)) {
  213. for (size_t j = 0; j < level; j++) {
  214. size_t l = i + 1;
  215. size_t k = level - 1;
  216. while (k > j && l < prof.e_log.size()) {
  217. k += (prof.e_log[l] ? 1 : -1);
  218. l++;
  219. }
  220. if (l < prof.e_log.size() ? prof.e_log[l] : false)
  221. s1 += "| ";
  222. else
  223. s1 += " ";
  224. }
  225. s1 += "\n";
  226. } // */
  227. out_stack.push(s1);
  228. }
  229. level--;
  230. }
  231. }
  232. if (!rank) std::cout << out_stack.top() << '\n';
  233. reset();
  234. }
  235. inline void Profile::reset() {
  236. ProfileData& prof = ProfData();
  237. prof.MEM = 0;
  238. prof.FLOP = 0;
  239. while (!prof.sync.empty()) prof.sync.pop();
  240. while (!prof.name.empty()) prof.name.pop();
  241. while (!prof.comm.empty()) prof.comm.pop();
  242. prof.e_log.clear();
  243. prof.s_log.clear();
  244. prof.n_log.clear();
  245. prof.t_log.clear();
  246. prof.f_log.clear();
  247. prof.m_log.clear();
  248. prof.max_m_log.clear();
  249. }
  250. #else
  251. inline Long Profile::Add_FLOP(Long inc) { return 0; }
  252. inline Long Profile::Add_MEM(Long inc) { return 0; }
  253. inline bool Profile::Enable(bool state) { return false; }
  254. inline void Profile::Tic(const char* name_, const Comm* comm_, bool sync_, Integer verbose) { }
  255. inline void Profile::Toc() { }
  256. inline void Profile::print(const Comm* comm_) { }
  257. inline void Profile::reset() { }
  258. #endif
  259. } // end namespace