Skip to content

Commit 0016bdb

Browse files
committed
[{roc,hip}fft] Revisions to accounting of system memory usage and its limit-enforcing logic
1 parent 1151d09 commit 0016bdb

File tree

17 files changed

+348
-252
lines changed

17 files changed

+348
-252
lines changed

projects/hipfft/clients/tests/gtest_main.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ int main(int argc, char* argv[])
490490
"--version",
491491
"Print queryable version information from the hipfft library's backend (and return)");
492492
app.add_option("--R", ramgb, "RAM limit in GiB for tests")
493-
->default_val(host_memory::singleton().get_total_gbytes());
493+
->default_val(system_memory::singleton().get_total_gbytes());
494494
app.add_option("--V", vramgb, "VRAM limit in GiB for tests")->default_val(0);
495495
app.add_option("--half_epsilon", half_epsilon)->default_val(9.77e-4);
496496
app.add_option("--single_epsilon", single_epsilon)->default_val(3.75e-5);
@@ -635,10 +635,9 @@ int main(int argc, char* argv[])
635635
#endif
636636

637637
// Set host memory limit from command-line options (if more restrictive)
638-
const auto usable_bytes = host_memory::singleton().get_usable_bytes();
639-
if(ramgb * ONE_GiB < usable_bytes)
640-
host_memory::singleton().set_limit_gbytes(ramgb);
641-
std::cout << "Usable host memory: " << bytes_to_GiB(host_memory::singleton().get_usable_bytes())
638+
if(ramgb * ONE_GiB < system_memory::singleton().get_limit_bytes())
639+
system_memory::singleton().set_limit_gbytes(ramgb);
640+
std::cout << "Usable system memory: " << system_memory::singleton().get_usable_gbytes()
642641
<< " GiB" << std::endl;
643642

644643
if(use_fftw_wisdom)

projects/hipfft/clients/tests/hipfft_accuracy_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ TEST_P(accuracy_test, vs_fftw)
153153
last_cpu_fft_data = last_cpu_fft_cache();
154154
GTEST_SKIP() << "host memory allocation failure";
155155
}
156-
catch(const HOSTBUF_MEM_USAGE& e)
156+
catch(const SYS_MEM_USAGE& e)
157157
{
158158
// explicitly clear cache
159159
last_cpu_fft_data = last_cpu_fft_cache();

projects/hipfft/clients/tests/hipfftw_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,7 +1983,7 @@ namespace
19831983
plan_execution_output.alloc(output_data_size);
19841984
}
19851985
}
1986-
catch(const HOSTBUF_MEM_USAGE& e)
1986+
catch(const SYS_MEM_USAGE& e)
19871987
{
19881988
GTEST_SKIP() << e.what();
19891989
}
@@ -2739,7 +2739,7 @@ namespace
27392739
else
27402740
GTEST_FAIL() << e.what() << "\nError code: " << e.hip_error << ".";
27412741
}
2742-
catch(const HOSTBUF_MEM_USAGE& e)
2742+
catch(const SYS_MEM_USAGE& e)
27432743
{
27442744
GTEST_SKIP() << e.what();
27452745
}

projects/hipfft/clients/tests/multi_stream_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ TEST_P(multiStreamTest, impulseSignalOnOutput)
623623
{
624624
GTEST_SKIP() << "host memory allocation failure";
625625
}
626-
catch(const HOSTBUF_MEM_USAGE& e)
626+
catch(const SYS_MEM_USAGE& e)
627627
{
628628
GTEST_SKIP() << e.what();
629629
}

projects/hipfft/shared/gpubuf.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121
#ifndef ROCFFT_GPUBUF_H
2222
#define ROCFFT_GPUBUF_H
2323

24+
#include "device_properties.h"
2425
#include "rocfft_hip.h"
26+
#include "sys_mem.h"
2527
#include <cstdlib>
28+
#include <sstream>
2629

2730
// Simple RAII class for GPU buffers. T is the type of pointer that
2831
// data() returns
@@ -74,6 +77,19 @@ class gpubuf_t
7477

7578
hipError_t alloc(const size_t size, bool make_it_shared = false)
7679
{
80+
free();
81+
const auto dev_prop = get_curr_device_prop();
82+
if(dev_prop.integrated && size > system_memory::singleton().get_usable_bytes())
83+
{
84+
std::stringstream msg;
85+
msg << "Unauthorized (integrated) device allocation.\n"
86+
<< "\tRequested byte size is " << size << "\n"
87+
<< "\tUsable byte size is " << system_memory::singleton().get_usable_bytes() << "\n"
88+
<< "\tFree system memory: " << system_memory::singleton().get_free_bytes() << "\n"
89+
<< "\tUsed system memory: " << system_memory::singleton().get_used_bytes() << "\n"
90+
<< "\tEnforced usage limit: " << system_memory::singleton().get_limit_bytes();
91+
throw SYS_MEM_USAGE{msg.str()};
92+
}
7793
// remember the device that was current as of alloc, so we can
7894
// free on the correct device
7995
auto ret = hipGetDevice(&device);
@@ -82,13 +98,16 @@ class gpubuf_t
8298

8399
bsize = size;
84100
is_managed_memory = use_alloc_managed() || make_it_shared;
85-
free();
86101
ret = is_managed_memory ? hipMallocManaged(&buf, bsize) : hipMalloc(&buf, bsize);
87102
if(ret != hipSuccess)
88103
{
89104
buf = nullptr;
90105
bsize = 0;
91106
}
107+
108+
if(dev_prop.integrated)
109+
system_memory::singleton().record_used_bytes(bsize);
110+
92111
return ret;
93112
}
94113

@@ -106,6 +125,10 @@ class gpubuf_t
106125
// free on the device we allocated on
107126
rocfft_scoped_device dev(device);
108127
(void)hipFree(buf);
128+
129+
const auto dev_prop = get_curr_device_prop();
130+
if(dev_prop.integrated)
131+
system_memory::singleton().release_used_bytes(bsize);
109132
}
110133
buf = nullptr;
111134
bsize = 0;

projects/hipfft/shared/hostbuf.h

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,17 @@
2323

2424
#include "arithmetic.h"
2525
#include "sys_mem.h"
26-
#include <atomic>
2726
#include <cstdlib>
2827
#include <cstring>
29-
#include <iostream>
28+
#include <sstream>
29+
3030
#include <new>
3131

3232
#ifndef _WIN32
3333
#include <stdlib.h>
3434
#include <sys/mman.h>
3535
#endif
3636

37-
struct HOSTBUF_MEM_USAGE : public std::runtime_error
38-
{
39-
using std::runtime_error::runtime_error;
40-
};
41-
4237
// Simple RAII class for host buffers. T is the type of pointer that
4338
// data() returns
4439
template <class T = void>
@@ -87,18 +82,19 @@ class hostbuf_t
8782
{
8883
free();
8984

90-
bsize = size;
91-
92-
auto usable_mem = host_memory::singleton().get_usable_bytes();
93-
if(total_used_mem + size > usable_mem)
85+
if(size > system_memory::singleton().get_usable_bytes())
9486
{
9587
std::stringstream msg;
96-
msg << "Host memory usage limit exceed (used mem: "
97-
<< bytes_to_GiB(total_used_mem + size)
98-
<< "GiB, free mem: " << bytes_to_GiB(usable_mem) << " GiB)";
99-
throw HOSTBUF_MEM_USAGE{msg.str()};
88+
msg << "Unauthorized host allocation.\n"
89+
<< "\tRequested byte size is " << size << "\n"
90+
<< "\tUsable byte size is " << system_memory::singleton().get_usable_bytes() << "\n"
91+
<< "\tFree system memory: " << system_memory::singleton().get_free_bytes() << "\n"
92+
<< "\tUsed system memory: " << system_memory::singleton().get_used_bytes() << "\n"
93+
<< "\tEnforced usage limit: " << system_memory::singleton().get_limit_bytes();
94+
throw SYS_MEM_USAGE{msg.str()};
10095
}
10196

97+
bsize = size;
10298
if(make_it_pinned)
10399
{
104100
if(hipHostMalloc(&buf, size) != hipSuccess)
@@ -142,7 +138,7 @@ class hostbuf_t
142138

143139
is_pinned_memory = make_it_pinned;
144140
bsize_track = size;
145-
total_used_mem += bsize_track;
141+
system_memory::singleton().record_used_bytes(bsize_track);
146142
}
147143

148144
size_t size() const
@@ -161,7 +157,6 @@ class hostbuf_t
161157
{
162158
if(owned)
163159
{
164-
total_used_mem -= bsize_track;
165160
if(is_pinned_memory)
166161
{
167162
(void)hipHostFree(buf);
@@ -174,6 +169,7 @@ class hostbuf_t
174169
std::free(buf);
175170
#endif
176171
}
172+
system_memory::singleton().release_used_bytes(bsize_track);
177173
}
178174
buf = nullptr;
179175
bsize = bsize_track = 0;
@@ -238,9 +234,6 @@ class hostbuf_t
238234
// Buffer size for tracking total memory usage.
239235
// When buffer is shrunk in place, bsize_track is not changed.
240236
size_t bsize_track = 0;
241-
242-
// Keeps track of total used memory for all hostbufs
243-
inline static std::atomic<size_t> total_used_mem = 0;
244237
};
245238

246239
// default hostbuf that gives out void* pointers

0 commit comments

Comments
 (0)