Research Article

Using Heuristic Value Prediction and Dynamic Task Granularity Resizing to Improve Software Speculation

Algorithm 1

A code example optimized by HEUSPEC model. (The benchmark is Pi in the OmpSrc package. (a) the labeled sequential code. The loop is bolded. (b) the HEUSPEC style code after transformation. (c) the HEUSPEC_main_body function. The HVP and the DTGR are bolded. (d) the HEUSPEC_threadFunc .)
(a) original labeled C code
int main(int argc, char * argv ) {
#pragma heuspec parallel for (w, pi)
++)  {
;
  +=   ;
}
}
(b) HEUSPEC style C code
int main(int argc, char * argv ) {
#ifdef ENABLE_HEUSPEC
HEUSPEC_register_cvar_in_version_table(&w, sizeof(double), 1, NORMAL);
HEUSPEC_register_cvar_in_version_table(&pi, sizeof(double), 2, RD_PLUS);
HEUSPEC_main_body(&threadfunc, N),
#else
++)  {
;
  +=  
#endif
(c) HEUSPEC_main_body
int HEUSPEC_main_body(void * (*threadfunc)(long *)) {
HEUSPEC_initiallization ;
for ( < NUM_THREAD; ++) {
HEUSPEC_create_thread( , threadfunc);
HEUSPEC_assign_task( ); }
for
( < NUM_THREAD&&exceed_flag ! = 1; = ( )%NUM_THREAD) {
 while (1) {
    = HEUSPEC_catch_message(msg_buffer, );
   if (msg_buffer[ ].type == FINISH && ) //in-order commit
    HEUSPEC_commit
   if (msg_buffer[ ].type == EXCEPTION) //do HVP
    HEUSPEC_HeuristicValuePrediction ; }
 if(taskno ≥ AP) //an AP passed, do DTGR
   gran = HEUSPEC_DynamicTaskGranResizing(OC, OR, MT, AP, gran));
 If(taskno > quan ∣∣ HEUSPEC_break ) {
   set_exceeded_flag( );
   if(HEUSPEC_check_exceed_flag == 1) break;
   else continue; }
 HEUSPEC_assign_new_task ; }
HEUSPEC_main_body_end ; }
(d) HEUSPEC_threadFuncion
void * HEUSPEC_threadfunc(unsigned long * child_args) {
 double pi, w;
 while (1) {//main loop on threadfunc
  TLS_wait_start_msg(_ );
  mt[_ ] .PAddr = &w;
  mt[_ ] .PAddr = &pi;
  HEUSPEC_getHeadAndTail(&head, &tail, _ );
  pi = 0.0; //it is a RD_PLUS cvar
  TLS_spec_read(&w, 0);
  TLS_spec_read(&pi, 1);
   ++)  {
        ;
          +=   ;  }            
  TLS_spec_write(&(pi), 1);
  TLS_send_finish_msg(_ );
  TLS_wait_confirm_msg(_ );
  TLS_task_terminated(taskno);
  TLS_send_ready_msg(_ ); }
 return; }