Skip to content

Commit

Permalink
Move cpu_thread_count to concurrency, and add some comments to our
Browse files Browse the repository at this point in the history
thread option parsing function.
  • Loading branch information
scamille committed Jan 17, 2015
1 parent fdd42fe commit d1b5986
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 42 deletions.
31 changes: 0 additions & 31 deletions engine/sc_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,37 +216,6 @@ double stopwatch_t::elapsed()
double util::wall_time() { return wall_sw.elapsed(); }
double util::cpu_time() { return cpu_sw.elapsed(); }

// cpu_thread_count =========================================================

int util::cpu_thread_count()
{
// Use std::thread to determine logical thread count
#if defined( SC_STD_THREAD ) && ! defined( SC_MINGW )
return std::thread::hardware_concurrency();
// OS X uses systemctl() to fetch the thread count for the CPU. This returns 8
// (i.e., the logical thread count) on Hyperthreading enabled machines.
#elif defined( SC_OSX )
int32_t n_threads = -1;
size_t sizeof_n_threads = sizeof( int32_t );
int ret = sysctlbyname( "machdep.cpu.thread_count",
static_cast<void*>( &n_threads ),
&( sizeof_n_threads ),
NULL,
0 );

// Error, return 0
if ( ret == -1 )
{
return 0;
}
else
{
return n_threads;
}
#endif // SC_STD_THREAD
return 0;
}

// str_compare_ci ===========================================================

bool util::str_compare_ci( const std::string& l,
Expand Down
29 changes: 19 additions & 10 deletions engine/sim/sc_sim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,24 @@ bool parse_maximize_reporting( sim_t* sim,

return true;
}

/**
* Parse threads option, and if equal or lower than 0, adjust
* the number of threads to the number of cpu cores minus the absolute value given as a thread option.
* So eg. threads=-2 on a 8 core machine will result in 6 threads.
*/
void adjust_threads( int& threads )
{
// This is to resolve https://code.google.com/p/simulationcraft/issues/detail?id=2305
int max_threads = sc_thread_t::cpu_thread_count();
if ( threads <= 0 && max_threads > 0 ) //max_threads will return 0 if it has no clue how many threads it can use.
{
threads *= -1;
if ( threads <= max_threads )
threads = max_threads - threads;
}
}

// Proxy cast ===============================================================

struct proxy_cast_check_t : public event_t
Expand Down Expand Up @@ -2859,16 +2877,7 @@ void sim_t::setup( sim_control_t* c )
print_options();
}

#if defined( SC_STD_THREAD ) || defined( SC_OSX )
// This is to resolve https://code.google.com/p/simulationcraft/issues/detail?id=2305
int max_threads = util::cpu_thread_count();
if ( threads <= 0 && max_threads > 0 ) //max_threads will return 0 if it has no clue how many threads it can use.
{
threads *= -1;
if ( threads <= max_threads )
threads = max_threads - threads;
}
#endif // SC_STD_THREAD
adjust_threads( threads );

if ( log )
{
Expand Down
1 change: 0 additions & 1 deletion engine/simulationcraft.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,6 @@ namespace util
{
double wall_time();
double cpu_time();
int cpu_thread_count();

template <typename T>
T ability_rank( int player_level, T ability_value, int ability_level, ... );
Expand Down
32 changes: 32 additions & 0 deletions engine/util/concurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,35 @@ void sc_thread_t::wait()

void sc_thread_t::sleep_seconds( double t )
{ native_t::sleep_seconds( t ); }

/**
* Get the number of concurrent threads supported by the CPU.
*/
int sc_thread_t::cpu_thread_count()
{
// Use std::thread to determine logical thread count
#if defined( SC_STD_THREAD ) && ! defined( SC_MINGW )
return std::thread::hardware_concurrency();
// OS X uses systemctl() to fetch the thread count for the CPU. This returns 8
// (i.e., the logical thread count) on Hyperthreading enabled machines.
#elif defined( SC_OSX )
int32_t n_threads = -1;
size_t sizeof_n_threads = sizeof( int32_t );
int ret = sysctlbyname( "machdep.cpu.thread_count",
static_cast<void*>( &n_threads ),
&( sizeof_n_threads ),
NULL,
0 );

// Error, return 0
if ( ret == -1 )
{
return 0;
}
else
{
return n_threads;
}
#endif // SC_STD_THREAD
return 0;
}
1 change: 1 addition & 0 deletions engine/util/concurrency.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class sc_thread_t : public noncopyable

static void sleep_seconds( double );
static void set_calling_thread_priority( priority_e );
static int cpu_thread_count();
};

class auto_lock_t
Expand Down

0 comments on commit d1b5986

Please sign in to comment.