Skip to content

Commit 008f8cd

Browse files
author
Christopher Dunn
committed
GW_USE_DEVICE_ALLOCATOR_FILE
based on gw_enable_caching_allocator This way, after the library is built and installed, the user does not need to specify `-DGW_ENABLE_CACHING_ALLOCATOR`, which would select the DefaultDeviceAllocator to use.
1 parent 52024f5 commit 008f8cd

File tree

5 files changed

+161
-39
lines changed

5 files changed

+161
-39
lines changed

common/base/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ set(MODULE_NAME gwbase)
2121
set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -std=c++14")
2222
message(STATUS "nvcc flags for ${MODULE_NAME}: ${CUDA_NVCC_FLAGS}")
2323

24+
configure_file(include/claraparabricks/genomeworks/utils/allocator.in include/claraparabricks/genomeworks/utils/allocator.hpp)
25+
2426
get_property(gw_library_type GLOBAL PROPERTY gw_library_type)
2527
add_library(${MODULE_NAME} ${gw_library_type}
2628
src/cudautils.cpp
@@ -40,7 +42,9 @@ if (gw_device_synchronize_kernels)
4042
endif()
4143

4244
if(gw_enable_caching_allocator)
43-
target_compile_definitions(${MODULE_NAME} PUBLIC GW_ENABLE_CACHING_ALLOCATOR)
45+
set(GW_USE_DEVICE_ALLOCATOR_FILE "use_caching_device_allocator.hpp")
46+
else()
47+
set(GW_USE_DEVICE_ALLOCATOR_FILE "use_cuda_malloc_allocator.hpp")
4448
endif()
4549

4650
target_include_directories(${MODULE_NAME}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2019-2020 NVIDIA CORPORATION.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#pragma once
18+
19+
#include @GW_USE_DEVICE_ALLOCATOR_FILE@
20+
21+
namespace claraparabricks
22+
{
23+
24+
namespace genomeworks
25+
{
26+
27+
/// Gets the size of the largest free memory block in the allocator
28+
///
29+
/// @see create_default_device_allocator
30+
/// \return returns the size in bytes
31+
inline int64_t get_size_of_largest_free_memory_block(DefaultDeviceAllocator const& allocator)
32+
{
33+
return allocator.get_size_of_largest_free_memory_block();
34+
}
35+
36+
} // namespace genomeworks
37+
38+
} // namespace claraparabricks

common/base/include/claraparabricks/genomeworks/utils/allocator.hpp renamed to common/base/include/claraparabricks/genomeworks/utils/allocators.hpp

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -319,44 +319,6 @@ class CachingDeviceAllocator
319319
cudaStream_t default_stream_;
320320
};
321321

322-
#ifdef GW_ENABLE_CACHING_ALLOCATOR
323-
using DefaultDeviceAllocator = CachingDeviceAllocator<char, details::DevicePreallocatedAllocator>;
324-
#else
325-
using DefaultDeviceAllocator = CudaMallocAllocator<char>;
326-
#endif
327-
328-
/// Gets the size of the largest free memory block in the allocator
329-
///
330-
/// \return returns the size in bytes
331-
inline int64_t get_size_of_largest_free_memory_block(DefaultDeviceAllocator const& allocator)
332-
{
333-
return allocator.get_size_of_largest_free_memory_block();
334-
}
335-
336-
/// Constructs a DefaultDeviceAllocator
337-
///
338-
/// This function provides a way to construct a valid DefaultDeviceAllocator
339-
/// for all possible DefaultDeviceAllocators.
340-
/// Use this function to obtain a DefaultDeviceAllocator object.
341-
/// This function is needed, since construction of CachingDeviceAllocator
342-
/// requires a max_caching_size argument to obtain a valid allocator.
343-
/// Default constuction of CachingDeviceAllocator yields an dummy object
344-
/// which cannot allocate memory.
345-
/// \param max_cached_bytes max bytes used by memory resource used by CachingDeviceAllocator (default: 2GiB, unused for CudaMallocAllocator)
346-
/// \param default_stream if a call to allocate() does not specify any streams this stream will be used instead (unused for CudaMallocAllocator)
347-
inline DefaultDeviceAllocator create_default_device_allocator(std::size_t max_caching_size = 2ull * 1024 * 1024 * 1024,
348-
cudaStream_t default_stream = 0)
349-
{
350-
#ifdef GW_ENABLE_CACHING_ALLOCATOR
351-
return DefaultDeviceAllocator(max_caching_size,
352-
default_stream);
353-
#else
354-
static_cast<void>(max_caching_size);
355-
static_cast<void>(default_stream);
356-
return DefaultDeviceAllocator();
357-
#endif
358-
}
359-
360322
} // namespace genomeworks
361323

362324
} // namespace claraparabricks
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2019-2020 NVIDIA CORPORATION.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef GW_INCLUDED_DEVICE_ALLOCATOR_HPP
18+
#define GW_INCLUDED_DEVICE_ALLOCATOR_HPP
19+
20+
#ifdef GW_ENABLE_CACHING_ALLOCATOR
21+
#warning "GW_ENABLE_CACHING_ALLOCATOR should not be already set."
22+
#else
23+
#define GW_ENABLE_CACHING_ALLOCATOR
24+
#endif
25+
26+
#include "allocators.hpp"
27+
28+
namespace claraparabricks
29+
{
30+
31+
namespace genomeworks
32+
{
33+
34+
using DefaultDeviceAllocator = CachingDeviceAllocator<char, details::DevicePreallocatedAllocator>;
35+
36+
/// Constructs a DefaultDeviceAllocator
37+
///
38+
/// This function provides a way to construct a valid DefaultDeviceAllocator
39+
/// for all possible DefaultDeviceAllocators.
40+
/// Use this function to obtain a DefaultDeviceAllocator object.
41+
/// This function is needed, since construction of CachingDeviceAllocator
42+
/// requires a max_caching_size argument to obtain a valid allocator.
43+
/// Default constuction of CachingDeviceAllocator yields an dummy object
44+
/// which cannot allocate memory.
45+
/// \param max_cached_bytes max bytes used by memory resource used by CachingDeviceAllocator (default: 2GiB)
46+
/// \param default_stream if a call to allocate() does not specify any streams this stream will be used instead
47+
inline DefaultDeviceAllocator create_default_device_allocator(std::size_t max_caching_size = 2ull * 1024 * 1024 * 1024,
48+
cudaStream_t default_stream = 0)
49+
{
50+
return DefaultDeviceAllocator(max_caching_size,
51+
default_stream);
52+
}
53+
54+
} // namespace genomeworks
55+
56+
} // namespace claraparabricks
57+
58+
#else
59+
#error "Attempted to included 2 DeviceAllocators!"
60+
#endif
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2019-2020 NVIDIA CORPORATION.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef GW_INCLUDED_DEVICE_ALLOCATOR_HPP
18+
#define GW_INCLUDED_DEVICE_ALLOCATOR_HPP
19+
20+
#ifdef GW_ENABLE_CACHING_ALLOCATOR
21+
#warning "GW_ENABLE_CACHING_ALLOCATOR should not be set for CudaMallocAllocator."
22+
#undef GW_ENABLE_CACHING_ALLOCATOR
23+
#endif
24+
25+
namespace claraparabricks
26+
{
27+
28+
namespace genomeworks
29+
{
30+
31+
using DefaultDeviceAllocator = CudaMallocAllocator<char>;
32+
33+
/// Constructs a DefaultDeviceAllocator
34+
///
35+
/// This function provides a way to construct a valid DefaultDeviceAllocator
36+
/// for all possible DefaultDeviceAllocators.
37+
/// Use this function to obtain a DefaultDeviceAllocator object.
38+
/// This function is needed, since construction of CachingDeviceAllocator
39+
/// requires a max_caching_size argument to obtain a valid allocator.
40+
/// Default constuction of CachingDeviceAllocator yields an dummy object
41+
/// which cannot allocate memory.
42+
/// \param max_cached_bytes max bytes used by memory resource used by CachingDeviceAllocator (unused)
43+
/// \param default_stream if a call to allocate() does not specify any streams this stream will be used instead (unused)
44+
inline DefaultDeviceAllocator create_default_device_allocator(std::size_t max_caching_size = 2ull * 1024 * 1024 * 1024,
45+
cudaStream_t default_stream = 0)
46+
{
47+
static_cast<void>(max_caching_size);
48+
static_cast<void>(default_stream);
49+
return DefaultDeviceAllocator();
50+
}
51+
52+
} // namespace genomeworks
53+
54+
} // namespace claraparabricks
55+
56+
#else
57+
#error "Attempted to included 2 DeviceAllocators!"
58+
#endif

0 commit comments

Comments
 (0)