[769] | 1 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[708] | 2 | // File : transpose.c |
---|
| 3 | // Date : september 2015 |
---|
| 4 | // author : Alain Greiner |
---|
[769] | 5 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[764] | 6 | // This multi-threaded aplication read a raw image (one byte per pixel) |
---|
| 7 | // stored on disk, transpose it, display the result on the frame buffer, |
---|
| 8 | // and store the transposed image on disk. |
---|
| 9 | // The input image can be interactively selected if the INTERACTIVE flag is set. |
---|
[708] | 10 | // It can run on a multi-processors, multi-clusters architecture, with one thread |
---|
| 11 | // per processor, and uses the POSIX threads API. |
---|
[764] | 12 | // It uses the giet_fat_mmap() to directly access the input and output files |
---|
| 13 | // in the kernel files cache. It does not use the CMA to display the result image. |
---|
[708] | 14 | // |
---|
| 15 | // The main() function can be launched on any processor P[x,y,l]. |
---|
| 16 | // It makes the initialisations, launch (N-1) threads to run the execute() function |
---|
| 17 | // on the (N-1) other processors than P[x,y,l], call himself the execute() function, |
---|
| 18 | // and finally call the instrument() function to display instrumentation results |
---|
| 19 | // when the parallel execution is completed. |
---|
| 20 | // |
---|
[764] | 21 | // The buf_in[x,y] and buf_out[put buffers containing the direct ans transposed images |
---|
| 22 | // are distributed in clusters: |
---|
| 23 | // In each cluster[x,y], the thread running on processor P[x,y,0] uses the giet_fat_mmap() |
---|
| 24 | // function to map the buf_in[x,y] and buf_out[x,y] buffers containing a set of lines. |
---|
| 25 | // Then, all threads in cluster[x,y] read pixels from the local buf_in[x,y] buffer, and |
---|
| 26 | // write the pixels to the remote buf_out[x,y] buffers. Finally, each thread display |
---|
| 27 | // a part of the transposed image to the frame buffer. |
---|
[708] | 28 | // |
---|
[769] | 29 | // - The image size must fit the frame buffer width and height, that must be power of 2. |
---|
[708] | 30 | // - The number of clusters must be a power of 2 no larger than 256. |
---|
| 31 | // - The number of processors per cluster must be a power of 2 no larger than 4. |
---|
[764] | 32 | // - The number of clusters cannot be larger than (image_size * image_size) / 4096, |
---|
| 33 | // because the size of buf_in[x,y] and buf_out[x,y] must be multiple of 4096. |
---|
| 34 | // |
---|
| 35 | // The transpose_rw.c file contains a variant that use the giet_fat_read() |
---|
| 36 | // and giet_fat_write() system calls, to access the files. |
---|
[769] | 37 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[708] | 38 | |
---|
| 39 | #include "stdio.h" |
---|
[712] | 40 | #include "stdlib.h" |
---|
[782] | 41 | #include "string.h" |
---|
[708] | 42 | #include "user_barrier.h" |
---|
| 43 | #include "malloc.h" |
---|
| 44 | |
---|
[764] | 45 | #define BLOCK_SIZE 512 // block size on disk |
---|
| 46 | #define X_MAX 16 // max number of clusters in row |
---|
| 47 | #define Y_MAX 16 // max number of clusters in column |
---|
| 48 | #define PROCS_MAX 4 // max number of procs per cluster |
---|
| 49 | #define CLUSTER_MAX (X_MAX * Y_MAX) // max number of clusters |
---|
[775] | 50 | #define IMAGE_SIZE 128 // default image size |
---|
| 51 | #define INPUT_FILE_PATH "/misc/images_128.raw" // default input file pathname |
---|
| 52 | #define OUTPUT_FILE_PATH "/home/trsp_128.raw" // default output file pathname |
---|
[764] | 53 | #define INTERACTIVE 0 // interactive capture of filenames |
---|
[775] | 54 | #define VERBOSE 0 // print comments on TTY |
---|
[708] | 55 | |
---|
[764] | 56 | |
---|
[708] | 57 | // macro to use a shared TTY |
---|
| 58 | #define printf(...); { lock_acquire( &tty_lock ); \ |
---|
| 59 | giet_tty_printf(__VA_ARGS__); \ |
---|
| 60 | lock_release( &tty_lock ); } |
---|
| 61 | |
---|
| 62 | /////////////////////////////////////////////////////// |
---|
| 63 | // global variables stored in seg_data in cluster(0,0) |
---|
| 64 | /////////////////////////////////////////////////////// |
---|
| 65 | |
---|
| 66 | // instrumentation counters for each processor in each cluster |
---|
[764] | 67 | unsigned int MMAP_START[X_MAX][Y_MAX][PROCS_MAX] = {{{ 0 }}}; |
---|
| 68 | unsigned int MMAP_END [X_MAX][Y_MAX][PROCS_MAX] = {{{ 0 }}}; |
---|
[708] | 69 | unsigned int TRSP_START[X_MAX][Y_MAX][PROCS_MAX] = {{{ 0 }}}; |
---|
| 70 | unsigned int TRSP_END [X_MAX][Y_MAX][PROCS_MAX] = {{{ 0 }}}; |
---|
| 71 | unsigned int DISP_START[X_MAX][Y_MAX][PROCS_MAX] = {{{ 0 }}}; |
---|
| 72 | unsigned int DISP_END [X_MAX][Y_MAX][PROCS_MAX] = {{{ 0 }}}; |
---|
| 73 | |
---|
| 74 | // arrays of pointers on distributed buffers |
---|
| 75 | // one input buffer & one output buffer per cluster |
---|
| 76 | unsigned char* buf_in [CLUSTER_MAX]; |
---|
| 77 | unsigned char* buf_out[CLUSTER_MAX]; |
---|
| 78 | |
---|
| 79 | // lock protecting shared TTY |
---|
| 80 | user_lock_t tty_lock; |
---|
| 81 | |
---|
| 82 | // synchronisation barrier (all threads) |
---|
| 83 | giet_sqt_barrier_t barrier; |
---|
| 84 | |
---|
[712] | 85 | // input & output files pathname and size |
---|
| 86 | char input_file_name[256]; |
---|
| 87 | char output_file_name[256]; |
---|
| 88 | unsigned int image_size; |
---|
| 89 | |
---|
[764] | 90 | // input & output file descriptors |
---|
| 91 | int fd_in; |
---|
| 92 | int fd_out; |
---|
| 93 | |
---|
[708] | 94 | //////////////////////////////////////////// |
---|
| 95 | __attribute__ ((constructor)) void execute() |
---|
| 96 | //////////////////////////////////////////// |
---|
| 97 | { |
---|
| 98 | unsigned int l; // line index for loops |
---|
| 99 | unsigned int p; // pixel index for loops |
---|
| 100 | |
---|
| 101 | // get processor identifiers |
---|
| 102 | unsigned int x_id; // x cluster coordinate |
---|
| 103 | unsigned int y_id; // y cluster coordinate |
---|
| 104 | unsigned int p_id; // local processor index |
---|
| 105 | |
---|
| 106 | giet_proc_xyp( &x_id, &y_id, &p_id); |
---|
| 107 | |
---|
| 108 | // get & check plat-form parameters |
---|
| 109 | unsigned int x_size; // number of clusters in a row |
---|
| 110 | unsigned int y_size; // number of clusters in a column |
---|
| 111 | unsigned int nprocs; // number of processors per cluster |
---|
| 112 | |
---|
| 113 | giet_procs_number( &x_size , &y_size , &nprocs ); |
---|
| 114 | |
---|
| 115 | unsigned int nclusters = x_size * y_size; // number of clusters |
---|
| 116 | unsigned int nthreads = x_size * y_size * nprocs; // number of threads |
---|
[712] | 117 | unsigned int npixels = image_size * image_size; // pixels per image |
---|
[708] | 118 | unsigned int cluster_id = (x_id * y_size) + y_id; // "continuous" index |
---|
[764] | 119 | unsigned int thread_id = (cluster_id * nprocs) + p_id; // "continuous" index |
---|
[708] | 120 | |
---|
| 121 | // parallel load of image: |
---|
[764] | 122 | // thread running on processor[x,y,0] |
---|
| 123 | // map input & output files in buf_in & buf_out buffers. |
---|
[708] | 124 | |
---|
[764] | 125 | MMAP_START[x_id][y_id][p_id] = giet_proctime(); |
---|
[708] | 126 | |
---|
| 127 | if ( p_id == 0 ) |
---|
| 128 | { |
---|
[775] | 129 | // map buf_in |
---|
[764] | 130 | unsigned int length = npixels / nclusters; |
---|
| 131 | unsigned int offset = length * cluster_id; |
---|
| 132 | |
---|
| 133 | buf_in[cluster_id] = giet_fat_mmap( NULL, |
---|
| 134 | length, |
---|
| 135 | MAP_PROT_READ, |
---|
| 136 | MAP_SHARED, |
---|
| 137 | fd_in, |
---|
| 138 | offset ); |
---|
| 139 | if ( buf_in[cluster_id] == NULL ) |
---|
[708] | 140 | { |
---|
[764] | 141 | printf("\n[TRANSPOSE ERROR] Thread[%d,%d,%d] cannot map input file\n", |
---|
| 142 | x_id , y_id , p_id ); |
---|
| 143 | giet_pthread_exit( NULL ); |
---|
[708] | 144 | } |
---|
[764] | 145 | |
---|
[769] | 146 | if ( VERBOSE ) |
---|
[775] | 147 | { |
---|
| 148 | printf("\n@@@ Thread[%d,%d,%d] call mmap for input file\n" |
---|
| 149 | " length = %x / offset = %x / buf_in = %x\n", |
---|
| 150 | x_id , y_id , p_id , length , offset , buf_in[cluster_id] ); |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | // map buf_out |
---|
[764] | 154 | buf_out[cluster_id] = giet_fat_mmap( NULL, |
---|
| 155 | length, |
---|
| 156 | MAP_PROT_WRITE, |
---|
| 157 | MAP_SHARED, |
---|
| 158 | fd_out, |
---|
| 159 | offset ); |
---|
[775] | 160 | |
---|
[764] | 161 | if ( buf_out[cluster_id] == NULL ) |
---|
[708] | 162 | { |
---|
[764] | 163 | printf("\n[TRANSPOSE ERROR] Thread[%d,%d,%d] cannot map output file\n", |
---|
| 164 | x_id , y_id , p_id ); |
---|
| 165 | giet_pthread_exit( NULL ); |
---|
[708] | 166 | } |
---|
[764] | 167 | |
---|
[769] | 168 | if ( VERBOSE ) |
---|
[775] | 169 | { |
---|
| 170 | printf("\n@@@ Thread[%d,%d,%d] call mmap for output file\n" |
---|
| 171 | " length = %x / offset = %x / buf_out = %x\n", |
---|
| 172 | x_id , y_id , p_id , length , offset , buf_out[cluster_id] ); |
---|
| 173 | } |
---|
[764] | 174 | |
---|
[708] | 175 | } |
---|
| 176 | |
---|
[764] | 177 | MMAP_END[x_id][y_id][p_id] = giet_proctime(); |
---|
[708] | 178 | |
---|
| 179 | ///////////////////////////// |
---|
| 180 | sqt_barrier_wait( &barrier ); |
---|
| 181 | ///////////////////////////// |
---|
| 182 | |
---|
| 183 | // parallel transpose from buf_in to buf_out |
---|
[712] | 184 | // each thread makes the transposition for nlt lines (nlt = image_size/nthreads) |
---|
[708] | 185 | // from line [thread_id*nlt] to line [(thread_id + 1)*nlt - 1] |
---|
| 186 | // (p,l) are the absolute pixel coordinates in the source image |
---|
| 187 | |
---|
| 188 | TRSP_START[x_id][y_id][p_id] = giet_proctime(); |
---|
| 189 | |
---|
[712] | 190 | unsigned int nlt = image_size / nthreads; // number of lines per thread |
---|
| 191 | unsigned int nlc = image_size / nclusters; // number of lines per cluster |
---|
[708] | 192 | |
---|
| 193 | unsigned int src_cluster; |
---|
| 194 | unsigned int src_index; |
---|
| 195 | unsigned int dst_cluster; |
---|
| 196 | unsigned int dst_index; |
---|
| 197 | |
---|
| 198 | unsigned char byte; |
---|
| 199 | |
---|
| 200 | unsigned int first = thread_id * nlt; // first line index for a given thread |
---|
| 201 | unsigned int last = first + nlt; // last line index for a given thread |
---|
| 202 | |
---|
| 203 | for ( l = first ; l < last ; l++ ) |
---|
| 204 | { |
---|
| 205 | // in each iteration we transfer one byte |
---|
[712] | 206 | for ( p = 0 ; p < image_size ; p++ ) |
---|
[708] | 207 | { |
---|
| 208 | // read one byte from local buf_in |
---|
| 209 | src_cluster = l / nlc; |
---|
[712] | 210 | src_index = (l % nlc)*image_size + p; |
---|
[708] | 211 | byte = buf_in[src_cluster][src_index]; |
---|
| 212 | |
---|
| 213 | // write one byte to remote buf_out |
---|
| 214 | dst_cluster = p / nlc; |
---|
[712] | 215 | dst_index = (p % nlc)*image_size + l; |
---|
[708] | 216 | buf_out[dst_cluster][dst_index] = byte; |
---|
| 217 | } |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | if ( (p_id == 0) && (x_id==0) && (y_id==0) ) |
---|
| 221 | { |
---|
[764] | 222 | printf("\n[TRANSPOSE] Thread[%d,%d,%d] completes transpose at cycle %d\n", |
---|
[708] | 223 | x_id, y_id, p_id, giet_proctime() ); |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | TRSP_END[x_id][y_id][p_id] = giet_proctime(); |
---|
| 227 | |
---|
| 228 | ///////////////////////////// |
---|
| 229 | sqt_barrier_wait( &barrier ); |
---|
| 230 | ///////////////////////////// |
---|
| 231 | |
---|
| 232 | // parallel display from local buf_out to frame buffer |
---|
| 233 | // all threads contribute to display using memcpy... |
---|
| 234 | |
---|
| 235 | DISP_START[x_id][y_id][p_id] = giet_proctime(); |
---|
| 236 | |
---|
| 237 | unsigned int npt = npixels / nthreads; // number of pixels per thread |
---|
| 238 | |
---|
| 239 | giet_fbf_sync_write( npt * thread_id, |
---|
| 240 | &buf_out[cluster_id][p_id*npt], |
---|
| 241 | npt ); |
---|
| 242 | |
---|
| 243 | if ( (x_id==0) && (y_id==0) && (p_id==0) ) |
---|
| 244 | { |
---|
[764] | 245 | printf("\n[TRANSPOSE] Thread[%d,%d,%d] completes display at cycle %d\n", |
---|
[708] | 246 | x_id, y_id, p_id, giet_proctime() ); |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | DISP_END[x_id][y_id][p_id] = giet_proctime(); |
---|
| 250 | |
---|
| 251 | ///////////////////////////// |
---|
| 252 | sqt_barrier_wait( &barrier ); |
---|
| 253 | ///////////////////////////// |
---|
| 254 | |
---|
[764] | 255 | // all threads, but thread[0,0,0], suicide |
---|
[708] | 256 | if ( (x_id != 0) || (y_id != 0) || (p_id != 0) ) |
---|
| 257 | giet_pthread_exit( "completed" ); |
---|
| 258 | |
---|
| 259 | } // end execute() |
---|
| 260 | |
---|
| 261 | |
---|
| 262 | |
---|
| 263 | ////////////////////////////////////// |
---|
| 264 | void instrument( unsigned int x_size, |
---|
| 265 | unsigned int y_size, |
---|
| 266 | unsigned int nprocs ) |
---|
| 267 | ////////////////////////////////////// |
---|
| 268 | { |
---|
| 269 | unsigned int x, y, l; |
---|
| 270 | |
---|
| 271 | unsigned int min_load_start = 0xFFFFFFFF; |
---|
| 272 | unsigned int max_load_start = 0; |
---|
| 273 | unsigned int min_load_ended = 0xFFFFFFFF; |
---|
| 274 | unsigned int max_load_ended = 0; |
---|
| 275 | unsigned int min_trsp_start = 0xFFFFFFFF; |
---|
| 276 | unsigned int max_trsp_start = 0; |
---|
| 277 | unsigned int min_trsp_ended = 0xFFFFFFFF; |
---|
| 278 | unsigned int max_trsp_ended = 0; |
---|
| 279 | unsigned int min_disp_start = 0xFFFFFFFF; |
---|
| 280 | unsigned int max_disp_start = 0; |
---|
| 281 | unsigned int min_disp_ended = 0xFFFFFFFF; |
---|
| 282 | unsigned int max_disp_ended = 0; |
---|
[769] | 283 | |
---|
[775] | 284 | char string[64]; |
---|
| 285 | |
---|
| 286 | snprintf( string , 64 , "/home/transpose_%d_%d_%d" , x_size , y_size , nprocs ); |
---|
| 287 | |
---|
[769] | 288 | // open instrumentation file |
---|
[778] | 289 | unsigned int fd = giet_fat_open( string , O_CREAT ); |
---|
[769] | 290 | if ( fd < 0 ) |
---|
| 291 | { |
---|
[775] | 292 | printf("\n[TRANSPOSE ERROR] cannot open instrumentation file %s\n", string ); |
---|
[769] | 293 | giet_pthread_exit( NULL ); |
---|
| 294 | } |
---|
[708] | 295 | |
---|
| 296 | for (x = 0; x < x_size; x++) |
---|
| 297 | { |
---|
| 298 | for (y = 0; y < y_size; y++) |
---|
| 299 | { |
---|
| 300 | for ( l = 0 ; l < nprocs ; l++ ) |
---|
| 301 | { |
---|
[764] | 302 | if (MMAP_START[x][y][l] < min_load_start) min_load_start = MMAP_START[x][y][l]; |
---|
| 303 | if (MMAP_START[x][y][l] > max_load_start) max_load_start = MMAP_START[x][y][l]; |
---|
| 304 | if (MMAP_END[x][y][l] < min_load_ended) min_load_ended = MMAP_END[x][y][l]; |
---|
| 305 | if (MMAP_END[x][y][l] > max_load_ended) max_load_ended = MMAP_END[x][y][l]; |
---|
[708] | 306 | if (TRSP_START[x][y][l] < min_trsp_start) min_trsp_start = TRSP_START[x][y][l]; |
---|
| 307 | if (TRSP_START[x][y][l] > max_trsp_start) max_trsp_start = TRSP_START[x][y][l]; |
---|
| 308 | if (TRSP_END[x][y][l] < min_trsp_ended) min_trsp_ended = TRSP_END[x][y][l]; |
---|
| 309 | if (TRSP_END[x][y][l] > max_trsp_ended) max_trsp_ended = TRSP_END[x][y][l]; |
---|
| 310 | if (DISP_START[x][y][l] < min_disp_start) min_disp_start = DISP_START[x][y][l]; |
---|
| 311 | if (DISP_START[x][y][l] > max_disp_start) max_disp_start = DISP_START[x][y][l]; |
---|
| 312 | if (DISP_END[x][y][l] < min_disp_ended) min_disp_ended = DISP_END[x][y][l]; |
---|
| 313 | if (DISP_END[x][y][l] > max_disp_ended) max_disp_ended = DISP_END[x][y][l]; |
---|
| 314 | } |
---|
| 315 | } |
---|
| 316 | } |
---|
| 317 | |
---|
[775] | 318 | giet_tty_printf( "\n ------ %s ------\n" , string ); |
---|
| 319 | giet_fat_fprintf( fd , "\n ------ %s ------\n" , string ); |
---|
[708] | 320 | |
---|
[775] | 321 | giet_tty_printf( " - MMAP_START : min = %d / max = %d / med = %d / delta = %d\n", |
---|
[708] | 322 | min_load_start, max_load_start, (min_load_start+max_load_start)/2, |
---|
[775] | 323 | max_load_start-min_load_start ); |
---|
| 324 | |
---|
[769] | 325 | giet_fat_fprintf( fd , " - MMAP_START : min = %d / max = %d / med = %d / delta = %d\n", |
---|
| 326 | min_load_start, max_load_start, (min_load_start+max_load_start)/2, |
---|
[775] | 327 | max_load_start-min_load_start ); |
---|
[708] | 328 | |
---|
[775] | 329 | giet_tty_printf( " - MMAP_END : min = %d / max = %d / med = %d / delta = %d\n", |
---|
[708] | 330 | min_load_ended, max_load_ended, (min_load_ended+max_load_ended)/2, |
---|
[775] | 331 | max_load_ended-min_load_ended ); |
---|
| 332 | |
---|
[769] | 333 | giet_fat_fprintf( fd , " - MMAP_END : min = %d / max = %d / med = %d / delta = %d\n", |
---|
| 334 | min_load_ended, max_load_ended, (min_load_ended+max_load_ended)/2, |
---|
[775] | 335 | max_load_ended-min_load_ended ); |
---|
[708] | 336 | |
---|
[775] | 337 | giet_tty_printf( " - TRSP_START : min = %d / max = %d / med = %d / delta = %d\n", |
---|
[708] | 338 | min_trsp_start, max_trsp_start, (min_trsp_start+max_trsp_start)/2, |
---|
[775] | 339 | max_trsp_start-min_trsp_start ); |
---|
| 340 | |
---|
[769] | 341 | giet_fat_fprintf( fd , " - TRSP_START : min = %d / max = %d / med = %d / delta = %d\n", |
---|
| 342 | min_trsp_start, max_trsp_start, (min_trsp_start+max_trsp_start)/2, |
---|
[775] | 343 | max_trsp_start-min_trsp_start ); |
---|
[708] | 344 | |
---|
[775] | 345 | giet_tty_printf( " - TRSP_END : min = %d / max = %d / med = %d / delta = %d\n", |
---|
[708] | 346 | min_trsp_ended, max_trsp_ended, (min_trsp_ended+max_trsp_ended)/2, |
---|
[775] | 347 | max_trsp_ended-min_trsp_ended ); |
---|
| 348 | |
---|
[769] | 349 | giet_fat_fprintf( fd , " - TRSP_END : min = %d / max = %d / med = %d / delta = %d\n", |
---|
| 350 | min_trsp_ended, max_trsp_ended, (min_trsp_ended+max_trsp_ended)/2, |
---|
[775] | 351 | max_trsp_ended-min_trsp_ended ); |
---|
[708] | 352 | |
---|
[775] | 353 | giet_tty_printf( " - DISP_START : min = %d / max = %d / med = %d / delta = %d\n", |
---|
[708] | 354 | min_disp_start, max_disp_start, (min_disp_start+max_disp_start)/2, |
---|
[775] | 355 | max_disp_start-min_disp_start ); |
---|
| 356 | |
---|
[769] | 357 | giet_fat_fprintf( fd , " - DISP_START : min = %d / max = %d / med = %d / delta = %d\n", |
---|
| 358 | min_disp_start, max_disp_start, (min_disp_start+max_disp_start)/2, |
---|
[775] | 359 | max_disp_start-min_disp_start ); |
---|
[708] | 360 | |
---|
[775] | 361 | giet_tty_printf( " - DISP_END : min = %d / max = %d / med = %d / delta = %d\n", |
---|
[708] | 362 | min_disp_ended, max_disp_ended, (min_disp_ended+max_disp_ended)/2, |
---|
[775] | 363 | max_disp_ended-min_disp_ended ); |
---|
| 364 | |
---|
[769] | 365 | giet_fat_fprintf( fd , " - DISP_END : min = %d / max = %d / med = %d / delta = %d\n", |
---|
| 366 | min_disp_ended, max_disp_ended, (min_disp_ended+max_disp_ended)/2, |
---|
[775] | 367 | max_disp_ended-min_disp_ended ); |
---|
[708] | 368 | |
---|
[769] | 369 | giet_fat_close( fd ); |
---|
| 370 | |
---|
[708] | 371 | } // end instrument() |
---|
| 372 | |
---|
| 373 | |
---|
| 374 | |
---|
| 375 | ////////////////////////////////////////// |
---|
| 376 | __attribute__ ((constructor)) void main() |
---|
| 377 | ////////////////////////////////////////// |
---|
| 378 | { |
---|
| 379 | // indexes for loops |
---|
| 380 | unsigned int x , y , n; |
---|
| 381 | |
---|
| 382 | // get identifiers for proc executing main |
---|
| 383 | unsigned int x_id; // x cluster coordinate |
---|
| 384 | unsigned int y_id; // y cluster coordinate |
---|
| 385 | unsigned int p_id; // local processor index |
---|
| 386 | giet_proc_xyp( &x_id , &y_id , &p_id ); |
---|
| 387 | |
---|
| 388 | // get & check plat-form parameters |
---|
| 389 | unsigned int x_size; // number of clusters in a row |
---|
| 390 | unsigned int y_size; // number of clusters in a column |
---|
| 391 | unsigned int nprocs; // number of processors per cluster |
---|
| 392 | giet_procs_number( &x_size , &y_size , &nprocs ); |
---|
| 393 | |
---|
| 394 | giet_pthread_assert( ((nprocs == 1) || (nprocs == 2) || (nprocs == 4)), |
---|
| 395 | "[TRANSPOSE ERROR] number of procs per cluster must be 1, 2 or 4"); |
---|
| 396 | |
---|
| 397 | giet_pthread_assert( ((x_size == 1) || (x_size == 2) || (x_size == 4) || |
---|
[764] | 398 | (x_size == 8) || (x_size == 16)), |
---|
[708] | 399 | "[TRANSPOSE ERROR] x_size must be 1,2,4,8,16"); |
---|
| 400 | |
---|
| 401 | giet_pthread_assert( ((y_size == 1) || (y_size == 2) || (y_size == 4) || |
---|
[764] | 402 | (y_size == 8) || (y_size == 16)), |
---|
[708] | 403 | "[TRANSPOSE ERROR] y_size must be 1,2,4,8,16"); |
---|
| 404 | |
---|
[712] | 405 | // compute number of threads |
---|
[708] | 406 | unsigned int nthreads = x_size * y_size * nprocs; |
---|
| 407 | |
---|
| 408 | // shared TTY allocation |
---|
| 409 | giet_tty_alloc( 1 ); |
---|
| 410 | lock_init( &tty_lock); |
---|
| 411 | |
---|
[712] | 412 | // get FBF ownership and FBF size |
---|
| 413 | unsigned int width; |
---|
| 414 | unsigned int height; |
---|
| 415 | giet_fbf_alloc(); |
---|
| 416 | giet_fbf_size( &width , &height ); |
---|
[708] | 417 | |
---|
[712] | 418 | printf("\n[TRANSPOSE] start at cycle %d on %d cores / FBF = %d * %d pixels\n", |
---|
| 419 | giet_proctime(), nthreads , width , height ); |
---|
| 420 | |
---|
[764] | 421 | if ( INTERACTIVE ) // input_file_name, output_file_name, and size acquisition |
---|
| 422 | { |
---|
| 423 | printf("\n[TRANSPOSE] enter path for input file / default is : %s\n> ", INPUT_FILE_PATH ); |
---|
| 424 | giet_tty_gets( input_file_name , 256 ); |
---|
| 425 | printf("\n"); |
---|
| 426 | if ( strcmp( input_file_name , "" ) == 0 ) strcpy( input_file_name , INPUT_FILE_PATH ); |
---|
[712] | 427 | |
---|
[764] | 428 | printf("\n[TRANSPOSE] enter path for output file / default is : %s\n> ", OUTPUT_FILE_PATH ); |
---|
| 429 | giet_tty_gets( output_file_name , 256 ); |
---|
| 430 | printf("\n"); |
---|
| 431 | if ( strcmp( output_file_name , "" ) == 0 ) strcpy( output_file_name , OUTPUT_FILE_PATH ); |
---|
[712] | 432 | |
---|
[764] | 433 | printf("\n[TRANSPOSE] enter image size / default is : %d\n> ", IMAGE_SIZE ); |
---|
| 434 | giet_tty_getw( &image_size ); |
---|
| 435 | printf("\n"); |
---|
| 436 | if ( image_size == 0 ) image_size = IMAGE_SIZE; |
---|
| 437 | } |
---|
| 438 | else |
---|
| 439 | { |
---|
| 440 | strcpy( input_file_name , INPUT_FILE_PATH ); |
---|
| 441 | strcpy( output_file_name , OUTPUT_FILE_PATH ); |
---|
| 442 | image_size = IMAGE_SIZE; |
---|
| 443 | } |
---|
[712] | 444 | |
---|
[764] | 445 | // check image size / number of clusters |
---|
| 446 | giet_pthread_assert( ((((image_size * image_size) / (x_size * y_size)) & 0xFFF) == 0) , |
---|
| 447 | "[TRANSPOSE ERROR] pixels per cluster must be multiple of 4096"); |
---|
| 448 | |
---|
[712] | 449 | printf("\n[TRANSPOSE] input = %s / output = %s / size = %d\n", |
---|
| 450 | input_file_name, output_file_name, image_size ); |
---|
| 451 | |
---|
[708] | 452 | // distributed heap initialisation |
---|
| 453 | for ( x = 0 ; x < x_size ; x++ ) |
---|
| 454 | { |
---|
| 455 | for ( y = 0 ; y < y_size ; y++ ) |
---|
| 456 | { |
---|
| 457 | heap_init( x , y ); |
---|
| 458 | } |
---|
| 459 | } |
---|
| 460 | |
---|
[764] | 461 | // open input and output files |
---|
| 462 | fd_in = giet_fat_open( input_file_name , O_RDONLY ); // read_only |
---|
| 463 | if ( fd_in < 0 ) |
---|
| 464 | { |
---|
| 465 | printf("\n[TRANSPOSE ERROR] main cannot open file %s\n", input_file_name ); |
---|
| 466 | giet_pthread_exit( NULL ); |
---|
| 467 | } |
---|
| 468 | else |
---|
| 469 | { |
---|
| 470 | printf("\n[TRANSPOSE] main open file %s / fd = %d\n", input_file_name , fd_in ); |
---|
| 471 | } |
---|
| 472 | |
---|
[778] | 473 | fd_out = giet_fat_open( output_file_name , O_CREAT ); // create if required |
---|
[764] | 474 | if ( fd_out < 0 ) |
---|
| 475 | { |
---|
| 476 | printf("\n[TRANSPOSE ERROR] main cannot open file %s\n", output_file_name ); |
---|
| 477 | giet_pthread_exit(" open() failure"); |
---|
| 478 | } |
---|
| 479 | else |
---|
| 480 | { |
---|
| 481 | printf("\n[TRANSPOSE] main open file %s / fd = %d\n", output_file_name , fd_out ); |
---|
| 482 | } |
---|
| 483 | |
---|
[708] | 484 | // allocate thread[] array |
---|
| 485 | pthread_t* thread = malloc( nthreads * sizeof(pthread_t) ); |
---|
| 486 | |
---|
| 487 | // barrier initialisation |
---|
| 488 | sqt_barrier_init( &barrier, x_size , y_size , nprocs ); |
---|
| 489 | |
---|
| 490 | // Initialisation completed |
---|
[764] | 491 | printf("\n[TRANSPOSE] main completes initialisation\n"); |
---|
[708] | 492 | |
---|
| 493 | // launch other threads to run execute() function |
---|
| 494 | for ( n = 1 ; n < nthreads ; n++ ) |
---|
| 495 | { |
---|
| 496 | if ( giet_pthread_create( &thread[n], |
---|
| 497 | NULL, // no attribute |
---|
| 498 | &execute, |
---|
| 499 | NULL ) ) // no argument |
---|
| 500 | { |
---|
| 501 | printf("\n[TRANSPOSE ERROR] creating thread %x\n", thread[n] ); |
---|
| 502 | giet_pthread_exit( NULL ); |
---|
| 503 | } |
---|
| 504 | } |
---|
| 505 | |
---|
| 506 | // run the execute() function |
---|
| 507 | execute(); |
---|
| 508 | |
---|
| 509 | // wait other threads completion |
---|
| 510 | for ( n = 1 ; n < nthreads ; n++ ) |
---|
| 511 | { |
---|
| 512 | if ( giet_pthread_join( thread[n], NULL ) ) |
---|
| 513 | { |
---|
| 514 | printf("\n[TRANSPOSE ERROR] joining thread %x\n", thread[n] ); |
---|
| 515 | giet_pthread_exit( NULL ); |
---|
| 516 | } |
---|
| 517 | else |
---|
| 518 | { |
---|
| 519 | printf("\n[TRANSPOSE] thread %x joined at cycle %d\n", |
---|
| 520 | thread[n] , giet_proctime() ); |
---|
| 521 | } |
---|
| 522 | } |
---|
| 523 | |
---|
| 524 | // call the instrument() function |
---|
| 525 | instrument( x_size , y_size , nprocs ); |
---|
| 526 | |
---|
[764] | 527 | // close input and output files |
---|
| 528 | giet_fat_close( fd_in ); |
---|
| 529 | giet_fat_close( fd_out ); |
---|
| 530 | |
---|
| 531 | // suicide |
---|
[708] | 532 | giet_pthread_exit( "completed" ); |
---|
| 533 | |
---|
| 534 | } // end main() |
---|
| 535 | |
---|