User and Reference Guide for the Intel® C++ Compiler 14.0

Programming Example

This topic only applies to Intel® Many Integrated Core Architecture (Intel® MIC Architecture).

This sample program, sample.cpp, uses the F32vec16 class to average the elements of an 80 element floating point array.

To generate the object file a.out, which only runs on Intel® MIC Architecture, compile the following code with the following command:

icpc -mmic sample.cpp


// Include Vector Class Definitions 

#include <micvec.h> 
#include <stdio.h> 
 
#define SIZE 80 
 
// Global variables 
float result; 
__declspec(align(64)) float array[SIZE]; 
 
//***************************************************** 
// Function: Add80ArrayElements 
// Add all the elements of a 80 element array 
//***************************************************** 
 
void Add80ArrayElements (F32vec16 *array, float *result) 
{ 
   F32vec16 vec0, vec1; 
  vec0 = _mm512_load_ps((char*)array); // Load array's first 16 floats 
 
   //***************************************************** 
   // Add all elements of the array, 16 elements at a time 
   //****************************************************** 
 
   vec0 += array[1]; // Add elements 16-31 
   vec0 += array[2]; // Add elements 32-47 
   vec0 += array[3]; // Add elements 48-63 
   vec0 += array[4]; // Add elements 64-79 
 
   //***************************************************** 
   // Add all elements in resulting vector 
   //****************************************************** 
 
    *result = _mm512_reduce_add_ps(vec0);
} 
 
int main(int argc, char *argv[]) 
{ 
   int i; 
   // Initialize the array 
   for (i=0; i < SIZE; i++) { 
      array[i] = (float) i; 
   } 
 
   // Call function to add all array elements 
   Add80ArrayElements ((F32vec16*)array, &result); 
 
   // Print average array element value 
   printf ("Average of all array values = %f\n", result/80.); 
   printf ("The correct answer is %f\n\n\n", 39.5);
   return 0;
}


Submit feedback on this help topic