|
| 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 | +#ifndef _H_ALP_AMF_BASED_FUNCTORBASEDMATRIX |
| 19 | +#define _H_ALP_AMF_BASED_FUNCTORBASEDMATRIX |
| 20 | + |
| 21 | +#include <functional> |
| 22 | + |
| 23 | +#include <alp/backends.hpp> |
| 24 | +#include <alp/base/matrix.hpp> |
| 25 | +#include <alp/config.hpp> |
| 26 | +#include <alp/type_traits.hpp> |
| 27 | +#include <alp/utils.hpp> |
| 28 | +#include <alp/imf.hpp> |
| 29 | + |
| 30 | +namespace alp { |
| 31 | + |
| 32 | + namespace internal { |
| 33 | + |
| 34 | + /** Forward declaration */ |
| 35 | + template< typename DerivedMatrix > |
| 36 | + class MatrixBase; |
| 37 | + |
| 38 | + /** Forward declaration */ |
| 39 | + template< typename T, typename ImfR, typename ImfC, typename DataLambdaType > |
| 40 | + class FunctorBasedMatrix; |
| 41 | + |
| 42 | + /** Functor reference getter used by friend functions of specialized Matrix */ |
| 43 | + template< typename T, typename ImfR, typename ImfC, typename DataLambdaType > |
| 44 | + const typename FunctorBasedMatrix< T, ImfR, ImfC, DataLambdaType >::functor_type &getFunctor( const FunctorBasedMatrix< T, ImfR, ImfC, DataLambdaType > &A ); |
| 45 | + |
| 46 | + /** |
| 47 | + * Getter for the functor of a functor-based matrix. |
| 48 | + * |
| 49 | + * @tparam MatrixType The type of input matrix. |
| 50 | + * |
| 51 | + * @param[in] A Input matrix. |
| 52 | + * |
| 53 | + * @returns A constant reference to a functor object within the |
| 54 | + * provided functor-based matrix. |
| 55 | + */ |
| 56 | + template< |
| 57 | + typename MatrixType, |
| 58 | + std::enable_if_t< |
| 59 | + internal::is_functor_based< MatrixType >::value |
| 60 | + > * = nullptr |
| 61 | + > |
| 62 | + const typename MatrixType::functor_type &getFunctor( const MatrixType &A ) { |
| 63 | + return static_cast< const typename MatrixType::base_type & >( A ).getFunctor(); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Specialization of MatrixReference with a lambda function as a target. |
| 68 | + * Used as a result of low-rank operation to avoid the need for allocating a container. |
| 69 | + * The data is produced lazily by invoking the lambda function stored as a part of this object. |
| 70 | + * |
| 71 | + * \note Views-over-lambda-functions types are used internally as results of low-rank operations and are not |
| 72 | + * directly exposed to users. From the users perspective, the use of objects of this type does not differ |
| 73 | + * from the use of other \a alp::Matrix types. The difference lies in a lazy implementation of the access |
| 74 | + * to matrix elements, which is not exposed to the user. |
| 75 | + * |
| 76 | + */ |
| 77 | + template< typename T, typename ImfR, typename ImfC, typename DataLambdaType > |
| 78 | + class FunctorBasedMatrix : public MatrixBase< FunctorBasedMatrix< T, ImfR, ImfC, DataLambdaType > > { |
| 79 | + public: |
| 80 | + |
| 81 | + /** Expose static properties */ |
| 82 | + typedef T value_type; |
| 83 | + /** Type returned by access function */ |
| 84 | + typedef T access_type; |
| 85 | + /** Type of the index used to access the physical storage */ |
| 86 | + typedef std::pair< size_t, size_t > storage_index_type; |
| 87 | + |
| 88 | + protected: |
| 89 | + |
| 90 | + typedef FunctorBasedMatrix< T, ImfR, ImfC, DataLambdaType > self_type; |
| 91 | + friend MatrixBase< self_type >; |
| 92 | + |
| 93 | + typedef std::function< bool() > initialized_functor_type; |
| 94 | + const initialized_functor_type initialized_lambda; |
| 95 | + |
| 96 | + const ImfR imf_r; |
| 97 | + const ImfC imf_c; |
| 98 | + |
| 99 | + const DataLambdaType data_lambda; |
| 100 | + |
| 101 | + std::pair< size_t, size_t > dims() const noexcept { |
| 102 | + return std::make_pair( imf_r.n, imf_c.n ); |
| 103 | + } |
| 104 | + |
| 105 | + const DataLambdaType &getFunctor() const noexcept { |
| 106 | + return data_lambda; |
| 107 | + } |
| 108 | + |
| 109 | + bool getInitialized() const noexcept { |
| 110 | + return initialized_lambda(); |
| 111 | + } |
| 112 | + |
| 113 | + void setInitialized( const bool ) noexcept { |
| 114 | + static_assert( "Calling setInitialized on a FunctorBasedMatrix is not allowed." ); |
| 115 | + } |
| 116 | + |
| 117 | + access_type access( const storage_index_type &storage_index ) const { |
| 118 | + T result = 0; |
| 119 | + data_lambda( result, imf_r.map( storage_index.first ), imf_c.map( storage_index.second ) ); |
| 120 | + return static_cast< access_type >( result ); |
| 121 | + } |
| 122 | + |
| 123 | + storage_index_type getStorageIndex( const size_t i, const size_t j, const size_t s, const size_t P ) const { |
| 124 | + (void)s; |
| 125 | + (void)P; |
| 126 | + return std::make_pair( i, j ); |
| 127 | + } |
| 128 | + |
| 129 | + public: |
| 130 | + |
| 131 | + FunctorBasedMatrix( |
| 132 | + initialized_functor_type initialized_lambda, |
| 133 | + ImfR imf_r, |
| 134 | + ImfC imf_c, |
| 135 | + const DataLambdaType data_lambda |
| 136 | + ) : |
| 137 | + initialized_lambda( initialized_lambda ), |
| 138 | + imf_r( imf_r ), |
| 139 | + imf_c( imf_c ), |
| 140 | + data_lambda( data_lambda ) {} |
| 141 | + |
| 142 | + }; // class FunctorBasedMatrix |
| 143 | + |
| 144 | + } // namespace internal |
| 145 | + |
| 146 | +} // namespace alp |
| 147 | + |
| 148 | +#endif // end ``_H_ALP_AMF_BASED_FUNCTORBASEDMATRIX'' |
0 commit comments