Skip to content

Commit 489b08b

Browse files
authored
Merge pull request #13903 from rouault/GDALGetNumThreads
Add GDALGetNumThreads() and use it
2 parents 7267c9f + 8c5aae5 commit 489b08b

32 files changed

+300
-325
lines changed

alg/gdal_tps.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "gdal_alg_priv.h"
3434
#include "gdal_priv.h"
3535
#include "gdalgenericinverse.h"
36+
#include "gdal_thread_pool.h"
3637

3738
CPL_C_START
3839
CPLXMLNode *GDALSerializeTPSTransformer(void *pTransformArg);
@@ -243,15 +244,11 @@ void *GDALCreateTPSTransformerInt(int nGCPCount, const GDAL_GCP *pasGCPList,
243244
// and checking memory usage too...
244245
if (nGCPCount > 100)
245246
{
246-
const char *pszWarpThreads =
247-
CSLFetchNameValue(papszOptions, "NUM_THREADS");
248-
if (pszWarpThreads == nullptr)
249-
pszWarpThreads = CPLGetConfigOption("GDAL_NUM_THREADS", "1");
250-
if (EQUAL(pszWarpThreads, "ALL_CPUS"))
251-
nThreads = CPLGetNumCPUs();
252-
else
253-
nThreads = atoi(pszWarpThreads);
254-
nThreads = std::clamp(nThreads, 1, 2);
247+
// We don't need more than 2 threads: one for forward transformation,
248+
// and another one for reverse transformation.
249+
nThreads = GDALGetNumThreads(papszOptions, "NUM_THREADS",
250+
/* nMaxVal = */ 2,
251+
/* bDefaultAllCPUs = */ false);
255252

256253
// Do sanity checks w.r.t. available RAM
257254

alg/gdalgrid.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "cpl_vsi.h"
3737
#include "cpl_worker_thread_pool.h"
3838
#include "gdal.h"
39+
#include "gdal_thread_pool.h"
3940

4041
constexpr double TO_RADIANS = M_PI / 180.0;
4142

@@ -3266,14 +3267,8 @@ GDALGridContext *GDALGridContextCreate(GDALGridAlgorithm eAlgorithm,
32663267
/* -------------------------------------------------------------------- */
32673268
/* Start thread pool. */
32683269
/* -------------------------------------------------------------------- */
3269-
const char *pszThreads = CPLGetConfigOption("GDAL_NUM_THREADS", "ALL_CPUS");
3270-
int nThreads = 0;
3271-
if (EQUAL(pszThreads, "ALL_CPUS"))
3272-
nThreads = CPLGetNumCPUs();
3273-
else
3274-
nThreads = atoi(pszThreads);
3275-
if (nThreads > 128)
3276-
nThreads = 128;
3270+
const int nThreads = GDALGetNumThreads(GDAL_DEFAULT_MAX_THREAD_COUNT,
3271+
/* bDefaultToAllCPUs = */ true);
32773272
if (nThreads > 1)
32783273
{
32793274
psContext->poWorkerThreadPool = new CPLWorkerThreadPool();

alg/gdalpansharpen.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "../frmts/vrt/vrtdataset.h"
3434
#include "gdal_priv.h"
3535
#include "gdal_priv_templates.hpp"
36+
#include "gdal_thread_pool.h"
3637
// #include "gdalsse_priv.h"
3738

3839
// Limit types to practical use cases.
@@ -461,15 +462,8 @@ GDALPansharpenOperation::Initialize(const GDALPansharpenOptions *psOptionsIn)
461462
nThreads = CPLGetNumCPUs();
462463
else if (nThreads == 0)
463464
{
464-
const char *pszNumThreads =
465-
CPLGetConfigOption("GDAL_NUM_THREADS", nullptr);
466-
if (pszNumThreads)
467-
{
468-
if (EQUAL(pszNumThreads, "ALL_CPUS"))
469-
nThreads = CPLGetNumCPUs();
470-
else
471-
nThreads = std::max(0, std::min(128, atoi(pszNumThreads)));
472-
}
465+
nThreads = GDALGetNumThreads(GDAL_DEFAULT_MAX_THREAD_COUNT,
466+
/* bDefaulAllCPUs = */ false);
473467
}
474468
if (nThreads > 1)
475469
{

alg/gdalwarpkernel.cpp

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -309,25 +309,13 @@ void *GWKThreadsCreate(char **papszWarpOptions,
309309
GDALTransformerFunc /* pfnTransformer */,
310310
void *pTransformerArg)
311311
{
312-
const char *pszWarpThreads =
313-
CSLFetchNameValue(papszWarpOptions, "NUM_THREADS");
314-
if (pszWarpThreads == nullptr)
315-
pszWarpThreads = CPLGetConfigOption("GDAL_NUM_THREADS", "1");
316-
317-
int nThreads = 0;
318-
if (EQUAL(pszWarpThreads, "ALL_CPUS"))
319-
nThreads = CPLGetNumCPUs();
320-
else
321-
nThreads = atoi(pszWarpThreads);
322-
if (nThreads <= 1)
323-
nThreads = 0;
324-
if (nThreads > 128)
325-
nThreads = 128;
326-
312+
const int nThreads = GDALGetNumThreads(papszWarpOptions, "NUM_THREADS",
313+
GDAL_DEFAULT_MAX_THREAD_COUNT,
314+
/* bDefaultAllCPUs = */ false);
327315
GWKThreadData *psThreadData = new GWKThreadData();
328316
auto poThreadPool =
329-
nThreads > 0 ? GDALGetGlobalThreadPool(nThreads) : nullptr;
330-
if (nThreads && poThreadPool)
317+
nThreads > 1 ? GDALGetGlobalThreadPool(nThreads) : nullptr;
318+
if (poThreadPool)
331319
{
332320
psThreadData->nMaxThreads = nThreads;
333321
psThreadData->threadJobs.reset(new std::vector<GWKJobStruct>(

apps/ogr2ogr_lib.cpp

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include "gdal_alg.h"
4949
#include "gdal_alg_priv.h"
5050
#include "gdal_priv.h"
51+
#include "gdal_thread_pool.h"
5152
#include "ogr_api.h"
5253
#include "ogr_core.h"
5354
#include "ogr_feature.h"
@@ -6240,26 +6241,13 @@ bool LayerTranslator::TranslateArrow(
62406241
std::vector<GByte> abyModifiedWKB;
62416242
const int nNumReprojectionThreads = []()
62426243
{
6243-
const int nNumCPUs = CPLGetNumCPUs();
6244-
if (nNumCPUs <= 1)
6245-
{
6246-
return 1;
6247-
}
6248-
else
6249-
{
6250-
const char *pszNumThreads =
6251-
CPLGetConfigOption("GDAL_NUM_THREADS", nullptr);
6252-
if (pszNumThreads)
6253-
{
6254-
if (EQUAL(pszNumThreads, "ALL_CPUS"))
6255-
return CPLGetNumCPUs();
6256-
return std::min(atoi(pszNumThreads), 1024);
6257-
}
6258-
else
6259-
{
6260-
return std::max(2, nNumCPUs / 2);
6261-
}
6262-
}
6244+
const char *pszNumThreads = nullptr;
6245+
int nVal =
6246+
GDALGetNumThreads(GDAL_DEFAULT_MAX_THREAD_COUNT,
6247+
/* bDefaultToAllCPUs = */ false, &pszNumThreads);
6248+
if (!pszNumThreads)
6249+
nVal = std::max(1, CPLGetNumCPUs() / 2);
6250+
return nVal;
62636251
}();
62646252

62656253
// Somewhat arbitrary threshold (config option only/mostly for autotest purposes)

autotest/cpp/test_gdal_algorithm.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4862,6 +4862,13 @@ TEST_F(test_gdal_algorithm, AddNumThreadsArg)
48624862
EXPECT_EQ(alg.m_numThreads, 1);
48634863
}
48644864

4865+
{
4866+
CPLConfigOptionSetter oSetter("GDAL_NUM_THREADS", "ALL_CPUS", false);
4867+
MyAlgorithm alg;
4868+
EXPECT_TRUE(alg.ParseCommandLineArguments({}));
4869+
EXPECT_EQ(alg.m_numThreads, CPLGetNumCPUs());
4870+
}
4871+
48654872
{
48664873
MyAlgorithm alg;
48674874
EXPECT_TRUE(alg.ParseCommandLineArguments({"--num-threads=1"}));

frmts/avif/avifdataset.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "avifdrivercore.h"
1919
#include "gdalexif.h"
2020
#include "memdataset.h"
21+
#include "gdal_thread_pool.h"
2122

2223
#include <avif/avif.h>
2324

@@ -929,13 +930,9 @@ GDALAVIFDataset::CreateCopy(const char *pszFilename, GDALDataset *poSrcDS,
929930
avifCodecChoiceFromName(CPLString(pszCodec).tolower().c_str());
930931
}
931932

932-
const char *pszThreads = CSLFetchNameValueDef(
933-
papszOptions, "NUM_THREADS",
934-
CPLGetConfigOption("GDAL_NUM_THREADS", "ALL_CPUS"));
935-
if (pszThreads && !EQUAL(pszThreads, "ALL_CPUS"))
936-
encoder->maxThreads = atoi(pszThreads);
937-
else
938-
encoder->maxThreads = CPLGetNumCPUs();
933+
encoder->maxThreads = GDALGetNumThreads(papszOptions, "NUM_THREADS",
934+
GDAL_DEFAULT_MAX_THREAD_COUNT,
935+
/* bDefaultToAllCPUs = */ true);
939936

940937
#if AVIF_VERSION_MAJOR >= 1
941938
encoder->quality = nQuality;

frmts/basisu_ktx2/common.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "gdal_frmts.h"
1515
#include "common.h"
1616
#include "include_basisu_sdk.h"
17+
#include "gdal_thread_pool.h"
1718

1819
#include <algorithm>
1920
#include <mutex>
@@ -264,11 +265,8 @@ bool GDAL_KTX2_BASISU_CreateCopy(const char *pszFilename, GDALDataset *poSrcDS,
264265
params.m_mip_srgb = params.m_perceptual;
265266
}
266267

267-
const int nNumThreads = std::max(
268-
1, atoi(CSLFetchNameValueDef(
269-
papszOptions, "NUM_THREADS",
270-
CPLGetConfigOption("GDAL_NUM_THREADS",
271-
CPLSPrintf("%d", CPLGetNumCPUs())))));
268+
const int nNumThreads = GDALGetNumThreads(GDAL_DEFAULT_MAX_THREAD_COUNT,
269+
/* bDefaultAllCPUs = */ true);
272270
CPLDebug("KTX2", "Using %d threads", nNumThreads);
273271
if (params.m_uastc)
274272
{

frmts/gti/gdaltileindexdataset.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "cpl_mem_cache.h"
3030
#include "cpl_minixml.h"
3131
#include "cpl_quad_tree.h"
32+
#include "cpl_worker_thread_pool.h"
3233
#include "vrtdataset.h"
3334
#include "vrt_priv.h"
3435
#include "ogrsf_frmts.h"
@@ -3753,15 +3754,8 @@ int GDALTileIndexDataset::GetNumThreads() const
37533754
CSLFetchNameValueDef(GetOpenOptions(), "NUM_THREADS", nullptr);
37543755
if (!pszNumThreads)
37553756
pszNumThreads = CPLGetConfigOption("GTI_NUM_THREADS", nullptr);
3756-
if (!pszNumThreads)
3757-
pszNumThreads = CPLGetConfigOption("GDAL_NUM_THREADS", "ALL_CPUS");
3758-
if (EQUAL(pszNumThreads, "0") || EQUAL(pszNumThreads, "1"))
3759-
return atoi(pszNumThreads);
3760-
const int nMaxPoolSize = GDALGetMaxDatasetPoolSize();
3761-
const int nLimit = std::min(CPLGetNumCPUs(), nMaxPoolSize);
3762-
if (EQUAL(pszNumThreads, "ALL_CPUS"))
3763-
return nLimit;
3764-
return std::min(atoi(pszNumThreads), nLimit);
3757+
return GDALGetNumThreads(pszNumThreads, GDALGetMaxDatasetPoolSize(),
3758+
/* bDefaultAllCPUs = */ true);
37653759
}
37663760

37673761
/************************************************************************/

frmts/gtiff/cogdriver.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -677,11 +677,12 @@ static std::unique_ptr<GDALDataset> CreateReprojectedDS(
677677
std::unique_ptr<CPLConfigOptionSetter> poWarpThreadSetter;
678678
if (pszNumThreads)
679679
{
680-
poWarpThreadSetter.reset(new CPLConfigOptionSetter(
681-
"GDAL_NUM_THREADS", pszNumThreads, false));
680+
poWarpThreadSetter = std::make_unique<CPLConfigOptionSetter>(
681+
"GDAL_NUM_THREADS", pszNumThreads, false);
682682
}
683683

684684
auto hRet = GDALWarp(osTmpFile, nullptr, 1, &hSrcDS, psOptions, nullptr);
685+
CPL_IGNORE_RET_VAL(poWarpThreadSetter);
685686
GDALWarpAppOptionsFree(psOptions);
686687
CPLDebug("COG", "Reprojecting source dataset: end");
687688

0 commit comments

Comments
 (0)