def append(self, node): if isinstance(node, Writer): for line in node.buf: self.buf.append(line) elif isinstance(node, compiler.Marker): self.buf.append(node) elif isinstance(node, compiler.Label): self.buf.append(node) else: self.buf.append(newline(str(node)))
def eval_offsets(self): repeat = True offsets = {} tmp_buf = self.buf while repeat: unresolved_markers = False unresolved_addresses = False for line_cnt, elem in enumerate(tmp_buf): if isinstance(elem, compiler.Label): offsets[elem.label] = line_cnt self.buf.remove(elem) unresolved_labels = True elif isinstance(elem, compiler.Marker): if not elem.label in offsets: # print "Found an unresolved marker" unresolved_markers = True else: offset = offsets[elem.label] self.buf[self.buf.index(elem)] = newline(offset) repeat = unresolved_markers or unresolved_addresses tmp_buf = self.buf
def _save_targets_not_yet_processed(targets, filename): with open(filename, 'w') as fp: writer = csv.DictWriter(fp, ['target', 'repeated'], lineterminator=u.newline()) writer.writeheader() for target in targets: writer.writerow(target)
def look(self): rtn_str = self.describe_room() + newline(1) rtn_str += self.describe_doors() return rtn_str
def describe_room(self): rtn_str = 'You are in the '+self.name + newline(1) rtn_str += self.description + newline(2) rtn_str += 'You see %s door' % number_name(door_count(self.doors)) rtn_str += plural_str(self.doors) + newline(1) return rtn_str
def energy(user_func, *args, powerLoss = 0.8, year, printToScreen, timeseries): """ Evaluates the kwh needed for your code to run Parameters: user_func (function): user's function Returns: (process_kwh, return_value, watt_averages) """ baseline_check_seconds = 5 files, multiple_cpus = utils.get_files() is_nvidia_gpu = utils.valid_gpu() is_valid_cpu = utils.valid_cpu() # GPU handling if Nvidia gpu_baseline =[0] gpu_process = [0] bash_command = "nvidia-smi -i 0 --format=csv,noheader --query-gpu=power.draw" time_baseline = [] reading_baseline_wattage = [] time_process = [] reading_process_wattage = [] for i in range(int(baseline_check_seconds / DELAY)): if is_nvidia_gpu: output = subprocess.check_output(['bash','-c', bash_command]) output = float(output.decode("utf-8")[:-2]) gpu_baseline.append(output) if is_valid_cpu: files = utils.measure_files(files, DELAY) files = utils.update_files(files) else: time.sleep(DELAY) # Adds the most recent value of GPU; 0 if not Nvidia last_reading = utils.get_total(files, multiple_cpus) + gpu_baseline[-1] if last_reading >=0 and printToScreen: utils.log("Baseline wattage", last_reading) time = round(i* DELAY, 1) time_baseline.append(time) reading_baseline_wattage.append(last_reading) if timeseries: with open('baseline_wattage.csv', 'w') as baseline_wattage_file: baseline_wattage_writer = csv.writer(baseline_wattage_file) baseline_wattage_writer.writerow(["time", "baseline wattage reading"]) for i in range(len(time_baseline)): baseline_wattage_writer.writerow([time_baseline[i], reading_baseline_wattage[i]]) if printToScreen: utils.newline() # Running the process and measuring wattage q = Queue() p = Process(target = func, args = (user_func, q, *args,)) start = timer() small_delay_counter = 0 return_value = None p.start() while(p.is_alive()): # Checking at a faster rate for quick processes if (small_delay_counter > DELAY): delay = DELAY / 10 small_delay_counter+=1 else: delay = DELAY if is_nvidia_gpu: output = subprocess.check_output(['bash','-c', bash_command]) output = float(output.decode("utf-8")[:-2]) gpu_process.append(output) if is_valid_cpu: files = utils.measure_files(files, delay) files = utils.update_files(files, True) else: time.sleep(delay) # Just output, not added last_reading = (utils.get_total(files, multiple_cpus) + gpu_process[-1]) / powerLoss if last_reading >=0 and printToScreen: utils.log("Process wattage", last_reading) time = round(timer()-start, 1) time_process.append(time) reading_process_wattage.append(last_reading) # Getting the return value of the user's function try: return_value = q.get_nowait() break except queue.Empty: pass if timeseries: with open('process_wattage.csv', 'w') as process_wattage_file: process_wattage_writer = csv.writer(process_wattage_file) process_wattage_writer.writerow(["time", "process wattage reading"]) for i in range(len(time_process)): process_wattage_writer.writerow([time_process[i], reading_process_wattage[i]]) p.join() end = timer() for file in files: file.process = file.process[1:-1] file.baseline = file.baseline[1:-1] if is_nvidia_gpu: gpu_baseline_average = statistics.mean(gpu_baseline[2:-1]) gpu_process_average = statistics.mean(gpu_process[2:-1]) else: gpu_baseline_average = 0 gpu_process_average = 0 total_time = end-start # seconds # Formatting the time nicely timedelta = str(datetime.timedelta(seconds=total_time)).split('.')[0] if files[0].process == []: raise Exception("Process executed too fast to gather energy consumption") files = utils.average_files(files) process_average = utils.get_process_average(files, multiple_cpus, gpu_process_average) baseline_average = utils.get_baseline_average(files, multiple_cpus, gpu_baseline_average) difference_average = process_average - baseline_average watt_averages = [baseline_average, process_average, difference_average, timedelta] # Subtracting baseline wattage to get more accurate result process_kwh = convert.to_kwh((process_average - baseline_average)*total_time) / powerLoss if is_nvidia_gpu: gpu_file = file("GPU", "") gpu_file.create_gpu(gpu_baseline_average, gpu_process_average) files.append(file("GPU", "")) # Logging if printToScreen: utils.log("Final Readings", baseline_average, process_average, difference_average, timedelta) return (process_kwh, return_value, watt_averages, files, total_time, time_baseline, reading_baseline_wattage, time_process, reading_process_wattage)