3.3.1.1.2 : Le fichier asterics_alloc.cpp
Développons le fichier asterics_alloc.cpp :

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
#ifndef __APPLE__
#	include <malloc.h>
#else
#	include <stdlib.h>
#endif

#include <string.h>
#include "asterics_alloc.h"

#ifdef __APPLE__
///Alloc an aligned vector
/**	@param sizeOfVectorInBytes : size of the vector xe want to allocate
 * 	@param alignementInBytes : alignement of the vector we want to allocate
 * 	@return aligned pointor of the vector
*/
void * memalign(long unsigned int alignementInBytes, long unsigned int sizeOfVectorInBytes){
	void * ptr = NULL;
	posix_memalign(&ptr, alignementInBytes, sizeOfVectorInBytes);
	return ptr;
}
#endif

///Get the pitch of a matrix
/**	@param nbCol : number of columns of the matrix
 * 	@return pitch of the matrix
*/
long unsigned int getPitch(long unsigned int nbCol){
	long unsigned int vecSize(VECTOR_ALIGNEMENT/sizeof(float));
	long unsigned int pitch(vecSize - (nbCol % vecSize));
	if(pitch == vecSize){pitch = 0lu;}
	return pitch;
}

///Do the aligned allocation of a pointer
/**	@param sizeOfVectorInBytes : number of bytes to be allocated
 * 	@return allocated pointer
*/
void * asterics_malloc(long unsigned int sizeOfVectorInBytes){
	return memalign(VECTOR_ALIGNEMENT, sizeOfVectorInBytes + getPitch(sizeOfVectorInBytes));
}

///Free an aligned pointer
/**	@param ptr : ptr to be freed
*/
void asterics_free(void* ptr){
	free(ptr);
}

///Allocate a 2d matrix with a pitch for float
/**	@param nbRow : number of rows of the matrix
 * 	@param nbCol : number of columns of the matrix
 * 	@return allocated matrix
*/
float * asterics_malloc2f(long unsigned int nbRow, long unsigned int nbCol){
	long unsigned int pitch(getPitch(nbCol));
	long unsigned int sizeByte(sizeof(float)*nbRow*(nbCol + pitch));
	float* mat = (float*)asterics_malloc(sizeByte);
	memset(mat, 0, sizeByte);			//Do not forget to initialse the values of the matrix which are in the pitch
	return mat;
}


Le fichier asterics_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
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
****************************************/

#ifndef __APPLE__
#	include <malloc.h>
#else
#	include <stdlib.h>
#endif

#include <string.h>
#include "asterics_alloc.h"

#ifdef __APPLE__
///Alloc an aligned vector
/**	@param sizeOfVectorInBytes : size of the vector xe want to allocate
 * 	@param alignementInBytes : alignement of the vector we want to allocate
 * 	@return aligned pointor of the vector
*/
void * memalign(long unsigned int alignementInBytes, long unsigned int sizeOfVectorInBytes){
	void * ptr = NULL;
	posix_memalign(&ptr, alignementInBytes, sizeOfVectorInBytes);
	return ptr;
}
#endif

///Get the pitch of a matrix
/**	@param nbCol : number of columns of the matrix
 * 	@return pitch of the matrix
*/
long unsigned int getPitch(long unsigned int nbCol){
	long unsigned int vecSize(VECTOR_ALIGNEMENT/sizeof(float));
	long unsigned int pitch(vecSize - (nbCol % vecSize));
	if(pitch == vecSize){pitch = 0lu;}
	return pitch;
}

///Do the aligned allocation of a pointer
/**	@param sizeOfVectorInBytes : number of bytes to be allocated
 * 	@return allocated pointer
*/
void * asterics_malloc(long unsigned int sizeOfVectorInBytes){
	return memalign(VECTOR_ALIGNEMENT, sizeOfVectorInBytes + getPitch(sizeOfVectorInBytes));
}

///Free an aligned pointer
/**	@param ptr : ptr to be freed
*/
void asterics_free(void* ptr){
	free(ptr);
}

///Allocate a 2d matrix with a pitch for float
/**	@param nbRow : number of rows of the matrix
 * 	@param nbCol : number of columns of the matrix
 * 	@return allocated matrix
*/
float * asterics_malloc2f(long unsigned int nbRow, long unsigned int nbCol){
	long unsigned int pitch(getPitch(nbCol));
	long unsigned int sizeByte(sizeof(float)*nbRow*(nbCol + pitch));
	float* mat = (float*)asterics_malloc(sizeByte);
	memset(mat, 0, sizeByte);			//Do not forget to initialse the values of the matrix which are in the pitch
	return mat;
}


Vous pouvez télécharger le fichier ici.