From 3f78965b70ceec127902f3b49d0769ae1a1b5752 Mon Sep 17 00:00:00 2001 From: perillaroc Date: Wed, 25 Jul 2018 23:11:57 +0800 Subject: [PATCH] add simple OpenMP in GradsConverter. #9 --- CMakeLists.txt | 6 ++++ src/grads_convert/CMakeLists.txt | 7 +++++ src/grads_convert/src/grads_converter.cpp | 35 ++++++++++++++++------- src/grads_convert/src/grads_converter.h | 2 +- src/porter/CMakeLists.txt | 7 +++++ src/porter/src/main.cpp | 22 ++++++++++++++ 6 files changed, 67 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fd168c6..bd5f5f7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,5 +36,11 @@ if(ENABLE_TESTS) find_package(GTest REQUIRED) endif() +option(ENABLE_PORTER_OPENMP "Using OpenMP in Porter" OFF) + +if(ENABLE_PORTER_OPENMP) + find_package(OpenMP REQUIRED) +endif() + add_subdirectory(src) add_subdirectory(example) \ No newline at end of file diff --git a/src/grads_convert/CMakeLists.txt b/src/grads_convert/CMakeLists.txt index 17cb9ff..68ccb38 100644 --- a/src/grads_convert/CMakeLists.txt +++ b/src/grads_convert/CMakeLists.txt @@ -37,6 +37,13 @@ target_compile_definitions(grads_convert PRIVATE MUPARSER_STATIC ) +if(ENABLE_PORTER_OPENMP) + message("GradsConvert in openmp") + target_link_libraries(grads_convert + PUBLIC + OpenMP::OpenMP_CXX) +endif() + add_library(Porter::grads_convert ALIAS grads_convert) # testing diff --git a/src/grads_convert/src/grads_converter.cpp b/src/grads_convert/src/grads_converter.cpp index d681fad..8adc063 100644 --- a/src/grads_convert/src/grads_converter.cpp +++ b/src/grads_convert/src/grads_converter.cpp @@ -99,16 +99,24 @@ GradsCtl GradsConverter::getGradsCtl() { void GradsConverter::convertMessages(GradsCtl &grads_ctl, vector &converted_messages) { - GradsDataHandler data_handler{grads_ctl}; - data_handler.openDataFile(); +// GradsDataHandler data_handler{grads_ctl}; +// data_handler.openDataFile(); int message_count = 0; - for(auto &m: converted_messages) { +# pragma omp parallel for num_threads(4) \ + shared(converted_messages, grads_ctl, message_count) + for(auto it = converted_messages.begin(); it < converted_messages.end(); it++) { + +// message_count++; + auto m = *it; + + GradsDataHandler data_handler{grads_ctl}; + data_handler.openDataFile(); + auto message_handler = data_handler.loadByIndex(m.index_); cout<<"Converting..."< message_handler, ParamConfig ¶m_config, - int message_count) + int &message_count) { auto level = message_handler->variable().level_; @@ -246,13 +254,18 @@ void GradsConverter::convertMessage( double *value_array = &double_values[0]; codes_set_double_array(handle, "values", value_array, values.size()); - const char* output_file_mode = "wb"; - if(message_count > 0) +# pragma omp critical { - output_file_mode = "ab"; - } - - codes_write_message(handle, output_file_path_.c_str(), output_file_mode); + message_count++; + const char* output_file_mode = "wb"; + if(message_count > 1) + { + output_file_mode = "ab"; + } + std::cout<<"Writing message to file..."< &converted_messages); void convertMessage(std::shared_ptr message_handler, - ParamConfig ¶m_config, int message_count); + ParamConfig ¶m_config, int &message_count); std::string convert_config_file_path_; std::string ctl_file_path_; diff --git a/src/porter/CMakeLists.txt b/src/porter/CMakeLists.txt index ca7b455..9846e64 100644 --- a/src/porter/CMakeLists.txt +++ b/src/porter/CMakeLists.txt @@ -23,4 +23,11 @@ target_link_libraries(porter #$<$:${YAMLCPP_LIBRARY_RELEASE}> ) +if(ENABLE_PORTER_OPENMP) + message("Porter in openmp: ${OpenMP_CXX_FLAGS}") + target_link_libraries(porter + PUBLIC + OpenMP::OpenMP_CXX) +endif() + install(TARGETS porter RUNTIME DESTINATION bin) \ No newline at end of file diff --git a/src/porter/src/main.cpp b/src/porter/src/main.cpp index 6cf4e51..e259653 100644 --- a/src/porter/src/main.cpp +++ b/src/porter/src/main.cpp @@ -37,7 +37,29 @@ int main(int argc, char *argv[]) converter.setCtlFilePath(ctl_file_path); converter.setOutputFilePath(output_file_path); + { + auto t = std::time(nullptr); + auto tm = *std::localtime(&t); + + std::ostringstream oss; + oss << std::put_time(&tm, "%Y-%m-%d %H-%M-%S"); + auto str = oss.str(); + + std::cout << str << std::endl; + } + converter.convert(); + + { + auto t = std::time(nullptr); + auto tm = *std::localtime(&t); + + std::ostringstream oss; + oss << std::put_time(&tm, "%Y-%m-%d %H-%M-%S"); + auto str = oss.str(); + + std::cout << str << std::endl; + } } return 0;