123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508 |
- // Boost.uBLAS
- //
- // Copyright (c) 2018 Fady Essam
- // Copyright (c) 2018 Stefan Seefeld
- //
- // Distributed under the Boost Software License, Version 1.0.
- // (See accompanying file LICENSE_1_0.txt or
- // copy at http://www.boost.org/LICENSE_1_0.txt)
- #ifndef boost_numeric_ublas_opencl_elementwise_hpp_
- #define boost_numeric_ublas_opencl_elementwise_hpp_
- #include <boost/numeric/ublas/opencl/library.hpp>
- #include <boost/numeric/ublas/opencl/vector.hpp>
- #include <boost/numeric/ublas/opencl/matrix.hpp>
- namespace boost { namespace numeric { namespace ublas { namespace opencl {
- namespace compute = boost::compute;
- namespace lambda = boost::compute::lambda;
- template <typename T, typename L1, typename L2, typename L3, class O>
- void element_wise(ublas::matrix<T, L1, opencl::storage> const &a,
- ublas::matrix<T, L2, opencl::storage> const &b,
- ublas::matrix<T, L3, opencl::storage> &result,
- O op, compute::command_queue& queue)
- {
- assert(a.device() == b.device() &&
- a.device() == result.device() &&
- a.device() == queue.get_device());
- assert(a.size1() == b.size1() && a.size2() == b.size2());
- compute::transform(a.begin(),
- a.end(),
- b.begin(),
- result.begin(),
- op,
- queue);
- queue.finish();
- }
- template <typename T, typename L1, typename L2, typename L3, typename A, class O>
- void element_wise(ublas::matrix<T, L1, A> const &a,
- ublas::matrix<T, L2, A> const &b,
- ublas::matrix<T, L3, A> &result,
- O op,
- compute::command_queue &queue)
- {
- ublas::matrix<T, L1, opencl::storage> adev(a, queue);
- ublas::matrix<T, L2, opencl::storage> bdev(b, queue);
- ublas::matrix<T, L3, opencl::storage> rdev(a.size1(), b.size2(), queue.get_context());
- element_wise(adev, bdev, rdev, op, queue);
- rdev.to_host(result, queue);
- }
- template <typename T, typename L1, typename L2, typename A, typename O>
- ublas::matrix<T, L1, A> element_wise(ublas::matrix<T, L1, A> const &a,
- ublas::matrix<T, L2, A> const &b,
- O op,
- compute::command_queue &queue)
- {
- ublas::matrix<T, L1, A> result(a.size1(), b.size2());
- element_wise(a, b, result, op, queue);
- return result;
- }
- template <typename T, typename O>
- void element_wise(ublas::vector<T, opencl::storage> const &a,
- ublas::vector<T, opencl::storage> const &b,
- ublas::vector<T, opencl::storage> &result,
- O op,
- compute::command_queue& queue)
- {
- assert(a.device() == b.device() &&
- a.device() == result.device() &&
- a.device() == queue.get_device());
- assert(a.size() == b.size());
- compute::transform(a.begin(),
- a.end(),
- b.begin(),
- result.begin(),
- op,
- queue);
- queue.finish();
- }
- template <typename T, typename A, typename O>
- void element_wise(ublas::vector<T, A> const &a,
- ublas::vector<T, A> const &b,
- ublas::vector<T, A>& result,
- O op,
- compute::command_queue &queue)
- {
- ublas::vector<T, opencl::storage> adev(a, queue);
- ublas::vector<T, opencl::storage> bdev(b, queue);
- ublas::vector<T, opencl::storage> rdev(a.size(), queue.get_context());
- element_wise(adev, bdev, rdev, op, queue);
- rdev.to_host(result, queue);
- }
- template <typename T, typename A, typename O>
- ublas::vector<T, A> element_wise(ublas::vector<T, A> const &a,
- ublas::vector<T, A> const &b,
- O op,
- compute::command_queue &queue)
- {
- ublas::vector<T, A> result(a.size());
- element_wise(a, b, result, op, queue);
- return result;
- }
- template <typename T, typename L1, typename L2, typename L3>
- void element_add(ublas::matrix<T, L1, opencl::storage> const &a,
- ublas::matrix<T, L2, opencl::storage> const &b,
- ublas::matrix<T, L3, opencl::storage> &result,
- compute::command_queue &queue)
- {
- element_wise(a, b, result, compute::plus<T>(), queue);
- }
- template <typename T, typename L1, typename L2, typename L3, typename A>
- void element_add(ublas::matrix<T, L1, A> const &a,
- ublas::matrix<T, L2, A> const &b,
- ublas::matrix<T, L3, A> &result,
- compute::command_queue &queue)
- {
- element_wise(a, b, result, compute::plus<T>(), queue);
- }
- template <typename T, typename L1, typename L2, typename A>
- ublas::matrix<T, L1, A> element_add(ublas::matrix<T, L1, A> const &a,
- ublas::matrix<T, L2, A> const &b,
- compute::command_queue &queue)
- {
- return element_wise(a, b, compute::plus<T>(), queue);
- }
- template <typename T>
- void element_add(ublas::vector<T, opencl::storage> const &a,
- ublas::vector<T, opencl::storage> const &b,
- ublas::vector<T, opencl::storage> &result,
- compute::command_queue& queue)
- {
- element_wise(a, b, result, compute::plus<T>(), queue);
- }
- template <typename T, typename A>
- void element_add(ublas::vector<T, A> const &a,
- ublas::vector<T, A> const &b,
- ublas::vector<T, A> &result,
- compute::command_queue &queue)
- {
- element_wise(a, b, result, compute::plus<T>(), queue);
- }
- template <typename T, typename A>
- ublas::vector<T, A> element_add(ublas::vector<T, A> const &a,
- ublas::vector<T, A> const &b,
- compute::command_queue &queue)
- {
- return element_wise(a, b, compute::plus<T>(), queue);
- }
- template<typename T, typename L>
- void element_add(ublas::matrix<T, L, opencl::storage> const &m, T value,
- ublas::matrix<T, L, opencl::storage> &result,
- compute::command_queue& queue)
- {
- assert(m.device() == result.device() && m.device() == queue.get_device());
- assert(m.size1() == result.size1() && m.size2() == result.size2());
- compute::transform(m.begin(), m.end(), result.begin(), lambda::_1 + value, queue);
- queue.finish();
- }
- template<typename T, typename L, typename A>
- void element_add(ublas::matrix<T, L, A> const &m, T value,
- ublas::matrix<T, L, A> &result,
- compute::command_queue& queue)
- {
- ublas::matrix<T, L, opencl::storage> mdev(m, queue);
- ublas::matrix<T, L, opencl::storage> rdev(result.size1(), result.size2(), queue.get_context());
- element_add(mdev, value, rdev, queue);
- rdev.to_host(result, queue);
- }
- template<typename T, typename L, typename A>
- ublas::matrix<T, L, A> element_add(ublas::matrix<T, L, A> const &m, T value,
- compute::command_queue& queue)
- {
- ublas::matrix<T, L, A> result(m.size1(), m.size2());
- element_add(m, value, result, queue);
- return result;
- }
- template<typename T>
- void element_add(ublas::vector<T, opencl::storage> const &v, T value,
- ublas::vector<T, opencl::storage> &result,
- compute::command_queue& queue)
- {
- assert(v.device() == result.device() && v.device() == queue.get_device());
- assert(v.size() == result.size());
- compute::transform(v.begin(), v.end(), result.begin(), lambda::_1 + value, queue);
- queue.finish();
- }
- template<typename T, typename A>
- void element_add(ublas::vector<T, A> const &v, T value,
- ublas::vector<T, A> &result,
- compute::command_queue& queue)
- {
- ublas::vector<T, opencl::storage> vdev(v, queue);
- ublas::vector<T, opencl::storage> rdev(v.size(), queue.get_context());
- element_add(vdev, value, rdev, queue);
- rdev.to_host(result, queue);
- }
- template <typename T, typename A>
- ublas::vector<T, A> element_add(ublas::vector<T, A> const &v, T value,
- compute::command_queue& queue)
- {
- ublas::vector<T, A> result(v.size());
- element_add(v, value, result, queue);
- return result;
- }
- template <typename T, typename L1, typename L2, typename L3>
- void element_sub(ublas::matrix<T, L1, opencl::storage> const &a,
- ublas::matrix<T, L2, opencl::storage> const &b,
- ublas::matrix<T, L3, opencl::storage> &result,
- compute::command_queue& queue)
- {
- element_wise(a, b, compute::minus<T>(), result, queue);
- }
- template <typename T, typename L1, typename L2, typename L3, typename A>
- void element_sub(ublas::matrix<T, L1, A> const &a,
- ublas::matrix<T, L2, A> const &b,
- ublas::matrix<T, L3, A> &result,
- compute::command_queue &queue)
- {
- element_wise(a, b, result, compute::minus<T>(), queue);
- }
- template <typename T, typename L1, typename L2, typename A>
- ublas::matrix<T, L1, A> element_sub(ublas::matrix<T, L1, A> const &a,
- ublas::matrix<T, L2, A> const &b,
- compute::command_queue &queue)
- {
- return element_wise(a, b, compute::minus<T>(), queue);
- }
- template <typename T>
- void element_sub(ublas::vector<T, opencl::storage> const &a,
- ublas::vector<T, opencl::storage> const &b,
- ublas::vector<T, opencl::storage> &result,
- compute::command_queue& queue)
- {
- element_wise(a, b, result, compute::minus<T>(), queue);
- }
- template <typename T, typename A>
- void element_sub(ublas::vector<T, A> const &a,
- ublas::vector<T, A> const &b,
- ublas::vector<T, A> &result,
- compute::command_queue &queue)
- {
- element_wise(a, b, result, compute::minus<T>(), queue);
- }
- template <typename T, typename A>
- ublas::vector<T, A> element_sub(ublas::vector<T, A> const &a,
- ublas::vector<T, A> const &b,
- compute::command_queue &queue)
- {
- return element_wise(a, b, compute::minus<T>(), queue);
- }
- template <typename T, typename L>
- void element_sub(ublas::matrix<T, L, opencl::storage> const &m, T value,
- ublas::matrix<T, L, opencl::storage> &result,
- compute::command_queue& queue)
- {
- assert(m.device() == result.device() && m.device() == queue.get_device());
- assert(m.size1() == result.size1() && m.size2() == result.size2());
- compute::transform(m.begin(), m.end(), result.begin(), lambda::_1 - value, queue);
- queue.finish();
- }
- template <typename T, typename L, typename A>
- void element_sub(ublas::matrix<T, L, A> const &m, T value,
- ublas::matrix<T, L, A> &result,
- compute::command_queue& queue)
- {
- ublas::matrix<T, L, opencl::storage> mdev(m, queue);
- ublas::matrix<T, L, opencl::storage> rdev(result.size1(), result.size2(), queue.get_context());
- element_sub(mdev, value, rdev, queue);
- rdev.to_host(result, queue);
- }
- template <typename T, typename L, typename A>
- ublas::matrix<T, L, A> element_sub(ublas::matrix<T, L, A> const &m, T value,
- compute::command_queue& queue)
- {
- ublas::matrix<T, L, A> result(m.size1(), m.size2());
- element_sub(m, value, result, queue);
- return result;
- }
- template <typename T>
- void element_sub(ublas::vector<T, opencl::storage> const &v, T value,
- ublas::vector<T, opencl::storage> &result,
- compute::command_queue& queue)
- {
- assert(v.device() == result.device() && v.device() == queue.get_device());
- assert(v.size() == result.size());
- compute::transform(v.begin(), v.end(), result.begin(), lambda::_1 - value, queue);
- queue.finish();
- }
- template <typename T, typename A>
- void element_sub(ublas::vector<T, A> const &v, T value,
- ublas::vector<T, A> &result,
- compute::command_queue& queue)
- {
- ublas::vector<T, opencl::storage> vdev(v, queue);
- ublas::vector<T, opencl::storage> rdev(v.size(), queue.get_context());
- element_sub(vdev, value, rdev, queue);
- rdev.to_host(result, queue);
- }
- template <typename T, typename A>
- ublas::vector<T, A> element_sub(ublas::vector<T, A> const &v, T value,
- compute::command_queue& queue)
- {
- ublas::vector<T, A> result(v.size());
- element_sub(v, value, result, queue);
- return result;
- }
- template <typename T, typename L1, typename L2, typename L3>
- void element_prod(ublas::matrix<T, L1, opencl::storage> const &a,
- ublas::matrix<T, L2, opencl::storage> const &b,
- ublas::matrix<T, L3, opencl::storage> &result,
- compute::command_queue& queue)
- {
- element_wise(a, b, result, compute::multiplies<T>(), queue);
- }
- template <typename T, typename L1, typename L2, typename L3, typename A>
- void element_prod(ublas::matrix<T, L1, A> const &a,
- ublas::matrix<T, L2, A> const &b,
- ublas::matrix<T, L3, A> &result,
- compute::command_queue &queue)
- {
- element_wise(a, b, result, compute::multiplies<T>(), queue);
- }
- template <typename T, typename L1, typename L2, typename A>
- ublas::matrix<T, L1, A> element_prod(ublas::matrix<T, L1, A> const &a,
- ublas::matrix<T, L2, A> const &b,
- compute::command_queue &queue)
- {
- return element_wise(a, b, compute::multiplies<T>(), queue);
- }
- template <typename T>
- void element_prod(ublas::vector<T, opencl::storage> const &a,
- ublas::vector<T, opencl::storage> const &b,
- ublas::vector<T, opencl::storage> &result,
- compute::command_queue& queue)
- {
- element_wise(a, b, result, compute::multiplies<T>(), queue);
- }
- template <typename T, typename A>
- void element_prod(ublas::vector<T, A> const &a,
- ublas::vector<T, A> const &b,
- ublas::vector<T, A> &result,
- compute::command_queue &queue)
- {
- element_wise(a, b, result, compute::multiplies<T>(), queue);
- }
- template <typename T, typename A>
- ublas::vector<T, A> element_prod(ublas::vector<T, A> const &a,
- ublas::vector<T, A> const &b,
- compute::command_queue &queue)
- {
- return element_wise(a, b, compute::multiplies<T>(), queue);
- }
- template <typename T, typename L>
- void element_scale(ublas::matrix<T, L, opencl::storage> const &m, T value,
- ublas::matrix<T, L, opencl::storage> &result,
- compute::command_queue& queue)
- {
- assert(m.device() == result.device() && m.device() == queue.get_device());
- assert(m.size1() == result.size1() && m.size2() == result.size2());
- compute::transform(m.begin(), m.end(), result.begin(), lambda::_1 * value, queue);
- queue.finish();
- }
- template <typename T, typename L, typename A>
- void element_scale(ublas::matrix<T, L, A> const &m, T value,
- ublas::matrix<T, L, A> &result,
- compute::command_queue& queue)
- {
- ublas::matrix<T, L, opencl::storage> mdev(m, queue);
- ublas::matrix<T, L, opencl::storage> rdev(result.size1(), result.size2(), queue.get_context());
- element_scale(mdev, value, rdev, queue);
- rdev.to_host(result, queue);
- }
- template <typename T, typename L, typename A>
- ublas::matrix<T, L, A> element_scale(ublas::matrix<T, L, A> const &m, T value,
- compute::command_queue& queue)
- {
- ublas::matrix<T, L, A> result(m.size1(), m.size2());
- element_scale(m, value, result, queue);
- return result;
- }
- template <typename T>
- void element_scale(ublas::vector<T, opencl::storage> const &v, T value,
- ublas::vector<T, opencl::storage> &result,
- compute::command_queue& queue)
- {
- assert(v.device() == result.device() && v.device() == queue.get_device());
- assert(v.size() == result.size());
- compute::transform(v.begin(), v.end(), result.begin(), lambda::_1 * value, queue);
- queue.finish();
- }
- template <typename T, typename A>
- void element_scale(ublas::vector<T, A> const &v, T value,
- ublas::vector<T, A> & result,
- compute::command_queue& queue)
- {
- ublas::vector<T, opencl::storage> vdev(v, queue);
- ublas::vector<T, opencl::storage> rdev(v.size(), queue.get_context());
- element_scale(vdev, value, rdev, queue);
- rdev.to_host(result, queue);
- }
- template <typename T, typename A>
- ublas::vector<T,A> element_scale(ublas::vector<T, A> const &v, T value,
- compute::command_queue& queue)
- {
- ublas::vector<T, A> result(v.size());
- element_scale(v, value, result, queue);
- return result;
- }
- template <typename T, typename L1, typename L2, typename L3>
- void element_div(ublas::matrix<T, L1, opencl::storage> const &a,
- ublas::matrix<T, L2, opencl::storage> const &b,
- ublas::matrix<T, L3, opencl::storage> &result,
- compute::command_queue& queue)
- {
- element_wise(a, b, result, compute::divides<T>(), queue);
- }
- template <typename T, typename L1, typename L2, typename L3, typename A>
- void element_div(ublas::matrix<T, L1, A> const &a,
- ublas::matrix<T, L2, A> const &b,
- ublas::matrix<T, L3, A> &result,
- compute::command_queue &queue)
- {
- element_wise(a, b, result, compute::divides<T>(), queue);
- }
- template <typename T, typename L1, typename L2, typename A>
- ublas::matrix<T, L1, A> element_div(ublas::matrix<T, L1, A> const &a,
- ublas::matrix<T, L2, A> const &b,
- compute::command_queue &queue)
- {
- return element_wise(a, b, compute::divides<T>(), queue);
- }
- template <typename T>
- void element_div(ublas::vector<T, opencl::storage> const &a,
- ublas::vector<T, opencl::storage> const &b,
- ublas::vector<T, opencl::storage> &result,
- compute::command_queue& queue)
- {
- element_wise(a, b, result, compute::divides<T>(), queue);
- }
- template <typename T, typename A>
- void element_div(ublas::vector<T, A> const &a,
- ublas::vector<T, A> const &b,
- ublas::vector<T, A> &result,
- compute::command_queue &queue)
- {
- element_wise(a, b, result, compute::divides<T>(), queue);
- }
- template <typename T, typename A>
- ublas::vector<T, A> element_div(ublas::vector<T, A> const &a,
- ublas::vector<T, A> const &b,
- compute::command_queue &queue)
- {
- return element_wise(a, b, compute::divides<T>(), queue);
- }
- }}}}
- #endif
|