Skip to content

Instantly share code, notes, and snippets.

@dsuess
Last active March 30, 2019 03:28
Show Gist options
  • Save dsuess/0bf6e2bcf612792b3bb047db804d48b8 to your computer and use it in GitHub Desktop.
Save dsuess/0bf6e2bcf612792b3bb047db804d48b8 to your computer and use it in GitHub Desktop.
#include <iostream>
#include "xtensor/xio.hpp"
#include "xtensor/xnpy.hpp"
#include "xtensor/xrandom.hpp"
#include "xtensor/xtensor.hpp"
#include "xtensor/xview.hpp"
using farray = xt::xtensor<float, 2, xt::layout_type::row_major>;
const int B = 4;
const int M = 4;
const int N = 4;
template <class E>
auto transform(E& x) {
auto x_reshaped = xt::reshape_view(x, {B, M, N});
x_reshaped = x_reshaped * 2;
}
int main() {
const farray x_orig = xt::random::randn<float>({B, 2 * M * N});
const size_t split = M * N;
using namespace xt::placeholders;
{
// Processing without copying
farray x_view = x_orig;
auto x1 = xt::view(x_view, xt::all(), xt::range(_, split));
transform(x1);
auto x2 = xt::view(x_view, xt::all(), xt::range(split, _));
transform(x2);
std::cout << "Same with views? " << xt::allclose(x_view, 2 * x_orig)
<< std::endl;
}
{
// Processing with copying
farray x_view = x_orig;
auto x1 = xt::view(x_view, xt::all(), xt::range(_, split));
{
farray tmp = x1;
transform(tmp);
x1 = tmp;
}
auto x2 = xt::view(x_view, xt::all(), xt::range(split, _));
{
farray tmp = x2;
transform(tmp);
x2 = tmp;
}
std::cout << "Same with copies? " << xt::allclose(x_view, 2 * x_orig)
<< std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment