\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#377: Portably Determining the Number of Processors in the System}
\date{September 13, 2010}
\author{Andreas Kupries}
\urlstyle{sf}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0pt}
\def\tipversion$#1${\texttt{\$#1\$}}
\def\tiplangle#1{\ensuremath{<}}
\def\tiprangle#1{\ensuremath{>}}
\def\tipbar#1{\ensuremath{|}}
\def\tipmail#1#2{\(\langle\){\small\expandafter\url{#1@#2}}\(\rangle\)}
\ifx\pdfoutput\undefined
\newcommand{\tipimage}[2]{\typeout{Make sure you download #1.eps}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.eps}}{\includegraphics[width=0.8\textwidth]{#1.eps}}}
\newcommand{\tipxref}[1]{}
\newcommand{\tipxrefend}{}
\else
\newcommand{\tipimage}[2]{\typeout{Make sure you create #1.pdf}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.pdf}}{\includegraphics[width=0.8\textwidth]{#1.pdf}}}
\pdfcatalog{/PageMode /UseOutlines}
\newcommand{\tipxref}[1]{\pdfannotlink attr {/C [0.5 0.5 1.0] /Border [0 0 1]} goto name {#1}}
\newcommand{\tipxrefend}{\pdfendlink}
\fi
\newenvironment{tipabstract}{\begin{abstract}}{\end{abstract}}
\begin{document}\maketitle
\begin{center}\begin{tabularx}{\linewidth}{|r@{: }X|}\hline
\textbf{TIP \#377}&\textbf{Portably Determining the Number of Processors in the System}\\\hline
Author&

Andreas Kupries \tipmail{andreask}{activestate.com}
\\
Created&Monday, \(\text{13}^{\text{th}}\) September 2010\\
Type&Project\\
Tcl Version&\(8.6\)\\
State&Withdrawn\\
Vote&Pending\\
Version&\tipversion$Revision: 1.2 $\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

A C API and Tcl command are proposed which return the number of CPUs (a.k.a. Processors a.k.a. Cores) in the system, portably wrapping the OS specific APIs for doing so.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Background and Motivation}

With the relentless advance of multi-core chips it becomes sensible, IMVHO, to have a portable method of determining the number of CPUs (also called Processors or Cores) in the system.

This information is mostly important for the initialization of thread pools, in order to have a sensible number of worker threads which provide the maximum speedup without overloading the system. The general use-case the previous is an example of is the dynamic reconfiguration of an application or library to automatically adapt it to the resources available in the environment it finds itself in. That is, this is useful for multi-process applications as well.

Semi-concrete examples: The CRIMP package is currently single-threaded. For the best parallelization by threading, it should be possible to query the number of processors the application can use. A web server like Wub can make use of this functionality as well. They should not have to implement this on their own, duplicating the functionality, or, worse, each having slightly different implementations.

\section{C Level API}

\begin{quote}
int \textbf{Tcl\_GetNumberOfProcessors}(int \textit{flags})
\end{quote}

Returns the number of processors for the current system, as provided by the OS. If the system is unable to provide this information the returned value is '1', as at least one processors is necessary to run anything at all.

The \textit{flags} argument is not used. It is provided as a means of extensibility. Something which can be used in the future to distinguish between physical processors, hyperthreaded virtual processors, cores on a chip, number of processor chips, and the like. Right now such advanced queries are out of the scope of the TIP.

The main point of having the 'flags' argument now is that future extensions using it will not have to change the signature of the function.

\section{Script Level API}

\begin{quote}
\textbf{info processors}
\end{quote}

Returns the number of processors for the current system, as provided by \textbf{Tcl\_GetNumberProcessors}(0).

\section{Reference Implementation}

A reference implementation is provided at SourceForge [\url{https://sourceforge.net/support/tracker.php?aid=3065485}].

\section{Discussion}

The stackoverflow messaging board has a question on the various OS-specific APIs. See [\url{http://stackoverflow.com/q/150355/301832}] for details.

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\end{document}
