Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DPCPP SpGEMM, SpGEAM, Transpose, Sort #799

Merged
merged 5 commits into from
Jul 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
211 changes: 211 additions & 0 deletions dpcpp/components/atomic.dp.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
/*******************************<GINKGO LICENSE>******************************
Copyright (c) 2017-2021, the Ginkgo authors
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************<GINKGO LICENSE>*******************************/

#ifndef GKO_DPCPP_COMPONENTS_ATOMIC_DP_HPP_
#define GKO_DPCPP_COMPONENTS_ATOMIC_DP_HPP_


#include <type_traits>


#include <CL/sycl.hpp>


#include "dpcpp/base/dpct.hpp"


namespace gko {
namespace kernels {
namespace dpcpp {
namespace atomic {


constexpr auto local_space = cl::sycl::access::address_space::local_space;
constexpr auto global_space = cl::sycl::access::address_space::global_space;


} // namespace atomic

namespace {


template <cl::sycl::access::address_space addressSpace = atomic::global_space,
thoasm marked this conversation as resolved.
Show resolved Hide resolved
typename T>
T atomic_compare_exchange_strong(
cl::sycl::multi_ptr<T, addressSpace> addr, T expected, T desired,
cl::sycl::memory_order success = cl::sycl::memory_order::relaxed,
cl::sycl::memory_order fail = cl::sycl::memory_order::relaxed)
{
cl::sycl::atomic<T, addressSpace> obj(addr);
obj.compare_exchange_strong(expected, desired, success, fail);
return expected;
}

template <cl::sycl::access::address_space addressSpace = atomic::global_space,
typename T>
T atomic_compare_exchange_strong(
T *addr, T expected, T desired,
cl::sycl::memory_order success = cl::sycl::memory_order::relaxed,
cl::sycl::memory_order fail = cl::sycl::memory_order::relaxed)
{
return atomic_compare_exchange_strong(
cl::sycl::multi_ptr<T, addressSpace>(addr), expected, desired, success,
fail);
}


template <cl::sycl::access::address_space addressSpace = atomic::global_space,
typename T>
inline T atomic_fetch_add(
T *addr, T operand,
cl::sycl::memory_order memoryOrder = cl::sycl::memory_order::relaxed)
{
cl::sycl::atomic<T, addressSpace> obj(
(cl::sycl::multi_ptr<T, addressSpace>(addr)));
return cl::sycl::atomic_fetch_add(obj, operand, memoryOrder);
}


} // namespace


namespace detail {


template <cl::sycl::access::address_space addressSpace, typename ValueType,
typename = void>
struct atomic_helper {
__dpct_inline__ static ValueType atomic_add(ValueType *, ValueType)
{
static_assert(sizeof(ValueType) == 0,
"This default function is not implemented, only the "
"specializations are.");
// TODO: add proper implementation of generic atomic add
}
};


template <typename ResultType, typename ValueType>
__dpct_inline__ ResultType reinterpret(ValueType val)
{
static_assert(sizeof(ValueType) == sizeof(ResultType),
"The type to reinterpret to must be of the same size as the "
"original type.");
return reinterpret_cast<ResultType &>(val);
}


#define GKO_BIND_ATOMIC_HELPER_STRUCTURE(CONVERTER_TYPE) \
template <cl::sycl::access::address_space addressSpace, \
typename ValueType> \
struct atomic_helper< \
addressSpace, ValueType, \
std::enable_if_t<(sizeof(ValueType) == sizeof(CONVERTER_TYPE))>> { \
__dpct_inline__ static ValueType atomic_add( \
ValueType *__restrict__ addr, ValueType val) \
Comment on lines +130 to +134
Copy link
Member

@tcojean tcojean Jun 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this? Since they have fetch_add and their stuff is templated everywhere, can't we use the base type directly?
https://intel.github.io/llvm-docs/doxygen/classcl_1_1sycl_1_1atomic.html

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IsValidAtomicType does not support complex and some of them do not support float.( __SYCL_STATIC_ASSERT_NOT_FLOAT)
I do not do it pretty well by template. the complex is done by 8 byte type but complex is two 8 byte impl.

{ \
CONVERTER_TYPE *address_as_converter = \
reinterpret_cast<CONVERTER_TYPE *>(addr); \
CONVERTER_TYPE old = *address_as_converter; \
CONVERTER_TYPE assumed; \
do { \
assumed = old; \
old = atomic_compare_exchange_strong<addressSpace>( \
address_as_converter, assumed, \
reinterpret<CONVERTER_TYPE>( \
val + reinterpret<ValueType>(assumed))); \
} while (assumed != old); \
return reinterpret<ValueType>(old); \
} \
};

// Support 64-bit ATOMIC_ADD
GKO_BIND_ATOMIC_HELPER_STRUCTURE(unsigned long long int);
// Support 32-bit ATOMIC_ADD
GKO_BIND_ATOMIC_HELPER_STRUCTURE(unsigned int);


#undef GKO_BIND_ATOMIC_HELPER_STRUCTURE

#define GKO_BIND_ATOMIC_HELPER_VALUETYPE(ValueType) \
template <cl::sycl::access::address_space addressSpace> \
struct atomic_helper<addressSpace, ValueType, std::enable_if_t<true>> { \
__dpct_inline__ static ValueType atomic_add( \
ValueType *__restrict__ addr, ValueType val) \
{ \
return atomic_fetch_add<addressSpace>(addr, val); \
} \
};

GKO_BIND_ATOMIC_HELPER_VALUETYPE(int);
GKO_BIND_ATOMIC_HELPER_VALUETYPE(unsigned int);
GKO_BIND_ATOMIC_HELPER_VALUETYPE(unsigned long long int);

#undef GKO_BIND_ATOMIC_HELPER_VALUETYPE


template <cl::sycl::access::address_space addressSpace, typename ValueType>
struct atomic_helper<
addressSpace, ValueType,
std::enable_if_t<is_complex<ValueType>() && sizeof(ValueType) >= 16>> {
__dpct_inline__ static ValueType atomic_add(ValueType *__restrict__ addr,
ValueType val)
{
using real_type = remove_complex<ValueType>;
real_type *real_addr = reinterpret_cast<real_type *>(addr);
// Separate to real part and imag part
auto real = atomic_helper<addressSpace, real_type>::atomic_add(
&real_addr[0], val.real());
auto imag = atomic_helper<addressSpace, real_type>::atomic_add(
&real_addr[1], val.imag());
return {real, imag};
}
};


} // namespace detail


template <cl::sycl::access::address_space addressSpace = atomic::global_space,
typename T>
__dpct_inline__ T atomic_add(T *__restrict__ addr, T val)
{
return detail::atomic_helper<addressSpace, T>::atomic_add(addr, val);
}


} // namespace dpcpp
} // namespace kernels
} // namespace gko


#endif // GKO_DPCPP_COMPONENTS_ATOMIC_DP_HPP_
13 changes: 12 additions & 1 deletion dpcpp/components/prefix_sum.dp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,18 @@ namespace components {

template <typename IndexType>
void prefix_sum(std::shared_ptr<const DefaultExecutor> exec, IndexType *counts,
size_type num_entries) GKO_NOT_IMPLEMENTED;
size_type num_entries)
{
// TODO actually implement parallel prefix sum
upsj marked this conversation as resolved.
Show resolved Hide resolved
exec->get_queue()->submit([&](sycl::handler &cgh) {
cgh.parallel_for(sycl::range<1>{1}, [=](sycl::id<1> idx) {
IndexType sum{};
for (size_type i = 0; i < num_entries; i++) {
sum += std::exchange(counts[i], sum);
}
});
});
}

GKO_INSTANTIATE_FOR_EACH_INDEX_TYPE(GKO_DECLARE_PREFIX_SUM_KERNEL);
// instantiate for size_type as well, as this is used in the Sellp format
Expand Down
Loading