<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The ILNumerics Blog &#187; vectorization</title>
	<atom:link href="https://ilnumerics.net/blog/tag/vectorization/feed/" rel="self" type="application/rss+xml" />
	<link>https://ilnumerics.net/blog</link>
	<description>The Productivity Machine  &#124;  A fresh attempt for scientific computing  &#124;  http://ilnumerics.net</description>
	<lastBuildDate>Thu, 05 Dec 2024 09:09:24 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.41</generator>
	<item>
		<title>N-dim Array Broadcasting Efficiency in ILNumerics 4.10</title>
		<link>https://ilnumerics.net/blog/n-dim-array-broadcasting-efficiency-in-ilnumerics-4-10/</link>
		<comments>https://ilnumerics.net/blog/n-dim-array-broadcasting-efficiency-in-ilnumerics-4-10/#comments</comments>
		<pubDate>Sun, 01 May 2016 09:47:13 +0000</pubDate>
		<dc:creator><![CDATA[haymo]]></dc:creator>
				<category><![CDATA[ILNumerics]]></category>
		<category><![CDATA[Scientific Computing]]></category>
		<category><![CDATA[Usage]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[Broadcasting]]></category>
		<category><![CDATA[computing]]></category>
		<category><![CDATA[vectorization]]></category>

		<guid isPermaLink="false">http://ilnumerics.net/blog/?p=915</guid>
		<description><![CDATA[<p>Next to other great improvements in version 4.10 of ILNumerics Ultimate VS, it is especially one new feature which requires some attention: general broadcasting for n-dimensional ILArrays. Broadcasting as a concept today is found in many popular mathematical prototyping systems. The most direct correspondence probably exists in the numpy package. Matlab and Octave offer similar &#8230; <a href="https://ilnumerics.net/blog/n-dim-array-broadcasting-efficiency-in-ilnumerics-4-10/" class="more-link">Continue reading <span class="screen-reader-text">N-dim Array Broadcasting Efficiency in ILNumerics 4.10</span> <span class="meta-nav">&#8594;</span></a></p>
<p>The post <a rel="nofollow" href="https://ilnumerics.net/blog/n-dim-array-broadcasting-efficiency-in-ilnumerics-4-10/">N-dim Array Broadcasting Efficiency in ILNumerics 4.10</a> appeared first on <a rel="nofollow" href="https://ilnumerics.net/blog">The ILNumerics Blog</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>Next to other great improvements in version 4.10 of ILNumerics Ultimate VS, it is especially one new feature which requires some attention: general broadcasting for n-dimensional ILArrays.</p>
<p>Broadcasting as a concept today is found in many popular mathematical prototyping systems. The most direct correspondence probably exists in the <a href="http://docs.scipy.org/doc/numpy-1.10.1/user/basics.broadcasting.html">numpy </a>package. Matlab and Octave offer similar functionality by means of the <a href="http://de.mathworks.com/help/matlab/ref/bsxfun.html">bsxfun function</a>.</p>
<p>The term &#8216;broadcasting&#8217; refers to a binary operator which is able to apply an elementwise operation to the elements of two n-dimensional arrays. The &#8216;operation&#8217; is often as simple as a straight addition (ILMath.add, ILMath.divide, a.s.o.). However, what is special about broadcasting is that it allows the operation even for the case where both arrays involved do not have the same number of elements.</p>
<h2>Broadcasting in ILNumerics prior Version 4.10</h2>
<p>In ILNumerics, broadcasting is available for long already. But prior version 4.10 it was limited to scalars operating on n-dim arrays and vectors operating on matrices. Therefore, we had used the term &#8216;vector expansion&#8217; instead of broadcasting. Obviously, broadcasting can be seen as a generalization of vector expansion.</p>
<p>Let&#8217;s visualize the concept by considering the following matrix A:</p>
<pre class="brush: csharp; title: ; notranslate">
A

1  5   9  13  17
2  6  10  14  18
3  7  11  15  19
4  8  12  16  20
</pre>
<p>Matrix A might represent 5 data points of a 4 dimensional dataset as columns. One common requirement is to apply a certain operation to all datapoints in a similar way. In order to, let&#8217;s say, scale/weight the components of each dimension by a certain factor, one would multiply each datapoint with a vector of length 4.</p>
<pre class="brush: csharp; title: ; notranslate">

ILArray&lt;double&gt; V = new[] { 0.5, 3.0, 0.5, 1.0 };

0.5
3.0
0.5
1.0
</pre>
<p>The traditional way of implementing this operation would be to expand the scaling vector by replicating it from a single column to a matrix matching the size of A.</p>
<pre class="brush: csharp; title: ; notranslate">
VExp = repmat(V, 1, 5); 

0.5  0.5  0.5  0.5  0.5
3.0  3.0  3.0  3.0  3.0
0.5  0.5  0.5  0.5  0.5
1.0  1.0  1.0  1.0  1.0
</pre>
<p>Afterwards, the result can be operated with A elementwise in the common way.</p>
<pre class="brush: csharp; title: ; notranslate">
ILArray&lt;double&gt; Result = VExp * A;

0.5   2.5   4.5   6.5   8.5
6.0  18.0  30.0  42.0  54.0
1.5   3.5   5.5   7.5   9.5
4.0   8.0  12.0  16.0  20.0
</pre>
<p>The problem with the above approach is that the vector data need to be expanded first. There is little advantage in doing so: a lot of new memory is being used up in order to store completely redundant data. We all know that memory is the biggest bottleneck today. We should prevent from lots of memory allocations whenever possible. This is where vector expansion comes into play. In ILNumerics, for long, one can prevent from the additional replication step and operate the vector on the matrix directly. Internally, the operation is implemented in a very efficient way, without replicating any data, without allocating new memory.</p>
<pre class="brush: csharp; title: ; notranslate">
ILArray&lt;double&gt; Result = V * A;

0.5   2.5   4.5   6.5   8.5
6.0  18.0  30.0  42.0  54.0
1.5   3.5   5.5   7.5   9.5
4.0   8.0  12.0  16.0  20.0</pre>
<h2>Generalizing for n-Dimensions</h2>
<p>Representing data as matrices is very popular in scientific computing. However, if the data are stored into arrays of other shapes, having more than two dimensions, one had to fall back to repmatting in order for the binary operation to succeed. This nuissance has been removed in version 4.10.</p>
<p>Now it is possible to apply broadcasting to two arrays of <em>any matching</em> shape &#8211; without the need for using repmat. In order for two arrays to &#8216;<em>match</em>&#8216; in the binary operation, the following rules must be fullfilled:</p>
<ol>
<li>All corresponding dimensions of both arrays must match.</li>
<li>In order for two  corresponding dimensions to match,
<ul>
<li>both dimensions must be of the same length, or</li>
<li>one of the dimensions must be of length 1.</li>
</ul>
</li>
</ol>
<p>An example of two matching arrays would be a vector running along the 3rd dimension and a 3 dimensional array:</p>
<p><a href="http://ilnumerics.net/blog/wp-content/uploads/2016/12/3D-Cubes-4-Broadcasting2.png"><img class="aligncenter size-full wp-image-929" src="http://ilnumerics.net/blog/wp-content/uploads/2016/12/3D-Cubes-4-Broadcasting2.png" alt="3D Cubes 4 Broadcasting2" width="655" height="485" /></a>In the above image the vector (green) has the same length as the corresponding dimension of the 3D array (gray). The size of the vector is [1 x 1 x 6]. The size of the 3D array is [4 x 5 x 6]. Hence, any dimension of both, the vector and the 3D array &#8216;match&#8217; in terms of broadcasting. A broadcasting operation for both, the vector and the array would give the same result as if the vector would be replicated along the 1st and the 2nd dimensions. The first element will serve all elements in the first 4 x 5 slice in the 1-2 plane. This slice is marked red in the next image: <a href="http://ilnumerics.net/blog/wp-content/uploads/2016/12/3D-Cubes-4-Broadcasting_slice1.png"><img class="aligncenter size-full wp-image-931" src="http://ilnumerics.net/blog/wp-content/uploads/2016/12/3D-Cubes-4-Broadcasting_slice1.png" alt="3D Cubes 4 Broadcasting_slice" width="812" height="633" /></a>Note that all red elements here derive from the same value &#8211; from the first element of the green vector.  The same is true for all other vector elements: they fill corresponding slices on the 3D array along the 3rd dimension.</p>
<p>Slowly, a huge performance advantage of broadcasting becomes clear: the amount of memory saved explodes when more, longer dimensions are involved.</p>
<h2> Special Case: Broadcasting on Vectors</h2>
<p>In the most general case and if broadcasting is blindly applied, the following special case potentially causes issues. Consider two vectors, one row vector and one column vector being provided as input parameters to a binary operation. In ILNumerics, every array carries at least two dimensions. A column vector of length 4 is considered an array of size [4 x 1]. A row vector of length 5 is considered an array of size [1 x 5]. In fact, <em>any</em> two vectors match according to the general  broadcasting rules.</p>
<p>As a consequence operating a row vector [1 x 5] with a column vector [4 x 1] results in a matrix [4 x 5]. The row vector is getting &#8216;replicated&#8217; (again, without really executing the replication) four times along the 1st dimension, and the column vector 5 times along the rows.</p>
<pre class="brush: csharp; title: ; notranslate">
array(new[] {1.0,2.0,3.0,4.0,5.0}, 1, 5) +array(new[] {1.0,2.0,3.0,4.0}, 4, 1)

&lt;Double&gt; [4,5]
[0]:          2          3          4          5          6
[1]:          3          4          5          6          7
[2]:          4          5          6          7          8
[3]:          5          6          7          8          9
</pre>
<p>Note, in order for the above code example to work, one needs to apply a certain switch:</p>
<pre class="brush: csharp; title: ; notranslate">
Settings.BroadcastCompatibilityMode = false;
</pre>
<p>The reason is that in the traditional version of ILNumerics (just like in Matlab and Octave) the above code would simply not execute but throw an exception instead. Originally, binary operations on vectors would ignore the fact that vectors are matrices and only take the length of the vectors into account, operating on the corresponding elements if the length of both vectors do match. Now, in order to keep compatibility for existing applications, we kept the former behavior.</p>
<p>The new switch &#8216;Settings.BroadcastCompatibilityMode&#8217; by default is set to &#8216;true&#8217;. This will cause the Computing Engine to throw an exception when two vectors of inequal length are provided to binary operators. Applying vectors of the same length (regardless of their orientation) will result in a vector of the same length.</p>
<p>If the &#8216;Settings.BroadcastCompatibilityMode&#8217; switch is set to &#8216;false&#8217; then general broadcasting is applied in all cases according to the above rules &#8211; even on vectors. For the earlier vector example this leads to the resulting matrix as shown above: operating a row on a column vector expands both vectors and gives a matrix of corresponding size.</p>
<p>Further reading: <a href="http://ilnumerics.net/Opoverload.html">binary operators, online documentation</a></p>
<p>The post <a rel="nofollow" href="https://ilnumerics.net/blog/n-dim-array-broadcasting-efficiency-in-ilnumerics-4-10/">N-dim Array Broadcasting Efficiency in ILNumerics 4.10</a> appeared first on <a rel="nofollow" href="https://ilnumerics.net/blog">The ILNumerics Blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>https://ilnumerics.net/blog/n-dim-array-broadcasting-efficiency-in-ilnumerics-4-10/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Intel vectorizes for OpenCL with LLVM</title>
		<link>https://ilnumerics.net/blog/intels-opencl-compiler-vectorizes/</link>
		<comments>https://ilnumerics.net/blog/intels-opencl-compiler-vectorizes/#comments</comments>
		<pubDate>Wed, 15 Feb 2012 16:58:31 +0000</pubDate>
		<dc:creator><![CDATA[haymo]]></dc:creator>
				<category><![CDATA[Comparison]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[computing]]></category>
		<category><![CDATA[Intel]]></category>
		<category><![CDATA[llvm]]></category>
		<category><![CDATA[parallel]]></category>
		<category><![CDATA[vectorization]]></category>

		<guid isPermaLink="false">http://ilnumerics.net/blog/?p=111</guid>
		<description><![CDATA[<p>OpenCL support in ILNumerics is one of the most challenging items on our (internal) wishlist, still. The current OpenCL SDK from Intel does auto vectorization[1][2] and is based on the LLVM compiler suite. A pretty interesting approach. I must take a look at it&#8230;</p>
<p>The post <a rel="nofollow" href="https://ilnumerics.net/blog/intels-opencl-compiler-vectorizes/">Intel vectorizes for OpenCL with LLVM</a> appeared first on <a rel="nofollow" href="https://ilnumerics.net/blog">The ILNumerics Blog</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>OpenCL support in ILNumerics is one of the most challenging items on our (internal) wishlist, still. The current <a href="http://software.intel.com/en-us/articles/vcsource-tools-opencl-sdk/" target="_blank">OpenCL SDK from Intel</a> does auto vectorization[<a href="http://blog.llvm.org/2011/12/llvm-31-vector-changes.html#more" target="_blank">1</a>][<a href="http://software.intel.com/en-us/articles/auto-vectorization-of-opencl-code-with-the-intel-opencl-sdk/" target="_blank">2</a>] and is based on the LLVM compiler suite. A pretty interesting approach. I must take a look at it&#8230;</p>
<p>The post <a rel="nofollow" href="https://ilnumerics.net/blog/intels-opencl-compiler-vectorizes/">Intel vectorizes for OpenCL with LLVM</a> appeared first on <a rel="nofollow" href="https://ilnumerics.net/blog">The ILNumerics Blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>https://ilnumerics.net/blog/intels-opencl-compiler-vectorizes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
