Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Threadpool support for matrix multiplication #124

Merged
merged 25 commits into from
Jul 30, 2020
Merged

Threadpool support for matrix multiplication #124

merged 25 commits into from
Jul 30, 2020

Conversation

bcebere
Copy link
Member

@bcebere bcebere commented Jul 29, 2020

Description

The current flow spawns a number of threads on every multiplication.This can lead to denial of service, we should have a controlled way of creating new threads.

For that, this PR extends the current approach with:

  • A blocking queue and a thread pool implementation, for executing the jobs.
  • Move the thread creating responsability in the TenSEAL context.
  • Rework the multiplication with globals/mutexes by spliting the input in batches and using C++'s future promises in the main thread.
  • Added Google benchmark for measuring the performance per operations.
  • TODO: Comment the code.

fixes #118

Checklist

@bcebere bcebere requested review from youben11 and philomath213 July 29, 2020 19:52
@youben11 youben11 added the Type: Improvement 📈 Performance improvement not introducing a new feature or requiring a major refactor label Jul 29, 2020
@bcebere bcebere added the Type: Refactor 🔨 A complete overhaul of a file, feature, or codebase label Jul 29, 2020
Copy link
Member

@youben11 youben11 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing work Bogdan! Threadpool is gonna serve us a lot, and I love the benchmarks.

README.md Outdated Show resolved Hide resolved
tenseal/cpp/context/tensealcontext.h Outdated Show resolved Hide resolved
tenseal/cpp/tensors/ckksvector.h Outdated Show resolved Hide resolved
Comment on lines 111 to 127
if (tenseal_context->get_concurrency() == 1)
return worker_func(0, vector_size);

size_t thread_cnt = tenseal_context->get_concurrency();
std::vector<std::future<Ciphertext>> future_results;
size_t batch_size = (vector_size + thread_cnt - 1) / thread_cnt;

for (size_t i = 0; i < thread_cnt; i++) {
future_results.push_back(tenseal_context->dispatcher()->enqueue_task(
worker_func, i * batch_size,
std::min((i + 1) * batch_size, vector_size)));
}

for (size_t i = 0; i < tenseal_context->get_concurrency(); i++) {
tenseal_context->evaluator->add_inplace(result,
future_results[i].get());
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this new design without threads synchronization! But I'm wondering about the distribution of ranges, my thoughts about whether specify the range in advance vs. let them compute collectively till the end is that the former doesn't include synchronization but works well in case where threads might not run in an equitable manner (suppose one thread doesn't run like the other, so they will still wait for it to finish its range when other threads might have finished it), while the latter include synchronization but make the threads work hard till the end. All this is just me with my imagination, I don't know how this translate in practice, do you have any thoughts about this? But of course, when we don't have synchronization, we can go up to many threads without the fear of threads' synchronization becoming a bottleneck.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, you're saying that this loop might get stuck at 0, let's say, waiting for a slow thread, while the others finished the job, right?

Windows has a mechanism for waiting for multiple objects efficiently, unfortunately I cannot find something similar in standard C++ library, but I'll research it. I was looking for a solution too here, it's not ideal to loop over the futures and do a blocking wait.

I could experiment with more designs. One other idea was to add the results to a blocking queue, and notify the main thread using a condition variable. That might schedule the operations more efficiently.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current design is great, I don't think we should jump directly into a different one, I just wanted to give it some thoughts. But this is definitely something I would merge

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll make a test with a condition variable&mutex, to see how the benchmarks look that way.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a test using a mutex and a condition variable to notify the main thread(instead of waiting for a std::future), but the benchmarks look pretty similar.

I think it needs better load testing here, I am not sure how to simulate the scenario.

Maybe a random sleep in each worker might show the performance differences(cond variable vs future.get() )
I have the git stash anyway, I can try to add a test in another PR.

Copy link
Member

@youben11 youben11 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! I just think the API for matmul with n_jobs would be great

vec.matmul(matrix, n_jobs=j) with:

  • 0 being the default (which uses the current number of threads in the threadpool)
  • j > 0, first verify that the current number of threads is greater or equal than j, and run only j jobs

tenseal/binding.cpp Outdated Show resolved Hide resolved
@bcebere bcebere requested a review from youben11 July 30, 2020 11:22
Copy link
Member

@youben11 youben11 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Just one last thing, is it okay to create more jobs than there is threads? I thought we must throw an error there... not sure though

@bcebere bcebere merged commit 5d64a98 into master Jul 30, 2020
@delete-merged-branch delete-merged-branch bot deleted the threadpool branch July 30, 2020 14:56
pierreeliseeflory pushed a commit to pierreeliseeflory/TenSEAL that referenced this pull request Apr 27, 2022
* add benchmarks for mamul_plain operation

* add threadpool implementation

Co-authored-by: Ayoub Benaissa <ayouben9@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Improvement 📈 Performance improvement not introducing a new feature or requiring a major refactor Type: Refactor 🔨 A complete overhaul of a file, feature, or codebase
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement Thread Pool
2 participants