Snippets

Lilian Besson (Naereen) Count the number of CPU on a Linux machine with a tiny MATLAB/Octave script

Updated by Lilian Besson (Naereen)

File countcpu.m Modified

  • Ignore whitespace
  • Hide word diff
 % Count the number of CPU on a Linux machine with a MATLAB/Octave tiny script
-% NOTE: the variable nproc should do the same, please try first to see if it is available on your MATLAB or Octave
+% WARNING: the variable nproc should do the same, please try first to see if it is available on your MATLAB or Octave
 %
 function nb = countcpu()
     % nb = countcpu()
Updated by Lilian Besson (Naereen)

File countcpu.m Modified

  • Ignore whitespace
  • Hide word diff
 % Count the number of CPU on a Linux machine with a MATLAB/Octave tiny script
+% NOTE: the variable nproc should do the same, please try first to see if it is available on your MATLAB or Octave
+%
 function nb = countcpu()
     % nb = countcpu()
     %     Return the number of CPU core on your Linux machine (Linux only!).
Updated by Lilian Besson (Naereen)

File countcpu.m Added

  • Ignore whitespace
  • Hide word diff
+% Count the number of CPU on a Linux machine with a MATLAB/Octave tiny script
+function nb = countcpu()
+    % nb = countcpu()
+    %     Return the number of CPU core on your Linux machine (Linux only!).
+    %
+    %     - How: Read and parse the special file /proc/cpuinfo to count the number of line tagged as 'processor'
+    %     - Author: Lilian Besson (https://bitbucket.org/lbesson)
+    %     - Web: https://bitbucket.org/snippets/lbesson/G6q7E/
+    %     - License: MIT Licensed (http://lbesson.mit-license.org/)
+    %     - Date: 02-05-2016
+
+    try
+        nb = 0;  % Start with no CPU core
+        myfile = fopen('/proc/cpuinfo', 'r');  % Open the special file /proc/cpuinfo
+        ansRead = fgets(myfile);  % Start to read it (line by line)
+        while ischar(ansRead)  % ansRead is -1 if we read all the file
+            % If we read more than 9 characters
+            % And if the beginning of the line is 'processor'
+            if (length(ansRead) >= 9) && strcmp(ansRead(1:9), 'processor')
+                % Then we detected one more CPU core
+                nb = nb + 1;
+            end;
+            ansRead = fgets(myfile);  % Still reading it (line by line)
+        end;
+        % Done, we close the file and return [nb].
+        fclose(myfile);
+    catch
+        % disp('countcpu(): error when opening /proc/cpuinfo or reading or parsing it. Returning 1 as nb.');
+        printf('countcpu(): error (%s) when opening /proc/cpuinfo or reading or parsing it. Returning 1 as nb.\n', lasterr);
+        % Thanks to https://stackoverflow.com/questions/12115046/in-gnu-octave-how-to-catch-an-exception#comment16199450_12115169
+        nb = 1;
+    end;
+end;

File countcpu.octave Deleted

  • Ignore whitespace
  • Hide word diff
-function nb = countcpu()
-    % nb = countcpu()
-    %     Return the number of CPU core on your Linux machine (Linux only!).
-    %
-    %     - How: Read and parse the special file /proc/cpuinfo to count the number of line tagged as 'processor'
-    %     - Author: Lilian Besson (https://bitbucket.org/lbesson)
-    %     - Web: https://bitbucket.org/snippets/lbesson/G6q7E/
-    %     - License: MIT Licensed (http://lbesson.mit-license.org/)
-    %     - Date: 02-05-2016
-
-    try
-        nb = 0;  % Start with no CPU core
-        myfile = fopen('/proc/cpuinfo', 'r');  % Open the special file /proc/cpuinfo
-        ansRead = fgets(myfile);  % Start to read it (line by line)
-        while ischar(ansRead)  % ansRead is -1 if we read all the file
-            % If we read more than 9 characters
-            % And if the beginning of the line is 'processor'
-            if (length(ansRead) >= 9) && strcmp(ansRead(1:9), 'processor')
-                % Then we detected one more CPU core
-                nb = nb + 1;
-            end;
-            ansRead = fgets(myfile);  % Still reading it (line by line)
-        end;
-        % Done, we close the file and return [nb].
-        fclose(myfile);
-    catch
-        % disp('countcpu(): error when opening /proc/cpuinfo or reading or parsing it. Returning 1 as nb.');
-        printf('countcpu(): error (%s) when opening /proc/cpuinfo or reading or parsing it. Returning 1 as nb.\n', lasterr);
-        % Thanks to https://stackoverflow.com/questions/12115046/in-gnu-octave-how-to-catch-an-exception#comment16199450_12115169
-        nb = 1;
-    end;
-end;
Updated by Lilian Besson (Naereen)

File countcpu.m Deleted

  • Ignore whitespace
  • Hide word diff
-function nb = countcpu()
-    % nb = countcpu()
-    %     Return the number of CPU core on your Linux machine (Linux only!).
-    %
-    %     - How: Read and parse the special file /proc/cpuinfo to count the number of line tagged as 'processor'
-    %     - Author: Lilian Besson (https://bitbucket.org/lbesson)
-    %     - Web: https://bitbucket.org/snippets/lbesson/G6q7E/
-    %     - License: MIT Licensed (http://lbesson.mit-license.org/)
-    %     - Date: 02-05-2016
-
-    try
-        nb = 0;  % Start with no CPU core
-        myfile = fopen('/proc/cpuinfo', 'r');  % Open the special file /proc/cpuinfo
-        ansRead = fgets(myfile);  % Start to read it (line by line)
-        while ischar(ansRead)  % ansRead is -1 if we read all the file
-            % If we read more than 9 characters
-            % And if the beginning of the line is 'processor'
-            if (length(ansRead) >= 9) && strcmp(ansRead(1:9), 'processor')
-                % Then we detected one more CPU core
-                nb = nb + 1;
-            end;
-            ansRead = fgets(myfile);  % Still reading it (line by line)
-        end;
-        % Done, we close the file and return [nb].
-        fclose(myfile);
-    catch
-        % disp('countcpu(): error when opening /proc/cpuinfo or reading or parsing it. Returning 1 as nb.');
-        printf('countcpu(): error (%s) when opening /proc/cpuinfo or reading or parsing it. Returning 1 as nb.\n', lasterr);
-        % Thanks to https://stackoverflow.com/questions/12115046/in-gnu-octave-how-to-catch-an-exception#comment16199450_12115169
-        nb = 1;
-    end;
-end;

File countcpu.octave Added

  • Ignore whitespace
  • Hide word diff
+function nb = countcpu()
+    % nb = countcpu()
+    %     Return the number of CPU core on your Linux machine (Linux only!).
+    %
+    %     - How: Read and parse the special file /proc/cpuinfo to count the number of line tagged as 'processor'
+    %     - Author: Lilian Besson (https://bitbucket.org/lbesson)
+    %     - Web: https://bitbucket.org/snippets/lbesson/G6q7E/
+    %     - License: MIT Licensed (http://lbesson.mit-license.org/)
+    %     - Date: 02-05-2016
+
+    try
+        nb = 0;  % Start with no CPU core
+        myfile = fopen('/proc/cpuinfo', 'r');  % Open the special file /proc/cpuinfo
+        ansRead = fgets(myfile);  % Start to read it (line by line)
+        while ischar(ansRead)  % ansRead is -1 if we read all the file
+            % If we read more than 9 characters
+            % And if the beginning of the line is 'processor'
+            if (length(ansRead) >= 9) && strcmp(ansRead(1:9), 'processor')
+                % Then we detected one more CPU core
+                nb = nb + 1;
+            end;
+            ansRead = fgets(myfile);  % Still reading it (line by line)
+        end;
+        % Done, we close the file and return [nb].
+        fclose(myfile);
+    catch
+        % disp('countcpu(): error when opening /proc/cpuinfo or reading or parsing it. Returning 1 as nb.');
+        printf('countcpu(): error (%s) when opening /proc/cpuinfo or reading or parsing it. Returning 1 as nb.\n', lasterr);
+        % Thanks to https://stackoverflow.com/questions/12115046/in-gnu-octave-how-to-catch-an-exception#comment16199450_12115169
+        nb = 1;
+    end;
+end;
Updated by Former user

File countcpu.m Modified

  • Ignore whitespace
  • Hide word diff
-function [nb] = countcpu()
-    % [nb] = countcpu()
-    %     Read and parse the special file /proc/cpuinfo to count the number of CPU on your machine.
-    %     
-    %     Author: Lilian Besson (https://bitbucket.org/lbesson)
-    %     Web: https://bitbucket.org/snippets/lbesson/G6q7E/
-    %     License: MIT Licensed (http://lbesson.mit-license.org/)
-    %     Date: 02-05-2016
+function nb = countcpu()
+    % nb = countcpu()
+    %     Return the number of CPU core on your Linux machine (Linux only!).
+    %
+    %     - How: Read and parse the special file /proc/cpuinfo to count the number of line tagged as 'processor'
+    %     - Author: Lilian Besson (https://bitbucket.org/lbesson)
+    %     - Web: https://bitbucket.org/snippets/lbesson/G6q7E/
+    %     - License: MIT Licensed (http://lbesson.mit-license.org/)
+    %     - Date: 02-05-2016
 
-    nb = 0;
-    myfile = fopen('/proc/cpuinfo', 'r');
-    ansRead = fgets(myfile);
-    while ischar(ansRead)
-        if (length(ansRead) >= 9) && strcmp(ansRead(1:9), 'processor')
-            nb = nb + 1;
-        end
-        ansRead = fgets(myfile);
-    end
-end
+    try
+        nb = 0;  % Start with no CPU core
+        myfile = fopen('/proc/cpuinfo', 'r');  % Open the special file /proc/cpuinfo
+        ansRead = fgets(myfile);  % Start to read it (line by line)
+        while ischar(ansRead)  % ansRead is -1 if we read all the file
+            % If we read more than 9 characters
+            % And if the beginning of the line is 'processor'
+            if (length(ansRead) >= 9) && strcmp(ansRead(1:9), 'processor')
+                % Then we detected one more CPU core
+                nb = nb + 1;
+            end;
+            ansRead = fgets(myfile);  % Still reading it (line by line)
+        end;
+        % Done, we close the file and return [nb].
+        fclose(myfile);
+    catch
+        % disp('countcpu(): error when opening /proc/cpuinfo or reading or parsing it. Returning 1 as nb.');
+        printf('countcpu(): error (%s) when opening /proc/cpuinfo or reading or parsing it. Returning 1 as nb.\n', lasterr);
+        % Thanks to https://stackoverflow.com/questions/12115046/in-gnu-octave-how-to-catch-an-exception#comment16199450_12115169
+        nb = 1;
+    end;
+end;
  1. 1
  2. 2
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.