12.1.4.7 : Le main_precompute_modulo.cpp
Le problème de notre implémentation précédente est que l'on calcule toujours le modulo afin de déterminer les données de calibration à utiliser, ce qui est un peu dommage.
On commence avec les includes standards :
1 2 3 4 5 6 7 8 |
#include <iostream> #include <vector> #include <numeric> #include <execution> #include <algorithm> #include "micro_benchmark.h" |
On inclue notre allocateur aligné et on définit des type de std::vector alignés :
1 2 3 4 5 6 |
#include "AlignedAllocator.h" ///Defines a vector of index typedef std::vector<int, AlignedAllocator<int> > VecIndex; ///Defines a vector of data typedef std::vector<float, AlignedAllocator<float> > VecData; |
On déclare notre kernel :
1 2 3 4 5 6 7 8 9 10 |
///Compute the calibration /** @param[out] vecCalibSignal : vector of calibrated signal * @param vecADC : vector of ADC values * @param tabPed : pointer to the table of pedestal * @param tabGain : pointer to the table of gain * @param vecIdx : vector of index to be used to round robin over the pixel */ void compute_calibration(VecData & vecCalibSignal, const VecData & vecADC, const VecData & tabPed, const VecData & tabGain, const VecIndex & vecIdx) { |
On utilise le métode des indices et macro EXECUTION_POLICY gère toujours la méthode d'exécution à la compilation :
1 2 3 4 5 |
std::transform(EXECUTION_POLICY, std::begin(vecADC), std::end(vecADC), std::begin(vecIdx), std::begin(vecCalibSignal), [&](float adc, int i){ return (adc - tabPed[i])*tabGain[i]; }); } |
La fonction qui évaluera notre kernel :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
///Get the number of nanoseconds per elements of the Calibration /** @param nbPixel : number of pixels of the tables */ void evaluateCalibration(size_t nbPixel){ //Let's define size of data : size_t nbEvent(NB_EVENT), nbSlice(NB_SLICE); size_t nbElement(nbEvent*nbSlice*nbPixel); VecData vecGain(nbPixel), vecPedestal(nbPixel); std::fill(vecGain.begin(), vecGain.end(), 0.02f); std::fill(vecPedestal.begin(), vecPedestal.end(), 40.0f); VecData vecADCSignal(nbElement), vecCalibSignal(nbElement); std::fill(vecADCSignal.begin(), vecADCSignal.end(), 42.0f); |
On calcule le modulo une bonne fois pour toutes avant d'appeler notre kernel :
1 2 3 4 5 6 7 8 |
VecIndex vecIdx(nbElement); //Init vector of index for the computation std::iota(vecIdx.begin(), vecIdx.end(), 0); //Hope some day views will work to avoid allocation of index vector std::transform(EXECUTION_POLICY, vecIdx.begin(), vecIdx.end(), vecIdx.begin(), [=](int i){ //Modulo computation, only once at the initialisation time return i % nbPixel; } ); |
On lance l'évaluation de performances :
1 2 3 |
size_t fullNbElement(nbElement); micro_benchmarkAutoNsPrint("evaluateCalibration pre computed modulo", fullNbElement, compute_calibration, vecCalibSignal, vecADCSignal, vecPedestal, vecGain, vecIdx); } |
Enfin, nous appellons la fonction d'évaluation de MicroBenchmark :
1 2 3 |
int main(int argc, char** argv){ return micro_benchmarkParseArg(argc, argv, evaluateCalibration); } |
Le fichier main_precompute_modulo.cpp complet :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
/*************************************** Auteur : Pierre Aubert Mail : pierre.aubert@lapp.in2p3.fr Licence : CeCILL-C ****************************************/ #include <iostream> #include <vector> #include <numeric> #include <execution> #include <algorithm> #include "micro_benchmark.h" #include "AlignedAllocator.h" ///Defines a vector of index typedef std::vector<int, AlignedAllocator<int> > VecIndex; ///Defines a vector of data typedef std::vector<float, AlignedAllocator<float> > VecData; ///Compute the calibration /** @param[out] vecCalibSignal : vector of calibrated signal * @param vecADC : vector of ADC values * @param tabPed : pointer to the table of pedestal * @param tabGain : pointer to the table of gain * @param vecIdx : vector of index to be used to round robin over the pixel */ void compute_calibration(VecData & vecCalibSignal, const VecData & vecADC, const VecData & tabPed, const VecData & tabGain, const VecIndex & vecIdx) { std::transform(EXECUTION_POLICY, std::begin(vecADC), std::end(vecADC), std::begin(vecIdx), std::begin(vecCalibSignal), [&](float adc, int i){ return (adc - tabPed[i])*tabGain[i]; }); } ///Get the number of nanoseconds per elements of the Calibration /** @param nbPixel : number of pixels of the tables */ void evaluateCalibration(size_t nbPixel){ //Let's define size of data : size_t nbEvent(NB_EVENT), nbSlice(NB_SLICE); size_t nbElement(nbEvent*nbSlice*nbPixel); VecData vecGain(nbPixel), vecPedestal(nbPixel); std::fill(vecGain.begin(), vecGain.end(), 0.02f); std::fill(vecPedestal.begin(), vecPedestal.end(), 40.0f); VecData vecADCSignal(nbElement), vecCalibSignal(nbElement); std::fill(vecADCSignal.begin(), vecADCSignal.end(), 42.0f); VecIndex vecIdx(nbElement); //Init vector of index for the computation std::iota(vecIdx.begin(), vecIdx.end(), 0); //Hope some day views will work to avoid allocation of index vector std::transform(EXECUTION_POLICY, vecIdx.begin(), vecIdx.end(), vecIdx.begin(), [=](int i){ //Modulo computation, only once at the initialisation time return i % nbPixel; } ); size_t fullNbElement(nbElement); micro_benchmarkAutoNsPrint("evaluateCalibration pre computed modulo", fullNbElement, compute_calibration, vecCalibSignal, vecADCSignal, vecPedestal, vecGain, vecIdx); } int main(int argc, char** argv){ return micro_benchmarkParseArg(argc, argv, evaluateCalibration); } |