Skip to content

Commit

Permalink
Refactor axis spaces into separate TUs
Browse files Browse the repository at this point in the history
  • Loading branch information
robertmaynard committed Apr 13, 2022
1 parent 40a6711 commit e7b4800
Show file tree
Hide file tree
Showing 10 changed files with 320 additions and 117 deletions.
3 changes: 3 additions & 0 deletions nvbench/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ set(srcs
device_manager.cu
float64_axis.cxx
int64_axis.cxx
linear_axis_space.cxx
markdown_printer.cu
named_values.cxx
option_parser.cu
Expand All @@ -21,6 +22,8 @@ set(srcs
string_axis.cxx
type_axis.cxx
type_strings.cxx
user_axis_space.cxx
zip_axis_space.cxx

detail/measure_cold.cu
detail/measure_hot.cu
Expand Down
3 changes: 3 additions & 0 deletions nvbench/axes_metadata.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
#include <nvbench/axis_iteration_space.cuh>
#include <nvbench/float64_axis.cuh>
#include <nvbench/int64_axis.cuh>
#include <nvbench/linear_axis_space.cuh>
#include <nvbench/string_axis.cuh>
#include <nvbench/type_axis.cuh>
#include <nvbench/types.cuh>
#include <nvbench/user_axis_space.cuh>
#include <nvbench/zip_axis_space.cuh>

#include <functional>
#include <memory>
Expand Down
84 changes: 45 additions & 39 deletions nvbench/axis_iteration_space.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@
namespace nvbench
{

/*!
* Base class for all axi and axes iteration spaces.
*
* If we consider an axi to be a container of values, iteration_spaces
* would be the different types of iterators supported by that container.
*
* With that in mind we get the following mapping:
* * linear_axis_space is equivalant to a forward iterator.
*
* * zip_axis_space is equivalant to a zip iterator.
*
* * user_axis_space is equivalant to a transform iterator.
*
*
*/
struct iteration_space_base
{
using axes_type = std::vector<std::unique_ptr<nvbench::axis_base>>;
Expand All @@ -32,62 +47,53 @@ struct iteration_space_base
nvbench::detail::axis_space_iterator::AdvanceSignature;
using UpdateSignature = nvbench::detail::axis_space_iterator::UpdateSignature;

/*!
* Construct a new iteration_space_base
*
* @param[input_indices]
* @param[output_indices]
*/
iteration_space_base(std::vector<std::size_t> input_indices,
std::vector<std::size_t> output_indices);
std::vector<std::size_t> output_indices);
virtual ~iteration_space_base();

[[nodiscard]] std::unique_ptr<iteration_space_base> clone() const;
[[nodiscard]] std::vector<std::unique_ptr<iteration_space_base>>
clone_as_linear() const;

[[nodiscard]] detail::axis_space_iterator get_iterator(const axes_type &axes) const;
/*!
* Construct a new iteration_space_base
*
*/
[[nodiscard]] detail::axis_space_iterator
get_iterator(const axes_type &axes) const;

/*!
* Construct a new iteration_space_base
*
*/
[[nodiscard]] std::size_t get_size(const axes_type &axes) const;

/*!
* Construct a new iteration_space_base
*
*/
[[nodiscard]] std::size_t get_active_count(const axes_type &axes) const;

/*!
* Construct a new iteration_space_base
*
*/
[[nodiscard]] bool contains(std::size_t input_index) const;

protected:
std::vector<std::size_t> m_input_indices;
std::vector<std::size_t> m_output_indices;

virtual std::unique_ptr<iteration_space_base> do_clone() const = 0;
virtual std::unique_ptr<iteration_space_base> do_clone() const = 0;
virtual detail::axis_space_iterator do_get_iterator(axes_info info) const = 0;
virtual std::size_t do_get_size(const axes_info &info) const = 0;
virtual std::size_t do_get_active_count(const axes_info &info) const = 0;
};

struct linear_axis_space final : iteration_space_base
{
linear_axis_space(std::size_t in, std::size_t out);
~linear_axis_space();

std::unique_ptr<iteration_space_base> do_clone() const override;
detail::axis_space_iterator do_get_iterator(axes_info info) const override;
std::size_t do_get_size(const axes_info &info) const override;
std::size_t do_get_active_count(const axes_info &info) const override;
};

struct zip_axis_space final : iteration_space_base
{
zip_axis_space(std::vector<std::size_t> input_indices,
std::vector<std::size_t> output_indices);
~zip_axis_space();

std::unique_ptr<iteration_space_base> do_clone() const override;
detail::axis_space_iterator do_get_iterator(axes_info info) const override;
std::size_t do_get_size(const axes_info &info) const override;
std::size_t do_get_active_count(const axes_info &info) const override;
virtual std::size_t do_get_size(const axes_info &info) const = 0;
virtual std::size_t do_get_active_count(const axes_info &info) const = 0;
};

struct user_axis_space : iteration_space_base
{
user_axis_space(std::vector<std::size_t> input_indices,
std::vector<std::size_t> output_indices);
~user_axis_space();
};

using make_user_space_signature =
std::unique_ptr<iteration_space_base>(std::vector<std::size_t> input_indices,
std::vector<std::size_t> output_indices);

} // namespace nvbench
79 changes: 1 addition & 78 deletions nvbench/axis_iteration_space.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "axis_iteration_space.cuh"

#include <nvbench/type_axis.cuh>
#include <nvbench/linear_axis_space.cuh>

namespace nvbench
{
Expand Down Expand Up @@ -93,82 +94,4 @@ bool iteration_space_base::contains(std::size_t in_index) const
return iter != m_input_indices.end();
}

linear_axis_space::linear_axis_space(std::size_t in_index,
std::size_t out_index)
: iteration_space_base({in_index}, {out_index})
{}

linear_axis_space::~linear_axis_space() = default;

detail::axis_space_iterator linear_axis_space::do_get_iterator(axes_info info) const
{
std::size_t loc(m_output_indices[0]);
auto update_func = [=](std::size_t inc_index,
std::vector<detail::axis_index> &indices) {
indices[loc] = info[0];
indices[loc].index = inc_index;
};

return detail::make_space_iterator(1, info[0].size, update_func);
}

std::size_t linear_axis_space::do_get_size(const axes_info &info) const
{
return info[0].size;
}

std::size_t linear_axis_space::do_get_active_count(const axes_info &info) const
{
return info[0].active_size;
}

std::unique_ptr<iteration_space_base> linear_axis_space::do_clone() const
{
return std::make_unique<linear_axis_space>(*this);
}

zip_axis_space::zip_axis_space(std::vector<std::size_t> input_indices,
std::vector<std::size_t> output_indices)
: iteration_space_base(std::move(input_indices), std::move(output_indices))
{}

zip_axis_space::~zip_axis_space() = default;

detail::axis_space_iterator zip_axis_space::do_get_iterator(axes_info info) const
{
std::vector<std::size_t> locs = m_output_indices;
auto update_func = [=](std::size_t inc_index,
std::vector<detail::axis_index> &indices) {
for (std::size_t i = 0; i < info.size(); ++i)
{
detail::axis_index temp = info[i];
temp.index = inc_index;
indices[locs[i]] = temp;
}
};

return detail::make_space_iterator(locs.size(), info[0].size, update_func);
}

std::size_t zip_axis_space::do_get_size(const axes_info &info) const
{
return info[0].size;
}

std::size_t zip_axis_space::do_get_active_count(const axes_info &info) const
{
return info[0].active_size;
}

std::unique_ptr<iteration_space_base> zip_axis_space::do_clone() const
{
return std::make_unique<zip_axis_space>(*this);
}

user_axis_space::user_axis_space(std::vector<std::size_t> input_indices,
std::vector<std::size_t> output_indices)
: iteration_space_base(std::move(input_indices), std::move(output_indices))
{}
user_axis_space::~user_axis_space() = default;

} // namespace nvbench
37 changes: 37 additions & 0 deletions nvbench/linear_axis_space.cuh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2022 NVIDIA Corporation
*
* Licensed under the Apache License, Version 2.0 with the LLVM exception
* (the "License"); you may not use this file except in compliance with
* the License.
*
* You may obtain a copy of the License at
*
* http://llvm.org/foundation/relicensing/LICENSE.txt
*
* 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 <nvbench/axis_iteration_space.cuh>

namespace nvbench
{

struct linear_axis_space final : iteration_space_base
{
linear_axis_space(std::size_t in, std::size_t out);
~linear_axis_space();

std::unique_ptr<iteration_space_base> do_clone() const override;
detail::axis_space_iterator do_get_iterator(axes_info info) const override;
std::size_t do_get_size(const axes_info &info) const override;
std::size_t do_get_active_count(const axes_info &info) const override;
};

} // namespace nvbench
60 changes: 60 additions & 0 deletions nvbench/linear_axis_space.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2022 NVIDIA Corporation
*
* Licensed under the Apache License, Version 2.0 with the LLVM exception
* (the "License"); you may not use this file except in compliance with
* the License.
*
* You may obtain a copy of the License at
*
* http://llvm.org/foundation/relicensing/LICENSE.txt
*
* 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.
*/

#include "linear_axis_space.cuh"

#include <nvbench/type_axis.cuh>

namespace nvbench
{

linear_axis_space::linear_axis_space(std::size_t in_index,
std::size_t out_index)
: iteration_space_base({in_index}, {out_index})
{}

linear_axis_space::~linear_axis_space() = default;

detail::axis_space_iterator linear_axis_space::do_get_iterator(axes_info info) const
{
std::size_t loc(m_output_indices[0]);
auto update_func = [=](std::size_t inc_index,
std::vector<detail::axis_index> &indices) {
indices[loc] = info[0];
indices[loc].index = inc_index;
};

return detail::make_space_iterator(1, info[0].size, update_func);
}

std::size_t linear_axis_space::do_get_size(const axes_info &info) const
{
return info[0].size;
}

std::size_t linear_axis_space::do_get_active_count(const axes_info &info) const
{
return info[0].active_size;
}

std::unique_ptr<iteration_space_base> linear_axis_space::do_clone() const
{
return std::make_unique<linear_axis_space>(*this);
}

} // namespace nvbench
37 changes: 37 additions & 0 deletions nvbench/user_axis_space.cuh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2022 NVIDIA Corporation
*
* Licensed under the Apache License, Version 2.0 with the LLVM exception
* (the "License"); you may not use this file except in compliance with
* the License.
*
* You may obtain a copy of the License at
*
* http://llvm.org/foundation/relicensing/LICENSE.txt
*
* 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 <nvbench/axis_iteration_space.cuh>

namespace nvbench
{

struct user_axis_space : iteration_space_base
{
user_axis_space(std::vector<std::size_t> input_indices,
std::vector<std::size_t> output_indices);
~user_axis_space();
};

using make_user_space_signature =
std::unique_ptr<iteration_space_base>(std::vector<std::size_t> input_indices,
std::vector<std::size_t> output_indices);

} // namespace nvbench
Loading

0 comments on commit e7b4800

Please sign in to comment.