Skip to content

Commit

Permalink
add simple OpenMP in GradsConverter. #9
Browse files Browse the repository at this point in the history
  • Loading branch information
perillaroc committed Jul 25, 2018
1 parent cc8439d commit 3f78965
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
7 changes: 7 additions & 0 deletions src/grads_convert/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 24 additions & 11 deletions src/grads_convert/src/grads_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,24 +99,32 @@ GradsCtl GradsConverter::getGradsCtl() {

void GradsConverter::convertMessages(GradsCtl &grads_ctl,
vector<ConvertedMessage> &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..."<<endl;
convertMessage(message_handler, m.param_config_, message_count);
cout<<"Converting...Done"<<endl;
message_count++;
}
}


void GradsConverter::convertMessage(
shared_ptr<GradsMessagedHandler> message_handler,
ParamConfig &param_config,
int message_count)
int &message_count)
{
auto level = message_handler->variable().level_;

Expand Down Expand Up @@ -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..."<<std::endl;
codes_write_message(handle, output_file_path_.c_str(), output_file_mode);
std::cout<<"Writing message to file...Done"<<std::endl;
};

codes_handle_delete(handle);
}
2 changes: 1 addition & 1 deletion src/grads_convert/src/grads_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class GradsConverter
void convertMessages(GradsParser::GradsCtl &grads_ctl, std::vector<ConvertedMessage> &converted_messages);

void convertMessage(std::shared_ptr<GradsParser::GradsMessagedHandler> message_handler,
ParamConfig &param_config, int message_count);
ParamConfig &param_config, int &message_count);

std::string convert_config_file_path_;
std::string ctl_file_path_;
Expand Down
7 changes: 7 additions & 0 deletions src/porter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,11 @@ target_link_libraries(porter
#$<$<CONFIG:Release>:${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)
22 changes: 22 additions & 0 deletions src/porter/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3f78965

Please sign in to comment.