00001 #include "charm++.h"
00002 #include "cktiming.h"
00003 #include <stdio.h>
00004 #include <stdarg.h>
00005 #include <vector>
00006
00007
00008
00009
00010
00011
00012
00013
00014 #undef WITH_MAMBO
00015 #undef SPLIT_APART_CYCLE_ACCURATE
00016
00017
00018 #if WITH_MAMBO
00019 #include "mambo.h"
00020 static uint64_t start_time, end_time;
00021 #else
00022 CkpvStaticDeclare(double, start_time);
00023 CkpvStaticDeclare(double, end_time);
00024 #endif
00025
00026 CkpvDeclare(FILE*, bgfp);
00027
00028 CkpvDeclare(int, outputParameters);
00029
00030
00031
00032
00033
00034
00035 #ifdef BIG_SIM_PAPI
00036 #include <papi.h>
00037 #define NUM_PAPI_EVENTS 9
00038 #define BIGSIM_PAPI
00039
00040 int errorcode;
00041 int events[NUM_PAPI_EVENTS];
00042 long long values[NUM_PAPI_EVENTS];
00043 char errorstring[PAPI_MAX_STR_LEN+1];
00044 #endif
00045
00046 unsigned long bgTraceCounter;
00047 double startTime;
00048
00049 CkpvStaticDeclare(bool, insideTraceBracket);
00050
00051 class StringPool {
00052 std::vector<char *> events;
00053 int dumped;
00054 public:
00055 StringPool(): dumped(0) {}
00056 void dump() {
00057 char fname[128];
00058 const char *subdir = "params";
00059 if (dumped) return;
00060 CmiMkdir(subdir);
00061 sprintf(fname, "%s/param.%d", subdir, CkMyPe());
00062 FILE *fp = fopen(fname, "w");
00063 if (fp == NULL)
00064 CmiAbort("Failed to generated trace param file!");
00065
00066 for (int i=0; i<events.size(); i++)
00067 fprintf(fp, "%s", events[i]);
00068 fclose(fp);
00069 dumped = 1;
00070 }
00071 void insert(char *e) {
00072 events.push_back(strdup(e));
00073 }
00074 };
00075
00076 CkpvStaticDeclare(StringPool, eventsPool);
00077
00078 #if CMK_BIGSIM_CHARM
00079 static int outputTiming = 0;
00080 #endif
00081
00082
00083 void initBigSimTrace(int outputParams, int _outputTiming)
00084 {
00085 CkpvInitialize(int, outputParameters);
00086 CkpvAccess(outputParameters) = outputParams;
00087
00088 bgTraceCounter = 0;
00089 #if CMK_BIGSIM_CHARM
00090 if (!BgIsReplay()) outputTiming = 0;
00091 outputTiming = _outputTiming;
00092 #endif
00093 CkpvInitialize(bool, insideTraceBracket);
00094 CkpvAccess(insideTraceBracket) = false;
00095
00096 CkpvInitialize(double, start_time);
00097 CkpvInitialize(double, end_time);
00098
00099 CkpvInitialize(FILE*, bgfp);
00100 CkpvAccess(bgfp) = NULL;
00101 #if CMK_BIGSIM_CHARM
00102
00103
00104 if (!outputTiming) {
00105 char fname[128];
00106 const char *subdir = "params";
00107 CmiMkdir(subdir);
00108 sprintf(fname, "%s/param.%d", subdir, CkMyPe());
00109 CkpvAccess(bgfp) = fopen(fname, "w");
00110 if (CkpvAccess(bgfp) == NULL)
00111 CmiAbort("Failed to generated trace param file!");
00112 }
00113 #endif
00114
00115
00116 if (CkpvAccess(outputParameters)) {
00117 CkpvInitialize(StringPool, eventsPool);
00118 if (CkMyPe()==0) CmiPrintf("outputParameters enabled!\n");
00119 #if CMK_BIGSIM_CHARM
00120 BgRegisterUserTracingFunction(finalizeBigSimTrace);
00121 #endif
00122 }
00123
00124
00125 #ifdef BIG_SIM_PAPI
00126 CkPrintf("PAPI: number of available counters: %d\n", PAPI_num_counters());
00127 CkAssert(PAPI_num_counters() >= 0);
00128 #endif
00129
00130 }
00131
00132 void finalizeBigSimTrace()
00133 {
00134 if (CkpvAccess(bgfp) != NULL) {
00135 fclose(CkpvAccess(bgfp));
00136 CkpvAccess(bgfp) = NULL;
00137 CkpvAccess(outputParameters) = 0;
00138 }
00139 else {
00140 if (CkpvAccess(outputParameters))
00141 CkpvAccess(eventsPool).dump();
00142 }
00143 }
00144
00145 void startTraceBigSim(){
00146
00147 CkAssert(CkpvAccess(insideTraceBracket) == false);
00148 CkpvAccess(insideTraceBracket) = true;
00149
00150 #if SPLIT_APART_CYCLE_ACCURATE
00151 SimParameters *simParams = Node::Object()->simParameters;
00152 if(simParams->bgSplitNumProcs != -1 && simParams->bgSplitMyProc!=-1){
00153 (bgTraceCounter) ++;
00154 if( ((bgTraceCounter) % simParams->bgSplitNumProcs) == simParams->bgSplitMyProc){
00155
00156
00157 start_time = begin();
00158 }
00159 }
00160 #endif
00161
00162
00163 #ifdef BIGSIM_PAPI
00164
00165 for(int i=0;i<NUM_PAPI_EVENTS;i++)
00166 values[i] = 0;
00167
00168 events[0] = PAPI_FP_OPS;
00169 events[1] = PAPI_TOT_INS;
00170 events[2] = PAPI_L1_ICM;
00171 events[3] = PAPI_L2_TCM;
00172 events[4] = PAPI_L3_TCM;
00173 events[5] = PAPI_TLB_TL;
00174 events[6] = PAPI_LD_INS;
00175 events[7] = PAPI_SR_INS;
00176 events[8] = PAPI_RES_STL;
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193 CkAssert(PAPI_start_counters(events, NUM_PAPI_EVENTS) == PAPI_OK);
00194
00195 #endif
00196
00197
00198 #if CMK_BIGSIM_CHARM
00199 BgMark("startTraceBigSim %f\n");
00200 #endif
00201
00202 #if WITH_MAMBO
00203
00204 start_time = begin();
00205 #else
00206 CkpvAccess(start_time) = CmiWallTimer();
00207 #endif
00208
00209 }
00210
00211
00212 void endTraceBigSim_20param(char * eventname, int stepno, int num_params, double p1 , double p2 , double p3 , double p4 , double p5 , double p6 , double p7 , double p8 , double p9 , double p10 , double p11 , double p12 , double p13 , double p14 , double p15 , double p16 , double p17 , double p18 , double p19 , double p20 ) {
00213
00214 #if WITH_MAMBO
00215 end_time=end();
00216
00217 #else
00218 CkpvAccess(end_time) = CmiWallTimer();
00219 #endif
00220
00221 CkAssert(CkpvAccess(insideTraceBracket) == true);
00222 CkpvAccess(insideTraceBracket) = false;
00223 #if CMK_BIGSIM_CHARM
00224 char perfCountString[1024];
00225 perfCountString[0] = 0;
00226 #endif
00227 char params[2048];
00228
00229 if(num_params==0) params[0] = 0;
00230 if(num_params==1) sprintf(params, "%f", p1);
00231 if(num_params==2) sprintf(params, "%f %f", p1, p2);
00232 if(num_params==3) sprintf(params, "%f %f %f", p1, p2, p3);
00233 if(num_params==4) sprintf(params, "%f %f %f %f", p1, p2, p3, p4);
00234 if(num_params==5) sprintf(params, "%f %f %f %f %f", p1, p2, p3, p4, p5);
00235 if(num_params==6) sprintf(params, "%f %f %f %f %f %f", p1, p2, p3, p4, p5, p6);
00236 if(num_params==7) sprintf(params, "%f %f %f %f %f %f %f", p1, p2, p3, p4, p5, p6, p7);
00237 if(num_params==8) sprintf(params, "%f %f %f %f %f %f %f %f", p1, p2, p3, p4, p5, p6, p7, p8);
00238 if(num_params==9) sprintf(params, "%f %f %f %f %f %f %f %f %f", p1, p2, p3, p4, p5, p6, p7, p8, p9);
00239 if(num_params==10) sprintf(params, "%f %f %f %f %f %f %f %f %f %f", p1, p2, p3, p4, p5, p6, p7, p8, p9, p10);
00240 if(num_params==11) sprintf(params, "%f %f %f %f %f %f %f %f %f %f %f", p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11);
00241 if(num_params==12) sprintf(params, "%f %f %f %f %f %f %f %f %f %f %f %f", p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12);
00242 if(num_params==13) sprintf(params, "%f %f %f %f %f %f %f %f %f %f %f %f %f", p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13);
00243 if(num_params==14) sprintf(params, "%f %f %f %f %f %f %f %f %f %f %f %f %f %f", p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14);
00244 if(num_params==15) sprintf(params, "%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f", p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15);
00245 if(num_params==16) sprintf(params, "%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f", p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16);
00246 if(num_params==17) sprintf(params, "%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f", p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17);
00247 if(num_params==18) sprintf(params, "%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f", p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18);
00248 if(num_params==19) sprintf(params, "%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f", p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19);
00249 if(num_params==20) sprintf(params, "%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f", p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20);
00250
00251 char paramString[2560];
00252 sprintf(paramString, "params:{ %s }", params);
00253
00254 char eventNameString[1024];
00255 sprintf(eventNameString, "event:{ %s }", eventname);
00256
00257 #ifdef BIGSIM_PAPI
00258 CkAssert(PAPI_stop_counters(values, NUM_PAPI_EVENTS) == PAPI_OK);
00259
00260 sprintf(perfCountString, " PAPI:{ " );
00261
00262 for(int i=0;i<NUM_PAPI_EVENTS;i++){
00263 sprintf(perfCountString+strlen(perfCountString), " %lld ", values[i] );
00264 }
00265
00266
00267 printf("value=%lld\n", values[0]);
00268
00269 sprintf(perfCountString+strlen(perfCountString), " }");
00270
00271 #endif
00272
00273
00274 char timeString[4096];
00275 timeString[0] = 0;
00276 char stepString[128];
00277 stepString[0] = 0;
00278 sprintf(stepString, "step:{ %d }", stepno);
00279
00280 #if ! CMK_BIGSIM_CHARM
00281 #if WITH_MAMBO
00282
00283 sprintf(timeString, "time_in_cycles:{ %llu }", end_time-start_time);
00284 #endif
00285 #endif
00286
00287 if (CkpvAccess(bgfp) == NULL) {
00288 if (CkpvAccess(outputParameters)) {
00289 double t = CkpvAccess(end_time)-CkpvAccess(start_time);
00290 if (t<0.0) {
00291 CmiPrintf("time: %f\n", t);
00292 t = 0.0;
00293 }
00294 CmiAssert(t >= 0.0);
00295
00296 sprintf(timeString, "time_in_us:{ %lf } %s %s %s\n", t*1e6, eventNameString, stepString, paramString);
00297 CkpvAccess(eventsPool).insert(timeString);
00298 }
00299 }
00300
00301
00302 #if SPLIT_APART_CYCLE_ACCURATE
00303 SimParameters *simParams = Node::Object()->simParameters;
00304 if(simParams->bgSplitNumProcs != -1 && simParams->bgSplitMyProc!=-1){
00305 if( ((bgTraceCounter) % simParams->bgSplitNumProcs) == simParams->bgSplitMyProc){
00306
00307
00308 }
00309 }
00310 #endif
00311 #if CMK_BIGSIM_CHARM
00312
00313 char sequenceString[128];
00314 sequenceString[0] = 0;
00315
00316 BgMark("endTraceBigSim %f\n");
00317 if (CkpvAccess(bgfp) != NULL) {
00318
00319 int seqno = tTIMELINEREC.length()-1;
00320 if (seqno<0) CkAbort("Traces are not generated. Please run emulation with +bglog");
00321 fprintf(CkpvAccess(bgfp),"%d ",seqno);
00322 sprintf(sequenceString, "seqno:{ %d } ",seqno);
00323
00324 fprintf(CkpvAccess(bgfp), "TRACEBIGSIM: %s %s %s %s %s %s\n", eventNameString, stepString, sequenceString, timeString, perfCountString, paramString);
00325 }
00326 #else
00327
00328
00329
00330
00331
00332
00333 #endif
00334
00335
00336 }
00337
00338
00339
00340
00341
00347 void endTraceBigSim( char * eventName, int stepno ){endTraceBigSim_20param( eventName, stepno, 0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 );}
00348 void endTraceBigSim( char * eventName , int stepno, double p1 ){endTraceBigSim_20param( eventName, stepno, 1 , p1 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 );}
00349 void endTraceBigSim( char * eventName , int stepno, double p1 , double p2 ){endTraceBigSim_20param( eventName, stepno, 2 , p1 , p2 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 );}
00350 void endTraceBigSim( char * eventName , int stepno, double p1 , double p2 , double p3 ){endTraceBigSim_20param( eventName, stepno, 3 , p1 , p2 , p3 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 );}
00351 void endTraceBigSim( char * eventName , int stepno, double p1 , double p2 , double p3 , double p4 ){endTraceBigSim_20param( eventName, stepno, 4 , p1 , p2 , p3 , p4 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 );}
00352 void endTraceBigSim( char * eventName , int stepno, double p1 , double p2 , double p3 , double p4 , double p5 ){endTraceBigSim_20param( eventName, stepno, 5 , p1 , p2 , p3 , p4 , p5 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 );}
00353 void endTraceBigSim( char * eventName , int stepno, double p1 , double p2 , double p3 , double p4 , double p5 , double p6 ){endTraceBigSim_20param( eventName, stepno, 6 , p1 , p2 , p3 , p4 , p5 , p6 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 );}
00354 void endTraceBigSim( char * eventName , int stepno, double p1 , double p2 , double p3 , double p4 , double p5 , double p6 , double p7 ){endTraceBigSim_20param( eventName, stepno, 7 , p1 , p2 , p3 , p4 , p5 , p6 , p7 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 );}
00355 void endTraceBigSim( char * eventName , int stepno, double p1 , double p2 , double p3 , double p4 , double p5 , double p6 , double p7 , double p8 ){endTraceBigSim_20param( eventName, stepno, 8 , p1 , p2 , p3 , p4 , p5 , p6 , p7 , p8 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 );}
00356 void endTraceBigSim( char * eventName , int stepno, double p1 , double p2 , double p3 , double p4 , double p5 , double p6 , double p7 , double p8 , double p9 ){endTraceBigSim_20param( eventName, stepno, 9 , p1 , p2 , p3 , p4 , p5 , p6 , p7 , p8 , p9 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 );}
00357 void endTraceBigSim( char * eventName , int stepno, double p1 , double p2 , double p3 , double p4 , double p5 , double p6 , double p7 , double p8 , double p9 , double p10 ){endTraceBigSim_20param( eventName, stepno, 10 , p1 , p2 , p3 , p4 , p5 , p6 , p7 , p8 , p9 , p10 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 );}
00358 void endTraceBigSim( char * eventName , int stepno, double p1 , double p2 , double p3 , double p4 , double p5 , double p6 , double p7 , double p8 , double p9 , double p10 , double p11 ){endTraceBigSim_20param( eventName, stepno, 11 , p1 , p2 , p3 , p4 , p5 , p6 , p7 , p8 , p9 , p10 , p11 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 );}
00359 void endTraceBigSim( char * eventName , int stepno, double p1 , double p2 , double p3 , double p4 , double p5 , double p6 , double p7 , double p8 , double p9 , double p10 , double p11 , double p12 ){endTraceBigSim_20param( eventName, stepno, 12 , p1 , p2 , p3 , p4 , p5 , p6 , p7 , p8 , p9 , p10 , p11 , p12 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 );}
00360 void endTraceBigSim( char * eventName , int stepno, double p1 , double p2 , double p3 , double p4 , double p5 , double p6 , double p7 , double p8 , double p9 , double p10 , double p11 , double p12 , double p13 ){endTraceBigSim_20param( eventName, stepno, 13 , p1 , p2 , p3 , p4 , p5 , p6 , p7 , p8 , p9 , p10 , p11 , p12 , p13 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 );}
00361 void endTraceBigSim( char * eventName , int stepno, double p1 , double p2 , double p3 , double p4 , double p5 , double p6 , double p7 , double p8 , double p9 , double p10 , double p11 , double p12 , double p13 , double p14 ){endTraceBigSim_20param( eventName, stepno, 14 , p1 , p2 , p3 , p4 , p5 , p6 , p7 , p8 , p9 , p10 , p11 , p12 , p13 , p14 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 );}
00362 void endTraceBigSim( char * eventName , int stepno, double p1 , double p2 , double p3 , double p4 , double p5 , double p6 , double p7 , double p8 , double p9 , double p10 , double p11 , double p12 , double p13 , double p14 , double p15 ){endTraceBigSim_20param( eventName, stepno, 15 , p1 , p2 , p3 , p4 , p5 , p6 , p7 , p8 , p9 , p10 , p11 , p12 , p13 , p14 , p15 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 );}
00363 void endTraceBigSim( char * eventName , int stepno, double p1 , double p2 , double p3 , double p4 , double p5 , double p6 , double p7 , double p8 , double p9 , double p10 , double p11 , double p12 , double p13 , double p14 , double p15 , double p16 ){endTraceBigSim_20param( eventName, stepno, 16 , p1 , p2 , p3 , p4 , p5 , p6 , p7 , p8 , p9 , p10 , p11 , p12 , p13 , p14 , p15 , p16 , 0.0 , 0.0 , 0.0 , 0.0 );}
00364 void endTraceBigSim( char * eventName , int stepno, double p1 , double p2 , double p3 , double p4 , double p5 , double p6 , double p7 , double p8 , double p9 , double p10 , double p11 , double p12 , double p13 , double p14 , double p15 , double p16 , double p17 ){endTraceBigSim_20param( eventName, stepno, 17 , p1 , p2 , p3 , p4 , p5 , p6 , p7 , p8 , p9 , p10 , p11 , p12 , p13 , p14 , p15 , p16 , p17 , 0.0 , 0.0 , 0.0 );}
00365 void endTraceBigSim( char * eventName , int stepno, double p1 , double p2 , double p3 , double p4 , double p5 , double p6 , double p7 , double p8 , double p9 , double p10 , double p11 , double p12 , double p13 , double p14 , double p15 , double p16 , double p17 , double p18 ){endTraceBigSim_20param( eventName, stepno, 18 , p1 , p2 , p3 , p4 , p5 , p6 , p7 , p8 , p9 , p10 , p11 , p12 , p13 , p14 , p15 , p16 , p17 , p18 , 0.0 , 0.0 );}
00366 void endTraceBigSim( char * eventName , int stepno, double p1 , double p2 , double p3 , double p4 , double p5 , double p6 , double p7 , double p8 , double p9 , double p10 , double p11 , double p12 , double p13 , double p14 , double p15 , double p16 , double p17 , double p18 , double p19 ){endTraceBigSim_20param( eventName, stepno, 19 , p1 , p2 , p3 , p4 , p5 , p6 , p7 , p8 , p9 , p10 , p11 , p12 , p13 , p14 , p15 , p16 , p17 , p18 , p19 , 0.0 );}
00367 void endTraceBigSim( char * eventName , int stepno, double p1 , double p2 , double p3 , double p4 , double p5 , double p6 , double p7 , double p8 , double p9 , double p10 , double p11 , double p12 , double p13 , double p14 , double p15 , double p16 , double p17 , double p18 , double p19 , double p20 ){endTraceBigSim_20param( eventName, stepno, 20 , p1 , p2 , p3 , p4 , p5 , p6 , p7 , p8 , p9 , p10 , p11 , p12 , p13 , p14 , p15 , p16 , p17 , p18 , p19 , p20 );}