From 92bc2b423bc92c7681885a23b8feb33fdd727b48 Mon Sep 17 00:00:00 2001 From: Vincent Danjean <Vincent.Danjean@ens-lyon.org> Date: Fri, 2 Nov 2012 15:27:25 +0100 Subject: [PATCH] If ALTREE_PARALLEL is set to -1, all available CPU are used This function need to be tested on another plateform than linux, mainly MacOS X. --- CUtils/c_sources/resampling.c | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/CUtils/c_sources/resampling.c b/CUtils/c_sources/resampling.c index 2525d9d..549a51d 100644 --- a/CUtils/c_sources/resampling.c +++ b/CUtils/c_sources/resampling.c @@ -162,6 +162,37 @@ static void *resampling_worker(void* param) { return NULL; } +/* From http://stackoverflow.com/questions/150355/programmatically-find-the-number-of-cores-on-a-machine */ +static int nbproc() +{ + int numCPU=0; +#ifdef __linux + numCPU = sysconf( _SC_NPROCESSORS_ONLN ); +#elif defined(__bsd) + 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; + } + } +#else +# warning no support on this plate-form. Patch welcome. +#endif + return numCPU; +} + int resampling_chi2(const struct tree *tree, const struct cc *lcc, int prolonge, int nb_permutations, datatype_t *results, int parallel) { @@ -180,6 +211,12 @@ int resampling_chi2(const struct tree *tree, const struct cc *lcc, int prolonge, if (envvar) { parallel=atoi(envvar); } + if (parallel == -1) { + parallel=nbproc(); + } + if (parallel < 0) { + parallel=0; + } debug("parallel=%i", parallel); struct memory *mem=mem_alloc(tree); -- GitLab