TSP: The Transport Sample Protocol



Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

tsp_group_algo.c

Go to the documentation of this file.
00001 
00039 #include "tsp_sys_headers.h"
00040 
00041 #include "tsp_group_algo.h"
00042 #include "tsp_group_algo_data.h"
00043 
00044 #include "tsp_datapool.h"
00045 #include "tsp_data_sender.h"
00046 
00048 #define PGCD(n1, n2, pgcd) \
00049 { \
00050     int ln2 = n2; \
00051     pgcd = n1; \
00052     if ( pgcd != ln2 ) \
00053         {for(;pgcd != ln2; (pgcd > ln2) ? (pgcd -= ln2) : (ln2 -= pgcd )); }\
00054 }
00055 
00057 #define PPCM(n1, n2, ppcm) \
00058 { \
00059     int pgcd; \
00060     PGCD(n1, n2, pgcd); \
00061     ppcm = (n1 * n2) / pgcd ; \
00062 }
00063 
00064 /* True, if a given symbol with frequency_ratio and phase belongs to groupe
00065 number group_number */
00066 #define BELONGS_TO_GROUP(frequency_ratio, phase , group_number) \
00067     (!((group_number - NORMALIZED_PHASE(frequency_ratio, phase) ) % frequency_ratio))
00068     
00069 /* Phase normalisation */
00070 #define NORMALIZED_PHASE(frequency_ratio, phase) (phase % frequency_ratio )
00071 
00072 /*-----------------------------------------------*/
00073 
00079 int TSP_group_algo_get_nb_groups(const TSP_sample_symbol_info_list_t* symbols)
00080 {
00081         
00082   uint32_t nb_symbols = symbols->TSP_sample_symbol_info_list_t_len;
00083   uint32_t i;
00084   int nb_groups = 0;
00085    
00086     
00087   STRACE_IO(("-->IN"));
00088 
00089     
00090   assert(symbols);
00091     
00092   if( nb_symbols > 0 )
00093     {
00094         
00095       /* We calculate the LCM for the period of all symbols */
00096       int ppcm = (symbols->TSP_sample_symbol_info_list_t_val[0].period);
00097       STRACE_DEBUG(("frequency_ratio No0= %d", ppcm));
00098 
00099       for( i = 1 ; i < nb_symbols ; i++)
00100         {
00101                 
00102                  
00103           int frequency_ratio =
00104             (symbols->TSP_sample_symbol_info_list_t_val[i].period);
00105                   
00106           STRACE_DEBUG(("frequency_ratio No%u= %d", i, frequency_ratio));
00107 
00108          /* LCM */         
00109           PPCM(ppcm,frequency_ratio, ppcm); 
00110         }
00111         
00112       nb_groups = ppcm;
00113         
00114     }
00115   else
00116     {
00117       STRACE_WARNING(("No symbols in list !"));
00118 
00119     }
00120 
00121   STRACE_DEBUG(("Nombre de groupes = %d", nb_groups));
00122 
00123   STRACE_IO(("-->OUT"));
00124 
00125   return nb_groups;
00126     
00127 }
00128 
00135 static int TSP_group_algo_get_group_size(const TSP_sample_symbol_info_list_t* symbols, 
00136                                          int group_id)
00137 
00138 {
00139     
00140   int group_size = 0;
00141   uint32_t nb_symbols = symbols->TSP_sample_symbol_info_list_t_len;
00142   uint32_t i;
00143 
00144   STRACE_IO(("-->IN"));
00145 
00146     
00147   assert(symbols);
00148     
00149   /* We search all symbols that belong to the group group_number*/
00150   for( i = 0 ; i < nb_symbols ; i++ )
00151     {
00152       int frequency_ratio = symbols->TSP_sample_symbol_info_list_t_val[i].period;
00153       int phase = symbols->TSP_sample_symbol_info_list_t_val[i].phase;
00154       /* Does - it belong to the group */
00155       if(BELONGS_TO_GROUP(frequency_ratio, phase , group_id) )
00156         {
00157           /*Yes ! */
00158           group_size++;
00159         }
00160         
00161     }
00162     
00163   STRACE_IO(("-->OUT group_size for group[%d] is %d", group_id, group_size));
00164 
00165     
00166   return group_size;
00167 }
00168 
00175 static int TSP_group_algo_get_groups_summed_size(const TSP_sample_symbol_info_list_t* symbols,
00176                                                  int nb_groups)
00177 {
00178     
00179   int groups_summed_size = 0;
00180   int group_id;
00181 
00182   STRACE_IO(("-->IN"));
00183 
00184     
00185   assert(symbols);
00186     
00187   /* For all groups, get the size and sum the result */
00188   for( group_id = 0 ; group_id < nb_groups ; group_id++ )
00189     {
00190         
00191       groups_summed_size += TSP_group_algo_get_group_size(symbols, group_id); 
00192     }
00193     
00194   STRACE_IO(("-->OUT groups_summed_size is %d", groups_summed_size));
00195 
00196     
00197   return groups_summed_size;
00198 }
00199 
00200 
00201 
00202 
00209 static TSP_algo_table_t*
00210 TSP_group_algo_allocate_group_table(const TSP_sample_symbol_info_list_t* symbols)
00211 {
00212     
00213   int group_id;
00214     
00215   TSP_algo_table_t* table = 0;
00216   TSP_algo_group_item_t* items_table = 0;
00217     
00218   STRACE_IO(("-->IN"));
00219 
00220   assert(symbols);
00221     
00222   table = (TSP_algo_table_t*)calloc(1, sizeof(TSP_algo_table_t));
00223   TSP_CHECK_ALLOC(table, 0);
00224     
00225   /* Get total number of groups */
00226   table->table_len = TSP_group_algo_get_nb_groups(symbols);
00227     
00228   /*Allocate room for all groups */
00229   table->groups = (TSP_algo_group_t*)calloc(table->table_len, sizeof(TSP_algo_group_t));
00230   TSP_CHECK_ALLOC(table->groups, 0);
00231     
00232   /*Allocate room for all group items*/
00233   table->groups_summed_size = TSP_group_algo_get_groups_summed_size(symbols, table->table_len);
00234   items_table = (TSP_algo_group_item_t*)calloc(table->groups_summed_size, sizeof(TSP_algo_group_item_t));
00235   table->all_items = items_table;
00236   TSP_CHECK_ALLOC(items_table, 0);
00237         
00238   /*Initialize groups items*/
00239   /* And make them point at the right place in the item list */
00240   for(group_id = 0 ;
00241       group_id < table->table_len ;
00242       group_id++)
00243     {
00244             
00245       /* Get size of group i */
00246       table->groups[group_id].group_len = TSP_group_algo_get_group_size(symbols,
00247                                                                         group_id);
00248       /* Correct items pointer */
00249       table->groups[group_id].items = items_table;
00250 
00251       /* Memorise max group len */
00252       if (table->max_group_len <  table->groups[group_id].group_len)
00253         {
00254           table->max_group_len = table->groups[group_id].group_len;
00255         }
00256 
00257       
00258                 
00259       /* Get ready for next round ! */        
00260       items_table += table->groups[group_id].group_len;
00261 
00262     }
00263     
00264   
00265   STRACE_DEBUG(("Max group size = %d", table->max_group_len));
00266   STRACE_IO(("-->OUT"));
00267 
00268     
00269   return table;
00270 }
00271                                                              
00272 void TSP_group_algo_destroy_symbols_table(TSP_groups_t* groups)
00273 {
00274 
00275    TSP_algo_table_t* table = (TSP_algo_table_t*)groups;
00276 
00277    STRACE_IO(("-->IN"));
00278    if(table)
00279      {
00280        free(table->all_items);
00281        free(table->groups);
00282        free(table);
00283      }
00284 
00285    STRACE_IO(("-->OUT"));
00286 
00287 }
00288 
00289 void TSP_group_algo_create_symbols_table_free_call(TSP_sample_symbol_info_list_t* symbols)
00290 {
00291 
00292   int i;
00293 
00294    STRACE_IO(("-->IN"));
00295    
00296    for( i=0 ; i < symbols->TSP_sample_symbol_info_list_t_len ; i++)
00297      {
00298        free(symbols->TSP_sample_symbol_info_list_t_val[i].name);
00299        symbols->TSP_sample_symbol_info_list_t_val[i].name = 0;
00300      }
00301 
00302    free(symbols->TSP_sample_symbol_info_list_t_val);
00303 
00304    STRACE_IO(("-->OUT"));
00305 }
00306 
00307 int TSP_group_algo_create_symbols_table(const TSP_sample_symbol_info_list_t* in_symbols,
00308                                         TSP_sample_symbol_info_list_t* out_symbols,
00309                                         TSP_groups_t* out_groups,
00310                                         TSP_datapool_t datapool)
00311 {
00312        
00313   TSP_algo_table_t* table;
00314     
00315   int rank; 
00316   int group_id; 
00318   /* total number of in symbols*/
00319   uint32_t nb_symbols = in_symbols->TSP_sample_symbol_info_list_t_len;
00320     
00321   uint32_t out_current_info, in_current_info;
00322   TSP_sample_symbol_info_t* in_info;
00323   TSP_sample_symbol_info_t* out_info;
00324     
00325   STRACE_IO(("-->IN"));
00326 
00327     
00328   table = TSP_group_algo_allocate_group_table(in_symbols);
00329   *out_groups = table;
00330     
00331   if(table)
00332     {
00333       /* Allocate memory for the out_symbols */
00334       out_symbols->TSP_sample_symbol_info_list_t_len = table->groups_summed_size;
00335       out_symbols->TSP_sample_symbol_info_list_t_val = 
00336         (TSP_sample_symbol_info_t*)calloc(table->groups_summed_size, sizeof(TSP_sample_symbol_info_t));
00337       TSP_CHECK_ALLOC(out_symbols->TSP_sample_symbol_info_list_t_val, FALSE);
00338         
00339       /* For each group...*/
00340       for( out_current_info = 0, group_id = 0 ; group_id < table->table_len ; group_id++)
00341         {
00342             
00343           /* For each symbol, does it belong to the group ? */
00344           /* Start at first rank */
00345           for(rank = 0, in_current_info = 0 ; in_current_info < nb_symbols ; in_current_info++)
00346             {
00347               out_info = &(out_symbols->TSP_sample_symbol_info_list_t_val[out_current_info]);
00348               in_info = &(in_symbols->TSP_sample_symbol_info_list_t_val[in_current_info]);
00349 
00350             
00351               /* Does - it belong to the group */
00352               if(BELONGS_TO_GROUP(in_info->period, in_info->phase , group_id) )
00353                 {
00354                   
00355                   /*Yes, we add it*/
00356                   STRACE_DEBUG(("Adding provider_global_index %d at group %d and rank %d",
00357                                 in_info->provider_global_index,
00358                                 group_id,
00359                                 rank))
00360                     /* 1 - In the out group table */
00361                     
00362                     /* FIXME : When more types (RAW, STRING...) will be managed, there will be
00363                        other functions like TSP_data_sender_get_string_encoder or TSP_data_sender_get_raw_encoder */
00364                     table->groups[group_id].items[rank].data_encoder = TSP_data_sender_get_double_encoder();
00365                   
00366                   
00367                   table->groups[group_id].items[rank].data = 
00368                     TSP_datapool_get_symbol_value(datapool, in_info->provider_global_index, 0);
00369                   
00370                   /* 2 - In the out symbol table */
00371                   
00372                   (*out_info) = (*in_info);
00373                     
00374                   out_info->name = strdup(in_info->name);
00375                   TSP_CHECK_ALLOC(out_info->name, FALSE);
00376                     
00377                   out_info->provider_group_index = group_id;
00378                   out_info->provider_group_rank = rank;
00379                     
00380                   /* inc */
00381                   rank++;
00382                   out_current_info++;
00383                     
00384                   /* Some optimization  : the group might be full*/
00385                   if(rank == table->groups[group_id].group_len) break;
00386                 }
00387                 
00388             }
00389         }
00390     }
00391   else
00392     {
00393       STRACE_ERROR(("Unable to allocate group table"));
00394 
00395     }
00396     
00397   STRACE_IO(("-->OUT"));
00398 
00399     
00400   return TRUE;
00401 }
00402 
00403 int TSP_group_algo_get_group_number(TSP_groups_t* groups)
00404 {
00405        
00406   TSP_algo_table_t* group_table = (TSP_algo_table_t*)groups;
00407     
00408   STRACE_IO(("-->IN"));
00409 
00410     
00411   assert(groups);
00412     
00413   STRACE_IO(("-->OUT"));
00414 
00415     
00416   return group_table->table_len;
00417 }
00418 
00419 int TSP_group_algo_get_biggest_group_size(TSP_groups_t* groups)
00420 {
00421     
00422   TSP_algo_table_t* group_table = (TSP_algo_table_t*)groups;
00423     
00424   STRACE_IO(("-->IN"));
00425     
00426   assert(groups);
00427     
00428   STRACE_IO(("-->OUT"));
00429     
00430   return group_table->max_group_len;
00431 
00432 }
Framework Home Page.

Beware !! TSP wave is coming...