@@ -33,14 +33,15 @@ namespace ippl::comms {
3333 return ((2 * num + (ALIGMNEMT - 1 )) & (-ALIGMNEMT));
3434 }
3535
36- struct AlignedCudaBuffer {
36+ template <typename MemorySpace>
37+ struct AlignedBuffer {
3738 void * ptrOriginal{nullptr };
3839 void * ptrAligned{nullptr };
3940 detail::size_type space{0 };
4041 //
41- AlignedCudaBuffer () {}
42+ AlignedBuffer () {}
4243 //
43- AlignedCudaBuffer & operator =(AlignedCudaBuffer && other) {
44+ AlignedBuffer & operator =(AlignedBuffer && other) {
4445 ptrOriginal = other.ptrOriginal ;
4546 ptrAligned = other.ptrAligned ;
4647 space = other.space ;
@@ -50,28 +51,51 @@ namespace ippl::comms {
5051 return *this ;
5152 }
5253 //
53- AlignedCudaBuffer (std::size_t size) {
54+ AlignedBuffer (std::size_t size) {
55+ ptrOriginal = static_cast <int *>(std::aligned_alloc (ALIGMNEMT, size));
56+ ptrAligned = ptrOriginal;
57+ space = size;
58+ SPDLOG_TRACE (" AlignedBuffer: original {}, aligned {}, size {}, space {}" ,
59+ (void *)(ptrOriginal), (void *)(ptrAligned), size, space);
60+ // sanity check should always be true when std::align used
61+ assert (space >= size);
62+ }
63+ //
64+ ~AlignedBuffer () {
65+ if (ptrOriginal) {
66+ SPDLOG_DEBUG (" Destroying cuda buffer {}" , ptrOriginal);
67+ std::free (ptrOriginal);
68+ }
69+ }
70+ };
71+
72+ #ifdef KOKKOS_ENABLE_CUDA
73+ template <>
74+ struct AlignedBuffer <Kokkos::CudaSpace> {
75+ //
76+ AlignedBuffer (std::size_t size) {
5477 void * original;
5578 space = to_multiple (size);
5679 cudaMalloc (&original, space);
5780 if (!original) {
58- throw std::runtime_error (" Error allocating cuda memory in AlignedCudaBuffer " );
81+ throw std::runtime_error (" Error allocating cuda memory in AlignedBuffer " );
5982 }
6083 ptrOriginal = original;
6184 ptrAligned = std::align (ALIGMNEMT, size, original, space);
62- SPDLOG_TRACE (" AlignedCudaBuffer : original {}, aligned {}, size {}, space {}" ,
85+ SPDLOG_TRACE (" AlignedBuffer : original {}, aligned {}, size {}, space {}" ,
6386 (void *)(ptrOriginal), (void *)(ptrAligned), size, space);
6487 // sanity check should always be true when std::align used
6588 assert (space >= size);
6689 }
6790 //
68- ~AlignedCudaBuffer () {
91+ ~AlignedBuffer () {
6992 if (ptrOriginal) {
7093 SPDLOG_DEBUG (" Destroying cuda buffer {}" , ptrOriginal);
7194 cudaFree (ptrOriginal);
7295 }
7396 }
7497 };
98+ #endif
7599
76100 template <typename MemorySpace, typename ... Properties>
77101 struct comm_storage_wrapper {
@@ -97,16 +121,16 @@ namespace ippl::comms {
97121 // Note that this makes no effort to preserve any existing data
98122 void reallocBuffer (size_type newsize) {
99123 // wipe the old memory, before allocating new, (help prevent out-of-space errors)
100- buffer = AlignedCudaBuffer ();
124+ buffer = AlignedBuffer<memory_space> ();
101125 // allocate new
102- buffer = AlignedCudaBuffer (newsize);
126+ buffer = AlignedBuffer<memory_space> (newsize);
103127 view = buffer_type ((pointer_type)buffer.ptrAligned , newsize);
104128 SPDLOG_DEBUG (" Realloc : view {}, aligned {}, size {}, space {}" , (void *)(view.data ()),
105129 (void *)(buffer.ptrAligned ), newsize, buffer.space );
106130 }
107131 //
108- AlignedCudaBuffer buffer;
109132 buffer_type view;
133+ AlignedBuffer<memory_space> buffer;
110134 };
111135
112136 // ---------------------------------------------
0 commit comments