Skip to content

Commit

Permalink
Add iterators to device_vector
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Schaetz committed Jun 30, 2016
1 parent 6d8b51e commit 70b1d3f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
44 changes: 39 additions & 5 deletions include/boost/aura/device_array.hpp
Expand Up @@ -83,21 +83,55 @@ class device_array
return *this;
}

/// Access bounds and size
BoundsType bounds() const
{
return bounds_;
}

std::size_t size() const
{
return product(bounds_);
}

/// Begin
iterator begin()
{
return *(data_.get());
}
const_iterator begin() const
{
return *(data_.get());
}

/// End
iterator end()
{
return *(data_.get()) + product(bounds_);
}
const_iterator end() const
{
return *(data_.get()) + product(bounds_);
}

private:
/// Deleter type
typedef detail::device_array_deleter<T> deleter_t;

/// Data type
typedef std::unique_ptr<device_ptr<T>, deleter_t> data_t;

/// Allocation helper function
data_t allocate(std::size_t size, device& d)
{
auto ptr = new device_ptr<T>;
*ptr = device_malloc<T>(size, d);
return data_t(ptr, deleter_t());
data_ = data_t(new device_ptr<T>(device_malloc<T>(size, d)),
deleter_t());
}

/// Stores the bounds.
/// Stores the bounds
BoundsType bounds_;

/// Holds data.
/// Holds data
data_t data_;
};

Expand Down
21 changes: 21 additions & 0 deletions test/device_array.cpp
Expand Up @@ -20,7 +20,10 @@ BOOST_AUTO_TEST_CASE(basic)
device d(AURA_UNIT_TEST_DEVICE);
device_array<float> ar0;
device_array<float> ar1(1024, d);
BOOST_CHECK(ar1.size() == 1024);
device_array<float> ar2(bounds({2, 2, 2, 2}), d);
BOOST_CHECK(ar2.size() == 2 * 2 * 2 * 2);
BOOST_CHECK(ar2.bounds() == bounds({2, 2, 2, 2}));
}
finalize();
}
Expand All @@ -36,3 +39,21 @@ BOOST_AUTO_TEST_CASE(move)
}
finalize();
}

BOOST_AUTO_TEST_CASE(iterators)
{
initialize();
{
device d(AURA_UNIT_TEST_DEVICE);
device_array<float> ar0(bounds({2, 2, 2, 2}), d);
auto begin = ar0.begin();
auto end = ar0.end();
BOOST_CHECK(begin != end);
BOOST_CHECK(begin + product(bounds({2, 2, 2, 2})) == end);
BOOST_CHECK(ar0.begin() != ar0.end());
auto end2 = ar0.begin() + product(bounds({2, 2, 2, 2}));
BOOST_CHECK(ar0.begin() + product(bounds({2, 2, 2, 2})) ==
ar0.end());
}
finalize();
}
9 changes: 9 additions & 0 deletions test/tiny_vector.cpp
Expand Up @@ -28,6 +28,15 @@ BOOST_AUTO_TEST_CASE(test_product)
BOOST_CHECK((std::size_t)(b1) == 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9);
}

BOOST_AUTO_TEST_CASE(test_product_alternative)
{
using bounds = std::vector<std::size_t>;
bounds b0;
BOOST_CHECK(product(b0) == 0);
bounds b1({1, 2, 3, 4, 5, 6, 7, 8, 9});
BOOST_CHECK(product(b1) == 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9);
}

BOOST_AUTO_TEST_CASE(test_other)
{
using bounds = tiny_vector<std::size_t, 10>;
Expand Down

0 comments on commit 70b1d3f

Please sign in to comment.