Open access peer-reviewed chapter

# Convolution Kernel for Fast CPU/GPU Computation of 2D/3D Isotropic Gradients on a Square/Cubic Lattice

By Sébastien Leclaire, Maud El-Hachem and Marcelo Reggio

Submitted: December 20th 2011Reviewed: March 14th 2012Published: September 26th 2012

DOI: 10.5772/46455

## 1. Introduction

The design of discrete operators or filters for the calculation of gradients is a classical topic in scientific computing. Typical applications are gradient reconstruction in computational fluid dynamics, edge detection in computer graphics and biomedical imaging, and phase boundary definition in the modeling of multiphase flows.

Edge detection, which is widely performed in image analysis, is an operation that requires gradient calculation. Commonly used edge detection methods are Canny, Prewitt, Robertsand Sobel, which can be found in MATLAB’s platform. In this field, edge detection techniques rely on the application of convolution masks to provide a filter or kernel to calculate gradients in two perpendicular directions. A threshold is then applied to obtain an edge shape.

For multiphase flows, an edge or contour corresponds to the interface between the fluids. In this respect, traditional gradient calculation methods based on 1D edge detection are not necessarily suited for the underlying physics, because there is no direction in which the gradients of the phase contours tend to evolve over time. As a result, definition of the geometric progress of the interface requires many gradient estimation computations, as is the case in moving and deforming bubbles or droplets, for example. Although it can still be a viable tool, it is clear that the 1D-based method is becoming less useful for simulating these phenomena, which are not, in general, biased toward any particular direction.

To address this issue, we present an efficient computational method for obtaining discrete isotropic gradients that was previously applied to simulate two-phase flows within the lattice Boltzman framework [1, 2]. This "omnidirectional" approach makes it possible to improve the limitations inherent in handling high density ratios between the phases and to significantly reduce spurious currents at the interface. The method is based on a filter which is generally not split along any direction, and there is no need to make the assumption of a continuous filter to reach isotropy, as done by . We also believe that optimal or maximal isotropy can only be reached with a discrete filter when the error terms of Taylor’s series expansion are isotropic, as explained in detail by [1, 2].

Below, we describe isotropic and anisotropic discretizations that will and will not conserve the isotropic property of the differentiated function respectively. This is followed by a description of how convolution can be used to reduce computer time in the gradient calculation. We then present details of the MATLAB implementation of these ideas, along with speedup comparisons of convolution performed on a single core of an Intel® Core i7-970 processor and on an Nvidia® GeForce GTX 580 GPU using the Jacket plugin for MATLAB developed by AccelerEyes®. The GPU with the Jacket plugin for MATLAB speeds up gradient computation by a factor of up to 138xin the more challenging case. Our study ends with an example of multiphase flow simulation conducted using the lattice Boltzmann method. Finally, we provide useful stencils and weights (or kernels) to yield isotropic gradients in two and three dimensions.

Let us define the real scalar functionF(x,y)R. There are various methods for calculating the derivative of a function, and one way of doing so is to apply a finite difference approximation. If both the horizontal and vertical lattice spacings are hin length, a simple procedure for evaluating the gradient of this function is as follows:

Fx16h2iF(x+cix,y+ciy)cixE1
Fy16h2iF(x+cix,y+ciy)ciyE2

with

cx=[0,h,h,h,0,-h,-h,-h]E3
cy=[h,h,0,-h,-h,-h,0,h]E4

This finite difference discretization is very similar to Prewitt’s operator/kernel , which is used in image processing. Note that h=1in this field, and that the application of Prewitt’s operator results in a vector that points in the same direction as the finite difference gradient, but with a norm 6times larger. The application of Prewitt’s operator to an image can be computed very quickly using a convolution product. We address the topic of convolution product later in section (3), but first, let us analyze the isotropy property of the previous gradient discretization.

### 2.1. Anisotropic discretization

As in Ref.  and without loss of generality, the function Fis expressed using a 2D Taylor series expansion around the zero vector:

F(x,y)=F(0,0)+xFx|(0,0)+yFy|(0,0)+12x22Fx2|(0,0)+xy2Fxy|(0,0)E5
+12y22Fy2|(0,0)+16x33Fx3|(0,0)+12x2y3Fx2y|(0,0)E6
+12xy23Fxy2|(0,0)+16y33Fy3|(0,0)+O(xnym)E7

with

n+m>3E8
.

To calculate the gradient in the xdirection, the following stencil values can be taken:F(h,h), F(h,0), F(h,-h), F(-h,-h), F(-h,0), andF(-h,h). When these stencil values are combined, as in the case of the gradient approximation in Eqs. (1) and (2), an exact expression for the gradient in the xdirection at (0,0)is obtained:

Fx|(0,0)=16h(F(h,h)+F(h,0)+F(h,-h)-F(-h,-h)-F(-h,0)-F(-h,h))-h263Fx3|(0,0)-h233Fx2y|(0,0)+O(h3)E9

A similar expression is found for the ydirection:

Fy|(0,0)=16h(F(-h,h)+F(0,h)+F(h,h)-F(h,-h)-F(0,-h)-F(-h,-h))-h263Fy3|(0,0)-h233Fxy2|(0,0)+O(h3)E10

In the gradients of Eqs. (5) and (6), the leading O(h2)differential operator of the error term can be written in vector form:

E(2)=-h26[3x3+23x2y,3y3+23xy2]E11

Using the following transformation from a Cartesian to a polar partial derivative operator:

x=cos(θ)r-1rsin(θ)θE12
y=sin(θ)r+1rcos(θ)θE13

And, by supposing that F=F(r)is rotationally invariant, it is possible to show (after a lengthy algebraic manipulation) that the vector that results when the differential operator E(2)is applied to F(r)will have a Euclidean norm that is a function of θandr, except if F(r)is a constant. This result, that is, the norm||E(2)F(r)||f(r,θ), is an equation that takes up almost a page, and so is not presented here. Let us note, however, that this expression can easily be obtained using symbolic mathematics software. For a vector function to be rotationally invariant, it must have an Euclidean norm that is a function of the radius only. Therefore, this gradient approximation is not isotropic to the second order in space, but anisotropic. It is worth noting that the calculated derivatives have a leading error term that is not isotropic, even if the function Fis isotropic, i.e. F=F(r)around (0,0). This means that the discrete gradient will not conserve the isotropic property of the differentiated function when the gradient is approximated with this finite difference stencil.

### 2.2. Isotropic discretization

Taking into consideration the previous anisotropy problem, it is possible to change the weights of the grid points when computing the gradients to make them isotropic, up to the second order in space, by defining gradients in the xand ydirections, as follows:

Fx|(0,0)=112h(F(h,h)+4F(h,0)+F(h,-h)-F(-h,-h)-4F(-h,0)-F(-h,h))-h26x(2Fx2+2Fy2)|(0,0)-h472x(4Fx4+24Fx2y2+4Fy4)|(0,0)-h41805Fx5|(0,0)+O(h5)E14
Fy|(0,0)=112h(F(-h,h)+4F(0,h)+F(h,h)-F(h,-h)-4F(0,-h)-F(-h,-h))-h26y(2Fx2+2Fy2)|(0,0)-h472y(4Fy4+24Fx2y2+4Fx4)|(0,0)-h41805Fy5|(0,0)+O(h5)E15

With this new discretization, the dominant differential operator of the second order error term takes the form:

E(2)=[Ex(2),Ey(2)]=-h26[x(2x2+2y2),y(2x2+2y2)]E16

If a gradient has a small dependence on direction, this would imply that the dominant error term has only an axial dependence when the function being derived also only depends on the radius. That is, the operator in Eq. (12) applied on F(r)would lead to a function that depends only on the radius:

E(2)F(r)erf(r)E17

with er=[cos(θ),sin(θ)]being the unit radial vector. Using the partial derivative operator transformation of Eqs. (8) and (9), and supposing that F=F(r)around (0,0), the components Ex(2)F(r)and Ey(2)F(r)are:

Ex(2)F(r)=h2cos(θ)6r2(-F(r)r+r2F(r)r2+r23F(r)r3)E18
Ey(2)F(r)=h2sin(θ)6r2(-F(r)r+r2F(r)r2+r23F(r)r3)E19

which can be rewritten in the same form as Eq. (13). Similarly, the first differential operator of the fourth order error term in Eqs. (10) and (11) takes the form:

Eiso(4)=[Ex,iso(4),Ey,iso(4)]=-h472[x(4x4+24x2y2+4y4),y(4y4+24x2y2+4x4)]E20

and the associated components in polar coordinates, when applied to a rotationally invariant function F(r)around (0,0), are given by:

Ex,iso(4)F(r)=h4cos(θ)72(3F(r)r-3r2F(r)r2+3r23F(r)r3-2r34F(r)r4-r45F(r)r5)E21
Ey,iso(4)F(r)=h4sin(θ)72(3F(r)r-3r2F(r)r2+3r23F(r)r3-2r34F(r)r4-r45F(r)r5)E22

which again meets the rotational invariance requirement, and can be rewritten in the same form as given in Eq. (13). The last differential error operator of the fourth order error term in Eqs. (10) and (11) is:

Eani(4)=-h4180[5x5,5y5]E23

and can be shown to be anisotropic (i.e. fin Eq. 13 would also be a function ofθ). Therefore, the error associated with the anisotropy is lower by two orders when compared to the main second order leading term. It is important to point out that both gradient approximations presented so far are second order approximations in space, but only the latter is a second order approximation in space for the isotropy.

In this work, we only consider the gradient approximation of scalar functions over a square or cubic lattice of unit spacing, so we need to take h=1from now on. In a more general way, the stencil points with the corresponding weights needed to obtain 2D and 3D isotropic gradients was generalized by .

## 3. Convolution

Here, we present the mathematical and computational aspects of convolution. As finite difference discretization and edge detection kernels are very similar, let us return to the mathematical foundations of these techniques. We often give examples involving 2D images, but we could give the same examples and talk about 2D discrete functions. We don’t know the exact coding in the MATLAB and Jacket libraries, as they are under license, but we do have a general idea about function algorithms, which is given in the next section.

### 3.1. Frequential and spatial filtering

The convolution product of two functions f(x)andg(x), defined by Eq. (20), calculates an average function by sliding g all over f. In common language, we say that the function g(x)acts as a filter of the functionf(x).

(f*g)(x)=-f(x)g(x-s)dsE24

If Eq. (20) defines the convolution of two functions in the space domain, it is also equivalent to the point wise product of these functions in the frequency domain. In mathematics, the convolution theorem for Fourier transforms [5, chap. 4] is formalized by the following equation, where Fcorresponds to the Fourier transform applied on a function:

F{f*g}=F{f}-F{g}E25

The Fourier transform of a function generates the frequency spectrum of a function. This representation can easily be extended to two dimensions, which is more suitable to image or spatial analysis if the image is a function of two spatial variables. In our case, the output of the Fourier transform will generate a 2D function in the frequency domain from the 2D spatial domain. High frequencies will correspond to information varying rapidly in the original function, while low frequencies correspond to slow variations.

By applying the inverse Fourier Transform to both sides of Eq. (21), we obtain a method for calculating the convolution of two functions:

f*g=F-1{F{f}-F{g}}E26

Therefore, there are two possible ways of convoluting two functions: the Fourier transform pipeline in three steps (Fourier transform, frequency spectrum filtering, and Fourier inverse transform), or the application of a stencil in the spatial domain. Indeed, edge detection kernels acts as high pass filters, accentuating high frequency contributions to the image or to the 2D function in the form of edges and details.

### 3.2. Discrete circular convolution

Knowing that the properties of the Fourier transform also work for a sampled signal, we present the definition of the 2D discrete Fourier transform (2D DFT), where M and N represent the number of samples in each dimension, x and y are the discrete spatial variables, and u and v are the transform or frequency variables:

F{u,v}=1MNx=0N-1y=0M-1f(x,y)e-2πj(uxN+vyM)E27

In addition to having the same properties as the continuous Fourier transform, which are outside the scope of this presentation and can be found in , there are two more in 2D DFT that are important: periodicity and separability. Since the discrete signal is sampled from the finite length sequences, its frequency spectrum will be periodic. In the spatial domain, this property allows us to slide the filter from one frontier to the opposite one, and then start again at the frontier where we began. Moreover, its Fourier transform is separable. The 2D DFT can be processed in two steps: applying 1D DFT on the lines, and then applying 1D DFT on the resulting columns. In the spatial domain, a 2D filter represented by K1-by-K2matrix is separable if it can be obtained by the outer product of two vectors of size K1andK2, knowing that DFT separability does not imply that the matrix kernel is always separable.

As an example, let us take kernelX, the 2D separable kernel in section (4.2):

1630-316141=13630-3141=11213112000-112-13-112E28

We can easily deduce the main functionality of the 2D kernel, the gradient component in x that approximates the derivative in x, by calculating the difference between the first and third rows in the neighborhood of a point, instead of the first and third columns, because of the rotation of our axis system. The separated 1D filters give us further information about the kernel function: a smoothing mask is applied on the differentiating mask, in order to reduce noise that could be exaggerated by the first filter.

Now, to explain how to apply a circular convolution mask in the spatial domain, we go back to the definition of convolution for discrete functions, presented in 2D and 3D in Eqs. (25) and (26) respectively, where A is the image and B is the mask. Processing a convolution filter consists of computing the scalar product of the filter weights with the input values within a window of the filter dimensions surrounding each of the output values, after flipping the mask in each dimension, as described in [7, chap. 4].

(A*B)(x,y)=ijA(i,j)B(x-i,y-j)E29
(A*B)(x,y,z)=ijkA(i,j,k)B(x-i,y-j,z-k)E30

In circular convolution, the equation is slightly modified to model periodic shifting. Here we consider a square image N by N:

(A#B)(x,y)=ijA(i,j)B[(x-i)modN,(y-j)modN]E31

The values on the image we see in the window of the K-by-K filter are distributed on a periodic surface. The 2D filter is rolled up in the opposite direction and will turn in a clockwise direction. For each output value, the stencil is lined up on the input values, the scalar product is applied, and then the stencil rotates to shift to the next position to compute. Therefore, in the spatial domain, an N-by-N image convoluted with a K-by-K filter requires N2K2multiplications and nearly N2K2additions. The K2multiplications or additions can be reduced to 2Kif the filter is separable, resulting in a total complexity of O(N2K)for spatial convolution with a separable filter.

In the frequency domain, the 2D discrete Fourier transform and its inverse are computed using the divide-and-conquer algorithm of the Fast Fourier Transform (FFT and IFFT), which reduces the complexity from N3multiplications and N3additions to 2N2log2(N)operations. The same order of complexity is required to compute the FFT of the K-by-K filter:2K2log2(K). To those computational complexities, the product cost of two complex numbers, which is six operations, has to be added N2times. In the frequency domain, the resulting computational complexity of convoluting an N-by-N image by a K-by-K filter would equal2N2log2(N)+K2log2(K)+6N2, or O(N2log2(N))ifNK. Furthermore, the separability property could be used to implement a 2D FFT by the application of 1D FFT along each direction, in turn, with a global transpose between each of the 1D transform, which computationnal complexity isO(Nlog2(N)). To decide whether to use the FFT algorithm or spatial convolution, the two complexity functions should be compared to verify that convolution in the spatial domain performs better than FFT for small kernels only.

## 4. MATLAB

In this section, we present a systematic method that can be applied in MATLAB to calculate the gradient of scalar functions on a square lattice, or in 2D, which is simply the gradient of the images. The 3D case is a straightforward extension of the 2D case. First, let us define a function with which it is possible to test the gradient discretization previously defined in section (2.2):

F(x,y)=Ne-30((xN-12)2+(yN-12)2)E32

For the purpose of this test and for simplicity, we consider only a square image Nin width. The exponential function is multiplied by Nto make sure that the scaling ratio of the function F is always the same, whatever the value ofN. In MATLAB m-code, this function can be defined as:

Figures 1(a) and 1(b) show the surface shape and contour of this function.

### 4.1. Naive computation of the gradient

When the gradient of a function needs to be computed, it may be tempting to apply Eqs. (10) and (11) directly. This is quite a simple approach, and in MATLAB can be applied as follows:

However, this is rather a naive and slow implementation for MATLAB. It is far more efficient to evaluate the gradient by using a convolution product, as presented in section (4.2).

### 4.2. Techniques for calculating the gradient with convolution

Instead of calculating the gradient as previously shown, the use of a convolution product between the functions to differentiate them, and a kernel representing the weights and stencils of the finite difference approximation is more beneficial. For the isotropic discretization of Eqs. (10) and (11), this computation can be performed in MATLAB as follows:

A useful list of 2D and 3D kernels for calculating isotropic gradients is available in sections (6.1) and (6.2).

### 4.3. Separable kernel

Sometimes the kernel is separable, which means that, instead of applying an nD convolution, we can apply an 1D convolution ntimes to obtain the same result with much less computational effort. The previous kernel for computing a 2D isotropic gradient with only the nearest neighbors is, fortunately, separable, and in MATLAB the evaluation takes place as follows:

Note that in the work of , one of the most important 3D isotropic kernels, which is the one that uses the nearest neighbors only, was not presented. In this work, two variants of this kernel were obtained, one using only 10 nearest neighbors and the other using only 18 nearest neighbors, and their MATLAB form are given in section (6.2). Moreover, the 3D kernel using 18 nearest neighbors has the advantage of being separable, which means that we should expect to be able to rapidly compute a 3D gradient of low isotropic order using the knowledge presented in this chapter. Also note that the higher isotropic order (>2nd) kernels given in sections (6.1) and (6.2) are not separable.

### 4.4. Accuracy

In this work, the order accuracy is defined byn, the value in hnin Taylor’s series expansion for which the space or the isotropic order is achieved. Therefore, the space and isotropic order may be different. Although this may not have been explicitly stated by [2, 1], all isotropic gradients found in sections (6.1) and (6.2) are of second order accuracy in space, i.e. if the lattice size doubles, the error on the gradient is divided approximately by four. This can be demonstrated numerically, or analytically by means of the expression Ex(2)F(r)and Ey(2)F(r)in polar coordinates, as in Eqs. (14) and (15). These expressions are non zero for every isotropic gradient presented in this work.

The main point about the kernel that we make in this study is that, as its size increases, it becomes possible to set isotropic the higher order error term in the Taylor series expansion at fixed space second order accuracy. It is therefore important to be careful not to confuse space order accuracy and isotropic order accuracy. We believe that, based on this result, future research could provide other kernels (perhaps of similar size) for which the leading space order accuracy would be the same as the leading isotropic order accuracy. For some applications, finding such kernels could be a significant step forward. In fact, achieving leading higher space order accuracy with equal leading isotropic order accuracy might have greater impact than achieving leading low space order with very high isotropic order accuracy, as is currently the case. However, higher space order gradient discretizations may suffer from another non physical numerical artifacts, known as spurious oscillations.

### 4.5. Performance

As previously indicated, computation of the gradient by applying convolution is faster than using a simpler, more straightforward method, but a naive one. We present some numerical results in this section that will show that this is indeed true. We also show that using a GPU instead of a CPU significantly reduces computation time.

First, all performance testing consists in evaluating gradients of random 2D and 3D double precision images. Note that we suppose a periodic padding for these images. All these image gradients are computed using MATLAB with the -singleCompThread startup option. This is done for benchmarking purposes, because the reference case should be computed using a sequential algorithm, that is, with a single core only. The CPU used in this work is an Intel® Core i7-970 processor, while the GPU is an Nvidia® GeForce GTX 580. All computations on the GPU are performed in MATLAB via the Jacket plugin developed by AccelerEyes®. The version of MATLAB is R2010b, and the Jacket version is 2.0 (build a15607c).

The timing method on the CPU is the usual MATLAB tic; m-code; toc; procedure. However, this method is not suited for timing m-code on the GPU. For this, we refer the reader to the method proposed by . In Figures 2-5, the time taken for one simulation "dot" or result "dot" is the average of a hundred simulations.

To test performance, five different algorithms are considered for computing the gradient:

1. MATLAB singlethread naive (section 4.1)

2. MATLAB singlethread convolution (section 4.2) [REFERENCE CASE]

3. MATLAB Jacket naive

4. MATLAB Jacket convolution

5. MATLAB Jacket GFOR + convolution

Note that all results differ with respect to machine accuracy in double precision, and that the padding of the images is computed on the fly to save computer memory. This is because padding is very cheap in terms of computing cost, when compared to the cost of evaluating the gradient.

Case (4), MATLAB Jacket convolution, is the GPU equivalent of reference case (2) with a CPU. Case (3) is the GPU equivalent of case (1) with a CPU. The last case (5), MATLAB Jacket GFOR + convolution, is a special case that is not available on the CPU. To explain this, let us suppose that the user wishes to evaluate the gradient of Ndifferent images simultaneously. Using MATLAB, this is not possible without parfor, which is only available with the Parallel Computing Toolbox. Also note that parfor is usually used for coarse-grained parallelism, while gfor can be used for fine-grained parallelism. Without parfor, an for loop is required to evaluate the gradients of the images sequentially, and to store the results in one large matrix by subassignment. With Jacket, it is possible to perform these Nconvolutions in parallel on the GPU using an gfor loop. Usually, this significantly reduces computation time, compared to the sequential for loop. More details on the functionality and limitations of the gfor loop can be found in . In order to be able to compare the gfor loop case to the other cases, all performance tests have been conducted withN=3, unless otherwise stated, i.e. it is supposed the user needs to evaluate three image gradients simultaneously at a given time.

In all the figures showing performance behavior, the y-axis is in log scale. Figures 2(a) and 2(b) show the performance speedup with the 2D 2nd and 14th order isotropic gradients as a function of image size. For large images, speedups of 31xto 39xcan be achieved.

Figures 3(a) and 3(b) show the same situation, but with the 3D 2nd and 8th order isotropic gradients. For large images, a speedup of 34xto 124xcan be achieved. These results indicate an important speedup and show the usefulness of Jacket for MATLAB.

Figures 4(a) and 4(b) show the speedup with the 2D and 3D isotropic gradient as function of the isotropy order at a fixed image size. In 2D, images are 992x992, and in 3D they are 110x110x110. For high isotropy, speedups of 49xto 124xcan be achieved.

Figure 5 shows the speedup with the 3D 8th order isotropic gradient as a function ofN, the number of images to be evaluated simultaneously at a fixed image size of 110x110x110. Speedups of 86xto 138xcan be achieved, depending on the value ofN.

For both 2D and 3D cases, as the isotropy order or the number of images to evaluate simultaneously increases, the speedup that can be achieved using the GPU also increases. This is to be expected, since the computational complexity increases. Nevertheless, the chances of obtaining a speedup of 86xto 138xfor the more challenging case were much better than we had anticipated. In fact, the Jacket plugin allowed us to tackle problems that would not have been possible to deal with without using a low level programming language.

We must remember that the speedups were computed with the reference case, which uses convolution. It is important to note that a speedup of 280xmay be achieved for the most computationally expensive test (Fig. 5) when comparing the two following cases: Jacket GFOR + convolution, and MATLAB singlethread naive. Thus, depending on how the gradient evaluations are implemented, a huge difference in computational time may be reached. Note that Jacket code does not currently take advantage of the zero entry in the kernel, and that the convolutions are performed in the spatial domain only for 9x9 kernels in 2D and 3x3x3 kernels in 3D. The naive implementation always takes advantage of the zero entry in the kernel, which means that the zero entry could yield an additional speedup, because some kernels are sparse. For example, 2D kernels have 26%to 68%zeros, while in 3D, this number varies from 33%to84%. The 3x3 kernel contains 33%zeros while the 3x3x3 kernels contain 67%or 33%zeros.

## 5. Scientific application: lattice Boltzmann method

The lattice Boltzmann method is a computational approach that is mainly used for fluid flow simulation with its roots in the field of cellular automata . This method is particularly useful for solving complex flow systems, such as multiphase flows, in porous media where classical approaches based on the Navier-Stokes equations, like finite volumes or finite elements, encounter some difficulties. The method we use is based on the original Ph.D. thesis of . Since then, there have been several improvements. However, these enhancements are outside the scope of this chapter, and will not be described.

The isotropic gradients we present here are useful for simulating immiscible multiphase flows, where the orientation of the various fluid interfaces has to be computed very frequently. The gradients of the density of each fluid color (phase) define the normal orientation of the interface, and special operators are used to keep the different interfaces between the fluids defined. Moreover, the norm of these gradients serves as a means to introduce a certain amount of surface tension between the fluids.

Suppose we wish to simulate the behavior of three fluids. At a certain point in the algorithm, three image gradients need to be computed, corresponding to the interface normal of each fluid density. Here is where the methods presented in this chapter become useful, a situation described in section (4.5) withN=3. It is important to point out that the isotropy of the gradient helps to reduce some of the computational artifacts that appear at the interface [1, 2]. These are called spurious currents in the lattice Boltzmann community, and are non-physical.

In multiphase flow simulation, calculation of a high order isotropic gradient is the most expensive part of the method, and the use of Jacket, has enabled us to reduce the computational cost by an astonishing amount. This type of calculation would not have been possible, in a reasonable time, by applying plain MATLAB.

We end this section with a simulation example that shows the spinodal decomposition of a three phase flow. This flow consists of an initial random mixture, where each phase self-agglomerates until a steady state is achieved. Figure 6 shows the spinodal decomposition at various times (in lattice Boltzmann units). Note that this simulation is given for illustration purposes only, and that spinodal decomposition has been quantitatively studied by .

## 6. Convolution kernel for discrete gradient approximation

In this section, we give several convolution kernels in the form of MATLAB m-code, which is very useful for calculating 2D/3D isotropic gradients on a square or cubic lattice. Most of the weights were taken from Ref. .

## 7. Conclusion

In this work, a detailed description of isotropic gradient discretizations and convolution products has been presented. These isotropic gradients are useful, and superior to anisotropic discretizations. This is especially true in the field of flow simulation, when the lattice Boltzmann method is used. However, high order isotropic gradients are computationally expensive. To address this issue, we combined the convolution product with the Jacket plugin in MATLAB and GPU hardware, which enabled us to achieve high computational speedups (up to138x), compared to plain MATLAB computations using a CPU. We end this chapter with one final note. While we have provided a useful list of MATLAB m-code defining the kernels needed for evaluating 2D and 3D isotropic gradients, these kernels only lead to second order space accuracy with high isotropic order. The development of kernels that would lead to high order space accuracy combined with high isotropic order could generate significant benefits, particularly for the lattice Boltzmann community addressing multiphase flows. However, the benefits may not be straightforward, because the higher space order gradient discretization may lead to another unwanted numerical artifacts, such as spurious oscillations in regions separating two phases.

### Acknowledgement

We extend our special thanks to Pavan Yalamanchili from AccelerEyes for his quick response to our queries and his generous support. We applied the sequence-determines-credit (SDC) approach to our listing of authors . This work was supported by a grant from the NSERC (Natural Sciences and Engineering Research Council of Canada).

chapter PDF
Citations in RIS format
Citations in bibtex format

## More

© 2012 The Author(s). Licensee IntechOpen. This chapter is distributed under the terms of the Creative Commons Attribution 3.0 License, which permits unrestricted use, distribution, and reproduction in any medium, provided the original work is properly cited.

## How to cite and reference

### Cite this chapter Copy to clipboard

Sébastien Leclaire, Maud El-Hachem and Marcelo Reggio (September 26th 2012). Convolution Kernel for Fast CPU/GPU Computation of 2D/3D Isotropic Gradients on a Square/Cubic Lattice, MATLAB - A Fundamental Tool for Scientific Computing and Engineering Applications - Volume 3, Vasilios N. Katsikis, IntechOpen, DOI: 10.5772/46455. Available from:

### chapter statistics

4Crossref citations

### Related Content

Next chapter

#### MATLAB/Simulink-Based Grid Power Inverter for Renewable Energy Sources Integration

By Marian Gaiceanu

First chapter

#### PID Control Design

By A.B. Campo

We are IntechOpen, the world's leading publisher of Open Access books. Built by scientists, for scientists. Our readership spans scientists, professors, researchers, librarians, and students, as well as business professionals. We share our knowledge and peer-reveiwed research papers with libraries, scientific and engineering societies, and also work with corporate R&D departments and government entities.