Skip to content

Commit

Permalink
Addressed further comments from @masterleinad
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoo committed Jun 3, 2024
1 parent d683e81 commit 85d4173
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
31 changes: 16 additions & 15 deletions benchmarks/async_alloc/async_alloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ std::vector<std::pair<size_t, double>> inner_loop_times;
//
std::pair<double, double> test(bool up) {
int iters = 50;
size_t minimum = 8 / sizeof(float); // 64K
size_t minimum = 8 / sizeof(float);

size_t gb = 1024 * 1024 * 1024 / sizeof(float); // number of floats per GiB
size_t maximum = gb; // on 32 bit, we make 1GiB the max
Expand All @@ -30,8 +30,10 @@ std::pair<double, double> test(bool up) {
}

Kokkos::Timer first_alloc_timer;
{ // Prime the pump - first long alloc -- Time it.
Kokkos::View<float *, MemorySpace> dummy("unlabeled", 64);
{ // Prime the pump - first long alloc -- Time it
// 64 bytes is an arbitrary number here. .
Kokkos::View<float *, MemorySpace> dummy(
Kokkos::view_alloc(Kokkos::WithoutInitializing, "unlabeled"), 64);
}
double first_alloc_time = first_alloc_timer.seconds();

Expand All @@ -43,7 +45,8 @@ std::pair<double, double> test(bool up) {
for (size_t num : sizes) {
inner_loop_timer.reset();
for (int i = 0; i < iters; i++) {
Kokkos::View<float *, MemorySpace> a("unlabeled", num);
Kokkos::View<float *, MemorySpace> a(
Kokkos::view_alloc(Kokkos::WithoutInitializing, "unlabeled"), num);
}
double inner_loop_time = inner_loop_timer.seconds();

Expand All @@ -67,36 +70,34 @@ int main(int argc, char *argv[]) {

// Check the env var for reporting
char *env_string = getenv("KOKKOS_CUDA_MEMPOOL_SIZE");
std::cout << "Async Malloc Benchmark: KOKKOS_CUDA_MEMPOOL_SIZE is ";
std::cout << "# Async Malloc Benchmark: KOKKOS_CUDA_MEMPOOL_SIZE is ";

if (env_string == nullptr)
std::cout << "not set,";
else
std::cout << " " << env_string << ",";

if (up)
std::cout << " memory cycling upwards \n";
std::cout << "# memory cycling upwards \n";
else
std::cout << " memory_cycling downwards \n";
std::cout << "# memory_cycling downwards \n";
std::cout << std::flush;

Kokkos::initialize(argc, argv);

inner_loop_times.reserve(34);

// Love structured bindings?
const auto [first_alloc_time, alloc_loop_time] = test(up);

std::cout << "First Alloc: " << 64 << " bytes, " << first_alloc_time
if (!up) std::reverse(inner_loop_times.begin(), inner_loop_times.end());

std::cout << "# First Alloc: " << 64 << " bytes, " << first_alloc_time
<< " sec\n";
std::cout << "Test Alloc Loop Total: " << alloc_loop_time << " sec\n";
std::cout << "Alloc Loop Timings:\n";
std::cout << "===================\n";
std::cout << "# Test Alloc Loop Total: " << alloc_loop_time << " sec\n";
std::cout << "# Alloc Loop Timings:\n";
std::cout << "# ===================\n";

std::cout << "# size (B) \t time (sec) \n";
std::cout << "# -----------------------\n";
std::sort(inner_loop_times.begin(), inner_loop_times.end(),
[=](const auto &a, const auto &b) { return a.first < b.first; });
for (auto pair : inner_loop_times) {
std::cout << pair.first << ", " << pair.second << "\n";
}
Expand Down
16 changes: 8 additions & 8 deletions core/src/Cuda/Kokkos_CudaSpace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include <unordered_set>
#include <limits>

// #include <Cuda/Kokkos_Cuda_BlockSize_Deduction.hpp>
#include <impl/Kokkos_Error.hpp>

#include <impl/Kokkos_Tools.hpp>
Expand Down Expand Up @@ -233,15 +232,15 @@ bool initializeMempool(const int device_id, const cudaStream_t stream,

// Handle exception in case the string is unconvertible
try {
requested_size = static_cast<size_t>(std::stod(mempool_size_string));
requested_size = std::stod(mempool_size_string);
} catch (...) {
std::cerr << "Unable to convert " << mempool_size_string
<< " to a number\n";
return false;
}

// Check for non-positive size memory requests
if (requested_size <= 0) {
// Check for negative size memory requests (zero is allowed)
if (requested_size < 0) {
std::cerr << "Negative amount of memory requested in allocation\n";
return false;
}
Expand All @@ -250,17 +249,18 @@ bool initializeMempool(const int device_id, const cudaStream_t stream,
requested_size *= factor;

// Check we are not asking for memory that is greater than what size_t can
// hold. Since requested can be the larger I convert the maximum of size_t
// to a double and compare those
// hold. Since the requested size can be the larger I convert the maximum of
// size_t to a double and compare those
double max_size_t = static_cast<double>(std::numeric_limits<size_t>::max());
if (requested_size > max_size_t) {
std::cerr << "Requested amount of memory " << requested_size << " exceeds "
<< "maximum alloatable size " << max_size_t << "\n";
<< "maximum allocatable size " << max_size_t << "\n";
return false;
}

// At this point requested_size should be appropriate
// neither too big nor negative.
// neither too big nor negative - even with the ceiling it should
// not be bigger than max_size_t. Safe to cast
size_t n_bytes = static_cast<size_t>(std::ceil(requested_size));

// We set up the default memory pool
Expand Down

0 comments on commit 85d4173

Please sign in to comment.