|
| 1 | + |
| 2 | +/* |
| 3 | + * Copyright 2021 Huawei Technologies Co., Ltd. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +/** |
| 19 | + * @file |
| 20 | + * |
| 21 | + * Describes raw functionalities that a so-called <em>final backend</em> should |
| 22 | + * provide. |
| 23 | + * |
| 24 | + * A final backend is one that other backends rely on for implementing core |
| 25 | + * computations. |
| 26 | + * |
| 27 | + * @author A. N. Yzelman |
| 28 | + * @date 13th of February, 2024 |
| 29 | + */ |
| 30 | + |
| 31 | +#ifndef _H_GRB_BASE_FINAL |
| 32 | +#define _H_GRB_BASE_FINAL |
| 33 | + |
| 34 | +#include <string> |
| 35 | + |
| 36 | +#include <graphblas/rc.hpp> |
| 37 | +#include <graphblas/ops.hpp> |
| 38 | +#include <graphblas/backends.hpp> |
| 39 | +#include <graphblas/descriptors.hpp> |
| 40 | + |
| 41 | + |
| 42 | +namespace grb::internal { |
| 43 | + |
| 44 | + /** |
| 45 | + * This class gathers raw functionalities that non-final backends cannot |
| 46 | + * implement directly because it is unaware whether final computations should |
| 47 | + * occur in parallel or not, while, if it should execute in parallel, it is |
| 48 | + * unaware which parallelisation scheme it should employ. |
| 49 | + * |
| 50 | + * The base implementation defines all functions every final backend should |
| 51 | + * implement and provides a sequential implementation for each such function. |
| 52 | + * Therefore, only parallel final backends should override this class. |
| 53 | + */ |
| 54 | + template< grb::Backend backend > |
| 55 | + class FinalBackend { |
| 56 | + |
| 57 | + public: |
| 58 | + |
| 59 | + /** |
| 60 | + * Provides a basic memory copy. |
| 61 | + * |
| 62 | + * @param[out] out The area where to write to. |
| 63 | + * @param[in] in The memory area to copy from. |
| 64 | + * @param[in] size How many bytes should be copied. |
| 65 | + * |
| 66 | + * The input and output memory areas are not allowed to overlap. |
| 67 | + */ |
| 68 | + static void memcpy( |
| 69 | + void * const __restrict__ out, |
| 70 | + const void * const __restrict__ in, |
| 71 | + const size_t size |
| 72 | + ) { |
| 73 | + (void) std::memcpy( out, in, size ); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Folds (reduces) every column of a matrix into a vector. |
| 78 | + * |
| 79 | + * @tparam descr The descriptor to be taken into account. |
| 80 | + * @tparam IOType The type of vector \em and matrix elements. |
| 81 | + * @tparam OP The operator used for reduction. |
| 82 | + * |
| 83 | + * @param[in,out] out The output vector. |
| 84 | + * |
| 85 | + * Pre-existing values in \a out are reduced into. |
| 86 | + * |
| 87 | + * @param[in] matrix The matrix which should be column-wise reduced into |
| 88 | + * \a out. |
| 89 | + * @param[in] cols The colums of \a matrix. |
| 90 | + * @param[in] rows The rows of \a matrix. |
| 91 | + * @param[in] skip Which column of \a matrix to skip. |
| 92 | + * |
| 93 | + * Taking \a skip higher or equal to \a cols will mean no column is |
| 94 | + * skipped. |
| 95 | + * |
| 96 | + * @param[in] op The operator by which to reduce. |
| 97 | + */ |
| 98 | + template< grb::Descriptor descr, typename IOType, typename OP > |
| 99 | + static void foldMatrixToVector( |
| 100 | + IOType * const __restrict__ out, |
| 101 | + const IOType * const __restrict__ matrix, |
| 102 | + const size_t cols, const size_t rows, |
| 103 | + const size_t skip, |
| 104 | + const OP &op |
| 105 | + ) { |
| 106 | + (void) op; |
| 107 | + if( skip >= cols ) { |
| 108 | + for( size_t j = 0; j < cols; ++j ) { |
| 109 | + OP::eWiseFoldlAA( out, matrix + j * rows, rows ); |
| 110 | + } |
| 111 | + } else { |
| 112 | + for( size_t j = 0; j < skip; ++j ) { |
| 113 | + OP::eWiseFoldlAA( out, matrix + j * rows, rows ); |
| 114 | + } |
| 115 | + for( size_t j = skip + 1; j < cols; ++j ) { |
| 116 | + OP::eWiseFoldlAA( out, matrix + j * rows, rows ); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + }; |
| 122 | + |
| 123 | +} |
| 124 | + |
| 125 | +#endif // end ``_H_GRB_BASE_FINAL´´ |
| 126 | + |
0 commit comments