Intel® C++ Compiler 16.0 User and Reference Guide

Programming Example

This topic only applies to Intel® 64 architecture targeting the Intel® Xeon Phi™ coprocessor x100 product family (formerly code name Knights Corner).

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) 
{ 

   //****************************************************
   // Load array's first 16 floats 
 
   //***************************************************** 
      F32vec16 vec0((float*)array);

   //***************************************************** 
   // 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 = vec0.reduce_add();
} 
 
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;
}