mirror of
https://gitlab.com/niansa/discord_llama.git
synced 2025-03-06 20:48:25 +01:00
parent
be9828adea
commit
a6be845652
14 changed files with 787 additions and 13 deletions
3
.gitmodules
vendored
3
.gitmodules
vendored
|
@ -4,6 +4,3 @@
|
|||
[submodule "libjustlm"]
|
||||
path = libjustlm
|
||||
url = https://gitlab.com/niansa/libjustlm.git
|
||||
[submodule "cosched"]
|
||||
path = cosched
|
||||
url = https://gitlab.com/niansa/cosched.git
|
||||
|
|
|
@ -5,13 +5,10 @@ project(discord_llama LANGUAGES C CXX)
|
|||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
set(LM_COSCHED ON)
|
||||
set(ANYPROC_COSCHED ON)
|
||||
|
||||
add_subdirectory(libjustlm)
|
||||
add_subdirectory(anyproc)
|
||||
add_subdirectory(cosched)
|
||||
add_subdirectory(DPP)
|
||||
add_subdirectory(thread-pool)
|
||||
add_subdirectory(fmt)
|
||||
|
||||
add_executable(discord_llama
|
||||
|
@ -19,7 +16,7 @@ add_executable(discord_llama
|
|||
config.hpp config.cpp
|
||||
utils.cpp utils.hpp
|
||||
)
|
||||
target_link_libraries(discord_llama PUBLIC dpp fmt pthread libjustlm anyproc ggml threadpool cosched sqlite3)
|
||||
target_link_libraries(discord_llama PUBLIC dpp fmt pthread libjustlm anyproc ggml threadpool sqlite3)
|
||||
|
||||
install(TARGETS discord_llama
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
|
||||
|
|
2
anyproc
2
anyproc
|
@ -1 +1 @@
|
|||
Subproject commit a184fef76409c4667bd004e98157819af657cfb0
|
||||
Subproject commit c90cea8ac8d5182c0e9ca5dc515500ad668bca4d
|
1
cosched
1
cosched
|
@ -1 +0,0 @@
|
|||
Subproject commit e53d6c2c7e38371bd5c4ead731f2bcc006c27694
|
|
@ -1 +1 @@
|
|||
Subproject commit 5a57db5e7522e817abd95700ae73c23c19c9d3f5
|
||||
Subproject commit 57364ae560253257f89a5164960a27d4bed242db
|
4
main.cpp
4
main.cpp
|
@ -23,12 +23,12 @@
|
|||
#include <justlm.hpp>
|
||||
#include <justlm_pool.hpp>
|
||||
#include <anyproc.hpp>
|
||||
#include <scheduled_thread.hpp>
|
||||
#include <ThreadPool.h>
|
||||
|
||||
|
||||
|
||||
class Bot {
|
||||
CoSched::ScheduledThread scheduler;
|
||||
ThreadPool thread_pool{1};
|
||||
LM::InferencePool llm_pool;
|
||||
std::unique_ptr<Translator> translator;
|
||||
std::vector<dpp::snowflake> my_messages;
|
||||
|
|
52
thread-pool/.gitignore
vendored
Normal file
52
thread-pool/.gitignore
vendored
Normal file
|
@ -0,0 +1,52 @@
|
|||
# Build
|
||||
#VS
|
||||
.vs/
|
||||
Debug/
|
||||
Release/
|
||||
# Netbeans
|
||||
nbproject/
|
||||
*.user
|
||||
*.filters
|
||||
*.vcxproj
|
||||
*.sln
|
||||
|
||||
# Cmake
|
||||
build
|
||||
CMakeCache.txt
|
||||
CMakeFiles
|
||||
CMakeScripts
|
||||
Makefile
|
||||
cmake_install.cmake
|
||||
install_manifest.txt
|
||||
CTestTestfile.cmake
|
||||
|
||||
# Compiled Object files
|
||||
*.slo
|
||||
*.lo
|
||||
*.o
|
||||
*.obj
|
||||
!assets/models/*/*.obj
|
||||
|
||||
# Precompiled Headers
|
||||
*.gch
|
||||
*.pch
|
||||
|
||||
# Compiled Dynamic libraries
|
||||
*.so
|
||||
*.dylib
|
||||
*.dll
|
||||
|
||||
# Fortran module files
|
||||
*.mod
|
||||
*.smod
|
||||
|
||||
# Compiled Static libraries
|
||||
*.lai
|
||||
*.la
|
||||
*.a
|
||||
*.lib
|
||||
|
||||
# Executables
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
5
thread-pool/CMakeLists.txt
Normal file
5
thread-pool/CMakeLists.txt
Normal file
|
@ -0,0 +1,5 @@
|
|||
cmake_minimum_required (VERSION 3.5)
|
||||
project (threadpool)
|
||||
|
||||
add_library(threadpool INTERFACE)
|
||||
target_include_directories(threadpool INTERFACE include)
|
21
thread-pool/LICENSE
Normal file
21
thread-pool/LICENSE
Normal file
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2016 Mariano Trebino
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
380
thread-pool/README.md
Normal file
380
thread-pool/README.md
Normal file
|
@ -0,0 +1,380 @@
|
|||
# Table of Contents
|
||||
[Introduction](https://github.com/mtrebi/thread-pool/blob/master/README.md#introduction) <br/>
|
||||
[Build instructions](https://github.com/mtrebi/thread-pool/blob/master/README.md#build-instructions) <br/>
|
||||
[Thread pool](https://github.com/mtrebi/thread-pool/blob/master/README.md#thread-pool)<br/>
|
||||
[Queue](https://github.com/mtrebi/thread-pool/blob/master/README.md#queue)<br/>
|
||||
[Submit function](https://github.com/mtrebi/thread-pool/blob/master/README.md#submit-function) <br/>
|
||||
[Thread worker](https://github.com/mtrebi/thread-pool/blob/master/README.md#thread-worker) <br/>
|
||||
[Usage example](https://github.com/mtrebi/thread-pool/blob/master/README.md#usage-example) <br/>
|
||||
[Use case#1](https://github.com/mtrebi/thread-pool#use-case-1) <br/>
|
||||
[Use case#2](https://github.com/mtrebi/thread-pool#use-case-2) <br/>
|
||||
[Use case#3](https://github.com/mtrebi/thread-pool#use-case-3) <br/>
|
||||
[Future work](https://github.com/mtrebi/thread-pool/blob/master/README.md#future-work) <br/>
|
||||
[References](https://github.com/mtrebi/thread-pool/blob/master/README.md#references) <br/>
|
||||
|
||||
# Introduction:
|
||||
|
||||
A [thread pool](https://en.wikipedia.org/wiki/Thread_pool) is a technique that allows developers to exploit the concurrency of modern processors in an **easy** and **efficient** manner. It's easy because you send "work" to the pool and somehow this work gets done without blocking the main thread. It's efficient because threads are not initialized each time we want the work to be done. Threads are initialized once and remain inactive until some work has to be done. This way we minimize the overhead.
|
||||
|
||||
There are many more Thread pool implementations in C++, many of them are probably better (safer, faster...) than mine. However,I believe my implementation are **very straightforward and easy to understand**.
|
||||
|
||||
__Disclaimer: Please Do not use this project in a professional environment. It may contain bugs and/or not work as expected.__ I did this project to learn how C++11 Threads work and provide an easy way for other people to understand it too.
|
||||
|
||||
# Build instructions:
|
||||
|
||||
This project has been developed using Netbeans and Linux but it should work on Windows, MAC OS and Linux. It can be easily build using CMake and different other generators. The following code can be used to generate the VS 2017 project files:
|
||||
|
||||
```c
|
||||
// VS 2017
|
||||
cd <project-folder>
|
||||
mkdir build
|
||||
cd build/
|
||||
cmake .. "Visual Studio 15 2017 Win64"
|
||||
```
|
||||
|
||||
Then, from VS you can edit and execute the project. Make sure that __main project is set up as the startup project__
|
||||
|
||||
If you are using Linux, you need to change the generator (use the default) and execute an extra operation to actually make it executable:
|
||||
|
||||
```c
|
||||
// Linux
|
||||
cd <project-folder>
|
||||
mkdir build
|
||||
cd build/
|
||||
cmake ..
|
||||
make
|
||||
```
|
||||
|
||||
# Thread pool
|
||||
|
||||
The way that I understand things better is with images. So, let's take a look at the image of thread pool given by Wikipedia:
|
||||
|
||||
<p align="center"> <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/0/0c/Thread_pool.svg/580px-Thread_pool.svg.png"> </p>
|
||||
|
||||
As you can see, we have three important elements here:
|
||||
* *Tasks Queue*. This is where the work that has to be done is stored.
|
||||
* *Thread Pool*. This is set of threads (or workers) that continuously take work from the queue and do it.
|
||||
* *Completed Tasks*. When the Thread has finished the work we return "something" to notify that the work has finished.
|
||||
|
||||
## Queue
|
||||
|
||||
We use a queue to store the work because it's the more sensible data structure. We want the work to be **started** in the same order that we sent it. However, this queue is a little bit **special**. As I said in the previous section, threads are continuously (well, not really, but let's assume that they are) querying the queue to ask for work. When there's work available, threads take the work from the queue and do it. What would happen if two threads try to take the same work at the same time? Well, the program would crash.
|
||||
|
||||
To avoid these kinds of problems, I implemented a wrapper over the standard C++ Queue that uses mutex to restrict the concurrent access. Let's see a small sample of the SafeQueue class:
|
||||
|
||||
```c
|
||||
void enqueue(T& t) {
|
||||
std::unique_lock<std::mutex> lock(m_mutex);
|
||||
m_queue.push(t);
|
||||
}
|
||||
|
||||
```
|
||||
To enqueue the first thing we do is lock the mutex to make sure that no one else is accessing the resource. Then, we push the element to the queue. When the lock goes out of scopes it gets automatically released. Easy, huh? This way, we make the Queue thread-safe and thus we don't have to worry many threads accessing and/or modifying it at the same "time".
|
||||
|
||||
## Submit function
|
||||
|
||||
The most important method of the thread pool is the one responsible of adding work to the queue. I called this method **submit**. It's not difficult to understand how it works but its implementation can seem scary at first. Let's think about **what** should do and after that we will worry about **how** to do it. What:
|
||||
* Accept any function with any parameters.
|
||||
* Return "something" immediately to avoid blocking main thread. This returned object should **eventually** contain the result of the operation.
|
||||
|
||||
Cool, let's see **how** we can implement it.
|
||||
|
||||
### Submit implementation
|
||||
|
||||
The complete submit functions looks like this:
|
||||
|
||||
```c
|
||||
// Submit a function to be executed asynchronously by the pool
|
||||
template<typename F, typename...Args>
|
||||
auto submit(F&& f, Args&&... args) -> std::future<decltype(f(args...))> {
|
||||
// Create a function with bounded parameters ready to execute
|
||||
std::function<decltype(f(args...))()> func = std::bind(std::forward<F>(f), std::forward<Args>(args)...);
|
||||
// Encapsulate it into a shared ptr in order to be able to copy construct / assign
|
||||
auto task_ptr = std::make_shared<std::packaged_task<decltype(f(args...))()>>(func);
|
||||
|
||||
// Wrap packaged task into void function
|
||||
std::function<void()> wrapper_func = [task_ptr]() {
|
||||
(*task_ptr)();
|
||||
};
|
||||
|
||||
// Enqueue generic wrapper function
|
||||
m_queue.enqueue(wrapperfunc);
|
||||
|
||||
// Wake up one thread if its waiting
|
||||
m_conditional_lock.notify_one();
|
||||
|
||||
// Return future from promise
|
||||
return task_ptr->get_future();
|
||||
}
|
||||
```
|
||||
|
||||
Nevertheless, we're going to inspect line by line what's going on in order to fully understand how it works.
|
||||
|
||||
#### Variadic template function
|
||||
|
||||
```c
|
||||
template<typename F, typename...Args>
|
||||
```
|
||||
|
||||
This means that the next statement is templated. The first template parameter is called F (our function) and second one is a parameter pack. A parameter pack is a special template parameter that can accept zero or more template arguments. It is, in fact, a way to express a variable number of arguments in a template. A template with at least one parameter pack is called **variadic template**
|
||||
|
||||
Summarizing, we are telling the compiler that our submit function is going to take one generic parameter of type F (our function) and a parameter pack Args (the parameters of the function F).
|
||||
|
||||
#### Function declaration
|
||||
|
||||
```c
|
||||
auto submit(F&& f, Args&&... args) -> std::future<decltype(f(args...))> {
|
||||
```
|
||||
|
||||
This may seem weird but, it's not. A function, in fact, can be declared using two different syntaxes. The following is the most well known:
|
||||
|
||||
```c
|
||||
return-type identifier ( argument-declarations... )
|
||||
```
|
||||
|
||||
But, we can also declare the function like this:
|
||||
|
||||
```c
|
||||
auto identifier ( argument-declarations... ) -> return_type
|
||||
```
|
||||
|
||||
Why two syntaxes? Well, imagine that you have a function that has a return type that depends on the input parameters of the function. Using the first syntax you can't declare that function without getting a compiler error since you would be using a variable in the return type that has not been declared yet (because the return type declaration goes before the parameters type declaration).
|
||||
|
||||
Using the second syntax you can declare the function to have return type **auto** then, using the -> you can declare the return type depending on the arguments of the functions that have been declared previously.
|
||||
|
||||
Now, let's inspect the parameters of the submit function. When the type of a parameter is declared as **T&&** for some deducted type T that parameter is a **universal reference**. This term was coined by [Scott Meyers](https://isocpp.org/blog/2012/11/universal-references-in-c11-scott-meyers) because **T&&** can also mean r-value reference. However, in the context of type deduction, it means that it can be bound to both l-values and r-values, unlike l-value references that can only be bound to non-const objects (they bind only to modifiable lvalues) and r-value references (they bind only to rvalues).
|
||||
|
||||
|
||||
The return type of the function is of type **std::future<T>**. An std::future is a special type that provides a mechanism to access the result of asynchronous operations, in our case, the result of executing a specific function. This makes sense with what we said earlier.
|
||||
|
||||
Finally, the template type of std::future is **decltype(f(args...))**. Decltype is a special C++ keyword that inspects the declared type of an entity or the type and value category of an expression. In our case, we want to know the return type of the function _f_, so we give decltype our generic function _f_ and the parameter pack _args_.
|
||||
|
||||
#### Function body
|
||||
|
||||
```c
|
||||
// Create a function with bounded parameters ready to execute
|
||||
std::function<decltype(f(args...))()> func = std::bind(std::forward<F>(f), std::forward<Args>(args)...);
|
||||
```
|
||||
|
||||
There are many many things happening here. First of all, the **std::bind(F, Args)** is a function that creates a wrapper for F with the given Args. Caling this wrapper is the same as calling F with the Args that it has been bound. Here, we are simply calling bind with our generic function _f_ and the parameter pack _args_ but using another wrapper **std::forward<T>(t)** for each parameter. This second wrapper is needed to achieve perfect forwarding of universal references.
|
||||
The result of this bind call is a **std::function<T>**. The std::function<T> is a C++ object that encapsulates a function. It allows you to execute the function as if it were a normal function calling the operator() with the required parameters BUT, because it is an object, you can store it, copy it and move it around. The template type of any std::function is the signature of that function: std::function< return-type (arguments)>. In this case, we already know how to get the return type of this function using decltype. But, what about the arguments? Well, because we bound all arguments _args_ to the function _f_ we just have to add an empty pair of parenthesis that represents an empty list of arguments: **decltype(f(args...))()**.
|
||||
|
||||
|
||||
```c
|
||||
// Encapsulate it into a shared ptr in order to be able to copy construct / assign
|
||||
auto task_ptr = std::make_shared<std::packaged_task<decltype(f(args...))()>>(func);
|
||||
```
|
||||
|
||||
The next thing we do is we create a **std::packaged_task<T>(t)**. A packaged_task is a wrapper around a function that can be executed asynchronously. It's result is stored in a shared state inside an std::future<T> object. The templated type T of an std::packaged_task<T>(t) is the type of the function _t_ that is wrapping. Because we said it before, the signature of the function _f_ is **decltype(f(args...))()** that is the same type of the packaged_task. Then, we just wrap again this packaged task inside a **std::shared_ptr** using the initialize function **std::make_shared**.
|
||||
|
||||
```c
|
||||
// Wrap packaged task into void function
|
||||
std::function<void()> wrapperfunc = [task_ptr]() {
|
||||
(*task_ptr)();
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
Again, we create a std:.function, but, note that this time its template type is **void()**. Independently of the function _f_ and its parameters _args_ this _wrapperfunc_ the return type will always be **void**. Since all functions _f_ may have different return types, the only way to store them in a container (our Queue) is wrapping them with a generic void function. Here, we are just declaring this _wrapperfunc_ to execute the actual task _taskptr_ that will execute the bound function _func_.
|
||||
|
||||
```c
|
||||
// Enqueue generic wrapper function
|
||||
m_queue.enqueue(wrapperfunc);
|
||||
```
|
||||
|
||||
We enqueue this _wrapperfunc_.
|
||||
|
||||
```c
|
||||
// Wake up one thread if its waiting
|
||||
m_conditional_lock.notify_one();
|
||||
```
|
||||
|
||||
Before finishing, we wake up one thread in case it is waiting.
|
||||
|
||||
```c
|
||||
// Return future from promise
|
||||
return task_ptr->get_future();
|
||||
```
|
||||
|
||||
And finally, we return the future of the packaged_task. Because we are returning the future that is bound to the packaged_task _taskptr_ that, at the same time, is bound with the function _func_, executing this _taskptr_ will automatically update the future. Because we wrapped the execution of the _taskptr_ with a generic wrapper function, is the execution of _wrapperfunc_ that, in fact, updates the future. Aaaaand. since we enqueued this wrapper function, it will be executed by a thread after being dequeued calling the operator().
|
||||
|
||||
|
||||
## Thread worker
|
||||
|
||||
Now that we understand how the submit method works, we're going to focus on how the work gets done. Probably, the simplest implementation of a thread worker could be using polling:
|
||||
|
||||
Loop
|
||||
If Queue is not empty
|
||||
Dequeue work
|
||||
Do it
|
||||
|
||||
This looks alright but it's **not very efficient**. Do you see why? What would happen if there is no work in the Queue? The threads would keep looping and asking all the time: Is the queue empty?
|
||||
|
||||
The more sensible implementation is done by "sleeping" the threads until some work is added to the queue. As we saw before, as soon as we enqueue work, a signal **notify_one()** is sent. This allows us to implement a more efficient algorithm:
|
||||
|
||||
Loop
|
||||
If Queue is empty
|
||||
Wait signal
|
||||
Dequeue work
|
||||
Do it
|
||||
|
||||
This signal system is implemented in C++ with **conditional variables**. Conditional variables are always bound to a mutex, so I added a mutex to the thread pool class just to manage this. The final code of a worker looks like this:
|
||||
|
||||
```c
|
||||
void operator()() {
|
||||
std::function<void()> func;
|
||||
bool dequeued;
|
||||
while (!m_pool->m_shutdown) {
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(m_pool->m_conditional_mutex);
|
||||
if (m_pool->m_queue.empty()) {
|
||||
m_pool->m_conditional_lock.wait(lock);
|
||||
}
|
||||
dequeued = m_pool->m_queue.dequeue(func);
|
||||
}
|
||||
if (dequeued) {
|
||||
func();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
The code is really easy to understand so I am not going to explain anything. The only thing to note here is that, _func_ is our wrapper function declared as:
|
||||
|
||||
```c
|
||||
std::function<void()> wrapperfunc = [task_ptr]() {
|
||||
(*task_ptr)();
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
So, executing this function will automatically update the future.
|
||||
|
||||
# Usage example
|
||||
|
||||
Creating the thread pool is as easy as:
|
||||
|
||||
```c
|
||||
// Create pool with 3 threads
|
||||
ThreadPool pool(3);
|
||||
|
||||
// Initialize pool
|
||||
pool.init();
|
||||
```
|
||||
|
||||
When we want to shutdown the pool just call:
|
||||
|
||||
```c
|
||||
// Shutdown the pool, releasing all threads
|
||||
pool.shutdown()
|
||||
```
|
||||
|
||||
Ff we want to send some work to the pool, after we have initialized it, we just have to call the submit function:
|
||||
|
||||
```c
|
||||
pool.submit(work);
|
||||
```
|
||||
|
||||
Depending on the type of work, I've distinguished different use-cases. Suppose that the work that we have to do is multiply two numbers. We can do it in many different ways. I've implemented the three most common ways to do it that I can imagine:
|
||||
* Use-Case #1. Function returns the result
|
||||
* Use-Case #2. Function updates by ref parameter with the result
|
||||
* Use-Case #3. Function prints the result
|
||||
|
||||
_Note: This is just to show how the submit function works. Options are not exclusive_
|
||||
|
||||
## Use-Case #1
|
||||
The multiply function with a return looks like this:
|
||||
|
||||
```c
|
||||
// Simple function that adds multiplies two numbers and returns the result
|
||||
int multiply(const int a, const int b) {
|
||||
const int res = a * b;
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
Then, the submit:
|
||||
|
||||
```c
|
||||
// The type of future is given by the return type of the function
|
||||
std::future<int> future = pool.submit(multiply, 2, 3);
|
||||
```
|
||||
|
||||
We can also use the **auto** keyword for convenience:
|
||||
|
||||
```c
|
||||
auto future = pool.submit(multiply, 2, 3);
|
||||
```
|
||||
|
||||
Nice, when the work is finished by the thread pool we know that the future will get updated and we can retrieve the result calling:
|
||||
```c
|
||||
const int result = future.get();
|
||||
std::cout << result << std::endl;
|
||||
```
|
||||
|
||||
The get() function of std::future<T> always return the type T of the future. **This type will always be equal to the return type of the function passed to the submit method**. In this case, int.
|
||||
|
||||
## Use-Case #2
|
||||
The multiply function has a parameter passed by ref:
|
||||
|
||||
```c
|
||||
// Simple function that adds multiplies two numbers and updates the out_res variable passed by ref
|
||||
void multiply(int& out_res, const int a, const int b) {
|
||||
out_res = a * b;
|
||||
}
|
||||
```
|
||||
|
||||
Now, we have to call the submit function with a subtle difference. Because we are using templates and type deduction (universal references), the parameter passed by ref needs to be called using **std::ref(param)** to make sure that we are passing it by ref and not by value.
|
||||
|
||||
```c
|
||||
int result = 0;
|
||||
auto future = pool.submit(multiply, std::ref(result), 2, 3);
|
||||
// result is 0
|
||||
future.get();
|
||||
// result is 6
|
||||
std::cout << result << std::endl;
|
||||
```
|
||||
|
||||
In this case, what's the type of future? Well, as I said before, the return type will always be equal to the return type of the function passed to the submit method. Because this function is of type void, the future is **std::future<void>**. Calling future.get() returns void. That's not very useful, but we still need to call .get() to make sure that the work has been done.
|
||||
|
||||
## Use-Case #3
|
||||
The last case is the easiest one. Our multiply function simply prints the result:
|
||||
|
||||
We have a simple function without output parameters. For this example I implemented the following multiplication function:
|
||||
|
||||
```c
|
||||
// Simple function that adds multiplies two numbers and prints the result
|
||||
void multiply(const int a, const int b) {
|
||||
const int result = a * b;
|
||||
std::cout << result << std::endl;
|
||||
}
|
||||
```
|
||||
|
||||
Then, we can simply call:
|
||||
|
||||
```c
|
||||
auto future = pool.submit(multiply, 2, 3);
|
||||
future.get();
|
||||
```
|
||||
|
||||
In this case, we know that as soon as the multiplication is done it will be printed. If we care when this is done, we can wait for it calling future.get().
|
||||
|
||||
Checkout the [main](https://github.com/mtrebi/thread-pool/blob/master/src/main.cpp) program for a complete example.
|
||||
|
||||
# Future work
|
||||
|
||||
* Make it more reliable and safer (exceptions)
|
||||
* Find a better way to use it with member functions (thanks to @rajenk)
|
||||
* Run benchmarks and improve performance if needed
|
||||
* Evaluate performance and impact of std::function in the heap and try alternatives if necessary. (thanks to @JensMunkHansen)
|
||||
|
||||
# References
|
||||
|
||||
* [MULTI-THREADED PROGRAMMING TERMINOLOGY - 2017](http://www.bogotobogo.com/cplusplus/multithreaded.php): Fast analysis of how a multi-thread system works
|
||||
|
||||
* [Universal References in C++11—Scott Meyers](https://isocpp.org/blog/2012/11/universal-references-in-c11-scott-meyers): Universal references in C++11 by Scott Meyers
|
||||
|
||||
* [Perfect forwarding and universal references in C++](http://eli.thegreenplace.net/2014/perfect-forwarding-and-universal-references-in-c/): Article about how and when to use perfect forwarding and universal references
|
||||
|
||||
* [C++ documentation](http://www.cplusplus.com/reference/): Thread, conditional variables, mutex and many others...
|
100
thread-pool/affinity.patch
Normal file
100
thread-pool/affinity.patch
Normal file
|
@ -0,0 +1,100 @@
|
|||
--- ThreadPool.h Wed May 17 15:01:04 2017
|
||||
+++ ThreadPool.h Sun Mar 4 04:32:07 2018
|
||||
@@ -1,5 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
+#ifdef AFFINITY
|
||||
+#if defined __sun__
|
||||
+#include <sys/types.h>
|
||||
+#include <sys/processor.h>
|
||||
+#include <sys/procset.h>
|
||||
+#include <unistd.h> /* For sysconf */
|
||||
+#elif __linux__
|
||||
+#include <cstdio> /* For fprintf */
|
||||
+#include <sched.h>
|
||||
+#endif
|
||||
+#endif
|
||||
+
|
||||
+#include <cstddef> /* For std::size_t */
|
||||
#include <functional>
|
||||
#include <future>
|
||||
#include <mutex>
|
||||
@@ -14,10 +27,10 @@
|
||||
private:
|
||||
class ThreadWorker {
|
||||
private:
|
||||
- int m_id;
|
||||
+ std::size_t m_id;
|
||||
ThreadPool * m_pool;
|
||||
public:
|
||||
- ThreadWorker(ThreadPool * pool, const int id)
|
||||
+ ThreadWorker(ThreadPool * pool, const std::size_t id)
|
||||
: m_pool(pool), m_id(id) {
|
||||
}
|
||||
|
||||
@@ -45,7 +58,7 @@
|
||||
std::mutex m_conditional_mutex;
|
||||
std::condition_variable m_conditional_lock;
|
||||
public:
|
||||
- ThreadPool(const int n_threads)
|
||||
+ ThreadPool(const std::size_t n_threads)
|
||||
: m_threads(std::vector<std::thread>(n_threads)), m_shutdown(false) {
|
||||
}
|
||||
|
||||
@@ -57,7 +70,44 @@
|
||||
|
||||
// Inits thread pool
|
||||
void init() {
|
||||
- for (int i = 0; i < m_threads.size(); ++i) {
|
||||
+ #if (defined __sun__ || defined __linux__) && defined AFFINITY
|
||||
+ std::size_t v_cpu = 0;
|
||||
+ std::size_t v_cpu_max = std::thread::hardware_concurrency() - 1;
|
||||
+ #endif
|
||||
+
|
||||
+ #if defined __sun__ && defined AFFINITY
|
||||
+ std::vector<processorid_t> v_cpu_id; /* Struct for CPU/core ID */
|
||||
+
|
||||
+ processorid_t i, cpuid_max;
|
||||
+ cpuid_max = sysconf(_SC_CPUID_MAX);
|
||||
+ for (i = 0; i <= cpuid_max; i++) {
|
||||
+ if (p_online(i, P_STATUS) != -1) /* Get only online cores ID */
|
||||
+ v_cpu_id.push_back(i);
|
||||
+ }
|
||||
+ #endif
|
||||
+
|
||||
+ for (std::size_t i = 0; i < m_threads.size(); ++i) {
|
||||
+
|
||||
+ #if (defined __sun__ || defined __linux__) && defined AFFINITY
|
||||
+ if (v_cpu > v_cpu_max) {
|
||||
+ v_cpu = 0;
|
||||
+ }
|
||||
+
|
||||
+ #ifdef __sun__
|
||||
+ processor_bind(P_LWPID, P_MYID, v_cpu_id[v_cpu], NULL);
|
||||
+ #elif __linux__
|
||||
+ cpu_set_t mask;
|
||||
+ CPU_ZERO(&mask);
|
||||
+ CPU_SET(v_cpu, &mask);
|
||||
+ pthread_t thread = pthread_self();
|
||||
+ if (pthread_setaffinity_np(thread, sizeof(cpu_set_t), &mask) != 0) {
|
||||
+ fprintf(stderr, "Error setting thread affinity\n");
|
||||
+ }
|
||||
+ #endif
|
||||
+
|
||||
+ ++v_cpu;
|
||||
+ #endif
|
||||
+
|
||||
m_threads[i] = std::thread(ThreadWorker(this, i));
|
||||
}
|
||||
}
|
||||
--- SafeQueue.h Wed May 17 15:01:04 2017
|
||||
+++ SafeQueue.h Sun Mar 4 04:32:07 2018
|
||||
@@ -28,7 +28,7 @@
|
||||
return m_queue.empty();
|
||||
}
|
||||
|
||||
- int size() {
|
||||
+ std::size_t size() {
|
||||
std::unique_lock<std::mutex> lock(m_mutex);
|
||||
return m_queue.size();
|
||||
}
|
72
thread-pool/example/main.cpp
Normal file
72
thread-pool/example/main.cpp
Normal file
|
@ -0,0 +1,72 @@
|
|||
#include <iostream>
|
||||
#include <random>
|
||||
|
||||
#include "../include/ThreadPool.h"
|
||||
|
||||
std::random_device rd;
|
||||
std::mt19937 mt(rd());
|
||||
std::uniform_int_distribution<int> dist(-1000, 1000);
|
||||
auto rnd = std::bind(dist, mt);
|
||||
|
||||
|
||||
void simulate_hard_computation() {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(2000 + rnd()));
|
||||
}
|
||||
|
||||
// Simple function that adds multiplies two numbers and prints the result
|
||||
void multiply(const int a, const int b) {
|
||||
simulate_hard_computation();
|
||||
const int res = a * b;
|
||||
std::cout << a << " * " << b << " = " << res << std::endl;
|
||||
}
|
||||
|
||||
// Same as before but now we have an output parameter
|
||||
void multiply_output(int & out, const int a, const int b) {
|
||||
simulate_hard_computation();
|
||||
out = a * b;
|
||||
std::cout << a << " * " << b << " = " << out << std::endl;
|
||||
}
|
||||
|
||||
// Same as before but now we have an output parameter
|
||||
int multiply_return(const int a, const int b) {
|
||||
simulate_hard_computation();
|
||||
const int res = a * b;
|
||||
std::cout << a << " * " << b << " = " << res << std::endl;
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
// Create pool with 3 threads
|
||||
ThreadPool pool(3);
|
||||
|
||||
// Initialize pool
|
||||
pool.init();
|
||||
|
||||
// Submit (partial) multiplication table
|
||||
for (int i = 1; i < 3; ++i) {
|
||||
for (int j = 1; j < 10; ++j) {
|
||||
pool.submit(multiply, i, j);
|
||||
}
|
||||
}
|
||||
|
||||
// Submit function with output parameter passed by ref
|
||||
int output_ref;
|
||||
auto future1 = pool.submit(multiply_output, std::ref(output_ref), 5, 6);
|
||||
|
||||
// Wait for multiplication output to finish
|
||||
future1.get();
|
||||
std::cout << "Last operation result is equals to " << output_ref << std::endl;
|
||||
|
||||
// Submit function with return parameter
|
||||
auto future2 = pool.submit(multiply_return, 5, 3);
|
||||
|
||||
// Wait for multiplication output to finish
|
||||
int res = future2.get();
|
||||
std::cout << "Last operation result is equals to " << res << std::endl;
|
||||
|
||||
pool.shutdown();
|
||||
|
||||
return 0;
|
||||
}
|
52
thread-pool/include/SafeQueue.h
Normal file
52
thread-pool/include/SafeQueue.h
Normal file
|
@ -0,0 +1,52 @@
|
|||
#pragma once
|
||||
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
|
||||
// Thread safe implementation of a Queue using an std::queue
|
||||
template <typename T>
|
||||
class SafeQueue {
|
||||
private:
|
||||
std::queue<T> m_queue;
|
||||
std::mutex m_mutex;
|
||||
public:
|
||||
SafeQueue() {
|
||||
|
||||
}
|
||||
|
||||
SafeQueue(SafeQueue& other) {
|
||||
//TODO:
|
||||
}
|
||||
|
||||
~SafeQueue() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
bool empty() {
|
||||
std::unique_lock<std::mutex> lock(m_mutex);
|
||||
return m_queue.empty();
|
||||
}
|
||||
|
||||
int size() {
|
||||
std::unique_lock<std::mutex> lock(m_mutex);
|
||||
return m_queue.size();
|
||||
}
|
||||
|
||||
void enqueue(T& t) {
|
||||
std::unique_lock<std::mutex> lock(m_mutex);
|
||||
m_queue.push(t);
|
||||
}
|
||||
|
||||
bool dequeue(T& t) {
|
||||
std::unique_lock<std::mutex> lock(m_mutex);
|
||||
|
||||
if (m_queue.empty()) {
|
||||
return false;
|
||||
}
|
||||
t = std::move(m_queue.front());
|
||||
|
||||
m_queue.pop();
|
||||
return true;
|
||||
}
|
||||
};
|
99
thread-pool/include/ThreadPool.h
Normal file
99
thread-pool/include/ThreadPool.h
Normal file
|
@ -0,0 +1,99 @@
|
|||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <future>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <thread>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "SafeQueue.h"
|
||||
|
||||
class ThreadPool {
|
||||
private:
|
||||
class ThreadWorker {
|
||||
private:
|
||||
int m_id;
|
||||
ThreadPool * m_pool;
|
||||
public:
|
||||
ThreadWorker(ThreadPool * pool, const int id)
|
||||
: m_pool(pool), m_id(id) {
|
||||
}
|
||||
|
||||
void operator()() {
|
||||
std::function<void()> func;
|
||||
bool dequeued;
|
||||
while (!m_pool->m_shutdown) {
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(m_pool->m_conditional_mutex);
|
||||
if (m_pool->m_queue.empty()) {
|
||||
m_pool->m_conditional_lock.wait(lock);
|
||||
}
|
||||
dequeued = m_pool->m_queue.dequeue(func);
|
||||
}
|
||||
if (dequeued) {
|
||||
func();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
bool m_shutdown;
|
||||
SafeQueue<std::function<void()>> m_queue;
|
||||
std::vector<std::thread> m_threads;
|
||||
std::mutex m_conditional_mutex;
|
||||
std::condition_variable m_conditional_lock;
|
||||
public:
|
||||
ThreadPool(const int n_threads)
|
||||
: m_threads(std::vector<std::thread>(n_threads)), m_shutdown(false) {
|
||||
}
|
||||
|
||||
ThreadPool(const ThreadPool &) = delete;
|
||||
ThreadPool(ThreadPool &&) = delete;
|
||||
|
||||
ThreadPool & operator=(const ThreadPool &) = delete;
|
||||
ThreadPool & operator=(ThreadPool &&) = delete;
|
||||
|
||||
// Inits thread pool
|
||||
void init() {
|
||||
for (int i = 0; i < m_threads.size(); ++i) {
|
||||
m_threads[i] = std::thread(ThreadWorker(this, i));
|
||||
}
|
||||
}
|
||||
|
||||
// Waits until threads finish their current task and shutdowns the pool
|
||||
void shutdown() {
|
||||
m_shutdown = true;
|
||||
m_conditional_lock.notify_all();
|
||||
|
||||
for (int i = 0; i < m_threads.size(); ++i) {
|
||||
if(m_threads[i].joinable()) {
|
||||
m_threads[i].join();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Submit a function to be executed asynchronously by the pool
|
||||
template<typename F, typename...Args>
|
||||
auto submit(F&& f, Args&&... args) -> std::future<decltype(f(args...))> {
|
||||
// Create a function with bounded parameters ready to execute
|
||||
std::function<decltype(f(args...))()> func = std::bind(std::forward<F>(f), std::forward<Args>(args)...);
|
||||
// Encapsulate it into a shared ptr in order to be able to copy construct / assign
|
||||
auto task_ptr = std::make_shared<std::packaged_task<decltype(f(args...))()>>(func);
|
||||
|
||||
// Wrap packaged task into void function
|
||||
std::function<void()> wrapper_func = [task_ptr]() {
|
||||
(*task_ptr)();
|
||||
};
|
||||
|
||||
// Enqueue generic wrapper function
|
||||
m_queue.enqueue(wrapper_func);
|
||||
|
||||
// Wake up one thread if its waiting
|
||||
m_conditional_lock.notify_one();
|
||||
|
||||
// Return future from promise
|
||||
return task_ptr->get_future();
|
||||
}
|
||||
};
|
Loading…
Add table
Reference in a new issue