Do parallel digest calculation when there are multiple reels (#855).
authorCarl Hetherington <cth@carlh.net>
Tue, 19 Jul 2016 20:09:12 +0000 (21:09 +0100)
committerCarl Hetherington <cth@carlh.net>
Wed, 27 Jul 2016 12:15:34 +0000 (13:15 +0100)
src/lib/reel_writer.cc
src/lib/reel_writer.h
src/lib/writer.cc
src/lib/writer.h

index e6533d2cc292ca0e9c49f8da78a41f0604b5a276..d576eb2a0bfad1c0eb12ce827be8525bd87ace02 100644 (file)
@@ -417,16 +417,14 @@ ReelWriter::create_reel (list<ReferencedReelAsset> const & refs, list<shared_ptr
 }
 
 void
-ReelWriter::calculate_digests (shared_ptr<Job> job)
+ReelWriter::calculate_digests (boost::function<void (float)> 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);
        }
 }
 
index 6e27171ecb0d9fb37f39331e8a0fb285e9dac316..9dc7401710fe39d857e7da1ac176169c8eaf69a9 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
@@ -57,7 +57,7 @@ public:
 
        void finish ();
        boost::shared_ptr<dcp::Reel> create_reel (std::list<ReferencedReelAsset> const & refs, std::list<boost::shared_ptr<Font> > const & fonts);
-       void calculate_digests (boost::shared_ptr<Job> job);
+       void calculate_digests (boost::function<void (float)> set_progress);
 
        Frame start () const;
 
index efd43a25b8e52971a5baf421889d9d96bfc18626..1874e68f498cb9302ebe98aea990233c05000a19 100644 (file)
@@ -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 = _job.lock ();
+       job->sub (_("Computing digests"));
+
+       boost::asio::io_service service;
+       boost::thread_group pool;
+
+       shared_ptr<boost::asio::io_service::work> 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<void (float)> 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 = _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<boost::thread::id, float>::const_iterator i = _digest_progresses.begin(); i != _digest_progresses.end(); ++i) {
+               min_progress = min (min_progress, i->second);
+       }
+
+       job->set_progress (min_progress);
+}
index 170307d8d5580862a2412995b9d29e5bd4f7711a..c1d7e4f8bdd41e9afc96f73147676cd3e6283756 100644 (file)
@@ -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<const Film> _film;
@@ -150,6 +151,9 @@ private:
        */
        int _pushed_to_disk;
 
+       boost::mutex _digest_progresses_mutex;
+       std::map<boost::thread::id, float> _digest_progresses;
+
        std::list<ReferencedReelAsset> _reel_assets;
 
        std::list<boost::shared_ptr<Font> > _fonts;