1 | /////////////////////////////////////////////////////////////////////////////////////////////// |
---|
2 | // File : main.c (for transpose application) |
---|
3 | // Date : february 2014 |
---|
4 | // author : Alain Greiner |
---|
5 | // |
---|
6 | // This application makes a transpose for a NN*NN pixels sequence of images. |
---|
7 | // The image sequence is read from a file (one byte per pixel). |
---|
8 | // The input and output buffers containing the image are distributed in all clusters. |
---|
9 | // |
---|
10 | // - The image size NN must be a power of 2. |
---|
11 | // - The number of clusters containing processors must be a power of 2. |
---|
12 | // - The number of processors per cluster (NB_PROCS_MAX) must be a power of 2. |
---|
13 | // - The image size NN must be larger or equal to the total number of processor. |
---|
14 | // |
---|
15 | // For each image the application makes a self test (checksum for each line). |
---|
16 | // The actual display on the frame buffer is optional (controled by DISPLAY_OK define) |
---|
17 | /////////////////////////////////////////////////////////////////////////////////////////////// |
---|
18 | |
---|
19 | #include "hard_config.h" |
---|
20 | #include "stdio.h" |
---|
21 | #include "malloc.h" |
---|
22 | #include "barrier.h" |
---|
23 | |
---|
24 | #define NN 128 // image size : nlines = npixels = 128 |
---|
25 | #define NB_IMAGES 5 // number of images to be handled |
---|
26 | #define FILE_PATHNAME "misc/images.raw" // file pathname on disk |
---|
27 | |
---|
28 | #define INSTRUMENTATION_OK 0 // display statistcs on TTY when non zero |
---|
29 | |
---|
30 | /////////////////////////////////////////////////////// |
---|
31 | // global variables stored in seg_data in cluster(0,0) |
---|
32 | /////////////////////////////////////////////////////// |
---|
33 | |
---|
34 | // instrumentation counters |
---|
35 | // for each processor (up to 4 processors) |
---|
36 | // in each cluster (up to 32 clusters) |
---|
37 | unsigned int LOAD_START[32][4]; |
---|
38 | unsigned int LOAD_END [32][4]; |
---|
39 | unsigned int TRSP_START[32][4]; |
---|
40 | unsigned int TRSP_END [32][4]; |
---|
41 | unsigned int DISP_START[32][4]; |
---|
42 | unsigned int DISP_END [32][4]; |
---|
43 | |
---|
44 | // arrays of pointers on distributed buffers |
---|
45 | // one input buffer & one output buffer per cluster |
---|
46 | unsigned char* buf_in [32]; |
---|
47 | unsigned char* buf_out[32]; |
---|
48 | |
---|
49 | // checksum variables |
---|
50 | unsigned check_line_before[NN]; |
---|
51 | unsigned check_line_after[NN]; |
---|
52 | |
---|
53 | // synchronisation barriers |
---|
54 | giet_barrier_t barrier_0; |
---|
55 | giet_barrier_t barrier_1; |
---|
56 | giet_barrier_t barrier_2; |
---|
57 | giet_barrier_t barrier_3; |
---|
58 | giet_barrier_t barrier_4; |
---|
59 | |
---|
60 | volatile unsigned int init_ok = 1; |
---|
61 | |
---|
62 | ////////////////////////////////////////// |
---|
63 | __attribute__ ((constructor)) void main() |
---|
64 | { |
---|
65 | |
---|
66 | int file; // file descriptor for images |
---|
67 | unsigned int l; // line index for loops |
---|
68 | unsigned int p; // pixel index for loops |
---|
69 | |
---|
70 | unsigned int proc_id = giet_procid(); // processor id |
---|
71 | unsigned int lpid = proc_id % NB_PROCS_MAX; // local processor id |
---|
72 | unsigned int cluster_xy = proc_id / NB_PROCS_MAX; // cluster index (8 bits format) |
---|
73 | unsigned int x = cluster_xy >> Y_WIDTH; // x coordinate |
---|
74 | unsigned int y = cluster_xy & ((1<<Y_WIDTH)-1); // y coordinate |
---|
75 | |
---|
76 | unsigned int npixels = NN * NN; // number of pixel per image |
---|
77 | unsigned int nblocks = npixels / 512; // number of blocks per image |
---|
78 | unsigned int image = 0; // image counter |
---|
79 | |
---|
80 | unsigned int nclusters = X_SIZE * Y_SIZE; // number of clusters with procs |
---|
81 | unsigned int cluster_id = (x * Y_SIZE) + y; // "continuous" cluster index |
---|
82 | unsigned int ntasks = nclusters * NB_PROCS_MAX; // number of tasks |
---|
83 | unsigned int task_id = (cluster_id * NB_PROCS_MAX) +lpid; // "continuous" task index |
---|
84 | |
---|
85 | // processor [0,0,0] makes parameters checking and barriers initialization |
---|
86 | if ( proc_id == 0 ) |
---|
87 | { |
---|
88 | if ((NB_PROCS_MAX != 1) && (NB_PROCS_MAX != 2) && (NB_PROCS_MAX != 4)) |
---|
89 | { |
---|
90 | giet_exit("[TRANSPOSE ERROR] NB_PROCS_MAX must be 1, 2 or 4"); |
---|
91 | } |
---|
92 | if ((nclusters != 1) && (nclusters != 2) && (nclusters != 4) && (nclusters != 8) && |
---|
93 | (nclusters != 16) && (nclusters != 32) ) |
---|
94 | { |
---|
95 | giet_exit("[TRANSPOSE ERROR] number of clusters must be 2,4,8,16,32"); |
---|
96 | } |
---|
97 | if ( ntasks > NN ) |
---|
98 | { |
---|
99 | giet_exit("[TRANSPOSE ERROR] number of tasks larger than number of lines"); |
---|
100 | } |
---|
101 | |
---|
102 | if ( ntasks > 1 ) |
---|
103 | { |
---|
104 | barrier_init( &barrier_0, ntasks ); |
---|
105 | barrier_init( &barrier_1, ntasks ); |
---|
106 | barrier_init( &barrier_2, ntasks ); |
---|
107 | barrier_init( &barrier_3, ntasks ); |
---|
108 | barrier_init( &barrier_4, ntasks ); |
---|
109 | } |
---|
110 | |
---|
111 | giet_shr_printf("\n[TRANSPOSE] Proc [%d,%d,%d] completes barrier init at cycle %d\n", |
---|
112 | 0, 0, 0, giet_proctime() ); |
---|
113 | giet_shr_printf(" - nclusters = %d\n", nclusters ); |
---|
114 | giet_shr_printf(" - ntasks = %d\n", ntasks ); |
---|
115 | |
---|
116 | init_ok = 0; |
---|
117 | } |
---|
118 | else // others processors wait initialisation completion |
---|
119 | { |
---|
120 | while ( init_ok == 1 ); |
---|
121 | } |
---|
122 | |
---|
123 | // The buffers containing the images are distributed in clusters |
---|
124 | // (one buf_in and one buf_out per cluster). |
---|
125 | // Each buffer contains (NN*NN / nclusters) bytes. |
---|
126 | // They are allocated in the cluster[x,y] heap by processor[x,y,0] |
---|
127 | if ( lpid == 0 ) |
---|
128 | { |
---|
129 | // get heap vaddr in cluster[0,0] |
---|
130 | unsigned int heap_base; |
---|
131 | giet_vobj_get_vbase( "transpose", "trsp_heap_0_0", &heap_base ); |
---|
132 | |
---|
133 | // allocate buffers in cluster[x,y] |
---|
134 | buf_in[cluster_id] = ((unsigned char*)heap_base) + (cluster_xy << 20); |
---|
135 | buf_out[cluster_id] = buf_in[cluster_id] + NN*NN/nclusters; |
---|
136 | |
---|
137 | giet_shr_printf("\n[TRANSPOSE] Proc [%d,%d,%d] completes buffer allocation at cycle %d\n" |
---|
138 | " - buf_in = %x\n" |
---|
139 | " - buf_out = %x\n", |
---|
140 | x, y, 0, giet_proctime(), |
---|
141 | (unsigned int)buf_in[cluster_id], |
---|
142 | (unsigned int)buf_out[cluster_id] ); |
---|
143 | |
---|
144 | // open file containing images |
---|
145 | file = giet_fat_open( "misc/images.raw", 0); |
---|
146 | |
---|
147 | if (file < 0) |
---|
148 | { |
---|
149 | giet_shr_printf("[TRANSPOSE ERROR] Processor[%d,%d,%d]" |
---|
150 | " cannot open file misc/images.raw", |
---|
151 | x, y, lpid ); |
---|
152 | giet_exit(" open() failure"); |
---|
153 | } |
---|
154 | else |
---|
155 | { |
---|
156 | giet_shr_printf("\n[TRANSPOSE] Processor[%d,%d,%d]" |
---|
157 | " open file misc/images.raw\n", |
---|
158 | x, y, lpid ); |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | ///////////////////////////////////////////// |
---|
163 | if ( ntasks > 1 ) barrier_wait( &barrier_0 ); |
---|
164 | |
---|
165 | // Main loop (on images) |
---|
166 | while (image < NB_IMAGES) |
---|
167 | { |
---|
168 | // pseudo parallel load from disk to buf_in buffer : nblocks/nclusters blocks |
---|
169 | // only task running on processor with (lpid == 0) does it |
---|
170 | |
---|
171 | LOAD_START[cluster_id][lpid] = giet_proctime(); |
---|
172 | |
---|
173 | if (lpid == 0) |
---|
174 | { |
---|
175 | giet_fat_read( file, |
---|
176 | buf_in[cluster_id], |
---|
177 | (nblocks / nclusters), |
---|
178 | ((image*nblocks) + ((nblocks*cluster_id)/nclusters)) ); |
---|
179 | |
---|
180 | giet_shr_printf("\n[TRANSPOSE] Proc [%d,%d,0] completes load" |
---|
181 | " for image %d at cycle %d\n", |
---|
182 | x, y, image, giet_proctime() ); |
---|
183 | } |
---|
184 | |
---|
185 | LOAD_END[cluster_id][lpid] = giet_proctime(); |
---|
186 | |
---|
187 | ///////////////////////////////////////////// |
---|
188 | if ( ntasks > 1 ) barrier_wait( &barrier_1 ); |
---|
189 | |
---|
190 | // parallel transpose from buf_in to buf_out |
---|
191 | // each task makes the transposition for nlt lines (nlt = NN/ntasks) |
---|
192 | // from line [task_id*nlt] to line [(task_id + 1)*nlt - 1] |
---|
193 | // (p,l) are the absolute pixel coordinates in the source image |
---|
194 | |
---|
195 | |
---|
196 | TRSP_START[cluster_id][lpid] = giet_proctime(); |
---|
197 | |
---|
198 | unsigned int nlt = NN / ntasks; // number of lines per task |
---|
199 | unsigned int nlc = NN / nclusters; // number of lines per cluster |
---|
200 | |
---|
201 | unsigned int src_cluster; |
---|
202 | unsigned int src_index; |
---|
203 | unsigned int dst_cluster; |
---|
204 | unsigned int dst_index; |
---|
205 | |
---|
206 | unsigned char byte; |
---|
207 | |
---|
208 | unsigned int first = task_id * nlt; // first line index for a given task |
---|
209 | unsigned int last = first + nlt; // last line index for a given task |
---|
210 | |
---|
211 | for ( l = first ; l < last ; l++ ) |
---|
212 | { |
---|
213 | check_line_before[l] = 0; |
---|
214 | |
---|
215 | // in each iteration we transfer one byte |
---|
216 | for ( p = 0 ; p < NN ; p++ ) |
---|
217 | { |
---|
218 | // read one byte from local buf_in |
---|
219 | src_cluster = l / nlc; |
---|
220 | src_index = (l % nlc)*NN + p; |
---|
221 | byte = buf_in[src_cluster][src_index]; |
---|
222 | |
---|
223 | // compute checksum |
---|
224 | check_line_before[l] = check_line_before[l] + byte; |
---|
225 | |
---|
226 | // write one byte to remote buf_out |
---|
227 | dst_cluster = p / nlc; |
---|
228 | dst_index = (p % nlc)*NN + l; |
---|
229 | buf_out[dst_cluster][dst_index] = byte; |
---|
230 | } |
---|
231 | } |
---|
232 | |
---|
233 | if ( lpid == 0 ) |
---|
234 | { |
---|
235 | giet_shr_printf("\n[TRANSPOSE] proc [%d,%d,0] completes transpose" |
---|
236 | " for image %d at cycle %d\n", |
---|
237 | x, y, image, giet_proctime() ); |
---|
238 | |
---|
239 | } |
---|
240 | TRSP_END[cluster_id][lpid] = giet_proctime(); |
---|
241 | |
---|
242 | ///////////////////////////////////////////// |
---|
243 | if ( ntasks > 1 ) barrier_wait( &barrier_2 ); |
---|
244 | |
---|
245 | // optional parallel display from local buf_out to frame buffer |
---|
246 | // all processors contribute to display using memcpy... |
---|
247 | |
---|
248 | if ( USE_FBF ) // external frame buffer available |
---|
249 | { |
---|
250 | DISP_START[cluster_id][lpid] = giet_proctime(); |
---|
251 | |
---|
252 | unsigned int npt = npixels / ntasks; // number of pixels per task |
---|
253 | |
---|
254 | giet_fb_sync_write( npt * task_id, |
---|
255 | &buf_out[cluster_id][lpid*npt], |
---|
256 | npt ); |
---|
257 | |
---|
258 | if ( lpid == 0 ) |
---|
259 | { |
---|
260 | giet_shr_printf("\n[TRANSPOSE] Proc [%d,%d,0] completes display" |
---|
261 | " for image %d at cycle %d\n", |
---|
262 | x, y, image, giet_proctime() ); |
---|
263 | } |
---|
264 | |
---|
265 | DISP_END[cluster_id][lpid] = giet_proctime(); |
---|
266 | |
---|
267 | ///////////////////////////////////////////// |
---|
268 | if ( ntasks > 1 ) barrier_wait( &barrier_3 ); |
---|
269 | } |
---|
270 | |
---|
271 | // checksum done by processor (lpid == 0) in each cluster |
---|
272 | |
---|
273 | if ( lpid == 0 ) |
---|
274 | { |
---|
275 | unsigned int success = 1; |
---|
276 | unsigned int start = cluster_id * nlc; |
---|
277 | unsigned int stop = start + nlc; |
---|
278 | |
---|
279 | for ( l = start ; l < stop ; l++ ) |
---|
280 | { |
---|
281 | check_line_after[l] = 0; |
---|
282 | |
---|
283 | for ( p = 0 ; p < NN ; p++ ) |
---|
284 | { |
---|
285 | // read one byte in remote buffer |
---|
286 | src_cluster = p / nlc; |
---|
287 | src_index = (p % nlc)*NN + l; |
---|
288 | |
---|
289 | unsigned char byte = buf_out[src_cluster][src_index]; |
---|
290 | |
---|
291 | check_line_after[l] = check_line_after[l] + byte; |
---|
292 | } |
---|
293 | |
---|
294 | if ( check_line_before[l] != check_line_after[l] ) success = 0; |
---|
295 | } |
---|
296 | |
---|
297 | if ( success ) |
---|
298 | { |
---|
299 | giet_shr_printf("\n[TRANSPOSE] proc [%d,%d,0] checksum OK" |
---|
300 | " for image %d\n", |
---|
301 | x, y, image ); |
---|
302 | } |
---|
303 | else |
---|
304 | { |
---|
305 | giet_shr_printf("\n[TRANSPOSE] proc [%d,%d,0] checksum KO" |
---|
306 | " for image %d\n", |
---|
307 | x, y, image ); |
---|
308 | } |
---|
309 | } |
---|
310 | |
---|
311 | // instrumentation done by processor [0,0,0] |
---|
312 | |
---|
313 | if ( (proc_id == 0) && INSTRUMENTATION_OK ) |
---|
314 | { |
---|
315 | int cc, pp; |
---|
316 | unsigned int min_load_start = 0xFFFFFFFF; |
---|
317 | unsigned int max_load_start = 0; |
---|
318 | unsigned int min_load_ended = 0xFFFFFFFF; |
---|
319 | unsigned int max_load_ended = 0; |
---|
320 | unsigned int min_trsp_start = 0xFFFFFFFF; |
---|
321 | unsigned int max_trsp_start = 0; |
---|
322 | unsigned int min_trsp_ended = 0xFFFFFFFF; |
---|
323 | unsigned int max_trsp_ended = 0; |
---|
324 | unsigned int min_disp_start = 0xFFFFFFFF; |
---|
325 | unsigned int max_disp_start = 0; |
---|
326 | unsigned int min_disp_ended = 0xFFFFFFFF; |
---|
327 | unsigned int max_disp_ended = 0; |
---|
328 | |
---|
329 | for (cc = 0; cc < nclusters; cc++) |
---|
330 | { |
---|
331 | for (pp = 0; pp < NB_PROCS_MAX; pp++) |
---|
332 | { |
---|
333 | if (LOAD_START[cc][pp] < min_load_start) min_load_start = LOAD_START[cc][pp]; |
---|
334 | if (LOAD_START[cc][pp] > max_load_start) max_load_start = LOAD_START[cc][pp]; |
---|
335 | if (LOAD_END[cc][pp] < min_load_ended) min_load_ended = LOAD_END[cc][pp]; |
---|
336 | if (LOAD_END[cc][pp] > max_load_ended) max_load_ended = LOAD_END[cc][pp]; |
---|
337 | if (TRSP_START[cc][pp] < min_trsp_start) min_trsp_start = TRSP_START[cc][pp]; |
---|
338 | if (TRSP_START[cc][pp] > max_trsp_start) max_trsp_start = TRSP_START[cc][pp]; |
---|
339 | if (TRSP_END[cc][pp] < min_trsp_ended) min_trsp_ended = TRSP_END[cc][pp]; |
---|
340 | if (TRSP_END[cc][pp] > max_trsp_ended) max_trsp_ended = TRSP_END[cc][pp]; |
---|
341 | if (DISP_START[cc][pp] < min_disp_start) min_disp_start = DISP_START[cc][pp]; |
---|
342 | if (DISP_START[cc][pp] > max_disp_start) max_disp_start = DISP_START[cc][pp]; |
---|
343 | if (DISP_END[cc][pp] < min_disp_ended) min_disp_ended = DISP_END[cc][pp]; |
---|
344 | if (DISP_END[cc][pp] > max_disp_ended) max_disp_ended = DISP_END[cc][pp]; |
---|
345 | } |
---|
346 | } |
---|
347 | |
---|
348 | giet_shr_printf(" - LOAD_START : min = %d / max = %d / med = %d / delta = %d\n", |
---|
349 | min_load_start, max_load_start, (min_load_start+max_load_start)/2, |
---|
350 | max_load_start-min_load_start); |
---|
351 | |
---|
352 | giet_shr_printf(" - LOAD_END : min = %d / max = %d / med = %d / delta = %d\n", |
---|
353 | min_load_ended, max_load_ended, (min_load_ended+max_load_ended)/2, |
---|
354 | max_load_ended-min_load_ended); |
---|
355 | |
---|
356 | giet_shr_printf(" - TRSP_START : min = %d / max = %d / med = %d / delta = %d\n", |
---|
357 | min_trsp_start, max_trsp_start, (min_trsp_start+max_trsp_start)/2, |
---|
358 | max_trsp_start-min_trsp_start); |
---|
359 | |
---|
360 | giet_shr_printf(" - TRSP_END : min = %d / max = %d / med = %d / delta = %d\n", |
---|
361 | min_trsp_ended, max_trsp_ended, (min_trsp_ended+max_trsp_ended)/2, |
---|
362 | max_trsp_ended-min_trsp_ended); |
---|
363 | |
---|
364 | giet_shr_printf(" - DISP_START : min = %d / max = %d / med = %d / delta = %d\n", |
---|
365 | min_disp_start, max_disp_start, (min_disp_start+max_disp_start)/2, |
---|
366 | max_disp_start-min_disp_start); |
---|
367 | |
---|
368 | giet_shr_printf(" - DISP_END : min = %d / max = %d / med = %d / delta = %d\n", |
---|
369 | min_disp_ended, max_disp_ended, (min_disp_ended+max_disp_ended)/2, |
---|
370 | max_disp_ended-min_disp_ended); |
---|
371 | } |
---|
372 | |
---|
373 | image++; |
---|
374 | |
---|
375 | ////////////////////////////////////////////////// |
---|
376 | // all tasks wait instrumentation completion |
---|
377 | ////////////////////////////////////////////////// |
---|
378 | if ( ntasks > 1 ) barrier_wait( &barrier_4 ); |
---|
379 | |
---|
380 | } // end while image |
---|
381 | |
---|
382 | giet_exit("Completed"); |
---|
383 | |
---|
384 | } // end main() |
---|
385 | |
---|
386 | // Local Variables: |
---|
387 | // tab-width: 3 |
---|
388 | // c-basic-offset: |
---|
389 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
390 | // indent-tabs-mode: nil |
---|
391 | // End: |
---|
392 | |
---|
393 | // vim: filetype=cpp:expandtab:shiftwidth=3:tabstop=3:softtabstop=3 |
---|
394 | |
---|
395 | |
---|
396 | |
---|