Expressed HER2K as two HERK calls

pull/274/head
Cedric Nugteren 2018-04-18 20:58:29 +02:00
parent dcce23d938
commit 458e6717a9
4 changed files with 54 additions and 172 deletions

View File

@ -22,18 +22,7 @@ namespace clblast {
// Constructor: forwards to base class constructor
template <typename T, typename U>
Xher2k<T,U>::Xher2k(Queue &queue, EventPointer event, const std::string &name):
Routine(queue, event, name, {"Copy","Pad","Transpose","Padtranspose","Xgemm"}, PrecisionValue<T>(), {}, {
#include "../../kernels/level3/level3.opencl"
#include "../../kernels/level3/copy_fast.opencl"
#include "../../kernels/level3/copy_pad.opencl"
#include "../../kernels/level3/transpose_fast.opencl"
#include "../../kernels/level3/transpose_pad.opencl"
, // separated in multiple parts to prevent C1091 in MSVC 2013
#include "../../kernels/level3/xgemm_part1.opencl"
#include "../../kernels/level3/xgemm_part2.opencl"
#include "../../kernels/level3/xgemm_part3.opencl"
#include "../../kernels/level3/xgemm_part4.opencl"
}) {
Xherk<T,U>(queue, event, name) {
}
// =================================================================================================
@ -48,160 +37,22 @@ void Xher2k<T,U>::DoHer2k(const Layout layout, const Triangle triangle, const Tr
const U beta,
const Buffer<T> &c_buffer, const size_t c_offset, const size_t c_ld) {
// Makes sure all dimensions are larger than zero
if ((n == 0) || (k == 0) ) { throw BLASError(StatusCode::kInvalidDimension); }
// Determines whether to apply the conjugate transpose to matrix B (argument: no transpose) or
// to matrix A (argument: conjugate transpose)
auto ab_conjugate = (ab_transpose != Transpose::kNo);
// Computes whether or not the matrices are transposed in memory. This is based on their layout
// (row or column-major) and whether or not they are requested to be pre-transposed.
auto ab_rotated = (layout == Layout::kColMajor && ab_conjugate) ||
(layout == Layout::kRowMajor && !ab_conjugate);
auto c_rotated = (layout == Layout::kRowMajor);
// Computes the first and second dimensions of the A and B matrices taking the layout into account
auto ab_one = (ab_rotated) ? k : n;
auto ab_two = (ab_rotated) ? n : k;
// Tests the matrices (A, B, C) for validity, first from a perspective of the OpenCL buffers and
// their sizes, and then from a perspective of parameter values (e.g. n, k). Tests whether the
// OpenCL buffers are valid and non-zero and whether the OpenCL buffers have sufficient storage
// space. Also tests that the leading dimensions of:
// matrix A cannot be less than N when rotated, or less than K when not-rotated
// matrix B cannot be less than N when rotated, or less than K when not-rotated
// matrix C cannot be less than N
TestMatrixA(ab_one, ab_two, a_buffer, a_offset, a_ld);
TestMatrixB(ab_one, ab_two, b_buffer, b_offset, b_ld);
TestMatrixC(n, n, c_buffer, c_offset, c_ld);
// Calculates the ceiled versions of n and k
auto n_ceiled = Ceil(Ceil(n, db_["MWG"]), db_["NWG"]);
auto k_ceiled = Ceil(k, db_["KWG"]);
// Decides which kernel to run: the upper-triangular or lower-triangular version
auto kernel_name = (triangle == Triangle::kUpper) ? "XgemmUpper" : "XgemmLower";
// Determines whether or not temporary matrices are needed
auto a1_no_temp = ab_one == n_ceiled && ab_two == k_ceiled && a_ld == n_ceiled && a_offset == 0 &&
ab_rotated == false && ab_conjugate == false;
auto a2_no_temp = ab_one == n_ceiled && ab_two == k_ceiled && a_ld == n_ceiled && a_offset == 0 &&
ab_rotated == false && ab_conjugate == true;
auto b1_no_temp = ab_one == n_ceiled && ab_two == k_ceiled && b_ld == n_ceiled && b_offset == 0 &&
ab_rotated == false && ab_conjugate == false;
auto b2_no_temp = ab_one == n_ceiled && ab_two == k_ceiled && b_ld == n_ceiled && b_offset == 0 &&
ab_rotated == false && ab_conjugate == true;
// Creates the temporary matrices
auto a1_temp = (a1_no_temp) ? a_buffer : Buffer<T>(context_, k_ceiled*n_ceiled);
auto a2_temp = (a2_no_temp) ? a_buffer : Buffer<T>(context_, k_ceiled*n_ceiled);
auto b1_temp = (b1_no_temp) ? b_buffer : Buffer<T>(context_, k_ceiled*n_ceiled);
auto b2_temp = (b2_no_temp) ? b_buffer : Buffer<T>(context_, k_ceiled*n_ceiled);
auto c_temp = Buffer<T>(context_, n_ceiled*n_ceiled);
// Convert the arguments to complex versions
// Runs the first matrix multiplication
auto first_herk_event = Event();
auto complex_beta = T{beta, static_cast<U>(0.0)};
// Events of all kernels (including pre/post processing kernels)
auto eventWaitList = std::vector<Event>();
auto emptyEventList = std::vector<Event>();
// Runs the pre-processing kernels. This transposes the matrices A and B, but also pads zeros to
// to fill it up until it reaches a certain multiple of size (kernel parameter dependent). In
// case nothing has to be done, these kernels can be skipped.
if (!a1_no_temp) {
auto eventProcessA1 = Event();
PadCopyTransposeMatrix(queue_, device_, db_, eventProcessA1.pointer(), emptyEventList,
ab_one, ab_two, a_ld, a_offset, a_buffer,
n_ceiled, k_ceiled, n_ceiled, 0, a1_temp,
ConstantOne<T>(), program_,
true, ab_rotated, ab_conjugate);
eventWaitList.push_back(eventProcessA1);
}
if (!a2_no_temp) {
auto eventProcessA2 = Event();
PadCopyTransposeMatrix(queue_, device_, db_, eventProcessA2.pointer(), emptyEventList,
ab_one, ab_two, a_ld, a_offset, a_buffer,
n_ceiled, k_ceiled, n_ceiled, 0, a2_temp,
ConstantOne<T>(), program_,
true, ab_rotated, !ab_conjugate);
eventWaitList.push_back(eventProcessA2);
}
if (!b1_no_temp) {
auto eventProcessB1 = Event();
PadCopyTransposeMatrix(queue_, device_, db_, eventProcessB1.pointer(), emptyEventList,
ab_one, ab_two, b_ld, b_offset, b_buffer,
n_ceiled, k_ceiled, n_ceiled, 0, b1_temp,
ConstantOne<T>(), program_,
true, ab_rotated, ab_conjugate);
eventWaitList.push_back(eventProcessB1);
}
if (!b2_no_temp) {
auto eventProcessB2 = Event();
PadCopyTransposeMatrix(queue_, device_, db_, eventProcessB2.pointer(), emptyEventList,
ab_one, ab_two, b_ld, b_offset, b_buffer,
n_ceiled, k_ceiled, n_ceiled, 0, b2_temp,
ConstantOne<T>(), program_,
true, ab_rotated, !ab_conjugate);
eventWaitList.push_back(eventProcessB2);
}
// Furthermore, also creates a (possibly padded) copy of matrix C, since it is not allowed to
// modify the other triangle.
auto eventProcessC = Event();
PadCopyTransposeMatrix(queue_, device_, db_, eventProcessC.pointer(), emptyEventList,
n, n, c_ld, c_offset, c_buffer,
n_ceiled, n_ceiled, n_ceiled, 0, c_temp,
ConstantOne<T>(), program_,
true, c_rotated, false);
eventWaitList.push_back(eventProcessC);
// Retrieves the XgemmUpper or XgemmLower kernel from the compiled binary
auto kernel = Kernel(program_, kernel_name);
// Sets the kernel arguments
kernel.SetArgument(0, static_cast<int>(n_ceiled));
kernel.SetArgument(1, static_cast<int>(k_ceiled));
kernel.SetArgument(2, GetRealArg(alpha));
kernel.SetArgument(3, GetRealArg(complex_beta));
kernel.SetArgument(4, a1_temp());
kernel.SetArgument(5, b2_temp());
kernel.SetArgument(6, c_temp());
// Computes the global and local thread sizes
auto global = std::vector<size_t>{
(n_ceiled * db_["MDIMC"]) / db_["MWG"],
(n_ceiled * db_["NDIMC"]) / db_["NWG"]
};
auto local = std::vector<size_t>{db_["MDIMC"], db_["NDIMC"]};
// Launches the kernel
auto eventKernel1 = Event();
RunKernel(kernel, queue_, device_, global, local, eventKernel1.pointer(), eventWaitList);
eventWaitList.push_back(eventKernel1);
const auto negated_ab_transpose = (ab_transpose != Transpose::kNo) ? Transpose::kNo : Transpose::kYes;
HerkAB(layout, triangle, ab_transpose, negated_ab_transpose, n, k, alpha,
a_buffer, a_offset, a_ld, b_buffer, b_offset, b_ld, complex_beta, c_buffer, c_offset, c_ld,
first_herk_event.pointer(), false);
;
first_herk_event.WaitForCompletion();
// Swaps the arguments for matrices A and B, sets 'beta' to 1, and conjugate alpha
auto conjugate_alpha = T{alpha.real(), -alpha.imag()};
auto complex_one = T{static_cast<U>(1.0), static_cast<U>(0.0)};
kernel.SetArgument(2, GetRealArg(conjugate_alpha));
kernel.SetArgument(3, GetRealArg(complex_one));
kernel.SetArgument(4, b1_temp());
kernel.SetArgument(5, a2_temp());
// Runs the kernel again
auto eventKernel2 = Event();
RunKernel(kernel, queue_, device_, global, local, eventKernel2.pointer(), eventWaitList);
eventWaitList.push_back(eventKernel2);
// Runs the post-processing kernel
auto upper = (triangle == Triangle::kUpper);
auto lower = (triangle == Triangle::kLower);
PadCopyTransposeMatrix(queue_, device_, db_, event_, eventWaitList,
n_ceiled, n_ceiled, n_ceiled, 0, c_temp,
n, n, c_ld, c_offset, c_buffer,
ConstantOne<T>(), program_,
false, c_rotated, false, upper, lower, true);
HerkAB(layout, triangle, ab_transpose, negated_ab_transpose, n, k, conjugate_alpha,
b_buffer, b_offset, b_ld, a_buffer, a_offset, a_ld, complex_one, c_buffer, c_offset, c_ld,
event_, true);
}
// =================================================================================================

View File

@ -17,14 +17,19 @@
#define CLBLAST_ROUTINES_XHER2K_H_
#include "routine.hpp"
#include "routines/level3/xherk.hpp"
namespace clblast {
// =================================================================================================
// See comment at top of file for a description of the class
template <typename T, typename U>
class Xher2k: public Routine {
public:
class Xher2k: public Xherk<T, U> {
public:
// Uses methods and variables the regular Xherk routine
using Xherk<T, U>::event_;
using Xherk<T, U>::HerkAB;
// Constructor
Xher2k(Queue &queue, EventPointer event, const std::string &name = "HER2K");

View File

@ -48,6 +48,25 @@ void Xherk<T,U>::DoHerk(const Layout layout, const Triangle triangle, const Tran
const U beta,
const Buffer<T> &c_buffer, const size_t c_offset, const size_t c_ld) {
const auto b_transpose = (a_transpose != Transpose::kNo) ? Transpose::kNo : Transpose::kYes;
const auto b_buffer = a_buffer;
const auto b_offset = a_offset;
const auto b_ld = a_ld;
const auto complex_alpha = T{alpha, static_cast<U>(0.0)};
const auto complex_beta = T{beta, static_cast<U>(0.0)};
HerkAB(layout, triangle, a_transpose, b_transpose, n, k, complex_alpha,
a_buffer, a_offset, a_ld, b_buffer, b_offset, b_ld, complex_beta, c_buffer, c_offset, c_ld,
event_, true);
}
template <typename T, typename U>
void Xherk<T,U>::HerkAB(const Layout layout, const Triangle triangle, const Transpose a_transpose, const Transpose b_transpose,
const size_t n, const size_t k,
const T complex_alpha,
const Buffer<T> &a_buffer, const size_t a_offset, const size_t a_ld,
const Buffer<T> &b_buffer, const size_t b_offset, const size_t b_ld,
const T complex_beta,
const Buffer<T> &c_buffer, const size_t c_offset, const size_t c_ld,
EventPointer final_event, const bool diagonal_to_zero) {
// Computes the transpose/conjugate options and sets the a/b/c sizes based on that
bool a_do_transpose, b_do_transpose, c_do_transpose, dummy1, dummy2;
@ -69,6 +88,7 @@ void Xherk<T,U>::DoHerk(const Layout layout, const Triangle triangle, const Tran
// matrix A cannot be less than N when rotated, or less than K when not-rotated
// matrix C cannot be less than N
TestMatrixA(a_one, a_two, a_buffer, a_offset, a_ld);
TestMatrixB(b_one, b_two, b_buffer, b_offset, b_ld);
TestMatrixC(n, n, c_buffer, c_offset, c_ld);
// Calculates the ceiled versions of n and k
@ -87,17 +107,13 @@ void Xherk<T,U>::DoHerk(const Layout layout, const Triangle triangle, const Tran
// Determines whether or not temporary matrices are needed
const auto a_no_temp = Xgemm<T>::NoTempBuffer(a_one, a_one_i, a_two, a_two_i, a_ld, a_offset, a_do_transpose, a_conjugate);
const auto b_no_temp = Xgemm<T>::NoTempBuffer(a_one, b_one_i, a_two, b_two_i, a_ld, a_offset, b_do_transpose, b_conjugate);
const auto b_no_temp = Xgemm<T>::NoTempBuffer(b_one, b_one_i, b_two, b_two_i, b_ld, b_offset, b_do_transpose, b_conjugate);
// Creates the temporary matrices
auto a_temp = (a_no_temp) ? a_buffer : Buffer<T>(context_, a_one_i * a_two_i);
auto b_temp = (b_no_temp) ? a_buffer : Buffer<T>(context_, b_one_i * b_two_i);
auto b_temp = (b_no_temp) ? b_buffer : Buffer<T>(context_, b_one_i * b_two_i);
auto c_temp = Buffer<T>(context_, n_ceiled*n_ceiled);
// Convert the arguments to complex versions
auto complex_alpha = T{alpha, static_cast<U>(0.0)};
auto complex_beta = T{beta, static_cast<U>(0.0)};
// Events of all kernels (including pre/post processing kernels)
auto eventWaitList = std::vector<Event>();
auto emptyEventList = std::vector<Event>();
@ -117,7 +133,7 @@ void Xherk<T,U>::DoHerk(const Layout layout, const Triangle triangle, const Tran
if (!b_no_temp) {
auto eventProcessB = Event();
PadCopyTransposeMatrix(queue_, device_, db_, eventProcessB.pointer(), emptyEventList,
b_one, b_two, a_ld, a_offset, a_buffer,
b_one, b_two, b_ld, b_offset, b_buffer,
b_one_i, b_two_i, b_one_i, 0, b_temp,
ConstantOne<T>(), program_,
true, b_do_transpose, b_conjugate);
@ -162,11 +178,11 @@ void Xherk<T,U>::DoHerk(const Layout layout, const Triangle triangle, const Tran
const auto upper = Xgemm<T>::c_want_rotated_(db_["GEMMK"]) ? (triangle == Triangle::kLower) :
(triangle == Triangle::kUpper);
const auto lower = !upper;
PadCopyTransposeMatrix(queue_, device_, db_, event_, eventWaitList,
PadCopyTransposeMatrix(queue_, device_, db_, final_event, eventWaitList,
n_ceiled, n_ceiled, n_ceiled, 0, c_temp,
n, n, c_ld, c_offset, c_buffer,
ConstantOne<T>(), program_,
false, c_do_transpose, false, upper, lower, true);
false, c_do_transpose, false, upper, lower, diagonal_to_zero);
}
// =================================================================================================

View File

@ -36,6 +36,16 @@ class Xherk: public Routine {
const Buffer<T> &a_buffer, const size_t a_offset, const size_t a_ld,
const U beta,
const Buffer<T> &c_buffer, const size_t c_offset, const size_t c_ld);
// Helper function to be reused for HER2K
void HerkAB(const Layout layout, const Triangle triangle, const Transpose a_transpose, const Transpose b_transpose,
const size_t n, const size_t k,
const T complex_alpha,
const Buffer<T> &a_buffer, const size_t a_offset, const size_t a_ld,
const Buffer<T> &b_buffer, const size_t b_offset, const size_t b_ld,
const T complex_beta,
const Buffer<T> &c_buffer, const size_t c_offset, const size_t c_ld,
EventPointer final_event, const bool diagonal_to_zero);
};
// =================================================================================================