6.6.2.1 : Things to verify before vectorizing

Data Contiguousness

The CPU has to read several elements at the same time.

Data contiguousness :
nothing

Illustration of data contiguousness.

Data Alignement

Data alignement :
nothing

Illustration of data alignement.

Let's do an other file (main_vecrization.cpp) to be able to compare its performances with the previous version.

First, we have to include the appropriate files :

1
2
3
4
#include <iostream>
#include "asterics_hpc.h"

using namespace std;

The __restrict__ keyword

Specify to the compiler there is no overhead between pointers. Otherwise, its will not vectorize your code.
nothing

Illustration of __restrict__ meaning.

The hadamard_product becomes :
1
2
3
void hadamard_product(float* __restrict__ ptabResult, const float* __restrict__ ptabX, const float* __restrict__ ptabY, long unsigned int nbElement){

}

The __builtin_assume_aligned function

Specify to the compiler pointers are aligned
	If this is not true, you will get a Segmentation Fault
The hadamard_product becomes :
1
2
3
4
5
6
7
8
9
void hadamard_product(float* __restrict__ ptabResult, const float* __restrict__ ptabX, const float* __restrict__ ptabY, long unsigned int nbElement){
	const float* tabX = (const float*)__builtin_assume_aligned(ptabX, VECTOR_ALIGNEMENT);
	const float* tabY = (const float*)__builtin_assume_aligned(ptabY, VECTOR_ALIGNEMENT);
	float* tabResult = (float*)__builtin_assume_aligned(ptabResult, VECTOR_ALIGNEMENT);
	
	for(long unsigned int i(0lu); i < nbElement; ++i){
		tabResult[i] = tabX[i]*tabY[i];
	}
}
We have to define the VECTOR_ALIGNEMENT macro. Let's modify the ExampleOptimisation/CMakeLists.txt file :
1
2
3
4
5
6
7
8
9
10
11
12
project(HPC_ASTERICS)
cmake_minimum_required(VERSION 3.0)

add_subdirectory(Performances)
include(runExample.cmake)

set(VECTOR_ALIGNEMENT 32)
add_definitions(-DVECTOR_ALIGNEMENT=${VECTOR_ALIGNEMENT})

add_subdirectory(AstericsHPC)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/AstericsHPC)
add_subdirectory(1-HadamardProduct)

The test function

We have to modify the test function to have aligned pointers :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void evaluateHadamardProduct(long unsigned int nbElement, long unsigned int nbRepetition){
	float * tabResult = (float*)asterics_malloc(sizeof(float)*nbElement);
	float * tabX = (float*)asterics_malloc(sizeof(float)*nbElement);
	float * tabY = (float*)asterics_malloc(sizeof(float)*nbElement);
	
	for(long unsigned int i(0lu); i < nbElement; ++i){
		tabX[i] = (float)(i*32lu%17lu);
		tabY[i] = (float)(i*57lu%31lu);
	}
	
	long unsigned int beginTime(rdtsc());
	for(long unsigned int i(0lu); i < nbRepetition; ++i){
		hadamard_product(tabResult, tabX, tabY, nbElement);
	}
	long unsigned int elapsedTime((double)(rdtsc() - beginTime)/((double)nbRepetition));
	
	double cyclePerElement(((double)elapsedTime)/((double)nbElement));
	cout << "evaluateHadamardProduct : nbElement = "<<nbElement<<", cyclePerElement = " << cyclePerElement << " cy/el, elapsedTime = " << elapsedTime << " cy" << endl;
	cerr << nbElement << "\t" << cyclePerElement << "\t" << elapsedTime << endl;
	asterics_free(tabResult);
	asterics_free(tabX);
	asterics_free(tabY);
}

The main function

The main function is quite the same as before :
1
2
3
4
5
6
7
8
9
10
11
12
13
int main(int argc, char** argv){
	cout << "Hadamard product vectorized" << endl;
	evaluateHadamardProduct(1000lu, 1000000lu);
	evaluateHadamardProduct(1500lu, 1000000lu);
	evaluateHadamardProduct(2000lu, 1000000lu);
	evaluateHadamardProduct(2500lu, 1000000lu);
	evaluateHadamardProduct(2666lu, 1000000lu);
	evaluateHadamardProduct(3000lu, 1000000lu);
	evaluateHadamardProduct(4000lu, 1000000lu);
	evaluateHadamardProduct(5000lu, 1000000lu);
	evaluateHadamardProduct(10000lu, 1000000lu);
	return 0;
}