Skip to content

Commit

Permalink
Added minimal example
Browse files Browse the repository at this point in the history
  • Loading branch information
G-071 committed Mar 30, 2020
1 parent 08cf411 commit b8bdb01
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
build
external_dependencies/build
external_dependencies/install
.spack-env
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,9 @@ add_executable(
${CMAKE_CURRENT_SOURCE_DIR}/src/allocator_executor_stream_test.cpp)
target_link_libraries(allocator_executor_stream_test
PRIVATE HPX::hpx Kokkos::kokkos HPXKokkos::hpx_kokkos buffer_manager)

add_executable(
minimal_parallel_for
${CMAKE_CURRENT_SOURCE_DIR}/src/minimal_parallel_for.cpp)
target_link_libraries(minimal_parallel_for
PRIVATE HPX::hpx Kokkos::kokkos HPXKokkos::hpx_kokkos buffer_manager)
2 changes: 2 additions & 0 deletions include/buffer_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ class buffer_recycler {
assert(std::get<1>(tuple) == number_of_elements);
assert(std::get<2>(tuple) >= 1);
std::get<2>(tuple)--; // decrease usage counter
std::cout << "Usage counter is now " << std::get<2>(tuple) << std::endl;
if (std::get<2>(tuple) == 0) { // not used anymore?
// move to the unused_buffer list
instance->unused_buffer_list.push_front(tuple);
Expand All @@ -174,6 +175,7 @@ class buffer_recycler {
assert(std::get<1>(tuple) == number_of_elements);
assert(std::get<2>(tuple) >= 1);
std::get<2>(tuple)++; // increase usage counter
std::cout << "Usage counter is now " << std::get<2>(tuple) << std::endl;
}

private:
Expand Down
9 changes: 8 additions & 1 deletion include/kokkos_buffer_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,24 @@ class recycled_view : public kokkos_type
total_elements(kokkos_type::required_allocation_size(args...) / sizeof(element_type))
{
//std::cout << "Got buffer for " << total_elements << std::endl;
std::cout << "Creating view" << std::endl;
}

recycled_view(const recycled_view<kokkos_type, alloc_type, element_type> &other) : kokkos_type(other)
{
total_elements = other.total_elements;
// std::cerr << "copy" << std::endl;
allocator.increase_usage_counter(other.data(), other.total_elements);
std::cout << "Copying view" << std::endl;

}

recycled_view<kokkos_type, alloc_type, element_type> &operator=(const recycled_view<kokkos_type, alloc_type, element_type> &other)
{
kokkos_type::operator=(other);
total_elements = other.total_elements;
allocator.increase_usage_counter(other.data(), other.total_elements);
std::cout << "Copying view" << std::endl;
return *this;
}

Expand All @@ -38,6 +42,7 @@ class recycled_view : public kokkos_type
total_elements = other.total_elements;
// so that is doesn't matter if deallocate is called in the moved-from object
allocator.increase_usage_counter(other.data(), other.total_elements);
std::cout << "Moving view" << std::endl;
}

recycled_view<kokkos_type, alloc_type, element_type> &operator=(recycled_view<kokkos_type, alloc_type, element_type> &&other)
Expand All @@ -46,11 +51,13 @@ class recycled_view : public kokkos_type
total_elements = other.total_elements;
// so that is doesn't matter if deallocate is called in the moved-from object
allocator.increase_usage_counter(other.data(), other.total_elements);
std::cout << "Moving view" << std::endl;
return *this;
}

~recycled_view(void)
virtual ~recycled_view(void)
{
std::cout << "Dellocating view" << std::endl;
allocator.deallocate(this->data(), total_elements);
}

Expand Down
2 changes: 1 addition & 1 deletion scripts/build_kokkos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ INSTALL_DIR=${SCRIPTS_DIR}/../external_dependencies/install/kokkos-${CMAKE_BUILD

mkdir -p ${BUILD_DIR}
pushd ${BUILD_DIR}
cmake -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} -DHPX_DIR=${HPX_ROOT} -DKokkos_CXX_STANDARD=14 -DKokkos_ARCH_PASCAL61=ON ${CURRENT_CUDA_ARCH_FLAG} -DKokkos_ARCH_HSW=ON -DKokkos_ENABLE_HPX=ON -DKokkos_ENABLE_CUDA=ON -DKokkos_ENABLE_SERIAL=OFF -DKokkos_ENABLE_TESTS=OFF -DKokkos_ENABLE_HPX_ASYNC_DISPATCH=ON -DKokkos_ENABLE_INTERNAL_FENCES=OFF -DKokkos_ENABLE_CUDA_LAMBDA=ON -DCMAKE_INSTALL_PREFIX=${INSTALL_DIR} ${SOURCE_DIR}
cmake -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} -DHPX_DIR=${HPX_ROOT} -DKokkos_CXX_STANDARD=14 -DKokkos_ARCH_PASCAL61=ON ${CURRENT_CUDA_ARCH_FLAG} -DKokkos_ARCH_HSW=ON -DKokkos_ENABLE_HPX=ON -DKokkos_ENABLE_CUDA=ON -DKokkos_ENABLE_SERIAL=ON -DKokkos_ENABLE_TESTS=OFF -DKokkos_ENABLE_HPX_ASYNC_DISPATCH=ON -DKokkos_ENABLE_INTERNAL_FENCES=OFF -DKokkos_ENABLE_CUDA_LAMBDA=ON -DCMAKE_INSTALL_PREFIX=${INSTALL_DIR} ${SOURCE_DIR}
make -j$(nproc) install
popd
51 changes: 51 additions & 0 deletions src/minimal_parallel_for.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

#include <hpx/hpx_main.hpp>
#include <hpx/include/async.hpp>
#include <hpx/include/lcos.hpp>

#include <hpx/kokkos.hpp>

#include <Kokkos_Core.hpp>
#include <cstdio>
#include <typeinfo>

#include "../include/buffer_manager.hpp"
#include "../include/cuda_buffer_util.hpp"
#include "../include/kokkos_buffer_util.hpp"
#include <hpx/timing/high_resolution_timer.hpp>
#include <memory>

constexpr size_t view_size_0 = 10;
constexpr size_t view_size_1 = 50;
template <class T>
using kokkos_um_array = Kokkos::View<T**, Kokkos::HostSpace, Kokkos::MemoryUnmanaged>;
template <class T>
using recycled_host_view = recycled_view<kokkos_um_array<T>, recycle_std<T>, T>;


template <typename Executor, typename ViewType>
auto get_iteration_policy(const Executor&& executor, const ViewType& view_to_iterate){
return get_iteration_policy(executor, view_to_iterate);
}

int main(int argc, char *argv[])
{
constexpr size_t passes = 1;
for (size_t pass = 0; pass < passes; pass++)
{
recycled_host_view<double> hostView(view_size_0,view_size_1);

// works - usage counter goes up to 9 for the hostView
// auto host_space = hpx::kokkos::make_execution_space<Kokkos::Serial>();

// broken - usage counter goes up to 13 for the hostView - goes only down to 2
auto host_space = hpx::kokkos::make_execution_space<Kokkos::DefaultHostExecutionSpace>();
auto policy_host = get_iteration_policy(host_space, hostView);
Kokkos::parallel_for(
"host init",
policy_host,
KOKKOS_LAMBDA(int n, int o) {
hostView(n, o) = 1.0;
});
}
}

0 comments on commit b8bdb01

Please sign in to comment.