Exemple #1
0
    def get_reward(self, new_code, current_filename,
                   unroll_idx):  #VF_idx,IF_idx):
        '''Calculates the RL agent's reward. The reward is the 
        execution time improvement after injecting the pragma
        normalized to -O3.'''
        f = open(current_filename, 'w')
        f.write(''.join(new_code))
        f.close()
        if self.compile:
            if self.runtimes[current_filename][
                    self.current_pragma_idx][unroll_idx][0]:  #VF_idx][IF_idx]:
                runtime = self.runtimes[current_filename][
                    self.current_pragma_idx][unroll_idx][0]  #VF_idx][IF_idx]
            else:
                runtime = get_runtime(self.new_rundir, new_code,
                                      current_filename)
                self.runtimes[current_filename][self.current_pragma_idx][
                    unroll_idx][0] = runtime  #VF_idx][IF_idx]=runtime
            if self.O3_runtimes[current_filename] == None:
                reward = 0
                logger.warning(
                    'Program ' + current_filename +
                    ' does not compile in two seconds.' +
                    ' Consider removing it or increasing the timeout parameter'
                    + ' in utility.py.')
            elif runtime == None:
                #penalizing for long compilation time for bad VF/IF
                reward = -9
            else:
                reward = (self.O3_runtimes[current_filename] -
                          runtime) / self.O3_runtimes[current_filename]
            # In inference mode and finished inserting pragmas to this file.
            if self.inference_mode and self.current_pragma_idx + 1 == self.num_loops[
                    current_filename]:
                improvement = self.O3_runtimes[current_filename] / runtime
                self.improvements.append(improvement)
                geomean = 1
                for imp in self.improvements:
                    geomean = geomean * (imp**(1 / len(self.improvements)))
                print('benchmark: ', current_filename, 'O3 runtime: ',
                      self.O3_runtimes[current_filename], 'RL runtime: ',
                      runtime, 'improvement:',
                      str(round(improvement, 2)) + 'X',
                      'improvement geomean so far:',
                      str(round(geomean, 2)) + 'X')
            #VF = self.vec_action_meaning[VF_idx]
            #IF = self.interleave_action_meaning[IF_idx]
            unroll = self.unroll_action_meaning[unroll_idx]
            opt_runtime_sofar = self.get_opt_runtime(current_filename,
                                                     self.current_pragma_idx)
            logger.info(
                current_filename + ' runtime ' + str(runtime) + ' O3 ' +
                str(self.O3_runtimes[current_filename]) + ' reward ' +
                str(reward) + ' opt ' +
                str(opt_runtime_sofar))  #+" VF "+str(VF)+" IF "+str(IF))
        else:
            # can't calculate the reward without compile/runtime.
            reward = 0

        return reward
Exemple #2
0
 def get_reward(self, new_code, current_filename):
     f = open(current_filename, 'w')
     f.write(''.join(new_code))
     f.close()
     if self.compile:
         runtime = get_runtime(self.new_rundir, new_code, current_filename)
         reward = (self.O3_runtimes[current_filename] -
                   runtime) / self.O3_runtimes[current_filename]
         if self.inference_mode and self.current_pragma_idx + 1 == self.num_loops[
                 current_filename]:  # in inference mode and finished inserting pragmas to this file
             print('benchmark: ', current_filename, 'O3 runtime: ',
                   self.O3_runtimes[current_filename], 'RL runtime: ',
                   runtime)
     else:
         reward = 0  # can't calculate the reward without runtime
     return reward