Skip to content
This repository has been archived by the owner on Jan 3, 2023. It is now read-only.

Commit

Permalink
Add windows support for event trace (#2695)
Browse files Browse the repository at this point in the history
* add windows support for event trace

* add needed windows.h header

* fix linking error

* Enable multi-threaded builds for windows
  • Loading branch information
rkimballn1 authored and diyessi committed Apr 5, 2019
1 parent 56d3a46 commit 8c08109
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Expand Up @@ -145,7 +145,7 @@ option(NGRAPH_DISTRIBUTED_ENABLE "Enable distributed training using MLSL/OpenMPI

if (NGRAPH_CPU_ENABLE
AND
((NOT NGRAPH_GPU_ENABLE) AND (NOT NGRAPH_GPUH_ENABLE)
((NOT NGRAPH_GPU_ENABLE) AND (NOT NGRAPH_GPUH_ENABLE)
AND (NOT NGRAPH_GENERIC_CPU_ENABLE) AND (NOT NGRAPH_INTELGPU_ENABLE))
)
set(NGRAPH_INTEL_CPU_ONLY_ENABLE ON)
Expand Down Expand Up @@ -267,7 +267,7 @@ endif()

if (WIN32)
set (CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "/W0 /EHsc")
set(CMAKE_CXX_FLAGS "/W0 /EHsc /MP")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-g")
Expand Down
49 changes: 32 additions & 17 deletions src/ngraph/event_tracing.cpp
Expand Up @@ -23,31 +23,36 @@

using namespace std;

static bool read_tracing_env_var()
{
return (std::getenv("NGRAPH_ENABLE_TRACING") != nullptr);
}

mutex ngraph::Event::s_file_mutex;
ofstream ngraph::Event::s_event_log;
bool ngraph::Event::s_tracing_enabled = false;
bool ngraph::Event::s_tracing_enabled = read_tracing_env_var();

void ngraph::Event::write_trace(const ngraph::Event& event)
{
lock_guard<mutex> lock(s_file_mutex);
if (!is_tracing_enabled())
if (is_tracing_enabled())
{
return;
}
lock_guard<mutex> lock(s_file_mutex);

static bool so_initialized = false;
if (!so_initialized)
{
// Open the file
s_event_log.open("ngraph_event_trace.json", ios_base::trunc);
s_event_log << "[\n";
s_event_log << event.to_json() << "\n";
so_initialized = true;
return;
}
static bool so_initialized = false;
if (!so_initialized)
{
// Open the file
s_event_log.open("ngraph_event_trace.json", ios_base::trunc);
s_event_log << "[\n";
so_initialized = true;
}
else
{
s_event_log << ",\n";
}

s_event_log << ",\n";
s_event_log << event.to_json() << "\n" << flush;
s_event_log << event.to_json() << "\n" << flush;
}
}

string ngraph::Event::to_json() const
Expand All @@ -73,3 +78,13 @@ string ngraph::Event::to_json() const
output << json_start << ",\n" << json_end;
return output.str();
}

void ngraph::Event::enable_event_tracing()
{
s_tracing_enabled = true;
}

void ngraph::Event::disable_event_tracing()
{
s_tracing_enabled = false;
}
25 changes: 10 additions & 15 deletions src/ngraph/event_tracing.hpp
Expand Up @@ -22,7 +22,14 @@
#include <mutex>
#include <string>
#include <thread>
#ifdef _WIN32
#include <windows.h>
// windows.h must be before processthreadsapi.h so we need this comment
#include <processthreadsapi.h>
#define getpid() GetCurrentProcessId()
#else
#include <unistd.h>
#endif

namespace ngraph
{
Expand Down Expand Up @@ -84,21 +91,9 @@ namespace ngraph
}

static void write_trace(const Event& event);
static bool is_tracing_enabled()
{
static bool s_check_env = true;
if (s_check_env)
{
s_check_env = false;
if (std::getenv("NGRAPH_ENABLE_TRACING") != nullptr)
{
s_tracing_enabled = true;
}
}
return s_tracing_enabled;
}
static void enable_event_tracing() { s_tracing_enabled = true; }
static void disable_event_tracing() { s_tracing_enabled = false; }
static bool is_tracing_enabled() { return s_tracing_enabled; }
static void enable_event_tracing();
static void disable_event_tracing();
std::string to_json() const;

Event(const Event&) = delete;
Expand Down

0 comments on commit 8c08109

Please sign in to comment.