Skip to content
Merged
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
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ nwx_add_pybind11_module(
if("${BUILD_TESTING}")
set(cxx_test_dir "${CMAKE_CURRENT_LIST_DIR}/tests/cxx")
set(cxx_unit_test_dir "${cxx_test_dir}/unit_tests/${PROJECT_NAME}")
set(
cxx_acceptance_test_dir
"${cxx_test_dir}/acceptance_tests/${PROJECT_NAME}"
)
set(python_test_dir "${CMAKE_CURRENT_LIST_DIR}/tests/python")
set(python_unit_test_dir "${python_test_dir}/unit_tests")

Expand All @@ -96,6 +100,14 @@ if("${BUILD_TESTING}")
DEPENDS Catch2 ${PROJECT_NAME}
)

cmaize_add_tests(
acceptance_test_${PROJECT_NAME}
SOURCE_DIR "${cxx_acceptance_test_dir}"
INCLUDE_DIRS "${project_src_dir}"
DEPENDS Catch2 ${PROJECT_NAME}
)


nwx_add_pybind11_module(
py_test_${PROJECT_NAME}
INSTALL OFF
Expand Down
25 changes: 20 additions & 5 deletions include/tensorwrapper/allocator/allocator_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

#pragma once
#include <parallelzone/parallelzone.hpp>
#include <tensorwrapper/buffer/buffer_base.hpp>
#include <tensorwrapper/detail_/dsl_base.hpp>
#include <tensorwrapper/detail_/polymorphic_base.hpp>
#include <tensorwrapper/layout/physical.hpp>
#include <tensorwrapper/types/buffer_traits.hpp>

namespace tensorwrapper::allocator {

Expand Down Expand Up @@ -49,19 +50,28 @@ class AllocatorBase : public detail_::PolymorphicBase<AllocatorBase> {
using layout_type = layout::Physical;

/// Type of a pointer to an object of type layout_type
using layout_pointer = typename layout_type::layout_pointer;
using layout_pointer = std::unique_ptr<layout_type>;

/// Type of a read-only reference to the layout
using const_layout_reference = const layout_type&;

/// Type all buffers derive from
using buffer_base_type = buffer::BufferBase;

/// Type of the class defining types for the buffer_base_type class
using buffer_base_traits = types::ClassTraits<buffer_base_type>;

/// Type of a mutable reference to an object of type buffer_base_type
using buffer_base_reference = buffer_base_type&;
using buffer_base_reference =
typename buffer_base_traits::buffer_base_reference;

/// Type of a read-only reference to an object of type buffer_base_type
using const_buffer_base_reference = const buffer_base_type&;
using const_buffer_base_reference =
typename buffer_base_traits::const_buffer_base_reference;

/// Type of a pointer to an object of type buffer_base_type
using buffer_base_pointer = typename buffer_base_type::buffer_base_pointer;
using buffer_base_pointer =
typename buffer_base_traits::buffer_base_pointer;

// -------------------------------------------------------------------------
// -- Ctors and assignment
Expand All @@ -85,6 +95,10 @@ class AllocatorBase : public detail_::PolymorphicBase<AllocatorBase> {
return allocate_(std::move(playout));
}

buffer_base_pointer allocate(const_layout_reference layout) {
return allocate(layout.clone_as<layout_type>());
}

/** @brief The runtime *this uses for allocating.
*
* Allocators are tied to runtimes. This method can be used to retrieve
Expand Down Expand Up @@ -151,6 +165,7 @@ class AllocatorBase : public detail_::PolymorphicBase<AllocatorBase> {
* @throw None No throw guarantee.
*/
explicit AllocatorBase(runtime_view_type rv) : m_rv_(std::move(rv)) {}

/** @brief Creates *this so that it uses the same runtime as @p other.
*
* @param[in] other The allocator to make a copy of.
Expand Down
33 changes: 33 additions & 0 deletions include/tensorwrapper/allocator/allocator_fwd.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2025 NWChemEx-Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

namespace tensorwrapper::allocator {

class AllocatorBase;

template<typename FloatType>
class Eigen;

class Local;

class Replicated;

template<typename FloatType>
class Contiguous;

} // namespace tensorwrapper::allocator
123 changes: 123 additions & 0 deletions include/tensorwrapper/allocator/contiguous.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright 2025 NWChemEx-Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once
#include <tensorwrapper/allocator/replicated.hpp>
#include <tensorwrapper/types/il_traits.hpp>

namespace tensorwrapper::allocator {

/** @brief Allocator that can create Contiguous buffers.
*
* @tparam FloatType Type of the elements in the contiguous buffer.
*/
template<typename FloatType>
class Contiguous : public Replicated {
private:
/// Type of *this
using my_type = Contiguous<FloatType>;

/// Type *this derives from
using base_type = Replicated;

public:
/// Pull in base types
///@{
using base_type::buffer_base_pointer;
using base_type::const_layout_reference;
using base_type::layout_pointer;
///@}

/// Type of each element in the tensor
using element_type = FloatType;

/// Type of the buffer associated with *this
using contiguous_buffer_type = buffer::Contiguous<element_type>;
using contiguous_pointer = std::unique_ptr<contiguous_buffer_type>;

/// Type of initializer lists
using rank0_il = typename types::ILTraits<element_type, 0>::type;
using rank1_il = typename types::ILTraits<element_type, 1>::type;
using rank2_il = typename types::ILTraits<element_type, 2>::type;
using rank3_il = typename types::ILTraits<element_type, 3>::type;
using rank4_il = typename types::ILTraits<element_type, 4>::type;

/// Pull in base class's ctors
using base_type::base_type;

/** @brief Allocates a contiguous pointer given @p layout.
*
* @note These methods shadow the function of the same name in the base
* class. The intent is to avoid needing to rebind a freshly
* allocated buffer when the user already knows it is a Contiguous
* buffer.
*
* @param[in] layout The layout of the tensor to allocate. May be passed as
* a unique_ptr or by reference. If passed by reference
* will be copied.
*
* @return A pointer to the newly allocated buffer::Contiguous object.
*/
///@{
contiguous_pointer allocate(const_layout_reference layout) {
return allocate(layout.clone_as<layout_type>());
}
contiguous_pointer allocate(layout_pointer layout) {
auto p = allocate_(std::move(layout));
return detail_::static_pointer_cast<contiguous_buffer_type>(p);
}
///@}

/// Constructs a contiguous buffer from an initializer list
///@{
contiguous_pointer construct(rank0_il il) { return construct_(il); }
contiguous_pointer construct(rank1_il il) { return construct_(il); }
contiguous_pointer construct(rank2_il il) { return construct_(il); }
contiguous_pointer construct(rank3_il il) { return construct_(il); }
contiguous_pointer construct(rank4_il il) { return construct_(il); }
///@}

/** @brief Constructs a contiguous buffer and sets all elements to @p value.
*
* @param[in] layout The layout of the buffer to allocate. May be passed
* either by unique_ptr or reference. If passed by
* reference will be copied.
*
* @return A pointer to the newly constructed buffer.
*/
///@{
contiguous_pointer construct(const_layout_reference layout,
element_type value) {
return construct(layout.clone_as<layout_type>(), std::move(value));
}
contiguous_pointer construct(layout_pointer layout, element_type value) {
return construct_(std::move(layout), std::move(value));
}
///@}

protected:
virtual contiguous_pointer construct_(rank0_il il) = 0;
virtual contiguous_pointer construct_(rank1_il il) = 0;
virtual contiguous_pointer construct_(rank2_il il) = 0;
virtual contiguous_pointer construct_(rank3_il il) = 0;
virtual contiguous_pointer construct_(rank4_il il) = 0;

/// To be overridden by the derived class to implement construct
virtual contiguous_pointer construct_(layout_pointer layout,
element_type value) = 0;
};

} // namespace tensorwrapper::allocator
Loading