| [251] | 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 | |
|---|
| 7 | |
|---|
| 8 | #include "stdio.h" |
|---|
| 9 | #include "limits.h" |
|---|
| 10 | #include "barrier.h" |
|---|
| 11 | #include "hard_config.h" |
|---|
| 12 | #include "mapping_info.h" |
|---|
| 13 | |
|---|
| 14 | #define WIDTH 128 |
|---|
| 15 | #define HEIGHT 128 |
|---|
| 16 | #define NB_CLUSTER_MAX 256 |
|---|
| 17 | #define NB_ITERATION 1000000000 |
|---|
| 18 | |
|---|
| 19 | #define PRINTF(...) ({ if (giet_procid() == 0) { giet_tty_printf(__VA_ARGS__); } }) |
|---|
| 20 | |
|---|
| 21 | giet_barrier_t barriers[2]; |
|---|
| 22 | |
|---|
| 23 | #define NEW 0 |
|---|
| 24 | #define OLD 1 |
|---|
| 25 | |
|---|
| 26 | typedef unsigned char uint8_t; |
|---|
| 27 | typedef unsigned int size_t; |
|---|
| 28 | |
|---|
| 29 | uint8_t world[2][HEIGHT][WIDTH]; |
|---|
| 30 | uint8_t world_yuv[HEIGHT][WIDTH]; |
|---|
| 31 | |
|---|
| 32 | /* Generate binary values for world between base_line and base_line + nb_line */ |
|---|
| 33 | void init_world(size_t base_line, size_t nb_line) |
|---|
| 34 | { |
|---|
| 35 | size_t x,y; |
|---|
| 36 | for (y = base_line ; y < base_line + nb_line; y++){ |
|---|
| 37 | for(x = 0; x < WIDTH ; x++) { |
|---|
| 38 | // TODO OPTIMIZE RANDOM INIT |
|---|
| 39 | world[OLD][y][x] = giet_rand() % 2; |
|---|
| 40 | } |
|---|
| 41 | } |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | uint8_t number_of_alive_neigh(size_t x, size_t y) |
|---|
| 45 | { |
|---|
| 46 | uint8_t nb = 0; |
|---|
| 47 | |
|---|
| 48 | nb += world[OLD][(y - 1) % HEIGHT][(x - 1) % WIDTH]; |
|---|
| 49 | nb += world[OLD][ y ][(x - 1) % WIDTH]; |
|---|
| 50 | nb += world[OLD][(y + 1) % HEIGHT][(x - 1) % WIDTH]; |
|---|
| 51 | nb += world[OLD][(y - 1) % HEIGHT][ x ]; |
|---|
| 52 | nb += world[OLD][(y + 1) % HEIGHT][ x ]; |
|---|
| 53 | nb += world[OLD][(y - 1) % HEIGHT][(x + 1) % WIDTH]; |
|---|
| 54 | nb += world[OLD][ y ][(x + 1) % WIDTH]; |
|---|
| 55 | nb += world[OLD][(y + 1) % HEIGHT][(x + 1) % WIDTH]; |
|---|
| 56 | |
|---|
| 57 | return nb; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | /* Compute cell x,y */ |
|---|
| 61 | uint8_t compute_cell(size_t x, size_t y) |
|---|
| 62 | { |
|---|
| 63 | uint8_t nb_neighbours_alive = number_of_alive_neigh(x,y); |
|---|
| 64 | if (world[OLD][y][x] == 1) { |
|---|
| 65 | if (nb_neighbours_alive == 2 || |
|---|
| 66 | nb_neighbours_alive == 3) |
|---|
| 67 | { |
|---|
| 68 | return 1; |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | else { |
|---|
| 72 | if (nb_neighbours_alive == 3) { |
|---|
| 73 | return 1; |
|---|
| 74 | } |
|---|
| 75 | else { |
|---|
| 76 | return world[OLD][y][x]; |
|---|
| 77 | } |
|---|
| 78 | } |
|---|
| 79 | return 0; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | void compute_new_gen(size_t base_line, size_t nb_line) |
|---|
| 83 | { |
|---|
| 84 | size_t x,y; |
|---|
| 85 | for (y = base_line; y < base_line + nb_line; y++){ |
|---|
| 86 | for(x = 0; x < WIDTH ; x++) { |
|---|
| 87 | world[NEW][y][x] = compute_cell(x,y); |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | void display_world(size_t base_line, size_t nb_line) |
|---|
| 93 | { |
|---|
| 94 | size_t x,y; |
|---|
| 95 | for (y = base_line; y < base_line + nb_line; y++){ |
|---|
| 96 | for(x = 0; x < WIDTH ; x++) { |
|---|
| 97 | //world_yuv[y][x] = world[NEW][y][x]*100; |
|---|
| 98 | world[NEW][y][x] = world[NEW][y][x]*255; |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | if (giet_fb_sync_write(base_line * WIDTH , &world[NEW][base_line][0], nb_line * WIDTH)) |
|---|
| 103 | { |
|---|
| 104 | PRINTF("Echec fb_sync_write\n"); |
|---|
| 105 | giet_exit(); |
|---|
| 106 | } |
|---|
| 107 | // TODO COLOR ! |
|---|
| 108 | /*if (giet_fb_sync_write(base_line * WIDTH + WIDTH*HEIGHT , &world_yuv[base_line][0], nb_line * WIDTH)) |
|---|
| 109 | { |
|---|
| 110 | PRINTF("Echec fb_sync_write\n"); |
|---|
| 111 | giet_exit(); |
|---|
| 112 | }*/ |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | void grow_old_world(size_t base_line, size_t nb_line) |
|---|
| 116 | { |
|---|
| 117 | size_t x,y; |
|---|
| 118 | for (y = base_line; y < base_line + nb_line; y++){ |
|---|
| 119 | for(x = 0; x < WIDTH ; x++) { |
|---|
| 120 | world[OLD][y][x] = world[NEW][y][x]; |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| [263] | 127 | //////////////////////////////////////// |
|---|
| [251] | 128 | __attribute__((constructor)) void main() |
|---|
| 129 | { |
|---|
| [263] | 130 | unsigned int proc_id = giet_procid(); // processor id |
|---|
| 131 | unsigned int nlocal_procs = NB_PROCS_MAX; // processors per cluster |
|---|
| 132 | unsigned int nclusters = X_SIZE*Y_SIZE; // number of clusters |
|---|
| 133 | unsigned int local_id = proc_id % nlocal_procs; // local processor id |
|---|
| 134 | unsigned int cluster_id = proc_id / nlocal_procs; // cluster id |
|---|
| 135 | unsigned int nglobal_procs = nclusters * nlocal_procs; // number of tasks |
|---|
| [251] | 136 | size_t i; |
|---|
| 137 | |
|---|
| [263] | 138 | size_t nb_line = HEIGHT / nglobal_procs; |
|---|
| 139 | size_t base_line = nb_line * proc_id; |
|---|
| [251] | 140 | |
|---|
| 141 | PRINTF("*** Starting init at cycle %d ***\n", giet_proctime()); |
|---|
| 142 | |
|---|
| 143 | // barriers initialization |
|---|
| 144 | barrier_init(&barriers[0], nglobal_procs); |
|---|
| 145 | barrier_init(&barriers[1], nglobal_procs); |
|---|
| 146 | init_world(base_line, nb_line); |
|---|
| 147 | |
|---|
| 148 | PRINTF("*** Completing init at cycle %d ***\n", giet_proctime()); |
|---|
| 149 | barrier_wait(&barriers[0]); |
|---|
| 150 | |
|---|
| 151 | for (i = 0; i < NB_ITERATION; i++) |
|---|
| 152 | { |
|---|
| 153 | //PRINTF("\n*** Starting computation for iteration %d at cycle %d\n", i, giet_proctime()); |
|---|
| 154 | compute_new_gen(base_line, nb_line); |
|---|
| 155 | //PRINTF("\n*** Starting display for iteration %d at cycle %d\n", i, giet_proctime()); |
|---|
| 156 | grow_old_world(base_line, nb_line); |
|---|
| 157 | display_world(base_line, nb_line); |
|---|
| 158 | barrier_wait(&barriers[1]); |
|---|
| 159 | barrier_init(&barriers[1], nglobal_procs); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | PRINTF("*** End of main at cycle %d ***\n", giet_proctime()); |
|---|
| 163 | |
|---|
| 164 | giet_exit(); |
|---|
| 165 | } // end main() |
|---|
| 166 | |
|---|
| 167 | // Local Variables: |
|---|
| 168 | // tab-width: 3 |
|---|
| 169 | // c-basic-offset: 3 |
|---|
| 170 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
|---|
| 171 | // indent-tabs-mode: nil |
|---|
| 172 | // End: |
|---|
| 173 | |
|---|
| 174 | // vim: filetype=cpp:expandtab:shiftwidth=3:tabstop=3:softtabstop=3 |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | |
|---|