File: GB_serialize_to_blob.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 (80 lines) | stat: -rw-r--r-- 2,775 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
//------------------------------------------------------------------------------
// GB_serialize_to_blob: copy a set of blocks to the blob
//------------------------------------------------------------------------------

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

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

#include "GB.h"
#include "GB_serialize.h"

void GB_serialize_to_blob
(
    // input/output
    GB_void *blob,          // blocks are appended to the blob
    size_t *s_handle,       // location to append into the blob
    // input:
    GB_blocks *Blocks,      // Blocks: array of size nblocks+1
    int64_t *Sblocks,       // array of size nblocks
    int32_t nblocks,        // # of blocks
    int nthreads_max        // # of threads to use
)
{

    //--------------------------------------------------------------------------
    // check inputs
    //--------------------------------------------------------------------------

    ASSERT (blob != NULL) ;
    ASSERT (s_handle != NULL) ;
    ASSERT (nblocks >= 0) ;
    ASSERT ((nblocks > 0) == (Blocks != NULL)) ;
    ASSERT (nthreads_max > 0) ;

    //--------------------------------------------------------------------------
    // check for quick return
    //--------------------------------------------------------------------------

    if (nblocks == 0)
    { 
        // no blocks for this array
        return ;
    }

    //--------------------------------------------------------------------------
    // copy the blocks into the blob
    //--------------------------------------------------------------------------

    size_t s = (*s_handle) ;

    if (nblocks == 1)
    { 
        // copy a single block into the blob in parallel
        GB_memcpy (blob + s, Blocks [0].p, Sblocks [0], nthreads_max) ;
    }
    else
    {
        // copy each block with a single task
        int nthreads = GB_IMIN (nthreads_max, nblocks) ;
        int32_t blockid ;
        #pragma omp parallel for num_threads(nthreads) schedule(dynamic)
        for (blockid = 0 ; blockid < nblocks ; blockid++)
        { 
            // copy the compressed block of size s_size into the blob
            size_t s_start = (blockid == 0) ? 0 : Sblocks [blockid-1] ;
            size_t s_end   = Sblocks [blockid] ;
            size_t s_size  = s_end - s_start ;
            memcpy (blob + s + s_start, Blocks [blockid].p, s_size) ;
        }
    }

    //--------------------------------------------------------------------------
    // return the updated index into the blob
    //--------------------------------------------------------------------------

    s += Sblocks [nblocks-1] ;
    (*s_handle) = s ;
}