source: soft/giet_vm/gameoflife/main.c @ 395

Last change on this file since 395 was 388, checked in by alain, 10 years ago

Cosmetic

File size: 4.7 KB
Line 
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
21giet_barrier_t barriers[2];
22
23unsigned int init_ok = 1;
24
25#define NEW 0
26#define OLD 1
27
28typedef unsigned char uint8_t;
29typedef unsigned int size_t;
30
31uint8_t world[2][HEIGHT][WIDTH];
32uint8_t world_yuv[HEIGHT][WIDTH];
33
34/* Generate binary values for world between base_line and base_line + nb_line */
35void init_world(size_t base_line, size_t nb_line)
36{
37   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
41         world[OLD][y][x] = giet_rand() % 2; 
42      }
43   }
44}
45
46uint8_t number_of_alive_neigh(size_t x, size_t y)
47{
48   uint8_t nb = 0;
49
50   nb += world[OLD][(y - 1) % HEIGHT][(x - 1) % WIDTH];
51   nb += world[OLD][ y              ][(x - 1) % WIDTH];
52   nb += world[OLD][(y + 1) % HEIGHT][(x - 1) % WIDTH];
53   nb += world[OLD][(y - 1) % HEIGHT][ x             ];
54   nb += world[OLD][(y + 1) % HEIGHT][ x             ];
55   nb += world[OLD][(y - 1) % HEIGHT][(x + 1) % WIDTH];
56   nb += world[OLD][ y              ][(x + 1) % WIDTH];
57   nb += world[OLD][(y + 1) % HEIGHT][(x + 1) % WIDTH];
58
59   return nb;
60}
61
62/* Compute cell x,y */
63uint8_t compute_cell(size_t x, size_t y)
64{
65   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      }
80   }
81   return 0;
82}
83
84//////////////////////////////////////////////////////
85void compute_new_gen(size_t base_line, size_t nb_line)
86{
87   size_t x,y;
88   for (y = base_line; y < base_line + nb_line; y++)
89   {
90      for(x = 0; x < WIDTH ; x++) 
91      {
92         world[NEW][y][x] = compute_cell(x,y); 
93      }
94   }
95}
96
97////////////////////////////////////////////////////
98void display_world(size_t base_line, size_t nb_line)
99{
100   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; 
105      }
106   }
107
108   giet_fb_sync_write( base_line * WIDTH , 
109                       &world[NEW][base_line][0], 
110                       nb_line * WIDTH);
111}
112
113/////////////////////////////////////////////////////
114void grow_old_world(size_t base_line, size_t nb_line)
115{
116   size_t x,y;
117   for (y = base_line; y < base_line + nb_line; y++)
118   {
119      for(x = 0; x < WIDTH ; x++) 
120      {
121         world[OLD][y][x] = world[NEW][y][x]; 
122      }
123   }
124}
125
126////////////////////////////////////////
127__attribute__((constructor)) void main()
128{
129   unsigned int proc_id       = giet_procid();              // processor id
130   unsigned int nlocal_procs  = NB_PROCS_MAX;               // processors per cluster
131   unsigned int nclusters     = X_SIZE*Y_SIZE;              // number of clusters
132   unsigned int nglobal_procs = nclusters * nlocal_procs;   // number of tasks
133   size_t i;
134
135   size_t       nb_line       = HEIGHT / nglobal_procs;
136   size_t       base_line     = nb_line * proc_id; 
137   
138   PRINTF("*** Starting init at cycle %d ***\n", giet_proctime());
139
140   //  barriers initialization
141   if ( proc_id == 0 )
142   {
143      barrier_init(&barriers[0], nglobal_procs);
144      barrier_init(&barriers[1], nglobal_procs);
145
146      init_ok = 0;
147   }
148   else
149   {
150      while ( init_ok == 1 );
151   }
152
153   init_world(base_line, nb_line);
154
155   PRINTF("*** Completing init at cycle %d ***\n", giet_proctime());
156   barrier_wait(&barriers[0]);
157   
158   for (i = 0; i < NB_ITERATION; i++)
159   {
160      //PRINTF("\n*** Starting computation for iteration %d at cycle %d\n", i, giet_proctime());
161      compute_new_gen(base_line, nb_line);
162      //PRINTF("\n*** Starting display for iteration %d at cycle %d\n", i, giet_proctime());
163      grow_old_world(base_line, nb_line);
164      display_world(base_line, nb_line);
165      barrier_wait(&barriers[1]);
166      barrier_init(&barriers[1], nglobal_procs);
167   }
168
169   PRINTF("*** End of main at cycle %d ***\n", giet_proctime());
170
171   giet_exit("Completed");
172} // end main()
173
174// Local Variables:
175// tab-width: 3
176// c-basic-offset: 3
177// c-file-offsets:((innamespace . 0)(inline-open . 0))
178// indent-tabs-mode: nil
179// End:
180
181// vim: filetype=cpp:expandtab:shiftwidth=3:tabstop=3:softtabstop=3
182
183
184
Note: See TracBrowser for help on using the repository browser.