src/pbe/pbe.h

Go to the documentation of this file.
00001 #ifndef _PBE_H
00002 #define _PBE_H
00003 
00004 #include "pbedefs.h"
00005 
00006 #ifdef __cplusplus
00007 extern "C" {
00008 #endif
00009 
00010 /* Global variables */
00011 struct tms _tms_aid;
00012 struct benchmark *pbe_data;
00013 int pbe_initialized;
00014 int pbe_numofslots;
00015 
00016 /* Define a portable reduce function */
00017 #ifdef _PBE_USE_MPI
00018 #ifdef POOMA_MPI
00019 float pbe_reduce(float t_local)
00020 {
00021     float t_global;
00022     reduce(&t_local,&(t_local)+1,&(t_global),OpMaxAssign());
00023     return t_global;
00024 }
00025 #else
00026 float pbe_reduce(float t_local)
00027 {
00028     float t_global;
00029     MPI_Reduce(&t_local, &t_global, 1, MPI_FLOAT, MPI_MAX, 0, MPI_COMM_WORLD);
00030     return t_global;
00031 }
00032 #endif
00033 #else
00034 float pbe_reduce(float t_local)
00035 {
00036     return t_local;
00037 }
00038 #endif
00039 
00040 
00041 int pbe_init(const char *name, int numofslots, int argparametercount, struct pbeparameter *argparameters)
00042 {
00043     int localhavempi=0, localmpirank, localmpisize, i;
00044 
00045     // exit if already initialized
00046     if (pbe_initialized) {
00047         assert(pbe_numofslots < numofslots);
00048         return 1;
00049     }
00050     pbe_initialized = 1;
00051     pbe_numofslots = numofslots;
00052 
00053 
00054 #ifdef _PBE_USE_MPI
00055     localhavempi = 1;
00056 #ifdef POOMA_MPI
00057     localmpisize = Pooma::getNodes();
00058     localmpirank = Pooma::myNode();
00059 #else
00060     MPI_Comm_rank(MPI_COMM_WORLD, &localmpirank);
00061     MPI_Comm_size(MPI_COMM_WORLD, &localmpisize);
00062 #endif
00063 #else
00064     localhavempi = 0;
00065     localmpirank = 0;
00066     localmpisize = 1;
00067 #endif
00068 
00069     pbe_data = (struct benchmark *) (calloc(1,sizeof(struct benchmark)));
00070     if(pbe_data == NULL) {
00071         if(localhavempi)
00072             printf("(%d/%d): ", localmpirank, localmpisize);
00073         printf("Memory allocation not ok for pbe_data (1*%d=%d Bytes)!\n",
00074             (int)sizeof(struct benchmark),
00075             (int)((numofslots+1)*sizeof(struct timer)));
00076         return 1;
00077     }
00078 
00079     pbe_data->slots = 0;
00080     pbe_data->have_mpi = localhavempi;
00081     pbe_data->mpirank = localmpirank;
00082     pbe_data->mpisize = localmpisize;
00083 
00084     pbe_data->timers = (struct timer *) (calloc((numofslots+1),sizeof(struct timer)));
00085 
00086     if(pbe_data->timers == NULL) {
00087         if(pbe_data->have_mpi) printf("(%d/%d): ", pbe_data->mpirank, pbe_data->mpisize);
00088         printf("Memory allocation not ok for timers (%d*%d=%d Bytes)!\n",
00089             numofslots+1,
00090             (int)sizeof(struct timer),
00091             (int)((numofslots+1)*sizeof(struct timer)));
00092         return 1;
00093     }
00094 
00095     for(i=0;i<numofslots;i++)
00096         sprintf(pbe_data->timers[i].name,"%s","N/A");
00097 
00098     /* HANDLING NAME */
00099     sprintf(pbe_data->name,"%s",name);
00100 
00101     /* HANDLING SLOTS */
00102     pbe_data->slots = numofslots;
00103 
00104     /* HANDLING PARAMETERS */
00105     pbe_data->parametercount = argparametercount + pbe_data->have_mpi;
00106     /* one perhaps for mpisize*/
00107 
00108     if(pbe_data->parametercount + pbe_data->have_mpi) {
00109         pbe_data->parameters = (struct pbeparameter *) calloc(pbe_data->parametercount, sizeof(struct pbeparameter));
00110 
00111         if(pbe_data->parameters == NULL) {
00112             if(pbe_data->have_mpi) printf("(%d/%d): ", pbe_data->mpirank, pbe_data->mpisize);
00113             printf("Memory allocation not ok for parameters (%d*%d=%d Bytes)!\n",
00114                 pbe_data->parametercount,
00115                 (int)sizeof(struct pbeparameter),
00116                 (pbe_data->parametercount)*(int)sizeof(struct pbeparameter));
00117             return 1;
00118         }
00119 
00120         for(i=0;i<argparametercount;i++) {
00121             sprintf(pbe_data->parameters[i].name,"%s",argparameters[i].name);
00122             sprintf(pbe_data->parameters[i].shortname,"%s",argparameters[i].shortname);
00123             pbe_data->parameters[i].value = argparameters[i].value;
00124         }
00125         if(pbe_data->have_mpi) {
00126             sprintf(pbe_data->parameters[i].name,"mpisize");
00127             sprintf(pbe_data->parameters[i].shortname,"p");
00128             pbe_data->parameters[i].value=pbe_data->mpisize;
00129         }
00130     } else
00131         pbe_data->parameters = NULL;
00132 
00133     return 0;
00134 }
00135 
00138 void pbe_dump() {
00139     int handle;
00140     int writer = (!pbe_data->have_mpi || pbe_data->mpirank == 0) ? 1 : 0;
00141 
00142     if (writer) {
00143         printf("Timing statistics:\n");
00144         printf("%42s %8s %12s %12s %12s %12s\n", "Name", "Count", "Real time", "User time", "System time", "Sys+usr time");
00145         printf("-------------------------------------------------------------------------------------------------------\n");
00146     }
00147     for (handle = 0; handle <= pbe_data->slots; handle ++) {
00148         if (pbe_getcounter(handle) > 0) {
00149             float rtime  = pbe_reduce(pbe_getrealtime(handle));
00150             float stime  = pbe_reduce(pbe_getsystime(handle));
00151             float utime  = pbe_reduce(pbe_getusrtime(handle));
00152             float sutime = pbe_reduce(pbe_getsysusrtime(handle));
00153             int count = pbe_getcounter(handle);
00154             if (writer) {
00155                 printf("%42s %8d %12.3f %12.3f %12.3f %12.3f\n",
00156                        pbe_data->timers[handle].name, count, rtime, utime, stime, sutime);
00157             }
00158         }
00159     }
00160 }
00161 
00168 int pbe_finalize(int numofiterations) {
00169     int i;
00170     float stime, utime, rtime, sutime;
00171     FILE *fout = NULL;
00172     char filename[200];
00173     int writer;
00174 
00175     writer = (!pbe_data->have_mpi || pbe_data->mpirank == 0) ? 1 : 0;
00176     /* MPI-Master or no MPI */
00177 
00178     if (numofiterations) {
00179         if (writer) {
00180             sprintf(filename,"./pbedata/pbeout_%s",pbe_data->name);
00181             for (i = 0; i < pbe_data->parametercount; i ++)
00182                 sprintf(filename, "%s_%s%d",
00183                         filename,
00184                         pbe_data->parameters[i].shortname,
00185                         pbe_data->parameters[i].value);
00186 
00187             fout = fopen(filename,"w");
00188             if (fout == NULL)
00189                 fprintf(stderr, "Error writing pbe output file.\n");
00190             else {
00191                 fprintf(fout,"# PBE Statistics: ################################");
00192                 fprintf(fout,"\n# Description:%s",pbe_data->name);
00193                 /* fprintf(fout,"\n# CLOCKS_PER_SEC=%d",CLOCKS_PER_SEC); */
00194                 fprintf(fout,"\n# CLK_TCK=%d",CLK_TCK);
00195                 fprintf(fout,"\n#PBE:INFO:numberofiterations=%d",numofiterations);
00196                 for (i=0; i<pbe_data->parametercount; i++)
00197                     fprintf(fout,"\n#PBE:PAR:%s:%s=%d",pbe_data->parameters[i].name,pbe_data->parameters[i].shortname,pbe_data->parameters[i].value);
00198                 fprintf(fout,"\n#PBE:COL:Counter Name Realtime Systime Usrtime Systime+Usrtime");
00199                 fprintf(fout,"\n#PBE:TYP:I S F F F F");
00200             }
00201         }
00202         for (i = 0; i <= pbe_data->slots; i ++) {
00203             if (pbe_getcounter(i) > 0) {
00204 
00205 #if 0
00206 #ifdef _PBE_USE_MPI
00207                 printf("(%d/%d):DEBUG:rcvb:real:%f,sys:%f,usr:%f,sysusr:%f\n",
00208                        pbe_data->mpirank,
00209                        pbe_data->mpisize,
00210                        pbe_getrealtime(i) / numofiterations,
00211                        pbe_getsystime(i) / numofiterations,
00212                        pbe_getusrtime(i) / numofiterations,
00213                        pbe_getsysusrtime(i) / numofiterations);
00214 #endif
00215 #endif
00216                 rtime  = pbe_reduce(pbe_getrealtime(i)) / numofiterations;
00217                 stime  = pbe_reduce(pbe_getsystime(i)) / numofiterations;
00218                 utime  = pbe_reduce(pbe_getusrtime(i)) / numofiterations;
00219                 sutime = pbe_reduce(pbe_getsysusrtime(i)) / numofiterations;
00220                 if (writer && fout != NULL) {
00221                     fprintf(fout, "\n%d %s %f %f %f %f",
00222                             i, pbe_data->timers[i].name,
00223                             rtime, stime, utime, sutime);
00224                 }
00225             }
00226         }
00227 
00228         if (writer && fout != NULL) {
00229             fprintf(fout,"\n");
00230             fclose(fout);
00231             printf("\nwrote to %s\n",filename);
00232         }
00233 
00234     } /* numiterations */
00235     free(pbe_data->timers);
00236     free(pbe_data->parameters);
00237     free(pbe_data);
00238 
00239     return 0;
00240 }
00241 
00242 #ifdef __cplusplus
00243 }
00244 #endif
00245 
00246 #endif /* not _PBE_H_ */

Generated on Fri Oct 26 13:35:12 2007 for FEMAXX (Finite Element Maxwell Eigensolver) by  doxygen 1.4.7