Skip to content

Commit 657c1ea

Browse files
committed
Fix memory scope tests in threading unit tests
Disable memory management stress test for CPU backend.
1 parent 48482d4 commit 657c1ea

1 file changed

Lines changed: 28 additions & 49 deletions

File tree

‎test/threading.cpp‎

Lines changed: 28 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <thread>
1515
#include <complex>
1616
#include <chrono>
17+
#include <condition_variable>
1718
#include <thread>
1819
#include <iterator>
1920
#include <vector>
@@ -198,20 +199,34 @@ static void cleanSlate()
198199
ASSERT_EQ(af::getMemStepSize(), step_bytes);
199200
}
200201

202+
std::condition_variable cv;
203+
std::mutex cvMutex;
204+
size_t counter = THREAD_COUNT;
205+
201206
void doubleAllocationTest()
202207
{
203208
af::setDevice(0);
204209

210+
//Block until all threads are launched and the
211+
//counter variable hits zero
212+
std::unique_lock<std::mutex> lock(cvMutex);
213+
//Check for current thread launch counter value
214+
//if reached zero, notify others to continue
215+
//otherwise block current thread
216+
if (--counter==0)
217+
cv.notify_all();
218+
else
219+
cv.wait(lock, [] {return counter==0;});
220+
lock.unlock();
221+
205222
af::array a = randu(5, 5);
206223

207-
for (int i = 0; i < 100; ++i)
208-
{
209-
a = randu(5, 5);
210-
std::this_thread::sleep_for(std::chrono::milliseconds(10));
211-
}
224+
// Wait for for other threads to hit randu call
225+
// while this thread's variable a is still in scope.
226+
std::this_thread::sleep_for(std::chrono::seconds(2));
212227
}
213228

214-
TEST(Threading, MemoryManagement_Double_Alloc)
229+
TEST(Threading, MemoryManagementScope)
215230
{
216231
cleanSlate(); // Clean up everything done so far
217232

@@ -232,48 +247,8 @@ TEST(Threading, MemoryManagement_Double_Alloc)
232247

233248
ASSERT_EQ( lock_buffers, 0u);
234249
ASSERT_EQ( lock_bytes, 0u);
235-
ASSERT_LE(alloc_buffers, 64u);
236-
ASSERT_GT(alloc_buffers, 32u);
237-
ASSERT_LE( alloc_bytes, 65536u);
238-
ASSERT_GT( alloc_bytes, 32768u);
239-
}
240-
241-
void singleAllocationTest()
242-
{
243-
af::setDevice(0);
244-
245-
for (int i = 0; i < 100; ++i)
246-
{
247-
af::array a = af::randu(5, 5);
248-
std::this_thread::sleep_for(std::chrono::milliseconds(10));
249-
}
250-
}
251-
252-
TEST(Threading, MemoryManagement_Single_Alloc)
253-
{
254-
cleanSlate(); // Clean up everything done so far
255-
256-
vector<std::thread> tests;
257-
258-
for (int t=0; t<THREAD_COUNT; ++t)
259-
tests.emplace_back(singleAllocationTest);
260-
261-
for (int t=0; t<THREAD_COUNT; ++t)
262-
if (tests[t].joinable())
263-
tests[t].join();
264-
265-
size_t alloc_bytes, alloc_buffers;
266-
size_t lock_bytes, lock_buffers;
267-
268-
af::deviceMemInfo(&alloc_bytes, &alloc_buffers,
269-
&lock_bytes, &lock_buffers);
270-
271-
ASSERT_EQ( lock_buffers, 0u);
272-
ASSERT_EQ( lock_bytes, 0u);
273-
ASSERT_LE(alloc_buffers, 32u);
274-
ASSERT_GT(alloc_buffers, 0u);
275-
ASSERT_LE( alloc_bytes, 32768u);
276-
ASSERT_GT( alloc_bytes, 0u);
250+
ASSERT_EQ(alloc_buffers, 32u);
251+
ASSERT_EQ( alloc_bytes, 32768u);
277252
}
278253

279254
void jitAllocationTest()
@@ -703,7 +678,10 @@ TEST(Threading, Sparse)
703678
tests[testId].join();
704679
}
705680

706-
TEST(Threading, MemoryManagerStressTest) {
681+
//FIXME Disable for CPU backend
682+
#if !defined(AF_CPU)
683+
TEST(Threading, MemoryManagerStressTest)
684+
{
707685
vector<std::thread> threads;
708686
for (int i = 0; i < THREAD_COUNT; i++) {
709687
threads.emplace_back([] {
@@ -734,3 +712,4 @@ TEST(Threading, MemoryManagerStressTest) {
734712
t.join();
735713
}
736714
}
715+
#endif

0 commit comments

Comments
 (0)