ALP User Documentation 0.7.0
Algebraic Programming User Documentation
semiring.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_SEMIRING
28#define _H_GRB_SEMIRING
29
31#include <graphblas/monoid.hpp>
32#include <graphblas/ops.hpp>
33
34
35namespace grb {
36
185 template< class _OP1, class _OP2, template< typename > class _ID1, template< typename > class _ID2 >
186 class Semiring {
187
188 static_assert( std::is_same< typename _OP2::D3, typename _OP1::D1 >::value,
189 "The multiplicative output type must match the left-hand additive "
190 "input type" );
191
192 static_assert( std::is_same< typename _OP1::D2, typename _OP1::D3 >::value,
193 "The right-hand input type of the additive operator must match its "
194 "output type" );
195
197 "Cannot construct a semiring using a non-associative additive "
198 "operator" );
199
201 "Cannot construct a semiring using a non-associative multiplicative "
202 "operator" );
203
205 "Cannot construct a semiring using a non-commutative additive "
206 "operator" );
207
208 public:
210 typedef typename _OP2::D1 D1;
211
213 typedef typename _OP2::D2 D2;
214
219 typedef typename _OP2::D3 D3;
220
225 typedef typename _OP1::D2 D4;
226
228 typedef _OP1 AdditiveOperator;
229
232
235
238
240 template< typename ZeroType >
241 using Zero = _ID1< ZeroType >;
242
244 template< typename OneType >
245 using One = _ID2< OneType >;
246
247 private:
248 static constexpr size_t D1_bsz = grb::config::SIMD_BLOCKSIZE< D1 >::value();
249 static constexpr size_t D2_bsz = grb::config::SIMD_BLOCKSIZE< D2 >::value();
250 static constexpr size_t D3_bsz = grb::config::SIMD_BLOCKSIZE< D3 >::value();
251 static constexpr size_t D4_bsz = grb::config::SIMD_BLOCKSIZE< D4 >::value();
252 static constexpr size_t mul_input_bsz = D1_bsz < D2_bsz ? D1_bsz : D2_bsz;
253
255 AdditiveMonoid additiveMonoid;
256
258 MultiplicativeMonoid multiplicativeMonoid;
259
260 public:
262 static constexpr size_t blocksize_add = D3_bsz < D4_bsz ? D3_bsz : D4_bsz;
263
265 static constexpr size_t blocksize_mul = mul_input_bsz < D3_bsz ? mul_input_bsz : D3_bsz;
266
269
282 template< typename D >
283 constexpr D getZero() const {
284 return additiveMonoid.template getIdentity< D >();
285 }
286
298 template< typename D >
299 constexpr D getOne() const {
300 return multiplicativeMonoid.template getIdentity< D >();
301 }
302
309 return additiveMonoid;
310 }
311
318 return multiplicativeMonoid;
319 }
320
327 return additiveMonoid.getOperator();
328 }
329
336 return multiplicativeMonoid.getOperator();
337 }
338 };
339
340 // overload for GraphBLAS type traits.
341 template< class _OP1, class _OP2, template< typename > class _ID1, template< typename > class _ID2 >
342 struct is_semiring< Semiring< _OP1, _OP2, _ID1, _ID2 > > {
344 static const constexpr bool value = true;
345 };
346
347 template< class _OP1, class _OP2, template< typename > class _ID1, template< typename > class _ID2 >
348 struct has_immutable_nonzeroes< Semiring< _OP1, _OP2, _ID1, _ID2 > > {
350 std::is_same< _OP1, typename grb::operators::logical_or< typename _OP1::D1, typename _OP1::D2, typename _OP1::D3 > >::value;
351 };
352
353} // namespace grb
354
355#endif
356
Operator getOperator() const
Retrieves the underlying operator.
Definition: monoid.hpp:120
A generalised semiring.
Definition: semiring.hpp:186
static constexpr size_t blocksize_mul
Blocksize for element-wise multiplication.
Definition: semiring.hpp:265
_OP2::D2 D2
The second input domain of the multiplicative operator.
Definition: semiring.hpp:213
_OP2::D3 D3
The output domain of the multiplicative operator.
Definition: semiring.hpp:219
static constexpr size_t blocksize
Blocksize for element-wise multiply-adds.
Definition: semiring.hpp:268
constexpr D getZero() const
Retrieves the zero corresponding to this semiring.
Definition: semiring.hpp:283
Monoid< _OP2, _ID2 > MultiplicativeMonoid
The multiplicative monoid type.
Definition: semiring.hpp:237
_OP1::D2 D4
The second input domain of the additive operator.
Definition: semiring.hpp:225
AdditiveMonoid getAdditiveMonoid() const
Retrieves the underlying additive monoid.
Definition: semiring.hpp:308
MultiplicativeOperator getMultiplicativeOperator() const
Retrieves the underlying multiplicative operator.
Definition: semiring.hpp:335
AdditiveOperator getAdditiveOperator() const
Retrieves the underlying additive operator.
Definition: semiring.hpp:326
_OP2::D1 D1
The first input domain of the multiplicative operator.
Definition: semiring.hpp:190
_OP2 MultiplicativeOperator
The multiplicative operator type.
Definition: semiring.hpp:231
constexpr D getOne() const
Sets the given value equal to one, corresponding to this semiring.
Definition: semiring.hpp:299
_ID1< ZeroType > Zero
The identity under addition.
Definition: semiring.hpp:241
Monoid< _OP1, _ID1 > AdditiveMonoid
The additive monoid type.
Definition: semiring.hpp:234
MultiplicativeMonoid getMultiplicativeMonoid() const
Retrieves the underlying multiplicative monoid.
Definition: semiring.hpp:317
_ID2< OneType > One
The identity under multiplication.
Definition: semiring.hpp:245
static constexpr size_t blocksize_add
Blocksize for element-wise addition.
Definition: semiring.hpp:262
_OP1 AdditiveOperator
The additive operator type.
Definition: semiring.hpp:228
Provides a set of standard identities for use with ALP.
Provides an ALP monoid.
The ALP/GraphBLAS namespace.
Definition: graphblas.hpp:452
Provides a set of standard binary operators.
static const constexpr bool value
Whether T a semiring where nonzeroes are immutable.
Definition: type_traits.hpp:286
Used to inspect whether a given operator or monoid is associative.
Definition: type_traits.hpp:202
Used to inspect whether a given operator or monoid is commutative.
Definition: type_traits.hpp:240
Used to inspect whether a given type is an ALP semiring.
Definition: type_traits.hpp:66
static const constexpr bool value
Whether T is an ALP semiring.
Definition: type_traits.hpp:73