From ea49dd17eea804a2569531ea4cb8e1bd216717f7 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Tue, 19 Jul 2016 21:09:12 +0100 Subject: [PATCH] Do parallel digest calculation when there are multiple reels (#855). --- src/lib/reel_writer.cc | 8 +++----- src/lib/reel_writer.h | 4 ++-- src/lib/writer.cc | 46 +++++++++++++++++++++++++++++++++++++++--- src/lib/writer.h | 4 ++++ 4 files changed, 52 insertions(+), 10 deletions(-) diff --git a/src/lib/reel_writer.cc b/src/lib/reel_writer.cc index e6533d2cc..d576eb2a0 100644 --- a/src/lib/reel_writer.cc +++ b/src/lib/reel_writer.cc @@ -417,16 +417,14 @@ ReelWriter::create_reel (list const & refs, list job) +ReelWriter::calculate_digests (boost::function set_progress) { - job->sub (_("Computing image digest")); if (_picture_asset) { - _picture_asset->hash (boost::bind (&Job::set_progress, job.get(), _1, false)); + _picture_asset->hash (set_progress); } if (_sound_asset) { - job->sub (_("Computing audio digest")); - _sound_asset->hash (boost::bind (&Job::set_progress, job.get(), _1, false)); + _sound_asset->hash (set_progress); } } diff --git a/src/lib/reel_writer.h b/src/lib/reel_writer.h index 6e27171ec..9dc740171 100644 --- a/src/lib/reel_writer.h +++ b/src/lib/reel_writer.h @@ -1,5 +1,5 @@ /* - Copyright (C) 2012-2015 Carl Hetherington + Copyright (C) 2012-2016 Carl Hetherington This file is part of DCP-o-matic. @@ -57,7 +57,7 @@ public: void finish (); boost::shared_ptr create_reel (std::list const & refs, std::list > const & fonts); - void calculate_digests (boost::shared_ptr job); + void calculate_digests (boost::function set_progress); Frame start () const; diff --git a/src/lib/writer.cc b/src/lib/writer.cc index efd43a25b..1874e68f4 100644 --- a/src/lib/writer.cc +++ b/src/lib/writer.cc @@ -58,6 +58,9 @@ using std::pair; using std::string; using std::list; using std::cout; +using std::map; +using std::min; +using std::max; using boost::shared_ptr; using boost::weak_ptr; using boost::dynamic_pointer_cast; @@ -456,12 +459,34 @@ Writer::finish () dcp.add (cpl); + /* Calculate digests for each reel in parallel */ + + shared_ptr job = _job.lock (); + job->sub (_("Computing digests")); + + boost::asio::io_service service; + boost::thread_group pool; + + shared_ptr work (new boost::asio::io_service::work (service)); + + int const threads = max (1, Config::instance()->num_local_encoding_threads ()); + + for (int i = 0; i < threads; ++i) { + pool.create_thread (boost::bind (&boost::asio::io_service::run, &service)); + } + BOOST_FOREACH (ReelWriter& i, _reels) { + boost::function set_progress = boost::bind (&Writer::set_digest_progress, this, job.get(), _1); + service.post (boost::bind (&ReelWriter::calculate_digests, &i, set_progress)); + } - shared_ptr job = _job.lock (); - DCPOMATIC_ASSERT (job); - i.calculate_digests (job); + work.reset (); + pool.join_all (); + service.stop (); + /* Add reels to CPL */ + + BOOST_FOREACH (ReelWriter& i, _reels) { cpl->add (i.create_reel (_reel_assets, _fonts)); } @@ -597,3 +622,18 @@ Writer::video_reel (int frame) const DCPOMATIC_ASSERT (i < _reels.size ()); return i; } + +void +Writer::set_digest_progress (Job* job, float progress) +{ + /* I believe this is thread-safe */ + _digest_progresses[boost::this_thread::get_id()] = progress; + + boost::mutex::scoped_lock lm (_digest_progresses_mutex); + float min_progress = 0; + for (map::const_iterator i = _digest_progresses.begin(); i != _digest_progresses.end(); ++i) { + min_progress = min (min_progress, i->second); + } + + job->set_progress (min_progress); +} diff --git a/src/lib/writer.h b/src/lib/writer.h index 170307d8d..c1d7e4f8b 100644 --- a/src/lib/writer.h +++ b/src/lib/writer.h @@ -113,6 +113,7 @@ private: void terminate_thread (bool); bool have_sequenced_image_at_queue_head (); size_t video_reel (int frame) const; + void set_digest_progress (Job* job, float progress); /** our Film */ boost::shared_ptr _film; @@ -150,6 +151,9 @@ private: */ int _pushed_to_disk; + boost::mutex _digest_progresses_mutex; + std::map _digest_progresses; + std::list _reel_assets; std::list > _fonts; -- 2.30.2