ALP User Documentation 0.7.alpha
Algebraic Programming User Documentation
Loading...
Searching...
No Matches
mpv.hpp
Go to the documentation of this file.
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
27#ifndef _H_GRB_ALGORITHMS_MPV
28#define _H_GRB_ALGORITHMS_MPV
29
30#include <graphblas.hpp>
31
32
33namespace grb {
34
35 namespace algorithms {
36
93 template< Descriptor descr, class Ring, typename IOType, typename InputType >
96 const Matrix< InputType > &A, const size_t k,
97 const Vector< IOType > &v,
98 Vector< IOType > &temp,
99 const Ring &ring
100 ) {
101 static_assert( !(descr & descriptors::no_casting) ||
102 (std::is_same< IOType, typename Ring::D4 >::value &&
103 std::is_same< InputType, typename Ring::D2 >::value &&
104 std::is_same< IOType, typename Ring::D1 >::value &&
105 std::is_same< IOType, typename Ring::D3 >::value
106 ),
107 "grb::mpv : some containers were passed with element types that do not"
108 "match the given semiring domains."
109 );
110
111 // runtime check
112 const size_t n = nrows( A );
113 if( n != ncols( A ) ) {
114 return ILLEGAL;
115 }
116 if( size( u ) != n || n != size( v ) ) {
117 return MISMATCH;
118 }
119 if( size( temp ) != n ) {
120 return MISMATCH;
121 }
122 if( capacity( u ) != n ) {
123 return ILLEGAL;
124 }
125 if( capacity( temp ) != n ) {
126 return ILLEGAL;
127 }
128 // catch trivial case
129 if( k == 0 ) {
130 return set< descr >( u, v );
131 }
132 // otherwise, do at least one multiplication
133#ifdef _DEBUG
134 std::cout << "init: input vector nonzeroes is " << grb::nnz( v ) << ".\n";
135#endif
136 RC ret = mxv< descr >( u, A, v, ring );
137 if( k == 1 ) {
138 return ret;
139 }
140 // do any remaining multiplications using a temporary output vector
141 bool copy;
142 ret = ret ? ret : clear( temp );
143 for( size_t iterate = 1; ret == SUCCESS && iterate < k; iterate += 2 ) {
144 // multiply with output into temporary
145 copy = true;
146#ifdef _DEBUG
147 std::cout << "up: input vector nonzeroes is " << grb::nnz( u ) << "\n";
148#endif
149 ret = mxv< descr >( temp, A, u, ring );
150 // check if this was the final multiplication
151 assert( iterate <= k );
152 if( iterate + 1 == k || ret != SUCCESS ) {
153 break;
154 }
155 // multiply with output into u
156 copy = false;
157#ifdef _DEBUG
158 std::cout << "down: input vector nonzeroes is " << grb::nnz( temp ) << "\n";
159#endif
160 ret = mxv< descr >( u, A, temp, ring );
161 }
162
163 // swap u and temp, if required
164 if( ret == SUCCESS && copy ) {
165 std::swap( u, temp );
166 }
167
168 // done
169 return ret;
170 }
171
172 } // namespace algorithms
173
174} // namespace grb
175
176#endif // end ``_H_GRB_ALGORITHMS_MPV''
177
An ALP/GraphBLAS matrix.
Definition: matrix.hpp:71
A GraphBLAS vector.
Definition: vector.hpp:64
The main header to include in order to use the ALP/GraphBLAS API.
size_t capacity(const Vector< InputType, backend, Coords > &x) noexcept
Queries the capacity of the given ALP/GraphBLAS container.
Definition: io.hpp:388
size_t nrows(const Matrix< InputType, backend, RIT, CIT, NIT > &A) noexcept
Requests the row size of a given matrix.
Definition: io.hpp:286
size_t nnz(const Vector< DataType, backend, Coords > &x) noexcept
Request the number of nonzeroes in a given vector.
Definition: io.hpp:479
size_t ncols(const Matrix< InputType, backend, RIT, CIT, NIT > &A) noexcept
Requests the column size of a given matrix.
Definition: io.hpp:339
RC clear(Vector< DataType, backend, Coords > &x) noexcept
Clears a given vector of all nonzeroes.
Definition: io.hpp:574
size_t size(const Vector< DataType, backend, Coords > &x) noexcept
Request the size of a given vector.
Definition: io.hpp:235
RC mpv(Vector< IOType > &u, const Matrix< InputType > &A, const size_t k, const Vector< IOType > &v, Vector< IOType > &temp, const Ring &ring)
The matrix powers kernel.
Definition: mpv.hpp:94
static constexpr Descriptor no_casting
Disallows the standard casting of input parameters to a compatible domain in case they did not match ...
Definition: descriptors.hpp:196
The ALP/GraphBLAS namespace.
Definition: graphblas.hpp:450
RC
Return codes of ALP primitives.
Definition: rc.hpp:47
@ ILLEGAL
A call to a primitive has determined that one of its arguments was illegal as per the specification o...
Definition: rc.hpp:143
@ MISMATCH
One or more of the ALP/GraphBLAS objects passed to the primitive that returned this error have mismat...
Definition: rc.hpp:90
@ SUCCESS
Indicates the primitive has executed successfully.
Definition: rc.hpp:54