File: GB_concat_hyper.c

package info (click to toggle)
suitesparse-graphblas 7.4.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 67,112 kB
  • sloc: ansic: 1,072,243; cpp: 8,081; sh: 512; makefile: 506; asm: 369; python: 125; awk: 10
file content (225 lines) | stat: -rw-r--r-- 8,329 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//------------------------------------------------------------------------------
// GB_concat_hyper: concatenate an array of matrices into a hypersparse matrix
//------------------------------------------------------------------------------

// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2022, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

//------------------------------------------------------------------------------

#define GB_FREE_ALL                 \
{                                   \
    GB_FREE (&Wi, Wi_size) ;        \
    GB_FREE_WORK (&Wj, Wj_size) ;   \
    GB_FREE_WORK (&Wx, Wx_size) ;   \
    GB_phybix_free (C) ;            \
}

#include "GB_concat.h"

GrB_Info GB_concat_hyper            // concatenate into a hypersparse matrix
(
    GrB_Matrix C,                   // input/output matrix for results
    const bool C_iso,               // if true, construct C as iso
    const GB_void *cscalar,         // iso value of C, if C is iso 
    const int64_t cnz,              // # of entries in C
    const GrB_Matrix *Tiles,        // 2D row-major array of size m-by-n,
    const GrB_Index m,
    const GrB_Index n,
    const int64_t *restrict Tile_rows,  // size m+1
    const int64_t *restrict Tile_cols,  // size n+1
    GB_Context Context
)
{

    //--------------------------------------------------------------------------
    // allocate triplet workspace to construct C as hypersparse
    //--------------------------------------------------------------------------

    GrB_Info info ;
    GrB_Matrix A = NULL ;
    ASSERT_MATRIX_OK (C, "C input to concat hyper", GB0) ;

    int64_t *restrict Wi = NULL ; size_t Wi_size = 0 ;
    int64_t *restrict Wj = NULL ; size_t Wj_size = 0 ;
    GB_void *restrict Wx = NULL ; size_t Wx_size = 0 ;

    GrB_Type ctype = C->type ;
    int64_t cvlen = C->vlen ;
    int64_t cvdim = C->vdim ;
    bool csc = C->is_csc ;
    size_t csize = ctype->size ;
    GB_Type_code ccode = ctype->code ;

    float hyper_switch = C->hyper_switch ;
    float bitmap_switch = C->bitmap_switch ;
    int sparsity_control = C->sparsity_control ;

    GB_phybix_free (C) ;

    Wi = GB_MALLOC (cnz, int64_t, &Wi_size) ;               // becomes C->i
    Wj = GB_MALLOC_WORK (cnz, int64_t, &Wj_size) ;          // freed below
    if (!C_iso)
    { 
        Wx = GB_MALLOC_WORK (cnz * csize, GB_void, &Wx_size) ;  // freed below
    }
    if (Wi == NULL || Wj == NULL || (!C_iso && Wx == NULL))
    { 
        // out of memory
        GB_FREE_ALL ;
        return (GrB_OUT_OF_MEMORY) ;
    }

    GB_GET_NTHREADS_MAX (nthreads_max, chunk, Context) ;

    int64_t nouter = csc ? n : m ;
    int64_t ninner = csc ? m : n ;

    //--------------------------------------------------------------------------
    // concatenate all matrices into the list of triplets
    //--------------------------------------------------------------------------

    int64_t pC = 0 ;
    for (int64_t outer = 0 ; outer < nouter ; outer++)
    {
        for (int64_t inner = 0 ; inner < ninner ; inner++)
        {

            //------------------------------------------------------------------
            // get the tile A
            //------------------------------------------------------------------

            A = csc ? GB_TILE (Tiles, inner, outer)
                    : GB_TILE (Tiles, outer, inner) ;
            ASSERT (!GB_ANY_PENDING_WORK (A)) ;

            //------------------------------------------------------------------
            // determine where to place the tile in C
            //------------------------------------------------------------------

            // The tile A appears in vectors cvstart:cvend-1 of C, and indices
            // cistart:ciend-1.

            int64_t cvstart, cistart ;
            if (csc)
            { 
                // C is held by column
                // Tiles is row-major and accessed in column order
                cvstart = Tile_cols [outer] ;
                cistart = Tile_rows [inner] ;
            }
            else
            { 
                // C is held by row
                // Tiles is row-major and accessed in row order
                cvstart = Tile_rows [outer] ;
                cistart = Tile_cols [inner] ;
            }

            //------------------------------------------------------------------
            // extract the tuples from tile A
            //------------------------------------------------------------------

            // if A is iso but C is not, extractTuples expands A->x [0] into
            // all Wx [...].   If both A and C are iso, then all tiles are iso,
            // and Wx is not extracted.

            int64_t anz = GB_nnz (A) ;
            GB_OK (GB_extractTuples (
                (GrB_Index *) ((csc ? Wi : Wj) + pC),
                (GrB_Index *) ((csc ? Wj : Wi) + pC),
                (C_iso) ? NULL : (Wx + pC * csize),
                (GrB_Index *) (&anz), ccode, A, Context)) ;

            //------------------------------------------------------------------
            // adjust the indices to reflect their new place in C
            //------------------------------------------------------------------

            int nth = GB_nthreads (anz, chunk, nthreads_max) ;
            if (cistart > 0 && cvstart > 0)
            { 
                int64_t pA ;
                #pragma omp parallel for num_threads(nth) schedule(static)
                for (pA = 0 ; pA < anz ; pA++)
                {
                    Wi [pC + pA] += cistart ;
                    Wj [pC + pA] += cvstart ;
                }
            }
            else if (cistart > 0)
            { 
                int64_t pA ;
                #pragma omp parallel for num_threads(nth) schedule(static)
                for (pA = 0 ; pA < anz ; pA++)
                {
                    Wi [pC + pA] += cistart ;
                }
            }
            else if (cvstart > 0)
            { 
                int64_t pA ;
                #pragma omp parallel for num_threads(nth) schedule(static)
                for (pA = 0 ; pA < anz ; pA++)
                {
                    Wj [pC + pA] += cvstart ;
                }
            }

            //------------------------------------------------------------------
            // advance the tuple counter
            //------------------------------------------------------------------

            pC += anz ;
        }
    }

    //--------------------------------------------------------------------------
    // build C from the triplets
    //--------------------------------------------------------------------------

    const GB_void *S_input = NULL ;
    if (C_iso)
    { 
        S_input = cscalar ;
    }

    GB_OK (GB_builder (
        C,                      // create C using a static or dynamic header
        ctype,                  // C->type
        cvlen,                  // C->vlen
        cvdim,                  // C->vdim
        csc,                    // C->is_csc
        (int64_t **) &Wi,       // Wi is C->i on output, or freed on error
        &Wi_size,
        (int64_t **) &Wj,       // Wj, free on output
        &Wj_size,
        (GB_void **) &Wx,       // Wx, free on output; or NULL if C is iso
        &Wx_size,
        false,                  // tuples need to be sorted
        true,                   // no duplicates
        cnz,                    // size of Wi and Wj in # of tuples
        true,                   // is_matrix: unused
        NULL, NULL,             // original I,J tuples
        S_input,                // cscalar if C is iso, or NULL
        C_iso,                  // true if C is iso
        cnz,                    // # of tuples
        NULL,                   // no duplicates, so dup is NUL
        ctype,                  // the type of Wx (no typecasting)
        true,                   // burble is allowed
        Context
    )) ;

    C->hyper_switch = hyper_switch ;
    C->bitmap_switch = bitmap_switch ;
    C->sparsity_control = sparsity_control ;
    ASSERT (GB_IS_HYPERSPARSE (C)) ;
    ASSERT_MATRIX_OK (C, "C from concat hyper", GB0) ;

    // workspace has been freed by GB_builder, or transplanted into C
    ASSERT (Wi == NULL) ;
    ASSERT (Wj == NULL) ;
    ASSERT (Wx == NULL) ;

    return (GrB_SUCCESS) ;
}