ALP User Documentation  0.8.preview
Algebraic Programming User Documentation
io.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 #include <type_traits>
28 #include <typeinfo>
29 
30 #ifndef _H_GRB_IO_BASE
31 #define _H_GRB_IO_BASE
32 
33 #include <graphblas/rc.hpp>
34 #include <graphblas/phase.hpp>
35 #include <graphblas/iomode.hpp>
36 #include <graphblas/synchronizedNonzeroIterator.hpp>
38 
39 #include "matrix.hpp"
40 #include "vector.hpp"
41 
42 #include <assert.h>
43 
44 
45 namespace grb {
46 
153  template<
154  typename ElementType, typename Coords,
155  Backend implementation = config::default_backend
156  >
157  uintptr_t getID(
159  ) {
160  (void) x;
161 #ifndef NDEBUG
162  const bool this_is_an_invalid_default_implementation = false;
163 #endif
164  assert( this_is_an_invalid_default_implementation );
165  return static_cast< uintptr_t >(-1);
166  }
167 
174  template<
175  typename ElementType, typename RIT, typename CIT, typename NIT,
176  Backend implementation = config::default_backend
177  >
178  uintptr_t getID(
180  ) {
181  (void) x;
182 #ifndef NDEBUG
183  const bool this_is_an_invalid_default_implementation = false;
184 #endif
185  assert( this_is_an_invalid_default_implementation );
186  return static_cast< uintptr_t >(-1);
187  }
188 
231  template<
232  typename DataType,
233  Backend backend, typename Coords
234  >
235  size_t size( const Vector< DataType, backend, Coords > &x ) noexcept {
236 #ifndef NDEBUG
237  const bool may_not_call_base_size = false;
238 #endif
239  (void) x;
240  assert( may_not_call_base_size );
241  return SIZE_MAX;
242  }
243 
282  template<
283  typename InputType, Backend backend,
284  typename RIT, typename CIT, typename NIT
285  >
286  size_t nrows(
288  ) noexcept {
289 #ifndef NDEBUG
290  const bool may_not_call_base_nrows = false;
291 #endif
292  (void) A;
293  assert( may_not_call_base_nrows );
294  return SIZE_MAX;
295  }
296 
335  template<
336  typename InputType, Backend backend,
337  typename RIT, typename CIT, typename NIT
338  >
339  size_t ncols(
341  ) noexcept {
342 #ifndef NDEBUG
343  const bool may_not_call_base_ncols = false;
344 #endif
345  (void) A;
346  assert( may_not_call_base_ncols );
347  return SIZE_MAX;
348  }
349 
385  template<
386  typename InputType, Backend backend, typename Coords
387  >
388  size_t capacity( const Vector< InputType, backend, Coords > &x ) noexcept {
389 #ifndef NDEBUG
390  const bool should_not_call_base_vector_capacity = false;
391 #endif
392  assert( should_not_call_base_vector_capacity );
393  (void) x;
394  return SIZE_MAX;
395  }
396 
428  template<
429  typename InputType, Backend backend,
430  typename RIT, typename CIT, typename NIT
431  >
432  size_t capacity(
434  ) noexcept {
435 #ifndef NDEBUG
436  const bool should_not_call_base_matrix_capacity = false;
437 #endif
438  assert( should_not_call_base_matrix_capacity );
439  (void) A;
440  return SIZE_MAX;
441  }
442 
478  template< typename DataType, Backend backend, typename Coords >
479  size_t nnz( const Vector< DataType, backend, Coords > &x ) noexcept {
480 #ifndef NDEBUG
481  const bool should_not_call_base_vector_nnz = false;
482 #endif
483  (void) x;
484  assert( should_not_call_base_vector_nnz );
485  return SIZE_MAX;
486  }
487 
519  template<
520  typename InputType, Backend backend,
521  typename RIT, typename CIT, typename NIT
522  >
523  size_t nnz(
525  ) noexcept {
526 #ifndef NDEBUG
527  const bool should_not_call_base_matrix_nnz = false;
528 #endif
529  (void) A;
530  assert( should_not_call_base_matrix_nnz );
531  return SIZE_MAX;
532  }
533 
573  template< typename DataType, Backend backend, typename Coords >
575 #ifndef NDEBUG
576  const bool should_not_call_base_vector_clear = false;
577 #endif
578  (void) x;
579  assert( should_not_call_base_vector_clear );
580  return UNSUPPORTED;
581  }
582 
618  template<
619  typename InputType, Backend backend,
620  typename RIT, typename CIT, typename NIT
621  >
624  ) noexcept {
625 #ifndef NDEBUG
626  const bool should_not_call_base_matrix_clear = false;
627 #endif
628  (void) A;
629  assert( should_not_call_base_matrix_clear );
630  return UNSUPPORTED;
631  }
632 
699  template<
700  typename InputType,
701  Backend backend, typename Coords
702  >
705  const size_t new_nz
706  ) noexcept {
707 #ifndef NDEBUG
708  const bool should_not_call_base_vector_resize = false;
709 #endif
710  (void) x;
711  (void) new_nz;
712  assert( should_not_call_base_vector_resize );
713  return UNSUPPORTED;
714  }
715 
782  template<
783  typename InputType, Backend backend,
784  typename RIT, typename CIT, typename NIT
785  >
788  const size_t new_nz
789  ) noexcept {
790 #ifndef NDEBUG
791  const bool should_not_call_base_matrix_resize = false;
792 #endif
793  (void) A;
794  (void) new_nz;
795  assert( should_not_call_base_matrix_resize );
796  return UNSUPPORTED;
797  }
798 
853  template<
855  typename DataType, typename T,
856  typename Coords, Backend backend
857  >
859  Vector< DataType, backend, Coords > &x, const T val,
860  const Phase &phase = EXECUTE,
861  const typename std::enable_if<
864  void >::type * const = nullptr
865  ) noexcept {
866 #ifndef NDEBUG
867  const bool should_not_call_base_vector_set = false;
868  assert( should_not_call_base_vector_set );
869 #endif
870  (void) x;
871  (void) val;
872  (void) phase;
873  return UNSUPPORTED;
874  }
875 
937  template<
939  typename DataType, typename MaskType, typename T,
940  Backend backend, typename Coords
941  >
945  const T val,
946  const Phase &phase = EXECUTE,
947  const typename std::enable_if<
949  void >::type * const = nullptr
950  ) {
951 #ifndef NDEBUG
952  const bool should_not_call_base_masked_vector_set = false;
953  assert( should_not_call_base_masked_vector_set );
954 #endif
955  (void) x;
956  (void) mask;
957  (void) val;
958  (void) phase;
959  return UNSUPPORTED;
960  }
961 
994  template<
996  typename OutputType, typename InputType,
997  Backend backend, typename Coords
998  >
1002  const Phase &phase = EXECUTE
1003  ) {
1004 #ifndef NDEBUG
1005  const bool should_not_call_base_vector_set_copy = false;
1006  assert( should_not_call_base_vector_set_copy );
1007 #endif
1008  (void) x;
1009  (void) y;
1010  (void) phase;
1011  return UNSUPPORTED;
1012  }
1013 
1055  template<
1057  typename OutputType, typename MaskType, typename InputType,
1058  Backend backend, typename Coords
1059  >
1064  const Phase &phase = EXECUTE,
1065  const typename std::enable_if< !grb::is_object< OutputType >::value &&
1068  void >::type * const = nullptr
1069  ) {
1070 #ifndef NDEBUG
1071  const bool should_not_call_base_vector_set_copy_masked = false;
1072  assert( should_not_call_base_vector_set_copy_masked );
1073 #endif
1074  (void) x;
1075  (void) mask;
1076  (void) y;
1077  (void) phase;
1078  return UNSUPPORTED;
1079  }
1080 
1124  template<
1126  typename DataType, typename T,
1127  Backend backend, typename Coords
1128  >
1131  const T val,
1132  const size_t i,
1133  const Phase &phase = EXECUTE,
1134  const typename std::enable_if< !grb::is_object< DataType >::value &&
1135  !grb::is_object< T >::value, void >::type * const = nullptr
1136  ) {
1137 #ifndef NDEBUG
1138  const bool should_not_call_base_setElement = false;
1139  assert( should_not_call_base_setElement );
1140 #endif
1141  (void) x;
1142  (void) val;
1143  (void) i;
1144  (void) phase;
1145  return UNSUPPORTED;
1146  }
1147 
1154  template<
1156  typename InputType, typename fwd_iterator,
1157  Backend backend, typename Coords
1158  >
1161  fwd_iterator start, const fwd_iterator end,
1162  const IOMode mode
1163  ) {
1165  return buildVector< descr >( x, accum, start, end, mode );
1166  }
1167 
1175  template< Descriptor descr = descriptors::no_operation,
1176  typename InputType,
1177  class Merger = operators::right_assign< InputType >,
1178  typename fwd_iterator1, typename fwd_iterator2,
1179  Backend backend, typename Coords
1180  >
1182  fwd_iterator1 ind_start, const fwd_iterator1 ind_end,
1183  fwd_iterator2 val_start, const fwd_iterator2 val_end,
1184  const IOMode mode, const Merger & merger = Merger()
1185  ) {
1187  return buildVector< descr >( x, accum, ind_start, ind_end, val_start, val_end,
1188  mode, merger );
1189  }
1190 
1222  template<
1224  typename InputType,
1225  class Merger = operators::right_assign< InputType >,
1226  typename fwd_iterator1, typename fwd_iterator2,
1227  Backend backend, typename Coords
1228  >
1231  fwd_iterator1 ind_start, const fwd_iterator1 ind_end,
1232  fwd_iterator2 val_start, const fwd_iterator2 val_end,
1233  const IOMode mode
1234  ) {
1235  return buildVector< descr | descriptors::no_duplicates >(
1236  x,
1237  ind_start, ind_end,
1238  val_start, val_end,
1239  mode
1240  );
1241  }
1242 
1332  template<
1334  typename InputType, typename RIT, typename CIT, typename NIT,
1335  typename fwd_iterator1 = const RIT * __restrict__,
1336  typename fwd_iterator2 = const CIT * __restrict__,
1337  typename fwd_iterator3 = const InputType * __restrict__,
1338  Backend implementation = config::default_backend
1339  >
1342  fwd_iterator1 I, const fwd_iterator1 I_end,
1343  fwd_iterator2 J, const fwd_iterator2 J_end,
1344  fwd_iterator3 V, const fwd_iterator3 V_end,
1345  const IOMode mode
1346  ) {
1347  // derive synchronized iterator
1348  auto start = internal::makeSynchronized(
1349  I, J, V,
1350  I_end, J_end, V_end
1351  );
1352  const auto end = internal::makeSynchronized(
1353  I_end, J_end, V_end,
1354  I_end, J_end, V_end
1355  );
1356  // defer to other signature
1357  return buildMatrixUnique< descr >( A, start, end, mode );
1358  }
1359 
1364  template<
1366  typename InputType, typename RIT, typename CIT, typename NIT,
1367  typename fwd_iterator1 = const RIT * __restrict__,
1368  typename fwd_iterator2 = const CIT * __restrict__,
1369  typename fwd_iterator3 = const InputType * __restrict__,
1370  Backend implementation = config::default_backend
1371  >
1374  fwd_iterator1 I, fwd_iterator2 J, fwd_iterator3 V,
1375  const size_t nz, const IOMode mode
1376  ) {
1377  return buildMatrixUnique< descr >(
1378  A,
1379  I, I + nz,
1380  J, J + nz,
1381  V, V + nz,
1382  mode
1383  );
1384  }
1385 
1390  template<
1392  typename InputType, typename RIT, typename CIT, typename NIT,
1393  typename fwd_iterator1 = const RIT * __restrict__,
1394  typename fwd_iterator2 = const CIT * __restrict__,
1395  typename length_type = size_t,
1396  Backend implementation = config::default_backend
1397  >
1400  fwd_iterator1 I, fwd_iterator2 J,
1401  const length_type nz, const IOMode mode
1402  ) {
1403  // derive synchronized iterator
1404  auto start = internal::makeSynchronized( I, J, I + nz, J + nz );
1405  const auto end = internal::makeSynchronized( I + nz, J + nz, I + nz, J + nz );
1406  // defer to other signature
1407  return buildMatrixUnique< descr >( A, start, end, mode );
1408  }
1409 
1414  template<
1416  typename InputType, typename RIT, typename CIT, typename NIT,
1417  typename fwd_iterator1 = const RIT * __restrict__,
1418  typename fwd_iterator2 = const CIT * __restrict__,
1419  Backend implementation = config::default_backend
1420  >
1423  fwd_iterator1 I, const fwd_iterator1 I_end,
1424  fwd_iterator2 J, const fwd_iterator2 J_end,
1425  const IOMode mode
1426  ) {
1427  // derive synchronized iterator
1428  auto start = internal::makeSynchronized( I, J, I_end, J_end );
1429  const auto end = internal::makeSynchronized( I_end, J_end, I_end, J_end );
1430  // defer to other signature
1431  return buildMatrixUnique< descr >( A, start, end, mode );
1432  }
1433 
1475  template<
1477  typename InputType, typename RIT, typename CIT, typename NIT,
1478  typename fwd_iterator,
1479  Backend implementation = config::default_backend
1480  >
1483  fwd_iterator start, const fwd_iterator end,
1484  const IOMode mode
1485  ) {
1486  (void) A;
1487  (void) start;
1488  (void) end;
1489  (void) mode;
1490 #ifndef NDEBUG
1491  std::cerr << "Should not call base grb::buildMatrixUnique" << std::endl;
1492  const bool should_not_call_base_buildMatrixUnique = false;
1493  assert( should_not_call_base_buildMatrixUnique );
1494 #endif
1495  return PANIC;
1496  }
1497 
1544  template< Backend backend = config::default_backend >
1545  RC wait() {
1546 #ifndef NDEBUG
1547  const bool should_not_call_base_wait = false;
1548  assert( should_not_call_base_wait );
1549 #endif
1550  return UNSUPPORTED;
1551  }
1552 
1578  template<
1579  Backend backend, typename InputType, typename Coords,
1580  typename... Args
1581  >
1584  const Args &... args
1585  ) {
1586 #ifndef NDEBUG
1587  const bool should_not_call_base_vector_wait = false;
1588  assert( should_not_call_base_vector_wait );
1589 #endif
1590  (void) x;
1591  return wait( args... );
1592  }
1593 
1619  template<
1620  Backend backend,
1621  typename InputType, typename RIT, typename CIT, typename NIT,
1622  typename... Args
1623  >
1626  const Args &... args
1627  ) {
1628 #ifndef NDEBUG
1629  const bool should_not_call_base_matrix_wait = false;
1630  assert( should_not_call_base_matrix_wait );
1631 #endif
1632  (void) A;
1633  return wait( args... );
1634  }
1635 
1638 } // namespace grb
1639 
1640 #endif // end _H_GRB_IO_BASE
1641 
Defines the ALP error codes.
RC set(Vector< DataType, backend, Coords > &x, const T val, const Phase &phase=EXECUTE, const typename std::enable_if< !grb::is_object< DataType >::value &&!grb::is_object< T >::value, void >::type *const =nullptr) noexcept
Sets all elements of a vector to the given value.
Definition: io.hpp:858
RC buildVector(Vector< InputType, backend, Coords > &x, fwd_iterator start, const fwd_iterator end, const IOMode mode)
Constructs a dense vector from a container of exactly grb::size(x) elements.
Definition: io.hpp:1159
Defines a series of useful type traits for both STL and ALP iterators.
An ALP/GraphBLAS matrix.
Definition: matrix.hpp:72
Defines the various I/O modes a user could employ with ALP data ingestion or extraction.
RC
Return codes of ALP primitives.
Definition: rc.hpp:47
A GraphBLAS vector.
Definition: vector.hpp:64
Specifies the ALP/GraphBLAS matrix container.
uintptr_t getID(const Vector< ElementType, implementation, Coords > &x)
Function that returns a unique ID for a given non-empty container.
Definition: io.hpp:157
RC wait()
Depending on the backend, ALP/GraphBLAS primitives may be non-blocking, meaning that the operation im...
Definition: io.hpp:1545
Speculatively assumes that the output container of the requested operation has enough capacity to com...
Definition: phase.hpp:257
Defines the various phases an ALP/GraphBLAS primitive may be executed with.
Used to inspect whether a given type is an ALP/GraphBLAS object.
Definition: type_traits.hpp:130
Phase
Primitives with sparse ALP/GraphBLAS output containers may run into the issue where an appropriate gr...
Definition: phase.hpp:152
size_t nnz(const Vector< DataType, backend, Coords > &x) noexcept
Request the number of nonzeroes in a given vector.
Definition: io.hpp:479
static constexpr Descriptor no_operation
Indicates no additional pre- or post-processing on any of the GraphBLAS function arguments.
Definition: descriptors.hpp:63
unsigned int Descriptor
Descriptors indicate pre- or post-processing for some or all of the arguments to an ALP/GraphBLAS cal...
Definition: descriptors.hpp:54
size_t nrows(const Matrix< InputType, backend, RIT, CIT, NIT > &A) noexcept
Requests the row size of a given matrix.
Definition: io.hpp:286
RC buildMatrixUnique(Matrix< InputType, implementation, RIT, CIT, NIT > &A, fwd_iterator1 I, const fwd_iterator1 I_end, fwd_iterator2 J, const fwd_iterator2 J_end, fwd_iterator3 V, const fwd_iterator3 V_end, const IOMode mode)
Assigns nonzeroes to the matrix from a coordinate format.
Definition: io.hpp:1340
size_t ncols(const Matrix< InputType, backend, RIT, CIT, NIT > &A) noexcept
Requests the column size of a given matrix.
Definition: io.hpp:339
Indicates that the execution of the requested primitive with the given arguments is not supported by ...
Definition: rc.hpp:129
This operator discards all left-hand side input and simply copies the right-hand side input to the ou...
Definition: ops.hpp:115
Backend
A collection of all backends.
Definition: backends.hpp:49
RC buildVectorUnique(Vector< InputType, backend, Coords > &x, fwd_iterator1 ind_start, const fwd_iterator1 ind_end, fwd_iterator2 val_start, const fwd_iterator2 val_end, const IOMode mode)
Ingests a set of nonzeroes into a given vector x.
Definition: io.hpp:1229
The ALP/GraphBLAS namespace.
Definition: graphblas.hpp:477
IOMode
The GraphBLAS input and output functionalities can either be used in a sequential or parallel fashion...
Definition: iomode.hpp:67
Specifies the ALP/GraphBLAS vector container.
Generic fatal error code.
Definition: rc.hpp:68
size_t size(const Vector< DataType, backend, Coords > &x) noexcept
Request the size of a given vector.
Definition: io.hpp:235
size_t capacity(const Vector< InputType, backend, Coords > &x) noexcept
Queries the capacity of the given ALP/GraphBLAS container.
Definition: io.hpp:388
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
RC resize(Vector< InputType, backend, Coords > &x, const size_t new_nz) noexcept
Resizes the nonzero capacity of this vector.
Definition: io.hpp:703