From ba97aeb0a6ff52b79f43f883c639a55c8e3cf78d Mon Sep 17 00:00:00 2001 From: Keuin Date: Wed, 20 Apr 2022 00:48:44 +0800 Subject: Reserve vector thread_pool::tasks to speed up task initializing. --- aa.h | 22 +++++++++++++++++++++- threading.h | 5 ++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/aa.h b/aa.h index 3a4def4..9622696 100644 --- a/aa.h +++ b/aa.h @@ -50,6 +50,26 @@ public: assert(samples >= 1); } + aa_viewport(const vec3 &cxyz, + const vec3 &screen_center, + uint16_t image_width, + uint16_t image_height, + double fov_h, + hitlist &world, + unsigned samples, + int threads = -1) : + cxyz(cxyz), + screen_center(screen_center), + image_width(image_width), + image_height(image_height), + screen_hw{(cxyz - screen_center).norm() * tan((double) fov_h / 2.0)}, + screen_hh{screen_hw * ((double) image_height / image_width)}, + world(world), + samples(samples), + threads((threads > 0) ? threads : (int) std::thread::hardware_concurrency()) { + assert(samples >= 1); + } + bitmap render() { static constexpr auto seed = 123456789012345678ULL; const unsigned thread_count = std::min((unsigned) threads, samples); @@ -62,7 +82,7 @@ public: uint64_t diffuse_seed; }; - thread_pool pool{thread_count, *this, images}; + thread_pool pool{thread_count, *this, images, samples}; timer tim{true}; std::cerr << "Seeding tasks..." << std::endl; diff --git a/threading.h b/threading.h index 6d79494..249a115 100644 --- a/threading.h +++ b/threading.h @@ -41,9 +41,12 @@ class thread_pool { void worker_main(); public: - explicit thread_pool(unsigned thread_count, const U &shared_ctx, V &mut_shared_ctx) : + thread_pool(unsigned thread_count, const U &shared_ctx, V &mut_shared_ctx, size_t reserve_tasks = -1) : thread_count{thread_count}, shared_ctx{shared_ctx}, mut_shared_ctx{mut_shared_ctx} { std::cerr << "Using " << (counter.is_lock_free() ? "lock-free" : "locking") << " dispatcher." << std::endl; + if (reserve_tasks > 0) { + tasks.reserve(reserve_tasks); + } } // Thread unsafe! -- cgit v1.2.3