1 //------------------------------------------------------------------------------
2 // GB_memset: parallel memset
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 // Note that this function uses its own hard-coded chunk size.
11 
12 #include "GB.h"
13 
14 #define GB_MEM_CHUNK (1024*1024)
15 
GB_memset(void * dest,const int c,size_t n,int nthreads)16 void GB_memset                  // parallel memset
17 (
18     void *dest,                 // destination
19     const int c,                // value to to set
20     size_t n,                   // # of bytes to set
21     int nthreads                // max # of threads to use
22 )
23 {
24 
25     if (nthreads <= 1 || n <= GB_MEM_CHUNK)
26     {
27 
28         //----------------------------------------------------------------------
29         // memset using a single thread
30         //----------------------------------------------------------------------
31 
32         memset (dest, c, n) ;
33     }
34     else
35     {
36 
37         //----------------------------------------------------------------------
38         // memset using multiple threads
39         //----------------------------------------------------------------------
40 
41         size_t nchunks = 1 + (n / GB_MEM_CHUNK) ;
42         if (((size_t) nthreads) > nchunks)
43         {
44             nthreads = (int) nchunks ;
45         }
46         GB_void *pdest = (GB_void *) dest ;
47 
48         int64_t k ;
49         #pragma omp parallel for num_threads(nthreads) schedule(dynamic,1)
50         for (k = 0 ; k < nchunks ; k++)
51         {
52             size_t start = k * GB_MEM_CHUNK ;
53             if (start < n)
54             {
55                 size_t chunk = GB_IMIN (n - start, GB_MEM_CHUNK) ;
56                 memset (pdest + start, c, chunk) ;
57             }
58         }
59     }
60 }
61 
62