A lot of scientists and programmers want to take advantage of fast and parallel computational devices. Writing vectorized code is becoming a necessity to get the best performance out of the current generation parallel hardware and scientific computing software. However writing vectorized code may not be intuitive immediately. There are many ways you can vectorize a given code segment. Each method has its own benefits and drawbacks. Hence writing vectorized code involves analyzing the pros and cons of the available methods and choosing the right one to solve your problem.

In this post we present various ways to vectorize your code using ArrayFire. ArrayFire is chosen because of my familiarity with the software. The same methods can be easily used in numpy, octave, julia or other scientific computing software.
<h3>The problem</h3>
In this post we will be looking to speed up the calculation of distances from every point in a set <strong>A</strong> to every point in set <strong>B</strong>. i.e. if <strong> A </strong> has <strong> 200 </strong> points and <strong> B </strong> has <strong> 300 </strong> points, the distance matrix will be of size <strong>200 x 300</strong>. Each point has co-ordinates in 3 dimensional space. We can generate the necessary data in the following manner.

<pre>
A = randu(3, 200);
B = randu(3, 300);
</pre>
<h3>Naive implementation</h3>
The naive method would be to have three loops. The outer most loop iterating through points in A. The second loop iterating through points in B. The inner most loop iterating through the three dimensions. You can find difference in co-ordinates between the two points and find the distance between the two points.

This can be implemented in the following manner.

CODE HERE
<h3>Vectorizing along one dimension</h3>
Keeping the outer two loops the same, you can improve the performance by treating the set of three co-ordinates as a single vector. You can perform the difference between these vectors in parallel. A reduction operation after getting the absolute differences will give us the distance without having to use the third loop.

CODE HERE
<h3>Using GFOR</h3>
GFOR is a parallel version of the for loop in ArrayFire. Since GFOR's can not be nested, we can replace one of the for loops using GFOR. This can be done in the following manners.

CODE HERE 1

CODE HERE 2
<h3>Vectorizing along two dimensions</h3>
We can get rid of another FOR loop to have 2-D vectorized code. You can get a single point from set <strong>A </strong>and replicate it to create a matrix that is the same size as <strong>B</strong>. You can now find distance of a <strong><em>single</em></strong> point in <strong>A </strong>from <strong><em>all </em></strong>points in <strong>B</strong>. The code can be seen below.

CODE HERE
<h3><strong>Vectorizing along three dimensions</strong></h3>
The previous method can be further extended. This would involve getting the differences between every point in <strong>A<em> </em></strong> from every point in <strong>B </strong> in parallel. This can be achieved by modifying and tiling <strong>A</strong>, <strong>B </strong>to get matrices of size <strong>[3, Alen, Blen]</strong>.

For <strong>A, </strong>we need to tile it for each point present in <strong>B</strong>.<strong> </strong>This generates a 3D array of the desired dimensions. For <strong>B, </strong>we need to first modify it such that each point in <strong>B</strong> is represented in the 3rd dimension rather than the second. This is followed up by a tiling operation in the second dimension to generate a 3D array of the desired dimensions. A simple illustration of what is happening can be seen below.

RESULTS HERE

Once we have the tiled versions of the matrices, a single reduction generates the desired results in a single step. The code for this optimization can be seen below.

CODE HERE
<h3>Results</h3>
The following results have been generated on a NVIDIA Quadro K5000.

RESULTS HERE
<ul>
	<li>As you can see the final performance improvements are about <em><strong>20,000 </strong><strong>times</strong><strong> faster</strong></em> than the <em><strong>naive implementation</strong></em>. It is also <em><strong>29 times faster</strong></em> than the next fastest methods.</li>
	<li><strong>gfor2</strong> (GFOR over B) is faster than <b>gfor2</b> (GFOR over A) because <em><strong>B</strong></em><strong> </strong>has more points than <em><strong>A. </strong></em></li>
	<li><strong>tile1</strong> and <strong>gfor2 </strong>have similar performance because they employ similar mechanisms internally.</li>
</ul>
<h3>Considerations</h3>
<ul>
	<li><strong>tile2 </strong>is the fastest method, but it also uses up the most memory as scratch space. It requires two temporary matrices of sizes <strong>[feat_len, Alen, Blen]. </strong>In this particular case, feat_len was 3, Alen was 200 and Blen was 300. However it is easy for the scratch space requirements to grow to a few gigabytes when the number for points is much higher (as is the case with most machine learning algorithms).</li>
	<li>Though <strong>gfor2 </strong>and <strong>tile1 </strong>give the same performance, <strong>tile1 </strong>should be preferred. This is because <strong>GFOR </strong>can not be nested. However using the <strong>tile1 </strong>method enables us to use GFOR at a higher level in the program.</li>
	<li>Of the functions presented here, <strong>tile1</strong> is perhaps the best compromise when constrained by memory.</li>
	<li>Hybrid algorithms which combine one or two of the method shown here can provide better performance than <strong>tile1.  </strong>We will talk about such algorithms at a later date.</li>
</ul>