Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Put in Deep copy for sampler #229

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions common/kokkos-sampler/kp_sampler_skip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
#include "../../profiling/all/kp_core.hpp"
#include "kp_config.hpp"

// using Kokkos::Tools::SpaceHandle;
struct SpaceHandle {
char name[64];
};

namespace KokkosTools {
namespace Sampler {
static uint64_t uniqID = 0;
Expand All @@ -21,15 +26,25 @@ typedef void (*initFunction)(const int, const uint64_t, const uint32_t, void*);
typedef void (*finalizeFunction)();
typedef void (*beginFunction)(const char*, const uint32_t, uint64_t*);
typedef void (*endFunction)(uint64_t);
typedef void (*beginDeepCopyFunction)(SpaceHandle,
const char*,
const void*,
SpaceHandle,
const char*,
const void*,
uint64_t);
typedef void (*endDeepCopyFunction)();

static initFunction initProfileLibrary = NULL;
static finalizeFunction finalizeProfileLibrary = NULL;
static beginFunction beginForCallee = NULL;
static beginFunction beginScanCallee = NULL;
static beginFunction beginReduceCallee = NULL;
static beginDeepCopyFunction beginDeepCopyCallee = NULL;
static endFunction endForCallee = NULL;
static endFunction endScanCallee = NULL;
static endFunction endReduceCallee = NULL;
static endDeepCopyFunction endDeepCopyCallee = NULL;

void kokkosp_request_tool_settings(const uint32_t,
Kokkos_Tools_ToolSettings* settings) {
Expand Down Expand Up @@ -134,13 +149,17 @@ void kokkosp_init_library(const int loadSeq, const uint64_t interfaceVer,
(beginFunction)dlsym(childLibrary, "kokkosp_begin_parallel_scan");
beginReduceCallee =
(beginFunction)dlsym(childLibrary, "kokkosp_begin_parallel_reduce");
beginDeepCopyCallee =
(beginDeepCopyFunction)dlsym(childLibrary, "kokkosp_begin_deep_copy");

endScanCallee =
(endFunction)dlsym(childLibrary, "kokkosp_end_parallel_scan");
endForCallee =
(endFunction)dlsym(childLibrary, "kokkosp_end_parallel_for");
endReduceCallee =
(endFunction)dlsym(childLibrary, "kokkosp_end_parallel_reduce");
endDeepCopyCallee =
(endDeepCopyFunction)dlsym(childLibrary, "kokkosp_end_deep_copy");

initProfileLibrary =
(initFunction)dlsym(childLibrary, "kokkosp_init_library");
Expand All @@ -160,12 +179,16 @@ void kokkosp_init_library(const int loadSeq, const uint64_t interfaceVer,
(beginScanCallee == NULL) ? "no" : "yes");
printf("KokkosP: begin-parallel-reduce: %s\n",
(beginReduceCallee == NULL) ? "no" : "yes");
printf("KokkosP: begin-deep-copy: %s\n",
(beginDeepCopyCallee == NULL) ? "no" : "yes");
printf("KokkosP: end-parallel-for: %s\n",
(endForCallee == NULL) ? "no" : "yes");
printf("KokkosP: end-parallel-scan: %s\n",
(endScanCallee == NULL) ? "no" : "yes");
printf("KokkosP: end-parallel-reduce: %s\n",
(endReduceCallee == NULL) ? "no" : "yes");
printf("KokkosP: end-deep-copy: %s\n",
(endDeepCopyCallee == NULL) ? "no" : "yes");
}
}
}
Expand Down Expand Up @@ -302,6 +325,29 @@ void kokkosp_end_parallel_reduce(const uint64_t kID) {
}
}

void kokkosp_begin_deep_copy(SpaceHandle dst_handle,
const char* dst_name,
const void* dst_ptr,
SpaceHandle src_handle,
const char* src_name,
const void* src_ptr, uint64_t size) {
static uint64_t invocationNum = 0;
++invocationNum;
if ((invocationNum % kernelSampleSkip) == 0) {
if (tool_verbosity > 0) {
printf("KokkosP: sample calling child-begin deep_copy function...\n");
}
if (NULL != beginDeepCopyCallee) {
if (tool_globFence) {
invoke_ktools_fence(0);
}
(*beginDeepCopyCallee)(dst_handle.name, dst_name, dst_ptr, src_handle.name, src_name, src_ptr,
size);
}
}
}


} // namespace Sampler
} // end namespace KokkosTools

Expand All @@ -319,5 +365,7 @@ EXPOSE_BEGIN_PARALLEL_SCAN(impl::kokkosp_begin_parallel_scan)
EXPOSE_END_PARALLEL_SCAN(impl::kokkosp_end_parallel_scan)
EXPOSE_BEGIN_PARALLEL_REDUCE(impl::kokkosp_begin_parallel_reduce)
EXPOSE_END_PARALLEL_REDUCE(impl::kokkosp_end_parallel_reduce)
EXPOSE_BEGIN_DEEP_COPY(impl::kokkosp_begin_deep_copy)


} // end extern "C"
36 changes: 33 additions & 3 deletions example/kernels.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ int run_calculation(const data_type SIZE) {
Kokkos::View<data_type*> data(Kokkos::ViewAllocateWithoutInitializing("data"),
SIZE);
Kokkos::parallel_for(
"initialize()", SIZE, KOKKOS_LAMBDA(data_type i) { data(i) = i; });
"initialize()", SIZE, KOKKOS_LAMBDA(data_type i) { host_data(i) = i; });
Kokkos::fence();

data_type sum = 0;
Kokkos::parallel_reduce(
"accumulate()", SIZE,
KOKKOS_LAMBDA(data_type i, data_type & lsum) { lsum += 1 + data(i); },
sum);
Kokkos::fence();

Kokkos::Profiling::popRegion();

// check results
Expand All @@ -34,3 +32,35 @@ int run_calculation(const data_type SIZE) {
}

//-------------------------------------------------------------------------------------//

template <typename data_type>
int run_calculation_with_deepcopy(const data_type SIZE) {
Kokkos::Profiling::pushRegion("Computation-DpCpy");

Kokkos::View<data_type*> data(Kokkos::ViewAllocateWithoutInitializing("data"),
SIZE);
Kokkos::View<data_type>::HostMirror host_data = create_mirror_view(data);

Kokkos::parallel_for(
"initialize()", SIZE, KOKKOS_LAMBDA(data_type i) { host_data(i) = i; });
Kokkos::fence();
Kokkos::deep_copy(data, host_data);
data_type sum = 0;
Kokkos::parallel_reduce(
"accumulate()", SIZE,
KOKKOS_LAMBDA(data_type i, data_type & lsum) { lsum += 1 + data(i); },
sum);
Kokkos::fence();
Kokkos::deep_copy(host_data, data);
Kokkos::Profiling::popRegion();

// check results
const data_type check = (SIZE + 1) * SIZE / 2;
if (sum != check) {
std::cout << "BAD result, got S(" << SIZE << ") = " << sum << " (expected "
<< check << ")" << std::endl;
return 1;
}
std::cout << "Result OK: S(" << SIZE << ") = " << sum << std::endl;
return 0;
}
4 changes: 4 additions & 0 deletions example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ int main(int argc, char *argv[]) {
int ret_code = run_calculation(100000);
std::cout << std::endl;

std::cout << std::endl;
int ret_code = run_calculation_with_deepcopy(100000);
std::cout << std::endl;

Kokkos::finalize();
#if USE_MPI
MPI_Finalize();
Expand Down
Loading