1 | |
---|
2 | #ifndef _CLOCK_H_ |
---|
3 | #define _CLOCK_H_ |
---|
4 | |
---|
5 | #include <stdint.h> |
---|
6 | |
---|
7 | #include "nrc_os_config.h" |
---|
8 | #if TARGET_OS == LINUX |
---|
9 | #include <sys/time.h> |
---|
10 | #endif |
---|
11 | |
---|
12 | /** |
---|
13 | * The macros should be called in the following order: |
---|
14 | * - CLOCK_DEC; |
---|
15 | * - CLOCK_INIT(num_threads, num_steps); |
---|
16 | * - CLOCK_APP_START; |
---|
17 | * - CLOCK_APP_CREATE; |
---|
18 | * - CLOCK_THREAD_START(thread_id); |
---|
19 | * - CLOCK_THREAD_COMPUTE_START(thread_id; |
---|
20 | * - CLOCK_THREAD_START_STEP(thread_id, step_id) |
---|
21 | * - CLOCK_THREAD_END_STEP(thread_id, step_id) |
---|
22 | * - (repeat num_steps times) |
---|
23 | * - CLOCK_THREAD_COMPUTE_END(thread_id); |
---|
24 | * - CLOCK_THREAD_END(thread_id) |
---|
25 | * - CLOCK_APP_JOIN; |
---|
26 | * - CLOCK_APP_END; |
---|
27 | * - CLOCK_FINALIZE(num_threads); |
---|
28 | * - PRINT_CLOCK; |
---|
29 | * - CLOCK_FREE; |
---|
30 | */ |
---|
31 | |
---|
32 | |
---|
33 | static void local_sort_asc(uint32_t tab[], int size) { |
---|
34 | int tmp; |
---|
35 | int i, j; |
---|
36 | for (i = 0; i < size; i++) { |
---|
37 | uint32_t min = tab[i]; |
---|
38 | int jmin = i; |
---|
39 | for (j = i + 1; j < size; j++) { |
---|
40 | if (tab[j] < min) { |
---|
41 | jmin = j; |
---|
42 | min = tab[j]; |
---|
43 | } |
---|
44 | } |
---|
45 | tmp = tab[i]; |
---|
46 | tab[i] = min; |
---|
47 | tab[jmin] = tmp; |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | |
---|
53 | #define CLOCK_DEC uint32_t app_start; \ |
---|
54 | uint32_t app_end; \ |
---|
55 | uint32_t app_create; \ |
---|
56 | uint32_t app_join; \ |
---|
57 | uint32_t * thread_start; \ |
---|
58 | uint32_t * thread_end; \ |
---|
59 | uint32_t * thread_compute_start; \ |
---|
60 | uint32_t * thread_compute_end; \ |
---|
61 | int32_t step_number; \ |
---|
62 | int32_t clock_thread_num; \ |
---|
63 | uint32_t ** thread_start_step; \ |
---|
64 | uint32_t ** thread_end_step; \ |
---|
65 | uint32_t global_thread_start; \ |
---|
66 | uint32_t global_thread_end; \ |
---|
67 | uint32_t global_thread_compute_start; \ |
---|
68 | uint32_t global_thread_compute_end; \ |
---|
69 | uint32_t * global_thread_start_step; \ |
---|
70 | uint32_t * global_thread_end_step; \ |
---|
71 | |
---|
72 | #if TARGET_OS == GIETVM |
---|
73 | #define CLOCK(x) ({ x = giet_proctime(); }) |
---|
74 | #elif TARGET_OS == LINUX |
---|
75 | #define CLOCK(x) ({ \ |
---|
76 | struct timeval full_time; \ |
---|
77 | gettimeofday(&full_time, NULL); \ |
---|
78 | x = (unsigned long) ((full_time.tv_usec + full_time.tv_sec * 1000000) / 1000); \ |
---|
79 | }) |
---|
80 | #endif |
---|
81 | |
---|
82 | // x = number of threads, y = number of steps |
---|
83 | #define CLOCK_INIT(x, y) ({ \ |
---|
84 | clock_thread_num = (x); \ |
---|
85 | step_number = (y); \ |
---|
86 | global_thread_start = 0xFFFFFFFFLLU; \ |
---|
87 | global_thread_end = 0; \ |
---|
88 | global_thread_compute_start = 0xFFFFFFFFLLU; \ |
---|
89 | global_thread_compute_end = 0; \ |
---|
90 | if ((x) > 0) { \ |
---|
91 | thread_start = (uint32_t *) malloc(sizeof(uint32_t) * (x)); \ |
---|
92 | thread_end = (uint32_t *) malloc(sizeof(uint32_t) * (x)); \ |
---|
93 | thread_compute_start = (uint32_t *) malloc(sizeof(uint32_t) * (x)); \ |
---|
94 | thread_compute_end = (uint32_t *) malloc(sizeof(uint32_t) * (x)); \ |
---|
95 | if ((y) > 0) { \ |
---|
96 | global_thread_start_step = (uint32_t *) malloc(sizeof(uint32_t) * (y)); \ |
---|
97 | global_thread_end_step = (uint32_t *) malloc(sizeof(uint32_t) * (y)); \ |
---|
98 | thread_start_step = (uint32_t **) malloc(sizeof(uint32_t *) * (y)); \ |
---|
99 | thread_end_step = (uint32_t **) malloc(sizeof(uint32_t *) * (y)); \ |
---|
100 | for (int j = 0; j < (y); j++) { \ |
---|
101 | global_thread_start_step[j] = 0xFFFFFFFFLU; \ |
---|
102 | global_thread_end_step[j] = 0; \ |
---|
103 | thread_start_step[j] = (uint32_t *) malloc(sizeof(uint32_t) * (x)); \ |
---|
104 | thread_end_step[j] = (uint32_t *) malloc(sizeof(uint32_t) * (x)); \ |
---|
105 | } \ |
---|
106 | } \ |
---|
107 | } \ |
---|
108 | }) |
---|
109 | |
---|
110 | |
---|
111 | #define CLOCK_APP_START ({ CLOCK(app_start); }) |
---|
112 | #define CLOCK_APP_END ({ CLOCK(app_end); }) |
---|
113 | #define CLOCK_APP_CREATE ({ CLOCK(app_create); }) |
---|
114 | #define CLOCK_APP_JOIN ({ CLOCK(app_join); }) |
---|
115 | #define CLOCK_THREAD_START(x) ({ CLOCK(thread_start[x]); }) |
---|
116 | #define CLOCK_THREAD_END(x) ({ CLOCK(thread_end[x]); }) |
---|
117 | #define CLOCK_THREAD_COMPUTE_START(x) ({ CLOCK(thread_compute_start[x]); }) |
---|
118 | #define CLOCK_THREAD_COMPUTE_END(x) ({ CLOCK(thread_compute_end[x]); }) |
---|
119 | #define CLOCK_THREAD_START_STEP(x, y) ({ CLOCK(thread_start_step[y][x]); }) |
---|
120 | #define CLOCK_THREAD_END_STEP(x, y) ({ CLOCK(thread_end_step[y][x]); }) |
---|
121 | |
---|
122 | |
---|
123 | // x = number of threads |
---|
124 | #define CLOCK_FINALIZE ({ \ |
---|
125 | for (int i = 0; i < clock_thread_num; i++) { \ |
---|
126 | if (thread_start[i] < global_thread_start) { \ |
---|
127 | global_thread_start = thread_start[i]; \ |
---|
128 | } \ |
---|
129 | if (thread_compute_start[i] < global_thread_compute_start) { \ |
---|
130 | global_thread_compute_start = thread_compute_start[i]; \ |
---|
131 | } \ |
---|
132 | if (thread_end[i] > global_thread_end) { \ |
---|
133 | global_thread_end = thread_end[i]; \ |
---|
134 | } \ |
---|
135 | if (thread_compute_end[i] > global_thread_compute_end) { \ |
---|
136 | global_thread_compute_end = thread_compute_end[i]; \ |
---|
137 | } \ |
---|
138 | for (int j = 0; j < step_number; j++) { \ |
---|
139 | if (thread_start_step[j][i] < global_thread_start_step[j]) { \ |
---|
140 | global_thread_start_step[j] = thread_start_step[j][i]; \ |
---|
141 | } \ |
---|
142 | if (thread_end_step[j][i] > global_thread_end_step[j]) { \ |
---|
143 | global_thread_end_step[j] = thread_end_step[j][i]; \ |
---|
144 | } \ |
---|
145 | } \ |
---|
146 | } \ |
---|
147 | }) |
---|
148 | |
---|
149 | #define PRINT_CLOCK ({ \ |
---|
150 | printf("Timestamps:\n"); \ |
---|
151 | printf("[APP_START] : %d\n", app_start); \ |
---|
152 | printf("[APP_CREATE] : %d\n", app_create); \ |
---|
153 | printf("[THREAD_START] : %d\n", global_thread_start); \ |
---|
154 | printf("[THREAD_COMPUTE_START] : %d\n", global_thread_compute_start); \ |
---|
155 | for (int j = 0; j < step_number; j++) { \ |
---|
156 | printf("[THREAD_START_STEP_%d] : %d\n", j, global_thread_start_step[j]); \ |
---|
157 | printf("[THREAD_END_STEP_%d] : %d\n", j, global_thread_end_step[j]); \ |
---|
158 | } \ |
---|
159 | printf("[THREAD_COMPUTE_END] : %d\n", global_thread_compute_end); \ |
---|
160 | printf("[THREAD_END] : %d\n", global_thread_end); \ |
---|
161 | printf("[APP_JOIN] : %d\n", app_join); \ |
---|
162 | printf("[APP_END] : %d\n", app_end); \ |
---|
163 | printf("Durations (in cycles):\n"); \ |
---|
164 | printf("[TOTAL] : %d\n", app_end - app_start); \ |
---|
165 | printf("[THREAD] : %d\n", app_join - app_create); \ |
---|
166 | printf("[PARALLEL] : %d\n", global_thread_end - global_thread_start); \ |
---|
167 | printf("[PARALLEL_COMPUTE] : %d\n", global_thread_compute_end - global_thread_compute_start); \ |
---|
168 | for (int j = 0; j < step_number; j++) { \ |
---|
169 | printf("[THREAD_STEP_%d] : %d\n", j, global_thread_end_step[j] - global_thread_start_step[j]); \ |
---|
170 | } \ |
---|
171 | printf("\n"); \ |
---|
172 | printf("*** All threads times output in a gnuplot data-style ***\n"); \ |
---|
173 | local_sort_asc(thread_start, clock_thread_num); \ |
---|
174 | local_sort_asc(thread_compute_start, clock_thread_num); \ |
---|
175 | local_sort_asc(thread_compute_end, clock_thread_num); \ |
---|
176 | local_sort_asc(thread_end, clock_thread_num); \ |
---|
177 | for (int j = 0; j < step_number; j++) { \ |
---|
178 | local_sort_asc(thread_start_step[j], clock_thread_num); \ |
---|
179 | local_sort_asc(thread_end_step[j], clock_thread_num); \ |
---|
180 | } \ |
---|
181 | printf("# cycle thread_id\n"); \ |
---|
182 | for (int i = 0; i < clock_thread_num; i++) { \ |
---|
183 | printf("%d\t%d\n", thread_start[i], i); \ |
---|
184 | printf("%d\t%d\n", thread_compute_start[i], i); \ |
---|
185 | for (int j = 0; j < step_number; j++) { \ |
---|
186 | printf("%d\t%d\n", thread_start_step[j][i], i); \ |
---|
187 | printf("%d\t%d\n", thread_end_step[j][i], i); \ |
---|
188 | } \ |
---|
189 | printf("%d\t%d\n", thread_compute_end[i], i); \ |
---|
190 | printf("%d\t%d\n", thread_end[i], i); \ |
---|
191 | } \ |
---|
192 | }) |
---|
193 | |
---|
194 | |
---|
195 | |
---|
196 | |
---|
197 | |
---|
198 | |
---|
199 | #define CLOCK_FREE ({ \ |
---|
200 | if (clock_thread_num > 0) { \ |
---|
201 | free(thread_start); \ |
---|
202 | free(thread_end); \ |
---|
203 | free(thread_compute_start); \ |
---|
204 | free(thread_compute_end); \ |
---|
205 | if (step_number > 0) { \ |
---|
206 | free(global_thread_start_step); \ |
---|
207 | free(global_thread_end_step); \ |
---|
208 | for (int j = 0; j < step_number; j++) { \ |
---|
209 | free(thread_start_step[j]); \ |
---|
210 | free(thread_end_step[j]); \ |
---|
211 | } \ |
---|
212 | free(thread_start_step); \ |
---|
213 | free(thread_end_step); \ |
---|
214 | } \ |
---|
215 | } \ |
---|
216 | }) |
---|
217 | |
---|
218 | |
---|
219 | |
---|
220 | |
---|
221 | #endif |
---|
222 | |
---|