Skip to content

Commit 160edff

Browse files
authored
Initialization fixes (ROCm#154)
- Remove tooling initialization from rocprofiler_configure: when rocprofiler configure is called from __hip_module_ctor (which in turn is called as a global constructor when loading shared libraries or before main in a hip program), initializing tooling in it can cause problems because it is too early to do some of the tasks that it involves (e.g. opening shared libraries, creating threads). Instead, we rely on rocprofsys_main to initialize tooling later. - Skip rocprofiler_configure if ROCPROFSYS_PRELOAD is not set since preload is required for tooling (such as perfetto, which is used by the rocprofiler callbacks) to be initialized. - Revert RCCL initialization changes: These are no longer needed since rocprofsys_init_tooling_hidden will not be called from rocprofiler_configure - Force rocprofiler_configure in rocprofsys_init_tooling_hidden if it hasn't been called through __hip_module_ctor global constructor [ROCm/rocprofiler-systems commit: 0e535da]
1 parent 17ed9dd commit 160edff

File tree

4 files changed

+30
-41
lines changed

4 files changed

+30
-41
lines changed

projects/rocprofiler-systems/source/lib/rocprof-sys/api.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ rocprofsys_init_library(void)
122122
extern "C" void
123123
rocprofsys_init_tooling(void)
124124
{
125-
rocprofsys_init_tooling_hidden(true);
125+
rocprofsys_init_tooling_hidden();
126126
}
127127

128128
extern "C" void

projects/rocprofiler-systems/source/lib/rocprof-sys/api.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ extern "C"
9595

9696
// these are the real implementations for internal calling convention
9797
void rocprofsys_init_library_hidden(void) ROCPROFSYS_HIDDEN_API;
98-
bool rocprofsys_init_tooling_hidden(bool postinit = false) ROCPROFSYS_HIDDEN_API;
98+
bool rocprofsys_init_tooling_hidden(void) ROCPROFSYS_HIDDEN_API;
9999
void rocprofsys_init_hidden(const char*, bool, const char*) ROCPROFSYS_HIDDEN_API;
100100
void rocprofsys_finalize_hidden(void) ROCPROFSYS_HIDDEN_API;
101101
void rocprofsys_reset_preload_hidden(void) ROCPROFSYS_HIDDEN_API;

projects/rocprofiler-systems/source/lib/rocprof-sys/library.cpp

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -404,42 +404,16 @@ rocprofsys_init_library_hidden()
404404
ROCPROFSYS_CONDITIONAL_BASIC_PRINT_F(_debug_init, "\n");
405405
}
406406

407-
// Initialize RCCL if:
408-
// - postinit=true - so the code doesn't hang at the initialization stage
409-
// - get_state() >= State::Init - so the code doesn't throw an exception
410-
// - rccl_initialized=false - so we don't try to initialize RCCL twice
411-
// - get_use_rcclp()=true - only if the environment is configured to use RCCL
412-
static void
413-
rccl_setup(bool postinit)
414-
{
415-
// Flag used to avoid initializing RCCL twice
416-
static bool rccl_initialized = false;
417-
418-
if(postinit && (get_state() >= State::Init) && !rccl_initialized && get_use_rcclp())
419-
{
420-
ROCPROFSYS_VERBOSE_F(1, "Setting up RCCLP...\n");
421-
rcclp::setup();
422-
rccl_initialized = true;
423-
}
424-
}
425-
426-
static void
427-
rocprofsys_init_library_hidden_with_rccl(bool postinit)
428-
{
429-
rocprofsys_init_library_hidden();
430-
rccl_setup(postinit);
431-
}
432-
433407
//======================================================================================//
434408

435409
extern "C" bool
436-
rocprofsys_init_tooling_hidden(bool postinit)
410+
rocprofsys_init_tooling_hidden()
437411
{
438412
if(get_env("ROCPROFSYS_MONOCHROME", false, false)) tim::log::monochrome() = true;
439413

440414
if(!tim::get_env("ROCPROFSYS_INIT_TOOLING", true))
441415
{
442-
rocprofsys_init_library_hidden_with_rccl(postinit);
416+
rocprofsys_init_library_hidden();
443417
return false;
444418
}
445419

@@ -456,11 +430,7 @@ rocprofsys_init_tooling_hidden(bool postinit)
456430
ROCPROFSYS_CONDITIONAL_BASIC_PRINT_F(_debug_init, "State is %s...\n",
457431
std::to_string(get_state()).c_str());
458432

459-
if(get_state() != State::PreInit || get_state() == State::Init || _once)
460-
{
461-
rccl_setup(postinit);
462-
return false;
463-
}
433+
if(get_state() != State::PreInit || get_state() == State::Init || _once) return false;
464434
_once = true;
465435

466436
ROCPROFSYS_SCOPED_THREAD_STATE(ThreadState::Internal);
@@ -481,7 +451,7 @@ rocprofsys_init_tooling_hidden(bool postinit)
481451
ROCPROFSYS_CONDITIONAL_BASIC_PRINT_F(_debug_init,
482452
"Calling rocprofsys_init_library()...\n");
483453

484-
rocprofsys_init_library_hidden_with_rccl(postinit);
454+
rocprofsys_init_library_hidden();
485455

486456
ROCPROFSYS_DEBUG_F("\n");
487457

@@ -579,6 +549,23 @@ rocprofsys_init_tooling_hidden(bool postinit)
579549
ompt::setup();
580550
}
581551

552+
#if defined(ROCPROFSYS_USE_ROCM) && ROCPROFSYS_USE_ROCM > 0
553+
// Force rocprofiler_configure if it hasn't been called through __hip_module_ctor.
554+
// rocprofiler_configure needs to be called before rcclp::setup to decide
555+
// whether we want to use gotcha wrappers for rccl or rocpofiler based tracing.
556+
if(get_use_rocm())
557+
{
558+
ROCPROFSYS_VERBOSE_F(1, "Setting up ROCm...\n");
559+
rocprofiler_sdk::setup();
560+
}
561+
#endif
562+
563+
if(get_use_rcclp())
564+
{
565+
ROCPROFSYS_VERBOSE_F(1, "Setting up RCCLP...\n");
566+
rcclp::setup();
567+
}
568+
582569
if(get_use_perfetto())
583570
{
584571
ROCPROFSYS_VERBOSE_F(1, "Starting Perfetto...\n");

projects/rocprofiler-systems/source/lib/rocprof-sys/library/rocprofiler-sdk.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,12 +1301,14 @@ rocprofiler_configure(uint32_t version, const char* runtime_version, uint32_t pr
13011301
_first = false;
13021302
}
13031303

1304-
if(!tim::get_env("ROCPROFSYS_INIT_TOOLING", true)) return nullptr;
1305-
if(!tim::settings::enabled()) return nullptr;
1304+
// If ROCPROFSYS_PRELOAD or ROCPROFSYS_INIT_TOOLING is not set,
1305+
// the tooling will not be initialized so we cannot enable
1306+
// profiling with rocprofiler
1307+
if(!tim::get_env("ROCPROFSYS_PRELOAD", true) ||
1308+
!tim::get_env("ROCPROFSYS_INIT_TOOLING", true))
1309+
return nullptr;
13061310

1307-
if(!rocprofsys::config::settings_are_configured() &&
1308-
rocprofsys::get_state() < rocprofsys::State::Active)
1309-
rocprofsys_init_tooling_hidden();
1311+
if(!tim::settings::enabled()) return nullptr;
13101312

13111313
if(!rocprofsys::config::get_use_rocm())
13121314
{

0 commit comments

Comments
 (0)