# Drawing GPU Blocks and Threads with TikZ

Although I only know the very basics of TikZ, I like what it can do and how it does it, so I try to use it whenever I can. Some time ago I used it in a paper to explain how the processing was divided between blocks and threads of a GPU and I was happy with the result, so I would like to share it here.

I wanted to express the idea that one GPU block would be responsible to process a line of a matrix and that the threads in the block would operate over the elements of this line. I tried to represent a block as a square, but I think it loses a bit of the idea that the blocks can have more than two dimensions. So the decision was to use a cube to represent a GPU block.

The cube I ended up using was found in an answer from StackExchange and I take no credit for it. The author made it easy to create a cube by using a macro (tikzcuboid) that I modified a bit (the complete source is available at GitHub):

\newcommand{\blockFig}[1]{
\begin{tikzpicture}
\tikzcuboid{%
scale=0.08,%
densityx=1,%
densityy=1,%
densityz=1,%
dimx=4,%
dimy=4,%
dimy=4,%
linefront=#1!75!black,%
linetop=#1!50!black,%
lineright=#1!25!black,%
fillfront=#1!25!white,%
filltop=#1!50!white,%
fillright=#1!75!white,
emphedge=Y,%
emphstyle=ultra thin,
}
\end{tikzpicture}
}


Although there are a few more options to change (scale, dimensions, etc), with this macro you call it by simply passing a color:

\blockFig{blue}


The result should be like this picture:

Recently I found a simpler way to draw a cube from another answer from StackExchange:

\newcommand{\simpleBlock}[1]{
\resizebox{#1pt}{#1pt}{
\begin{tikzpicture}
(0,0) rectangle +(3,3);
\draw[yslant=-0.5] (0,0) grid (3,3);
(3,-3) rectangle +(3,3);
\draw[yslant=0.5] (3,-3) grid (6,0);
top color=black!80] (6,3) rectangle +(-3,-3);
\draw[yslant=0.5,xslant=-1] (3,0) grid (6,3);
\end{tikzpicture}
}
}


To insert this second cube, you can call this macro by passing the size (in pt). This may not be the best way to resize it, but it works for this example:

\simpleBlock{15}


The result is a cube a bit different from the other (it’s a bit rotated for example):

As for drawing a thread, I used my elementary TikZ skills to bend a line a few times:

\newcommand{\threadFig}[1]{
\begin{tikzpicture}
\vspace{5pt}
\draw[very thick,color=#1] plot [smooth,tension=1.5] coordinates{(0.3,0.5) (0.25,0.37) (0.3,0.25) (0.25,0.12)};
\end{tikzpicture}
}


To call it you also just pass the color:

\threadFig{blue}


The result looks like a thread (at least in my eyes):

By drawing these figures on the side of a matrix I believe it helped explain how the processing is done in a better way:

I think there are other ways to explain this idea in a paper or other ways to draw it, and the comments are open for suggestions.

As mentioned, the code used in the examples is available at GitHub.