3.1.2.2 : Le fichier source
Écrivons le fichier temporary_alloc.cpp :
Nous commençons, bien sur, par inclure le header que nous venons d'écrire :
1 |
#include "temporary_alloc.h"
|
Et nous implémentons notre fonction d'initialisation (avec la documentation, j'insiste) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
///Allocate temporary /** @param[out] tmpU1 : matrix temporary U * @param[out] tmpU2 : matrix temporary U * @param[out] tmpV1 : matrix temporary V * @param[out] tmpV2 : matrix temporary V * @param[out] tmpInU : Tensor temporary in U * @param[out] tmpInV : Tensor temporary in V * @param[out] tmpOutU : Tensor temporary out U * @param[out] tmpOutV : Tensor temporary out V * @param nbRow : number of rows of the matrices to be created * @param nbCol : number of columns of the matrices to be created */ void allocate_temporary(float *& tmpU1, float *& tmpU2, float *& tmpV1, float *& tmpV2, PTensor<float> & tmpInU, PTensor<float> & tmpInV, PTensor<float> & tmpOutU, PTensor<float> & tmpOutV, size_t nbRow, size_t nbCol) { |
Nous devons redimensionner nos tenseurs avant de les utiliser :
1 2 3 4 |
tmpInU.resize(AllocMode::ALIGNED, nbRow, nbCol); tmpInV.resize(AllocMode::ALIGNED, nbRow, nbCol); tmpOutU.resize(AllocMode::ALIGNED, nbRow, nbCol); tmpOutV.resize(AllocMode::ALIGNED, nbRow, nbCol); |
Puis, nous initialisons leurs valeurs par défault (1 pour U et 0 pour V) :
1 2 3 4 |
tmpInU.fill(1.0f); tmpOutU.fill(1.0f); tmpInV.fill(0.0f); tmpOutV.fill(0.0f); |
Maintenant nous pouvons injecter du V et retirer du U au centre de nos images. POur cela nous définissons quelques variable de contrôl qui nous permettrons de jouer sur la taille du rectangle de départ :
1 2 3 4 5 6 7 |
size_t frac(16lu), numBegin(7lu), numEnd(8lu), rowShift(-4lu); for(size_t i(rowShift + (numBegin*nbRow)/frac); i < rowShift + (numEnd*nbRow)/frac; ++i){ for(size_t j((numBegin*nbCol)/frac); j < (numEnd*nbCol)/frac; ++j){ tmpInU.setValue(i, j, 0.0f); tmpInV.setValue(i, j, 1.0f); } } |
Cela nous donne une image V initiale qui ressemble à la figure 1. Vous pouvez, bien sur, modifier cette fonction pour initialiser votre matrice V comme bon vous semble.

Figure 1 : Initialisation de la matrice V, écrite dans la fonction allocate_temporary. Blanc : 1, noir : 0.
Il faut maintenant initialiser les pointeurs que nous avons passé par référence afin que l'on puisse les modifier à l'exérieur de la fonction :
1 2 3 4 5 |
tmpU1 = tmpInU.getData(); tmpU2 = tmpOutU.getData(); tmpV1 = tmpInV.getData(); tmpV2 = tmpOutV.getData(); |
Finalement, nous fermons cette implémentation de fonction :
1 |
} |
Le fichier temporary_alloc.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 |
/*************************************** Auteur : Pierre Aubert Mail : pierre.aubert@lapp.in2p3.fr Licence : CeCILL-C ****************************************/ #include "temporary_alloc.h" ///Allocate temporary /** @param[out] tmpU1 : matrix temporary U * @param[out] tmpU2 : matrix temporary U * @param[out] tmpV1 : matrix temporary V * @param[out] tmpV2 : matrix temporary V * @param[out] tmpInU : Tensor temporary in U * @param[out] tmpInV : Tensor temporary in V * @param[out] tmpOutU : Tensor temporary out U * @param[out] tmpOutV : Tensor temporary out V * @param nbRow : number of rows of the matrices to be created * @param nbCol : number of columns of the matrices to be created */ void allocate_temporary(float *& tmpU1, float *& tmpU2, float *& tmpV1, float *& tmpV2, PTensor<float> & tmpInU, PTensor<float> & tmpInV, PTensor<float> & tmpOutU, PTensor<float> & tmpOutV, size_t nbRow, size_t nbCol) { tmpInU.resize(AllocMode::ALIGNED, nbRow, nbCol); tmpInV.resize(AllocMode::ALIGNED, nbRow, nbCol); tmpOutU.resize(AllocMode::ALIGNED, nbRow, nbCol); tmpOutV.resize(AllocMode::ALIGNED, nbRow, nbCol); tmpInU.fill(1.0f); tmpOutU.fill(1.0f); tmpInV.fill(0.0f); tmpOutV.fill(0.0f); size_t frac(16lu), numBegin(7lu), numEnd(8lu), rowShift(-4lu); for(size_t i(rowShift + (numBegin*nbRow)/frac); i < rowShift + (numEnd*nbRow)/frac; ++i){ for(size_t j((numBegin*nbCol)/frac); j < (numEnd*nbCol)/frac; ++j){ tmpInU.setValue(i, j, 0.0f); tmpInV.setValue(i, j, 1.0f); } } tmpU1 = tmpInU.getData(); tmpU2 = tmpOutU.getData(); tmpV1 = tmpInV.getData(); tmpV2 = tmpOutV.getData(); } |
Vous pouvez le télécharger ici.