From b7308cd6455403d3ba04cc51a3911a2659a71037 Mon Sep 17 00:00:00 2001 From: David Poliakoff Date: Mon, 28 Jun 2021 08:57:44 -0600 Subject: [PATCH 1/2] Allowed Caliper to control Kokkos fencing behavior --- src/services/kokkos/CMakeLists.txt | 1 + .../kokkos/KokkosProfilingSymbols.cpp | 30 ++ .../kokkos/KokkosProfilingSymbols.hpp | 4 +- .../impl/Kokkos_Profiling_C_Interface.h | 296 ++++++++++++++++++ .../impl/Kokkos_Profiling_DeviceInfo.hpp | 56 ++++ .../impl/Kokkos_Profiling_Interface.hpp | 223 +++++++++++++ 6 files changed, 608 insertions(+), 2 deletions(-) create mode 100644 src/services/kokkos/include/impl/Kokkos_Profiling_C_Interface.h create mode 100644 src/services/kokkos/include/impl/Kokkos_Profiling_DeviceInfo.hpp create mode 100644 src/services/kokkos/include/impl/Kokkos_Profiling_Interface.hpp diff --git a/src/services/kokkos/CMakeLists.txt b/src/services/kokkos/CMakeLists.txt index aac0cccd8..cc0804d61 100644 --- a/src/services/kokkos/CMakeLists.txt +++ b/src/services/kokkos/CMakeLists.txt @@ -1,3 +1,4 @@ +include_directories(include) set(CALIPER_KOKKOS_SOURCES KokkosProfilingSymbols.cpp KokkosLookup.cpp diff --git a/src/services/kokkos/KokkosProfilingSymbols.cpp b/src/services/kokkos/KokkosProfilingSymbols.cpp index 51b594107..6558cbd70 100644 --- a/src/services/kokkos/KokkosProfilingSymbols.cpp +++ b/src/services/kokkos/KokkosProfilingSymbols.cpp @@ -3,10 +3,19 @@ #include "caliper/cali.h" #include "types.hpp" #include +#include cali::kokkos::callbacks kokkosp_callbacks; using cali::kokkos::SpaceHandle; namespace kokkos { cali::ConfigManager mgr; + bool can_control_fences = false; + Kokkos::Tools::Experimental::ToolProgrammingInterface kokkos_interface; + template + void fence_if_subscribed(const Container& in, uint32_t dev_id){ + if(can_control_fences && !in.empty()){ + kokkos_interface.fence(dev_id); + } + } } extern "C" void kokkosp_print_help(char* progName){ std::cerr << "Caliper: available configs: \n"; @@ -14,6 +23,16 @@ extern "C" void kokkosp_print_help(char* progName){ std::cerr << kokkos::mgr.get_documentation_for_spec(conf.c_str()) << std::endl; } } +extern "C" void kokkosp_request_tool_settings(int num_actions, Kokkos::Tools::Experimental::ToolSettings* settings){ + if((num_actions > 0) && (settings!=nullptr)){ + settings->requires_global_fencing = false; + } +} + +extern "C" void kokkosp_provide_tool_programming_interface(int num_actions, Kokkos::Tools::Experimental::ToolProgrammingInterface interface){ + kokkos::can_control_fences = true; + kokkos::kokkos_interface = interface; +} extern "C" void kokkosp_parse_args(int argc, char *argv_raw[]) { if (argc > 2) { std::cerr << "Error: the Kokkos Caliper connector takes only one argument" @@ -46,17 +65,23 @@ extern "C" void kokkosp_finalize_library() { extern "C" void kokkosp_begin_parallel_for(const char *name, const uint32_t devID, uint64_t *kID) { + kokkos::fence_if_subscribed(kokkosp_callbacks.kokkosp_begin_parallel_for_callback, devID); kokkosp_callbacks.kokkosp_begin_parallel_for_callback(name, devID, kID); + *kID = devID; } extern "C" void kokkosp_begin_parallel_reduce(const char *name, const uint32_t devID, uint64_t *kID) { + kokkos::fence_if_subscribed(kokkosp_callbacks.kokkosp_begin_parallel_reduce_callback, devID); kokkosp_callbacks.kokkosp_begin_parallel_reduce_callback(name, devID, kID); + *kID = devID; } extern "C" void kokkosp_begin_parallel_scan(const char *name, const uint32_t devID, uint64_t *kID) { + kokkos::fence_if_subscribed(kokkosp_callbacks.kokkosp_begin_parallel_scan_callback, devID); kokkosp_callbacks.kokkosp_begin_parallel_scan_callback(name, devID, kID); + *kID = devID; } extern "C" void kokkosp_begin_fence(const char *name, const uint32_t devID, uint64_t *kID) { @@ -64,12 +89,15 @@ extern "C" void kokkosp_begin_fence(const char *name, const uint32_t devID, } extern "C" void kokkosp_end_parallel_for(const uint64_t kID) { + kokkos::fence_if_subscribed(kokkosp_callbacks.kokkosp_end_parallel_for_callback, kID); kokkosp_callbacks.kokkosp_end_parallel_for_callback(kID); } extern "C" void kokkosp_end_parallel_reduce(const uint64_t kID) { + kokkos::fence_if_subscribed(kokkosp_callbacks.kokkosp_end_parallel_reduce_callback, kID); kokkosp_callbacks.kokkosp_end_parallel_reduce_callback(kID); } extern "C" void kokkosp_end_parallel_scan(const uint64_t kID) { + kokkos::fence_if_subscribed(kokkosp_callbacks.kokkosp_end_parallel_scan_callback, kID); kokkosp_callbacks.kokkosp_end_parallel_scan_callback(kID); } extern "C" void kokkosp_end_fence(const uint64_t kID) { @@ -77,9 +105,11 @@ extern "C" void kokkosp_end_fence(const uint64_t kID) { } extern "C" void kokkosp_push_profile_region(char *regionName) { + kokkos::fence_if_subscribed(kokkosp_callbacks.kokkosp_push_region_callback, 0); kokkosp_callbacks.kokkosp_push_region_callback(regionName); } extern "C" void kokkosp_pop_profile_region() { + kokkos::fence_if_subscribed(kokkosp_callbacks.kokkosp_pop_region_callback, 0); kokkosp_callbacks.kokkosp_pop_region_callback(); } extern "C" void kokkosp_allocate_data(const SpaceHandle space, diff --git a/src/services/kokkos/KokkosProfilingSymbols.hpp b/src/services/kokkos/KokkosProfilingSymbols.hpp index b69e5f226..cfebc186e 100644 --- a/src/services/kokkos/KokkosProfilingSymbols.hpp +++ b/src/services/kokkos/KokkosProfilingSymbols.hpp @@ -1,9 +1,9 @@ #ifndef CALIPER_SERVICES_KOKKOS_PROFILING_SYMBOLS_HPP #define CALIPER_SERVICES_KOKKOS_PROFILING_SYMBOLS_HPP - +//comment #include "types.hpp" - +#include #endif diff --git a/src/services/kokkos/include/impl/Kokkos_Profiling_C_Interface.h b/src/services/kokkos/include/impl/Kokkos_Profiling_C_Interface.h new file mode 100644 index 000000000..ed8751c50 --- /dev/null +++ b/src/services/kokkos/include/impl/Kokkos_Profiling_C_Interface.h @@ -0,0 +1,296 @@ +/* +//@HEADER +// ************************************************************************ +// +// Kokkos v. 3.0 +// Copyright (2020) National Technology & Engineering +// Solutions of Sandia, LLC (NTESS). +// +// Under the terms of Contract DE-NA0003525 with NTESS, +// the U.S. Government retains certain rights in this software. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// 1. Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// 3. Neither the name of the Corporation nor the names of the +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY NTESS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NTESS OR THE +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Questions? Contact Christian R. Trott (crtrott@sandia.gov) +// +// ************************************************************************ +//@HEADER +*/ + +#ifndef KOKKOS_PROFILING_C_INTERFACE_HPP +#define KOKKOS_PROFILING_C_INTERFACE_HPP + +#ifdef __cplusplus +#include +#include +#else +#include +#include +#include +#endif + +#define KOKKOSP_INTERFACE_VERSION 20210225 + +// Profiling + +struct Kokkos_Profiling_KokkosPDeviceInfo { + size_t deviceID; +}; + +struct Kokkos_Profiling_SpaceHandle { + char name[64]; +}; + +// NOLINTNEXTLINE(modernize-use-using): C compatibility +typedef void (*Kokkos_Profiling_initFunction)( + const int, const uint64_t, const uint32_t, + struct Kokkos_Profiling_KokkosPDeviceInfo*); +// NOLINTNEXTLINE(modernize-use-using): C compatibility +typedef void (*Kokkos_Profiling_finalizeFunction)(); +// NOLINTNEXTLINE(modernize-use-using): C compatibility +typedef void (*Kokkos_Profiling_parseArgsFunction)(int, char**); +// NOLINTNEXTLINE(modernize-use-using): C compatibility +typedef void (*Kokkos_Profiling_printHelpFunction)(char*); +// NOLINTNEXTLINE(modernize-use-using): C compatibility +typedef void (*Kokkos_Profiling_beginFunction)(const char*, const uint32_t, + uint64_t*); +// NOLINTNEXTLINE(modernize-use-using): C compatibility +typedef void (*Kokkos_Profiling_endFunction)(uint64_t); + +// NOLINTNEXTLINE(modernize-use-using): C compatibility +typedef void (*Kokkos_Profiling_pushFunction)(const char*); +// NOLINTNEXTLINE(modernize-use-using): C compatibility +typedef void (*Kokkos_Profiling_popFunction)(); + +// NOLINTNEXTLINE(modernize-use-using): C compatibility +typedef void (*Kokkos_Profiling_allocateDataFunction)( + const struct Kokkos_Profiling_SpaceHandle, const char*, const void*, + const uint64_t); +// NOLINTNEXTLINE(modernize-use-using): C compatibility +typedef void (*Kokkos_Profiling_deallocateDataFunction)( + const struct Kokkos_Profiling_SpaceHandle, const char*, const void*, + const uint64_t); + +// NOLINTNEXTLINE(modernize-use-using): C compatibility +typedef void (*Kokkos_Profiling_createProfileSectionFunction)(const char*, + uint32_t*); +// NOLINTNEXTLINE(modernize-use-using): C compatibility +typedef void (*Kokkos_Profiling_startProfileSectionFunction)(const uint32_t); +// NOLINTNEXTLINE(modernize-use-using): C compatibility +typedef void (*Kokkos_Profiling_stopProfileSectionFunction)(const uint32_t); +// NOLINTNEXTLINE(modernize-use-using): C compatibility +typedef void (*Kokkos_Profiling_destroyProfileSectionFunction)(const uint32_t); + +// NOLINTNEXTLINE(modernize-use-using): C compatibility +typedef void (*Kokkos_Profiling_profileEventFunction)(const char*); + +// NOLINTNEXTLINE(modernize-use-using): C compatibility +typedef void (*Kokkos_Profiling_beginDeepCopyFunction)( + struct Kokkos_Profiling_SpaceHandle, const char*, const void*, + struct Kokkos_Profiling_SpaceHandle, const char*, const void*, uint64_t); +// NOLINTNEXTLINE(modernize-use-using): C compatibility +typedef void (*Kokkos_Profiling_endDeepCopyFunction)(); +typedef void (*Kokkos_Profiling_beginFenceFunction)(const char*, const uint32_t, + uint64_t*); +typedef void (*Kokkos_Profiling_endFenceFunction)(uint64_t); + +// NOLINTNEXTLINE(modernize-use-using): C compatibility +typedef void (*Kokkos_Profiling_dualViewSyncFunction)(const char*, + const void* const, bool); +// NOLINTNEXTLINE(modernize-use-using): C compatibility +typedef void (*Kokkos_Profiling_dualViewModifyFunction)(const char*, + const void* const, + bool); + +// NOLINTNEXTLINE(modernize-use-using): C compatibility +typedef void (*Kokkos_Profiling_declareMetadataFunction)(const char*, + const char*); + +// NOLINTNEXTLINE(modernize-use-using): C compatibility +typedef void (*Kokkos_Tools_toolInvokedFenceFunction)(const uint32_t); + +// NOLINTNEXTLINE(modernize-use-using): C compatibility +typedef void (*Kokkos_Tools_functionPointer)(); +struct Kokkos_Tools_ToolProgrammingInterface { + Kokkos_Tools_toolInvokedFenceFunction fence; + // allow addition of more actions + Kokkos_Tools_functionPointer padding[31]; +}; + +struct Kokkos_Tools_ToolSettings { + bool requires_global_fencing; + bool padding[255]; +}; + +// NOLINTNEXTLINE(modernize-use-using): C compatibility +typedef void (*Kokkos_Tools_provideToolProgrammingInterfaceFunction)( + const uint32_t, struct Kokkos_Tools_ToolProgrammingInterface); +// NOLINTNEXTLINE(modernize-use-using): C compatibility +typedef void (*Kokkos_Tools_requestToolSettingsFunction)( + const uint32_t, struct Kokkos_Tools_ToolSettings*); + +// Tuning + +#define KOKKOS_TOOLS_TUNING_STRING_LENGTH 64 +typedef char Kokkos_Tools_Tuning_String[KOKKOS_TOOLS_TUNING_STRING_LENGTH]; +union Kokkos_Tools_VariableValue_ValueUnion { + int64_t int_value; + double double_value; + Kokkos_Tools_Tuning_String string_value; +}; + +union Kokkos_Tools_VariableValue_ValueUnionSet { + int64_t* int_value; + double* double_value; + Kokkos_Tools_Tuning_String* string_value; +}; + +struct Kokkos_Tools_ValueSet { + size_t size; + union Kokkos_Tools_VariableValue_ValueUnionSet values; +}; + +enum Kokkos_Tools_OptimizationType { + Kokkos_Tools_Minimize, + Kokkos_Tools_Maximize +}; + +struct Kokkos_Tools_OptimzationGoal { + size_t type_id; + enum Kokkos_Tools_OptimizationType goal; +}; + +struct Kokkos_Tools_ValueRange { + union Kokkos_Tools_VariableValue_ValueUnion lower; + union Kokkos_Tools_VariableValue_ValueUnion upper; + union Kokkos_Tools_VariableValue_ValueUnion step; + bool openLower; + bool openUpper; +}; + +enum Kokkos_Tools_VariableInfo_ValueType { + kokkos_value_double, + kokkos_value_int64, + kokkos_value_string, +}; + +enum Kokkos_Tools_VariableInfo_StatisticalCategory { + kokkos_value_categorical, // unordered distinct objects + kokkos_value_ordinal, // ordered distinct objects + kokkos_value_interval, // ordered distinct objects for which distance matters + kokkos_value_ratio // ordered distinct objects for which distance matters, + // division matters, and the concept of zero exists +}; + +enum Kokkos_Tools_VariableInfo_CandidateValueType { + kokkos_value_set, // I am one of [2,3,4,5] + kokkos_value_range, // I am somewhere in [2,12) + kokkos_value_unbounded // I am [text/int/float], but we don't know at + // declaration time what values are appropriate. Only + // valid for Context Variables +}; + +union Kokkos_Tools_VariableInfo_SetOrRange { + struct Kokkos_Tools_ValueSet set; + struct Kokkos_Tools_ValueRange range; +}; + +struct Kokkos_Tools_VariableInfo { + enum Kokkos_Tools_VariableInfo_ValueType type; + enum Kokkos_Tools_VariableInfo_StatisticalCategory category; + enum Kokkos_Tools_VariableInfo_CandidateValueType valueQuantity; + union Kokkos_Tools_VariableInfo_SetOrRange candidates; + void* toolProvidedInfo; +}; + +struct Kokkos_Tools_VariableValue { + size_t type_id; + union Kokkos_Tools_VariableValue_ValueUnion value; + struct Kokkos_Tools_VariableInfo* metadata; +}; + +typedef void (*Kokkos_Tools_outputTypeDeclarationFunction)( + const char*, const size_t, struct Kokkos_Tools_VariableInfo* info); +typedef void (*Kokkos_Tools_inputTypeDeclarationFunction)( + const char*, const size_t, struct Kokkos_Tools_VariableInfo* info); + +typedef void (*Kokkos_Tools_requestValueFunction)( + const size_t, const size_t, const struct Kokkos_Tools_VariableValue*, + const size_t count, struct Kokkos_Tools_VariableValue*); +typedef void (*Kokkos_Tools_contextBeginFunction)(const size_t); +typedef void (*Kokkos_Tools_contextEndFunction)( + const size_t, struct Kokkos_Tools_VariableValue); +typedef void (*Kokkos_Tools_optimizationGoalDeclarationFunction)( + const size_t, const struct Kokkos_Tools_OptimzationGoal goal); + +struct Kokkos_Profiling_EventSet { + Kokkos_Profiling_initFunction init; + Kokkos_Profiling_finalizeFunction finalize; + Kokkos_Profiling_parseArgsFunction parse_args; + Kokkos_Profiling_printHelpFunction print_help; + Kokkos_Profiling_beginFunction begin_parallel_for; + Kokkos_Profiling_endFunction end_parallel_for; + Kokkos_Profiling_beginFunction begin_parallel_reduce; + Kokkos_Profiling_endFunction end_parallel_reduce; + Kokkos_Profiling_beginFunction begin_parallel_scan; + Kokkos_Profiling_endFunction end_parallel_scan; + Kokkos_Profiling_pushFunction push_region; + Kokkos_Profiling_popFunction pop_region; + Kokkos_Profiling_allocateDataFunction allocate_data; + Kokkos_Profiling_deallocateDataFunction deallocate_data; + Kokkos_Profiling_createProfileSectionFunction create_profile_section; + Kokkos_Profiling_startProfileSectionFunction start_profile_section; + Kokkos_Profiling_stopProfileSectionFunction stop_profile_section; + Kokkos_Profiling_destroyProfileSectionFunction destroy_profile_section; + Kokkos_Profiling_profileEventFunction profile_event; + Kokkos_Profiling_beginDeepCopyFunction begin_deep_copy; + Kokkos_Profiling_endDeepCopyFunction end_deep_copy; + Kokkos_Profiling_beginFenceFunction begin_fence; + Kokkos_Profiling_endFenceFunction end_fence; + Kokkos_Profiling_dualViewSyncFunction sync_dual_view; + Kokkos_Profiling_dualViewModifyFunction modify_dual_view; + Kokkos_Profiling_declareMetadataFunction declare_metadata; + Kokkos_Tools_provideToolProgrammingInterfaceFunction + provide_tool_programming_interface; + Kokkos_Tools_requestToolSettingsFunction request_tool_settings; + char profiling_padding[9 * sizeof(Kokkos_Tools_functionPointer)]; + Kokkos_Tools_outputTypeDeclarationFunction declare_output_type; + Kokkos_Tools_inputTypeDeclarationFunction declare_input_type; + Kokkos_Tools_requestValueFunction request_output_values; + Kokkos_Tools_contextBeginFunction begin_tuning_context; + Kokkos_Tools_contextEndFunction end_tuning_context; + Kokkos_Tools_optimizationGoalDeclarationFunction declare_optimization_goal; + char padding[232 * + sizeof( + Kokkos_Tools_functionPointer)]; // allows us to add another + // 256 events to the Tools + // interface without + // changing struct layout +}; + +#endif // KOKKOS_PROFILING_C_INTERFACE_HPP diff --git a/src/services/kokkos/include/impl/Kokkos_Profiling_DeviceInfo.hpp b/src/services/kokkos/include/impl/Kokkos_Profiling_DeviceInfo.hpp new file mode 100644 index 000000000..be6f756d0 --- /dev/null +++ b/src/services/kokkos/include/impl/Kokkos_Profiling_DeviceInfo.hpp @@ -0,0 +1,56 @@ +/* + //@HEADER + // ************************************************************************ + // + // Kokkos v. 3.0 +// Copyright (2020) National Technology & Engineering +// Solutions of Sandia, LLC (NTESS). + // + // Under the terms of Contract DE-NA0003525 with NTESS, + // the U.S. Government retains certain rights in this software. + // + // Redistribution and use in source and binary forms, with or without + // modification, are permitted provided that the following conditions are + // met: + // + // 1. Redistributions of source code must retain the above copyright + // notice, this list of conditions and the following disclaimer. + // + // 2. Redistributions in binary form must reproduce the above copyright + // notice, this list of conditions and the following disclaimer in the + // documentation and/or other materials provided with the distribution. + // + // 3. Neither the name of the Corporation nor the names of the + // contributors may be used to endorse or promote products derived from + // this software without specific prior written permission. + // + // THIS SOFTWARE IS PROVIDED BY NTESS "AS IS" AND ANY + // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NTESS OR THE + // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + // + // Questions? Contact Christian R. Trott (crtrott@sandia.gov) + // + // ************************************************************************ + //@HEADER +*/ + +#ifndef KOKKOSP_DEVICE_INFO_HPP +#define KOKKOSP_DEVICE_INFO_HPP + +#include +#include +namespace Kokkos { +namespace Profiling { +using KokkosPDeviceInfo = Kokkos_Profiling_KokkosPDeviceInfo; +} // namespace Profiling +} // namespace Kokkos + +#endif diff --git a/src/services/kokkos/include/impl/Kokkos_Profiling_Interface.hpp b/src/services/kokkos/include/impl/Kokkos_Profiling_Interface.hpp new file mode 100644 index 000000000..7809632f7 --- /dev/null +++ b/src/services/kokkos/include/impl/Kokkos_Profiling_Interface.hpp @@ -0,0 +1,223 @@ +/* + //@HEADER + // ************************************************************************ + // + // Kokkos v. 3.0 +// Copyright (2020) National Technology & Engineering +// Solutions of Sandia, LLC (NTESS). + // + // Under the terms of Contract DE-NA0003525 with NTESS, + // the U.S. Government retains certain rights in this software. + // + // Redistribution and use in source and binary forms, with or without + // modification, are permitted provided that the following conditions are + // met: + // + // 1. Redistributions of source code must retain the above copyright + // notice, this list of conditions and the following disclaimer. + // + // 2. Redistributions in binary form must reproduce the above copyright + // notice, this list of conditions and the following disclaimer in the + // documentation and/or other materials provided with the distribution. + // + // 3. Neither the name of the Corporation nor the names of the + // contributors may be used to endorse or promote products derived from + // this software without specific prior written permission. + // + // THIS SOFTWARE IS PROVIDED BY NTESS "AS IS" AND ANY + // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NTESS OR THE + // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + // + // Questions? Contact Christian R. Trott (crtrott@sandia.gov) + // + // ************************************************************************ + //@HEADER + */ + +#ifndef KOKKOSP_INTERFACE_HPP +#define KOKKOSP_INTERFACE_HPP + +#include +#include + +#include + +// NOTE: in this Kokkos::Profiling block, do not define anything that shouldn't +// exist should Profiling be disabled + +namespace Kokkos { +namespace Tools { +namespace Experimental { +enum struct DeviceType { + Serial, + OpenMP, + Cuda, + HIP, + OpenMPTarget, + HPX, + Threads, + SYCL, + Unknown +}; + +template +struct DeviceTypeTraits; + +constexpr const size_t device_type_bits = 8; +constexpr const size_t instance_bits = 24; +template +inline uint32_t device_id(ExecutionSpace const& space) noexcept { + auto device_id = static_cast(DeviceTypeTraits::id); + return (device_id << instance_bits) + space.impl_instance_id(); +} +} // namespace Experimental +} // namespace Tools +} // end namespace Kokkos + +#if defined(KOKKOS_ENABLE_LIBDL) +// We check at configure time that libdl is available. +#include +#endif + +#include +#include + +namespace Kokkos { +namespace Tools { + +using SpaceHandle = Kokkos_Profiling_SpaceHandle; + +} // namespace Tools + +namespace Tools { + +namespace Experimental { +using EventSet = Kokkos_Profiling_EventSet; +static_assert(sizeof(EventSet) / sizeof(Kokkos_Tools_functionPointer) == 275, + "sizeof EventSet has changed, this is an error on the part of a " + "Kokkos developer"); +static_assert(sizeof(Kokkos_Tools_ToolSettings) / sizeof(bool) == 256, + "sizeof EventSet has changed, this is an error on the part of a " + "Kokkos developer"); +static_assert(sizeof(Kokkos_Tools_ToolProgrammingInterface) / + sizeof(Kokkos_Tools_functionPointer) == + 32, + "sizeof EventSet has changed, this is an error on the part of a " + "Kokkos developer"); + +using toolInvokedFenceFunction = Kokkos_Tools_toolInvokedFenceFunction; +using provideToolProgrammingInterfaceFunction = + Kokkos_Tools_provideToolProgrammingInterfaceFunction; +using requestToolSettingsFunction = Kokkos_Tools_requestToolSettingsFunction; +using ToolSettings = Kokkos_Tools_ToolSettings; +using ToolProgrammingInterface = Kokkos_Tools_ToolProgrammingInterface; +} // namespace Experimental +using initFunction = Kokkos_Profiling_initFunction; +using finalizeFunction = Kokkos_Profiling_finalizeFunction; +using parseArgsFunction = Kokkos_Profiling_parseArgsFunction; +using printHelpFunction = Kokkos_Profiling_printHelpFunction; +using beginFunction = Kokkos_Profiling_beginFunction; +using endFunction = Kokkos_Profiling_endFunction; +using pushFunction = Kokkos_Profiling_pushFunction; +using popFunction = Kokkos_Profiling_popFunction; +using allocateDataFunction = Kokkos_Profiling_allocateDataFunction; +using deallocateDataFunction = Kokkos_Profiling_deallocateDataFunction; +using createProfileSectionFunction = + Kokkos_Profiling_createProfileSectionFunction; +using startProfileSectionFunction = + Kokkos_Profiling_startProfileSectionFunction; +using stopProfileSectionFunction = Kokkos_Profiling_stopProfileSectionFunction; +using destroyProfileSectionFunction = + Kokkos_Profiling_destroyProfileSectionFunction; +using profileEventFunction = Kokkos_Profiling_profileEventFunction; +using beginDeepCopyFunction = Kokkos_Profiling_beginDeepCopyFunction; +using endDeepCopyFunction = Kokkos_Profiling_endDeepCopyFunction; +using beginFenceFunction = Kokkos_Profiling_beginFenceFunction; +using endFenceFunction = Kokkos_Profiling_endFenceFunction; +using dualViewSyncFunction = Kokkos_Profiling_dualViewSyncFunction; +using dualViewModifyFunction = Kokkos_Profiling_dualViewModifyFunction; +using declareMetadataFunction = Kokkos_Profiling_declareMetadataFunction; + +} // namespace Tools + +} // namespace Kokkos + +// Profiling + +namespace Kokkos { + +namespace Profiling { + +/** The Profiling namespace is being renamed to Tools. + * This is reexposing the contents of what used to be the Profiling + * Interface with their original names, to avoid breaking old code + */ + +namespace Experimental { + +using Kokkos::Tools::Experimental::device_id; +using Kokkos::Tools::Experimental::DeviceType; +using Kokkos::Tools::Experimental::DeviceTypeTraits; + +} // namespace Experimental + +using Kokkos::Tools::allocateDataFunction; +using Kokkos::Tools::beginDeepCopyFunction; +using Kokkos::Tools::beginFunction; +using Kokkos::Tools::createProfileSectionFunction; +using Kokkos::Tools::deallocateDataFunction; +using Kokkos::Tools::destroyProfileSectionFunction; +using Kokkos::Tools::endDeepCopyFunction; +using Kokkos::Tools::endFunction; +using Kokkos::Tools::finalizeFunction; +using Kokkos::Tools::initFunction; +using Kokkos::Tools::parseArgsFunction; +using Kokkos::Tools::popFunction; +using Kokkos::Tools::printHelpFunction; +using Kokkos::Tools::profileEventFunction; +using Kokkos::Tools::pushFunction; +using Kokkos::Tools::SpaceHandle; +using Kokkos::Tools::startProfileSectionFunction; +using Kokkos::Tools::stopProfileSectionFunction; + +} // namespace Profiling +} // namespace Kokkos + +// Tuning + +namespace Kokkos { +namespace Tools { +namespace Experimental { +using ValueSet = Kokkos_Tools_ValueSet; +using ValueRange = Kokkos_Tools_ValueRange; +using StatisticalCategory = Kokkos_Tools_VariableInfo_StatisticalCategory; +using ValueType = Kokkos_Tools_VariableInfo_ValueType; +using CandidateValueType = Kokkos_Tools_VariableInfo_CandidateValueType; +using SetOrRange = Kokkos_Tools_VariableInfo_SetOrRange; +using VariableInfo = Kokkos_Tools_VariableInfo; +using OptimizationGoal = Kokkos_Tools_OptimzationGoal; +using TuningString = Kokkos_Tools_Tuning_String; +using VariableValue = Kokkos_Tools_VariableValue; + +using outputTypeDeclarationFunction = + Kokkos_Tools_outputTypeDeclarationFunction; +using inputTypeDeclarationFunction = Kokkos_Tools_inputTypeDeclarationFunction; +using requestValueFunction = Kokkos_Tools_requestValueFunction; +using contextBeginFunction = Kokkos_Tools_contextBeginFunction; +using contextEndFunction = Kokkos_Tools_contextEndFunction; +using optimizationGoalDeclarationFunction = + Kokkos_Tools_optimizationGoalDeclarationFunction; +} // end namespace Experimental +} // end namespace Tools + +} // end namespace Kokkos + +#endif From c430e93bbfcebc91bd2624128162d836fbea4096 Mon Sep 17 00:00:00 2001 From: David Poliakoff Date: Tue, 29 Jun 2021 16:36:59 -0600 Subject: [PATCH 2/2] Added conditional fencing logic --- .../kokkos/KokkosProfilingSymbols.cpp | 69 ++++++++++++------- 1 file changed, 45 insertions(+), 24 deletions(-) diff --git a/src/services/kokkos/KokkosProfilingSymbols.cpp b/src/services/kokkos/KokkosProfilingSymbols.cpp index 6558cbd70..5f886d584 100644 --- a/src/services/kokkos/KokkosProfilingSymbols.cpp +++ b/src/services/kokkos/KokkosProfilingSymbols.cpp @@ -7,31 +7,37 @@ cali::kokkos::callbacks kokkosp_callbacks; using cali::kokkos::SpaceHandle; namespace kokkos { - cali::ConfigManager mgr; - bool can_control_fences = false; - Kokkos::Tools::Experimental::ToolProgrammingInterface kokkos_interface; - template - void fence_if_subscribed(const Container& in, uint32_t dev_id){ - if(can_control_fences && !in.empty()){ - kokkos_interface.fence(dev_id); - } - } +cali::ConfigManager mgr; +bool can_control_fences = false; +bool config_needs_fences = true; +Kokkos::Tools::Experimental::ToolProgrammingInterface kokkos_interface; +template +void fence_if_needed(const Container &in, uint32_t dev_id, + const FenceIf &predicate) { + if (can_control_fences && !in.empty() && predicate()) { + kokkos_interface.fence(dev_id); + } } -extern "C" void kokkosp_print_help(char* progName){ +} // namespace kokkos +extern "C" void kokkosp_print_help(char *progName) { std::cerr << "Caliper: available configs: \n"; - for(auto conf: kokkos::mgr.available_config_specs() ) { - std::cerr << kokkos::mgr.get_documentation_for_spec(conf.c_str()) << std::endl; + for (auto conf : kokkos::mgr.available_config_specs()) { + std::cerr << kokkos::mgr.get_documentation_for_spec(conf.c_str()) + << std::endl; } } -extern "C" void kokkosp_request_tool_settings(int num_actions, Kokkos::Tools::Experimental::ToolSettings* settings){ - if((num_actions > 0) && (settings!=nullptr)){ +extern "C" void kokkosp_request_tool_settings( + int num_actions, Kokkos::Tools::Experimental::ToolSettings *settings) { + if ((num_actions > 0) && (settings != nullptr)) { settings->requires_global_fencing = false; } } -extern "C" void kokkosp_provide_tool_programming_interface(int num_actions, Kokkos::Tools::Experimental::ToolProgrammingInterface interface){ +extern "C" void kokkosp_provide_tool_programming_interface( + int num_actions, + Kokkos::Tools::Experimental::ToolProgrammingInterface interface) { kokkos::can_control_fences = true; - kokkos::kokkos_interface = interface; + kokkos::kokkos_interface = interface; } extern "C" void kokkosp_parse_args(int argc, char *argv_raw[]) { if (argc > 2) { @@ -39,6 +45,10 @@ extern "C" void kokkosp_parse_args(int argc, char *argv_raw[]) { << std::endl; } if (argc == 2) { + if (std::string(argv_raw[1]).find("cuda-activity-report") != + std::string::npos) { + kokkos::config_needs_fences = false; + } kokkos::mgr.add(argv_raw[1]); if (kokkos::mgr.error()) { std::cerr << "Kokkos Caliper connector error: " << kokkos::mgr.error_msg() @@ -65,21 +75,26 @@ extern "C" void kokkosp_finalize_library() { extern "C" void kokkosp_begin_parallel_for(const char *name, const uint32_t devID, uint64_t *kID) { - kokkos::fence_if_subscribed(kokkosp_callbacks.kokkosp_begin_parallel_for_callback, devID); + kokkos::fence_if_needed(kokkosp_callbacks.kokkosp_begin_parallel_for_callback, + devID, [=]() { return kokkos::config_needs_fences; }); kokkosp_callbacks.kokkosp_begin_parallel_for_callback(name, devID, kID); *kID = devID; } extern "C" void kokkosp_begin_parallel_reduce(const char *name, const uint32_t devID, uint64_t *kID) { - kokkos::fence_if_subscribed(kokkosp_callbacks.kokkosp_begin_parallel_reduce_callback, devID); + kokkos::fence_if_needed( + kokkosp_callbacks.kokkosp_begin_parallel_reduce_callback, devID, + [=]() { return kokkos::config_needs_fences; }); kokkosp_callbacks.kokkosp_begin_parallel_reduce_callback(name, devID, kID); *kID = devID; } extern "C" void kokkosp_begin_parallel_scan(const char *name, const uint32_t devID, uint64_t *kID) { - kokkos::fence_if_subscribed(kokkosp_callbacks.kokkosp_begin_parallel_scan_callback, devID); + kokkos::fence_if_needed( + kokkosp_callbacks.kokkosp_begin_parallel_scan_callback, devID, + [=]() { return kokkos::config_needs_fences; }); kokkosp_callbacks.kokkosp_begin_parallel_scan_callback(name, devID, kID); *kID = devID; } @@ -89,15 +104,19 @@ extern "C" void kokkosp_begin_fence(const char *name, const uint32_t devID, } extern "C" void kokkosp_end_parallel_for(const uint64_t kID) { - kokkos::fence_if_subscribed(kokkosp_callbacks.kokkosp_end_parallel_for_callback, kID); + kokkos::fence_if_needed(kokkosp_callbacks.kokkosp_end_parallel_for_callback, + kID, [=]() { return kokkos::config_needs_fences; }); kokkosp_callbacks.kokkosp_end_parallel_for_callback(kID); } extern "C" void kokkosp_end_parallel_reduce(const uint64_t kID) { - kokkos::fence_if_subscribed(kokkosp_callbacks.kokkosp_end_parallel_reduce_callback, kID); + kokkos::fence_if_needed( + kokkosp_callbacks.kokkosp_end_parallel_reduce_callback, kID, + [=]() { return kokkos::config_needs_fences; }); kokkosp_callbacks.kokkosp_end_parallel_reduce_callback(kID); } extern "C" void kokkosp_end_parallel_scan(const uint64_t kID) { - kokkos::fence_if_subscribed(kokkosp_callbacks.kokkosp_end_parallel_scan_callback, kID); + kokkos::fence_if_needed(kokkosp_callbacks.kokkosp_end_parallel_scan_callback, + kID, [=]() { return kokkos::config_needs_fences; }); kokkosp_callbacks.kokkosp_end_parallel_scan_callback(kID); } extern "C" void kokkosp_end_fence(const uint64_t kID) { @@ -105,11 +124,13 @@ extern "C" void kokkosp_end_fence(const uint64_t kID) { } extern "C" void kokkosp_push_profile_region(char *regionName) { - kokkos::fence_if_subscribed(kokkosp_callbacks.kokkosp_push_region_callback, 0); + kokkos::fence_if_needed(kokkosp_callbacks.kokkosp_push_region_callback, 0, + [=]() { return true; }); kokkosp_callbacks.kokkosp_push_region_callback(regionName); } extern "C" void kokkosp_pop_profile_region() { - kokkos::fence_if_subscribed(kokkosp_callbacks.kokkosp_pop_region_callback, 0); + kokkos::fence_if_needed(kokkosp_callbacks.kokkosp_push_region_callback, 0, + [=]() { return true; }); kokkosp_callbacks.kokkosp_pop_region_callback(); } extern "C" void kokkosp_allocate_data(const SpaceHandle space,