Skip to content
Open
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
12 changes: 12 additions & 0 deletions src/opencl/cl_algo_registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include <core/registry.hpp>
#include <core/top.hpp>

#include <opencl/cl_m_eadd.hpp>
#include <opencl/cl_m_emult.hpp>
#include <opencl/cl_m_reduce.hpp>
#include <opencl/cl_mxmT_masked.hpp>
#include <opencl/cl_mxv.hpp>
Expand Down Expand Up @@ -79,6 +81,16 @@ namespace spla {
g_registry->add(MAKE_KEY_CL_0("m_reduce", UINT), std::make_shared<Algo_m_reduce_cl<T_UINT>>());
g_registry->add(MAKE_KEY_CL_0("m_reduce", FLOAT), std::make_shared<Algo_m_reduce_cl<T_FLOAT>>());

// algorithm m_eadd
g_registry->add(MAKE_KEY_CL_0("m_eadd", INT), std::make_shared<Algo_m_eadd_cl<T_INT>>());
g_registry->add(MAKE_KEY_CL_0("m_eadd", UINT), std::make_shared<Algo_m_eadd_cl<T_UINT>>());
g_registry->add(MAKE_KEY_CL_0("m_eadd", FLOAT), std::make_shared<Algo_m_eadd_cl<T_FLOAT>>());

// algorithm m_emult
g_registry->add(MAKE_KEY_CL_0("m_emult", INT), std::make_shared<Algo_m_emult_cl<T_INT>>());
g_registry->add(MAKE_KEY_CL_0("m_emult", UINT), std::make_shared<Algo_m_emult_cl<T_UINT>>());
g_registry->add(MAKE_KEY_CL_0("m_emult", FLOAT), std::make_shared<Algo_m_emult_cl<T_FLOAT>>());

// algorthm mxv_masked
g_registry->add(MAKE_KEY_CL_0("mxv_masked", INT), std::make_shared<Algo_mxv_masked_cl<T_INT>>());
g_registry->add(MAKE_KEY_CL_0("mxv_masked", UINT), std::make_shared<Algo_mxv_masked_cl<T_UINT>>());
Expand Down
9 changes: 8 additions & 1 deletion src/opencl/cl_format_csr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
179 changes: 179 additions & 0 deletions src/opencl/cl_m_eadd.hpp
Original file line number Diff line number Diff line change
@@ -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 <schedule/schedule_tasks.hpp>

#include <core/dispatcher.hpp>
#include <core/registry.hpp>
#include <core/tmatrix.hpp>
#include <core/top.hpp>
#include <core/tscalar.hpp>
#include <core/ttype.hpp>

#include <opencl/cl_accelerator.hpp>
#include <opencl/cl_alloc.hpp>
#include <opencl/cl_counter.hpp>
#include <opencl/cl_format_csr.hpp>
#include <opencl/cl_formats.hpp>
#include <opencl/cl_prefix_sum.hpp>
#include <opencl/cl_program_builder.hpp>
#include <opencl/generated/auto_m_eadd.hpp>

namespace spla {

static constexpr uint k_m_eadd_group_size = 32;

template<typename T>
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<ScheduleTask_m_eadd>();
if (!t) {
LOG_MSG(Status::InvalidArgument, "invalid task type for cl m_eadd");
return Status::InvalidArgument;
}

ref_ptr<TMatrix<T>> R = t->R.template cast_safe<TMatrix<T>>();
ref_ptr<TMatrix<T>> A = t->A.template cast_safe<TMatrix<T>>();
ref_ptr<TMatrix<T>> B = t->B.template cast_safe<TMatrix<T>>();
ref_ptr<TOpBinary<T, T, T>> op = t->op.template cast_safe<TOpBinary<T, T, T>>();

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<CLCsr<T>>();
const auto* p_A = A->template get<CLCsr<T>>();
const auto* p_B = B->template get<CLCsr<T>>();

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<CLProgram> 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<uint>((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<uint>(queue, cl_counts, n_rows + 1,
PLUS_UINT.template cast_safe<TOpBinary<uint, uint, uint>>(), 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<TOpBinary<T, T, T>>& op, std::shared_ptr<CLProgram>& program) {
CLProgramBuilder builder;
builder.set_name("m_eadd")
.add_type("TYPE", get_ttype<T>().template as<Type>())
.add_define("GROUP_SIZE", k_m_eadd_group_size)
.add_op("OP_BINARY", op.template as<OpBinary>())
.set_source(source_m_eadd)
.acquire();

program = builder.get_program();
return true;
}
};

}// namespace spla

#endif//SPLA_CL_M_EADD_HPP
Loading