Skip to content

Commit

Permalink
Adding thread syncronization variable to class members and passing la…
Browse files Browse the repository at this point in the history
…mbda for thread signalling

Signed-off-by: Akshay Tondak <[email protected]>
  • Loading branch information
Akshay Tondak authored and Akshay Tondak committed Sep 19, 2024
1 parent 90a4c6d commit af505f2
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 39 deletions.
17 changes: 0 additions & 17 deletions src/runtime_src/core/tools/common/TestRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,20 +499,3 @@ TestRunner::get_test_header()
ptree.put("explicit", m_explicit);
return ptree;
}

// Method to wait for threads to be ready
// Parameters:
// - thread_num: Number of threads to wait for
// - mut: Mutex to lock the critical section
// - cond_var: Condition variable to wait on
// - thread_ready: Counter to track the number of ready threads
void TestRunner::wait_for_threads_ready(const int thread_num, std::mutex& mut, std::condition_variable& cond_var, int& thread_ready) {
std::unique_lock<std::mutex> lock(mut);
while (thread_ready != thread_num) {
lock.unlock();
std::this_thread::sleep_for(std::chrono::microseconds(10));
lock.lock();
}
cond_var.notify_all();
lock.unlock();
}
1 change: 0 additions & 1 deletion src/runtime_src/core/tools/common/TestRunner.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class TestRunner : public JSONConfigurable {
const std::string & getConfigName() const { return get_name(); };
virtual const std::string& getConfigDescription() const { return m_description; };
boost::property_tree::ptree get_test_header();
void wait_for_threads_ready(const int thread_num, std::mutex& mut, std::condition_variable& cond_var, int& thread_ready);

// Child class helper methods
protected:
Expand Down
12 changes: 2 additions & 10 deletions src/runtime_src/core/tools/common/tests/TestHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void BO_set::set_kernel_args(xrt::run& run) {
// - cond_var: Condition variable to wait on
// - thread_ready: Counter to track the number of ready threads
void
TestCase::run(std::mutex& mut, std::condition_variable& cond_var, int& thread_ready)
TestCase::run(std::function<void()> thread_ready_to_run)
{
std::vector<xrt::kernel> kernels;
std::vector<BO_set> bo_set_list;
Expand All @@ -77,7 +77,7 @@ TestCase::run(std::mutex& mut, std::condition_variable& cond_var, int& thread_re
}

// Signal that the current thread is ready to run
thread_ready_to_run(mut, cond_var, thread_ready);
thread_ready_to_run();

for (int i = 0; i < itr_count; i++) {
// Start all runs in the queue so that they run in parallel
Expand All @@ -90,11 +90,3 @@ TestCase::run(std::mutex& mut, std::condition_variable& cond_var, int& thread_re
}
}
}

// Method to signal that a thread is ready to run
void TestCase::thread_ready_to_run(std::mutex& mut, std::condition_variable& cond_var, int& thread_ready) {
std::unique_lock<std::mutex> lock(mut);
thread_ready++;
cond_var.wait(lock);
lock.unlock();
}
5 changes: 1 addition & 4 deletions src/runtime_src/core/tools/common/tests/TestHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ class TestCase {
size_t buffer_size; // Size of the buffer
int itr_count; // Number of iterations

// Method to signal that a thread is ready to run
void thread_ready_to_run(std::mutex&, std::condition_variable&, int&);

public:
// Constructor to initialize the test case with xclbin and kernel name with hardware context creation
TestCase(xrt::xclbin& xclbin, std::string& kernel, xrt::device& device)
Expand All @@ -52,6 +49,6 @@ class TestCase {
hw_ctx = xrt::hw_context(device, xclbin.get_uuid());
}

void run(std::mutex&, std::condition_variable&, int&);
void run(std::function<void()>);
};
#endif
33 changes: 27 additions & 6 deletions src/runtime_src/core/tools/common/tests/TestSpatialSharingOvd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "xrt/xrt_device.h"
#include "xrt/xrt_hw_context.h"
#include "xrt/xrt_kernel.h"
#include "experimental/xrt_ini.h"
#include <thread>

namespace XBU = XBUtilities;
Expand All @@ -24,6 +25,7 @@ static constexpr size_t host_app = 1; //opcode
boost::property_tree::ptree TestSpatialSharingOvd::run(std::shared_ptr<xrt_core::device> dev) {
// Clear any existing "xclbin" entry in the property tree
ptree.erase("xclbin");
xrt::ini::set("Runtime.dummy_app_context_type", "proxy");

// Query the xclbin name from the device
const auto xclbin_name = xrt_core::device_query<xrt_core::query::xclbin_name>(dev, xrt_core::query::xclbin_name::type::validate);
Expand Down Expand Up @@ -81,9 +83,6 @@ boost::property_tree::ptree TestSpatialSharingOvd::run(std::shared_ptr<xrt_core:
auto working_dev = xrt::device(dev);
working_dev.register_xclbin(xclbin);

std::mutex mut;
std::condition_variable cond_var;
int thread_ready = 0;

/* Run 1 */
std::vector<std::thread> threads;
Expand All @@ -93,10 +92,18 @@ boost::property_tree::ptree TestSpatialSharingOvd::run(std::shared_ptr<xrt_core:
testcases.emplace_back(xclbin, kernelName, working_dev);
testcases.emplace_back(xclbin, kernelName, working_dev);

// Lambda funtion to signal that a thread is ready to run
auto thread_ready_to_run = [&]() {
std::unique_lock<std::mutex> lock(mut);
thread_ready++;
cond_var.notify_all();
lock.unlock();
};

// Lambda function to run a test case. This will be sent to individual thread to be run.
auto runTestcase = [&](TestCase& test) {
try {
test.run(mut, cond_var, thread_ready);
test.run(thread_ready_to_run);
} catch (const std::exception& ex) {
logger(ptree, "Error", ex.what());
ptree.put("status", test_token_failed);
Expand All @@ -109,7 +116,7 @@ boost::property_tree::ptree TestSpatialSharingOvd::run(std::shared_ptr<xrt_core:
threads.emplace_back(runTestcase, std::ref(testcases[1]));

// Wait for both threads to be ready to begin clocking
wait_for_threads_ready((int)threads.size(), mut, cond_var, thread_ready);
wait_for_threads_ready((int)threads.size());

// Measure the latency for running the test cases in parallel
auto start = std::chrono::high_resolution_clock::now();
Expand All @@ -128,7 +135,7 @@ boost::property_tree::ptree TestSpatialSharingOvd::run(std::shared_ptr<xrt_core:
std::thread thr(runTestcase, std::ref(t));

// Wait for the thread to be ready
wait_for_threads_ready(1, mut, cond_var, thread_ready);
wait_for_threads_ready(1);

// Measure the latency for running the test case in a single thread
start = std::chrono::high_resolution_clock::now();
Expand All @@ -148,3 +155,17 @@ boost::property_tree::ptree TestSpatialSharingOvd::run(std::shared_ptr<xrt_core:
ptree.put("status", test_token_passed);
return ptree;
}

// Method to wait for threads to be ready
// Parameters:
// - thread_num: Number of threads to wait for
void TestSpatialSharingOvd::wait_for_threads_ready(const int thread_num) {
std::unique_lock<std::mutex> lock(mut);
while (thread_ready != thread_num) {
lock.unlock();
std::this_thread::sleep_for(std::chrono::microseconds(10));
lock.lock();
}
cond_var.notify_all();
lock.unlock();
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,19 @@

// Class representing the TestSpatialSharingOvd test
class TestSpatialSharingOvd : public TestRunner {
std::mutex mut;
std::condition_variable cond_var;
int thread_ready;

public:
boost::property_tree::ptree ptree;

boost::property_tree::ptree run(std::shared_ptr<xrt_core::device> dev);
void wait_for_threads_ready(const int thread_num);

// Constructor to initialize the test runner with a name and description
TestSpatialSharingOvd()
: TestRunner("spatial-sharing-overhead", "Run Spatial Sharing Overhead Test"), ptree(get_test_header()){}
: TestRunner("spatial-sharing-overhead", "Run Spatial Sharing Overhead Test"), ptree(get_test_header()), thread_ready(0){}
};

#endif

0 comments on commit af505f2

Please sign in to comment.