1 //------------------------------------------------------------------------------
2 // GB_bitmap_selector: select entries from a bitmap or full matrix
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 #include "GB_select.h"
11 #include "GB_sel__include.h"
12
13 #define GB_FREE_ALL ;
14
GB_bitmap_selector(GrB_Matrix C,GB_Select_Opcode opcode,const GxB_select_function user_select,const bool flipij,GrB_Matrix A,const int64_t ithunk,const GB_void * restrict xthunk,GB_Context Context)15 GrB_Info GB_bitmap_selector
16 (
17 GrB_Matrix C, // output matrix, static header
18 GB_Select_Opcode opcode, // selector opcode
19 const GxB_select_function user_select, // user select function
20 const bool flipij, // if true, flip i and j for user operator
21 GrB_Matrix A, // input matrix
22 const int64_t ithunk, // (int64_t) Thunk, if Thunk is NULL
23 const GB_void *restrict xthunk,
24 GB_Context Context
25 )
26 {
27
28 //--------------------------------------------------------------------------
29 // check inputs
30 //--------------------------------------------------------------------------
31
32 GrB_Info info ;
33 ASSERT_MATRIX_OK (A, "A for bitmap selector", GB0) ;
34 ASSERT (GB_is_packed (A)) ;
35 ASSERT (opcode != GB_RESIZE_opcode) ;
36 ASSERT (opcode != GB_NONZOMBIE_opcode) ;
37 ASSERT (C != NULL && C->static_header) ;
38
39 //--------------------------------------------------------------------------
40 // get A
41 //--------------------------------------------------------------------------
42
43 int64_t anz = GB_NNZ_HELD (A) ;
44 const GB_Type_code typecode = A->type->code ;
45
46 //--------------------------------------------------------------------------
47 // allocate C
48 //--------------------------------------------------------------------------
49
50 // C->b and C->x are malloc'd, not calloc'd
51 GB_OK (GB_new_bix (&C, true, // always bitmap, static header
52 A->type, A->vlen, A->vdim, GB_Ap_calloc, true,
53 GxB_BITMAP, false, A->hyper_switch, -1, anz, true, Context)) ;
54 int64_t cnvals ;
55
56 //--------------------------------------------------------------------------
57 // determine the number of threads to use
58 //--------------------------------------------------------------------------
59
60 GB_GET_NTHREADS_MAX (nthreads_max, chunk, Context) ;
61 int nthreads = GB_nthreads (anz, chunk, nthreads_max) ;
62
63 //--------------------------------------------------------------------------
64 // clear C for the EQ_ZERO opcode
65 //--------------------------------------------------------------------------
66
67 // All other opcodes set C->x in the worker below
68 if (opcode == GB_EQ_ZERO_opcode)
69 {
70 GB_memset (C->x, 0, anz * A->type->size, nthreads_max) ;
71 }
72
73 //--------------------------------------------------------------------------
74 // launch the switch factory to select the entries
75 //--------------------------------------------------------------------------
76
77 #define GB_BITMAP_SELECTOR
78 #define GB_selbit(opname,aname) GB (_sel_bitmap_ ## opname ## aname)
79 #define GB_SEL_WORKER(opname,aname,atype) \
80 { \
81 GB_selbit (opname, aname) (C->b, (atype *) C->x, &cnvals, A, \
82 flipij, ithunk, (atype *) xthunk, user_select, nthreads) ; \
83 } \
84 break ;
85 #include "GB_select_factory.c"
86
87 //--------------------------------------------------------------------------
88 // return result
89 //--------------------------------------------------------------------------
90
91 C->nvals = cnvals ;
92 C->magic = GB_MAGIC ;
93 ASSERT_MATRIX_OK (C, "C from bitmap selector", GB0) ;
94 return (GrB_SUCCESS) ;
95 }
96
97