11// ------------------------------------------------------------------------------
2- // GraphBLAS/CUDA/template/GB_cuda_cumsum: cumlative sum of array on the GPU(s)
2+ // GraphBLAS/CUDA/template/GB_cuda_cumsum: cumlative sum of an array on the GPU(s)
33// ------------------------------------------------------------------------------
44
55// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2025, All Rights Reserved.
1212
1313#include < cub/cub.cuh>
1414
15- #define GB_FREE_ALL \
16- { \
17- cudaFree (d_temp_storage) ; \
15+ #define GB_FREE_ALL \
16+ { \
17+ cudaFreeAsync (d_temp_storage, stream ) ; \
1818}
1919
2020typedef enum GB_cuda_cumsum_type
@@ -23,7 +23,7 @@ typedef enum GB_cuda_cumsum_type
2323 GB_CUDA_CUMSUM_INCLUSIVE
2424} GB_cuda_cumsum_type ;
2525
26- __host__ GrB_Info GB_cuda_cumsum // compute the cumulative sum of an array
26+ __host__ GrB_Info GB_cuda_cumsum // compute the cumsum of an array
2727(
2828 int64_t *__restrict__ out, // size n or n+1, output.
2929 int64_t *__restrict__ in, // size n or n+1, input
@@ -43,7 +43,7 @@ __host__ GrB_Info GB_cuda_cumsum // compute the cumulative sum of an array
4343 ASSERT (out != NULL ) ;
4444 ASSERT (n >= 0 ) ;
4545
46- void *d_temp_storage = NULL ;
46+ void *d_temp_storage = NULL ;
4747 size_t temp_storage_bytes = 0 ;
4848
4949 switch (type)
@@ -57,20 +57,23 @@ __host__ GrB_Info GB_cuda_cumsum // compute the cumulative sum of an array
5757 in, out, n, stream) ;
5858 }
5959
60- CUDA_OK (cudaMalloc (&d_temp_storage, temp_storage_bytes)) ;
60+ CUDA_OK (cudaMallocAsync (&d_temp_storage, temp_storage_bytes, stream)) ;
61+ CUDA_OK (cudaStreamSynchronize (stream)) ;
6162
6263 // Run
6364 switch (type)
6465 {
6566 case GB_CUDA_CUMSUM_INCLUSIVE :
66- cub::DeviceScan::InclusiveSum (d_temp_storage, temp_storage_bytes,
67- in, out, n, stream) ;
67+ cub::DeviceScan::InclusiveSum (d_temp_storage,
68+ temp_storage_bytes, in, out, n, stream) ;
6869 break ;
6970 default :
70- cub::DeviceScan::ExclusiveSum (d_temp_storage, temp_storage_bytes,
71- in, out, n, stream) ;
71+ cub::DeviceScan::ExclusiveSum (d_temp_storage,
72+ temp_storage_bytes, in, out, n, stream) ;
7273 }
73- cudaFree (d_temp_storage) ;
74+
75+ cudaFreeAsync (d_temp_storage, stream) ;
76+ cudaStreamSynchronize (stream) ;
7477
7578 return GrB_SUCCESS;
7679}
0 commit comments