ALP User Documentation 0.7.0
Algebraic Programming User Documentation
ops.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_OPERATORS
28#define _H_GRB_OPERATORS
29
30#include "internalops.hpp"
31#include "type_traits.hpp"
32
33
34namespace grb {
35
40 namespace operators {
41
54 template<
55 typename D1, typename D2 = D1, typename D3 = D2,
56 enum Backend implementation = config::default_backend
57 >
59 public internal::Operator<
60 internal::left_assign< D1, D2, D3, implementation >
61 > {
62
63 public:
64
65 template< typename A, typename B, typename C, enum Backend D >
67
68 left_assign() {}
69
70 };
71
81 template<
82 typename D1, typename D2 = D1, typename D3 = D2,
83 enum Backend implementation = config::default_backend
84 >
86 public internal::Operator<
87 internal::left_assign_if< D1, D2, D3, implementation >
88 > {
89
90 public:
91
92 template< typename A, typename B, typename C, enum Backend D >
94
96
97 };
98
111 template<
112 typename D1, typename D2 = D1, typename D3 = D2,
113 enum Backend implementation = config::default_backend
114 >
115 class right_assign : public internal::Operator<
116 internal::right_assign< D1, D2, D3, implementation >
117 > {
118
119 public:
120
121 template< typename A, typename B, typename C, enum Backend D >
123
124 right_assign() {}
125
126 };
127
137 template<
138 typename D1, typename D2 = D1, typename D3 = D2,
139 enum Backend implementation = config::default_backend
140 >
141 class right_assign_if : public internal::Operator<
142 internal::right_assign_if< D1, D2, D3, implementation >
143 > {
144
145 public:
146
147 template< typename A, typename B, typename C, enum Backend D >
149
150 right_assign_if() {}
151
152 };
153
170 // [Operator Wrapping]
171 template<
172 typename D1, typename D2 = D1, typename D3 = D2,
173 enum Backend implementation = config::default_backend
174 >
175 class add : public internal::Operator<
176 internal::add< D1, D2, D3, implementation >
177 > {
178
179 public:
180
181 template< typename A, typename B, typename C, enum Backend D >
183
184 add() {}
185 };
186 // [Operator Wrapping]
187
204 template<
205 typename D1, typename D2 = D1, typename D3 = D2,
206 enum Backend implementation = config::default_backend
207 >
208 class mul : public internal::Operator<
209 internal::mul< D1, D2, D3, implementation >
210 > {
211
212 public:
213
214 template< typename A, typename B, typename C, enum Backend D >
216
217 mul() {}
218 };
219
237 template<
238 typename D1, typename D2 = D1, typename D3 = D2,
239 enum Backend implementation = config::default_backend
240 >
241 class max : public internal::Operator<
242 internal::max< D1, D2, D3, implementation >
243 > {
244
245 public:
246
247 template< typename A, typename B, typename C, enum Backend D >
249
250 max() {}
251 };
252
270 template<
271 typename D1, typename D2 = D1, typename D3 = D2,
272 enum Backend implementation = config::default_backend
273 >
274 class min : public internal::Operator<
275 internal::min< D1, D2, D3, implementation >
276 > {
277
278 public:
279
280 template< typename A, typename B, typename C, enum Backend D >
282
283 min() {}
284 };
285
297 template<
298 typename D1, typename D2 = D1, typename D3 = D2,
299 enum Backend implementation = config::default_backend
300 >
301 class subtract : public internal::Operator<
302 internal::substract< D1, D2, D3, implementation >
303 > {
304
305 public:
306
307 template< typename A, typename B, typename C, enum Backend D >
309
310 subtract() {}
311 };
312
324 template<
325 typename D1, typename D2 = D1, typename D3 = D2,
326 enum Backend implementation = config::default_backend
327 >
328 class divide : public internal::Operator<
329 internal::divide< D1, D2, D3, implementation >
330 > {
331
332 public:
333
334 template< typename A, typename B, typename C, enum Backend D >
336
337 divide() {}
338 };
339
349 template<
350 typename D1, typename D2 = D1, typename D3 = D2,
351 enum Backend implementation = config::default_backend
352 >
353 class divide_reverse : public internal::Operator<
354 internal::divide_reverse< D1, D2, D3, implementation >
355 > {
356
357 public:
358
359 template< typename A, typename B, typename C, enum Backend D >
361
362 divide_reverse() {}
363 };
364
375 template<
376 typename D1, typename D2 = D1, typename D3 = D2,
377 enum Backend implementation = config::default_backend
378 >
379 class equal : public internal::Operator<
380 internal::equal< D1, D2, D3, implementation >
381 > {
382
383 public:
384
385 template< typename A, typename B, typename C, enum Backend D >
387
388 equal() {}
389 };
390
401 template<
402 typename D1, typename D2 = D1, typename D3 = D2,
403 enum Backend implementation = config::default_backend
404 >
405 class not_equal : public internal::Operator<
406 internal::not_equal< D1, D2, D3, implementation >
407 > {
408
409 public:
410
411 template< typename A, typename B, typename C, enum Backend D >
413
414 not_equal() {}
415 };
416
430 template<
431 typename D1, typename D2 = D1, typename D3 = D2,
432 enum Backend implementation = config::default_backend
433 >
434 class any_or : public internal::Operator<
435 internal::any_or< D1, D2, D3, implementation >
436 > {
437
438 public:
439
440 template< typename A, typename B, typename C, enum Backend D >
442
443 any_or() {}
444 };
445
458 template<
459 typename D1, typename D2 = D1, typename D3 = D2,
460 enum Backend implementation = config::default_backend
461 >
462 class logical_or : public internal::Operator<
463 internal::logical_or< D1, D2, D3, implementation >
464 > {
465
466 public:
467
468 template< typename A, typename B, typename C, enum Backend D >
470
471 logical_or() {}
472 };
473
486 template<
487 typename D1, typename D2 = D1, typename D3 = D2,
488 enum Backend implementation = config::default_backend
489 >
490 class logical_and : public internal::Operator<
491 internal::logical_and< D1, D2, D3, implementation >
492 > {
493
494 public:
495
496 template< typename A, typename B, typename C, enum Backend D >
498
499 logical_and() {}
500 };
501
510 template<
511 typename D1, typename D2 = D1, typename D3 = D2,
512 enum Backend implementation = config::default_backend
513 >
514 class relu : public internal::Operator<
515 internal::relu< D1, D2, D3, implementation >
516 > {
517
518 public:
519
520 template< typename A, typename B, typename C, enum Backend D >
522
523 relu() {}
524 };
525
537 template<
538 typename D1, typename D2 = D1, typename D3 = D2,
539 enum Backend implementation = config::default_backend
540 >
541 class abs_diff : public internal::Operator<
542 internal::abs_diff< D1, D2, D3, implementation >
543 > {
544
545 public:
546
547 template< typename A, typename B, typename C, enum Backend D >
549
550 abs_diff() {}
551
552 };
553
572 template< typename IType, typename VType >
573 class argmin : public internal::Operator< internal::argmin< IType, VType > > {
574
575 public:
576
577 argmin() {}
578
579 };
580
599 template< typename IType, typename VType >
600 class argmax : public internal::Operator< internal::argmax< IType, VType > > {
601
602 public:
603
604 argmax() {}
605
606 };
607
619 template<
620 typename D1, typename D2, typename D3,
621 enum Backend implementation = config::default_backend
622 >
623 class square_diff : public internal::Operator<
624 internal::square_diff< D1, D2, D3, implementation >
625 > {
626
627 public:
628
629 template< typename A, typename B, typename C, enum Backend D >
631
632 square_diff() {}
633
634 };
635
645 template<
646 typename IN1, typename IN2,
647 enum Backend implementation = config::default_backend
648 >
649 class zip : public internal::Operator<
650 internal::zip< IN1, IN2, implementation >
651 > {
652
653 public:
654
655 template< typename A, typename B, enum Backend D >
657
658 zip() {}
659
660 };
661
674 template<
675 typename D1, typename D2 = D1, typename D3 = D2,
676 enum Backend implementation = config::default_backend
677 >
678 class equal_first : public internal::Operator<
679 internal::equal_first< D1, D2, D3, implementation >
680 > {
681
682 public:
683
684 template< typename A, typename B, typename C, enum Backend D >
686
687 equal_first() {}
688
689 };
690
703 template<
704 typename D1, typename D2 = D1, typename D3 = D2,
705 enum Backend implementation = config::default_backend
706 >
707 class less_than : public internal::Operator<
708 internal::lt< D1, D2, D3, implementation >
709 > {
710
711 public:
712
713 template< typename A, typename B, typename C, enum Backend D >
715
716 less_than() {}
717
718 };
719
732 template<
733 typename D1, typename D2 = D1, typename D3 = D2,
734 enum Backend implementation = config::default_backend
735 >
736 class leq : public internal::Operator<
737 internal::leq< D1, D2, D3, implementation >
738 > {
739
740 public:
741
742 template< typename A, typename B, typename C, enum Backend D >
744
745 leq() {}
746
747 };
748
761 template<
762 typename D1, typename D2 = D1, typename D3 = D2,
763 enum Backend implementation = config::default_backend
764 >
765 class greater_than: public internal::Operator<
766 internal::gt< D1, D2, D3, implementation >
767 > {
768
769 public:
770
771 template< typename A, typename B, typename C, enum Backend D >
773
774 greater_than() {}
775
776 };
777
790 template<
791 typename D1, typename D2 = D1, typename D3 = D2,
792 enum Backend implementation = config::default_backend
793 >
794 class geq : public internal::Operator<
795 internal::geq< D1, D2, D3, implementation >
796 > {
797
798 public:
799
800 template< typename A, typename B, typename C, enum Backend D >
802
803 geq() {}
804
805 };
806
807 } // namespace operators
808
809 template< typename D1, typename D2, typename D3, enum Backend implementation >
810 struct is_operator< operators::left_assign_if< D1, D2, D3, implementation > > {
811 static const constexpr bool value = true;
812 };
813
814 template< typename D1, typename D2, typename D3, enum Backend implementation >
815 struct is_operator< operators::right_assign_if< D1, D2, D3, implementation > > {
816 static const constexpr bool value = true;
817 };
818
819 template< typename D1, typename D2, typename D3, enum Backend implementation >
820 struct is_operator< operators::left_assign< D1, D2, D3, implementation > > {
821 static const constexpr bool value = true;
822 };
823
824 template< typename D1, typename D2, typename D3, enum Backend implementation >
825 struct is_operator< operators::right_assign< D1, D2, D3, implementation > > {
826 static const constexpr bool value = true;
827 };
828
829 // [Operator Type Traits]
830 template< typename D1, typename D2, typename D3, enum Backend implementation >
831 struct is_operator< operators::add< D1, D2, D3, implementation > > {
832 static const constexpr bool value = true;
833 };
834 // [Operator Type Traits]
835
836 template< typename D1, typename D2, typename D3, enum Backend implementation >
837 struct is_operator< operators::mul< D1, D2, D3, implementation > > {
838 static const constexpr bool value = true;
839 };
840
841 template< typename D1, typename D2, typename D3, enum Backend implementation >
842 struct is_operator< operators::max< D1, D2, D3, implementation > > {
843 static const constexpr bool value = true;
844 };
845
846 template< typename D1, typename D2, typename D3, enum Backend implementation >
847 struct is_operator< operators::min< D1, D2, D3, implementation > > {
848 static const constexpr bool value = true;
849 };
850
851 template< typename D1, typename D2, typename D3, enum Backend implementation >
852 struct is_operator< operators::subtract< D1, D2, D3, implementation > > {
853 static const constexpr bool value = true;
854 };
855
856 template< typename D1, typename D2, typename D3, enum Backend implementation >
857 struct is_operator< operators::divide< D1, D2, D3, implementation > > {
858 static const constexpr bool value = true;
859 };
860
861 template< typename D1, typename D2, typename D3, enum Backend implementation >
862 struct is_operator< operators::divide_reverse< D1, D2, D3, implementation > > {
863 static const constexpr bool value = true;
864 };
865
866 template< typename D1, typename D2, typename D3, enum Backend implementation >
867 struct is_operator< operators::equal< D1, D2, D3, implementation > > {
868 static const constexpr bool value = true;
869 };
870
871 template< typename D1, typename D2, typename D3, enum Backend implementation >
872 struct is_operator< operators::not_equal< D1, D2, D3, implementation > > {
873 static const constexpr bool value = true;
874 };
875
876 template< typename D1, typename D2, typename D3, enum Backend implementation >
877 struct is_operator< operators::any_or< D1, D2, D3, implementation > > {
878 static const constexpr bool value = true;
879 };
880
881 template< typename D1, typename D2, typename D3, enum Backend implementation >
882 struct is_operator< operators::logical_or< D1, D2, D3, implementation > > {
883 static const constexpr bool value = true;
884 };
885
886 template< typename D1, typename D2, typename D3, enum Backend implementation >
887 struct is_operator< operators::logical_and< D1, D2, D3, implementation > > {
888 static const constexpr bool value = true;
889 };
890
891 template< typename D1, typename D2, typename D3, enum Backend implementation >
892 struct is_operator< operators::abs_diff< D1, D2, D3, implementation > > {
893 static const constexpr bool value = true;
894 };
895
896 template< typename D1, typename D2, typename D3, enum Backend implementation >
897 struct is_operator< operators::relu< D1, D2, D3, implementation > > {
898 static const constexpr bool value = true;
899 };
900
901 template< typename IType, typename VType >
902 struct is_operator< operators::argmin< IType, VType > > {
903 static const constexpr bool value = true;
904 };
905
906 template< typename IType, typename VType >
907 struct is_operator< operators::argmax< IType, VType > > {
908 static const constexpr bool value = true;
909 };
910
911 template< typename D1, typename D2, typename D3, enum Backend implementation >
912 struct is_operator< operators::square_diff< D1, D2, D3, implementation > > {
913 static const constexpr bool value = true;
914 };
915
916 template< typename IN1, typename IN2, enum Backend implementation >
917 struct is_operator< operators::zip< IN1, IN2, implementation > > {
918 static const constexpr bool value = true;
919 };
920
921 template< typename D1, typename D2, typename D3, enum Backend implementation >
922 struct is_operator< operators::equal_first< D1, D2, D3, implementation > > {
923 static const constexpr bool value = true;
924 };
925
926 template< typename D1, typename D2, typename D3, enum Backend implementation >
927 struct is_operator< operators::less_than< D1, D2, D3, implementation > > {
928 static const constexpr bool value = true;
929 };
930
931 template< typename D1, typename D2, typename D3, enum Backend implementation >
932 struct is_operator< operators::leq< D1, D2, D3, implementation > > {
933 static const constexpr bool value = true;
934 };
935
936 template< typename D1, typename D2, typename D3, enum Backend implementation >
937 struct is_operator< operators::greater_than< D1, D2, D3, implementation > > {
938 static const constexpr bool value = true;
939 };
940
941 template< typename D1, typename D2, typename D3, enum Backend implementation >
942 struct is_operator< operators::geq< D1, D2, D3, implementation > > {
943 static const constexpr bool value = true;
944 };
945
946 template< typename D1, typename D2, typename D3 >
947 struct is_idempotent< operators::min< D1, D2, D3 >, void > {
948 static const constexpr bool value = true;
949 };
950
951 template< typename D1, typename D2, typename D3 >
952 struct is_idempotent< operators::max< D1, D2, D3 >, void > {
953 static const constexpr bool value = true;
954 };
955
956 template< typename D1, typename D2, typename D3 >
957 struct is_idempotent< operators::any_or< D1, D2, D3 >, void > {
958 static const constexpr bool value = true;
959 };
960
961 template< typename D1, typename D2, typename D3 >
962 struct is_idempotent< operators::logical_or< D1, D2, D3 >, void > {
963 static const constexpr bool value = true;
964 };
965
966 template< typename D1, typename D2, typename D3 >
967 struct is_idempotent< operators::logical_and< D1, D2, D3 >, void > {
968 static const constexpr bool value = true;
969 };
970
971 template< typename D1, typename D2, typename D3 >
972 struct is_idempotent< operators::relu< D1, D2, D3 >, void > {
973 static const constexpr bool value = true;
974 };
975
976 template< typename D1, typename D2, typename D3 >
977 struct is_idempotent< operators::left_assign_if< D1, D2, D3 >, void > {
978 static const constexpr bool value = true;
979 };
980
981 template< typename D1, typename D2, typename D3 >
982 struct is_idempotent< operators::right_assign_if< D1, D2, D3 >, void > {
983 static const constexpr bool value = true;
984 };
985
986 template< typename IType, typename VType >
987 struct is_idempotent< operators::argmin< IType, VType >, void > {
988 static const constexpr bool value = true;
989 };
990
991 template< typename IType, typename VType >
992 struct is_idempotent< operators::argmax< IType, VType >, void > {
993 static const constexpr bool value = true;
994 };
995
996 template< typename OP >
997 struct is_associative<
998 OP,
999 typename std::enable_if< is_operator< OP >::value, void >::type
1000 > {
1001 static constexpr const bool value = OP::is_associative();
1002 };
1003
1004 template< typename OP >
1005 struct is_commutative<
1006 OP,
1007 typename std::enable_if< is_operator< OP >::value, void >::type
1008 > {
1009 static constexpr const bool value = OP::is_commutative();
1010 };
1011
1012 // internal type traits follow
1013
1014 namespace internal {
1015
1016 template< typename D1, typename D2, typename D3, enum Backend implementation >
1017 struct maybe_noop< operators::left_assign_if< D1, D2, D3, implementation > > {
1018 static const constexpr bool value = true;
1019 };
1020
1021 template< typename D1, typename D2, typename D3, enum Backend implementation >
1022 struct maybe_noop< operators::right_assign_if< D1, D2, D3, implementation > > {
1023 static const constexpr bool value = true;
1024 };
1025
1026 } // end namespace grb::internal
1027
1028} // end namespace grb
1029
1030#ifdef __DOXYGEN__
1036 #define _DEBUG_NO_IOSTREAM_PAIR_CONVERTER
1037#endif
1038
1039#ifdef _DEBUG
1040 #ifndef _DEBUG_NO_IOSTREAM_PAIR_CONVERTER
1041namespace std {
1042 template< typename U, typename V >
1043 std::ostream & operator<<( std::ostream &out, const std::pair< U, V > &pair ) {
1044 out << "( " << pair.first << ", " << pair.second << " )";
1045 return out;
1046 }
1047} // end namespace std
1048 #endif
1049#endif
1050
1051#endif // end ``_H_GRB_OPERATORS''
1052
This operator returns the absolute difference between two numbers.
Definition: ops.hpp:543
This operator takes the sum of the two input parameters and writes it to the output variable.
Definition: ops.hpp:177
This operator is a generalisation of the logical or.
Definition: ops.hpp:436
The argmax operator on key-value pairs.
Definition: ops.hpp:600
The argmin operator on key-value pairs.
Definition: ops.hpp:573
Reversed division of two numbers.
Definition: ops.hpp:355
Numerical division of two numbers.
Definition: ops.hpp:330
Compares std::pair inputs taking the first entry in every pair as the comparison key,...
Definition: ops.hpp:680
Operator which returns true if its inputs compare equal, and false otherwise.
Definition: ops.hpp:381
This operation returns whether the left operand compares greater-than or equal to the right operand.
Definition: ops.hpp:796
This operation returns whether the left operand compares greater-than the right operand.
Definition: ops.hpp:767
This operator assigns the left-hand input if the right-hand input evaluates true.
Definition: ops.hpp:88
This operator discards all right-hand side input and simply copies the left-hand side input to the ou...
Definition: ops.hpp:61
This operation returns whether the left operand compares less-than or equal to the right operand.
Definition: ops.hpp:738
This operation returns whether the left operand compares less-than the right operand.
Definition: ops.hpp:709
The logical and.
Definition: ops.hpp:492
The logical or.
Definition: ops.hpp:464
This operator takes the maximum of the two input parameters and writes the result to the output varia...
Definition: ops.hpp:243
This operator takes the minimum of the two input parameters and writes the result to the output varia...
Definition: ops.hpp:276
This operator multiplies the two input parameters and writes the result to the output variable.
Definition: ops.hpp:210
Operator that returns false whenever its inputs compare equal, and true otherwise.
Definition: ops.hpp:407
This operation is equivalent to grb::operators::min.
Definition: ops.hpp:516
This operator assigns the right-hand input if the left-hand input evaluates true.
Definition: ops.hpp:143
This operator discards all left-hand side input and simply copies the right-hand side input to the ou...
Definition: ops.hpp:117
This operation returns the squared difference between two numbers.
Definition: ops.hpp:625
Numerical substraction of two numbers.
Definition: ops.hpp:303
The zip operator that operators on keys as a left-hand input and values as a right hand input,...
Definition: ops.hpp:651
RC zip(Matrix< OutputType, backend, RIT, CIT, NIT > &A, const Vector< InputType1, backend, Coords > &x, const Vector< InputType2, backend, Coords > &y, const Vector< InputType3, backend, Coords > &z, const Phase &phase=EXECUTE)
The grb::zip merges three vectors into a matrix.
Definition: blas3.hpp:181
Backend
A collection of all backends.
Definition: backends.hpp:46
The ALP/GraphBLAS namespace.
Definition: graphblas.hpp:452
static const constexpr bool value
Whether T is associative.
Definition: type_traits.hpp:208
static const constexpr bool value
Whether T is commutative.
Definition: type_traits.hpp:246
static const constexpr bool value
Wheter T is idempotent.
Definition: type_traits.hpp:167
Used to inspect whether a given type is an ALP operator.
Definition: type_traits.hpp:104
static const constexpr bool value
Whether T is an ALP operator.
Definition: type_traits.hpp:111
Specifies the ALP algebraic type traits.