2
2
// Created by nuxeslin on 2018/11/7.
3
3
//
4
4
#include < iostream>
5
- #include < Eigen/ Core>
5
+ #include < Core>
6
6
#include < stdlib.h>
7
7
8
8
using namespace std ;
9
9
using namespace Eigen ;
10
10
11
- template <int M, int N>
11
+ template <int M, int N>
12
12
void InitMat (float mat[M][N], int row_num, int col_num) {
13
13
// for (int i = 0; i < row_num; i++) {
14
14
// for (int j = 0; j < col_num; j++) {
15
15
// mat[i * col_num + j] = 1;
16
16
// }
17
17
// }
18
18
for (int k = 0 ; k < row_num * col_num; k++) {
19
- *((float *) mat + k) = 1 ;
19
+ *((float *) mat + k) = 1 ;
20
20
}
21
21
}
22
22
23
- template <size_t N>
23
+ template <size_t N>
24
24
struct MyAllocator {
25
25
char buf[N];
26
26
void *ptr;
27
27
size_t sz;
28
+
28
29
MyAllocator () : ptr(buf), sz(N) {}
29
- template <typename T>
30
- T* aligned_malloc (size_t a = alignof(T)) {
30
+
31
+ template <typename T>
32
+ T *aligned_malloc (size_t a = alignof(T)) {
31
33
if (std::align (a, sizeof (T), ptr, sz)) {
32
- auto res = reinterpret_cast <T*>(ptr);
33
- ptr = (char *) res + sizeof (T);
34
+ auto res = reinterpret_cast <T *>(ptr);
35
+ ptr = (char *) res + sizeof (T);
34
36
sz -= sizeof (T);
35
37
return res;
36
38
}
@@ -42,7 +44,7 @@ struct MyAllocator {
42
44
struct KMatrix {
43
45
int width;
44
46
int height;
45
- float * data;
47
+ float * data;
46
48
};
47
49
48
50
// set memory alignment
@@ -59,40 +61,41 @@ struct Area {
59
61
double x3;
60
62
};
61
63
62
- void Run () {
63
- int size = 3 ;
64
- Eigen::VectorXf v (size), u (size), w (size);
65
- u = v + w;
66
- std::cout << u << std::endl;
67
- }
68
-
69
64
// at runtime
70
65
void KL_bad_allocate () {
71
66
auto huge = static_cast <size_t >(-1 );
72
67
::operator new (huge);
73
68
}
74
69
75
70
void KL_compute_op () {
76
- Eigen::Matrix2d mat;
77
- KL_bad_allocate ();
71
+ Eigen::MatrixXf mat (2 , 2 );
72
+ mat << 2 , 4 ,
73
+ 3 , 9 ;
74
+ mat.inverse ();
75
+ // KL_bad_allocate();
78
76
// std::bad_alloc();
79
77
}
80
78
79
+ void Run () {
80
+ KL_compute_op ();
81
+ }
82
+
83
+
81
84
void AlignedAllocate () {
82
85
char n = ' x' ;
83
86
float q = 2.0 ;
84
- printf (" y1: 0x%lx\n " , (size_t )&n);
87
+ printf (" y1: 0x%lx\n " , (size_t ) &n);
85
88
// without customized alignment
86
89
// alignment equal to min(sizeof(float), 16) = 4 bytes
87
90
// so there will be 3 bytes padding subsequently
88
91
// - (4 + 3 = 7) bytes
89
- printf (" y2: 0x%lx\n " , (size_t )&q);
92
+ printf (" y2: 0x%lx\n " , (size_t ) &q);
90
93
// alignment at heap
91
94
printf (" at heap:\n " );
92
95
// auto pool = (Area*)malloc(sizeof(Area) * 3);
93
96
Area *pool;
94
97
// guarantee the address of `pool` multiple of 32(bytes), while `malloc` will not
95
- if (posix_memalign (reinterpret_cast <void **>(&pool), 32 , 64 * 3 )) {
98
+ if (posix_memalign (reinterpret_cast <void **>(&pool), 32 , 64 * 3 )) {
96
99
perror (" error while allocate aligned memory\n " );
97
100
}
98
101
// pool = (Area*)malloc(24 * 3);
@@ -108,11 +111,11 @@ void AlignedAllocate() {
108
111
pool[1 ] = {' o' , 4 };
109
112
// it works
110
113
// pool[1000] = {'e', 7, 2};
111
- printf (" r0: %ld\n " , ((size_t )&pool[0 ]) % 32 );
112
- printf (" p1: 0x%lx\n " , (size_t )&pool[1 ]);
114
+ printf (" r0: %ld\n " , ((size_t ) &pool[0 ]) % 32 );
115
+ printf (" p1: 0x%lx\n " , (size_t ) &pool[1 ]);
113
116
// + 48 bytes
114
- printf (" p2: 0x%lx\n " , (size_t )&pool[2 ]);
115
- printf (" offset: %lu\n " , ((char *) &pool[1 ] - (char *) &pool[0 ]));
117
+ printf (" p2: 0x%lx\n " , (size_t ) &pool[2 ]);
118
+ printf (" offset: %lu\n " , ((char *) &pool[1 ] - (char *) &pool[0 ]));
116
119
// printf("%c\n", pool[1].x1);
117
120
}
118
121
@@ -125,13 +128,13 @@ void Zmalloc() {
125
128
// so we can access an element by *(buffer + i), let the pointer move up
126
129
char buffer[6 ] = " kkkkk" ;
127
130
// initialize to 0 when allocated at heap
128
- auto ptr = (float *) malloc (4 );
131
+ auto ptr = (float *) malloc (4 );
129
132
// non-successive at heap
130
133
// arithmetic on a pointer to void is invalid
131
- auto other = (float *) malloc (4 );
134
+ auto other = (float *) malloc (4 );
132
135
// posix_memalign((void**)&ptr, 16, 4);
133
- cout << " ptr: " << (void *) ptr << endl;
134
- printf (" other: 0x%lx\n " , (size_t )other);
136
+ cout << " ptr: " << (void *) ptr << endl;
137
+ printf (" other: 0x%lx\n " , (size_t ) other);
135
138
// only pointers to compatible type can be subtracted
136
139
printf (" ptr offset: %lu\n " , (other - ptr));
137
140
printf (" alignof T: %lu\n " , alignof (char ));
@@ -140,12 +143,12 @@ void Zmalloc() {
140
143
cout << " ptr0: " << allocator.ptr << endl;
141
144
auto ptr1 = allocator.aligned_malloc <char >();
142
145
*ptr1 = ' x' ;
143
- cout << " ptr1: " << (void *) ptr1 << endl;
146
+ cout << " ptr1: " << (void *) ptr1 << endl;
144
147
// internal pointer of allocator will be adjusted when std::align has been invoked
145
148
// aligned at runtime
146
149
auto ptr2 = allocator.aligned_malloc <int >(8 );
147
150
*ptr2 = 7 ;
148
- cout << " ptr2: " << (void *) ptr2 << endl;
151
+ cout << " ptr2: " << (void *) ptr2 << endl;
149
152
// size is 48 bytes when alignment is 16 bytes, and 32 bytes when 8 bytes, 28 bytes when 4 bytes
150
153
printf (" size of area: %lu\n " , sizeof (Area));
151
154
Area ka = {' x' , 5 };
@@ -175,11 +178,6 @@ int main() {
175
178
char my_key[] = " lollipop" ;
176
179
memset (my_key, ' k' , 6 );
177
180
printf (" %s\n " , my_key);
178
- InitMat<6 , 6 >(mat, 6 , 6 );
179
- ::KMatrix cmat = {6 , 5 };
180
- cmat.data = (float *)malloc (cmat.width * cmat.height * sizeof (float ));
181
- cmat.data [3 ] = 1 ;
182
- printf (" %.1f\n " , cmat.data [3 ]);
183
181
Run ();
184
182
Zmalloc ();
185
183
// printf("size is %ld\n", sizeof(st));
0 commit comments