|
| 1 | +/**********************************************************************************/ |
| 2 | +/* MIT License */ |
| 3 | +/* */ |
| 4 | +/* Copyright (c) 2020, 2021 JetBrains-Research */ |
| 5 | +/* */ |
| 6 | +/* Permission is hereby granted, free of charge, to any person obtaining a copy */ |
| 7 | +/* of this software and associated documentation files (the "Software"), to deal */ |
| 8 | +/* in the Software without restriction, including without limitation the rights */ |
| 9 | +/* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */ |
| 10 | +/* copies of the Software, and to permit persons to whom the Software is */ |
| 11 | +/* furnished to do so, subject to the following conditions: */ |
| 12 | +/* */ |
| 13 | +/* The above copyright notice and this permission notice shall be included in all */ |
| 14 | +/* copies or substantial portions of the Software. */ |
| 15 | +/* */ |
| 16 | +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */ |
| 17 | +/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */ |
| 18 | +/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */ |
| 19 | +/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */ |
| 20 | +/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */ |
| 21 | +/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ |
| 22 | +/* SOFTWARE. */ |
| 23 | +/**********************************************************************************/ |
| 24 | + |
| 25 | +#ifndef CUBOOL_SPEWISEADD_HPP |
| 26 | +#define CUBOOL_SPEWISEADD_HPP |
| 27 | + |
| 28 | +#include <cuda/details/sp_vector.hpp> |
| 29 | + |
| 30 | +namespace cubool { |
| 31 | + namespace kernels { |
| 32 | + |
| 33 | + template <typename IndexType, typename AllocType> |
| 34 | + struct SpVectorEWiseAdd { |
| 35 | + template<typename T> |
| 36 | + using ContainerType = thrust::device_vector<T, typename AllocType::template rebind<T>::other>; |
| 37 | + using VectorType = details::SpVector<IndexType, AllocType>; |
| 38 | + |
| 39 | + VectorType operator()(const VectorType& a, const VectorType& b) { |
| 40 | + auto aNvals = a.m_vals; |
| 41 | + auto bNvals = b.m_vals; |
| 42 | + auto worst = aNvals + bNvals; |
| 43 | + |
| 44 | + // Allocate memory for the worst case scenario |
| 45 | + if (mCacheBuffer.size() < worst) |
| 46 | + mCacheBuffer.resize(aNvals + bNvals); |
| 47 | + |
| 48 | + // Merge sorted arrays without duplicates |
| 49 | + auto out = thrust::set_union(a.m_rows_index.begin(), a.m_rows_index.end(), |
| 50 | + b.m_rows_index.begin(), b.m_rows_index.end(), |
| 51 | + mCacheBuffer.begin()); |
| 52 | + |
| 53 | + // Count result nvals count |
| 54 | + auto nvals = thrust::distance(mCacheBuffer.begin(), out); |
| 55 | + |
| 56 | + // Fill the result buffer |
| 57 | + ContainerType<index> rowIndex(nvals); |
| 58 | + thrust::copy(mCacheBuffer.begin(), out, rowIndex.begin()); |
| 59 | + |
| 60 | + return VectorType(std::move(rowIndex), a.m_rows, nvals); |
| 61 | + } |
| 62 | + |
| 63 | + private: |
| 64 | + ContainerType<index> mCacheBuffer; |
| 65 | + }; |
| 66 | + |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +#endif //CUBOOL_SPEWISEADD_HPP |
0 commit comments