Skip to content

Commit

Permalink
feat: Get physical processor number instead of logical
Browse files Browse the repository at this point in the history
Avoid extra calculation. They should be handled in
get_avail_physic_nprocs().
  • Loading branch information
marktwtn committed Sep 27, 2019
1 parent 44a016c commit 922e3fd
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 40 deletions.
84 changes: 47 additions & 37 deletions src/cpu-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion src/pow_avx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/pow_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/pow_sse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 922e3fd

Please sign in to comment.