[502] | 1 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
[295] | 2 | // File : main.c (for transpose application) |
---|
| 3 | // Date : february 2014 |
---|
| 4 | // author : Alain Greiner |
---|
[502] | 5 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
| 6 | // This multi-threaded application makes a transpose for a NN*NN pixels |
---|
| 7 | // sequence of images. |
---|
[444] | 8 | // It can run on a multi-processors, multi-clusters architecture, with one thread |
---|
[502] | 9 | // per processor. |
---|
| 10 | // |
---|
[295] | 11 | // The image sequence is read from a file (one byte per pixel). |
---|
| 12 | // The input and output buffers containing the image are distributed in all clusters. |
---|
| 13 | // |
---|
[502] | 14 | // - The image size NN must fit the frame buffer size: 128 bytes |
---|
| 15 | // - The block size in block device must be 512 bytes. |
---|
| 16 | // - The number of clusters must be a power of 2 no larger than 32 |
---|
| 17 | // - The number of processors per cluster must be a power of 2 no larger than 4 |
---|
[295] | 18 | // |
---|
| 19 | // For each image the application makes a self test (checksum for each line). |
---|
[393] | 20 | // The actual display on the frame buffer depends on frame buffer availability. |
---|
[502] | 21 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
[295] | 22 | |
---|
| 23 | #include "stdio.h" |
---|
[502] | 24 | #include "user_barrier.h" |
---|
[383] | 25 | #include "malloc.h" |
---|
[295] | 26 | |
---|
[502] | 27 | #define BLOCK_SIZE 512 // block size on disk |
---|
| 28 | #define CLUSTERS_MAX 32 // max number of clusters |
---|
| 29 | #define PROCS_MAX 4 // max number of processors per cluster |
---|
[295] | 30 | #define NN 128 // image size : nlines = npixels = 128 |
---|
[444] | 31 | #define NB_IMAGES 5 // number of images to be handled |
---|
[295] | 32 | #define FILE_PATHNAME "misc/images.raw" // file pathname on disk |
---|
[393] | 33 | #define INSTRUMENTATION_OK 0 // display statistics on TTY when non zero |
---|
[295] | 34 | |
---|
| 35 | /////////////////////////////////////////////////////// |
---|
| 36 | // global variables stored in seg_data in cluster(0,0) |
---|
| 37 | /////////////////////////////////////////////////////// |
---|
| 38 | |
---|
[502] | 39 | // instrumentation counters for each processor in each cluster |
---|
| 40 | unsigned int LOAD_START[CLUSTERS_MAX][PROCS_MAX]; |
---|
| 41 | unsigned int LOAD_END [CLUSTERS_MAX][PROCS_MAX]; |
---|
| 42 | unsigned int TRSP_START[CLUSTERS_MAX][PROCS_MAX]; |
---|
| 43 | unsigned int TRSP_END [CLUSTERS_MAX][PROCS_MAX]; |
---|
| 44 | unsigned int DISP_START[CLUSTERS_MAX][PROCS_MAX]; |
---|
| 45 | unsigned int DISP_END [CLUSTERS_MAX][PROCS_MAX]; |
---|
[295] | 46 | |
---|
| 47 | // arrays of pointers on distributed buffers |
---|
| 48 | // one input buffer & one output buffer per cluster |
---|
[502] | 49 | unsigned char* buf_in [CLUSTERS_MAX]; |
---|
| 50 | unsigned char* buf_out[CLUSTERS_MAX]; |
---|
[295] | 51 | |
---|
| 52 | // checksum variables |
---|
| 53 | unsigned check_line_before[NN]; |
---|
| 54 | unsigned check_line_after[NN]; |
---|
| 55 | |
---|
[383] | 56 | // global synchronisation barrier |
---|
[502] | 57 | giet_sqt_barrier_t barrier; |
---|
[295] | 58 | |
---|
[444] | 59 | volatile unsigned int init_ok = 0; |
---|
[295] | 60 | |
---|
| 61 | ////////////////////////////////////////// |
---|
| 62 | __attribute__ ((constructor)) void main() |
---|
[383] | 63 | ////////////////////////////////////////// |
---|
[295] | 64 | { |
---|
| 65 | |
---|
[502] | 66 | unsigned int l; // line index for loops |
---|
| 67 | unsigned int p; // pixel index for loops |
---|
| 68 | unsigned int c; // cluster index for loops |
---|
[295] | 69 | |
---|
[502] | 70 | // processor identifiers |
---|
| 71 | unsigned int x; // x cluster coordinate |
---|
| 72 | unsigned int y; // y cluster coordinate |
---|
| 73 | unsigned int lpid; // local processor index |
---|
| 74 | |
---|
| 75 | // plat-form parameters |
---|
| 76 | unsigned int x_size; // number of clusters in a row |
---|
| 77 | unsigned int y_size; // number of clusters in a column |
---|
| 78 | unsigned int nprocs; // number of processors per cluster |
---|
| 79 | |
---|
[432] | 80 | giet_proc_xyp( &x, &y, &lpid); |
---|
[295] | 81 | |
---|
[502] | 82 | giet_procs_number( &x_size , &y_size , &nprocs ); |
---|
[295] | 83 | |
---|
[502] | 84 | unsigned int nclusters = x_size * y_size; // number of clusters |
---|
| 85 | unsigned int ntasks = x_size * y_size * nprocs; // number of tasks |
---|
| 86 | unsigned int npixels = NN * NN; // pixels per image |
---|
| 87 | unsigned int nblocks = npixels / BLOCK_SIZE; // blocks per image |
---|
| 88 | unsigned int image = 0; // image counter |
---|
| 89 | int file = 0; // file descriptor |
---|
| 90 | unsigned int cluster_id = (x * y_size) + y; // "continuous" index |
---|
| 91 | unsigned int task_id = (cluster_id * nprocs) + lpid; // "continuous" task index |
---|
| 92 | |
---|
[398] | 93 | // Processor [0,0,0] makes initialisation |
---|
[502] | 94 | // It includes parameters checking, barrier initialization, |
---|
[393] | 95 | // distributed buffers allocation, and file open |
---|
[432] | 96 | if ( (x==0) && (y==0) && (lpid==0) ) |
---|
[295] | 97 | { |
---|
[502] | 98 | if ((nprocs != 1) && (nprocs != 2) && (nprocs != 4)) |
---|
[295] | 99 | { |
---|
[502] | 100 | giet_exit("[TRANSPOSE ERROR] number of procs per cluster must be 1, 2 or 4"); |
---|
[295] | 101 | } |
---|
[502] | 102 | if ((nclusters != 1) && (nclusters != 2) && (nclusters != 4) && |
---|
| 103 | (nclusters != 8) && (nclusters != 16) && (nclusters != 32) ) |
---|
[295] | 104 | { |
---|
[393] | 105 | giet_exit("[TRANSPOSE ERROR] number of clusters must be 1,2,4,8,16,32"); |
---|
[295] | 106 | } |
---|
| 107 | if ( ntasks > NN ) |
---|
| 108 | { |
---|
| 109 | giet_exit("[TRANSPOSE ERROR] number of tasks larger than number of lines"); |
---|
| 110 | } |
---|
| 111 | |
---|
[393] | 112 | // Distributed buffers allocation |
---|
[502] | 113 | // The buffers containing one image are distributed in the user |
---|
| 114 | // heap (one buf_in and one buf_out per cluster). |
---|
| 115 | // Each buffer contains (NN*NN / nclusters) bytes. |
---|
| 116 | for ( c = 0 ; c < nclusters ; c++ ) |
---|
[393] | 117 | { |
---|
[502] | 118 | unsigned int rx = c / y_size; |
---|
| 119 | unsigned int ry = c % y_size; |
---|
[307] | 120 | |
---|
[543] | 121 | heap_init( rx, ry ); |
---|
[502] | 122 | buf_in[c] = remote_malloc( npixels/nclusters, rx, ry ); |
---|
| 123 | buf_out[c] = remote_malloc( npixels/nclusters, rx, ry ); |
---|
[295] | 124 | |
---|
[393] | 125 | giet_shr_printf("\n[TRANSPOSE] Proc [0,0,0] completes buffer allocation" |
---|
| 126 | " for cluster[%d,%d] at cycle %d\n" |
---|
| 127 | " - buf_in = %x\n" |
---|
| 128 | " - buf_out = %x\n", |
---|
| 129 | rx, ry, giet_proctime(), |
---|
| 130 | (unsigned int)buf_in[c], |
---|
| 131 | (unsigned int)buf_out[c] ); |
---|
| 132 | } |
---|
[295] | 133 | |
---|
[543] | 134 | // Barrier initialisation |
---|
| 135 | sqt_barrier_init( &barrier, x_size , y_size , nprocs ); |
---|
| 136 | |
---|
| 137 | giet_shr_printf("\n[TRANSPOSE] Proc [0,0,0] completes barrier init at cycle %d\n", |
---|
| 138 | giet_proctime() ); |
---|
| 139 | |
---|
[295] | 140 | // open file containing images |
---|
| 141 | file = giet_fat_open( "misc/images.raw", 0); |
---|
| 142 | |
---|
[307] | 143 | if (file < 0) |
---|
| 144 | { |
---|
[432] | 145 | giet_shr_printf("\n[TRANSPOSE ERROR] Proc [%d,%d,%d]" |
---|
[307] | 146 | " cannot open file misc/images.raw", |
---|
| 147 | x, y, lpid ); |
---|
| 148 | giet_exit(" open() failure"); |
---|
| 149 | } |
---|
[317] | 150 | else |
---|
| 151 | { |
---|
[432] | 152 | giet_shr_printf("\n[TRANSPOSE] Proc [0,0,0] open file misc/images.raw\n"); |
---|
[317] | 153 | } |
---|
[444] | 154 | init_ok = 1; |
---|
[295] | 155 | } |
---|
[393] | 156 | else // others processors wait initialisation completion |
---|
| 157 | { |
---|
[444] | 158 | while ( init_ok == 0 ); |
---|
[393] | 159 | } |
---|
| 160 | |
---|
[383] | 161 | ///////////////////////// |
---|
[295] | 162 | // Main loop (on images) |
---|
| 163 | while (image < NB_IMAGES) |
---|
| 164 | { |
---|
[502] | 165 | // pseudo parallel load from disk to buf_in buffer : nblocks/nclusters blocks |
---|
[295] | 166 | // only task running on processor with (lpid == 0) does it |
---|
| 167 | |
---|
| 168 | LOAD_START[cluster_id][lpid] = giet_proctime(); |
---|
| 169 | |
---|
| 170 | if (lpid == 0) |
---|
| 171 | { |
---|
| 172 | giet_fat_read( file, |
---|
| 173 | buf_in[cluster_id], |
---|
[502] | 174 | (nblocks / nclusters), |
---|
| 175 | ((image*nblocks) + ((nblocks*cluster_id)/nclusters)) ); |
---|
[295] | 176 | |
---|
[502] | 177 | if ( (x==0) && (y==0) ) |
---|
[432] | 178 | giet_shr_printf("\n[TRANSPOSE] Proc [%d,%d,%d] completes load" |
---|
[317] | 179 | " for image %d at cycle %d\n", |
---|
[432] | 180 | x, y, lpid, image, giet_proctime() ); |
---|
[295] | 181 | } |
---|
| 182 | |
---|
| 183 | LOAD_END[cluster_id][lpid] = giet_proctime(); |
---|
| 184 | |
---|
[502] | 185 | ///////////////////////////// |
---|
| 186 | sqt_barrier_wait( &barrier ); |
---|
[295] | 187 | |
---|
| 188 | // parallel transpose from buf_in to buf_out |
---|
| 189 | // each task makes the transposition for nlt lines (nlt = NN/ntasks) |
---|
| 190 | // from line [task_id*nlt] to line [(task_id + 1)*nlt - 1] |
---|
| 191 | // (p,l) are the absolute pixel coordinates in the source image |
---|
| 192 | |
---|
| 193 | |
---|
| 194 | TRSP_START[cluster_id][lpid] = giet_proctime(); |
---|
| 195 | |
---|
| 196 | unsigned int nlt = NN / ntasks; // number of lines per task |
---|
[502] | 197 | unsigned int nlc = NN / nclusters; // number of lines per cluster |
---|
[295] | 198 | |
---|
| 199 | unsigned int src_cluster; |
---|
| 200 | unsigned int src_index; |
---|
| 201 | unsigned int dst_cluster; |
---|
| 202 | unsigned int dst_index; |
---|
| 203 | |
---|
| 204 | unsigned char byte; |
---|
| 205 | |
---|
| 206 | unsigned int first = task_id * nlt; // first line index for a given task |
---|
| 207 | unsigned int last = first + nlt; // last line index for a given task |
---|
| 208 | |
---|
| 209 | for ( l = first ; l < last ; l++ ) |
---|
| 210 | { |
---|
| 211 | check_line_before[l] = 0; |
---|
| 212 | |
---|
| 213 | // in each iteration we transfer one byte |
---|
| 214 | for ( p = 0 ; p < NN ; p++ ) |
---|
| 215 | { |
---|
| 216 | // read one byte from local buf_in |
---|
| 217 | src_cluster = l / nlc; |
---|
| 218 | src_index = (l % nlc)*NN + p; |
---|
| 219 | byte = buf_in[src_cluster][src_index]; |
---|
| 220 | |
---|
| 221 | // compute checksum |
---|
| 222 | check_line_before[l] = check_line_before[l] + byte; |
---|
| 223 | |
---|
| 224 | // write one byte to remote buf_out |
---|
| 225 | dst_cluster = p / nlc; |
---|
| 226 | dst_index = (p % nlc)*NN + l; |
---|
| 227 | buf_out[dst_cluster][dst_index] = byte; |
---|
| 228 | } |
---|
| 229 | } |
---|
| 230 | |
---|
[307] | 231 | if ( lpid == 0 ) |
---|
| 232 | { |
---|
[502] | 233 | if ( (x==0) && (y==0) ) |
---|
[317] | 234 | giet_shr_printf("\n[TRANSPOSE] proc [%d,%d,0] completes transpose" |
---|
| 235 | " for image %d at cycle %d\n", |
---|
| 236 | x, y, image, giet_proctime() ); |
---|
[295] | 237 | |
---|
[307] | 238 | } |
---|
[295] | 239 | TRSP_END[cluster_id][lpid] = giet_proctime(); |
---|
| 240 | |
---|
[502] | 241 | ///////////////////////////// |
---|
| 242 | sqt_barrier_wait( &barrier ); |
---|
[295] | 243 | |
---|
| 244 | |
---|
[307] | 245 | if ( USE_FBF ) // external frame buffer available |
---|
[295] | 246 | { |
---|
[515] | 247 | // parallel display from local buf_out to frame buffer |
---|
| 248 | // all processors contribute to display using memcpy... |
---|
| 249 | |
---|
[295] | 250 | DISP_START[cluster_id][lpid] = giet_proctime(); |
---|
| 251 | |
---|
| 252 | unsigned int npt = npixels / ntasks; // number of pixels per task |
---|
| 253 | |
---|
[444] | 254 | giet_fbf_sync_write( npt * task_id, |
---|
| 255 | &buf_out[cluster_id][lpid*npt], |
---|
| 256 | npt ); |
---|
[295] | 257 | |
---|
[502] | 258 | if ( (x==0) && (y==0) && (lpid==0) ) |
---|
| 259 | giet_shr_printf("\n[TRANSPOSE] Proc [%d,%d,%d] completes display" |
---|
| 260 | " for image %d at cycle %d\n", |
---|
| 261 | x, y, lpid, image, giet_proctime() ); |
---|
[295] | 262 | |
---|
| 263 | DISP_END[cluster_id][lpid] = giet_proctime(); |
---|
| 264 | |
---|
[502] | 265 | ///////////////////////////// |
---|
| 266 | sqt_barrier_wait( &barrier ); |
---|
[295] | 267 | } |
---|
[515] | 268 | else // checksum by processor(x,y,0) in each cluster |
---|
[295] | 269 | { |
---|
[515] | 270 | if ( lpid == 0 ) |
---|
[295] | 271 | { |
---|
[515] | 272 | unsigned int success = 1; |
---|
| 273 | unsigned int start = cluster_id * nlc; |
---|
| 274 | unsigned int stop = start + nlc; |
---|
[295] | 275 | |
---|
[515] | 276 | for ( l = start ; l < stop ; l++ ) |
---|
[295] | 277 | { |
---|
[515] | 278 | check_line_after[l] = 0; |
---|
[295] | 279 | |
---|
[515] | 280 | for ( p = 0 ; p < NN ; p++ ) |
---|
| 281 | { |
---|
| 282 | // read one byte in remote buffer |
---|
| 283 | src_cluster = p / nlc; |
---|
| 284 | src_index = (p % nlc)*NN + l; |
---|
[295] | 285 | |
---|
[515] | 286 | unsigned char byte = buf_out[src_cluster][src_index]; |
---|
| 287 | |
---|
| 288 | check_line_after[l] = check_line_after[l] + byte; |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | if ( check_line_before[l] != check_line_after[l] ) success = 0; |
---|
[295] | 292 | } |
---|
| 293 | |
---|
[515] | 294 | if ( success ) |
---|
| 295 | { |
---|
| 296 | giet_shr_printf("\n[TRANSPOSE] proc [%d,%d,0] checksum OK" |
---|
| 297 | " for image %d at cycle %d\n", |
---|
| 298 | x, y, image, giet_proctime() ); |
---|
| 299 | } |
---|
| 300 | else |
---|
| 301 | { |
---|
| 302 | giet_shr_printf("\n[TRANSPOSE] proc [%d,%d,0] checksum KO" |
---|
| 303 | " for image %d at cycle %d\n", |
---|
| 304 | x, y, image, giet_proctime() ); |
---|
| 305 | } |
---|
| 306 | } |
---|
[295] | 307 | } |
---|
| 308 | |
---|
[502] | 309 | ///////////////////////////// |
---|
| 310 | sqt_barrier_wait( &barrier ); |
---|
[336] | 311 | |
---|
[432] | 312 | // instrumentation done by processor [0,0,0] |
---|
| 313 | if ( (x==0) && (y==0) && (lpid==0) && INSTRUMENTATION_OK ) |
---|
[295] | 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 | |
---|
[502] | 329 | for (cc = 0; cc < nclusters; cc++) |
---|
[295] | 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 | |
---|
[502] | 375 | ///////////////////////////// |
---|
| 376 | sqt_barrier_wait( &barrier ); |
---|
[295] | 377 | |
---|
| 378 | } // end while image |
---|
| 379 | |
---|
[398] | 380 | // Processor[0,0,0] releases the Distributed buffers |
---|
[432] | 381 | if ( (x==0) && (y==0) && (lpid==0) ) |
---|
[398] | 382 | { |
---|
[502] | 383 | for ( c = 0 ; c < nclusters ; c++ ) |
---|
[398] | 384 | { |
---|
| 385 | free( buf_in[c] ); |
---|
| 386 | free( buf_in[c] ); |
---|
| 387 | } |
---|
| 388 | } |
---|
| 389 | |
---|
[295] | 390 | giet_exit("Completed"); |
---|
| 391 | |
---|
| 392 | } // end main() |
---|
| 393 | |
---|
| 394 | // Local Variables: |
---|
| 395 | // tab-width: 3 |
---|
| 396 | // c-basic-offset: |
---|
| 397 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
| 398 | // indent-tabs-mode: nil |
---|
| 399 | // End: |
---|
| 400 | |
---|
| 401 | // vim: filetype=cpp:expandtab:shiftwidth=3:tabstop=3:softtabstop=3 |
---|
| 402 | |
---|
| 403 | |
---|
| 404 | |
---|