[502] | 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 | ////////////////////////////////////////////////////////////////////////////////// |
---|
[251] | 11 | |
---|
| 12 | #include "stdio.h" |
---|
| 13 | #include "limits.h" |
---|
[502] | 14 | #include "user_barrier.h" |
---|
[251] | 15 | #include "mapping_info.h" |
---|
| 16 | |
---|
| 17 | #define WIDTH 128 |
---|
| 18 | #define HEIGHT 128 |
---|
| 19 | #define NB_ITERATION 1000000000 |
---|
| 20 | |
---|
[502] | 21 | #define PRINTF(...) ({ if ( proc_id==0) { giet_shr_printf(__VA_ARGS__); } }) |
---|
[251] | 22 | |
---|
[502] | 23 | giet_sqt_barrier_t barrier; |
---|
[251] | 24 | |
---|
[502] | 25 | unsigned int init_ok = 0; |
---|
[295] | 26 | |
---|
[502] | 27 | #define OLD 0 |
---|
| 28 | #define NEW 1 |
---|
| 29 | #define DSP 2 |
---|
[251] | 30 | |
---|
| 31 | typedef unsigned char uint8_t; |
---|
| 32 | typedef unsigned int size_t; |
---|
| 33 | |
---|
[502] | 34 | uint8_t world[3][HEIGHT][WIDTH]; |
---|
[251] | 35 | |
---|
[502] | 36 | ///////////////////////////////////////////////// |
---|
[251] | 37 | void init_world(size_t base_line, size_t nb_line) |
---|
| 38 | { |
---|
| 39 | size_t x,y; |
---|
[502] | 40 | for (y = base_line ; y < base_line + nb_line; y++) |
---|
| 41 | { |
---|
| 42 | for(x = 0; x < WIDTH ; x++) |
---|
| 43 | { |
---|
[251] | 44 | world[OLD][y][x] = giet_rand() % 2; |
---|
| 45 | } |
---|
| 46 | } |
---|
| 47 | } |
---|
| 48 | |
---|
[502] | 49 | ///////////////////////////////////////////////// |
---|
[251] | 50 | uint8_t number_of_alive_neigh(size_t x, size_t y) |
---|
| 51 | { |
---|
| 52 | uint8_t nb = 0; |
---|
| 53 | |
---|
| 54 | nb += world[OLD][(y - 1) % HEIGHT][(x - 1) % WIDTH]; |
---|
| 55 | nb += world[OLD][ y ][(x - 1) % WIDTH]; |
---|
| 56 | nb += world[OLD][(y + 1) % HEIGHT][(x - 1) % WIDTH]; |
---|
| 57 | nb += world[OLD][(y - 1) % HEIGHT][ x ]; |
---|
| 58 | nb += world[OLD][(y + 1) % HEIGHT][ x ]; |
---|
| 59 | nb += world[OLD][(y - 1) % HEIGHT][(x + 1) % WIDTH]; |
---|
| 60 | nb += world[OLD][ y ][(x + 1) % WIDTH]; |
---|
| 61 | nb += world[OLD][(y + 1) % HEIGHT][(x + 1) % WIDTH]; |
---|
| 62 | |
---|
| 63 | return nb; |
---|
| 64 | } |
---|
| 65 | |
---|
[502] | 66 | ///////////////////////////////////////////////// |
---|
[251] | 67 | uint8_t compute_cell(size_t x, size_t y) |
---|
| 68 | { |
---|
| 69 | uint8_t nb_neighbours_alive = number_of_alive_neigh(x,y); |
---|
[502] | 70 | if (world[OLD][y][x] == 1) |
---|
| 71 | { |
---|
| 72 | if (nb_neighbours_alive == 2 || nb_neighbours_alive == 3) return 1; |
---|
[251] | 73 | } |
---|
[502] | 74 | else |
---|
| 75 | { |
---|
| 76 | if (nb_neighbours_alive == 3) return 1; |
---|
| 77 | else return world[OLD][y][x]; |
---|
[251] | 78 | } |
---|
| 79 | return 0; |
---|
| 80 | } |
---|
| 81 | |
---|
[295] | 82 | ////////////////////////////////////////////////////// |
---|
[251] | 83 | void compute_new_gen(size_t base_line, size_t nb_line) |
---|
| 84 | { |
---|
| 85 | size_t x,y; |
---|
[295] | 86 | for (y = base_line; y < base_line + nb_line; y++) |
---|
| 87 | { |
---|
| 88 | for(x = 0; x < WIDTH ; x++) |
---|
| 89 | { |
---|
[251] | 90 | world[NEW][y][x] = compute_cell(x,y); |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
[295] | 95 | //////////////////////////////////////////////////// |
---|
[251] | 96 | void display_world(size_t base_line, size_t nb_line) |
---|
| 97 | { |
---|
| 98 | size_t x,y; |
---|
[502] | 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; |
---|
[251] | 104 | } |
---|
| 105 | } |
---|
| 106 | |
---|
[444] | 107 | giet_fbf_sync_write( base_line * WIDTH , |
---|
[502] | 108 | &world[DSP][base_line][0], |
---|
| 109 | nb_line * WIDTH ); |
---|
[251] | 110 | } |
---|
| 111 | |
---|
[295] | 112 | ///////////////////////////////////////////////////// |
---|
[251] | 113 | void grow_old_world(size_t base_line, size_t nb_line) |
---|
| 114 | { |
---|
| 115 | size_t x,y; |
---|
[295] | 116 | for (y = base_line; y < base_line + nb_line; y++) |
---|
| 117 | { |
---|
| 118 | for(x = 0; x < WIDTH ; x++) |
---|
| 119 | { |
---|
[251] | 120 | world[OLD][y][x] = world[NEW][y][x]; |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | |
---|
[263] | 125 | //////////////////////////////////////// |
---|
[251] | 126 | __attribute__((constructor)) void main() |
---|
| 127 | { |
---|
[432] | 128 | // get processor identifier |
---|
| 129 | unsigned int x; |
---|
| 130 | unsigned int y; |
---|
| 131 | unsigned int p; |
---|
| 132 | giet_proc_xyp( &x, &y, &p ); |
---|
| 133 | |
---|
[502] | 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 | |
---|
[432] | 140 | // compute continuous processor index |
---|
[502] | 141 | unsigned int proc_id = (((x * y_size) + y) * n_local_procs) + p; |
---|
[432] | 142 | |
---|
[502] | 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 |
---|
[251] | 145 | size_t i; |
---|
| 146 | |
---|
[502] | 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; |
---|
[263] | 155 | size_t base_line = nb_line * proc_id; |
---|
[251] | 156 | |
---|
[502] | 157 | PRINTF("\n*** Starting barrier initialisation at cycle %d ***\n" |
---|
| 158 | " nprocs = %d / nlines = %d\n", |
---|
| 159 | giet_proctime() , n_global_procs, HEIGHT ); |
---|
[251] | 160 | |
---|
[502] | 161 | // barrier initialization |
---|
[295] | 162 | if ( proc_id == 0 ) |
---|
| 163 | { |
---|
[502] | 164 | sqt_barrier_init( &barrier , x_size , y_size , n_local_procs ); |
---|
| 165 | init_ok = 1; |
---|
[295] | 166 | } |
---|
| 167 | else |
---|
| 168 | { |
---|
[502] | 169 | while ( init_ok == 0 ) asm volatile("nop"); |
---|
[295] | 170 | } |
---|
| 171 | |
---|
[502] | 172 | PRINTF("\n*** Starting world initialisation at cycle %d ***\n", |
---|
| 173 | giet_proctime() ); |
---|
[251] | 174 | |
---|
[502] | 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() ); |
---|
[251] | 188 | |
---|
| 189 | for (i = 0; i < NB_ITERATION; i++) |
---|
| 190 | { |
---|
[502] | 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 ); |
---|
[251] | 198 | } |
---|
| 199 | |
---|
[502] | 200 | PRINTF("\n*** End of main at cycle %d ***\n", giet_proctime()); |
---|
[251] | 201 | |
---|
[388] | 202 | giet_exit("Completed"); |
---|
[251] | 203 | } // end main() |
---|
| 204 | |
---|
| 205 | // Local Variables: |
---|
| 206 | // tab-width: 3 |
---|
| 207 | // c-basic-offset: 3 |
---|
| 208 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
| 209 | // indent-tabs-mode: nil |
---|
| 210 | // End: |
---|
| 211 | |
---|
| 212 | // vim: filetype=cpp:expandtab:shiftwidth=3:tabstop=3:softtabstop=3 |
---|
| 213 | |
---|
| 214 | |
---|
| 215 | |
---|