ALP User Documentation  0.8.preview
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 
30 #include <graphblas/identities.hpp>
31 #include <graphblas/monoid.hpp>
32 #include <graphblas/ops.hpp>
33 
34 
35 namespace grb {
36 
185  template<
186  class _OP1, class _OP2,
187  template< typename > class _ID1,
188  template< typename > class _ID2
189  >
190  class Semiring {
191 
192  static_assert( std::is_same< typename _OP2::D3, typename _OP1::D1 >::value,
193  "The multiplicative output type must match the left-hand additive "
194  "input type" );
195 
196  static_assert( std::is_same< typename _OP1::D2, typename _OP1::D3 >::value,
197  "The right-hand input type of the additive operator must match its "
198  "output type" );
199 
200  static_assert( grb::is_associative< _OP1 >::value,
201  "Cannot construct a semiring using a non-associative additive "
202  "operator" );
203 
204  static_assert( grb::is_associative< _OP2 >::value,
205  "Cannot construct a semiring using a non-associative multiplicative "
206  "operator" );
207 
208  static_assert( grb::is_commutative< _OP1 >::value,
209  "Cannot construct a semiring using a non-commutative additive "
210  "operator" );
211 
212  public:
213 
215  typedef typename _OP2::D1 D1;
216 
218  typedef typename _OP2::D2 D2;
219 
224  typedef typename _OP2::D3 D3;
225 
230  typedef typename _OP1::D2 D4;
231 
233  typedef _OP1 AdditiveOperator;
234 
237 
240 
243 
245  template< typename ZeroType >
246  using Zero = _ID1< ZeroType >;
247 
249  template< typename OneType >
250  using One = _ID2< OneType >;
251 
252 
253  private:
254 
255  static constexpr size_t D1_bsz = grb::config::SIMD_BLOCKSIZE< D1 >::value();
256  static constexpr size_t D2_bsz = grb::config::SIMD_BLOCKSIZE< D2 >::value();
257  static constexpr size_t D3_bsz = grb::config::SIMD_BLOCKSIZE< D3 >::value();
258  static constexpr size_t D4_bsz = grb::config::SIMD_BLOCKSIZE< D4 >::value();
259  static constexpr size_t mul_input_bsz = D1_bsz < D2_bsz ? D1_bsz : D2_bsz;
260 
262  AdditiveMonoid additiveMonoid;
263 
265  MultiplicativeMonoid multiplicativeMonoid;
266 
267 
268  public:
269 
271  static constexpr size_t blocksize_add = D3_bsz < D4_bsz
272  ? D3_bsz
273  : D4_bsz;
274 
276  static constexpr size_t blocksize_mul = mul_input_bsz < D3_bsz
277  ? mul_input_bsz
278  : D3_bsz;
279 
281  static constexpr size_t blocksize = blocksize_mul < blocksize_add
282  ? blocksize_mul
283  : blocksize_add;
284 
297  template< typename D >
298  D getZero() const {
299  return additiveMonoid.template getIdentity< D >();
300  }
301 
313  template< typename D >
314  D getOne() const {
315  return multiplicativeMonoid.template getIdentity< D >();
316  }
317 
324  return additiveMonoid;
325  }
326 
333  return multiplicativeMonoid;
334  }
335 
342  return additiveMonoid.getOperator();
343  }
344 
351  return multiplicativeMonoid.getOperator();
352  }
353 
354  };
355 
356  // overload for GraphBLAS type traits.
357  template<
358  class _OP1, class _OP2,
359  template< typename > class _ID1,
360  template< typename > class _ID2
361  >
362  struct is_semiring<
363  Semiring< _OP1, _OP2, _ID1, _ID2 >
364  > {
366  static const constexpr bool value = true;
367  };
368 
369  template<
370  class _OP1, class _OP2,
371  template< typename > class _ID1,
372  template< typename > class _ID2
373  >
374  struct has_immutable_nonzeroes<
375  Semiring< _OP1, _OP2, _ID1, _ID2 >
376  > {
377  static const constexpr bool value = grb::is_semiring<
378  Semiring< _OP1, _OP2, _ID1, _ID2 > >::value &&
379  std::is_same<
380  _OP1, typename grb::operators::logical_or< typename _OP1::D1,
381  typename _OP1::D2, typename _OP1::D3
382  > >::value;
383 
384  };
385 
386 } // namespace grb
387 
388 #endif
389 
Used to inspect whether a given operator or monoid is commutative.
Definition: type_traits.hpp:240
static constexpr size_t blocksize_add
Blocksize for element-wise addition.
Definition: semiring.hpp:271
Used to inspect whether a given operator or monoid is associative.
Definition: type_traits.hpp:202
_OP1::D2 D4
The second input domain of the additive operator.
Definition: semiring.hpp:230
static const constexpr bool value
Whether T is an ALP semiring.
Definition: type_traits.hpp:73
AdditiveOperator getAdditiveOperator() const
Retrieves the underlying additive operator.
Definition: semiring.hpp:341
MultiplicativeOperator getMultiplicativeOperator() const
Retrieves the underlying multiplicative operator.
Definition: semiring.hpp:350
MultiplicativeMonoid getMultiplicativeMonoid() const
Retrieves the underlying multiplicative monoid.
Definition: semiring.hpp:332
Used to inspect whether a given type is an ALP semiring.
Definition: type_traits.hpp:66
_ID1< ZeroType > Zero
The identity under addition.
Definition: semiring.hpp:246
_OP2::D3 D3
The output domain of the multiplicative operator.
Definition: semiring.hpp:224
AdditiveMonoid getAdditiveMonoid() const
Retrieves the underlying additive monoid.
Definition: semiring.hpp:323
Provides a set of standard identities for use with ALP.
D getZero() const
Retrieves the zero corresponding to this semiring.
Definition: semiring.hpp:298
_OP2 MultiplicativeOperator
The multiplicative operator type.
Definition: semiring.hpp:236
_OP1 AdditiveOperator
The additive operator type.
Definition: semiring.hpp:233
Monoid< _OP2, _ID2 > MultiplicativeMonoid
The multiplicative monoid type.
Definition: semiring.hpp:242
Operator getOperator() const
Retrieves the underlying operator.
Definition: monoid.hpp:120
_OP2::D1 D1
The first input domain of the multiplicative operator.
Definition: semiring.hpp:194
static constexpr size_t blocksize
Blocksize for element-wise multiply-adds.
Definition: semiring.hpp:281
Monoid< _OP1, _ID1 > AdditiveMonoid
The additive monoid type.
Definition: semiring.hpp:239
The ALP/GraphBLAS namespace.
Definition: graphblas.hpp:477
D getOne() const
Sets the given value equal to one, corresponding to this semiring.
Definition: semiring.hpp:314
_OP2::D2 D2
The second input domain of the multiplicative operator.
Definition: semiring.hpp:218
The logical or.
Definition: ops.hpp:462
A generalised semiring.
Definition: semiring.hpp:190
_ID2< OneType > One
The identity under multiplication.
Definition: semiring.hpp:250
static const constexpr bool value
Whether T a semiring where nonzeroes are immutable.
Definition: type_traits.hpp:286
Provides a set of standard binary operators.
static constexpr size_t blocksize_mul
Blocksize for element-wise multiplication.
Definition: semiring.hpp:276
Provides an ALP monoid.