eg_raset.ex.c

Go to the documentation of this file.
00001 /* EGlib "Efficient General Library" provides some basic structures and
00002  * algorithms commons in many optimization algorithms.
00003  *
00004  * Copyright (C) 2005-2010 Daniel Espinoza
00005  * 
00006  * This library is free software; you can redistribute it and/or modify it
00007  * under the terms of the GNU Lesser General Public License as published by the
00008  * Free Software Foundation; either version 2.1 of the License, or (at your
00009  * option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful, but 
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
00013  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
00014  * License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this library; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
00019  * */
00020 /** @file 
00021  * @ingroup EGrSet */
00022 /** @addtogroup EGrSet */
00023 /** @{ */
00024 /* ========================================================================= */
00025 /** @brief given a groud set size, perform the given number of
00026  * insertion/deletion operations, and grow the set twice. */
00027 /* ========================================================================= */
00028 #include "EGlib.h"
00029 
00030 /* ========================================================================= */
00031 /** @brief display the usage message for this program */
00032 void usage (char *program)
00033 {
00034   fprintf (stdout, "Usage: %s sz nop [display]\n", program);
00035   fprintf (stdout, "\twhere `sz' is the starting size of the groud sent\n");
00036   fprintf (stdout, "\twhere `nop' is the number of random additions/deletions/peek operations to perform in every loop\n");
00037   fprintf (stdout, "\twhere `display' is an integer setting the display level\n");
00038 }
00039 
00040 /* ========================================================================= */
00041 /** @brief main function */
00042 int main (int argc,
00043           char **argv)
00044 {
00045   int grsz = 0, nop=0, i,j,cnt=0, display=0,rval;
00046   uint32_t k;
00047   EGrandState_t seed;
00048   EGrset_t set;
00049   EGtimer_t t1, t2, t3;
00050   EGlib_info();
00051   EGlib_version();
00052   EGtimerReset(&t1);
00053   EGtimerReset(&t2);
00054   EGtimerReset(&t3);
00055   EGrandInit(&seed);
00056   EGrsetInit(&set,0);
00057   /* set signal and limits */
00058   EGsigSet(rval,CLEANUP);
00059   EGsetLimits(3600.0,4294967295UL);
00060   if((argc<3) || (argc>4) || ((grsz=atoi(argv[1]))<1) || ((nop=atoi(argv[2]))<1))
00061   {
00062     usage(argv[0]);
00063     return 1;
00064   }
00065   if(argc==4) display=atoi(argv[3]);
00066   fprintf(stdout,"\tGroud set size: %d\n\tOperations: %d\n\tDisplay %d\n", grsz, nop, display);
00067   /* starting log */
00068   EGrsetSetgrnd(&set,10);
00069   EGcallD(EGrsetCheck(&set));
00070   if(display) EGrsetDisplay(&set,stdout);
00071   EGcallD(EGrsetCheck(&set));
00072   EGrsetAdd(&set,0);
00073   if(display) EGrsetDisplay(&set,stdout);
00074   EGcallD(EGrsetCheck(&set));
00075   EGrsetAdd(&set,4);
00076   if(display) EGrsetDisplay(&set,stdout);
00077   EGcallD(EGrsetCheck(&set));
00078   EGrsetAdd(&set,4);
00079   if(display) EGrsetDisplay(&set,stdout);
00080   EGcallD(EGrsetCheck(&set));
00081   EGrsetAdd(&set,4);
00082   if(display) EGrsetDisplay(&set,stdout);
00083   EGcallD(EGrsetCheck(&set));
00084   EGrsetAdd(&set,4);
00085   if(display) EGrsetDisplay(&set,stdout);
00086   EGcallD(EGrsetCheck(&set));
00087   EGrsetAdd(&set,6);
00088   if(display) EGrsetDisplay(&set,stdout);
00089   EGcallD(EGrsetCheck(&set));
00090   EGrsetAdd(&set,1);
00091   if(display) EGrsetDisplay(&set,stdout);
00092   EGcallD(EGrsetCheck(&set));
00093   EGrsetAdd(&set,8);
00094   if(display) EGrsetDisplay(&set,stdout);
00095   EGcallD(EGrsetCheck(&set));
00096   EGrsetAdd(&set,9);
00097   if(display) EGrsetDisplay(&set,stdout);
00098   EGcallD(EGrsetCheck(&set));
00099   EGrsetAdd(&set,3);
00100   if(display) EGrsetDisplay(&set,stdout);
00101   EGcallD(EGrsetCheck(&set));
00102   EGrsetAdd(&set,5);
00103   if(display) EGrsetDisplay(&set,stdout);
00104   EGcallD(EGrsetCheck(&set));
00105   EGrsetAdd(&set,7);
00106   if(display) EGrsetDisplay(&set,stdout);
00107   EGcallD(EGrsetCheck(&set));
00108   EGrsetAdd(&set,2);
00109   if(display) EGrsetDisplay(&set,stdout);
00110   EGcallD(EGrsetCheck(&set));
00111   EGrsetDel(&set,0);
00112   if(display) EGrsetDisplay(&set,stdout);
00113   EGcallD(EGrsetCheck(&set));
00114   EGrsetDel(&set,0);
00115   if(display) EGrsetDisplay(&set,stdout);
00116   EGcallD(EGrsetCheck(&set));
00117   EGrsetDel(&set,0);
00118   if(display) EGrsetDisplay(&set,stdout);
00119   EGcallD(EGrsetCheck(&set));
00120   EGrsetDel(&set,0);
00121   if(display) EGrsetDisplay(&set,stdout);
00122   EGcallD(EGrsetCheck(&set));
00123   EGrsetDel(&set,1);
00124   if(display) EGrsetDisplay(&set,stdout);
00125   EGcallD(EGrsetCheck(&set));
00126   EGrsetDel(&set,2);
00127   if(display) EGrsetDisplay(&set,stdout);
00128   EGcallD(EGrsetCheck(&set));
00129   EGrsetDel(&set,3);
00130   if(display) EGrsetDisplay(&set,stdout);
00131   EGcallD(EGrsetCheck(&set));
00132   EGrsetDel(&set,4);
00133   if(display) EGrsetDisplay(&set,stdout);
00134   EGcallD(EGrsetCheck(&set));
00135   EGrsetDel(&set,5);
00136   if(display) EGrsetDisplay(&set,stdout);
00137   EGcallD(EGrsetCheck(&set));
00138   EGrsetDel(&set,6);
00139   if(display) EGrsetDisplay(&set,stdout);
00140   EGcallD(EGrsetCheck(&set));
00141   EGrsetDel(&set,7);
00142   if(display) EGrsetDisplay(&set,stdout);
00143   EGcallD(EGrsetCheck(&set));
00144   EGrsetDel(&set,8);
00145   if(display) EGrsetDisplay(&set,stdout);
00146   EGcallD(EGrsetCheck(&set));
00147   EGrsetDel(&set,9);
00148   if(display) EGrsetDisplay(&set,stdout);
00149   EGcallD(EGrsetCheck(&set));
00150   EGrsetDel(&set,9);
00151   if(display) EGrsetDisplay(&set,stdout);
00152   EGcallD(EGrsetCheck(&set));
00153   EGrsetDel(&set,9);
00154   if(display) EGrsetDisplay(&set,stdout);
00155   EGcallD(EGrsetCheck(&set));
00156   EGrsetDel(&set,9);
00157   if(display) EGrsetDisplay(&set,stdout);
00158   EGcallD(EGrsetCheck(&set));
00159   EGrsetAdd(&set,9);
00160   EGrsetAdd(&set,0);
00161   EGrsetAdd(&set,3);
00162   EGrsetAdd(&set,5);
00163   EGrsetAdd(&set,7);
00164   if(display) EGrsetDisplay(&set,stdout);
00165   EGcallD(EGrsetCheck(&set));
00166   EGrsetSetgrnd(&set,8);
00167   if(display) EGrsetDisplay(&set,stdout);
00168   EGcallD(EGrsetCheck(&set));
00169   EGrsetSetgrnd(&set,10);
00170   if(display) EGrsetDisplay(&set,stdout);
00171   EGcallD(EGrsetCheck(&set));
00172   EGrsetSetgrnd(&set,6);
00173   if(display) EGrsetDisplay(&set,stdout);
00174   EGcallD(EGrsetCheck(&set));
00175   EGrsetSetgrnd(&set,9);
00176   if(display) EGrsetDisplay(&set,stdout);
00177   EGcallD(EGrsetCheck(&set));
00178   EGrsetSetgrnd(&set,4);
00179   if(display) EGrsetDisplay(&set,stdout);
00180   EGcallD(EGrsetCheck(&set));
00181   EGrsetSetgrnd(&set,8);
00182   if(display) EGrsetDisplay(&set,stdout);
00183   EGcallD(EGrsetCheck(&set));
00184   /* main loop */
00185   for(i=1 ; i < 4 ; i++)
00186   {
00187     if(display) fprintf(stdout,"==========================================\nWorking on ground %d\n==========================================\n",grsz*i);
00188     EGrsetSetgrnd(&set,grsz*i);
00189     if(display) EGrsetDisplay(&set,stdout);
00190     EGcallD(EGrsetCheck(&set));
00191     EGtimerStart(&t1);
00192     for(j=nop;j--;)
00193     {
00194       k=EGrand(&seed)%(grsz*i);
00195       EGrsetAdd(&set,(int32_t)k);
00196     }
00197     EGtimerStop(&t1);
00198     if(display) EGrsetDisplay(&set,stdout);
00199     EGcallD(EGrsetCheck(&set));
00200     EGtimerStart(&t3);
00201     for(j=nop;j--;)
00202     {
00203       k=EGrand(&seed)%(grsz*i);
00204       cnt+=EGrsetPeek(&set,k);
00205     }
00206     EGtimerStop(&t3);
00207     if(display) EGrsetDisplay(&set,stdout);
00208     EGcallD(EGrsetCheck(&set));
00209     EGtimerStart(&t2);
00210     for(j=nop;j--;)
00211     {
00212       k=EGrand(&seed)%(grsz*i);
00213       EGrsetDel(&set,(int32_t)k);
00214     }
00215     EGtimerStop(&t2);
00216   }
00217   for(i=4 ; i > 0 ; i--)
00218   {
00219     if(display) fprintf(stdout,"==========================================\nWorking on ground %d\n==========================================\n",grsz*i);
00220     EGrsetSetgrnd(&set,grsz*i);
00221     if(display) EGrsetDisplay(&set,stdout);
00222     EGcallD(EGrsetCheck(&set));
00223     EGtimerStart(&t1);
00224     for(j=nop;j--;)
00225     {
00226       k=EGrand(&seed)%(grsz*i);
00227       EGrsetAdd(&set,(int32_t)k);
00228     }
00229     EGtimerStop(&t1);
00230     if(display) EGrsetDisplay(&set,stdout);
00231     EGcallD(EGrsetCheck(&set));
00232     EGtimerStart(&t3);
00233     for(j=nop;j--;)
00234     {
00235       k=EGrand(&seed)%(grsz*i);
00236       cnt+=EGrsetPeek(&set,(int32_t)k);
00237     }
00238     EGtimerStop(&t3);
00239     if(display) EGrsetDisplay(&set,stdout);
00240     EGcallD(EGrsetCheck(&set));
00241     EGtimerStart(&t2);
00242     for(j=nop;j--;)
00243     {
00244       k=EGrand(&seed)%(grsz*i);
00245       EGrsetDel(&set,(int32_t)k);
00246     }
00247     EGtimerStop(&t2);
00248   }
00249   /* Liberating allocated memory */
00250   EGrsetClear(&set);
00251   EGcallD(EGrsetCheck(&set));
00252   CLEANUP:
00253   fprintf(stdout,"Statistics:\n");
00254   fprintf(stdout,"\tAdd Cals  %9d Time %9.5lg Average %9.5lg\n",nop*3,t1.time, t1.time/(nop*3));
00255   fprintf(stdout,"\tDel Cals  %9d Time %9.5lg Average %9.5lg\n",nop*3,t2.time, t2.time/(nop*3));
00256   fprintf(stdout,"\tPeek Cals %9d Time %9.5lg Average %9.5lg Success %d %.2lf%%\n",nop*3,t3.time, t3.time/(nop*3), cnt, ((double)cnt)/(nop*3));
00257   return rval;
00258 }
00259 
00260 /* ========================================================================= */
00261 /** @} */