TinyLlama.cpp 1.0
A lightweight C++ implementation of the TinyLlama language model
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
ThreadPool Class Reference

Thread pool for parallel tensor loading operations. More...

#include <safetensors_loader.h>

Collaboration diagram for ThreadPool:
Collaboration graph

Public Member Functions

 ThreadPool (size_t num_threads)
 Constructs a thread pool with specified number of threads.
 
 ~ThreadPool ()
 Destructor that ensures proper cleanup of threads.
 
template<class F , class... Args>
std::future< typename std::result_of< F(Args...)>::type > submit (F &&f, Args &&... args)
 Submits a task to the thread pool.
 

Private Attributes

std::vector< std::thread > workers_
 
std::queue< std::function< void()> > tasks_
 
std::mutex queue_mutex_
 
std::condition_variable condition_
 
bool stop_ = false
 

Detailed Description

Thread pool for parallel tensor loading operations.

Manages a pool of worker threads for parallel processing of tensor data. Used by SafeTensorsLoader for parallel loading operations.

Definition at line 260 of file safetensors_loader.h.

Constructor & Destructor Documentation

◆ ThreadPool()

ThreadPool::ThreadPool ( size_t  num_threads)
explicit

Constructs a thread pool with specified number of threads.

Parameters
num_threadsNumber of worker threads to create

Definition at line 684 of file safetensors_loader.cpp.

684 : stop_(false) {
685 for (size_t i = 0; i < num_threads; ++i) {
686 workers_.emplace_back([this] {
687 while (true) {
688 std::function<void()> task;
689 {
690 std::unique_lock<std::mutex> lock(this->queue_mutex_);
691 this->condition_.wait(lock, [this] {
692 return this->stop_ || !this->tasks_.empty();
693 });
694 if (this->stop_ && this->tasks_.empty()) return;
695 task = std::move(this->tasks_.front());
696 this->tasks_.pop();
697 }
698 if(task) task();
699 }
700 });
701 }
702}
std::queue< std::function< void()> > tasks_
std::vector< std::thread > workers_
std::condition_variable condition_
std::mutex queue_mutex_

References condition_, queue_mutex_, stop_, tasks_, and workers_.

◆ ~ThreadPool()

ThreadPool::~ThreadPool ( )

Destructor that ensures proper cleanup of threads.

Definition at line 704 of file safetensors_loader.cpp.

704 {
705 {
706 std::unique_lock<std::mutex> lock(queue_mutex_);
707 stop_ = true;
708 }
709 condition_.notify_all();
710 for (std::thread& worker : workers_) {
711 if (worker.joinable()) {
712 worker.join();
713 }
714 }
715}

References condition_, queue_mutex_, stop_, and workers_.

Member Function Documentation

◆ submit()

template<class F , class... Args>
std::future< typename std::result_of< F(Args...)>::type > ThreadPool::submit ( F &&  f,
Args &&...  args 
)

Submits a task to the thread pool.

Template Parameters
FFunction type
ArgsArgument types
Parameters
fFunction to execute
argsArguments for the function
Returns
Future containing the result of the function

Definition at line 294 of file safetensors_loader.h.

295 {
296 using return_type = typename std::result_of<F(Args...)>::type;
297
298 auto task = std::make_shared<std::packaged_task<return_type()>>(
299 std::bind(std::forward<F>(f), std::forward<Args>(args)...));
300
301 std::future<return_type> res = task->get_future();
302 {
303 std::unique_lock<std::mutex> lock(queue_mutex_);
304 if (stop_) throw std::runtime_error("submit on stopped ThreadPool");
305 tasks_.emplace([task]() { (*task)(); });
306 }
307 condition_.notify_one();
308 return res;
309}

References condition_, queue_mutex_, stop_, and tasks_.

Referenced by SafeTensorsLoader::load_all_tensors_parallel().

Member Data Documentation

◆ condition_

std::condition_variable ThreadPool::condition_
private

Condition variable for thread synchronization

Definition at line 288 of file safetensors_loader.h.

Referenced by submit(), ThreadPool(), and ~ThreadPool().

◆ queue_mutex_

std::mutex ThreadPool::queue_mutex_
private

Mutex for task queue access

Definition at line 287 of file safetensors_loader.h.

Referenced by submit(), ThreadPool(), and ~ThreadPool().

◆ stop_

bool ThreadPool::stop_ = false
private

Flag to stop worker threads

Definition at line 289 of file safetensors_loader.h.

Referenced by submit(), ThreadPool(), and ~ThreadPool().

◆ tasks_

std::queue<std::function<void()> > ThreadPool::tasks_
private

Queue of pending tasks

Definition at line 286 of file safetensors_loader.h.

Referenced by submit(), and ThreadPool().

◆ workers_

std::vector<std::thread> ThreadPool::workers_
private

Worker threads

Definition at line 285 of file safetensors_loader.h.

Referenced by ThreadPool(), and ~ThreadPool().


The documentation for this class was generated from the following files: