HPMC User Guide v 1.00
© 2026 Bassem W. Jamaleddine
When writing programs for the MIC, you do not need to be concerned with its internal architecture. However, it is useful to understand how the vector operations are being performed, in particular to clarify the fact of what makes your HPMC (equipped with the MIC) a superscalar vector processor, in other term: a supercomputer. In this chapter, we will simplify the view of the vector processing over the vector registers: the vector will be schematically presented from a programmer point of view. First, we will go over a simple program, vector512.cpp, that we will disassemble, then draw the vector register by mapping it through four lanes. This allows us to clarify how the vector instructions are being executed on the MIC. Next, we will show various programs that uses the intrinsic operations to exploit the MIC vector registers when performing fast addition and multiplication on data arrays. This subchapter concludes on the subject of data casting, and why mixing data types should be avoided.
■ Vector Operations: vector512.cpp
Consider the following program vector512.cpp that contains various intrinsic operations to manipulate a list of sixteen float data elements saved in a vector.
1. #define MICVEC_DEFINE_OUTPUT_OPERATORS 2. #include <iostream> 3. #include <micvec.h> 4. 5. int main() { 6. _MM_PERM_ENUM p32; 7. _MM_SWIZZLE_ENUM s32; 8. 9. __declspec(align(64)) Is32vec16 inputData(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15); 10. __declspec(align(64)) Is32vec16 outputData; 11. std::cout << "input = " << inputData; 12. 13. // swizzle input data and print 14. std::cout << "\nswizzling with pattern 'cdab' \n" << inputData.cdab(); 15. std::cout << "\nswizzling with pattern 'aaaa' \n" << inputData.aaaa(); 16. std::cout << "\nswizzling with pattern 'bbbb' \n" << inputData.bbbb(); 17. std::cout << "\nswizzling with pattern 'cccc' \n" << inputData.cccc(); 18. std::cout << "\nswizzling with pattern 'dddd' \n" << inputData.dddd(); 19. std::cout << "\nswizzling with pattern 'cdab' // ghef cdab - Swap pairs \n" << inputData.cdab(); 20. std::cout << "\nswizzling with pattern 'dacb' \n" << inputData.dacb(); 21. 22. // shuffle input data 23. std::cout << "\nInter lane shuffle data for pattern 'aaaa' \n"; 24. p32 = _MM_PERM_AAAA; 25. //shuffle intra lane data 26. outputData = Is32vec16(_mm512_shuffle_epi32(__m512i(inputData), p32)); 27. std::cout << outputData << "\n"; 28. 29. // shuffle input data 30. std::cout << "Inter lane shuffle data for pattern 'dada' \n"; 31. p32 = _MM_PERM_DADA; 32. //shuffle intra lane data 33. outputData = Is32vec16(_mm512_shuffle_epi32(__m512i(inputData), p32)); 34. std::cout << outputData << "\n"; 35. 36. // Permute the four 128-bit elements of v2 according to indexes in 'perm'. 37. std::cout << "Inter lane permute data for pattern 'aabc' // Permute the four 128-bit elements \n"; 38. p32 = _MM_PERM_AABC; 39. outputData = Is32vec16(_mm512_permute4f128_epi32(__m512i(inputData), p32)); 40. std::cout << outputData << "\n"; 41. 42. std::cout << "Inter lane permute data for pattern 'dacb' \n"; 43. p32 = _MM_PERM_DACB; 44. //shuffle inter lane data 45. outputData = Is32vec16(_mm512_permute4f128_epi32(__m512i(inputData), p32)); 46. std::cout << outputData << "\n"; 47. 48. std::cout << "Inter lane for register swizzle NONE \n"; 49. s32 = _MM_SWIZ_REG_NONE; 50. outputData = Is32vec16(_mm512_swizzle_epi64(__m512i(inputData), s32)); 51. std::cout << outputData << "\n"; 52. 53. std::cout << "Inter lane for register swizzle for pattern 'dcba' \n"; 54. s32 = _MM_SWIZ_REG_CDAB; 55. outputData = Is32vec16(_mm512_swizzle_epi64(__m512i(inputData), s32)); 56. std::cout << outputData << "\n"; 57. 58. std::cout << "Inter lane for register swizzle for pattern 'cdab' // ghef cdab - Swap pairs \n"; 59. s32 = _MM_SWIZ_REG_CDAB; // /* ghef cdab - Swap pairs */ 60. outputData = Is32vec16(_mm512_swizzle_epi64(__m512i(inputData), s32)); 61. std::cout << outputData << "\n"; 62. 63. std::cout << "Inter lane for register swizzle for pattern 'dacb' CROSS PRODUCT // hegf dacb - cross-product \n"; 64. s32 = _MM_SWIZ_REG_DACB; // /* hegf dacb - cross-product */ 65. outputData = Is32vec16(_mm512_swizzle_epi64(__m512i(inputData), s32)); 66. std::cout << outputData << "\n"; 67. 68. std::cout << "Inter lane for register swizzle for pattern 'badc' // fehg badc - Swap with two-away \n"; 69. s32 = _MM_SWIZ_REG_BADC; // /* fehg badc - Swap with two-away */ 70. outputData = Is32vec16(_mm512_swizzle_epi64(__m512i(inputData), s32)); 71. std::cout << outputData << "\n"; 72. 73. std::cout << "Inter lane for register swizzle for pattern 'aaaa' // eeee aaaa - broadcast a element \n"; 74. s32 = _MM_SWIZ_REG_AAAA; // /* eeee aaaa - broadcast a element */ 75. outputData = Is32vec16(_mm512_swizzle_epi64(__m512i(inputData), s32)); 76. std::cout << outputData << "\n"; 77. }
This section is not included in the SAMPLE PREVIEW.
■ Vector Operations on Vector Registers with Four Lanes
The Intel Xeon Phi MIC has 61 cores, with each core has a VPU, Vector Processing Unit, that provides fine grained data parallelism using 128 entries 512-bit vector registers divided among the threads, thus 32 entries per thread. The VPU can do one load and one operation in the same cycle. Each VPU consists of 8 master ALUS, each containing two SP and one DP ALU with independent pipelines: DP pipeline SP pipeline
Each vector can contain sixteen 32-bit SP integers or floats, or eight 64-bit DP double integer or floats,
Each vector register is 512 bits wide. For explanation, it is simple to consider that each vector is divided into four lanes with each lane being 128 bits (consisting of four 32 bit elements read from right to left), thus LANE 0 has four 32 bit elements a, b, c, d, and so on.
128 bits 128 bits 128 bits 128 bits
32b 32b 32b 32b
| D C B A | D C B A | D C B A | D C B A|
+---------------+---------------+---------------+---------------+
| LANE 3 | LANE 2 | LANE 1 | LANE 0 |
+---------------+---------------+---------------+---------------+
| p o n m | l k j i | h g f e | d c b a |
+---------------+---------------+---------------+---------------+
This section is not included in the SAMPLE PREVIEW.
| {swizzle} Modifier | Function: 4 x 32 bits/4 x 64 Bits |
|---|---|
| No swizzle modifier as default or {dcba} | No swizzle |
| {cdab} | Swap inner pairs |
| {badc} | Swap with two away |
| {dacb} | Cross product swizzle |
| {aaaa} | Broadcast ‘a’ element across 4-element packets |
| {bbbb} | Broadcast ‘b’ element across 4-element packets |
| {cccc} | Broadcast ‘c’ element across 4-element packets |
| {dddd} | Broadcast ‘d’ element across 4-element packets |
■ Intrinsic Vector Operations for Addition
Many intrinsic operations can be performed directly on the 512 bit vectors. The following listing for the program adding512.cpp shows how to use intrinsic operations in a loop to add two arrays using 512 bit vectors.
This section is not included in the SAMPLE PREVIEW.
■ Intrinsic Vector Operations for Summation
Many intrinsic operations can be performed directly on the 512 bit vectors. The following listing for the program sum512.cpp shows how to use intrinsic operations in a loop to sum the data in an array using 512 bit vectors.
This section is not included in the SAMPLE PREVIEW.
■ Intrinsic Vector Operations for Multiplication
The program vecmul512f.cpp uses intrinsic vector operations to do multiplication on vector registers holding float data type.
This section is not included in the SAMPLE PREVIEW.
1. #define MICVEC_DEFINE_OUTPUT_OPERATORS 2. #include <stdio.h> 3. #include <stdlib.h> 4. #include <iostream> 5. #include <micvec.h> 6. 7. unsigned int const SIZEf=64000; 8. 9. int main() { 10. int i; 11. int n = SIZEf; 12. 13. __declspec(aligned(64)) double Af[SIZEf],Bf[SIZEf],Cf[SIZEf]; 14. 15. // initialize arrays 16. for (i=0;i<SIZEf;i++) { 17. Af[i] = 1.0; 18. Bf[i] = i; 19. } 20. // vector registers for double 21. __m512d va; 22. __m512d vb; 23. __m512d vc; 24. // double arrays Af[n] and Bf[n] are aligned on 64 bit boundary, 25. // where n is a multiple of 8 so to read all 8 doubles at once 26. // in each vector 27. for (i=0; i<n; i += 8) { 28. // evict cache line from specified cache first level 29. _mm_prefetch((const char*) (Af+i+8), _MM_HINT_T0); 30. va = _mm512_load_pd(Af+i); 31. // vector instructions 32. vb = _mm512_extload_pd( 33. Bf+i, _MM_UPCONV_PD_NONE, _MM_BROADCAST64_NONE, _MM_HINT_NONE 34. ); 35. vc = _mm512_mul_pd(va, vb); 36. _mm512_store_pd(Cf+i,vc); 37. } 38. for (int i=0; i<10; i++) printf("%d %f \n", i, Cf[i]); 39. }
■ Casting Vectorized Data Type
Type casting should be avoided when iterating or looping over a block of instructions in a program. We will use the previous program vecmul512d.cpp, however, we will modify one of the register type making it a 32 bit float instead of a double float. Here we are required to cast the vector register from float to double before doing the multiplication operation.
The program vecmul512mixCAST.cpp is shown in the following Listing 3.3.6. On line 12 we define two variables Af and Cf of type double float, and on line 13 the variable Bf is of type float. The vector registers for Af and Cf are defined by va and vc shown on line 20 and 23 respectively. Both va and vc have intrinsic type _m512d.
The variable Bf is an array of floats, and its corresponding vector registers are vb and vbd of intrinsic type _m512 and _m512d, shown on lines 21 and 22 respectively. We defined these two vector registers so that we can convert the Bf data type from float to double (using vector instructions). Line 30 shows the cast operation on vb returning vbd.1. #define MICVEC_DEFINE_OUTPUT_OPERATORS 2. #include <stdio.h> 3. #include <stdlib.h> 4. #include <iostream> 5. #include <micvec.h> 6. 7. unsigned int const SIZEf=64000; 8. 9. int main() { 10. int i; 11. int n = SIZEf; 12. __declspec(aligned(64)) double Af[SIZEf],Cf[SIZEf]; 13. __declspec(aligned(64)) float Bf[SIZEf]; 14. 15. // initialize arrays 16. for (i=0;i<SIZEf;i++) { 17. Af[i] = 1.0; 18. Bf[i] = i; 19. } 20. __m512d va; 21. __m512 vb; //__m512d vb; 22. __m512d vbd; 23. __m512d vc; 24. for (i=0; i<n; i += 8) { 25. _mm_prefetch((const char*) (Af+i+8), _MM_HINT_T0); 26. va = _mm512_load_pd(Af+i); 27. __m512 vb = _mm512_extload_ps( 28. Bf+i*2, _MM_UPCONV_PS_FLOAT16, _MM_BROADCAST32_NONE, _MM_HINT_NONE 29. ); 30. vbd = _mm512_castps_pd(vb); // NOTE 31. //vbd = _mm512_cvtpslo_pd(vb); //NOTE 32. vc = _mm512_mul_pd(va, vbd); 33. _mm512_store_pd(Cf+i,vc); 34. } 35. for (int i=0; i<20; i++) printf("%d %f \n", i, Cf[i]); 36. }
For a real cast operation, we need to use the operation _mm512_cvtpslo_pd() as shown in listing vecmul512mixCV.cpp on line 31.
This section is not included in the SAMPLE PREVIEW.