Padding with border reflect 101 #2732
Unanswered
guillaume-michel
asked this question in
Q&A
Replies: 1 comment
-
|
I managed to go a bit further: #include <cute/tensor.hpp>
#include <cute/layout_composed.hpp>
#include <cute/util/print_tensor.hpp>
#include <numeric>
#include <vector>
#include <spdlog/spdlog.h>
using namespace cute;
namespace {
// 1-D mirror-101 mapper acting on LINEAR indices
template <int Dim, int Halo>
struct Reflect101Index1D {
static_assert(Dim >= 2, "Reflect101 requires Dim >= 2");
// Optional: size of codomain (fold target)
CUTE_HOST_DEVICE
constexpr auto cosize() const { return Int<Dim>{}; }
// Map extended index [0..Dim+2*Halo) -> [0..Dim)
CUTE_HOST_DEVICE
constexpr int operator()(int idx_ext) const {
int j = idx_ext - Halo; // shift: [-Halo, Dim+Halo-1]
int period = 2 * (Dim - 1); // triangular-wave period
int m = j % period;
if (m < 0) {
m += period;
}
return (m <= Dim - 1)
? m
: (period - m); // 101 reflection (no duplicated endpoints)
}
};
template <int Dim, int Halo>
CUTE_HOST_DEVICE auto makeReflect101Layout1d() {
auto ext_id =
make_layout(Int<Dim + 2 * Halo>{}); // 1-D identity (domain provider)
return make_composed_layout(Reflect101Index1D<Dim, Halo>{}, Int<0>{},
ext_id);
}
// for ADL
template <int Dim, int Halo>
void print(const Reflect101Index1D<Dim, Halo>& l) {
cute::print("Reflect101Index1D<%d, %d>", Dim, Halo);
}
} // namespace
int main() {
constexpr auto halo = Int<2>{};
constexpr auto h = Int<4>{};
constexpr auto w = Int<4>{};
// Base data: 4x4 row-major
constexpr auto n = h * w;
std::vector<int> data(n);
std::iota(data.begin(), data.end(), 0);
auto t1 = cute::make_tensor(data.data(), cute::make_shape(h, w),
cute::LayoutRight{});
print("t1:\n");
print_tensor(t1);
print("\n");
auto ry = makeReflect101Layout1d<h, halo>();
spdlog::info("rank ry: {}", rank(ry));
spdlog::info("shape: {}", shape(ry));
spdlog::info("size: {}", size(ry));
auto t = make_tensor(data.data(), ry);
for (int i = 0; i < size<0>(t); ++i) {
print(t(i));
print(" ");
}
print("\n");
print("t:\n");
print_tensor(t);
print("\n");
return 0;
}It prints the following: which is the expected result. Now I want to go 2D. How to compose my 1D ComposedLayout for get a 2D layout? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I am trying to do the following with CuTe:
What I have so far:
Could you please help me?
Beta Was this translation helpful? Give feedback.
All reactions