Changeset 509 for soft/giet_vm


Ignore:
Timestamp:
Feb 10, 2015, 7:16:54 PM (10 years ago)
Author:
alain
Message:

Using the CMA component for display.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • soft/giet_vm/applications/gameoflife/main.c

    r504 r509  
    11//////////////////////////////////////////////////////////////////////////////////
    2 // File : main.c  (for gameoflife)
    3 // Date : November 2013
    4 // Author :  Alexandre Joannou <alexandre.joannou@lip6.fr>
     2// File    : main.c  (for gameoflife)
     3// Date    : November 2013 / February 2015
     4// Authors :  Alexandre Joannou <alexandre.joannou@lip6.fr> november 2013
     5//            Alain Greiner <alain.greiner@lip6.fr> february 2015
    56//
    6 // This application is an emulation of the game of life automaton.
     7// This application is an emulation of the Game of Life automaton.
    78// 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// There is one task per processor.
     10// Each task compute HEIGHT/nbprocs lines.
     11// Task running on processor P(0,0,0) initialises the barrier, and
     12// control the chained buffer DMA controler, when it is used.
     13//
    914// The number of processors must be a power of 2 not larger than HEIGHT.
    1015//////////////////////////////////////////////////////////////////////////////////
     
    2126#define PRINTF(...) ({ if ( proc_id==0) { giet_shr_printf(__VA_ARGS__); } })
    2227
     28typedef unsigned char uint8_t;
     29
     30uint8_t WORLD[2][HEIGHT][WIDTH] __attribute__((aligned(64)));
     31
     32uint8_t DISPLAY[2][HEIGHT][WIDTH] __attribute__((aligned(64)));
     33
    2334giet_sqt_barrier_t barrier;
    2435
    2536volatile unsigned int init_ok;
    2637
    27 #define OLD 0
    28 #define NEW 1
    29 #define DSP 2
    30 
    31 typedef unsigned char uint8_t;
    32 typedef unsigned int size_t;
    33 
    34 uint8_t world[3][HEIGHT][WIDTH];
    35 
    36 /////////////////////////////////////////////////
    37 void init_world(size_t base_line, size_t nb_line)
    38 {
    39    size_t x,y;
    40    for (y = base_line ; y < base_line + nb_line; y++)
     38////////////////////////////////////
     39void init_world( unsigned int phase,
     40                 unsigned int base_line,
     41                 unsigned int nb_line )
     42{
     43   unsigned int x,y;
     44   for (y = base_line ; y < base_line + nb_line ; y++)
     45   {
     46      for(x = 0 ; x < WIDTH ; x++)
     47      {
     48         WORLD[phase][y][x] = (giet_rand() >> (x % 8)) & 0x1;
     49      }
     50   }
     51}
     52
     53//////////////////////////////////////////////////////
     54uint8_t number_of_alive_neighbour( unsigned int phase,
     55                                   unsigned int x,
     56                                   unsigned int y )
     57{
     58   uint8_t nb = 0;
     59
     60   nb += WORLD[phase][(y - 1) % HEIGHT][(x - 1) % WIDTH];
     61   nb += WORLD[phase][ y              ][(x - 1) % WIDTH];
     62   nb += WORLD[phase][(y + 1) % HEIGHT][(x - 1) % WIDTH];
     63   nb += WORLD[phase][(y - 1) % HEIGHT][ x             ];
     64   nb += WORLD[phase][(y + 1) % HEIGHT][ x             ];
     65   nb += WORLD[phase][(y - 1) % HEIGHT][(x + 1) % WIDTH];
     66   nb += WORLD[phase][ y              ][(x + 1) % WIDTH];
     67   nb += WORLD[phase][(y + 1) % HEIGHT][(x + 1) % WIDTH];
     68
     69   return nb;
     70}
     71
     72/////////////////////////////////////////
     73uint8_t compute_cell( unsigned int phase,
     74                      unsigned int x,
     75                      unsigned int y )
     76{
     77   uint8_t nb_neighbours_alive = number_of_alive_neighbour( phase, x , y );
     78
     79   if (WORLD[phase][y][x] == 1)
     80   {
     81      if (nb_neighbours_alive == 2 || nb_neighbours_alive == 3)  return 1;
     82   }
     83   else
     84   {
     85      if (nb_neighbours_alive == 3) return 1;
     86      else                          return WORLD[phase][y][x];
     87   }
     88   return 0;
     89}
     90
     91/////////////////////////////////////////
     92void compute_new_gen( unsigned int phase,
     93                      unsigned int base_line,
     94                      unsigned int nb_line )
     95{
     96   unsigned int x,y;
     97   for (y = base_line; y < base_line + nb_line; y++)
    4198   {
    4299      for(x = 0; x < WIDTH ; x++)
    43100      {
    44          world[OLD][y][x] = (giet_rand() >> (x % 8)) & 0x1;
     101         WORLD[phase][y][x] = compute_cell( 1 - phase , x , y ); 
    45102      }
    46103   }
    47104}
    48105
    49 /////////////////////////////////////////////////
    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 
    66 /////////////////////////////////////////////////
    67 uint8_t compute_cell(size_t x, size_t y)
    68 {
    69    uint8_t nb_neighbours_alive = number_of_alive_neigh(x,y);
    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];
    78    }
    79    return 0;
    80 }
    81 
    82 //////////////////////////////////////////////////////
    83 void compute_new_gen(size_t base_line, size_t nb_line)
    84 {
    85    size_t x,y;
     106////////////////////////////////////
     107void copy_world( unsigned int phase,
     108                 unsigned int base_line,
     109                 unsigned int nb_line )
     110{
     111   unsigned int x,y;
    86112   for (y = base_line; y < base_line + nb_line; y++)
    87113   {
    88114      for(x = 0; x < WIDTH ; x++)
    89115      {
    90          world[NEW][y][x] = compute_cell(x,y); 
    91       }
    92    }
    93 }
    94 
    95 ////////////////////////////////////////////////////
    96 void display_world(size_t base_line, size_t nb_line)
    97 {
    98    size_t x,y;
    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; 
    104       }
    105    }
    106 
    107    giet_fbf_sync_write( base_line * WIDTH ,
    108                         &world[DSP][base_line][0],
    109                         nb_line * WIDTH );
    110 }
    111 
    112 /////////////////////////////////////////////////////
    113 void grow_old_world(size_t base_line, size_t nb_line)
    114 {
    115    size_t x,y;
    116    for (y = base_line; y < base_line + nb_line; y++)
    117    {
    118       for(x = 0; x < WIDTH ; x++)
    119       {
    120          world[OLD][y][x] = world[NEW][y][x]; 
     116         DISPLAY[phase][y][x] = WORLD[phase][y][x]*255; 
    121117      }
    122118   }
     
    138134   giet_procs_number( &x_size, &y_size, &n_local_procs );
    139135
    140    // compute continuous processor index
     136   // compute continuous processor index & number of procs
    141137   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
    145    size_t i;
     138   unsigned int n_global_procs = x_size * y_size * n_local_procs;
     139
     140   unsigned int i;
    146141
    147142   if ( n_global_procs > HEIGHT )
     
    152147   }
    153148
    154    size_t      nb_line       = HEIGHT / n_global_procs;
    155    size_t      base_line     = nb_line * proc_id;
    156    
    157    PRINTF("\n*** Starting barrier initialisation at cycle %d ***\n"
     149   unsigned int nb_line       = HEIGHT / n_global_procs;
     150   unsigned int base_line     = nb_line * proc_id;
     151   
     152   PRINTF("\n*** Starting barrier and CMA initialisation at cycle %d ***\n"
    158153          " nprocs = %d / nlines = %d\n",
    159154          giet_proctime() , n_global_procs, HEIGHT );
    160155
    161    // barrier initialization
     156   //////////// barrier & CMA initialization ( P(0,0,0) )
     157
    162158   if ( proc_id == 0 )
    163159   {
     160      // initialises CMA component
     161      giet_fbf_cma_alloc();
     162      giet_fbf_cma_start( &DISPLAY[0][0][0] ,
     163                          &DISPLAY[1][0][0] ,
     164                          HEIGHT * WIDTH );
     165
     166      // initialises barrier
    164167      sqt_barrier_init( &barrier , x_size , y_size , n_local_procs );
     168
     169      // activates all other processors
    165170      init_ok = 1;
    166171   }
     
    173178          giet_proctime() );
    174179
    175    //  parallel world  initialization
    176    init_world( base_line , nb_line );
    177    display_world( base_line , nb_line );
    178 
     180   ///////////// world  initialization ( All processors )
     181
     182   // initialises WORLD[0]
     183   init_world( 0 , base_line , nb_line );
     184
     185   // copy WORLD[0] to DISPLAY[0]
     186   copy_world( 0 , base_line , nb_line );
     187
     188   // synchronise with other procs
    179189   sqt_barrier_wait( &barrier );
    180190
    181    PRINTF("\n*** Starting life at cycle %d ***\n",
    182           giet_proctime() );
    183    
    184    for (i = 0; i < NB_ITERATION; i++)
    185    {
    186       compute_new_gen( base_line, nb_line );
    187       grow_old_world( base_line, nb_line );
    188       display_world( base_line, nb_line );
    189 
     191   // P(0,0,0) displays DISPLAY[0]
     192   if ( proc_id == 0 ) giet_fbf_cma_display ( 0 );
     193
     194   PRINTF("\n*** Starting evolution at cycle %d ***\n", giet_proctime() );
     195   
     196   //////////// evolution : 2 steps per iteration
     197
     198   for (i = 0 ; i < NB_ITERATION ; i++)
     199   {
     200      // compute WORLD[1] from WORLD[0]
     201      compute_new_gen( 1 , base_line , nb_line );
     202
     203      // copy WORLD[1] to DISPLAY[1]
     204      copy_world( 1 , base_line , nb_line );
     205
     206      // synchronise with other procs
    190207      sqt_barrier_wait( &barrier );
    191208
    192       PRINTF(" - iteration %d completed\n", i );
    193    }
     209      // P(0,0,0) displays DISPLAY[1]
     210      if ( proc_id == 0 ) giet_fbf_cma_display ( 1 );
     211   
     212      PRINTF(" - step %d completed\n", 2*i );
     213   
     214      // compute WORLD[0] from WORLD[1]
     215      compute_new_gen( 0 , base_line , nb_line );
     216
     217      // copy WORLD[0] to DISPLAY[0]
     218      copy_world( 0 , base_line , nb_line );
     219
     220      // synchronise with other procs
     221      sqt_barrier_wait( &barrier );
     222
     223      // P(0,0,0) displays DISPLAY[0]
     224      if ( proc_id == 0 ) giet_fbf_cma_display ( 0 );
     225
     226      PRINTF(" - step %d completed\n", 2*i + 1 );
     227   } // end main loop
    194228
    195229   PRINTF("\n*** End of main at cycle %d ***\n", giet_proctime());
Note: See TracChangeset for help on using the changeset viewer.