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):

        emphstyle=ultra thin,

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


The result should be like this picture:


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

      \shade[yslant=-0.5,right color=gray!10, left color=black!50]
      (0,0) rectangle +(3,3);
      \draw[yslant=-0.5] (0,0) grid (3,3);
      \shade[yslant=0.5,right color=gray!70,left color=gray!10]
      (3,-3) rectangle +(3,3);
      \draw[yslant=0.5] (3,-3) grid (6,0);
      \shade[yslant=0.5,xslant=-1,bottom color=gray!10,
        top color=black!80] (6,3) rectangle +(-3,-3);
      \draw[yslant=0.5,xslant=-1] (3,0) grid (6,3);

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:


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:

      \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)};

To call it you also just pass the color:


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:

Screen Shot 2016-10-07 at 21.34.04.png

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.



My Emacs Configuration

Emacs is a powerful editor that can be a bit intimidating in the beginning, but it has many useful features. Normally I use it to program in C/C++, R and Python, and to write all sorts of documents (including presentation slides) in LaTeX.

I would like to share the configuration I use for Emacs, which might be useful for someone else or might get me some feedback to improve as well. This will not be a tutorial teaching how to install Emacs itself or its packages, and I also assume basic knowledge about how to use it.

I use Emacs 24 and complement it with the following packages:

  • Auto-complete: Completes things I am typing
  • ESS: Useful features when I use R
  • Flycheck: Syntax checker
  • Helm: I mainly use it to navigate between folders
  • Undo Tree: Good for controlling undo/redo actions. It’s quite powerful and honestly I just use the basic features
  • Writegood: Based on writing tips by Matt Might, this helps me avoid some bad habits when writing
  • Yasnippet: Some code snippets that can speed up coding a bit. It mainly helps meĀ  when I am writing my presentation slides using Beamer

The configuration file I am currently using is available at GitHub and it is based on the one provided by Aaron Bedra. Here I’ll go through the main parts of it.

First, I use Dropbox to help me manage the plugins and configurations files, including the ones for Emacs. I also like to place my backup files there, as I don’t like them being at the directories I am working:

;Backup directory
(add-to-list 'backup-directory-alist '("." . "~/Dropbox/.emacsBackups"))

When I am editing a LaTeX file, I have a number of modes and commands turned on:

;;Start Latex mode with flyspell and writegood minor modes
(setq auto-mode-alist (cons '("\\.tex$" . latex-mode) auto-mode-alist))
(add-hook 'latex-mode-hook (lambda () (flyspell-mode 1)))
(add-hook 'latex-mode-hook (lambda () (writegood-mode 1)))

; Compilation command for Latex mode
(add-hook 'latex-mode-hook (lambda () (set (make-local-variable 'compile-command) (format "make -C %s" (get-closest-pathname)))))

The first one turns on Flyspell and Writegood modes when for when I am working with a LaTeX file. They are very helpful to catch some typos or bad habits when I am writing normal texts or presentation slides. The next one is related to the compilation and it runs the closest Makefile in relation to the directory of the currently open file, which is usually in the same directory or the parent directory. For this to work the following command is needed:

; Find the closest Makefile
(require 'cl) ; If you don't have it already
(defun* get-closest-pathname (&optional (file "Makefile"))
  "Determine the pathname of the first instance of FILE starting from the current directory towards root.
This may not do the correct thing in presence of links. If it does not find FILE, then it shall return the name
of FILE in the current directory, suitable for creation"
  (let ((root (expand-file-name "/"))) ; the win32 builds should translate this correctly
		for d = default-directory then (expand-file-name ".." d)
   			if (file-exists-p (expand-file-name file d))
				return d
			if (equal d root) return nil)))

Also related to this, I use a command that closes the compilation screen if it is successful, so I don’t need to do it myself and just go back to focus on the file I am editing:

; If it is latex-mode, close compilation screen after it's done
(add-hook 'compilation-finish-functions
  (lambda (buf strg)
      (string= "latex-mode" (buffer-mode (other-buffer (current-buffer) 1)))
	(let ((win  (get-buffer-window buf 'visible)))
          (when win (delete-window win)))

Regarding compilation, I have two more commands for the two other languages I usually use (C++ and Python):

; Compilation commands for different environments
(require 'compile)
(add-hook 'c++-mode-hook (lambda () (set (make-local-variable 'compile-command) (format "make -C %s run" (get-closest-pathname)))))
(add-hook 'python-mode-hook (lambda () (set (make-local-variable 'compile-command) (format "python %s" (buffer-name)))))

They work in similarly to the way I explained in the case of a LaTex file, but in case of C++ it also runs the program after compiling, and in case of Python it just runs the script directly without looking for a Makefile.

I like to keep the key bindings the same across different modes, and the way I managed to do it was by overriding the global bindings with a minor mode (this idea was borrowed from an answer at StackExchange):

;; Initialize myMode to override other keybidings
(load "~/Dropbox/SharedConf/myMode.el")
(require 'my-mode)

Then, for example, to make the compile command run with “C-c C-k” in any mode, I define it as a key in the minor mode:

(define-key my-mode-map (kbd "C-c C-k") 'compile)

Finally, I found a command that is useful when I want to split my buffer just as I would open another tab in a web browser (“C-t”):

(defun other-window-or-split ()
  (when (one-window-p)
  (other-window 1))
(global-set-key (kbd "C-t") 'other-window-or-split)

There are other configurations in the file at GitHub, and I would be happy to talk about them. Also, I am sure there are many other great ways to do things and tips when working with Emacs, so the comments are open for suggestions.