1 |
|
|
/*************************************** |
2 |
|
|
Auteur : Pierre Aubert |
3 |
|
|
Mail : pierre.aubert@lapp.in2p3.fr |
4 |
|
|
Licence : CeCILL-C |
5 |
|
|
****************************************/ |
6 |
|
|
|
7 |
|
|
#ifndef __TEMPLATE_ALLOC_IMPL_H__ |
8 |
|
|
#define __TEMPLATE_ALLOC_IMPL_H__ |
9 |
|
|
|
10 |
|
|
#include "template_alloc.h" |
11 |
|
|
|
12 |
|
|
///Get the padding with respect to the number of elements of the tables |
13 |
|
|
/** @param nbElement : number of elements of the table |
14 |
|
|
* @return associated padding |
15 |
|
|
*/ |
16 |
|
|
template<typename T> |
17 |
|
1106 |
size_t getPadding(size_t nbElement){ |
18 |
|
1106 |
size_t regVecSizeType(alignement_type<T>()); |
19 |
|
1106 |
size_t padding = regVecSizeType - (nbElement % regVecSizeType); |
20 |
✓✓ |
1106 |
if(padding == regVecSizeType){padding = 0lu;} |
21 |
|
1106 |
return padding; |
22 |
|
|
} |
23 |
|
|
|
24 |
|
|
///Allocate a tensor |
25 |
|
|
/** @param[out] padding : padding of the allocated tensor if it needs one |
26 |
|
|
* @param[out] isAligned : true if the allocated tensor is aligned, false otherwise |
27 |
|
|
* @param mode : allocation mode |
28 |
|
|
* @param tabDim : table of the dimensions of the tensor |
29 |
|
|
* @param nbDim : number of dimensions of the tensor |
30 |
|
|
* @return allocated pointer or NULL on failure |
31 |
|
|
*/ |
32 |
|
|
template<typename T> |
33 |
|
1774 |
T * template_alloc(size_t & padding, bool & isAligned, AllocMode::AllocMode mode, const size_t * tabDim, size_t nbDim){ |
34 |
|
1774 |
padding = 0lu; |
35 |
|
1774 |
isAligned = false; |
36 |
✓✓ |
1774 |
if(mode == AllocMode::NONE){ |
37 |
✓✗ |
655 |
T * tabData = new T[tensor_getFullSize(tabDim, nbDim)]; |
38 |
|
655 |
return tabData; |
39 |
✓✓ |
1119 |
}else if(mode == AllocMode::ALIGNED){ |
40 |
|
565 |
isAligned = true; |
41 |
|
565 |
size_t alignementInByte(alignement_in_bytes<T>()); |
42 |
✗✓ |
565 |
if(alignementInByte == 0lu){ //No alignement for this type |
43 |
|
|
T * tabData = new T[tensor_getFullSize(tabDim, nbDim)]; |
44 |
|
|
return tabData; |
45 |
|
|
}else{ |
46 |
|
565 |
T * tabData = (T*)pallocAlignedTab(tensor_getFullSize(tabDim, nbDim)*sizeof(T), alignementInByte); |
47 |
|
565 |
return tabData; |
48 |
|
|
} |
49 |
✓✗ |
554 |
}else if(mode == AllocMode::PADDING){ |
50 |
|
554 |
isAligned = true; |
51 |
|
554 |
size_t alignementInByte(alignement_in_bytes<T>()); |
52 |
✗✓ |
554 |
if(alignementInByte == 0lu){ //No alignement for this type |
53 |
|
|
T * tabData = new T[tensor_getFullSize(tabDim, nbDim)]; |
54 |
|
|
return tabData; |
55 |
|
|
}else{ |
56 |
|
554 |
size_t fullSize(tensor_getFullSize(tabDim, nbDim)); |
57 |
|
554 |
size_t nbCol(tabDim[nbDim - 1lu]); |
58 |
|
554 |
size_t nbRow(fullSize/nbCol); |
59 |
|
554 |
padding = getPadding<T>(nbCol); |
60 |
|
554 |
T * tabData = (T*)pallocAlignedTab((nbRow*(nbCol + padding))*sizeof(T), alignementInByte); |
61 |
|
554 |
return tabData; |
62 |
|
|
} |
63 |
|
|
} |
64 |
|
|
return NULL; |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
///Do a template allcoation of a type |
68 |
|
|
/** @param nbValue : number of value to be allcoated |
69 |
|
|
* @return allcated table |
70 |
|
|
*/ |
71 |
|
|
template<typename T> |
72 |
|
|
T * template_alloc_aligned1d(size_t nbValue){ |
73 |
|
|
size_t padding(0lu); |
74 |
|
|
bool isAligned(false); |
75 |
|
|
size_t dims[1]; |
76 |
|
|
dims[0] = nbValue; |
77 |
|
|
return template_alloc<T>(padding, isAligned, AllocMode::ALIGNED, dims, 1lu); |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
///Alloc an aligned vector |
81 |
|
|
/** @param alignedPtr : aligned pointor |
82 |
|
|
* @param sizeOfVectorInT : size of the vector we want to allocate |
83 |
|
|
* @param alignementInBytes : alignement of the vector we want to allocate, in bytes |
84 |
|
|
* |
85 |
|
|
* Usage : |
86 |
|
|
* T* alignedVector = NULL; |
87 |
|
|
* newAlignedTab(alignedVector, sizeOfVectorInT, alignementInT); |
88 |
|
|
* ... |
89 |
|
|
* Using of alignedVector pointor |
90 |
|
|
* ... |
91 |
|
|
* freeAlignedVector(alignedVector); |
92 |
|
|
*/ |
93 |
|
|
template<typename T> |
94 |
|
|
void newAlignedTab(T* & alignedPtr, size_t sizeOfVectorInT, size_t alignementInBytes){ |
95 |
|
|
alignedPtr = (T*)pallocAlignedTab(sizeOfVectorInT*sizeof(T), alignementInBytes); |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
#endif |
99 |
|
|
|