diff --git a/src/cpu-utils.h b/src/cpu-utils.h index 42fd084..2071af5 100644 --- a/src/cpu-utils.h +++ b/src/cpu-utils.h @@ -33,43 +33,6 @@ static unsigned int get_nprocs_conf() } #endif -/** - * @brief Get the available number of logical processor. - * - * Detect the logical processor number with `get_nproc_conf()` - * or get the number with the environment variable **DCURL_NUM_CPU**. - * @return The available number of logical processor. - * If the environment variable **DCURL_NUM_CPU** is not set, return the - * **maximum logical processor number - 1**. - */ -static inline int get_avail_logic_nprocs() -{ - size_t nproc = get_nprocs_conf() - 1; - - do { - char *env_ncpu = getenv("DCURL_NUM_CPU"); - if (!env_ncpu) { - break; - } - - char *end; - signed int num = strtol(env_ncpu, &end, 10); - if (end == env_ncpu) { - /* if no characters were converted these pointers are equal */ - break; - } - if (errno == ERANGE || num > INT_MAX || num < 0) { - /* because strtol produces a long, check for overflow */ - break; - } - nproc = num; - } while (0); - - if (!nproc) - nproc = 1; - return nproc; -} - /** * @brief Get the thread number per physical processor. * @@ -128,3 +91,50 @@ static inline int get_nthds_per_physic_proc() return -1; return nthread; } + +/** + * @brief Get the available number of physical processor. + * + * Detect the physical processor number with `get_nproc_conf()` + * or get the number with the environment variable **DCURL_NUM_CPU**. + * @return The available number of physical processor. + * @retval DCURL_NUM_CPU Return it if the environment variable + * **DCURL_NUM_CPU** is set. + * @retval Maximum_physical_processor_number-1 Return it if the environment + * variable **DCURL_NUM_CPU** is not set. The minimum value would be 1. + * @retval -1 Unexpected error. + */ +static inline int get_avail_physic_nprocs() +{ + int nthd; + size_t nproc; + + nthd = get_nthds_per_physic_proc(); + if (nthd <= 0) + return -1; + else + nproc = (get_nprocs_conf() / nthd) - 1; + + do { + char *env_ncpu = getenv("DCURL_NUM_CPU"); + if (!env_ncpu) { + break; + } + + char *end; + signed int num = strtol(env_ncpu, &end, 10); + if (end == env_ncpu) { + /* if no characters were converted these pointers are equal */ + break; + } + if (errno == ERANGE || num > INT_MAX || num < 0) { + /* because strtol produces a long, check for overflow */ + break; + } + nproc = num; + } while (0); + + if (!nproc) + nproc = 1; + return nproc; +} diff --git a/src/pow_avx.c b/src/pow_avx.c index a1c8332..6ad8d9c 100644 --- a/src/pow_avx.c +++ b/src/pow_avx.c @@ -574,7 +574,7 @@ static bool PowAVX(void *pow_ctx) static bool PoWAVX_Context_Initialize(ImplContext *impl_ctx) { impl_ctx->num_max_thread = get_nthds_per_physic_proc(); - int nproc = get_avail_logic_nprocs() / impl_ctx->num_max_thread; + int nproc = get_avail_physic_nprocs(); if (impl_ctx->num_max_thread <= 0 || nproc <= 0) return false; diff --git a/src/pow_c.c b/src/pow_c.c index f0d2230..392f3ac 100644 --- a/src/pow_c.c +++ b/src/pow_c.c @@ -338,7 +338,7 @@ bool PowC(void *pow_ctx) static bool PoWC_Context_Initialize(ImplContext *impl_ctx) { impl_ctx->num_max_thread = get_nthds_per_physic_proc(); - int nproc = get_avail_logic_nprocs() / impl_ctx->num_max_thread; + int nproc = get_avail_physic_nprocs(); if (impl_ctx->num_max_thread <= 0 || nproc <= 0) return false; diff --git a/src/pow_sse.c b/src/pow_sse.c index 6556a44..da5f4f5 100644 --- a/src/pow_sse.c +++ b/src/pow_sse.c @@ -356,7 +356,7 @@ static bool PowSSE(void *pow_ctx) static bool PoWSSE_Context_Initialize(ImplContext *impl_ctx) { impl_ctx->num_max_thread = get_nthds_per_physic_proc(); - int nproc = get_avail_logic_nprocs() / impl_ctx->num_max_thread; + int nproc = get_avail_physic_nprocs(); if (impl_ctx->num_max_thread <= 0 || nproc <= 0) return false;