12.1.4.6 : Le main_mask2_transform.cpp avec des std::transform imbriqués
Cette histoire d'indices est un peu ridicule car cela consomme beaucoup de mémoire. Essayons d'imbriquer des std::transform pour ne plus avoir à l'utiliser. Mais essayons avec 2 std::transform pour ne pas complexifier le programme pour rien.
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 7 |
#include "AlignedAllocator.h" ///Defines a vector of index typedef std::vector<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] tabCalibSignal : table of calibrated signal (input tabADC) * @param tabPed : pointer to the table of pedestal * @param tabGain : pointer to the table of gain * @param vecEventIdx : vector of index of events * @param nbPixel : number of pixels */ void compute_calibration(float * tabCalibSignal, const float * tabPed, const float* tabGain, const VecIndex & vecEventIdx, size_t nbPixel) { |
On parcours les événements avec un tableaux d'indices, mais bien plus petit que dans les implémentations précédentes :
1 2 |
std::for_each(std::execution::seq, std::begin(vecEventIdx), std::end(vecEventIdx), [=](int evtIdx){ |
Pour chaque indice d'événements on lance un calcul avec deux std::transform pour calculer notre calibration (et la macro EXECUTION_POLICY gère toujours la méthode d'exécution à la compilation) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
long firstPixelIdx = evtIdx*nbPixel; std::transform(EXECUTION_POLICY, tabCalibSignal + firstPixelIdx, tabCalibSignal + firstPixelIdx + nbPixel, tabPed, tabCalibSignal + firstPixelIdx, [=](float signal, float ped){ return signal - ped; } ); std::transform(EXECUTION_POLICY, tabCalibSignal + firstPixelIdx, tabCalibSignal + firstPixelIdx + nbPixel, tabGain, tabCalibSignal + firstPixelIdx, [=](float signal, float gain){ return signal * gain; } ); } ); } |
La fonction qui évaluera notre kernel :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
///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*NB_SLICE); size_t nbValuePerImage(nbPixel); size_t nbElement(nbEvent*nbValuePerImage); VecData vecGain(nbPixel), vecPedestal(nbPixel); std::fill(vecGain.begin(), vecGain.end(), 0.02f); std::fill(vecPedestal.begin(), vecPedestal.end(), 40.0f); VecData vecADCSignal(nbElement); std::fill(vecADCSignal.begin(), vecADCSignal.end(), 42.0f); VecIndex vecEventIdx(nbEvent); //Init vectors of index for the computation std::iota(vecEventIdx.begin(), vecEventIdx.end(), 0); //We have to create pointer to be able to catch them by copy without losing any time float * tabADC = vecADCSignal.data(), *tabGain = vecGain.data(), *tabPed = vecPedestal.data(); size_t fullNbElement(nbElement); micro_benchmarkAutoNsPrint("evaluateCalibration 2d tranform", fullNbElement, compute_calibration, tabADC, tabPed, tabGain, vecEventIdx, nbPixel); } |
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_mask2_transform.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 67 68 69 70 71 72 73 74 75 76 77 |
/*************************************** 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> VecIndex; ///Defines a vector of data typedef std::vector<float, AlignedAllocator<float> > VecData; ///Compute the calibration /** @param[out] tabCalibSignal : table of calibrated signal (input tabADC) * @param tabPed : pointer to the table of pedestal * @param tabGain : pointer to the table of gain * @param vecEventIdx : vector of index of events * @param nbPixel : number of pixels */ void compute_calibration(float * tabCalibSignal, const float * tabPed, const float* tabGain, const VecIndex & vecEventIdx, size_t nbPixel) { std::for_each(std::execution::seq, std::begin(vecEventIdx), std::end(vecEventIdx), [=](int evtIdx){ long firstPixelIdx = evtIdx*nbPixel; std::transform(EXECUTION_POLICY, tabCalibSignal + firstPixelIdx, tabCalibSignal + firstPixelIdx + nbPixel, tabPed, tabCalibSignal + firstPixelIdx, [=](float signal, float ped){ return signal - ped; } ); std::transform(EXECUTION_POLICY, tabCalibSignal + firstPixelIdx, tabCalibSignal + firstPixelIdx + nbPixel, tabGain, tabCalibSignal + firstPixelIdx, [=](float signal, float gain){ return signal * gain; } ); } ); } ///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*NB_SLICE); size_t nbValuePerImage(nbPixel); size_t nbElement(nbEvent*nbValuePerImage); VecData vecGain(nbPixel), vecPedestal(nbPixel); std::fill(vecGain.begin(), vecGain.end(), 0.02f); std::fill(vecPedestal.begin(), vecPedestal.end(), 40.0f); VecData vecADCSignal(nbElement); std::fill(vecADCSignal.begin(), vecADCSignal.end(), 42.0f); VecIndex vecEventIdx(nbEvent); //Init vectors of index for the computation std::iota(vecEventIdx.begin(), vecEventIdx.end(), 0); //We have to create pointer to be able to catch them by copy without losing any time float * tabADC = vecADCSignal.data(), *tabGain = vecGain.data(), *tabPed = vecPedestal.data(); size_t fullNbElement(nbElement); micro_benchmarkAutoNsPrint("evaluateCalibration 2d tranform", fullNbElement, compute_calibration, tabADC, tabPed, tabGain, vecEventIdx, nbPixel); } int main(int argc, char** argv){ return micro_benchmarkParseArg(argc, argv, evaluateCalibration); } |