diff --git a/source/source_base/timer.cpp b/source/source_base/timer.cpp index 8ddeb848a9d..4d85bd7cd3d 100644 --- a/source/source_base/timer.cpp +++ b/source/source_base/timer.cpp @@ -32,10 +32,10 @@ std::map> timer::timer_pool; void timer::finish(std::ofstream &ofs, const bool print_flag, const bool check_end) { - if(!timer_pool[""]["total"].start_flag) - { timer::end("","total"); } - if(print_flag) - { print_all( ofs, check_end ); } + if(!timer_pool[""]["total"].start_flag) + { timer::end("","total"); } + if(print_flag) + { print_all( ofs, check_end ); } } //---------------------------------------------------------- @@ -43,9 +43,9 @@ void timer::finish(std::ofstream &ofs, const bool print_flag, const bool check_e //---------------------------------------------------------- void timer::start() { - // first init ,then we can use tick - if(timer_pool[""]["total"].start_flag) - { timer::start("","total"); } + // first init ,then we can use tick + if(timer_pool[""]["total"].start_flag) + { timer::start("","total"); } } double timer::cpu_time() @@ -55,10 +55,10 @@ double timer::cpu_time() // only first call can let t0 = 0,clock begin // when enter this function second time , t0 > 0 //---------------------------------------------------------- - static auto t1 = std::chrono::system_clock::now(); - const auto t2 = std::chrono::system_clock::now(); - auto duration = std::chrono::duration_cast(t2 - t1); - return double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den; + static auto t1 = std::chrono::system_clock::now(); + const auto t2 = std::chrono::system_clock::now(); + auto duration = std::chrono::duration_cast(t2 - t1); + return double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den; } void timer::start(const std::string &class_name,const std::string &name) @@ -66,15 +66,15 @@ void timer::start(const std::string &class_name,const std::string &name) //---------------------------------------------------------- // EXPLAIN : if timer is disabled , return //---------------------------------------------------------- - if (disabled) - { return; } + if (disabled) + { return; } - #ifdef _OPENMP - if(omp_get_thread_num()) - { return; } - #endif + #ifdef _OPENMP + if(omp_get_thread_num()) + { return; } + #endif - Timer_One &timer_one = timer_pool[class_name][name]; + Timer_One &timer_one = timer_pool[class_name][name]; //---------------------------------------------------------- // CALL MEMBER FUNCTION : @@ -86,24 +86,24 @@ void timer::start(const std::string &class_name,const std::string &name) // if start_flag == false, means it's the end of this counting, // so we add the time during this two 'time point' to the clock time storage. //---------------------------------------------------------- - if(!timer_one.start_flag) - { throw std::runtime_error("timer::start " + class_name + "::" + name); } - #ifdef __MPI - int is_initialized = 0; - MPI_Initialized(&is_initialized); - if(is_initialized) - { timer_one.cpu_start = MPI_Wtime(); } - #else - timer_one.cpu_start = cpu_time(); - #endif - ++timer_one.calls; - timer_one.start_flag = false; - #if defined(__CUDA) && defined(__USE_NVTX) + if(!timer_one.start_flag) + { throw std::runtime_error("timer::start " + class_name + "::" + name); } + #ifdef __MPI + int is_initialized = 0; + MPI_Initialized(&is_initialized); + if(is_initialized) + { timer_one.cpu_start = MPI_Wtime(); } + #else + timer_one.cpu_start = cpu_time(); + #endif + ++timer_one.calls; + timer_one.start_flag = false; + #if defined(__CUDA) && defined(__USE_NVTX) if (PARAM.inp.timer_enable_nvtx){ std::string label = class_name + ":" + name; nvtxRangePushA(label.data()); } - #endif + #endif } void timer::end(const std::string &class_name,const std::string &name) @@ -111,15 +111,15 @@ void timer::end(const std::string &class_name,const std::string &name) //---------------------------------------------------------- // EXPLAIN : if timer is disabled , return //---------------------------------------------------------- - if (disabled) - { return; } + if (disabled) + { return; } - #ifdef _OPENMP - if(omp_get_thread_num()) - { return; } - #endif + #ifdef _OPENMP + if(omp_get_thread_num()) + { return; } + #endif - Timer_One &timer_one = timer_pool[class_name][name]; + Timer_One &timer_one = timer_pool[class_name][name]; //---------------------------------------------------------- // CALL MEMBER FUNCTION : @@ -131,224 +131,224 @@ void timer::end(const std::string &class_name,const std::string &name) // if start_flag == false, means it's the end of this counting, // so we add the time during this two 'time point' to the clock time storage. //---------------------------------------------------------- - if(timer_one.start_flag) - { throw std::runtime_error("timer::end " + class_name + "::" + name); } - #ifdef __MPI - int is_initialized = 0; - MPI_Initialized(&is_initialized); - if(is_initialized) - { timer_one.cpu_second += MPI_Wtime() - timer_one.cpu_start; } - #else - timer_one.cpu_second += (cpu_time() - timer_one.cpu_start); - #endif - timer_one.start_flag = true; - #if defined(__CUDA) && defined(__USE_NVTX) + if(timer_one.start_flag) + { throw std::runtime_error("timer::end " + class_name + "::" + name); } + #ifdef __MPI + int is_initialized = 0; + MPI_Initialized(&is_initialized); + if(is_initialized) + { timer_one.cpu_second += MPI_Wtime() - timer_one.cpu_start; } + #else + timer_one.cpu_second += (cpu_time() - timer_one.cpu_start); + #endif + timer_one.start_flag = true; + #if defined(__CUDA) && defined(__USE_NVTX) if (PARAM.inp.timer_enable_nvtx) { nvtxRangePop(); } - #endif + #endif } long double timer::print_until_now() { - if(!timer_pool[""]["total"].start_flag) - timer::end("","total"); - // start again - timer::start("","total"); - return timer_pool[""]["total"].cpu_second; + if(!timer_pool[""]["total"].start_flag) + timer::end("","total"); + // start again + timer::start("","total"); + return timer_pool[""]["total"].cpu_second; } void timer::write_to_json(std::string file_name) { #ifdef __MPI // in some unit test, the mpi is not initialized, so we need to check it - // if mpi is not initialized, we do not run this function - int is_initialized = 0; + // if mpi is not initialized, we do not run this function + int is_initialized = 0; MPI_Initialized(&is_initialized); - if (!is_initialized) { - return; -} - int my_rank = 0; - MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); - if (my_rank != 0) { - return; -} + if (!is_initialized) { + return; + } + int my_rank = 0; + MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); + if (my_rank != 0) { + return; + } #endif // check if a double is inf, if so, return "null", else return a string of the input double - auto double_to_string = [](double d) -> std::string - { - if(std::isinf(d)) + auto double_to_string = [](double d) -> std::string + { + if(std::isinf(d)) + { + return "Infinity"; + } + else { - return "Infinity"; + return FmtCore::format("%.15f", d); } - else + }; + + // The output json file format is like this: + // { + // "total": 1.0, + // "sub": [ + // { + // "class_name": "wavefunc", + // "sub": [ + // { + // "name": "evc", + // "cpu_second": 0.000318, + // "calls": 2, + // "cpu_second_per_call": 0.000159, + // "cpu_second_per_total": 0.000318 + // } + // ] + // } + // ] + // } + + std::ofstream ofs(file_name); + std::string indent = " "; + int order_a = 0; + ofs << "{\n"; + ofs << indent << "\"total\": " << timer_pool[""]["total"].cpu_second << ",\n"; + ofs << indent << "\"sub\": [\n"; + for(auto &timer_pool_A : timer_pool) + { + order_a ++; + // if calss_name == "", it means total time, so we skip it + if(timer_pool_A.first == "") { - return FmtCore::format("%.15f", d); + continue; } - }; - - // The output json file format is like this: - // { - // "total": 1.0, - // "sub": [ - // { - // "class_name": "wavefunc", - // "sub": [ - // { - // "name": "evc", - // "cpu_second": 0.000318, - // "calls": 2, - // "cpu_second_per_call": 0.000159, - // "cpu_second_per_total": 0.000318 - // } - // ] - // } - // ] - // } - - std::ofstream ofs(file_name); - std::string indent = " "; - int order_a = 0; - ofs << "{\n"; - ofs << indent << "\"total\": " << timer_pool[""]["total"].cpu_second << ",\n"; - ofs << indent << "\"sub\": [\n"; - for(auto &timer_pool_A : timer_pool) - { - order_a ++; - // if calss_name == "", it means total time, so we skip it - if(timer_pool_A.first == "") - { - continue; - } - int order_b = 0; - const std::string class_name = timer_pool_A.first; - ofs << indent << indent << "{\n"; - ofs << indent << indent << indent << "\"class_name\": \"" << class_name << "\",\n"; - ofs << indent << indent << indent << "\"sub\": [\n"; - for(auto &timer_pool_B : timer_pool_A.second) - { - order_b ++; - const std::string name = timer_pool_B.first; - const Timer_One timer_one = timer_pool_B.second; - ofs << indent << indent << indent << indent << "{\n"; - ofs << indent << indent << indent << indent << "\"name\": \"" << name << "\",\n"; - ofs << indent << indent << indent << indent << "\"cpu_second\": " - << std::setprecision(15) << timer_one.cpu_second << ",\n"; - ofs << indent << indent << indent << indent << "\"calls\": " << timer_one.calls << ",\n"; - ofs << indent << indent << indent << indent << "\"cpu_second_per_call\": " - << double_to_string(timer_one.cpu_second/timer_one.calls) << ",\n"; - ofs << indent << indent << indent << indent << "\"cpu_second_per_total\": " - << double_to_string(timer_one.cpu_second/timer_pool[""]["total"].cpu_second) << "\n"; - - if (order_b == timer_pool_A.second.size()) - { - ofs << indent << indent << indent << indent << "}\n"; - } - else - { - ofs << indent << indent << indent << indent << "},\n"; - } - } - ofs << indent << indent << indent << "]\n"; - if (order_a == timer_pool.size()) - { - ofs << indent << indent << "}\n"; - } - else - { - ofs << indent << indent << "},\n"; - } - } - ofs << indent << "]\n"; - ofs << "}\n"; - ofs.close(); + int order_b = 0; + const std::string class_name = timer_pool_A.first; + ofs << indent << indent << "{\n"; + ofs << indent << indent << indent << "\"class_name\": \"" << class_name << "\",\n"; + ofs << indent << indent << indent << "\"sub\": [\n"; + for(auto &timer_pool_B : timer_pool_A.second) + { + order_b ++; + const std::string name = timer_pool_B.first; + const Timer_One timer_one = timer_pool_B.second; + ofs << indent << indent << indent << indent << "{\n"; + ofs << indent << indent << indent << indent << "\"name\": \"" << name << "\",\n"; + ofs << indent << indent << indent << indent << "\"cpu_second\": " + << std::setprecision(15) << timer_one.cpu_second << ",\n"; + ofs << indent << indent << indent << indent << "\"calls\": " << timer_one.calls << ",\n"; + ofs << indent << indent << indent << indent << "\"cpu_second_per_call\": " + << double_to_string(timer_one.cpu_second/timer_one.calls) << ",\n"; + ofs << indent << indent << indent << indent << "\"cpu_second_per_total\": " + << double_to_string(timer_one.cpu_second/timer_pool[""]["total"].cpu_second) << "\n"; + + if (order_b == timer_pool_A.second.size()) + { + ofs << indent << indent << indent << indent << "}\n"; + } + else + { + ofs << indent << indent << indent << indent << "},\n"; + } + } + ofs << indent << indent << indent << "]\n"; + if (order_a == timer_pool.size()) + { + ofs << indent << indent << "}\n"; + } + else + { + ofs << indent << indent << "},\n"; + } + } + ofs << indent << "]\n"; + ofs << "}\n"; + ofs.close(); } void timer::print_all(std::ofstream &ofs, const bool check_end) { - constexpr double small = 0.1; // cpu = 10^6 - // if want to print > 1s , set small = 10^6 - - std::vector,Timer_One>> timer_pool_order; - for(auto &timer_pool_A : timer_pool) - { - const std::string class_name = timer_pool_A.first; - for(auto &timer_pool_B : timer_pool_A.second) - { - const std::string name = timer_pool_B.first; - const Timer_One &timer_one = timer_pool_B.second; - if(check_end && !timer_one.start_flag) - { throw std::runtime_error("timer::print_all " + class_name + "::" + name); } - if(timer_pool_order.size() < timer_one.order+1) - { - timer_pool_order.resize(timer_one.order+1); - } - //timer_pool_order[timer_one.order] = {{class_name, name}, timer_one}; //qianrui change it to make it compatible with old compiler version - timer_pool_order[timer_one.order] = std::pair, Timer_One> { + constexpr double small = 0.1; // cpu = 10^6 + // if want to print > 1s , set small = 10^6 + + std::vector,Timer_One>> timer_pool_order; + for(auto &timer_pool_A : timer_pool) + { + const std::string class_name = timer_pool_A.first; + for(auto &timer_pool_B : timer_pool_A.second) + { + const std::string name = timer_pool_B.first; + const Timer_One &timer_one = timer_pool_B.second; + if(check_end && !timer_one.start_flag) + { throw std::runtime_error("timer::print_all " + class_name + "::" + name); } + if(timer_pool_order.size() < timer_one.order+1) + { + timer_pool_order.resize(timer_one.order+1); + } + //timer_pool_order[timer_one.order] = {{class_name, name}, timer_one}; //qianrui change it to make it compatible with old compiler version + timer_pool_order[timer_one.order] = std::pair, Timer_One> { std::pair{class_name,name}, timer_one}; - } - } - std::vector class_names; - std::vector names; - std::vector times; - std::vector calls; - std::vector avgs; - std::vector pers; - for(auto &timer_pool_order_A : timer_pool_order) - { - const std::string &class_name = timer_pool_order_A.first.first; - const std::string &name = timer_pool_order_A.first.second; - const Timer_One &timer_one = timer_pool_order_A.second; - - if(timer_one.cpu_second < 0) - { - continue; - } - - // only print out timers that are larger than 1% - // mohan add 2025-03-09 - const double percentage_thr = 1.0; + } + } + std::vector class_names; + std::vector names; + std::vector times; + std::vector calls; + std::vector avgs; + std::vector pers; + for(auto &timer_pool_order_A : timer_pool_order) + { + const std::string &class_name = timer_pool_order_A.first.first; + const std::string &name = timer_pool_order_A.first.second; + const Timer_One &timer_one = timer_pool_order_A.second; + + if(timer_one.cpu_second < 0) + { + continue; + } + + // only print out timers that are larger than 1% + // mohan add 2025-03-09 + const double percentage_thr = 1.0; const double percentage = timer_one.cpu_second / timer_pool_order[0].second.cpu_second * 100; - if(percentage titles = {"CLASS_NAME", "NAME", "TIME/s", "CALLS", "AVG/s", "PER/%"}; - std::vector formats = {"%-10s", "%-10s", "%6.2f", "%8d", "%6.2f", "%6.2f"}; - FmtTable time_statistics(/*titles=*/titles, - /*nrows=*/pers.size(), - /*formats=*/formats, - /*indent=*/0, - /*align=*/{/*value*/FmtTable::Align::LEFT, /*title*/FmtTable::Align::CENTER}); - time_statistics << class_names << names << times << calls << avgs << pers; - const std::string table = "\nTIME STATISTICS\n" + time_statistics.str(); - std::cout< titles = {"CLASS_NAME", "NAME", "TIME/s", "CALLS", "AVG/s", "PER/%"}; + std::vector formats = {"%-10s", "%-10s", "%6.2f", "%8d", "%6.2f", "%6.2f"}; + FmtTable time_statistics(/*titles=*/titles, + /*nrows=*/pers.size(), + /*formats=*/formats, + /*indent=*/0, + /*align=*/{/*value*/FmtTable::Align::LEFT, /*title*/FmtTable::Align::CENTER}); + time_statistics << class_names << names << times << calls << avgs << pers; + const std::string table = "\n TIME STATISTICS\n" + time_statistics.str(); + std::cout<0); - std::cout << " START CHARGE : " << PARAM.inp.init_chg << std::endl; + std::cout << " START CHARGE : " << PARAM.inp.init_chg << std::endl; // we need to set the omega for the charge density set_omega(&ucell.omega); diff --git a/source/source_io/module_output/print_info.cpp b/source/source_io/module_output/print_info.cpp index 7db14b46380..b76e7631fa9 100644 --- a/source/source_io/module_output/print_info.cpp +++ b/source/source_io/module_output/print_info.cpp @@ -8,42 +8,42 @@ namespace ModuleIO { void print_parameters( - const UnitCell& ucell, - K_Vectors& kv, + const UnitCell& ucell, + K_Vectors& kv, const Input_para& inp) { ModuleBase::TITLE("ModuleIO", "print_parameters"); - if(inp.calculation=="scf" - || inp.calculation=="relax" - || inp.calculation=="cell-relax" - || inp.calculation=="nscf" - || inp.calculation=="get_pchg" - || inp.calculation=="get_wf" - || inp.calculation=="md") - { - std::cout << " ---------------------------------------------------------" << std::endl; - if(inp.calculation=="scf") - { - std::cout << " Self-consistent calculations for electrons" << std::endl; - } - else if(inp.calculation=="test") - { - std::cout << " Test run" << std::endl; - } - if(inp.calculation=="relax") - { + if(inp.calculation=="scf" + || inp.calculation=="relax" + || inp.calculation=="cell-relax" + || inp.calculation=="nscf" + || inp.calculation=="get_pchg" + || inp.calculation=="get_wf" + || inp.calculation=="md") + { + std::cout << " ----------------------------------------------------------------" << std::endl; + if(inp.calculation=="scf") + { + std::cout << " Self-consistent calculations for electrons" << std::endl; + } + else if(inp.calculation=="test") + { + std::cout << " Test run" << std::endl; + } + if(inp.calculation=="relax") + { std::cout << " Ion relaxation calculations" << std::endl; - } + } if(inp.calculation=="cell-relax") { std::cout << " Cell relaxation calculations" << std::endl; } - if(inp.calculation=="md") - { - std::cout << " Molecular Dynamics simulations" << std::endl; + if(inp.calculation=="md") + { + std::cout << " Molecular Dynamics simulations" << std::endl; - std::cout << " ---------------------------------------------------------" << std::endl; + std::cout << " ---------------------------------------------------------" << std::endl; if (PARAM.mdp.md_type == "fire") { @@ -74,127 +74,130 @@ void print_parameters( std::cout << " Time interval(fs) : " << PARAM.mdp.md_dt << std::endl; } - std::cout << " ---------------------------------------------------------" << std::endl; + std::cout << " ----------------------------------------------------------------" << std::endl; - std::cout << " " << std::setw(8) << "SPIN" - << std::setw(16) << "KPOINTS" - << std::setw(12) << "PROCESSORS" - << std::setw(12) << "THREADS"; + std::cout << " " << std::setw(8) << "SPIN" + << std::setw(16) << "KPOINTS" + << std::setw(12) << "PROCESSES" + << std::setw(14) << "THREADS/PROC" + << std::setw(14) << "THREADS/TOTAL"; - const bool orbinfo = (inp.basis_type=="lcao" || inp.basis_type=="lcao_in_pw" - || (inp.basis_type=="pw" && inp.init_wfc.substr(0, 3) == "nao")); - if (orbinfo) { std::cout << std::setw(12) << "NBASE"; } + const bool orbinfo = (inp.basis_type=="lcao" || inp.basis_type=="lcao_in_pw" + || (inp.basis_type=="pw" && inp.init_wfc.substr(0, 3) == "nao")); + if (orbinfo) { std::cout << std::setw(12) << "NBASE"; } - std::cout << std::endl; - std::cout << " " << std::setw(8) << inp.nspin; + std::cout << std::endl; + std::cout << " " << std::setw(8) << inp.nspin; - if(PARAM.globalv.gamma_only_local) - { - std::cout << std::setw(16) << "Gamma"; - } - else - { - std::cout << std::setw(16) << kv.get_nkstot(); - } + if(PARAM.globalv.gamma_only_local) + { + std::cout << std::setw(16) << "Gamma"; + } + else + { + std::cout << std::setw(16) << kv.get_nkstot(); + } - std::cout << std::setw(12) << GlobalV::NPROC - << std::setw(12) << PARAM.globalv.nthread_per_proc * GlobalV::NPROC; - if (orbinfo) { std::cout << std::setw(12) << PARAM.globalv.nlocal; } + std::cout << std::setw(12) << GlobalV::NPROC + << std::setw(14) << PARAM.globalv.nthread_per_proc + << std::setw(14) << PARAM.globalv.nthread_per_proc*GlobalV::NPROC; - std::cout << std::endl; + if (orbinfo) { std::cout << std::setw(12) << PARAM.globalv.nlocal; } + std::cout << std::endl; - std::cout << " ---------------------------------------------------------" << std::endl; - if(inp.basis_type == "lcao") - { - std::cout << " Use Systematically Improvable Atomic bases" << std::endl; - } - else if(inp.basis_type == "lcao_in_pw") - { - std::cout << " Expand Atomic bases into plane waves" << std::endl; - } - else if(inp.basis_type == "pw") - { - std::cout << " Use plane wave basis" << std::endl; - } - std::cout << " ---------------------------------------------------------" << std::endl; + std::cout << " ----------------------------------------------------------------" << std::endl; + if(inp.basis_type == "lcao") + { + std::cout << " Use Systematically Improvable Atomic bases" << std::endl; + } + else if(inp.basis_type == "lcao_in_pw") + { + std::cout << " Expand Atomic bases into plane waves" << std::endl; + } + else if(inp.basis_type == "pw") + { + std::cout << " Use plane wave basis" << std::endl; + } + std::cout << " ----------------------------------------------------------------" << std::endl; - //---------------------------------- - // second part - //---------------------------------- - std::cout << " " << std::setw(8) << "ELEMENT"; + //---------------------------------- + // second part + //---------------------------------- - if (orbinfo) - { - std::cout << std::setw(16) << "ORBITALS"; - std::cout << std::setw(12) << "NBASE"; - } - std::cout << std::setw(12) << "NATOM"; + std::cout << " " << std::setw(8) << "ELEMENT"; - std::cout << std::setw(12) << "XC"; - std::cout << std::endl; + if (orbinfo) + { + std::cout << std::setw(16) << "ORBITALS"; + std::cout << std::setw(12) << "NBASE"; + } + std::cout << std::setw(12) << "NATOM"; + std::cout << std::setw(12) << "XC"; + std::cout << std::endl; - const std::string spectrum = "spdfghi"; - for(int it=0; it