ALP User Documentation  0.8.preview
Algebraic Programming User Documentation
knn.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_KNN
28 #define _H_GRB_KNN
29 
31 
32 #include <graphblas.hpp>
33 
34 
35 namespace grb {
36 
37  namespace algorithms {
38 
81  template< Descriptor descr, typename OutputType, typename InputType >
84  const size_t source, const size_t k,
85  Vector< bool > &buf1
86  ) {
87  // the nearest-neighbourhood ring
88  Semiring<
91  > ring;
92 
93  // check input
94  const size_t n = nrows( A );
95  if( n != ncols( A ) ) {
96  return MISMATCH;
97  }
98  if( size( buf1 ) != n ) {
99  return MISMATCH;
100  }
101  if( size( u ) != n ) {
102  return MISMATCH;
103  }
104  if( capacity( u ) != n ) {
105  return ILLEGAL;
106  }
107  if( capacity( buf1 ) != n ) {
108  return ILLEGAL;
109  }
110 
111  // prepare
112  RC ret = SUCCESS;
113  if( nnz( u ) != 0 ) {
114  ret = clear( u );
115  }
116  if( nnz( buf1 ) != 0 ) {
117  ret = ret ? ret : clear( buf1 );
118  }
119 #ifdef _DEBUG
120  std::cout << "grb::algorithms::knn called with source " << source << " "
121  << "and k " << k << ".\n";
122 #endif
123  ret = ret ? ret : setElement( buf1, true, source );
124 
125  // do sparse matrix powers on the given ring
126  if( ret == SUCCESS ) {
127  if( descr & descriptors::transpose_matrix ) {
128  ret = mpv< (descr | descriptors::add_identity) &
130  >( u, A, k, buf1, buf1, ring );
131  } else {
132  ret = mpv< descr | descriptors::add_identity |
134  >( u, A, k, buf1, buf1, ring );
135  }
136  }
137 
138  // done
139  return ret;
140  }
141 
142  } // namespace algorithms
143 
144 } // namespace grb
145 
146 #endif
147 
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
A call to a primitive has determined that one of its arguments was illegal as per the specification o...
Definition: rc.hpp:143
An ALP/GraphBLAS matrix.
Definition: matrix.hpp:72
RC
Return codes of ALP primitives.
Definition: rc.hpp:47
RC knn(Vector< OutputType > &u, const Matrix< InputType > &A, const size_t source, const size_t k, Vector< bool > &buf1)
Given a graph and a source vertex, indicates which vertices are contained within k hops.
Definition: knn.hpp:82
A GraphBLAS vector.
Definition: vector.hpp:64
static constexpr Descriptor add_identity
For any call to a matrix computation, the input matrix A is instead interpreted as ,...
Definition: descriptors.hpp:159
static constexpr Descriptor transpose_matrix
Transposes the input matrix prior to applying it.
Definition: descriptors.hpp:71
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 nrows(const Matrix< InputType, backend, RIT, CIT, NIT > &A) noexcept
Requests the row size of a given matrix.
Definition: io.hpp:286
Standard identitity for the logical or operator.
Definition: identities.hpp:151
Implements the matrix powers kernel over arbitrary semirings.
size_t ncols(const Matrix< InputType, backend, RIT, CIT, NIT > &A) noexcept
Requests the column size of a given matrix.
Definition: io.hpp:339
Standard identity for the logical AND operator.
Definition: identities.hpp:178
The ALP/GraphBLAS namespace.
Definition: graphblas.hpp:477
The main header to include in order to use the ALP/GraphBLAS API.
size_t size(const Vector< DataType, backend, Coords > &x) noexcept
Request the size of a given vector.
Definition: io.hpp:235
The logical and.
Definition: ops.hpp:490
Indicates the primitive has executed successfully.
Definition: rc.hpp:54
size_t capacity(const Vector< InputType, backend, Coords > &x) noexcept
Queries the capacity of the given ALP/GraphBLAS container.
Definition: io.hpp:388
The logical or.
Definition: ops.hpp:462
A generalised semiring.
Definition: semiring.hpp:190
RC setElement(Vector< DataType, backend, Coords > &x, const T val, const size_t i, const Phase &phase=EXECUTE, const typename std::enable_if< !grb::is_object< DataType >::value &&!grb::is_object< T >::value, void >::type *const =nullptr)
Sets the element of a given vector at a given position to a given value.
Definition: io.hpp:1129
RC clear(Vector< DataType, backend, Coords > &x) noexcept
Clears a given vector of all nonzeroes.
Definition: io.hpp:574
One or more of the ALP/GraphBLAS objects passed to the primitive that returned this error have mismat...
Definition: rc.hpp:90