\begin{tikzpicture}
	\input{src/tikz/orchestration}

	\tikzset{orch/task/.append style={minimum width=10mm}}

	\pgfmathtruncatemacro{\nlasttask}{\N-1}
	\pgfmathtruncatemacro{\nlastthread}{\T-1}
	\pgfmathtruncatemacro{\R}{mod(\N, \T)}

	\begin{scope}[overlay]
		\node(origin){};
		\node at (origin) (t-\T) {};
	\end{scope}

	\begin{scope}[node distance=3mm and 3mm]
		\pgfmathtruncatemacro{\multiplier}{\N/\T}
		\pgfmathtruncatemacro{\offset}{mod(\N, \T)}

		\foreach \task in {0,...,\nlasttask} {
			\pgfmathtruncatemacro{\r}{mod(\task, \T)}
			\ifthenelse{\r = 0}{
				\pgfmathtruncatemacro{\uppertask}{\T*(\task/\T-1)}
				\def\reltype{below}
				\def\relnode{t\uppertask}
			}{
				\pgfmathtruncatemacro{\prevtask}{\task-1}
				\def\reltype{right}
				\def\relnode{t\prevtask}
			}

			\pgfmathtruncatemacro{\id}{(\task/\T)+\r*\multiplier+min(\r, \offset)}
			\node[orch/task=black,\reltype=of \relnode] (t\task) {$m_\id$};
		}

		\ifthenelse{\R = 0}{}{
			\pgfmathtruncatemacro{\ntaskfill}{mod(\T-\R, \T)-1}
			\foreach \taskfill in {0,...,\ntaskfill} {
				\pgfmathtruncatemacro{\task}{\nlasttask+\taskfill+1}
				\pgfmathtruncatemacro{\prevtask}{\task-1}
				\node[orch/task=white,draw=none,fill=none,right=of t\prevtask] (t\task) {};
				\draw[-] (t\task.north) -- (t\task.south);
			}
		}

		\foreach \thread in {0,...,\nlastthread} {
			\node[above=of t\thread] (label\thread) {\footnotesize\en{thread}~\thread};
			\draw[-] (label\thread) -- (t\thread);

			\pgfmathtruncatemacro{\n}{floor((\N-1)/\T)-1}
			\ifthenelse{\n = -1}{}{% only if N>T
				\foreach \task in {0,...,\n} {
					\pgfmathtruncatemacro{\curtask}{\thread+\task*\T}
					\pgfmathtruncatemacro{\nexttask}{\curtask+\T}
					\draw[-] (t\curtask) -- (t\nexttask);
				}
			}

			\pgfmathtruncatemacro{\lasttask}{\thread+(\n+1)*\T}
			\node[below=of t\lasttask] (endthread\thread) {};
			\draw[-] (t\lasttask) -- (endthread\thread);
		}
	\end{scope}
\end{tikzpicture}
