function N = num_cores() %NUM_CORES attempt to find the number of cores/CPUs on the current machine % Iain Murray, March 2011 N = []; try % Undocumented Matlab routine. % Could disappear and not currently (2011-03-06) in Octave: N = feature('numCores'); catch try if exist('/proc/cpuinfo', 'file') % Should work on Linux in Matlab and Octave: fid = fopen('/proc/cpuinfo'); N = length(strfind(char(fread(fid)'), ['processor' 9])); fclose(fid); elseif ispc % Windows is untested N = str2num(getenv('NUMBER_OF_PROCESSORS')); elseif ismac % Mac is untested [status, output] = system('sysctl hw.ncpu | awk ''{print $2}'''); N = str2num(output); end end end if isempty(N) warning('Was not able to find number of cores. Falling back to 1.'); N = 1; end