Skip to content
Snippets Groups Projects
Commit 1e081f35 authored by dg's avatar dg
Browse files

Detect number of CPUs and create particles threads accordingly (1+nb_cpus/2)

git-svn-id: http://svn.net-core.org/repos/t-engine4@2594 51575b47-30f0-44d4-a5cc-537603b46e54
parent 3a68789a
No related branches found
No related tags found
No related merge requests found
......@@ -23,6 +23,7 @@
#if defined(SELFEXE_LINUX)
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
const char *get_self_executable(int argc, char **argv)
{
......@@ -32,6 +33,11 @@ const char *get_self_executable(int argc, char **argv)
return res;
}
int get_number_cpus()
{
return sysconf(_SC_NPROCESSORS_ONLN);
}
#elif defined(SELFEXE_WINDOWS)
#include <stdlib.h>
#include <windows.h>
......@@ -43,9 +49,18 @@ const char *get_self_executable(int argc, char **argv)
return szEXEPath;
}
int get_number_cpus()
{
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
return sysinfo.dwNumberOfProcessors;
}
#elif defined(SELFEXE_MACOSX)
#include <mach-o/dyld.h>
#include <string.h>
#include <unistd.h>
const char *get_self_executable(int argc, char **argv)
{
......@@ -62,10 +77,40 @@ const char *get_self_executable(int argc, char **argv)
return buf;
}
int get_number_cpus()
{
int mib[4];
size_t len = sizeof(numCPU);
/* set the mib for hw.ncpu */
mib[0] = CTL_HW;
mib[1] = HW_AVAILCPU; // alternatively, try HW_NCPU;
/* get the number of CPUs from the system */
sysctl(mib, 2, &numCPU, &len, NULL, 0);
if( numCPU < 1 )
{
mib[1] = HW_NCPU;
sysctl( mib, 2, &numCPU, &len, NULL, 0 );
if( numCPU < 1 )
{
numCPU = 1;
}
}
return numCPU;
}
#else
const char *get_self_executable(int argc, char **argv)
{
return NULL;
}
int get_number_cpus()
{
return 1;
}
#endif
......@@ -22,5 +22,6 @@
#define _GETSELF_H_
extern const char *get_self_executable(int argc, char **argv);
extern int get_number_cpus();
#endif
......@@ -47,6 +47,7 @@
#define HEIGHT 600
lua_State *L = NULL;
int nb_cpus;
bool no_debug = FALSE;
int current_mousehandler = LUA_NOREF;
int current_keyhandler = LUA_NOREF;
......@@ -723,6 +724,10 @@ void boot_lua(int state, bool rebooting, int argc, char *argv[])
int main(int argc, char *argv[])
{
// Get cpu cores
nb_cpus = get_number_cpus();
printf("[CPU] Detected %d CPUs\n", nb_cpus);
// RNG init
init_gen_rand(time(NULL));
......@@ -743,6 +748,7 @@ int main(int argc, char *argv[])
}
boot_lua(1, FALSE, argc, argv);
create_particles_thread();
// initialize engine and set up resolution and depth
Uint32 flags=SDL_INIT_VIDEO | SDL_INIT_TIMER;
......@@ -795,7 +801,6 @@ int main(int argc, char *argv[])
boot_lua(2, FALSE, argc, argv);
// start_xmpp_thread();
create_particles_thread();
pass_command_args(argc, argv);
......
......@@ -37,8 +37,9 @@
#define PARTICLE_ETERNAL 999999
#define PARTICLES_PER_ARRAY 1000
#define MAX_THREADS 7
static particle_thread threads[MAX_THREADS];
int MAX_THREADS = 1;
extern int nb_cpus;
static particle_thread *threads;
static int textures_ref = LUA_NOREF;
static int nb_threads = 0;
static int cur_thread = 0;
......@@ -805,6 +806,9 @@ void create_particles_thread()
{
int i;
MAX_THREADS = 1 + nb_cpus / 2;
threads = calloc(MAX_THREADS, sizeof(particle_thread));
cur_thread = 0;
for (i = 0; i < MAX_THREADS; i++)
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment