Skip to content

Commit e2f3a0e

Browse files
author
Anonymous
authored
571 Generalize internal containers over backend (#117)
* Move is_a to structures.hpp * Add missing includes * Generalize StorageBasedMatrix over backend * Move friend functions of internal matrix-related classes to amf-base * Move Functor-based matrix code to a separate file * Move Storage-based matrix code to a separate file * Remove unnecessary forward declarations
1 parent 9a5efb0 commit e2f3a0e

File tree

7 files changed

+518
-374
lines changed

7 files changed

+518
-374
lines changed
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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''

include/alp/amf-based/matrix.hpp

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
#include <alp/density.hpp>
4646
#include <alp/views.hpp>
4747
#include <alp/imf.hpp>
48+
#include "functorbasedmatrix.hpp"
49+
#include "storagebasedmatrix.hpp"
4850

4951
namespace alp {
5052

@@ -58,13 +60,6 @@ namespace alp {
5860
// Matrix-related implementation
5961

6062
namespace internal {
61-
/** Forward declaration */
62-
template< typename T, typename AmfType, bool requires_allocation >
63-
class StorageBasedMatrix;
64-
65-
/** Forward declaration */
66-
template< typename T, typename ImfR, typename ImfC, typename DataLambdaType >
67-
class FunctorBasedMatrix;
6863

6964
/** Forward declaration */
7065
template< typename DerivedMatrix >
@@ -108,13 +103,24 @@ namespace alp {
108103
>
109104
typename MatrixType::storage_index_type getStorageIndex( const MatrixType &A, const size_t i, const size_t j, const size_t s = 0, const size_t P = 1 );
110105

111-
/** Returns the reference to the AMF of a storage-based matrix */
106+
template< typename DerivedMatrix >
107+
std::pair< size_t, size_t > dims( const MatrixBase< DerivedMatrix > & A ) noexcept;
108+
112109
template<
113110
typename MatrixType,
114-
std::enable_if< internal::is_storage_based< MatrixType >::value > * = nullptr
111+
std::enable_if_t< internal::is_storage_based< MatrixType >::value > * = nullptr
115112
>
116-
const typename MatrixType::amf_type &getAmf( const MatrixType &A ) noexcept;
113+
size_t getStorageDimensions( const MatrixType &A ) noexcept;
117114

115+
template< typename MatrixType,
116+
std::enable_if_t< is_matrix< MatrixType>::value > * = nullptr
117+
>
118+
bool getInitialized( const MatrixType &A ) noexcept;
119+
120+
template< typename MatrixType,
121+
std::enable_if_t< is_matrix< MatrixType>::value > * = nullptr
122+
>
123+
void setInitialized( MatrixType &, const bool ) noexcept;
118124
/**
119125
* Base Matrix class containing attributes common to all Matrix specialization
120126
*/
@@ -314,7 +320,8 @@ namespace alp {
314320
internal::FunctorBasedMatrix< T, ImfR, ImfC, typename View::applied_to >,
315321
internal::StorageBasedMatrix< T,
316322
typename internal::determine_amf_type< Structure, View, ImfR, ImfC, backend >::type,
317-
internal::requires_allocation< View >::value
323+
internal::requires_allocation< View >::value,
324+
backend
318325
>
319326
>::type type;
320327
};
@@ -687,23 +694,6 @@ namespace alp {
687694

688695
namespace structures {
689696

690-
/**
691-
* @brief Checks if TestedStructure is a \a Structure according to the ALP's structure classification.
692-
*
693-
* @tparam TestedStructure The structure to be tested.
694-
* @tparam Structure The structure that should be implied by \a TestedStructure.
695-
*/
696-
template< typename TestedStructure, typename Structure >
697-
struct is_a {
698-
699-
static_assert( std::is_base_of< structures::BaseStructure, TestedStructure >::value );
700-
701-
/**
702-
* \a value is true iff \a Structure is implied by \a TestedStructure.
703-
*/
704-
static constexpr bool value = is_in< Structure, typename TestedStructure::inferred_structures >::value;
705-
};
706-
707697
/**
708698
* Calculates the iteration space for row-dimension for the given matrix and band index.
709699
*
@@ -1258,15 +1248,6 @@ namespace alp {
12581248
template< typename MatrixType >
12591249
std::pair< size_t, size_t > getCoords( const MatrixType &A, const size_t storageIndex, const size_t s, const size_t P );
12601250

1261-
/** Get the reference to the AMF of a storage-based matrix */
1262-
template<
1263-
typename MatrixType,
1264-
std::enable_if< internal::is_storage_based< MatrixType >::value > * = nullptr
1265-
>
1266-
const typename MatrixType::amf_type &getAmf( const MatrixType &A ) noexcept {
1267-
return A.getAmf();
1268-
}
1269-
12701251
} // namespace internal
12711252

12721253
template< typename D, typename Structure, typename View, typename ImfR, typename ImfC, enum Backend backend >
@@ -1285,15 +1266,6 @@ namespace alp {
12851266
typename Matrix< D, Structure, Density::Dense, View, ImfR, ImfC, backend >::base_type > & > ( A ) );
12861267
}
12871268

1288-
template<
1289-
typename MatrixType,
1290-
std::enable_if< internal::is_storage_based< MatrixType >::value > * = nullptr
1291-
>
1292-
size_t internal::getStorageDimensions( const MatrixType &A ) noexcept {
1293-
static_assert( is_storage_based< MatrixType >::value, "getStorageDimensions supported only for storage-based containers.");
1294-
return static_cast< const typename MatrixType::base_type & >( A ).getStorageDimensions();
1295-
}
1296-
12971269
namespace structures {
12981270

12991271
template<

0 commit comments

Comments
 (0)