{"id":24131,"date":"2019-04-19T13:10:35","date_gmt":"2019-04-19T13:10:35","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cppblog\/?p=24131"},"modified":"2019-04-19T17:27:30","modified_gmt":"2019-04-19T17:27:30","slug":"accelerating-compute-intensive-workloads-with-intel-avx-512","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/accelerating-compute-intensive-workloads-with-intel-avx-512\/","title":{"rendered":"Accelerating Compute-Intensive Workloads with Intel\u00ae AVX-512"},"content":{"rendered":"<p><i>This guest post was authored by <\/i><i>Junfeng Dong, John Morgan, and Li Tian from Intel Corporation<a id=\"LPlnk487304\" name=\"_MailEndCompose\"><\/a>.<\/i><\/p>\n<h2>Introduction<\/h2>\n<p>Last year we introduced Intel\u00ae Advanced Vector Extensions 512 (Intel\u00ae AVX-512) support in Microsoft* Visual Studio* 2017 through <a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/microsoft-visual-studio-2017-supports-intel-avx-512\/\">this VC++ blog<\/a> post. In this follow-on post, we cover some examples to give you a taste of how Intel\u00ae AVX-512 provides performance benefits. These examples include calculating the average of an array, matrix vector multiplication, and the calculation of the Mandelbrot set. Microsoft Visual Studio 2017 version 15.5 or later (we recommend <a href=\"https:\/\/visualstudio.microsoft.com\/downloads\/\">the latest<\/a>) is required to compile these demos, and a computer with an Intel\u00ae processor which supports the Intel\u00ae AVX-512 instruction set is required to run them. See our<a href=\"https:\/\/ark.intel.com\/content\/www\/us\/en\/ark\/search\/featurefilter.html?productType=873&amp;1_Filter-InstructionSetExtensions=3533\"> ARK web site<\/a> for a list of the processors.<\/p>\n<h2>Test System Configuration<\/h2>\n<p>Here is the hardware and software configuration we used for compiling and running these examples:<\/p>\n<ul>\n<li>Processor: Intel\u00ae Core\u2122 i9 7980XE CPU @ 2.60GHz, 18 cores\/36 threads<\/li>\n<li>Memory: 64GB<\/li>\n<li>OS: Windows 10 Enterprise version 10.0.17134.112<\/li>\n<li>Power option in Windows: Balanced<\/li>\n<li>Microsoft Visual Studio 2017 version 15.8.3<\/li>\n<\/ul>\n<p>Note: The performance of any modern processor depends on many factors, including the amount and type of memory and storage, the graphics and display system, the particular processor configuration, and how effectively the program takes advantage of it all. As such, the results we obtained may not exactly match results on other configurations.<\/p>\n<h2><a id=\"post-24131-_Toc514848984\"><\/a>Sample Walkthrough: Array Average<\/h2>\n<p>Let\u2019s walk through a simple example of how to write some code using Intel\u00ae AVX-512 intrinsic functions. As the name implies, Intel\u00ae Advanced Vector Extensions (Intel\u00ae AVX) and Intel\u00ae AVX-512 instructions are designed to enhance calculations using <em>computational vectors<\/em>, which contain several elements of the same data type. We will use these AVX and AVX-512 intrinsic functions to speed up the calculation of the average of an array of floating point numbers.<\/p>\n<p>First we start with a function that calculates the average of array \u201ca\u201d using scalar (non-vector) operations:<\/p>\n<pre class=\"lang:default decode:true\">static const int length = 1024*8;\r\nstatic float a[length];\r\nfloat scalarAverage() {\r\n  float sum = 0.0;\r\n  for (uint32_t j = 0; j &lt; _countof(a); ++j) {\r\n    sum += a[j];\r\n  }\r\n  return sum \/ _countof(a);\r\n<\/pre>\n<p>This routine sums all the elements of the array and then divides it by the number of elements. To \u201cvectorize\u201d this calculation we break the array into groups of elements or \u201cvectors\u201d that we can calculate simultaneously. For Intel\u00ae AVX we can fit 8 <em>float<\/em> elements in each vector, as each float is 32 bits and the vector is 256 bits, so 256\/32 = 8 <em>float<\/em> elements. We sum all the groups into a vector of partial sums, and then add the elements of that vector to get the final sum. For simplicity, we assume that the number of elements is a multiple of 8 or 16 for AVX and AVX-512, respectively, in the sample code shown below. If the number of total elements doesn\u2019t cleanly fit into these vectors, you would have to write a special case to handle the remainder.<\/p>\n<p>Here is the new function:<\/p>\n<pre class=\"lang:default decode:true\">float avxAverage () {\r\n  __m256 sumx8 = _mm256_setzero_ps();\r\n  for (uint32_t j = 0; j &lt; _countof(a); j = j + 8) {\r\n    sumx8 = _mm256_add_ps(sumx8, _mm256_loadu_ps(&amp;(a[j])));\r\n  }\r\n  float sum = sumx8.m256_f32[0] + sumx8.m256_f32[1] +\r\n  sumx8.m256_f32[2] + sumx8.m256_f32[3] +\r\n  sumx8.m256_f32[4] + sumx8.m256_f32[5] +\r\n  sumx8.m256_f32[6] + sumx8.m256_f32[7];\r\n  return sum \/ _countof(a);\r\n}<\/pre>\n<p>Here, <a href=\"https:\/\/software.intel.com\/sites\/landingpage\/IntrinsicsGuide\/#text=_mm256_setzero_ps&amp;expand=133,4529\"><em>_mm256_setzero_ps<\/em><\/a> creates a vector with eight zero values, which is assigned to sumx8. Then, for each set of eight contiguous elements from the array, <a href=\"https:\/\/software.intel.com\/sites\/landingpage\/IntrinsicsGuide\/#text=_mm256_loadu_ps&amp;expand=3377\"><em>_mm256_loadu_ps<\/em><\/a> loads them into a 256-bit vector which <a href=\"https:\/\/software.intel.com\/sites\/landingpage\/IntrinsicsGuide\/#text=_mm256_add_ps&amp;expand=130\"><em>_mm256_add_ps<\/em><\/a> adds to the corresponding elements in sumx8, making sumx8 a vector of eight subtotals. Finally, these subtotals are added to create the total number.<\/p>\n<p>Compared to the scalar implementation, this single instruction, multiple data (SIMD) implementation executes fewer add instructions. For an array with <em>n<\/em> elements, a scalar implementation will execute <em>n<\/em> add instructions, but using Intel\u00ae AVX only (<em>n<\/em>\/8 + 7) add instructions are needed. As a result, Intel\u00ae AVX can potentially be up to 8X faster than the scalar implementation.<\/p>\n<p>Similarly, here is the code for the Intel\u00ae AVX-512 version:<\/p>\n<pre class=\"lang:default decode:true\">float avx512AverageKernel() {\r\n  __m512 sumx16 = _mm512_setzero_ps();\r\n  for (uint32_t j = 0; j &lt; _countof(a); j = j + 16) {\r\n    sumx16 = _mm512_add_ps(sumx16, _mm512_loadu_ps(&amp;(a[j])));\r\n  }\r\n  float sum = _mm512_reduce_add_ps (sumx16);\r\n  return sum \/ _countof(a);\r\n<\/pre>\n<p>As you can see, this version is almost identical to the previous function except for the way the final sum is calculated. With 16 elements in the vector, we would need 16 array references and 15 additions to calculate the final sum if we were to do it the same way as the AVX version. Fortunately, AVX-512 provides the <a href=\"https:\/\/software.intel.com\/sites\/landingpage\/IntrinsicsGuide\/#text=_mm512_reduce_add_ps%20&amp;expand=133\"><em>_mm512_reduce_add_ps<\/em><\/a> intrinsic function to generate the same result, which makes the code much easier to read. This function adds the first eight elements to the rest, then adds the first four of that vector to the rest, then two and finally sums those to get the total with just four addition instructions. Using Intel\u00ae AVX-512 to find the average of an array with <em>n<\/em> elements requires the execution of (<em>n<\/em>\/16+4) addition instructions which is about half of what was needed for Intel\u00ae AVX when <em>n<\/em> is large. As a result, Intel\u00ae AVX-512 can potentially be up to 2x faster than Intel\u00ae AVX.<\/p>\n<p>For this example, we used only a few of the most basic Intel\u00ae AVX-512 intrinsic functions, but there are hundreds of vector intrinsic functions available which perform various operations on a selection of different data types in 128-bit, 256-bit, and 512-bit vectors with options such as masking and rounding. These functions may use instructions from various Intel\u00ae instruction set extensions, including Intel\u00ae AVX-512. For example, the intrinsic function<a href=\"https:\/\/software.intel.com\/sites\/landingpage\/IntrinsicsGuide\/#text=_mm512_add_ps&amp;expand=133\"><em>_mm512_add_ps()<\/em><\/a> is implemented using the Intel\u00ae AVX-512 <em>vaddps <\/em>instruction. You can use the <a href=\"https:\/\/software.intel.com\/en-us\/articles\/intel-sdm\">Intel Software Developer Manuals<\/a> to learn more about Intel\u00ae AVX-512 instructions, and the <a href=\"https:\/\/software.intel.com\/sites\/landingpage\/IntrinsicsGuide\/\">Intel Intrinsics Guide<\/a> to find particular intrinsic functions. Click on a function entry in the <a href=\"https:\/\/software.intel.com\/sites\/landingpage\/IntrinsicsGuide\/\">Intrinsics Guide<\/a> and it will expand to show more details, such as Synopsis, Description, and Operation.<\/p>\n<p>These functions are declared using the <strong>immintrin.h<\/strong> header. Microsoft also offers the <strong>intrin.h<\/strong> header, which declares almost all Microsoft Visual C++* (MSVC) intrinsic functions, including the ones from immintrin.h. You can include either of these headers in your source file.<\/p>\n<h2>Sample Walkthrough: Matrix Vector Multiplication<\/h2>\n<p><img decoding=\"async\" width=\"513\" height=\"135\" class=\"wp-image-24132\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/04\/word-image.png\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/04\/word-image.png 513w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/04\/word-image-300x79.png 300w\" sizes=\"(max-width: 513px) 100vw, 513px\" \/> Mathematical vector and matrix arithmetic involves lots of multiplications and additions. For example, here is a simple multiplication of a matrix and a vector:<\/p>\n<p>A computer can do this simple calculation almost instantly but multiplying very large vectors and matrices can take hundreds or thousands of multiplications and additions like this. Let\u2019s see how we can \u201cvectorize\u201d those computations to make them faster. Let\u2019s start with a scalar function which multiplies matrix <em>t1<\/em> with vector <em>t2<\/em>:<\/p>\n<pre class=\"lang:default decode:true \">static float *out;\r\nstatic const int row = 16;\r\nstatic const int col = 4096;\r\nstatic float *scalarMultiply()\r\n{\r\n  for (uint64_t i = 0; i &lt; row; i++)\r\n  {\r\n    float sum = 0;\r\n    for (uint64_t j = 0; j &lt; col; j++)\r\n      sum = sum + t1[i * col + j] * t2[j];\r\n    out[i] = sum;\r\n  }\r\n  return out;\r\n}<\/pre>\n<p>As with the previous example, we \u201cvectorize\u201d this calculation by breaking each row into vectors. For AVX-512, because each <em>float<\/em> is 32 bits and a vector is 512 bits, a vector can have 512\/32 = 16 <em>float<\/em> elements. Note that this is a <em>computational <\/em>vector, which is different from the <em>mathematical <\/em>vector that is being multiplied. For each row in the matrix we load 16 columns as well as the corresponding 16 elements from the vector, multiply them together, and add the products to a 16-element accumulator. When the row is complete, we can sum the accumulator elements to get an element of the result. Note that we can do this with Intel\u00ae AVX or Intel\u00ae Streaming SIMD Extensions (Intel\u00ae SSE) as well, and the maximum vector sizes with those extensions are 8 (256\/32) and 4 (128\/32) elements respectively.<\/p>\n<p>A version of the multiply routine using Intel\u00ae AVX-512 intrinsic functions is shown here:<\/p>\n<pre class=\"lang:default decode:true \">static float *outx16;\r\nstatic float *avx512Multiply()\r\n{\r\n  for (uint64_t i = 0; i &lt; row; i++)\r\n  {\r\n    __m512 sumx16 = _mm512_set1_ps(0.0);\r\n    for (uint64_t j = 0; j &lt; col; j += 16)\r\n    {\r\n      __m512 a = _mm512_loadu_ps(&amp;(t1[i * col + j]));\r\n      __m512 b = _mm512_loadu_ps(&amp;(t2[j]));\r\n      sumx16 = _mm512_fmadd_ps(a, b, sumx16);\r\n    }\r\n    outx16[i] = _mm512_reduce_add_ps(sum16);\r\n  }\r\n  return outx16;\r\n}<\/pre>\n<p>You can see that many of the scalar expressions have been replaced by intrinsic function calls that perform the same operation on a vector.<\/p>\n<p>We replace the initialization of <em>sum<\/em> to zero with a call of the <a href=\"https:\/\/software.intel.com\/sites\/landingpage\/IntrinsicsGuide\/#text=_mm512_set1_ps&amp;expand=4947\"><em>_mm512_set1_ps()<\/em><\/a> function that creates a vector with 16 zero elements and assign that to <em>sumx16<\/em>. Inside the inner loop we load 16 elements of <em>t1<\/em> and <em>t2<\/em> into vector variables <em>a<\/em> and <em>b<\/em> respectively using <a href=\"https:\/\/software.intel.com\/sites\/landingpage\/IntrinsicsGuide\/#text=_mm512_loadu_ps&amp;expand=3380\"><em>_mm512_loadu_ps()<\/em><\/a>. The <a href=\"https:\/\/software.intel.com\/sites\/landingpage\/IntrinsicsGuide\/#text=_mm512_fmadd_ps&amp;expand=2524\"><em>_mm512_fmadd_ps()<\/em><\/a> function adds the product of each element in <em>a<\/em> and <em>b<\/em> to the corresponding elements in <em>sumx16<\/em>.<\/p>\n<p>At the end of the inner loop we have 16 partial sums in <em>sumx16<\/em> rather than a single sum. To calculate the final result we must add these 16 elements together using the <a href=\"https:\/\/software.intel.com\/sites\/landingpage\/IntrinsicsGuide\/#text=_mm512_reduce_add_ps&amp;expand=4529\"><em>_mm512_reduce_add_ps<\/em><\/a><em>()<\/em> function that we used in the array average example.<\/p>\n<h2>Floating-Point vs. Real Numbers<\/h2>\n<p>At this point we should note that this vectorized version does not calculate <em>exactly<\/em> the same thing as the scalar version. If we were doing all of this computation using mathematical <em>real numbers<\/em> it wouldn\u2019t matter what order we add the partial products, but that isn\u2019t true of <em>floating-point<\/em> values. When floating-point values are added, the precise result may not be representable as a floating-point value. In that case the result must be rounded to one of the two closest values that can be represented. The difference between the precise result and the representable result is the <em>rounding error<\/em>.<\/p>\n<p>When computing the sum of products like this the calculated result will differ from the precise result by the sum of all the rounding errors. Because the vectorized matrix multiply adds the partial products in a different order from the scalar version the rounding errors for each addition can also be different. Furthermore, the <a href=\"https:\/\/software.intel.com\/sites\/landingpage\/IntrinsicsGuide\/#text=_mm512_fmadd_ps&amp;expand=2524\"><em>_mm512_fmadd_ps<\/em><\/a><em>()<\/em> function does not round the partial products before adding them to the partial sums, so only the addition adds a rounding error. If the rounding errors differ between the scalar and vectorized computations the result may also differ. However, this doesn\u2019t mean that either version is wrong. It just shows the peculiarities of floating-point computation.<\/p>\n<h2><a id=\"post-24131-_Toc514848986\"><\/a>Mandelbrot Set<\/h2>\n<p>The <a href=\"https:\/\/en.wikipedia.org\/wiki\/Mandelbrot_set\">Mandelbrot set<\/a> is the set of all complex numbers z for which the sequence defined by the iteration<\/p>\n<p>z(0) = z, z(n+1) = z(n)*z(n) + z, n=0,1,2, &#8230;<\/p>\n<p>remains bounded. This means that there is a number B such that the absolute value of all iterates z(n) never gets larger than B. The calculation of the Mandelbrot set is often used to make colorful images of the points that are <em>not<\/em> in the set where each color indicates how many terms are needed to exceed the bound. It is widely used as a sample to demonstrate vector computation performance. The kernel code of calculating the Mandelbrot set where B is 2.0 is available <a href=\"https:\/\/github.com\/ispc\/ispc\/blob\/master\/examples\/mandelbrot_tasks\/mandelbrot_tasks_serial.cpp\">here<\/a>.<\/p>\n<pre class=\"lang:default decode:true\">static int mandel(float c_re, float c_im, int count)\r\n{\r\n  float z_re = c_re, z_im = c_im;\r\n  int i;\r\n  for (i = 0; i &lt; count; ++i)\r\n  {\r\n    if (z_re * z_re + z_im * z_im &gt; 4.f)\r\n      break;\r\n    float new_re = z_re * z_re - z_im * z_im;\r\n    float new_im = 2.f * z_re * z_im;\r\n    z_re = c_re + new_re;\r\n    z_im = c_im + new_im;\r\n  }\r\n  return i;\r\n}<\/pre>\n<p>Of course, it is impossible to evaluate every term of an infinite series, so this function evaluates no more than the number of terms specified by <em>count<\/em>, and we assume that if the series hasn\u2019t diverged by that point, it isn\u2019t going to. The value returned indicates how many terms did not diverge, and it is typically used to select a color for that point. If the function returns <em>count <\/em>the point is assumed to be in the Mandelbrot set.<\/p>\n<p>We can vectorize this by replacing each scalar operation with a vector equivalent similar to the way we vectorized matrix vector multiplication. But a complication arises with the following source line: \u201cif (z_re * z_re + z_im * z_im &gt; 4.0f) break;\u201d. How do you vectorize a conditional break?<\/p>\n<p>In this instance we know that once the series exceeds the bound all later terms will also exceed it, so we can unconditionally continue calculating all elements until all of them have exceeded the bound or we have reached the iteration limit. We can handle the condition by using a vector comparison to mask the elements that have exceeded the bound and use that to update the results for the remaining elements. Here is the code for a version using Intel\u00ae Advanced Vector Extensions 2 (Intel\u00ae AVX2) functions.<\/p>\n<pre class=\"lang:default decode:true \">\/* AVX2 Implementation *\/\r\n__m256i avx2Mandel (__m256 c_re8, __m256 c_im8, uint32_t max_iterations) {\r\n  __m256 z_re8 = c_re8;\r\n  __m256 z_im8 = c_im8;\r\n  __m256 four8 = _mm256_set1_ps(4.0f);\r\n  __m256 two8 = _mm256_set1_ps(2.0f);\r\n  __m256i result = _mm256_set1_epi32(0);\r\n  __m256i one8 = _mm256_set1_epi32(1);\r\n  for (auto i = 0; i &lt; max_iterations; i++) {\r\n  __m256 z_im8sq = _mm256_mul_ps(z_im8, z_im8);\r\n  __m256 z_re8sq = _mm256_mul_ps(z_re8, z_re8);\r\n  __m256 new_im8 = _mm256_mul_ps(z_re8, z_im8);\r\n  __m256 z_abs8sq = _mm256_add_ps(z_re8sq, z_im8sq);\r\n  __m256 new_re8 = _mm256_sub_ps(z_re8sq, z_im8sq);\r\n  __m256 mi8 = _mm256_cmp_ps(z_abs8sq, four8, _CMP_LT_OQ);\r\n  z_im8 = _mm256_fmadd_ps(two8, new_im8, c_im8);\r\n  z_re8 = _mm256_add_ps(new_re8, c_re8);\r\n  int mask = _mm256_movemask_ps(mi8);\r\n  __m256i masked1 = _mm256_and_si256(_mm256_castps_si256(mi8), one8);\r\n  if (0 == mask)\r\n    break;\r\n    result = _mm256_add_epi32(result, masked1);\r\n  }\r\nreturn result;\r\n}<\/pre>\n<p>The scalar function returns a value that represents how many iterations were calculated before the result diverged, so a vector function must return a vector with those same values. We compute that by first generating a vector <em>mi8<\/em> that indicates which elements have not exceeded the bound. Each element of this vector is either all zero bits (if the test condition <em>_CMP_LT_OQ<\/em> is not true) or all one bits (if it is true). If that vector is all zero, then everything has diverged, and we can break out of the loop. Otherwise the vector value is reinterpreted as a vector of 32-bit integer values by <em>_mm256_castps_si256<\/em>, and then masked with a vector of 32-bit ones. That leaves us with a one value for every element that has not diverged and zeros for those that have. All that is left is to add that vector to the vector accumulator <em>result<\/em>.<\/p>\n<p>The Intel\u00ae AVX-512 version of this function is similar, with one significant difference. The <em>_mm256_cmp_ps<\/em> function returns a vector value of type <em>__m256<\/em>. You might expect to use a <em>_mm512_cmp_ps<\/em> function that returns a vector of type <em>__m512<\/em>, but that function does not exist. Instead we use the <em>_mm512_cmp_ps_mask<\/em> function that returns a value of type <em>__mmask16<\/em>. This is a 16-bit value, where each bit represents one element of the vector. These values are held in a separate set of eight registers that are used for vectorized conditional execution. Where the Intel\u00ae AVX2 function had to calculate the values to be added to result explicitly, Intel\u00ae AVX-512 allows the mask to be applied directly to the addition with the <em>_mm512_mask_add_epi32<\/em> function.<\/p>\n<pre class=\"lang:default decode:true \">\/* AVX512 Implementation *\/\r\n__m512i avx512Mandel(__m512 c_re16, __m512 c_im16, uint32_t max_iterations) {\r\n  __m512 z_re16 = c_re16;\r\n  __m512 z_im16 = c_im16;\r\n  __m512 four16 = _mm512_set1_ps(4.0f);\r\n  __m512 two16 = _mm512_set1_ps(2.0f);\r\n  __m512i one16 = _mm512_set1_epi32(1);\r\n  __m512i result = _mm512_setzero_si512();\r\n  for (auto i = 0; i &lt; max_iterations; i++) {\r\n    __m512 z_im16sq = _mm512_mul_ps(z_im16, z_im16);\r\n    __m512 z_re16sq = _mm512_mul_ps(z_re16, z_re16);\r\n    __m512 new_im16 = _mm512_mul_ps(z_re16, z_im16);\r\n    __m512 z_abs16sq = _mm512_add_ps(z_re16sq, z_im16sq);\r\n    __m512 new_re16 = _mm512_sub_ps(z_re16sq, z_im16sq);\r\n    __mmask16 mask = _mm512_cmp_ps_mask(z_abs16sq, four16, _CMP_LT_OQ);\r\n    z_im16 = _mm512_fmadd_ps(two16, new_im16, c_im16);\r\n    z_re16 = _mm512_add_ps(new_re16, c_re16);\r\n    if (0 == mask)\r\n      break;\r\n    result = _mm512_mask_add_epi32(result, mask, result, one16);\r\n  }\r\n  return result;\r\n}<\/pre>\n<p><a id=\"post-24131-_Toc514848987\"><\/a> Each of the vectorized Mandelbrot calculations returns a vector instead of a scalar, and the value of each element is the same value that would have been returned by the original scalar function. You may have noticed that the returned value has a different type from the real and imaginary argument vectors. The arguments to the scalar function are type <em>float<\/em>, and the function returns an <em>unsigned integer<\/em>. The arguments to the vectorized functions are vector versions of <em>float<\/em>, and the return value is a vector that can hold 32-bit unsigned integers. If you need to vectorize a function that uses type <em>double<\/em>, there are vector types for holding elements of that type as well: <em>__m128d<\/em>, <em>__m256d<\/em> and <em>__m512d<\/em>. You may be wondering if there are vector types for other integer types such as <em>signed char<\/em> and <em>unsigned short<\/em>. There aren\u2019t. Vectors of type <em>__m128i<\/em>, <em>__m256i<\/em> and <em>__m512i<\/em> are used for all integer elements regardless of size or signedness.<\/p>\n<p>You can also convert or cast (reinterpret) vectors that hold elements of one type into a vector with elements of a different type. In the <em>mandelx8<\/em> function the <em>_mm256_castps_si256 <\/em>function is used to reinterpret the <em>__m256<\/em> result of the comparison function as a <em>__m256i<\/em> integer element mask for updating the <em>result<\/em> vector.<\/p>\n<h2>Performance of Intel\u00ae AVX-512<\/h2>\n<p>We measured the run time of the Mandelbrot, matrix vector multiplication, and array average kernel functions with Intel\u00ae AVX\/AVX2 and Intel\u00ae AVX-512 intrinsic functions to compare the performance. The source code is compiled with \u201c\/O2\u201d. On our test platform, Mandelbrot with Intel\u00ae AVX-512 is 1.77x<sup>1<\/sup> faster than the Intel\u00ae AVX2 version. The sample code is available <a href=\"https:\/\/github.com\/intel\/Developer-Tools-Runtimes-Blogs\/blob\/master\/AVX512_Blog\/MandelbrotImage.cpp\">here<\/a>. Array average (<a href=\"https:\/\/github.com\/intel\/Developer-Tools-Runtimes-Blogs\/blob\/master\/AVX512_Blog\/AverageFloat.cpp\">source code<\/a>) is 1.91x<sup>1<\/sup> faster and matrix vector multiplication (<a href=\"https:\/\/github.com\/intel\/Developer-Tools-Runtimes-Blogs\/blob\/master\/AVX512_Blog\/MatrixMultiplication.cpp\">source code<\/a>) is 1.80x<sup>1<\/sup> faster compared to their AVX2 versions.<\/p>\n<p>We previously stated that the performance achievable by Intel\u00ae AVX-512 should approximately double that of Intel\u00ae AVX2. We see that we don\u2019t quite reach that number, and there are a few reasons why the speedup might not reach the expected value. One is that only the innermost loop of the calculation is sped up by the larger vector instructions, but the total execution time includes time spent executing outer loops and other overhead that did not speed up. Another potential reason is because the bandwidth of the memory system must be shared between all cores doing calculations, and this is a finite resource. When most of that bandwidth is being used, the processor can\u2019t compute faster than the data becomes available.<\/p>\n<h2><a id=\"post-24131-_Toc514848988\"><\/a>Conclusions<\/h2>\n<p>We have presented several examples of how to vectorize array average and matrix vector multiplication as well as shown code for calculating the Mandelbrot set using Intel\u00ae AVX2 and Intel\u00ae AVX-512 functions. This code is a more realistic example of how to use Intel\u00ae AVX-512 than the sample code from our prior post. From data collected on our test platform, the Intel\u00ae AVX-512 code shows performance improvements between 77% and 91% when compared to Intel\u00ae AVX2.<\/p>\n<p>Intel\u00ae AVX-512 fully utilizes Intel\u00ae hardware capabilities to improve performance by doubling the data that can be processed with a single instruction compared to Intel\u00ae AVX2. This capability can be used in artificial intelligence, deep learning, scientific simulations, financial analytics, 3D modeling, image\/audio\/video processing, and data compression. Use Intel\u00ae AVX-512 and unlock your application\u2019s full potential.<\/p>\n<p>No license (express or implied, by estoppel or otherwise) to any intellectual property rights is granted by this document.<\/p>\n<p>Intel disclaims all express and implied warranties, including without limitation, the implied warranties of merchantability, fitness for a particular purpose, and non-infringement, as well as any warranty arising from course of performance, course of dealing, or usage in trade.<\/p>\n<p>This document contains information on products, services and\/or processes in development.\u00a0 All information provided here is subject to change without notice. Contact your Intel representative to obtain the latest forecast, schedule, specifications and roadmaps.<\/p>\n<p>The products and services described may contain defects or errors known as errata which may cause deviations from published specifications. Current characterized errata are available on request.<\/p>\n<p>Copies of documents which have an order number and are referenced in this document may be obtained by calling\u00a01-800-548-4725\u00a0or by visiting <a href=\"https:\/\/www.intel.com\/content\/www\/us\/en\/design\/resource-design-center.html\">https:\/\/www.intel.com\/content\/www\/us\/en\/design\/resource-design-center.html<\/a>.<\/p>\n<p>Intel, the Intel logo, Core are trademarks of Intel Corporation in the U.S. and\/or other countries.<\/p>\n<p>*Other names and brands may be claimed as the property of others<\/p>\n<p>\u00a9 Intel Corporation.<\/p>\n<p>\u00a7 (1) As provided in this document\n\u00a7 Software and workloads used in performance tests may have been optimized for performance only on Intel microprocessors. Performance tests, such as SYSmark and MobileMark, are measured using specific computer systems, components, software, operations and functions. Any change to any of those factors may cause the results to vary. You should consult other information and performance tests to assist you in fully evaluating your contemplated purchases, including the performance of that product when combined with other products.\n\u00a7 Configurations: Ran sample code on an Intel\u00ae desktop powered by Intel\u00ae Core\u2122 i9 7980XE CPU @ 2.60GHz with 64 GB RAM running Windows 10 Enterprise version 10.0.17134.112\n\u00a7 For more information go to\u00a0\u00a0<a href=\"https:\/\/www.intel.com\/content\/www\/us\/en\/benchmarks\/benchmark.html\">https:\/\/www.intel.com\/content\/www\/us\/en\/benchmarks\/benchmark.html<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This guest post was authored by Junfeng Dong, John Morgan, and Li Tian from Intel Corporation. Introduction Last year we introduced Intel\u00ae Advanced Vector Extensions 512 (Intel\u00ae AVX-512) support in Microsoft* Visual Studio* 2017 through this VC++ blog post. In this follow-on post, we cover some examples to give you a taste of how Intel\u00ae [&hellip;]<\/p>\n","protected":false},"author":3136,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[218],"tags":[],"class_list":["post-24131","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-performance"],"acf":[],"blog_post_summary":"<p>This guest post was authored by Junfeng Dong, John Morgan, and Li Tian from Intel Corporation. Introduction Last year we introduced Intel\u00ae Advanced Vector Extensions 512 (Intel\u00ae AVX-512) support in Microsoft* Visual Studio* 2017 through this VC++ blog post. In this follow-on post, we cover some examples to give you a taste of how Intel\u00ae [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/24131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/3136"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=24131"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/24131\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=24131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=24131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=24131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}