diff --git a/documents/references.md b/documents/references.md index 47ce71c7f87fd2927f361fb94b4f68b441436c9e..9af7096c45b1381096adaacd5aa6fc0c9db3ee6e 100644 --- a/documents/references.md +++ b/documents/references.md @@ -48,7 +48,13 @@ Various references used in project. * <https://stackoverflow.com/a/5420568> #### Memory Management -* <https://www.geeksforgeeks.org/delete-in-c/> +<https://www.geeksforgeeks.org/delete-in-c/> + +#### Tracking Time Elapsed +* Tracking Time - <https://stackoverflow.com/a/1735321> +* Mod on Doubles - <https://stackoverflow.com/a/9138794> +* Setting Double Precision - <https://stackoverflow.com/a/16280182> +* Saving Precision without Immediate Output to Console - <https://stackoverflow.com/a/48945767> ## CUDA References diff --git a/part_1/logging.cu b/part_1/logging.cu index e281960f4b1507035b7c9b69767432c8d5eeef79..fc66c735c97e95044f1386727792786192e329cb 100644 --- a/part_1/logging.cu +++ b/part_1/logging.cu @@ -31,6 +31,7 @@ Logging::Logging() { warn_stream.open(core_logging_folder + "warn.log", std::ios::app | std::ios::out); info_stream.open(core_logging_folder + "info.log", std::ios::app | std::ios::out); debug_stream.open(core_logging_folder + "debug.log", std::ios::app | std::ios::out); + timer_stream.open(core_logging_folder + "timer.log", std::ios::app | std::ios::out); } /** @@ -58,6 +59,9 @@ void Logging::close_log_files() { if (error_stream.is_open()) { error_stream.close(); } + if (timer_stream.is_open()) { + timer_stream.close(); + } } @@ -86,6 +90,7 @@ void Logging::info(std::string msg, std::string log_type, const char* file, long debug(msg, log_type, file, line_num); } + /** * Writes message to warning log file. */ @@ -98,6 +103,7 @@ void Logging::warn(std::string msg, std::string log_type, const char* file, long info(msg, log_type, file, line_num); } + /** * Writes message to error log file. */ @@ -111,6 +117,19 @@ void Logging::error(std::string msg, std::string log_type, const char* file, lon } +/** + * Writes message to timer log file. + */ +void Logging::timer(std::string msg, std::string log_type, const char* file, long line_num) { + // Build and write logging message. + std::string log_prefix = " [" + log_type + "] [ " + std::string(file) + " | " + std::to_string(line_num) + " ] "; + timer_stream << log_prefix << msg << std::endl; + + // Also write to info and lower level loggers. + info(msg, log_type, file, line_num); +} + + /** * Prints out logging location to console. */ @@ -131,11 +150,13 @@ void Logging::clear_log_files() { warn_stream.open(core_logging_folder + "warn.log", std::ios::out); info_stream.open(core_logging_folder + "info.log", std::ios::out); debug_stream.open(core_logging_folder + "debug.log", std::ios::out); + timer_stream.open(core_logging_folder + "timer.log", std::ios::out); // Close log files and reopen in "append mode". Probably not necessary but here to be safe. close_log_files(); error_stream.open(core_logging_folder + "error.log", std::ios::app | std::ios::out); warn_stream.open(core_logging_folder + "warn.log", std::ios::app | std::ios::out); info_stream.open(core_logging_folder + "info.log", std::ios::app | std::ios::out); - debug_stream.open(core_logging_folder + "debug.log", std::ios::out); + debug_stream.open(core_logging_folder + "debug.log", std::ios::app | std::ios::out); + timer_stream.open(core_logging_folder + "timer.log", std::ios::app | std::ios::out); } diff --git a/part_1/logging.h b/part_1/logging.h index 2d7d38e524853c5a70cc70755ea97f1afa869525..afa4aed8b8aff86fff23ec2e4f55aad15bd69554 100644 --- a/part_1/logging.h +++ b/part_1/logging.h @@ -27,6 +27,7 @@ class Logging { std::ofstream info_stream; std::ofstream warn_stream; std::ofstream error_stream; + std::ofstream timer_stream; public: /** @@ -65,6 +66,12 @@ class Logging { */ void error(std::string msg, std::string log_type = "ERROR", const char* file = __builtin_FILE(), long line_num = __builtin_LINE()); + /** + * Writes message to timer log file. + */ + void timer(std::string msg, std::string log_type = "TIMER", const char* file = __builtin_FILE(), long line_num = __builtin_LINE()); + + /** * Prints out logging location to console. */ diff --git a/part_1/main.cu b/part_1/main.cu index d1094ee0d6ec56b2a193d07833690bbbbb00df11..3fda4e5337e3c279c13fe1ab237492049c92ea49 100644 --- a/part_1/main.cu +++ b/part_1/main.cu @@ -7,6 +7,7 @@ #include <cctype> #include <cstdio> #include <cstdlib> +#include <ctime> #include <filesystem> #include <fstream> #include <iostream> @@ -98,11 +99,13 @@ void process_dir(std::string path_str) { logger.debug(" path_str: " + path_str); logger.debug(""); - logger.info(""); - logger.info(""); - logger.info(""); - logger.info(""); - logger.info("Handling dir."); + logger.timer(""); + logger.timer(""); + logger.timer(""); + logger.timer("Handling dir \"" + path_str + "\""); + + // Track time to process. + time_t start_time = time(0); // Process all files in directory. for (const auto & entry : std::filesystem::directory_iterator(path_str)) { @@ -127,6 +130,18 @@ void process_dir(std::string path_str) { process_file(file_path_str); } } + + // Calculate time processing dir. + double total_time = difftime( time(0), start_time); + logger.timer(""); + logger.timer("Time to Process Dir \"" + path_str + "\": "); + double seconds_elapsed = fmod(total_time, 60); + double minutes_elapsed = (total_time - seconds_elapsed) / 60; + std::ostringstream second_format_string; + std::ostringstream minute_format_string; + second_format_string << std::setprecision(0) << std::fixed << seconds_elapsed; + minute_format_string << std::setprecision(0) << std::fixed << minutes_elapsed; + logger.timer(minute_format_string.str() + " min, " + second_format_string.str() + " seconds"); } @@ -140,15 +155,27 @@ void process_file(std::string path_str) { logger.info(""); logger.info(""); - logger.info(""); - logger.info(""); - logger.info("Handling file."); + logger.timer(""); + logger.timer("Handling file \"" + path_str + "\""); + + // Track time to process. + time_t start_time = time(0); Image image(path_str); image.display_properties(); image.save(); -} + // Calculate time processing file. + double total_time = difftime( time(0), start_time); + logger.timer("Time to Process File \"" + path_str + "\": "); + double seconds_elapsed = fmod(total_time, 60); + double minutes_elapsed = (total_time - seconds_elapsed) / 60; + std::ostringstream second_format_string; + std::ostringstream minute_format_string; + second_format_string << std::setprecision(0) << std::fixed << seconds_elapsed; + minute_format_string << std::setprecision(0) << std::fixed << minutes_elapsed; + logger.timer(minute_format_string.str() + " min, " + second_format_string.str() + " seconds"); +} // /*