From 6b37ad7e1d72c1719ccdd8e14c1fd39ff86f50d1 Mon Sep 17 00:00:00 2001 From: Jonathan Thomas Date: Tue, 11 Sep 2018 10:48:30 -0500 Subject: [PATCH] Limiting threads for both FFmpeg and OpenMP (attempting to find a good balance of parallel performance, while not spawning too many threads). Sometimes more is not always better. --- include/OpenMPUtilities.h | 6 ++++-- src/FFmpegReader.cpp | 4 ++-- src/FFmpegWriter.cpp | 4 ++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/include/OpenMPUtilities.h b/include/OpenMPUtilities.h index c0f5597b..1525730d 100644 --- a/include/OpenMPUtilities.h +++ b/include/OpenMPUtilities.h @@ -32,8 +32,10 @@ #include #include -// Calculate the # of OpenMP Threads to allow -#define OPEN_MP_NUM_PROCESSORS omp_get_num_procs() +// Calculate the # of OpenMP and FFmpeg Threads to allow. We are limiting both +// of these based on our own performance tests (more is not always better). +#define OPEN_MP_NUM_PROCESSORS (min(omp_get_num_procs(), 6)) +#define FF_NUM_PROCESSORS (min(omp_get_num_procs(), 12)) using namespace std; diff --git a/src/FFmpegReader.cpp b/src/FFmpegReader.cpp index 17a612c1..9711b2e5 100644 --- a/src/FFmpegReader.cpp +++ b/src/FFmpegReader.cpp @@ -153,7 +153,7 @@ void FFmpegReader::Open() pCodecCtx = AV_GET_CODEC_CONTEXT(pStream, pCodec); // Set number of threads equal to number of processors (not to exceed 16) - pCodecCtx->thread_count = min(OPEN_MP_NUM_PROCESSORS, 16); + pCodecCtx->thread_count = min(FF_NUM_PROCESSORS, 16); if (pCodec == NULL) { throw InvalidCodec("A valid video codec could not be found for this file.", path); @@ -191,7 +191,7 @@ void FFmpegReader::Open() aCodecCtx = AV_GET_CODEC_CONTEXT(aStream, aCodec); // Set number of threads equal to number of processors (not to exceed 16) - aCodecCtx->thread_count = min(OPEN_MP_NUM_PROCESSORS, 16); + aCodecCtx->thread_count = min(FF_NUM_PROCESSORS, 16); if (aCodec == NULL) { throw InvalidCodec("A valid audio codec could not be found for this file.", path); diff --git a/src/FFmpegWriter.cpp b/src/FFmpegWriter.cpp index 11017219..bd5486b9 100644 --- a/src/FFmpegWriter.cpp +++ b/src/FFmpegWriter.cpp @@ -1026,7 +1026,7 @@ void FFmpegWriter::open_audio(AVFormatContext *oc, AVStream *st) AV_GET_CODEC_FROM_STREAM(st, audio_codec) // Set number of threads equal to number of processors (not to exceed 16) - audio_codec->thread_count = min(OPEN_MP_NUM_PROCESSORS, 16); + audio_codec->thread_count = min(FF_NUM_PROCESSORS, 16); // Find the audio encoder codec = avcodec_find_encoder_by_name(info.acodec.c_str()); @@ -1100,7 +1100,7 @@ void FFmpegWriter::open_video(AVFormatContext *oc, AVStream *st) AV_GET_CODEC_FROM_STREAM(st, video_codec) // Set number of threads equal to number of processors (not to exceed 16) - video_codec->thread_count = min(OPEN_MP_NUM_PROCESSORS, 16); + video_codec->thread_count = min(FF_NUM_PROCESSORS, 16); /* find the video encoder */ codec = avcodec_find_encoder_by_name(info.vcodec.c_str());