Skip to content

Commit

Permalink
[cilkscale] Adjust Cilkscale tool initialization to support different…
Browse files Browse the repository at this point in the history
… ways in which Cilkscale and dependent libraries can be linked, i.e., statically vs. dynamically.
  • Loading branch information
neboat committed Feb 17, 2021
1 parent b82d1ef commit 26db500
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 25 deletions.
60 changes: 49 additions & 11 deletions cilkscale/benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,35 @@ class BenchmarkImpl_t {
};

// Top-level benchmarking tool.
static BenchmarkImpl_t tool;
static BenchmarkImpl_t *create_tool(void) {
if (!__cilkrts_is_initialized())
// If the OpenCilk runtime is not yet initialized, then csi_init will
// register a call to init_tool to initialize the tool after the runtime is
// initialized.
return nullptr;

// Otherwise, ordered dynamic initalization should ensure that it's safe to
// create the tool.
return new BenchmarkImpl_t();
}
static BenchmarkImpl_t *tool = create_tool();

// Macro to access the correct timer, based on the initialized state of the
// tool.
#if SERIAL_TOOL
#define TIMER (tool.timer)
#define TIMER (tool->timer)
#else
#define TIMER (tool.timer.get_view())
#define TIMER (tool->timer.get_view())
#endif

// Macro to access the correct output stream, based on the initialized state of
// the tool.
#if SERIAL_TOOL
#define OUTPUT ((tool.outf.is_open()) ? (tool.outf) : (tool.outs))
#define OUTPUT ((tool->outf.is_open()) ? (tool->outf) : (tool->outs))
#else
#define OUTPUT \
((tool.outf_red) ? (**(tool.outf_red)) \
: ((tool.outf.is_open()) ? (tool.outf) : (tool.outs)))
((tool->outf_red) ? (**(tool->outf_red)) \
: ((tool->outf.is_open()) ? (tool->outf) : (tool->outs)))
#endif

static bool TOOL_INITIALIZED = false;
Expand Down Expand Up @@ -142,7 +153,7 @@ static void print_analysis(void) {
assert(TOOL_INITIALIZED);

ensure_header(OUTPUT);
print_results(OUTPUT, "", elapsed_time(&tool.stop, &tool.start));
print_results(OUTPUT, "", elapsed_time(&tool->stop, &tool->start));
}

///////////////////////////////////////////////////////////////////////////
Expand All @@ -153,7 +164,11 @@ BenchmarkImpl_t::BenchmarkImpl_t() {
if (envstr)
outf.open(envstr);

TOOL_INITIALIZED = true;
#if !SERIAL_TOOL
outf_red =
new cilk::reducer<cilk::op_ostream>((outf.is_open() ? outf : outs));
#endif

start.gettime();
}

Expand All @@ -168,17 +183,37 @@ BenchmarkImpl_t::~BenchmarkImpl_t() {
delete outf_red;
outf_red = nullptr;
#endif

TOOL_INITIALIZED = false;
}

///////////////////////////////////////////////////////////////////////////
// Hooks for operating the tool.

// Custom function to intialize tool after the OpenCilk runtime is initialized.
static void init_tool(void) {
assert(nullptr == tool && "Tool already initialized");
tool = new BenchmarkImpl_t();
}

static void destroy_tool(void) {
if (tool) {
delete tool;
tool = nullptr;
}

TOOL_INITIALIZED = false;
}

CILKTOOL_API void __csi_init() {
#if TRACE_CALLS
fprintf(stderr, "__csi_init()\n");
#endif

if (!__cilkrts_is_initialized())
__cilkrts_atinit(init_tool);

__cilkrts_atexit(destroy_tool);

TOOL_INITIALIZED = true;
}

CILKTOOL_API void __csi_unit_init(const char *const file_name,
Expand All @@ -190,8 +225,11 @@ CILKTOOL_API void __csi_unit_init(const char *const file_name,
// Probes and associated routines

CILKSCALE_EXTERN_C wsp_t wsp_getworkspan() CILKSCALE_NOTHROW {
if (!tool)
return wsp_zero();

TIMER.gettime();
duration_t time_since_start = elapsed_time(&TIMER, &tool.start);
duration_t time_since_start = elapsed_time(&TIMER, &tool->start);
wsp_t result = { cilk_time_t(time_since_start).get_raw_duration(), 0, 0 };

return result;
Expand Down
77 changes: 63 additions & 14 deletions cilkscale/cilkscale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#endif

// defined in libopencilk
extern "C" int __cilkrts_is_initialized(void);
extern "C" void __cilkrts_internal_set_nworkers(unsigned int nworkers);

///////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -66,24 +67,35 @@ class CilkscaleImpl_t {
};

// Top-level Cilkscale tool.
static CilkscaleImpl_t tool;
static CilkscaleImpl_t *create_tool(void) {
if (!__cilkrts_is_initialized())
// If the OpenCilk runtime is not yet initialized, then csi_init will
// register a call to init_tool to initialize the tool after the runtime is
// initialized.
return nullptr;

// Otherwise, ordered dynamic initalization should ensure that it's safe to
// create the tool.
return new CilkscaleImpl_t();
}
static CilkscaleImpl_t *tool = create_tool();

// Macro to access the correct shadow-stack data structure, based on the
// initialized state of the tool.
#if SERIAL_TOOL
#define STACK (*tool.shadow_stack)
#define STACK (*tool->shadow_stack)
#else
#define STACK (tool.shadow_stack->get_view())
#define STACK (tool->shadow_stack->get_view())
#endif

// Macro to use the correct output stream, based on the initialized state of the
// tool.
#if SERIAL_TOOL
#define OUTPUT ((tool.outf.is_open()) ? (tool.outf) : (tool.outs))
#define OUTPUT ((tool->outf.is_open()) ? (tool->outf) : (tool->outs))
#else
#define OUTPUT \
((tool.outf_red) ? (**(tool.outf_red)) \
: ((tool.outf.is_open()) ? (tool.outf) : (tool.outs)))
((tool->outf_red) ? (**(tool->outf_red)) \
: ((tool->outf.is_open()) ? (tool->outf) : (tool->outs)))
#endif

static bool TOOL_INITIALIZED = false;
Expand Down Expand Up @@ -139,7 +151,18 @@ static void print_analysis(void) {
// Ensure that this tool is run serially
static inline void ensure_serial_tool(void) {
fprintf(stderr, "Forcing CILK_NWORKERS=1.\n");
__cilkrts_internal_set_nworkers(1);
if (__cilkrts_is_initialized()) {
__cilkrts_internal_set_nworkers(1);
} else {
// Force the number of Cilk workers to be 1.
char *e = getenv("CILK_NWORKERS");
if (!e || 0 != strcmp(e, "1")) {
if (setenv("CILK_NWORKERS", "1", 1)) {
fprintf(err_io, "Error setting CILK_NWORKERS to be 1\n");
exit(1);
}
}
}
}
#endif

Expand All @@ -155,13 +178,17 @@ CilkscaleImpl_t::CilkscaleImpl_t() {
outf.open(envstr);

#if !SERIAL_TOOL
outf_red = new cilk::reducer<cilk::op_ostream>(OUTPUT);
outf_red =
new cilk::reducer<cilk::op_ostream>((outf.is_open() ? outf : outs));
#endif

TOOL_INITIALIZED = true;

STACK.push(frame_type::SPAWNER);
STACK.start.gettime();
#if SERIAL_TOOL
shadow_stack_t &stack = *shadow_stack;
#else
shadow_stack_t &stack = shadow_stack->get_view();
#endif
stack.push(frame_type::SPAWNER);
stack.start.gettime();
}

CilkscaleImpl_t::~CilkscaleImpl_t() {
Expand All @@ -179,23 +206,45 @@ CilkscaleImpl_t::~CilkscaleImpl_t() {
outf.close();
delete shadow_stack;

#if !SERIAL_TOOL
delete outf_red;
outf_red = nullptr;

TOOL_INITIALIZED = false;
#endif
}

///////////////////////////////////////////////////////////////////////////
// Hooks for operating the tool.

// Custom function to intialize tool after the OpenCilk runtime is initialized.
static void init_tool(void) {
assert(nullptr == tool && "Tool already initialized");
tool = new CilkscaleImpl_t();
}

static void destroy_tool(void) {
if (tool) {
delete tool;
tool = nullptr;
}

TOOL_INITIALIZED = false;
}

CILKTOOL_API void __csi_init() {
#if TRACE_CALLS
fprintf(stderr, "__csi_init()\n");
#endif

if (!__cilkrts_is_initialized())
__cilkrts_atinit(init_tool);

__cilkrts_atexit(destroy_tool);

#if SERIAL_TOOL
ensure_serial_tool();
#endif

TOOL_INITIALIZED = true;
}

CILKTOOL_API void __csi_unit_init(const char *const file_name,
Expand Down

0 comments on commit 26db500

Please sign in to comment.