Skip to content

Commit

Permalink
hostEnv -> host::Env + fixup to make f294db3 build
Browse files Browse the repository at this point in the history
  • Loading branch information
evaleev committed Sep 23, 2024
1 parent f613831 commit 6472326
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 43 deletions.
7 changes: 2 additions & 5 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ TiledArray/expressions/index_list.h
TiledArray/external/btas.h
TiledArray/external/madness.h
TiledArray/external/umpire.h
TiledArray/host/env.cpp
TiledArray/host/env.h
TiledArray/math/blas.h
TiledArray/math/gemm_helper.h
Expand Down Expand Up @@ -206,11 +207,7 @@ TiledArray/util/vector.h
if(HIP_FOUND OR CUDA_FOUND)
list(APPEND TILEDARRAY_HEADER_FILES
TiledArray/external/device.h
TiledArray/external/librett.h)
endif()

if(CUDA_FOUND OR HIP_FOUND)
list(APPEND TILEDARRAY_HEADER_FILES
TiledArray/external/librett.h
TiledArray/device/blas.cpp
TiledArray/device/blas.h
TiledArray/device/btas.h
Expand Down
40 changes: 26 additions & 14 deletions src/TiledArray/external/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@
#include <thrust/system_error.h>
#endif

#include <TiledArray/external/umpire.h>

#include <TiledArray/external/madness.h>
#include <madness/world/print.h>
#include <madness/world/safempi.h>
Expand All @@ -51,6 +49,20 @@
#include <TiledArray/error.h>
#include <TiledArray/initialize.h>

#include <TiledArray/external/umpire.h>

namespace TiledArray::detail {

struct get_um_allocator {
inline umpire::Allocator& operator()();
};

struct get_pinned_allocator {
inline umpire::Allocator& operator()();
};

} // namespace TiledArray::detail

#if defined(TILEDARRAY_HAS_CUDA)

inline void __DeviceSafeCall(cudaError err, const char* file, const int line) {
Expand Down Expand Up @@ -802,18 +814,6 @@ class Env {

namespace detail {

struct get_um_allocator {
umpire::Allocator& operator()() {
return deviceEnv::instance()->um_allocator();
}
};

struct get_pinned_allocator {
umpire::Allocator& operator()() {
return deviceEnv::instance()->pinned_allocator();
}
};

// in a madness device task point to its local optional stream to use by
// madness_task_stream_opt; set to nullptr after task callable finished
inline std::optional<Stream>*& madness_task_stream_opt_ptr_accessor() {
Expand Down Expand Up @@ -905,6 +905,18 @@ device::Stream stream_for(const Range& range) {

} // namespace device

namespace detail {

inline umpire::Allocator& get_um_allocator::operator()() {
return deviceEnv::instance()->um_allocator();
}

inline umpire::Allocator& get_pinned_allocator::operator()() {
return deviceEnv::instance()->pinned_allocator();
}

} // namespace detail

#endif // TILEDARRAY_HAS_DEVICE

#ifdef TILEDARRAY_HAS_CUDA
Expand Down
5 changes: 4 additions & 1 deletion src/TiledArray/fwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ class umpire_based_allocator;
template <typename T, typename A = std::allocator<T>>
class default_init_allocator;

class hostEnv;
namespace host {
class Env;
}
using hostEnv = host::Env;

/// pooled thread-safe host memory allocator
template <typename T>
Expand Down
36 changes: 36 additions & 0 deletions src/TiledArray/host/env.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* This file is a part of TiledArray.
* Copyright (C) 2021 Virginia Tech
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Chong Peng
* Department of Chemistry, Virginia Tech
* July 23, 2018
*
*/

#include <TiledArray/host/env.h>

namespace TiledArray {

namespace detail {

umpire::Allocator& get_host_allocator::operator()() {
return TiledArray::host::Env::instance()->host_allocator();
}

} // namespace detail

} // namespace TiledArray
47 changes: 24 additions & 23 deletions src/TiledArray/host/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,34 @@

namespace TiledArray {

namespace detail {

struct get_host_allocator {
umpire::Allocator& operator()();
};

} // namespace detail

namespace host {

/**
* hostEnv maintains the (host-side, as opposed to device-side) environment,
* Env maintains the (host-side, as opposed to device-side) environment,
* such as memory allocators
*
* \note this is a Singleton
*/
class hostEnv {
class Env {
public:
~hostEnv() = default;
~Env() = default;

hostEnv(const hostEnv&) = delete;
hostEnv(hostEnv&&) = delete;
hostEnv& operator=(const hostEnv&) = delete;
hostEnv& operator=(hostEnv&&) = delete;
Env(const Env&) = delete;
Env(Env&&) = delete;
Env& operator=(const Env&) = delete;
Env& operator=(Env&&) = delete;

/// access the singleton instance; if not initialized will be
/// initialized via hostEnv::initialize() with the default params
static std::unique_ptr<hostEnv>& instance() {
/// initialized via Env::initialize() with the default params
static std::unique_ptr<Env>& instance() {
if (!instance_accessor()) {
initialize();
}
Expand Down Expand Up @@ -103,8 +113,7 @@ class hostEnv {
"QuickPool_SizeLimited_HOST", host_size_limited_alloc, page_size,
page_size, /* alignment */ TILEDARRAY_ALIGN_SIZE);

auto host_env =
std::unique_ptr<hostEnv>(new hostEnv(world, host_dynamic_pool));
auto host_env = std::unique_ptr<Env>(new Env(world, host_dynamic_pool));
instance_accessor() = std::move(host_env);
}
}
Expand All @@ -131,7 +140,7 @@ class hostEnv {
}

protected:
hostEnv(World& world, umpire::Allocator host_alloc)
Env(World& world, umpire::Allocator host_alloc)
: world_(&world), host_allocator_(host_alloc) {}

private:
Expand All @@ -142,21 +151,13 @@ class hostEnv {
// N.B. not thread safe, so must be wrapped into umpire_based_allocator_impl
umpire::Allocator host_allocator_;

inline static std::unique_ptr<hostEnv>& instance_accessor() {
static std::unique_ptr<hostEnv> instance_{nullptr};
inline static std::unique_ptr<Env>& instance_accessor() {
static std::unique_ptr<Env> instance_{nullptr};
return instance_;
}
};

namespace detail {

struct get_host_allocator {
umpire::Allocator& operator()() {
return hostEnv::instance()->host_allocator();
}
};

} // namespace detail
} // namespace host

} // namespace TiledArray

Expand Down

0 comments on commit 6472326

Please sign in to comment.