From e9962d5b153a686d18c83e29dd8c00c69c686514 Mon Sep 17 00:00:00 2001 From: Anton Smirnov Date: Sat, 28 Nov 2026 19:22:55 +0300 Subject: [PATCH 1/2] fix empty csr read --- src/opencl/cl_format_csr.hpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/opencl/cl_format_csr.hpp b/src/opencl/cl_format_csr.hpp index ec6e105bb..aa87943ec 100644 --- a/src/opencl/cl_format_csr.hpp +++ b/src/opencl/cl_format_csr.hpp @@ -91,10 +91,17 @@ namespace spla { const std::size_t buffer_size_Ax = n_values * sizeof(T); cl::Buffer staging_Ap(get_acc_cl()->get_context(), staging_flags, buffer_size_Ap); + + queue.enqueueCopyBuffer(storage.Ap, staging_Ap, 0, 0, buffer_size_Ap); + + if (n_values == 0) { + queue.enqueueReadBuffer(staging_Ap, blocking, 0, buffer_size_Ap, Ap); + return; + } + cl::Buffer staging_Aj(get_acc_cl()->get_context(), staging_flags, buffer_size_Aj); cl::Buffer staging_Ax(get_acc_cl()->get_context(), staging_flags, buffer_size_Ax); - queue.enqueueCopyBuffer(storage.Ap, staging_Ap, 0, 0, buffer_size_Ap); queue.enqueueCopyBuffer(storage.Aj, staging_Aj, 0, 0, buffer_size_Aj); queue.enqueueCopyBuffer(storage.Ax, staging_Ax, 0, 0, buffer_size_Ax); From 8b541d681df84a60a456501fab7d49f47533dce7 Mon Sep 17 00:00:00 2001 From: Anton Smirnov Date: Sat, 28 Nov 2026 19:22:55 +0300 Subject: [PATCH 2/2] add opencl matrix eadd emult --- src/opencl/cl_algo_registry.cpp | 12 + src/opencl/cl_m_eadd.hpp | 179 ++++++++++ src/opencl/cl_m_emult.hpp | 179 ++++++++++ src/opencl/generated/auto_m_eadd.hpp | 454 ++++++++++++++++++++++++++ src/opencl/generated/auto_m_emult.hpp | 348 ++++++++++++++++++++ src/opencl/kernels/m_eadd.cl | 413 +++++++++++++++++++++++ src/opencl/kernels/m_emult.cl | 307 +++++++++++++++++ tests/CMakeLists.txt | 1 + tests/test_opencl_eadd_emult.cpp | 260 +++++++++++++++ 9 files changed, 2153 insertions(+) create mode 100644 src/opencl/cl_m_eadd.hpp create mode 100644 src/opencl/cl_m_emult.hpp create mode 100644 src/opencl/generated/auto_m_eadd.hpp create mode 100644 src/opencl/generated/auto_m_emult.hpp create mode 100644 src/opencl/kernels/m_eadd.cl create mode 100644 src/opencl/kernels/m_emult.cl create mode 100644 tests/test_opencl_eadd_emult.cpp diff --git a/src/opencl/cl_algo_registry.cpp b/src/opencl/cl_algo_registry.cpp index 65726579b..ad5f44edd 100644 --- a/src/opencl/cl_algo_registry.cpp +++ b/src/opencl/cl_algo_registry.cpp @@ -30,6 +30,8 @@ #include #include +#include +#include #include #include #include @@ -79,6 +81,16 @@ namespace spla { g_registry->add(MAKE_KEY_CL_0("m_reduce", UINT), std::make_shared>()); g_registry->add(MAKE_KEY_CL_0("m_reduce", FLOAT), std::make_shared>()); + // algorithm m_eadd + g_registry->add(MAKE_KEY_CL_0("m_eadd", INT), std::make_shared>()); + g_registry->add(MAKE_KEY_CL_0("m_eadd", UINT), std::make_shared>()); + g_registry->add(MAKE_KEY_CL_0("m_eadd", FLOAT), std::make_shared>()); + + // algorithm m_emult + g_registry->add(MAKE_KEY_CL_0("m_emult", INT), std::make_shared>()); + g_registry->add(MAKE_KEY_CL_0("m_emult", UINT), std::make_shared>()); + g_registry->add(MAKE_KEY_CL_0("m_emult", FLOAT), std::make_shared>()); + // algorthm mxv_masked g_registry->add(MAKE_KEY_CL_0("mxv_masked", INT), std::make_shared>()); g_registry->add(MAKE_KEY_CL_0("mxv_masked", UINT), std::make_shared>()); diff --git a/src/opencl/cl_m_eadd.hpp b/src/opencl/cl_m_eadd.hpp new file mode 100644 index 000000000..bc7576516 --- /dev/null +++ b/src/opencl/cl_m_eadd.hpp @@ -0,0 +1,179 @@ +/**********************************************************************************/ +/* This file is part of spla project */ +/* https://github.com/SparseLinearAlgebra/spla */ +/**********************************************************************************/ +/* MIT License */ +/* */ +/* Copyright (c) 2023 SparseLinearAlgebra */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining a copy */ +/* of this software and associated documentation files (the "Software"), to deal */ +/* in the Software without restriction, including without limitation the rights */ +/* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */ +/* copies of the Software, and to permit persons to whom the Software is */ +/* furnished to do so, subject to the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be included in all */ +/* copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */ +/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */ +/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */ +/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */ +/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */ +/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ +/* SOFTWARE. */ +/**********************************************************************************/ + +#ifndef SPLA_CL_M_EADD_HPP +#define SPLA_CL_M_EADD_HPP + +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace spla { + + static constexpr uint k_m_eadd_group_size = 32; + + template + class Algo_m_eadd_cl final : public RegistryAlgo { + public: + ~Algo_m_eadd_cl() override = default; + + std::string get_name() override { + return "m_eadd"; + } + + std::string get_description() override { + return "parallel element-wise add matrix operation on opencl device"; + } + + Status execute(const DispatchContext& ctx) override { + TIME_PROFILE_SCOPE("opencl/m_eadd"); + + auto t = ctx.task.template cast_safe(); + if (!t) { + LOG_MSG(Status::InvalidArgument, "invalid task type for cl m_eadd"); + return Status::InvalidArgument; + } + + ref_ptr> R = t->R.template cast_safe>(); + ref_ptr> A = t->A.template cast_safe>(); + ref_ptr> B = t->B.template cast_safe>(); + ref_ptr> op = t->op.template cast_safe>(); + + if (!R || !A || !B) { + LOG_MSG(Status::InvalidArgument, "null matrix argument for cl m_eadd"); + return Status::InvalidArgument; + } + if (!op) { + LOG_MSG(Status::Error, "failed to cast binary operation"); + return Status::Error; + } + + R->validate_wd(FormatMatrix::AccCsr); + A->validate_rw(FormatMatrix::AccCsr); + B->validate_rw(FormatMatrix::AccCsr); + + auto* p_R = R->template get>(); + const auto* p_A = A->template get>(); + const auto* p_B = B->template get>(); + + const uint n_rows = R->get_n_rows(); + const T fill = R->get_fill_value(); + + auto* p_cl_acc = get_acc_cl(); + auto& queue = p_cl_acc->get_queue_default(); + auto* tmp_alloc = p_cl_acc->get_alloc_tmp(); + + std::shared_ptr program; + if (!ensure_kernel(op, program)) { + return Status::CompilationError; + } + + cl::Buffer cl_counts = tmp_alloc->alloc(sizeof(uint) * (n_rows + 1)); + + const uint avg = static_cast((p_A->values + p_B->values) / (2 * n_rows)); + const bool use_group = avg >= k_m_eadd_group_size; + + const uint wgs = p_cl_acc->get_default_wgs(); + const uint global_size = use_group ? n_rows * k_m_eadd_group_size : align(n_rows, wgs); + const uint local_size = use_group ? k_m_eadd_group_size : wgs; + const cl::NDRange global(global_size); + const cl::NDRange local(local_size); + + auto kernel_count = program->make_kernel(use_group ? "m_eadd_count_group" : "m_eadd_count"); + kernel_count.setArg(0, p_A->Ap); + kernel_count.setArg(1, p_A->Aj); + kernel_count.setArg(2, p_A->Ax); + kernel_count.setArg(3, p_B->Ap); + kernel_count.setArg(4, p_B->Aj); + kernel_count.setArg(5, p_B->Ax); + kernel_count.setArg(6, cl_counts); + kernel_count.setArg(7, n_rows); + kernel_count.setArg(8, fill); + queue.enqueueNDRangeKernel(kernel_count, cl::NDRange(), global, local); + + cl_exclusive_scan(queue, cl_counts, n_rows + 1, + PLUS_UINT.template cast_safe>(), tmp_alloc); + + CLCounterWrapper cl_total; + queue.enqueueCopyBuffer(cl_counts, cl_total.buffer(), sizeof(uint) * n_rows, 0, sizeof(uint)); + const uint total = cl_total.get(queue); + + cl_csr_resize(n_rows, total == 0 ? 1 : total, *p_R); + p_R->values = total; + queue.enqueueCopyBuffer(cl_counts, p_R->Ap, 0, 0, sizeof(uint) * (n_rows + 1)); + + auto kernel_scatter = program->make_kernel(use_group ? "m_eadd_scatter_group" : "m_eadd_scatter"); + kernel_scatter.setArg(0, p_A->Ap); + kernel_scatter.setArg(1, p_A->Aj); + kernel_scatter.setArg(2, p_A->Ax); + kernel_scatter.setArg(3, p_B->Ap); + kernel_scatter.setArg(4, p_B->Aj); + kernel_scatter.setArg(5, p_B->Ax); + kernel_scatter.setArg(6, p_R->Ap); + kernel_scatter.setArg(7, p_R->Aj); + kernel_scatter.setArg(8, p_R->Ax); + kernel_scatter.setArg(9, n_rows); + kernel_scatter.setArg(10, fill); + queue.enqueueNDRangeKernel(kernel_scatter, cl::NDRange(), global, local); + + tmp_alloc->free_all(); + + return Status::Ok; + } + + private: + bool ensure_kernel(const ref_ptr>& op, std::shared_ptr& program) { + CLProgramBuilder builder; + builder.set_name("m_eadd") + .add_type("TYPE", get_ttype().template as()) + .add_define("GROUP_SIZE", k_m_eadd_group_size) + .add_op("OP_BINARY", op.template as()) + .set_source(source_m_eadd) + .acquire(); + + program = builder.get_program(); + return true; + } + }; + +}// namespace spla + +#endif//SPLA_CL_M_EADD_HPP diff --git a/src/opencl/cl_m_emult.hpp b/src/opencl/cl_m_emult.hpp new file mode 100644 index 000000000..9327e50a9 --- /dev/null +++ b/src/opencl/cl_m_emult.hpp @@ -0,0 +1,179 @@ +/**********************************************************************************/ +/* This file is part of spla project */ +/* https://github.com/SparseLinearAlgebra/spla */ +/**********************************************************************************/ +/* MIT License */ +/* */ +/* Copyright (c) 2023 SparseLinearAlgebra */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining a copy */ +/* of this software and associated documentation files (the "Software"), to deal */ +/* in the Software without restriction, including without limitation the rights */ +/* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */ +/* copies of the Software, and to permit persons to whom the Software is */ +/* furnished to do so, subject to the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be included in all */ +/* copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */ +/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */ +/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */ +/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */ +/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */ +/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ +/* SOFTWARE. */ +/**********************************************************************************/ + +#ifndef SPLA_CL_M_EMULT_HPP +#define SPLA_CL_M_EMULT_HPP + +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace spla { + + static constexpr uint k_m_emult_group_size = 32; + + template + class Algo_m_emult_cl final : public RegistryAlgo { + public: + ~Algo_m_emult_cl() override = default; + + std::string get_name() override { + return "m_emult"; + } + + std::string get_description() override { + return "parallel element-wise mult matrix operation on opencl device"; + } + + Status execute(const DispatchContext& ctx) override { + TIME_PROFILE_SCOPE("opencl/m_emult"); + + auto t = ctx.task.template cast_safe(); + if (!t) { + LOG_MSG(Status::InvalidArgument, "invalid task type for cl m_emult"); + return Status::InvalidArgument; + } + + ref_ptr> R = t->R.template cast_safe>(); + ref_ptr> A = t->A.template cast_safe>(); + ref_ptr> B = t->B.template cast_safe>(); + ref_ptr> op = t->op.template cast_safe>(); + + if (!R || !A || !B) { + LOG_MSG(Status::InvalidArgument, "null matrix argument for cl m_emult"); + return Status::InvalidArgument; + } + if (!op) { + LOG_MSG(Status::Error, "failed to cast binary operation"); + return Status::Error; + } + + R->validate_wd(FormatMatrix::AccCsr); + A->validate_rw(FormatMatrix::AccCsr); + B->validate_rw(FormatMatrix::AccCsr); + + auto* p_R = R->template get>(); + const auto* p_A = A->template get>(); + const auto* p_B = B->template get>(); + + const uint n_rows = R->get_n_rows(); + const T fill = R->get_fill_value(); + + auto* p_cl_acc = get_acc_cl(); + auto& queue = p_cl_acc->get_queue_default(); + auto* tmp_alloc = p_cl_acc->get_alloc_tmp(); + + std::shared_ptr program; + if (!ensure_kernel(op, program)) { + return Status::CompilationError; + } + + cl::Buffer cl_counts = tmp_alloc->alloc(sizeof(uint) * (n_rows + 1)); + + const uint avg = static_cast((p_A->values + p_B->values) / (2 * n_rows)); + const bool use_group = avg >= k_m_emult_group_size; + + const uint wgs = p_cl_acc->get_default_wgs(); + const uint global_size = use_group ? n_rows * k_m_emult_group_size : align(n_rows, wgs); + const uint local_size = use_group ? k_m_emult_group_size : wgs; + const cl::NDRange global(global_size); + const cl::NDRange local(local_size); + + auto kernel_count = program->make_kernel(use_group ? "m_emult_count_group" : "m_emult_count"); + kernel_count.setArg(0, p_A->Ap); + kernel_count.setArg(1, p_A->Aj); + kernel_count.setArg(2, p_A->Ax); + kernel_count.setArg(3, p_B->Ap); + kernel_count.setArg(4, p_B->Aj); + kernel_count.setArg(5, p_B->Ax); + kernel_count.setArg(6, cl_counts); + kernel_count.setArg(7, n_rows); + kernel_count.setArg(8, fill); + queue.enqueueNDRangeKernel(kernel_count, cl::NDRange(), global, local); + + cl_exclusive_scan(queue, cl_counts, n_rows + 1, + PLUS_UINT.template cast_safe>(), tmp_alloc); + + CLCounterWrapper cl_total; + queue.enqueueCopyBuffer(cl_counts, cl_total.buffer(), sizeof(uint) * n_rows, 0, sizeof(uint)); + const uint total = cl_total.get(queue); + + cl_csr_resize(n_rows, total == 0 ? 1 : total, *p_R); + p_R->values = total; + queue.enqueueCopyBuffer(cl_counts, p_R->Ap, 0, 0, sizeof(uint) * (n_rows + 1)); + + auto kernel_scatter = program->make_kernel(use_group ? "m_emult_scatter_group" : "m_emult_scatter"); + kernel_scatter.setArg(0, p_A->Ap); + kernel_scatter.setArg(1, p_A->Aj); + kernel_scatter.setArg(2, p_A->Ax); + kernel_scatter.setArg(3, p_B->Ap); + kernel_scatter.setArg(4, p_B->Aj); + kernel_scatter.setArg(5, p_B->Ax); + kernel_scatter.setArg(6, p_R->Ap); + kernel_scatter.setArg(7, p_R->Aj); + kernel_scatter.setArg(8, p_R->Ax); + kernel_scatter.setArg(9, n_rows); + kernel_scatter.setArg(10, fill); + queue.enqueueNDRangeKernel(kernel_scatter, cl::NDRange(), global, local); + + tmp_alloc->free_all(); + + return Status::Ok; + } + + private: + bool ensure_kernel(const ref_ptr>& op, std::shared_ptr& program) { + CLProgramBuilder builder; + builder.set_name("m_emult") + .add_type("TYPE", get_ttype().template as()) + .add_define("GROUP_SIZE", k_m_emult_group_size) + .add_op("OP_BINARY", op.template as()) + .set_source(source_m_emult) + .acquire(); + + program = builder.get_program(); + return true; + } + }; + +}// namespace spla + +#endif//SPLA_CL_M_EMULT_HPP diff --git a/src/opencl/generated/auto_m_eadd.hpp b/src/opencl/generated/auto_m_eadd.hpp new file mode 100644 index 000000000..cd1ffe415 --- /dev/null +++ b/src/opencl/generated/auto_m_eadd.hpp @@ -0,0 +1,454 @@ +//////////////////////////////////////////////////////////////////// +// Copyright (c) 2021 - 2026 SparseLinearAlgebra +// Autogenerated file, do not modify +//////////////////////////////////////////////////////////////////// + +#pragma once + +static const char source_m_eadd[] = R"( + + + +// memory bank conflict-free address and local buffer size +#ifdef LM_NUM_MEM_BANKS + #define LM_ADDR(address) (address + ((address) / LM_NUM_MEM_BANKS)) + #define LM_SIZE(size) (size + (size) / LM_NUM_MEM_BANKS) +#endif + +#define SWAP_KEYS(x, y) \ + uint tmp1 = x; \ + x = y; \ + y = tmp1; + +#define SWAP_VALUES(x, y) \ + TYPE tmp2 = x; \ + x = y; \ + y = tmp2; + +// nearest power of two number greater equals n +uint ceil_to_pow2(uint n) { + uint r = 1; + while (r < n) r *= 2; + return r; +} + +// find first element in a sorted array such x <= element +uint lower_bound(const uint x, + uint first, + uint size, + __global const uint* array) { + while (size > 0) { + int step = size / 2; + + if (array[first + step] < x) { + first = first + step + 1; + size -= step + 1; + } else { + size = step; + } + } + return first; +} + +// find first element in a sorted array such x <= element +uint lower_bound_local(const uint x, + uint first, + uint size, + __local const uint* array) { + while (size > 0) { + int step = size / 2; + + if (array[first + step] < x) { + first = first + step + 1; + size -= step + 1; + } else { + size = step; + } + } + return first; +} +__kernel void m_eadd_count( + __global const uint* a_ap, + __global const uint* a_aj, + __global const TYPE* a_ax, + __global const uint* b_ap, + __global const uint* b_aj, + __global const TYPE* b_ax, + __global uint* row_counts, + const uint n_rows, + const TYPE fill) { + + const uint r = get_global_id(0); + if (r >= n_rows) return; + + if (r == 0) { + row_counts[n_rows] = 0; + } + + uint ia = a_ap[r]; + uint ea = a_ap[r + 1]; + uint ib = b_ap[r]; + uint eb = b_ap[r + 1]; + uint count = 0; + + while (ia < ea && ib < eb) { + const uint ca = a_aj[ia]; + const uint cb = b_aj[ib]; + + TYPE v; + + if (ca < cb) { + v = a_ax[ia]; + ++ia; + } else if (cb < ca) { + v = b_ax[ib]; + ++ib; + } else { + v = OP_BINARY(a_ax[ia], b_ax[ib]); + ++ia; + ++ib; + } + + if (v != fill) { + ++count; + } + } + + while (ia < ea) { + if (a_ax[ia] != fill) { + ++count; + } + ++ia; + } + while (ib < eb) { + if (b_ax[ib] != fill) { + ++count; + } + ++ib; + } + + row_counts[r] = count; +} + +__kernel void m_eadd_scatter( + __global const uint* a_ap, + __global const uint* a_aj, + __global const TYPE* a_ax, + __global const uint* b_ap, + __global const uint* b_aj, + __global const TYPE* b_ax, + __global const uint* row_offsets, + __global uint* r_aj, + __global TYPE* r_ax, + const uint n_rows, + const TYPE fill) { + + const uint r = get_global_id(0); + if (r >= n_rows) return; + + uint ia = a_ap[r]; + uint ea = a_ap[r + 1]; + uint ib = b_ap[r]; + uint eb = b_ap[r + 1]; + uint k = row_offsets[r]; + + while (ia < ea && ib < eb) { + const uint ca = a_aj[ia]; + const uint cb = b_aj[ib]; + + uint col; + TYPE v; + + if (ca < cb) { + col = ca; + v = a_ax[ia]; + ++ia; + } else if (cb < ca) { + col = cb; + v = b_ax[ib]; + ++ib; + } else { + col = ca; + v = OP_BINARY(a_ax[ia], b_ax[ib]); + ++ia; + ++ib; + } + + if (v != fill) { + r_aj[k] = col; + r_ax[k] = v; + ++k; + } + } + + while (ia < ea) { + if (a_ax[ia] != fill) { + r_aj[k] = a_aj[ia]; + r_ax[k] = a_ax[ia]; + ++k; + } + ++ia; + } + while (ib < eb) { + if (b_ax[ib] != fill) { + r_aj[k] = b_aj[ib]; + r_ax[k] = b_ax[ib]; + ++k; + } + ++ib; + } +} + +__kernel void m_eadd_count_group( + __global const uint* a_ap, + __global const uint* a_aj, + __global const TYPE* a_ax, + __global const uint* b_ap, + __global const uint* b_aj, + __global const TYPE* b_ax, + __global uint* row_counts, + const uint n_rows, + const TYPE fill) { + + __local uint local_counts[GROUP_SIZE]; + + const uint r = get_group_id(0); + const uint lid = get_local_id(0); + + if (r == 0 && lid == 0) { + row_counts[n_rows] = 0; + } + + const uint a_start = a_ap[r]; + const uint a_end = a_ap[r + 1]; + const uint b_start = b_ap[r]; + const uint b_end = b_ap[r + 1]; + + const uint a_len = a_end - a_start; + const uint chunk = (a_len + GROUP_SIZE - 1) / GROUP_SIZE; + + const uint a0 = a_start + min(lid * chunk, a_len); + const uint a1 = a_start + min((lid + 1) * chunk, a_len); + + uint b0; + uint b1; + + if (lid == 0) { + b0 = b_start; + } else if (a0 == a_end) { + b0 = b_end; + } else { + b0 = b_start + lower_bound(a_aj[a0], 0, b_end - b_start, b_aj + b_start); + } + + if (lid == GROUP_SIZE - 1 || a1 == a_end) { + b1 = b_end; + } else { + b1 = b_start + lower_bound(a_aj[a1], 0, b_end - b_start, b_aj + b_start); + } + + uint i = a0; + uint j = b0; + uint count = 0; + + while (i < a1 && j < b1) { + const uint ca = a_aj[i]; + const uint cb = b_aj[j]; + + TYPE v; + + if (ca < cb) { + v = a_ax[i]; + ++i; + } else if (cb < ca) { + v = b_ax[j]; + ++j; + } else { + v = OP_BINARY(a_ax[i], b_ax[j]); + ++i; + ++j; + } + + if (v != fill) { + ++count; + } + } + + while (i < a1) { + if (a_ax[i] != fill) { + ++count; + } + ++i; + } + while (j < b1) { + if (b_ax[j] != fill) { + ++count; + } + ++j; + } + + local_counts[lid] = count; + barrier(CLK_LOCAL_MEM_FENCE); + + if (lid == 0) { + uint total = 0; + for (uint t = 0; t < GROUP_SIZE; ++t) { + total += local_counts[t]; + } + row_counts[r] = total; + } +} + +__kernel void m_eadd_scatter_group( + __global const uint* a_ap, + __global const uint* a_aj, + __global const TYPE* a_ax, + __global const uint* b_ap, + __global const uint* b_aj, + __global const TYPE* b_ax, + __global const uint* row_offsets, + __global uint* r_aj, + __global TYPE* r_ax, + const uint n_rows, + const TYPE fill) { + + __local uint local_counts[GROUP_SIZE]; + + const uint r = get_group_id(0); + const uint lid = get_local_id(0); + + const uint a_start = a_ap[r]; + const uint a_end = a_ap[r + 1]; + const uint b_start = b_ap[r]; + const uint b_end = b_ap[r + 1]; + + const uint a_len = a_end - a_start; + const uint chunk = (a_len + GROUP_SIZE - 1) / GROUP_SIZE; + + const uint a0 = a_start + min(lid * chunk, a_len); + const uint a1 = a_start + min((lid + 1) * chunk, a_len); + + uint b0; + uint b1; + + if (lid == 0) { + b0 = b_start; + } else if (a0 == a_end) { + b0 = b_end; + } else { + b0 = b_start + lower_bound(a_aj[a0], 0, b_end - b_start, b_aj + b_start); + } + + if (lid == GROUP_SIZE - 1 || a1 == a_end) { + b1 = b_end; + } else { + b1 = b_start + lower_bound(a_aj[a1], 0, b_end - b_start, b_aj + b_start); + } + + uint i = a0; + uint j = b0; + uint count = 0; + + while (i < a1 && j < b1) { + const uint ca = a_aj[i]; + const uint cb = b_aj[j]; + + TYPE v; + + if (ca < cb) { + v = a_ax[i]; + ++i; + } else if (cb < ca) { + v = b_ax[j]; + ++j; + } else { + v = OP_BINARY(a_ax[i], b_ax[j]); + ++i; + ++j; + } + + if (v != fill) { + ++count; + } + } + + while (i < a1) { + if (a_ax[i] != fill) { + ++count; + } + ++i; + } + while (j < b1) { + if (b_ax[j] != fill) { + ++count; + } + ++j; + } + + local_counts[lid] = count; + barrier(CLK_LOCAL_MEM_FENCE); + + if (lid == 0) { + uint acc = 0; + for (uint t = 0; t < GROUP_SIZE; ++t) { + const uint c = local_counts[t]; + local_counts[t] = acc; + acc += c; + } + } + + barrier(CLK_LOCAL_MEM_FENCE); + + uint k = row_offsets[r] + local_counts[lid]; + + i = a0; + j = b0; + + while (i < a1 && j < b1) { + const uint ca = a_aj[i]; + const uint cb = b_aj[j]; + + uint col; + TYPE v; + + if (ca < cb) { + col = ca; + v = a_ax[i]; + ++i; + } else if (cb < ca) { + col = cb; + v = b_ax[j]; + ++j; + } else { + col = ca; + v = OP_BINARY(a_ax[i], b_ax[j]); + ++i; + ++j; + } + + if (v != fill) { + r_aj[k] = col; + r_ax[k] = v; + ++k; + } + } + + while (i < a1) { + if (a_ax[i] != fill) { + r_aj[k] = a_aj[i]; + r_ax[k] = a_ax[i]; + ++k; + } + ++i; + } + while (j < b1) { + if (b_ax[j] != fill) { + r_aj[k] = b_aj[j]; + r_ax[k] = b_ax[j]; + ++k; + } + ++j; + } +} + +)"; \ No newline at end of file diff --git a/src/opencl/generated/auto_m_emult.hpp b/src/opencl/generated/auto_m_emult.hpp new file mode 100644 index 000000000..0621085e5 --- /dev/null +++ b/src/opencl/generated/auto_m_emult.hpp @@ -0,0 +1,348 @@ +//////////////////////////////////////////////////////////////////// +// Copyright (c) 2021 - 2026 SparseLinearAlgebra +// Autogenerated file, do not modify +//////////////////////////////////////////////////////////////////// + +#pragma once + +static const char source_m_emult[] = R"( + + + +// memory bank conflict-free address and local buffer size +#ifdef LM_NUM_MEM_BANKS + #define LM_ADDR(address) (address + ((address) / LM_NUM_MEM_BANKS)) + #define LM_SIZE(size) (size + (size) / LM_NUM_MEM_BANKS) +#endif + +#define SWAP_KEYS(x, y) \ + uint tmp1 = x; \ + x = y; \ + y = tmp1; + +#define SWAP_VALUES(x, y) \ + TYPE tmp2 = x; \ + x = y; \ + y = tmp2; + +// nearest power of two number greater equals n +uint ceil_to_pow2(uint n) { + uint r = 1; + while (r < n) r *= 2; + return r; +} + +// find first element in a sorted array such x <= element +uint lower_bound(const uint x, + uint first, + uint size, + __global const uint* array) { + while (size > 0) { + int step = size / 2; + + if (array[first + step] < x) { + first = first + step + 1; + size -= step + 1; + } else { + size = step; + } + } + return first; +} + +// find first element in a sorted array such x <= element +uint lower_bound_local(const uint x, + uint first, + uint size, + __local const uint* array) { + while (size > 0) { + int step = size / 2; + + if (array[first + step] < x) { + first = first + step + 1; + size -= step + 1; + } else { + size = step; + } + } + return first; +} +__kernel void m_emult_count( + __global const uint* a_ap, + __global const uint* a_aj, + __global const TYPE* a_ax, + __global const uint* b_ap, + __global const uint* b_aj, + __global const TYPE* b_ax, + __global uint* row_counts, + const uint n_rows, + const TYPE fill) { + + const uint r = get_global_id(0); + if (r >= n_rows) return; + + if (r == 0) { + row_counts[n_rows] = 0; + } + + uint ia = a_ap[r]; + uint ea = a_ap[r + 1]; + uint ib = b_ap[r]; + uint eb = b_ap[r + 1]; + uint count = 0; + + while (ia < ea && ib < eb) { + const uint ca = a_aj[ia]; + const uint cb = b_aj[ib]; + + if (ca < cb) { + ++ia; + } else if (cb < ca) { + ++ib; + } else { + const TYPE v = OP_BINARY(a_ax[ia], b_ax[ib]); + if (v != fill) { + ++count; + } + ++ia; + ++ib; + } + } + + row_counts[r] = count; +} + +__kernel void m_emult_scatter( + __global const uint* a_ap, + __global const uint* a_aj, + __global const TYPE* a_ax, + __global const uint* b_ap, + __global const uint* b_aj, + __global const TYPE* b_ax, + __global const uint* row_offsets, + __global uint* r_aj, + __global TYPE* r_ax, + const uint n_rows, + const TYPE fill) { + + const uint r = get_global_id(0); + if (r >= n_rows) return; + + uint ia = a_ap[r]; + uint ea = a_ap[r + 1]; + uint ib = b_ap[r]; + uint eb = b_ap[r + 1]; + uint k = row_offsets[r]; + + while (ia < ea && ib < eb) { + const uint ca = a_aj[ia]; + const uint cb = b_aj[ib]; + + if (ca < cb) { + ++ia; + } else if (cb < ca) { + ++ib; + } else { + const TYPE v = OP_BINARY(a_ax[ia], b_ax[ib]); + if (v != fill) { + r_aj[k] = ca; + r_ax[k] = v; + ++k; + } + ++ia; + ++ib; + } + } +} + +__kernel void m_emult_count_group( + __global const uint* a_ap, + __global const uint* a_aj, + __global const TYPE* a_ax, + __global const uint* b_ap, + __global const uint* b_aj, + __global const TYPE* b_ax, + __global uint* row_counts, + const uint n_rows, + const TYPE fill) { + + __local uint local_counts[GROUP_SIZE]; + + const uint r = get_group_id(0); + const uint lid = get_local_id(0); + + if (r == 0 && lid == 0) { + row_counts[n_rows] = 0; + } + + const uint a_start = a_ap[r]; + const uint a_end = a_ap[r + 1]; + const uint b_start = b_ap[r]; + const uint b_end = b_ap[r + 1]; + + const uint a_len = a_end - a_start; + const uint chunk = (a_len + GROUP_SIZE - 1) / GROUP_SIZE; + + const uint a0 = a_start + min(lid * chunk, a_len); + const uint a1 = a_start + min((lid + 1) * chunk, a_len); + + uint b0; + uint b1; + + if (lid == 0) { + b0 = b_start; + } else if (a0 == a_end) { + b0 = b_end; + } else { + b0 = b_start + lower_bound(a_aj[a0], 0, b_end - b_start, b_aj + b_start); + } + + if (lid == GROUP_SIZE - 1 || a1 == a_end) { + b1 = b_end; + } else { + b1 = b_start + lower_bound(a_aj[a1], 0, b_end - b_start, b_aj + b_start); + } + + uint i = a0; + uint j = b0; + uint count = 0; + + while (i < a1 && j < b1) { + const uint ca = a_aj[i]; + const uint cb = b_aj[j]; + + if (ca < cb) { + ++i; + } else if (cb < ca) { + ++j; + } else { + const TYPE v = OP_BINARY(a_ax[i], b_ax[j]); + if (v != fill) { + ++count; + } + ++i; + ++j; + } + } + + local_counts[lid] = count; + barrier(CLK_LOCAL_MEM_FENCE); + + if (lid == 0) { + uint total = 0; + for (uint t = 0; t < GROUP_SIZE; ++t) { + total += local_counts[t]; + } + row_counts[r] = total; + } +} + +__kernel void m_emult_scatter_group( + __global const uint* a_ap, + __global const uint* a_aj, + __global const TYPE* a_ax, + __global const uint* b_ap, + __global const uint* b_aj, + __global const TYPE* b_ax, + __global const uint* row_offsets, + __global uint* r_aj, + __global TYPE* r_ax, + const uint n_rows, + const TYPE fill) { + + __local uint local_counts[GROUP_SIZE]; + + const uint r = get_group_id(0); + const uint lid = get_local_id(0); + + const uint a_start = a_ap[r]; + const uint a_end = a_ap[r + 1]; + const uint b_start = b_ap[r]; + const uint b_end = b_ap[r + 1]; + + const uint a_len = a_end - a_start; + const uint chunk = (a_len + GROUP_SIZE - 1) / GROUP_SIZE; + + const uint a0 = a_start + min(lid * chunk, a_len); + const uint a1 = a_start + min((lid + 1) * chunk, a_len); + + uint b0; + uint b1; + + if (lid == 0) { + b0 = b_start; + } else if (a0 == a_end) { + b0 = b_end; + } else { + b0 = b_start + lower_bound(a_aj[a0], 0, b_end - b_start, b_aj + b_start); + } + + if (lid == GROUP_SIZE - 1 || a1 == a_end) { + b1 = b_end; + } else { + b1 = b_start + lower_bound(a_aj[a1], 0, b_end - b_start, b_aj + b_start); + } + + uint i = a0; + uint j = b0; + uint count = 0; + + while (i < a1 && j < b1) { + const uint ca = a_aj[i]; + const uint cb = b_aj[j]; + + if (ca < cb) { + ++i; + } else if (cb < ca) { + ++j; + } else { + const TYPE v = OP_BINARY(a_ax[i], b_ax[j]); + if (v != fill) { + ++count; + } + ++i; + ++j; + } + } + + local_counts[lid] = count; + barrier(CLK_LOCAL_MEM_FENCE); + + if (lid == 0) { + uint acc = 0; + for (uint t = 0; t < GROUP_SIZE; ++t) { + const uint c = local_counts[t]; + local_counts[t] = acc; + acc += c; + } + } + + barrier(CLK_LOCAL_MEM_FENCE); + + uint k = row_offsets[r] + local_counts[lid]; + + i = a0; + j = b0; + + while (i < a1 && j < b1) { + const uint ca = a_aj[i]; + const uint cb = b_aj[j]; + + if (ca < cb) { + ++i; + } else if (cb < ca) { + ++j; + } else { + const TYPE v = OP_BINARY(a_ax[i], b_ax[j]); + if (v != fill) { + r_aj[k] = ca; + r_ax[k] = v; + ++k; + } + ++i; + ++j; + } + } +} + +)"; \ No newline at end of file diff --git a/src/opencl/kernels/m_eadd.cl b/src/opencl/kernels/m_eadd.cl new file mode 100644 index 000000000..c45531d7a --- /dev/null +++ b/src/opencl/kernels/m_eadd.cl @@ -0,0 +1,413 @@ +/**********************************************************************************/ +/* This file is part of spla project */ +/* https://github.com/SparseLinearAlgebra/spla */ +/**********************************************************************************/ +/* MIT License */ +/* */ +/* Copyright (c) 2023 SparseLinearAlgebra */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining a copy */ +/* of this software and associated documentation files (the "Software"), to deal */ +/* in the Software without restriction, including without limitation the rights */ +/* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */ +/* copies of the Software, and to permit persons to whom the Software is */ +/* furnished to do so, subject to the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be included in all */ +/* copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */ +/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */ +/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */ +/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */ +/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */ +/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ +/* SOFTWARE. */ +/**********************************************************************************/ + +#include "common_def.cl" +#include "common_func.cl" + +__kernel void m_eadd_count( + __global const uint* a_ap, + __global const uint* a_aj, + __global const TYPE* a_ax, + __global const uint* b_ap, + __global const uint* b_aj, + __global const TYPE* b_ax, + __global uint* row_counts, + const uint n_rows, + const TYPE fill) { + + const uint r = get_global_id(0); + if (r >= n_rows) return; + + if (r == 0) { + row_counts[n_rows] = 0; + } + + uint ia = a_ap[r]; + uint ea = a_ap[r + 1]; + uint ib = b_ap[r]; + uint eb = b_ap[r + 1]; + uint count = 0; + + while (ia < ea && ib < eb) { + const uint ca = a_aj[ia]; + const uint cb = b_aj[ib]; + + TYPE v; + + if (ca < cb) { + v = a_ax[ia]; + ++ia; + } else if (cb < ca) { + v = b_ax[ib]; + ++ib; + } else { + v = OP_BINARY(a_ax[ia], b_ax[ib]); + ++ia; + ++ib; + } + + if (v != fill) { + ++count; + } + } + + while (ia < ea) { + if (a_ax[ia] != fill) { + ++count; + } + ++ia; + } + while (ib < eb) { + if (b_ax[ib] != fill) { + ++count; + } + ++ib; + } + + row_counts[r] = count; +} + +__kernel void m_eadd_scatter( + __global const uint* a_ap, + __global const uint* a_aj, + __global const TYPE* a_ax, + __global const uint* b_ap, + __global const uint* b_aj, + __global const TYPE* b_ax, + __global const uint* row_offsets, + __global uint* r_aj, + __global TYPE* r_ax, + const uint n_rows, + const TYPE fill) { + + const uint r = get_global_id(0); + if (r >= n_rows) return; + + uint ia = a_ap[r]; + uint ea = a_ap[r + 1]; + uint ib = b_ap[r]; + uint eb = b_ap[r + 1]; + uint k = row_offsets[r]; + + while (ia < ea && ib < eb) { + const uint ca = a_aj[ia]; + const uint cb = b_aj[ib]; + + uint col; + TYPE v; + + if (ca < cb) { + col = ca; + v = a_ax[ia]; + ++ia; + } else if (cb < ca) { + col = cb; + v = b_ax[ib]; + ++ib; + } else { + col = ca; + v = OP_BINARY(a_ax[ia], b_ax[ib]); + ++ia; + ++ib; + } + + if (v != fill) { + r_aj[k] = col; + r_ax[k] = v; + ++k; + } + } + + while (ia < ea) { + if (a_ax[ia] != fill) { + r_aj[k] = a_aj[ia]; + r_ax[k] = a_ax[ia]; + ++k; + } + ++ia; + } + while (ib < eb) { + if (b_ax[ib] != fill) { + r_aj[k] = b_aj[ib]; + r_ax[k] = b_ax[ib]; + ++k; + } + ++ib; + } +} + +__kernel void m_eadd_count_group( + __global const uint* a_ap, + __global const uint* a_aj, + __global const TYPE* a_ax, + __global const uint* b_ap, + __global const uint* b_aj, + __global const TYPE* b_ax, + __global uint* row_counts, + const uint n_rows, + const TYPE fill) { + + __local uint local_counts[GROUP_SIZE]; + + const uint r = get_group_id(0); + const uint lid = get_local_id(0); + + if (r == 0 && lid == 0) { + row_counts[n_rows] = 0; + } + + const uint a_start = a_ap[r]; + const uint a_end = a_ap[r + 1]; + const uint b_start = b_ap[r]; + const uint b_end = b_ap[r + 1]; + + const uint a_len = a_end - a_start; + const uint chunk = (a_len + GROUP_SIZE - 1) / GROUP_SIZE; + + const uint a0 = a_start + min(lid * chunk, a_len); + const uint a1 = a_start + min((lid + 1) * chunk, a_len); + + uint b0; + uint b1; + + if (lid == 0) { + b0 = b_start; + } else if (a0 == a_end) { + b0 = b_end; + } else { + b0 = b_start + lower_bound(a_aj[a0], 0, b_end - b_start, b_aj + b_start); + } + + if (lid == GROUP_SIZE - 1 || a1 == a_end) { + b1 = b_end; + } else { + b1 = b_start + lower_bound(a_aj[a1], 0, b_end - b_start, b_aj + b_start); + } + + uint i = a0; + uint j = b0; + uint count = 0; + + while (i < a1 && j < b1) { + const uint ca = a_aj[i]; + const uint cb = b_aj[j]; + + TYPE v; + + if (ca < cb) { + v = a_ax[i]; + ++i; + } else if (cb < ca) { + v = b_ax[j]; + ++j; + } else { + v = OP_BINARY(a_ax[i], b_ax[j]); + ++i; + ++j; + } + + if (v != fill) { + ++count; + } + } + + while (i < a1) { + if (a_ax[i] != fill) { + ++count; + } + ++i; + } + while (j < b1) { + if (b_ax[j] != fill) { + ++count; + } + ++j; + } + + local_counts[lid] = count; + barrier(CLK_LOCAL_MEM_FENCE); + + if (lid == 0) { + uint total = 0; + for (uint t = 0; t < GROUP_SIZE; ++t) { + total += local_counts[t]; + } + row_counts[r] = total; + } +} + +__kernel void m_eadd_scatter_group( + __global const uint* a_ap, + __global const uint* a_aj, + __global const TYPE* a_ax, + __global const uint* b_ap, + __global const uint* b_aj, + __global const TYPE* b_ax, + __global const uint* row_offsets, + __global uint* r_aj, + __global TYPE* r_ax, + const uint n_rows, + const TYPE fill) { + + __local uint local_counts[GROUP_SIZE]; + + const uint r = get_group_id(0); + const uint lid = get_local_id(0); + + const uint a_start = a_ap[r]; + const uint a_end = a_ap[r + 1]; + const uint b_start = b_ap[r]; + const uint b_end = b_ap[r + 1]; + + const uint a_len = a_end - a_start; + const uint chunk = (a_len + GROUP_SIZE - 1) / GROUP_SIZE; + + const uint a0 = a_start + min(lid * chunk, a_len); + const uint a1 = a_start + min((lid + 1) * chunk, a_len); + + uint b0; + uint b1; + + if (lid == 0) { + b0 = b_start; + } else if (a0 == a_end) { + b0 = b_end; + } else { + b0 = b_start + lower_bound(a_aj[a0], 0, b_end - b_start, b_aj + b_start); + } + + if (lid == GROUP_SIZE - 1 || a1 == a_end) { + b1 = b_end; + } else { + b1 = b_start + lower_bound(a_aj[a1], 0, b_end - b_start, b_aj + b_start); + } + + uint i = a0; + uint j = b0; + uint count = 0; + + while (i < a1 && j < b1) { + const uint ca = a_aj[i]; + const uint cb = b_aj[j]; + + TYPE v; + + if (ca < cb) { + v = a_ax[i]; + ++i; + } else if (cb < ca) { + v = b_ax[j]; + ++j; + } else { + v = OP_BINARY(a_ax[i], b_ax[j]); + ++i; + ++j; + } + + if (v != fill) { + ++count; + } + } + + while (i < a1) { + if (a_ax[i] != fill) { + ++count; + } + ++i; + } + while (j < b1) { + if (b_ax[j] != fill) { + ++count; + } + ++j; + } + + local_counts[lid] = count; + barrier(CLK_LOCAL_MEM_FENCE); + + if (lid == 0) { + uint acc = 0; + for (uint t = 0; t < GROUP_SIZE; ++t) { + const uint c = local_counts[t]; + local_counts[t] = acc; + acc += c; + } + } + + barrier(CLK_LOCAL_MEM_FENCE); + + uint k = row_offsets[r] + local_counts[lid]; + + i = a0; + j = b0; + + while (i < a1 && j < b1) { + const uint ca = a_aj[i]; + const uint cb = b_aj[j]; + + uint col; + TYPE v; + + if (ca < cb) { + col = ca; + v = a_ax[i]; + ++i; + } else if (cb < ca) { + col = cb; + v = b_ax[j]; + ++j; + } else { + col = ca; + v = OP_BINARY(a_ax[i], b_ax[j]); + ++i; + ++j; + } + + if (v != fill) { + r_aj[k] = col; + r_ax[k] = v; + ++k; + } + } + + while (i < a1) { + if (a_ax[i] != fill) { + r_aj[k] = a_aj[i]; + r_ax[k] = a_ax[i]; + ++k; + } + ++i; + } + while (j < b1) { + if (b_ax[j] != fill) { + r_aj[k] = b_aj[j]; + r_ax[k] = b_ax[j]; + ++k; + } + ++j; + } +} diff --git a/src/opencl/kernels/m_emult.cl b/src/opencl/kernels/m_emult.cl new file mode 100644 index 000000000..8f63bf5a0 --- /dev/null +++ b/src/opencl/kernels/m_emult.cl @@ -0,0 +1,307 @@ +/**********************************************************************************/ +/* This file is part of spla project */ +/* https://github.com/SparseLinearAlgebra/spla */ +/**********************************************************************************/ +/* MIT License */ +/* */ +/* Copyright (c) 2023 SparseLinearAlgebra */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining a copy */ +/* of this software and associated documentation files (the "Software"), to deal */ +/* in the Software without restriction, including without limitation the rights */ +/* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */ +/* copies of the Software, and to permit persons to whom the Software is */ +/* furnished to do so, subject to the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be included in all */ +/* copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */ +/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */ +/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */ +/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */ +/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */ +/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ +/* SOFTWARE. */ +/**********************************************************************************/ + +#include "common_def.cl" +#include "common_func.cl" + +__kernel void m_emult_count( + __global const uint* a_ap, + __global const uint* a_aj, + __global const TYPE* a_ax, + __global const uint* b_ap, + __global const uint* b_aj, + __global const TYPE* b_ax, + __global uint* row_counts, + const uint n_rows, + const TYPE fill) { + + const uint r = get_global_id(0); + if (r >= n_rows) return; + + if (r == 0) { + row_counts[n_rows] = 0; + } + + uint ia = a_ap[r]; + uint ea = a_ap[r + 1]; + uint ib = b_ap[r]; + uint eb = b_ap[r + 1]; + uint count = 0; + + while (ia < ea && ib < eb) { + const uint ca = a_aj[ia]; + const uint cb = b_aj[ib]; + + if (ca < cb) { + ++ia; + } else if (cb < ca) { + ++ib; + } else { + const TYPE v = OP_BINARY(a_ax[ia], b_ax[ib]); + if (v != fill) { + ++count; + } + ++ia; + ++ib; + } + } + + row_counts[r] = count; +} + +__kernel void m_emult_scatter( + __global const uint* a_ap, + __global const uint* a_aj, + __global const TYPE* a_ax, + __global const uint* b_ap, + __global const uint* b_aj, + __global const TYPE* b_ax, + __global const uint* row_offsets, + __global uint* r_aj, + __global TYPE* r_ax, + const uint n_rows, + const TYPE fill) { + + const uint r = get_global_id(0); + if (r >= n_rows) return; + + uint ia = a_ap[r]; + uint ea = a_ap[r + 1]; + uint ib = b_ap[r]; + uint eb = b_ap[r + 1]; + uint k = row_offsets[r]; + + while (ia < ea && ib < eb) { + const uint ca = a_aj[ia]; + const uint cb = b_aj[ib]; + + if (ca < cb) { + ++ia; + } else if (cb < ca) { + ++ib; + } else { + const TYPE v = OP_BINARY(a_ax[ia], b_ax[ib]); + if (v != fill) { + r_aj[k] = ca; + r_ax[k] = v; + ++k; + } + ++ia; + ++ib; + } + } +} + +__kernel void m_emult_count_group( + __global const uint* a_ap, + __global const uint* a_aj, + __global const TYPE* a_ax, + __global const uint* b_ap, + __global const uint* b_aj, + __global const TYPE* b_ax, + __global uint* row_counts, + const uint n_rows, + const TYPE fill) { + + __local uint local_counts[GROUP_SIZE]; + + const uint r = get_group_id(0); + const uint lid = get_local_id(0); + + if (r == 0 && lid == 0) { + row_counts[n_rows] = 0; + } + + const uint a_start = a_ap[r]; + const uint a_end = a_ap[r + 1]; + const uint b_start = b_ap[r]; + const uint b_end = b_ap[r + 1]; + + const uint a_len = a_end - a_start; + const uint chunk = (a_len + GROUP_SIZE - 1) / GROUP_SIZE; + + const uint a0 = a_start + min(lid * chunk, a_len); + const uint a1 = a_start + min((lid + 1) * chunk, a_len); + + uint b0; + uint b1; + + if (lid == 0) { + b0 = b_start; + } else if (a0 == a_end) { + b0 = b_end; + } else { + b0 = b_start + lower_bound(a_aj[a0], 0, b_end - b_start, b_aj + b_start); + } + + if (lid == GROUP_SIZE - 1 || a1 == a_end) { + b1 = b_end; + } else { + b1 = b_start + lower_bound(a_aj[a1], 0, b_end - b_start, b_aj + b_start); + } + + uint i = a0; + uint j = b0; + uint count = 0; + + while (i < a1 && j < b1) { + const uint ca = a_aj[i]; + const uint cb = b_aj[j]; + + if (ca < cb) { + ++i; + } else if (cb < ca) { + ++j; + } else { + const TYPE v = OP_BINARY(a_ax[i], b_ax[j]); + if (v != fill) { + ++count; + } + ++i; + ++j; + } + } + + local_counts[lid] = count; + barrier(CLK_LOCAL_MEM_FENCE); + + if (lid == 0) { + uint total = 0; + for (uint t = 0; t < GROUP_SIZE; ++t) { + total += local_counts[t]; + } + row_counts[r] = total; + } +} + +__kernel void m_emult_scatter_group( + __global const uint* a_ap, + __global const uint* a_aj, + __global const TYPE* a_ax, + __global const uint* b_ap, + __global const uint* b_aj, + __global const TYPE* b_ax, + __global const uint* row_offsets, + __global uint* r_aj, + __global TYPE* r_ax, + const uint n_rows, + const TYPE fill) { + + __local uint local_counts[GROUP_SIZE]; + + const uint r = get_group_id(0); + const uint lid = get_local_id(0); + + const uint a_start = a_ap[r]; + const uint a_end = a_ap[r + 1]; + const uint b_start = b_ap[r]; + const uint b_end = b_ap[r + 1]; + + const uint a_len = a_end - a_start; + const uint chunk = (a_len + GROUP_SIZE - 1) / GROUP_SIZE; + + const uint a0 = a_start + min(lid * chunk, a_len); + const uint a1 = a_start + min((lid + 1) * chunk, a_len); + + uint b0; + uint b1; + + if (lid == 0) { + b0 = b_start; + } else if (a0 == a_end) { + b0 = b_end; + } else { + b0 = b_start + lower_bound(a_aj[a0], 0, b_end - b_start, b_aj + b_start); + } + + if (lid == GROUP_SIZE - 1 || a1 == a_end) { + b1 = b_end; + } else { + b1 = b_start + lower_bound(a_aj[a1], 0, b_end - b_start, b_aj + b_start); + } + + uint i = a0; + uint j = b0; + uint count = 0; + + while (i < a1 && j < b1) { + const uint ca = a_aj[i]; + const uint cb = b_aj[j]; + + if (ca < cb) { + ++i; + } else if (cb < ca) { + ++j; + } else { + const TYPE v = OP_BINARY(a_ax[i], b_ax[j]); + if (v != fill) { + ++count; + } + ++i; + ++j; + } + } + + local_counts[lid] = count; + barrier(CLK_LOCAL_MEM_FENCE); + + if (lid == 0) { + uint acc = 0; + for (uint t = 0; t < GROUP_SIZE; ++t) { + const uint c = local_counts[t]; + local_counts[t] = acc; + acc += c; + } + } + + barrier(CLK_LOCAL_MEM_FENCE); + + uint k = row_offsets[r] + local_counts[lid]; + + i = a0; + j = b0; + + while (i < a1 && j < b1) { + const uint ca = a_aj[i]; + const uint cb = b_aj[j]; + + if (ca < cb) { + ++i; + } else if (cb < ca) { + ++j; + } else { + const TYPE v = OP_BINARY(a_ax[i], b_ax[j]); + if (v != fill) { + r_aj[k] = ca; + r_ax[k] = v; + ++k; + } + ++i; + ++j; + } + } +} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e20657a89..56d03fb1e 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -18,6 +18,7 @@ spla_test_target(test_op) if (SPLA_BUILD_OPENCL) spla_test_target(test_opencl) spla_test_target(test_opencl_merge) + spla_test_target(test_opencl_eadd_emult) endif () spla_test_target(test_schedule) spla_test_target(test_vector) \ No newline at end of file diff --git a/tests/test_opencl_eadd_emult.cpp b/tests/test_opencl_eadd_emult.cpp new file mode 100644 index 000000000..e115d4bfa --- /dev/null +++ b/tests/test_opencl_eadd_emult.cpp @@ -0,0 +1,260 @@ +#include "test_common.hpp" + +#include + +#include +#include + +using uint = spla::uint; + +static bool ensure_opencl() { + auto* lib = spla::Library::get(); + if (lib->get_accelerator() != nullptr) { + return true; + } + return lib->set_accelerator(spla::AcceleratorType::OpenCL) == spla::Status::Ok && + lib->get_accelerator() != nullptr; +} + +static void put(spla::ref_ptr& m, uint i, uint j, int v) { + m->set_int(i, j, v); +} + +static uint nnz(spla::ref_ptr& m) { + spla::ref_ptr k1; + spla::ref_ptr k2; + spla::ref_ptr v; + m->read(k1, k2, v); + return static_cast(v->get_size() / sizeof(int)); +} + +TEST(opencl_matrix, eadd_int) { + if (!ensure_opencl()) GTEST_SKIP() << "OpenCL accelerator is not available"; + + const uint M = 4, N = 5; + auto A = spla::Matrix::make(M, N, spla::INT); + auto B = spla::Matrix::make(M, N, spla::INT); + auto R = spla::Matrix::make(M, N, spla::INT); + + put(A, 0, 0, 1); + put(A, 0, 2, 3); + put(A, 1, 1, 5); + put(A, 2, 4, 7); + + put(B, 0, 2, 10); + put(B, 0, 3, 2); + put(B, 1, 1, 4); + put(B, 3, 0, 6); + + ASSERT_EQ(spla::exec_m_eadd(R, A, B, spla::PLUS_INT), spla::Status::Ok); + + int v = 0; + R->get_int(0, 0, v); + EXPECT_EQ(v, 1); + R->get_int(0, 2, v); + EXPECT_EQ(v, 13); + R->get_int(0, 3, v); + EXPECT_EQ(v, 2); + R->get_int(1, 1, v); + EXPECT_EQ(v, 9); + R->get_int(2, 4, v); + EXPECT_EQ(v, 7); + R->get_int(3, 0, v); + EXPECT_EQ(v, 6); + + EXPECT_EQ(nnz(R), 6u); +} + +TEST(opencl_matrix, emult_int) { + if (!ensure_opencl()) GTEST_SKIP() << "OpenCL accelerator is not available"; + + const uint M = 4, N = 5; + auto A = spla::Matrix::make(M, N, spla::INT); + auto B = spla::Matrix::make(M, N, spla::INT); + auto R = spla::Matrix::make(M, N, spla::INT); + + put(A, 0, 0, 1); + put(A, 0, 2, 3); + put(A, 1, 1, 5); + put(A, 2, 4, 7); + + put(B, 0, 2, 10); + put(B, 0, 3, 2); + put(B, 1, 1, 4); + put(B, 3, 0, 6); + + ASSERT_EQ(spla::exec_m_emult(R, A, B, spla::PLUS_INT), spla::Status::Ok); + + int v = 0; + R->get_int(0, 2, v); + EXPECT_EQ(v, 13); + R->get_int(1, 1, v); + EXPECT_EQ(v, 9); + R->get_int(0, 0, v); + EXPECT_EQ(v, 0); + + EXPECT_EQ(nnz(R), 2u); +} + +TEST(opencl_matrix, eadd_fill_filter_int) { + if (!ensure_opencl()) GTEST_SKIP() << "OpenCL accelerator is not available"; + + const uint M = 4, N = 5; + auto A = spla::Matrix::make(M, N, spla::INT); + auto B = spla::Matrix::make(M, N, spla::INT); + auto R = spla::Matrix::make(M, N, spla::INT); + + R->set_fill_value(spla::Scalar::make_int(13)); + + put(A, 0, 0, 1); + put(A, 0, 2, 3); + put(A, 1, 1, 5); + put(A, 2, 4, 7); + + put(B, 0, 2, 10); + put(B, 0, 3, 2); + put(B, 1, 1, 4); + put(B, 3, 0, 6); + + ASSERT_EQ(spla::exec_m_eadd(R, A, B, spla::PLUS_INT), spla::Status::Ok); + + int v = 0; + R->get_int(0, 2, v); + EXPECT_EQ(v, 13); + + EXPECT_EQ(nnz(R), 5u); +} + +TEST(opencl_matrix, emult_disjoint_int) { + if (!ensure_opencl()) GTEST_SKIP() << "OpenCL accelerator is not available"; + + const uint M = 2, N = 2; + auto A = spla::Matrix::make(M, N, spla::INT); + auto B = spla::Matrix::make(M, N, spla::INT); + auto R = spla::Matrix::make(M, N, spla::INT); + + A->set_int(0, 0, 1); + B->set_int(1, 1, 2); + + ASSERT_EQ(spla::exec_m_emult(R, A, B, spla::PLUS_INT), spla::Status::Ok); + + EXPECT_EQ(nnz(R), 0u); + + int v = 0; + R->get_int(0, 0, v); + EXPECT_EQ(v, 0); +} + +TEST(opencl_matrix, emult_float) { + if (!ensure_opencl()) GTEST_SKIP() << "OpenCL accelerator is not available"; + + const uint M = 3, N = 3; + auto A = spla::Matrix::make(M, N, spla::FLOAT); + auto B = spla::Matrix::make(M, N, spla::FLOAT); + auto R = spla::Matrix::make(M, N, spla::FLOAT); + + A->set_float(0, 0, 2.0f); + A->set_float(1, 2, 3.0f); + B->set_float(0, 0, 4.0f); + B->set_float(1, 1, 5.0f); + + ASSERT_EQ(spla::exec_m_emult(R, A, B, spla::PLUS_FLOAT), spla::Status::Ok); + + float v = 0; + R->get_float(0, 0, v); + EXPECT_FLOAT_EQ(v, 6.0f); + + EXPECT_EQ(nnz(R), 1u); +} + +TEST(opencl_matrix, eadd_long_rows_int) { + if (!ensure_opencl()) GTEST_SKIP() << "OpenCL accelerator is not available"; + + const uint M = 4, N = 300; + auto A = spla::Matrix::make(M, N, spla::INT); + auto B = spla::Matrix::make(M, N, spla::INT); + auto R = spla::Matrix::make(M, N, spla::INT); + + for (uint r = 0; r < M; ++r) { + for (uint c = 0; c < 100; ++c) { + A->set_int(r, c, 1); + } + for (uint c = 50; c < 150; ++c) { + B->set_int(r, c, 2); + } + } + + ASSERT_EQ(spla::exec_m_eadd(R, A, B, spla::PLUS_INT), spla::Status::Ok); + + int v = 0; + R->get_int(0, 0, v); + EXPECT_EQ(v, 1); + R->get_int(0, 50, v); + EXPECT_EQ(v, 3); + R->get_int(0, 100, v); + EXPECT_EQ(v, 2); + R->get_int(3, 149, v); + EXPECT_EQ(v, 2); + + EXPECT_EQ(nnz(R), 4u * 150u); +} + +TEST(opencl_matrix, emult_long_rows_int) { + if (!ensure_opencl()) GTEST_SKIP() << "OpenCL accelerator is not available"; + + const uint M = 4, N = 300; + auto A = spla::Matrix::make(M, N, spla::INT); + auto B = spla::Matrix::make(M, N, spla::INT); + auto R = spla::Matrix::make(M, N, spla::INT); + + for (uint r = 0; r < M; ++r) { + for (uint c = 0; c < 100; ++c) { + A->set_int(r, c, 1); + } + for (uint c = 50; c < 150; ++c) { + B->set_int(r, c, 2); + } + } + + ASSERT_EQ(spla::exec_m_emult(R, A, B, spla::PLUS_INT), spla::Status::Ok); + + int v = 0; + R->get_int(0, 50, v); + EXPECT_EQ(v, 3); + R->get_int(0, 99, v); + EXPECT_EQ(v, 3); + R->get_int(0, 100, v); + EXPECT_EQ(v, 0); + + EXPECT_EQ(nnz(R), 4u * 50u); +} + +TEST(opencl_matrix, emult_long_rows_fill_int) { + if (!ensure_opencl()) GTEST_SKIP() << "OpenCL accelerator is not available"; + + const uint M = 4, N = 300; + auto A = spla::Matrix::make(M, N, spla::INT); + auto B = spla::Matrix::make(M, N, spla::INT); + auto R = spla::Matrix::make(M, N, spla::INT); + + R->set_fill_value(spla::Scalar::make_int(3)); + + for (uint r = 0; r < M; ++r) { + for (uint c = 0; c < 100; ++c) { + A->set_int(r, c, 1); + } + for (uint c = 50; c < 150; ++c) { + B->set_int(r, c, 2); + } + } + + ASSERT_EQ(spla::exec_m_emult(R, A, B, spla::PLUS_INT), spla::Status::Ok); + + int v = 0; + R->get_int(0, 50, v); + EXPECT_EQ(v, 3); + + EXPECT_EQ(nnz(R), 0u); +} + +SPLA_GTEST_MAIN_WITH_FINALIZE