1 \chapterauthor{Gilles Perrot}{FEMTO-ST Institute}
3 %\newcommand{\kl}{\includegraphics[scale=0.6]{Chapters/chapter4/img/kernLeft.png}~}
4 %\newcommand{\kr}{\includegraphics[scale=0.6]{Chapters/chapter4/img/kernRight.png}}
9 %% basicstyle=\footnotesize\ttfamily,
16 %% extendedchars=true,
18 %% keywordstyle=\textbf,
20 %% % keywordstyle=[1]\textbf,
21 %% %identifierstyle=\textbf,
22 %% commentstyle=\color{white}\textbf,
23 %% stringstyle=\color{white}\ttfamily,
24 %% % xleftmargin=17pt,
25 %% % framexleftmargin=17pt,
26 %% % framexrightmargin=5pt,
27 %% % framexbottommargin=4pt,
28 %% backgroundcolor=\color{lightgray},
32 \chapter{Implementing an efficient convolution \index{Convolution} operation on GPU}
34 In this chapter, after dealing with GPU median filter implementations,
35 we propose to explore how convolutions can be implemented on modern
36 GPUs. Widely used in digital image processing filters, the \emph{convolution
37 operation} basically consists in taking the sum of products of elements
38 from two 2-D functions, letting one of the two functions move over
39 every element of the other, producing a third function that is typically
40 viewed as a modified version of one of the original functions. To
41 begin with, we shall examine non-separable or generic convolutions,
42 before adressing the matter of separable convolutions. We shall refer
43 to $I$ as an H x L pixel gray-level image, and to $I(x,y)$ as the gray-level
44 value of each pixel of coordinates $(x,y)$.
45 %dire qqs mots sur le filtrage IIR/FIR ?
49 Within a digital image $I$, the convolution operation is performed between
50 image $I$ and convolution mask \emph{h} (To avoid confusion with other
51 GPU functions referred to as kernels, we shall use\emph{ convolution
52 mask} instead of \emph{convolution kernel}) is defined by:
54 I'(x, y) = \left(I * h\right) = \sum_{i < H} \sum_{j < L}I(x-j, y-j).h(j,i)
57 While processing an image, function \emph{h} is bounded by a square
58 window of size \emph{k = 2r + 1}, i.e an uneven number, to ensure
59 there is a center. We shall also point out that, as stated earlier,
60 the square shape is no limiting factor to the process, as any shape
61 can be inscribed into a square. In the case of a more complex shape,
62 the remaining space is filled by null values (padding).
65 \section{Implementation}
66 The basic principle of computing a convolution between one $I$ picture
67 and one \emph{h} convolution mask defined on domain $\Omega$ is given
68 by algorithm \ref{algo_genconv} and illustrated by Figure \ref{fig:convoPrinciple}, which mainly shows how gray-level values of the center pixel's neighborhood are combined with the convolution mask values to compute the output value.
69 For more readability, only part of the connecting lines are shown.
72 \includegraphics[width=11cm]{Chapters/chapter4/img/convo1.png}
73 \caption[Principle of a generic convolution implementation.]{Principle of a generic convolution implementation. The center pixel is represented with a black background and the pixels of its neighborhood are denoted $I_{p,q}$ where $(p,q)$ is the relative position of the neighbor pixel. Elements $h_{t,u}$ are the values of the convolution mask.}
74 \label{fig:convoPrinciple}
77 \caption{generic convolution}
79 \ForEach{pixel at position $(x, y)$}{
80 Read all gray-level values $I(x, y)$ in the neighborhood\\\;
81 Compute the weighted sum \( I_\Omega = \sum_{(j,i) \in \Omega}I(x-j, y-j).h(j,i) \)\\\;
82 Normalize $I'(x, y)$ value\\\;
83 Outputs the new gray-level value
87 The gray-level value of each pixel of output image $I'$ is the weighted
88 sum of pixels included in the neighborhood defined by $\Omega$ around
89 the corresponding pixel in the input image. It has to be noted that,
90 in case the sum $S$ of all coefficients in the mask is not 1, the original
91 brightness of the image will be altered and a normalization stage
92 has to take place, as, for example, in the case of an 8-bit coded
95 \item if $S \ge 0$ then $I' = I_\Omega / S$
96 \item if $S = 0$ then $I' = I_\Omega + 128$
97 \item if $S < 0$ then $I' = I_\Omega + 255$
99 In case one, normalizing means performing a division operation for
100 each pixel, which will be quite time-costly when performed on a GPU. A simple work-around is to normalize mask values before using them in GPU kernels.
103 \subsection{First test implementation}
104 This first implementation consists of a rather naive application to
105 convolutions of the tuning recipes applied to median filters in the
106 previous chapter, as a reminder : texture memory used with incoming
107 data, pinned memory with output data, optimized use of registers
108 while processing data and multiple output per thread\index{Multiple output per thread}.
109 One signifcant difference lies in the fact
110 that the median filter uses only one parameter, the size of the window mask,
111 which can be hard-coded, while a convolution mask requires referring to several; hard-coding
112 its elements would lead to severe lack of flexibility (one function
113 per filter, no external settings) so we will just use it as a starting
114 point in our approach.
116 Let us assume that we are planning to implement the convolution defined by the following $3\times 3$ mask (low-pass filter or averaging filter):
117 $$h=\begin{bmatrix}1&1&1\\1&1&1\\1&1&1\end{bmatrix}$$
118 The kernel code presented in Listing \ref{lst:convoGene3Reg8} implements the convolution operation and applies all above optimizations except, for clarity reasons, multiple output per thread.
119 In the particular case of a generic convolution, it is important to note how mask coefficients are applied to image pixels in order to fit the definition of equation \ref{convoDef}: if the coordinates of the center pixel had been set to (0,0), then the pixel of coordinates $(i,j)$ would have been multiplied by the element $(-i,-j)$ of the mask, which, transposed in our kernel code, leads to multiply the $p^{th}$ pixel of the window by the $(n-p)^{th}$ element of the convolution mask.
121 \lstinputlisting[label={lst:convoGene3Reg8},caption=Generic CUDA kernel achieving a convolution operation with hard-coded mask values]{Chapters/chapter4/code/convoGene3Reg8.cu}
123 Table \ref{tab:convoNonSepReg1} shows kernel timings and throughput values for such a low-pass filter extended to $5\times 5$ and $7\times 7$ masks applied on 8-bit coded gray-level
124 images of sizes $512\times 512$, $1024\times 1024$, $2048\times 2048$, $4096\times 4096$ and run on a C2070 card with $32\times 8$ thread blocks.
125 As a reminder, Table \ref{tab:memcpy1} details the data transfer costs that helped computing throughput values.
131 \begin{tabular}{|c||r|r|r|r|r|r|}
133 \textbf{Mask size$\rightarrow$}&\multicolumn{2}{|c|}{\textbf{3x3}}&\multicolumn{2}{|c|}{\textbf{5x5}}&\multicolumn{2}{|c|}{\textbf{7x7}}\\
134 \textbf{Image size$\downarrow$}&time (ms)&TP&time (ms)&TP&time (ms)&TP\\\hline\hline
135 $\mathbf{512\times 512}$ &0.077&1165 &0.209&559 &0.407 &472 \\\hline
136 $\mathbf{1024\times 1024}$&0.297&1432 &0.820&836 &1.603 &515 \\\hline
137 $\mathbf{2048\times 2048}$&1.178&1549 &\bf 3.265&\bf 875 &6.398&529 \\\hline
138 $\mathbf{4096\times 4096}$&4.700&1585 &13.05&533 &25.56&533 \\\hline
141 \caption[Timings ($time$) and throughput values ($TP$ in Mpix/s) of one register-only non separable convolution kernel, for small mask sizes of $3\times 3$, $5\times 5$ and $7\times 7$ pixels, on a C2070 card.]{Timings ($time$) and throughput values ($TP$ in Mpix/s) of one register-only non separable convolution kernel, for small mask sizes of $3\times 3$, $5\times 5$ and $7\times 7$ pixels, on a C2070 card (fermi architecture). Data transfer duration are those of Table \ref{tab:memcpy1}.}
142 \label{tab:convoNonSepReg1}
148 \begin{tabular}{|c||r|r|r|r|r|r|}
150 \textbf{Mask size$\rightarrow$}&\multicolumn{2}{|c|}{\textbf{3x3}}&\multicolumn{2}{|c|}{\textbf{5x5}}&\multicolumn{2}{|c|}{\textbf{7x7}}\\
151 \textbf{Image size$\downarrow$}&time (ms)&TP&time (ms)&TP&time(ms)&TP\\\hline\hline
152 $\mathbf{512\times 512}$ &0.060&1186 &0.148&848 &0.280&594 \\\hline
153 $\mathbf{1024\times 1024}$&0.209&1407 &0.556&960 &1.080&649 \\\hline
154 $\mathbf{2048\times 2048}$&0.801&1092 &\bf 2.189&\bf 802 &4.278&573 \\\hline
155 $\mathbf{4096\times 4096}$&3.171&1075 &8.720&793 &17.076&569 \\\hline
158 \caption[Timings ($time$) and throughput values ($TP$ in Mpix/s) of one register-only non separable convolution kernel, for small mask sizes of $3\times 3$, $5\times 5$ and $7\times 7$ pixels, on a GTX280.]{Timings ($time$) and throughput values ($TP$ in Mpix/s) of one register-only non separable convolution kernel, for small mask sizes of $3\times 3$, $5\times 5$ and $7\times 7$ pixels, on a GTX280 (GT200 architecture). Data transfer duration are those of Table \ref{tab:memcpy1}.}
159 \label{tab:convoNonSepReg3}
165 \begin{tabular}{|c||r|r|}
167 \shortstack{\textbf{GPU card$\rightarrow$}\\\textbf{Image size$\downarrow$}}&\textbf{C2070}&\textbf{GTX280}\\\hline\hline
168 $\mathbf{512\times 512}$ &0.148 &0.161 \\\hline
169 $\mathbf{1024\times 1024}$&0.435 &0.536 \\\hline
170 $\mathbf{2048\times 2048}$&1.530 &3.039 \\\hline
171 $\mathbf{4096\times 4096}$&5.882 &12.431 \\\hline
174 \caption{Time cost of data transfers between CPU and GPU memories, on C2070 and GTX280 cards (in milliseconds).}
178 Table \ref{tab:convoNonSepReg3} shows timings and global throughput values achieved by those convolution masks on an Nvidia GT200 Tesla architecture (GTX480 card) with $16x8$ thread blocks. This measurement has been done in order to make a relevant comparison with a reference given by Nvidia in \cite{convolutionsoup} where they state that their fastest kernel achieves a $5\times5$ convolution of an 8-bit $2048\times 2048$ pixelimage in $1.4~ms$, which lead to a throughput value of 945~Mpix/s.
179 Our current value of 802~Mpix/s, though not unsatisfactory, remains lower to the one reached by the manufacturer'own coding.
180 Tested in the same conditions, the newer Fermi architecture of
181 Nvidia's GPUs proved slower (3.3 ms, see Table \ref{tab:convoNonSepReg1}) due to the lower maximum
182 register count allowed (63, against 128 for Tesla GT200).
184 It is interesting to note that, as long as each thread processes one single pixel, kernel execution time is ruled in proportion
185 with the number of pixels in the image multiplied by that of the mask.
186 The slope in this first implementaion is $3.14.10^{-8}~ms/pix$ on C2070.
188 \subsection{Using parameterizable masks}
190 To further improve the above implementation, it becomes necessary
191 to free ourselves from the hard-coding constraint. To achieve this,
192 as was the case with input image storing, several memory options are
193 available, but, since the amount of data involved in processing a
194 mask is quite small and constant, we considered it relevant to copy data
195 into \emph{symbol memory}. Listing \ref{lst:symbolmem} details the process, involving
196 the Cuda function \emph{CudaMemCopyToSymbol()}.
198 \lstinputlisting[label={lst:symbolmem},caption=code snippet showing how to setup a mask in GPU symbol memory]{Chapters/chapter4/code/maskInSymbol.cu}
200 In parallel, giving up the register-only constraint allows a more
201 conventional coding practice (loops). Listing \ref{lst:convoGene8r} presents
202 a generic convolution kernel, whose code immediately
203 appears both simple and concise. Its global time
204 performance, however, is comparatively lower than the register-only
205 process, due to the use of constant memory and of the \emph{r} parameter
206 (radius of the mask). The average slope amounts to $3.81~ms/pix$ on C2070,
207 which means a time-cost increase of around $20~\%$.
209 \lstinputlisting[label={lst:convoGene8r},caption=Generic CUDA kernel achieving a convolution operation with the mask in symbol memory and its radius passed as a parameter]{Chapters/chapter4/code/convoGene8r.cu}
211 \subsection{Increasing the number of pixels processed by each thread}
212 Much in the same way as we did with the Median Filter, we shall now
213 attempt to reduce the average latency due to writes into global memory
214 by having each thread process more than one output value. As the basic
215 structure of the above GPU kernel uses only 14 registers per thread, regardless
216 of the size of the convolution mask, one can envisage processing 2
217 or more pixels per thread while keeping safely within the 63-per-thread
220 However, when doing so, \textit{eg} processing what we shall call a \textit{packet} of pixels, window mask overlapping has to be taken into account
221 to avoid multiple texture fetches of each pixel's gray-level value, while benefiting from the 2-D cache.
222 In that case, both mask size and pixel packet shape determine the number of texture fetches to be performed for each pixel value.
223 Figure \ref{fig:convoOverlap1} illustrates two different situations: on top, a mask of radius 1 ($3\times 3$) applied to a packet of 8 pixels in row; at bottom, a mask of radius 2 ($5\times 5$).
224 The dark gray pixels are the center pixels (pixels of the packet), while light gray pixels belong to the halo around the packet. The number in each pixel box corresponds to the convolution count in which it is involved.
225 There would be little interest in using different \textit{packet} shapes, as the final global memory writes would not be coalescent; generating multiple latencies.
228 \subfigure[$3\times 3$ mask: there are 18 center pixels (out of 30) involved in 3 computations.]{ \includegraphics[width=5.8cm]{Chapters/chapter4/img/convoOverlap1.png}}\\
229 \subfigure[$5\times 5$ mask: only 20 center pixels (out of 60), involved in 5 computations.]{ \includegraphics[width=7cm]{Chapters/chapter4/img/convoOverlap2.png}}
230 \caption{Mask window overlapping when processing 8 pixels per thread. Top: $3\times 3$ mask. Bottom: $5\times 5$ mask.}
231 \label{fig:convoOverlap1}
234 Altough we actually wrote GPU kernels able to process 2, 4, 8 and 16 pixels per thread, only the one that processes 8 pixels per thread is presented below, as it proved to be the fastest one. Listing \ref{lst:convoGene8x8pL3} reproduce the source code of the kernel for $3\times 3$ masks.
235 The bottom line is that each thread is associated with one base pixel of coordinates $(x,y)$ which is the first of the packet to be processed, the last one being $(x+7,y)$.
236 \lstinputlisting[label={lst:convoGene8x8pL3},caption=CUDA kernel achieving a $3\times 3$ convolution operation with the mask in symbol memory and direct data fetches in texture memory]{Chapters/chapter4/code/convoGene8x8pL3.cu}
238 In this particular case of a $3\times 3$ mask, each pixel value is used in 3 different convolution sums, except pixels located near both ends of the packet, whose values are used in fewer sums.
239 The general rule, when performing a $n\times n$ convolution (radius $k$) by 8-pixel packets is that each of the $(8-2k).(2k+1)$ \textit{center} pixels of the halo is used in $k$ sums, while the $4k.(2k+1)$ remaining pixels, located around the ends of the packet are used in fewer sums, from $k-1$ to $1$ ($2.(2k+1)$ pixels each).
243 \begin{tabular}{|c||r|r|r|r|r|r|}
245 \textbf{Mask size$\rightarrow$}&\multicolumn{2}{|c|}{\textbf{3x3}}&\multicolumn{2}{|c|}{\textbf{5x5}}&\multicolumn{2}{|c|}{\textbf{7x7}}\\
246 \textbf{Image size$\downarrow$}&time (ms)&TP&time (ms)&TP&time (ms)&TP\\\hline\hline
247 $\mathbf{512\times 512}$ &0.036&1425 &0.069&1208 &0.110&1016 \\\hline
248 $\mathbf{1024\times 1024}$&0.128&1862 &0.253&1524 &0.413&1237 \\\hline
249 $\mathbf{2048\times 2048}$&0.495&2071 &\bf 0.987&1666 &1.615&1334 \\\hline
250 $\mathbf{4096\times 4096}$&1.964&2138 &3.926&1711 &6.416&1364 \\\hline
253 \caption[Timings ($time$) and throughput values ($TP$ in Mpix/s) of our generic fixed mask size convolution kernel run on a C2070 card.]{Timings ($time$) and throughput values ($TP$ in Mpix/s) of our generic fixed mask size convolution kernel run on a C2070 card. Data transfer durations are those of Table \ref{tab:memcpy1}.}
254 \label{tab:convoGene8x8p}
257 Timing results and throughput values are shown in Table \ref{tab:convoGene8x8p}, and show that this solution now outperforms Nvidia references.
258 It is important to remember that the above kernels have been optimized for the Fermi architecture, unlike those mentioned earlier, which were more efficient on the GT200 architecture.
259 However, our technique requires to write one kernel per mask size, which can be seen as a major constraint. To make it easier to use this method, we shall propose a kernel code generator that will be available in the near future.
261 \subsection{Using shared memory to store prefetched data\index{Prefetching}.}
262 \index{memory~hierarchy!shared~memory}
263 A more convenient way of coding a convolution kernel is to use shared memory to perform a prefetching stage of the whole halo before computing the convolution sums.
264 This proves to be quite efficient and more versatile, but it obviously generates some overhead as:
266 \item Each pixel value has to be read at least twice, first from texture memory into shared memory and then one or several more times from shared memory to be used in convolution computations.
267 \item Reducing the number of times a single pixel value is read from shared memory is bound to generate bank conflicts, hence once again performance loss.
271 \includegraphics[width=12cm]{Chapters/chapter4/img/convoShMem.png}
272 \caption[Organization of the prefetching stage of data, for a $5\times 5$ mask and a thread block size of $8\times 4$.]{Organization of the prefetching stage of data, for a $5\times 5$ mask and a thread block size of $8\times 4$. Threads in both top corners of the top figure are identified either by a circle or by a star symbol. The image tile, loaded into shared memory includes the pixels to be updated by the threads of the block, as well as its 2-pixel wide halo. Here, circle and star symbols in the image tile show which pixels are actually loaded into one shared memory vector by its corresponding thread. }
275 Still, we also implemented this method, in a similar manner as Nvidia did in its SDK sample code.
276 Some improvement has been obtained by increasing the number of pixels processed by each thread, to an optimum 8 pixels per thread.
277 The principle is to prefetch all pixel values involved in the computations performed by all threads of a block, including 8 pixels per thread plus the halo of radius $r$ (the radius of the convolution mask). As this obviously represents more values than the thread count in one block, some threads have to load more than one value.
278 The general organization is reproduced in Figure \ref{fig:ShMem1} for $5\times 5$ mask and a $8\times 4$ thread block, while Listing \ref{lst:convoGeneSh1} gives the details of the implementation with its two distinct code blocks: preload in shared memory (Lines 20 to 42) and convolution computations (Lines 45 to 57).
279 Table \ref{tab:convoGeneSh1} details timing results of this implementation ($16\times 8$ threads/block), up to $13\times 13$ masks, that will serve as a reference in the next section, devoted to separable convolution.
283 \begin{tabular}{|c||r|r|r|r|r|r|}
285 \shortstack{\textbf{Mask size$\rightarrow$}\\\textbf{Image size$\downarrow$}}&\textbf{3x3}&\textbf{5x5}&\textbf{7x7}&\textbf{9x9}&\textbf{11x11}&\textbf{13x13}\\\hline\hline
286 $\mathbf{512\times 512}$ &0.040 &0.075 &0.141 &0.243&0.314&0.402\\\hline
287 $\mathbf{1024\times 1024}$&0.141 &0.307 &0.524 &0.917&1.192&1.535\\\hline
288 $\mathbf{2048\times 2048}$&0.543 &\bf 1.115&2.048 &3.598&4.678&6.037\\\hline
289 $\mathbf{4096\times 4096}$&2.146 &4.364 &8.156 &14.341&18.652&24.020\\\hline
292 \caption{Performances, in milliseconds, of our generic 8 pixels per thread kernel using shared memory, run on a C2070 card.}
293 \label{tab:convoGeneSh1}
298 \begin{tabular}{|c||r|r|r|r|r|r|}
300 \shortstack{\textbf{Mask size$\rightarrow$}\\\textbf{Image size$\downarrow$}}&\textbf{3x3}&\textbf{5x5}&\textbf{7x7}&\textbf{9x9}&\textbf{11x11}&\textbf{13x13}\\\hline\hline
301 $\mathbf{512\times 512}$ &1394 &1176 &907 &670&567&477\\\hline
302 $\mathbf{1024\times 1024}$&1820 &1413 &1093 &776&644&532\\\hline
303 $\mathbf{2048\times 2048}$&2023 &\bf 1586 &1172 &818&676&554\\\hline
304 $\mathbf{4096\times 4096}$&2090 &1637 &1195 &830&684&561\\\hline
307 \caption[Throughput values, in MegaPixel per second, of our generic 8 pixels per thread kernel using shared memory, run on a C2070 card.]{Throughput values, in MegaPixel per second, of our generic 8 pixels per thread kernel using shared memory, run on a C2070 card. Data transfer durations are those of Table \ref{tab:memcpy1}.}
308 \label{tab:convoGeneSh2}
310 \lstinputlisting[label={lst:convoGeneSh1},caption=CUDA kernel achieving a generic convolution operation after a preloading of data in shared memory.]{Chapters/chapter4/code/convoGeneSh1.cu}
312 \section{Separable convolution}
313 A convolution operation is said separable when its masks $h$ is the product of 2 vectors $h_v$ and $h_h$, as is the case in the following example:
314 $$h = h_v \times h_h = \begin{bmatrix}1\\2\\1\end{bmatrix} \times \begin{bmatrix}-1&2&-1\end{bmatrix} = \begin{bmatrix}
319 Such a mask allows to replace a generic 2-D convolution operation by two consecutive stages of a 1-D convolution operation: a vertical of mask $h_v$ and a horizontal of mask $h_h$.
320 This saves a lot of arithmetic operations, as a generic $n\times n$ convolution applied on a $H\times L$ image basically represents $H.L.n^2$ multiplications and as many additions, while two consecutive $n\times 1$ convolutions only represents $2.H.L.n$ of each, \textit{eg} 60\% operations are saved per pixel of the image for a $5\times 5$ mask.\\
321 However, beside reducing the operation count, performing a separable convolution also means writing an intermediate image into global memory.
322 CPU implementations of separable convolutions often use a single function to perform both 1-D convolution stages. To do so, this function reads the input image and actually ouputs the transposed filtered image.
323 Applying that principle to GPUs is not efficient, as outputting the transposed image means non-coalescent writes into global memory, generating severe performance loss. Hence the idea of developing two different kernels, one for each of both vertical and horizontal convolutions.
325 Here, the use of Shared memory is the best choice, as there is no overlapping between neighbor windows and thus no possible optimization.
326 Moreover, to ensure efficiency, it is important to read the input image from texture memory, which implies an internal GPU data copy between both 1-D convolution stages.
327 Which, even if it is faster than CPU/GPU data transfer, makes separable convolutions slower than generic convolutions for small mask sizes. On C2070, the lower limit is $7\times 7$ pixels ($9\times 9$ for $512\times 512$ images).
329 Both vertical and horizontal kernels feature similar runtimes: Table \ref{tab:convoSepSh1} only contains their average execution time, including the internal data copy stage, while Table \ref{tab:convoSepSh2} shows the achieved global throughput values. Timings of the data copy stage are given in Table \ref{tab:cpyToArray}.
330 Listings \ref{lst:convoSepShV} and \ref{lst:convoSepShH} detail the implementation of both 1-D kernels, while Listing \ref{lst:convoSepSh} shows how to use them in addition with the data copy function in order to achieve a whole separable convolution. The shared memory size is dynamically passed as a parameter at kernel call time. Its expression is given in the comment line before its declaration.
334 \begin{tabular}{|c||r|}
336 \textbf{Image size}&\textbf{C2070}\\\hline\hline
337 $\mathbf{512\times 512}$ &0.029 \\\hline
338 $\mathbf{1024\times 1024}$&0.101 \\\hline
339 $\mathbf{2048\times 2048}$&0.387 \\\hline
340 $\mathbf{4096\times 4096}$&1.533 \\\hline
343 \caption{Time cost of data copy between the vertical and the horizontal 1-D convolution stages, on a C2070 cards (in milliseconds).}
344 \label{tab:cpyToArray}
349 \begin{tabular}{|c||r|r|r|r|r|r|}
351 \shortstack{\textbf{Mask size$\rightarrow$}\\\textbf{Image size$\downarrow$}}&\textbf{3x3}&\textbf{5x5}&\textbf{7x7}&\textbf{9x9}&\textbf{11x11}&\textbf{13x13}\\\hline\hline
352 $\mathbf{512\times 512}$ &0.080 &0.087 &0.095 &\bf 0.108&\bf 0.115&\bf 0.126\\\hline
353 $\mathbf{1024\times 1024}$&0.306 &0.333 &\bf 0.333 &\bf 0.378&\bf 0.404&\bf 0.468\\\hline
354 $\mathbf{2048\times 2048}$&1.094 &1.191 &\bf 1.260 &\bf 1.444&\bf 1.545&\bf 1.722\\\hline
355 $\mathbf{4096\times 4096}$&4.262 &4.631 &\bf 5.000 &\bf 5.676&\bf 6.105&\bf 6.736\\\hline
357 \caption[Performances, in milliseconds, of our generic 8 pixels per thread 1-D convolution kernels using shared memory, run on a C2070 card.]{Performances, in milliseconds, of our generic 8 pixels per thread 1-D convolution kernels using shared memory, run on a C2070 card. Timings include data copy. Bold values correspond to situations where separable-convolution kernels run faster than non separable ones.}
358 \label{tab:convoSepSh1}
363 \begin{tabular}{|c||r|r|r|r|r|r|}
365 \shortstack{\textbf{Mask size$\rightarrow$}\\\textbf{Image size$\downarrow$}}&\textbf{3x3}&\textbf{5x5}&\textbf{7x7}&\textbf{9x9}&\textbf{11x11}&\textbf{13x13}\\\hline\hline
366 $\mathbf{512\times 512}$ &1150 &1116 &1079 &\bf 1024&\bf 997 &\bf 957\\\hline
367 $\mathbf{1024\times 1024}$&1415 &1365 &\bf 1365 &\bf 1290&\bf 1250&\bf 1169\\\hline
368 $\mathbf{2048\times 2048}$&1598 &1541 &\bf 1503 &\bf 1410&\bf 1364&\bf 1290\\\hline
369 $\mathbf{4096\times 4096}$&1654 &1596 &\bf 1542 &\bf 1452&\bf 1400&\bf 1330\\\hline
372 \caption[Throughput values, in MegaPixel per second, of our generic 8 pixels per thread 1-D convolution kernel using shared memory, run on a C2070 card.]{Throughput values, in MegaPixel per second, of our generic 8 pixels per thread 1-D convolution kernel using shared memory, run on a C2070 card. Data transfer durations are those of Table \ref{tab:memcpy1}.}
373 \label{tab:convoSepSh2}
376 \lstinputlisting[label={lst:convoSepSh},caption=data copy between the calls to 1-D convolution kernels achieving a 2-D separable convolution operation.]{Chapters/chapter4/code/convoSepSh.cu}
377 \lstinputlisting[label={lst:convoSepShV},caption=CUDA kernel achieving a horizontal 1-D convolution operation after a preloading \index{Prefetching} of data in shared memory.]{Chapters/chapter4/code/convoSepShV.cu}
378 \lstinputlisting[label={lst:convoSepShH},caption=CUDA kernel achieving a vertical 1-D convolution operation after a preloading of data in shared memory.]{Chapters/chapter4/code/convoSepShH.cu}
381 Extensively detailing the various techniques that may be applied when designing a median or a convolution operation on GPU has enabled us determine that:
383 \item the use of registers with direct data fetching from texture often allows kernels to run faster than those which use the more conventionnal way of prefetching data from texture memory and storing them into shared memory.
384 \item increasing the pixel count processed by each thread brings important speedups. In this case, if neighboring windows overlap, optimized direct data fetching from texture will likely outperform the shared memory prefetching technique. That is the case for generic convolution kernels.
385 \item coding such optimized data fetching is not straightforward. Consequently, we are planning to provide a kernel code generator that will make our kernels more accessible by GPU users.
387 The presented kernels, optimized for a C2070 card, achieve up to 2138~Mpix/s including data transfers, which comes close to the absolute maximum throughput value allowed by the Fermi architecture. The next GPU generation (Kepler) may allow us not only to benefit from new dynamic parallelism capability to increase kernel paralelism level, but also to take advantage of an increase of the register count allowed per thread block which would allow us, for example, to extend our register-only median filter technique to larger mask sizes.
389 \putbib[Chapters/chapter4/biblio4]