1 //------------------------------------------------------------------------------
2 // GrB_Vector_reduce: reduce a vector to a scalar
3 //------------------------------------------------------------------------------
4 
5 // SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
6 // SPDX-License-Identifier: Apache-2.0
7 
8 //------------------------------------------------------------------------------
9 
10 // Reduce entries in a vector to a scalar, c = accum (c, reduce_to_scalar(u))
11 
12 // All entries in the vector are "summed" to a single scalar t using the reduce
13 // monoid, which must be associative (otherwise the results are undefined).
14 // The result is either assigned to the output scalar c (if accum is NULL), or
15 // it accumulated in the result c via c = accum(c,t).  If the u has no entries,
16 // the result t is the identity value of the monoid.  Unlike most other
17 // GraphBLAS operations, this operation uses an accum operator but no mask.
18 
19 #include "GB_reduce.h"
20 
21 #define GB_VECTOR_TO_SCALAR(prefix,type,T)                                    \
22 GrB_Info GB_EVAL3 (prefix, _Vector_reduce_, T) /* c = accum (c, reduce (u))*/ \
23 (                                                                             \
24     type *c,                        /* result scalar                       */ \
25     const GrB_BinaryOp accum,       /* optional accum for c=accum(c,t)     */ \
26     const GrB_Monoid monoid,        /* monoid to do the reduction          */ \
27     const GrB_Vector u,             /* vector to reduce                    */ \
28     const GrB_Descriptor desc       /* descriptor (currently unused)       */ \
29 )                                                                             \
30 {                                                                             \
31     GB_WHERE1 ("GrB_Vector_reduce_" GB_STR(T)                                 \
32         " (&c, accum, monoid, u, desc)") ;                                    \
33     GB_BURBLE_START ("GrB_reduce") ;                                          \
34     GB_RETURN_IF_NULL_OR_FAULTY (u) ;                                         \
35     ASSERT (GB_VECTOR_OK (u)) ;                                               \
36     GrB_Info info = GB_reduce_to_scalar (c, GB_EVAL3 (prefix, _, T), accum,   \
37         monoid, (GrB_Matrix) u, Context) ;                                    \
38     GB_BURBLE_END ;                                                           \
39     return (info) ;                                                           \
40 }
41 
GB_VECTOR_TO_SCALAR(GrB,bool,BOOL)42 GB_VECTOR_TO_SCALAR (GrB, bool      , BOOL   )
43 GB_VECTOR_TO_SCALAR (GrB, int8_t    , INT8   )
44 GB_VECTOR_TO_SCALAR (GrB, int16_t   , INT16  )
45 GB_VECTOR_TO_SCALAR (GrB, int32_t   , INT32  )
46 GB_VECTOR_TO_SCALAR (GrB, int64_t   , INT64  )
47 GB_VECTOR_TO_SCALAR (GrB, uint8_t   , UINT8  )
48 GB_VECTOR_TO_SCALAR (GrB, uint16_t  , UINT16 )
49 GB_VECTOR_TO_SCALAR (GrB, uint32_t  , UINT32 )
50 GB_VECTOR_TO_SCALAR (GrB, uint64_t  , UINT64 )
51 GB_VECTOR_TO_SCALAR (GrB, float     , FP32   )
52 GB_VECTOR_TO_SCALAR (GrB, double    , FP64   )
53 GB_VECTOR_TO_SCALAR (GxB, GxB_FC32_t, FC32   )
54 GB_VECTOR_TO_SCALAR (GxB, GxB_FC64_t, FC64   )
55 
56 GrB_Info GrB_Vector_reduce_UDT      // c = accum (c, reduce_to_scalar (u))
57 (
58     void *c,                        // result scalar
59     const GrB_BinaryOp accum,       // optional accum for c=accum(c,t)
60     const GrB_Monoid monoid,        // monoid to do the reduction
61     const GrB_Vector u,             // vector to reduce
62     const GrB_Descriptor desc       // descriptor (currently unused)
63 )
64 {
65     // See comments on GrB_Matrix_reduce_UDT
66     GB_WHERE1 ("GrB_Vector_reduce_UDT (&c, accum, monoid, u, desc)") ;
67     GB_BURBLE_START ("GrB_reduce") ;
68     GB_RETURN_IF_NULL_OR_FAULTY (u) ;
69     GB_RETURN_IF_NULL_OR_FAULTY (monoid) ;
70     ASSERT (GB_VECTOR_OK (u)) ;
71     GrB_Info info = GB_reduce_to_scalar (c, monoid->op->ztype,
72         accum, monoid, (GrB_Matrix) u, Context) ;
73     GB_BURBLE_END ;
74     return (info) ;
75 }
76 
77