Changeset 502 for soft/giet_vm/applications/gameoflife/main.c
- Timestamp:
- Feb 8, 2015, 9:20:45 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
soft/giet_vm/applications/gameoflife/main.c
r444 r502 1 /* 2 * This application is an emulation of the game of life automaton 3 * It must be deployed from processor 0 and use contiguous processor 4 * (example 0,1,2,3) 5 */ 6 1 ////////////////////////////////////////////////////////////////////////////////// 2 // File : main.c (for gameoflife) 3 // Date : November 2013 4 // Author : Alexandre Joannou <alexandre.joannou@lip6.fr> 5 // 6 // This application is an emulation of the game of life automaton. 7 // The world size is defined by the HEIGHT and WIDTH parameters. 8 // There is one task per processor, and each task compute HEIGHT/nbprocs lines. 9 // The number of processors must be a power of 2 not larger than HEIGHT. 10 ////////////////////////////////////////////////////////////////////////////////// 7 11 8 12 #include "stdio.h" 9 13 #include "limits.h" 10 #include "barrier.h" 11 #include "hard_config.h" 14 #include "user_barrier.h" 12 15 #include "mapping_info.h" 13 16 14 17 #define WIDTH 128 15 18 #define HEIGHT 128 16 #define NB_CLUSTER_MAX 25617 19 #define NB_ITERATION 1000000000 18 20 19 #define PRINTF(...) ({ if ( proc_id==0) { giet_tty_printf(__VA_ARGS__); } }) 20 21 giet_barrier_t barriers[2]; 22 23 unsigned int init_ok = 1; 24 25 #define NEW 0 26 #define OLD 1 21 #define PRINTF(...) ({ if ( proc_id==0) { giet_shr_printf(__VA_ARGS__); } }) 22 23 giet_sqt_barrier_t barrier; 24 25 unsigned int init_ok = 0; 26 27 #define OLD 0 28 #define NEW 1 29 #define DSP 2 27 30 28 31 typedef unsigned char uint8_t; 29 32 typedef unsigned int size_t; 30 33 31 uint8_t world[2][HEIGHT][WIDTH]; 32 uint8_t world_yuv[HEIGHT][WIDTH]; 33 34 /* Generate binary values for world between base_line and base_line + nb_line */ 34 uint8_t world[3][HEIGHT][WIDTH]; 35 36 ///////////////////////////////////////////////// 35 37 void init_world(size_t base_line, size_t nb_line) 36 38 { 37 39 size_t x,y; 38 for (y = base_line ; y < base_line + nb_line; y++){ 39 for(x = 0; x < WIDTH ; x++) { 40 // TODO OPTIMIZE RANDOM INIT 40 for (y = base_line ; y < base_line + nb_line; y++) 41 { 42 for(x = 0; x < WIDTH ; x++) 43 { 41 44 world[OLD][y][x] = giet_rand() % 2; 42 45 } … … 44 47 } 45 48 49 ///////////////////////////////////////////////// 46 50 uint8_t number_of_alive_neigh(size_t x, size_t y) 47 51 { … … 60 64 } 61 65 62 / * Compute cell x,y */66 ///////////////////////////////////////////////// 63 67 uint8_t compute_cell(size_t x, size_t y) 64 68 { 65 69 uint8_t nb_neighbours_alive = number_of_alive_neigh(x,y); 66 if (world[OLD][y][x] == 1) { 67 if (nb_neighbours_alive == 2 || 68 nb_neighbours_alive == 3) 69 { 70 return 1; 71 } 72 } 73 else { 74 if (nb_neighbours_alive == 3) { 75 return 1; 76 } 77 else { 78 return world[OLD][y][x]; 79 } 70 if (world[OLD][y][x] == 1) 71 { 72 if (nb_neighbours_alive == 2 || nb_neighbours_alive == 3) return 1; 73 } 74 else 75 { 76 if (nb_neighbours_alive == 3) return 1; 77 else return world[OLD][y][x]; 80 78 } 81 79 return 0; … … 99 97 { 100 98 size_t x,y; 101 for (y = base_line; y < base_line + nb_line; y++){ 102 for(x = 0; x < WIDTH ; x++) { 103 //world_yuv[y][x] = world[NEW][y][x]*100; 104 world[NEW][y][x] = world[NEW][y][x]*255; 99 for (y = base_line; y < base_line + nb_line; y++) 100 { 101 for(x = 0; x < WIDTH ; x++) 102 { 103 world[DSP][y][x] = world[OLD][y][x]*255; 105 104 } 106 105 } 107 106 108 107 giet_fbf_sync_write( base_line * WIDTH , 109 &world[NEW][base_line][0],110 nb_line * WIDTH);108 &world[DSP][base_line][0], 109 nb_line * WIDTH ); 111 110 } 112 111 … … 133 132 giet_proc_xyp( &x, &y, &p ); 134 133 134 // get processors number 135 unsigned int x_size; 136 unsigned int y_size; 137 unsigned int n_local_procs; 138 giet_procs_number( &x_size, &y_size, &n_local_procs ); 139 135 140 // compute continuous processor index 136 unsigned int proc_id = (((x * Y_SIZE) + y) * NB_PROCS_MAX) + p; 137 138 unsigned int nlocal_procs = NB_PROCS_MAX; // processors per cluster 139 unsigned int nclusters = X_SIZE*Y_SIZE; // number of clusters 140 unsigned int nglobal_procs = nclusters * nlocal_procs; // number of processors 141 unsigned int proc_id = (((x * y_size) + y) * n_local_procs) + p; 142 143 unsigned int n_clusters = x_size * y_size; // number of clusters 144 unsigned int n_global_procs = n_clusters * n_local_procs; // number of processors 141 145 size_t i; 142 146 143 size_t nb_line = HEIGHT / nglobal_procs; 147 if ( n_global_procs > HEIGHT ) 148 { 149 PRINTF("[GAMEOFLIFE ERROR] Number or processors too large :" 150 " nb_procs = %d / image heigth = %d\n", n_global_procs, HEIGHT ); 151 giet_exit("error"); 152 } 153 154 size_t nb_line = HEIGHT / n_global_procs; 144 155 size_t base_line = nb_line * proc_id; 145 156 146 PRINTF("*** Starting init at cycle %d ***\n", giet_proctime()); 147 148 // barriers initialization 157 PRINTF("\n*** Starting barrier initialisation at cycle %d ***\n" 158 " nprocs = %d / nlines = %d\n", 159 giet_proctime() , n_global_procs, HEIGHT ); 160 161 // barrier initialization 149 162 if ( proc_id == 0 ) 150 163 { 151 barrier_init(&barriers[0], nglobal_procs); 152 barrier_init(&barriers[1], nglobal_procs); 153 154 init_ok = 0; 164 sqt_barrier_init( &barrier , x_size , y_size , n_local_procs ); 165 init_ok = 1; 155 166 } 156 167 else 157 168 { 158 while ( init_ok == 1 ); 159 } 160 161 init_world(base_line, nb_line); 162 163 PRINTF("*** Completing init at cycle %d ***\n", giet_proctime()); 164 barrier_wait(&barriers[0]); 169 while ( init_ok == 0 ) asm volatile("nop"); 170 } 171 172 PRINTF("\n*** Starting world initialisation at cycle %d ***\n", 173 giet_proctime() ); 174 175 // parallel world initialization 176 init_world( base_line , nb_line ); 177 178 PRINTF("coucou 0\n"); 179 180 display_world( base_line , nb_line ); 181 182 PRINTF("coucou 1\n"); 183 184 sqt_barrier_wait( &barrier ); 185 186 PRINTF("\n*** Starting life at cycle %d ***\n", 187 giet_proctime() ); 165 188 166 189 for (i = 0; i < NB_ITERATION; i++) 167 190 { 168 compute_new_gen(base_line, nb_line); 169 grow_old_world(base_line, nb_line); 170 display_world(base_line, nb_line); 171 barrier_wait(&barriers[1]); 172 barrier_init(&barriers[1], nglobal_procs); 173 } 174 175 PRINTF("*** End of main at cycle %d ***\n", giet_proctime()); 191 compute_new_gen( base_line, nb_line ); 192 grow_old_world( base_line, nb_line ); 193 display_world( base_line, nb_line ); 194 195 sqt_barrier_wait( &barrier ); 196 197 PRINTF(" - iteration %d completed\n", i ); 198 } 199 200 PRINTF("\n*** End of main at cycle %d ***\n", giet_proctime()); 176 201 177 202 giet_exit("Completed");
Note: See TracChangeset
for help on using the changeset viewer.