def get_gpu(card) -> GPU: if card == "eGPU": return GPU.e_gpu() elif card == "iGPU": return GPU.i_gpu() else: raise Exception(f"Unknown card: {card}")
def __init__(self, function): self.gpu = GPU(0) self.stream = None self.pr = cProfile.Profile() self.function = function self.statistics = List[Statistics] self.header_written = False
class Profiler: base_mem_used = GPU(0).mem_used filename: Path = Path('stats.csv') def __init__(self, function): self.gpu = GPU(0) self.stream = None self.pr = cProfile.Profile() self.function = function self.statistics = List[Statistics] self.header_written = False def __call__(self, train_loader, test_loader, epoch: int, device, optimizer, scheduler) -> Tuple[float, float]: if self.stream is None: self.stream = Profiler.filename.open('w+') print(f"AdaS: Profiler: Writing csv to {Profiler.filename}") self.gpu.update() self.pr.enable() result = memory_usage(proc=(self.function, (train_loader, test_loader, epoch, device, optimizer, scheduler)), max_usage=True, retval=True) self.pr.disable() s = io.StringIO() sortby = SortKey.CUMULATIVE ps = pstats.Stats(self.pr, stream=s).sort_stats(sortby) ps.print_stats( "epoch_iteration|step|trial_iteration|test_main|evaluate") stats_list = pstats_to_dict(s.getvalue()) header = 'epoch,epoch_gpu_mem_used,epoch_ram_used' content = f'{epoch},{self.gpu.mem_used - Profiler.base_mem_used},{result[0]}' for stat in stats_list: header += f",{stat['name']}_n_calls" header += f",{stat['name']}_tot_time" header += f",{stat['name']}_per_call1" header += f",{stat['name']}_cum_time" header += f",{stat['name']}_per_call2" content += f",{stat['n_calls']}" content += f",{stat['tot_time']}" content += f",{stat['per_call1']}" content += f",{stat['cum_time']}" content += f",{stat['per_call2']}" header += '\n' content += '\n' if not self.header_written: self.stream.write(header) self.header_written = True self.stream.write(content) return result[1] def __exit__(self, exc_type, exc_value, traceback) -> None: self.stream.close()
def startup() -> bool: print("Auto select GPU") e_gpu = GPU.e_gpu() i_gpu = GPU.i_gpu() return_value = False if e_gpu.status == Status.Missing: return_value = return_value | i_gpu.enable() else: return_value = return_value & e_gpu.enable() return_value = return_value & i_gpu.disable() return return_value
def select_card(card) -> bool: print(f"Select card: {card}") e_gpu = GPU.e_gpu() i_gpu = GPU.i_gpu() if card == "eGPU": e_gpu.enable() i_gpu.disable() elif card == "iGPU": i_gpu.enable() e_gpu.disable() else: raise Exception(f"Unknown card: {card}") return False
def get(self): self.response.headers['Content-Type'] = 'text/html' user = users.get_current_user() if user == None: template_values = { 'login_url' : users.create_login_url(self.request.uri) } template = JINJA_ENVIRONMENT.get_template('mainpage_guest.html') self.response.write(template.render(template_values)) return myuser_key = ndb.Key('MyUser', user.user_id()) myuser = myuser_key.get() if myuser == None: myuser = MyUser(id=user.user_id()) myuser.put() gpu_query = GPU().query().fetch() template_values = { 'logout_url' : users.create_logout_url(self.request.uri), 'gpu' : gpu_query } template = JINJA_ENVIRONMENT.get_template('mainpage.html') self.response.write(template.render(template_values))
def get_data(self): gpu = GPU() usage = Usage(3) data = [] data_list = [ usage.cpu_csv(), usage.disk_csv(), usage.mem_ram_csv(), usage.mem_swap_csv(), gpu.csv() ] for i in data_list: data += i.split(',') return data
def __init__(self): self.logger = Log() # Create components self.cpu = CPU(self) self.memory = Memory(self) self.interrupts = Interrupts(self) self.screen = Screen(self) self.gpu = GPU(self) self.debug_mode = False self.step_mode = False
def __init__(self, path, repository, user): self.path = path self.gpu = GPU() self.usage = Usage(15) self.headers = [ self.usage.cpu_header, self.usage.disk_header, self.usage.mem_ram_header, self.usage.mem_swap_header, self.gpu.header ] self.values = [ self.usage.cpu_csv, self.usage.disk_csv, self.usage.mem_ram_csv, self.usage.mem_swap_csv, self.gpu.csv ] self.logger = Logger(os.path.join(self.path, repository), delay=0) self.logger.set_header(self.headers) self.logger.set_csv(self.values) self.saver = Saver() self.saver.get_repository(path, repository, user)
class GB: """ GB components instantiation """ def __init__(self): self.logger = Log() # Create components self.cpu = CPU(self) self.memory = Memory(self) self.interrupts = Interrupts(self) self.screen = Screen(self) self.gpu = GPU(self) self.debug_mode = False self.step_mode = False def execute(self, cartridge_data: bytes, debug: bool = False, step: bool = False): """ Execution main loop. :param cartridge_data: game to execute :param debug: If will run in debug mode or not :param step: If it will stop after executing each loop or not. Requires debug==True. """ self.print_cartridge_info(cartridge_data) self.gpu.prepare() self.step_mode = step self.debug_mode = debug self.logger.setDebugMode(self.debug_mode) self.logger.info("Debug: %s\tStep: %s",self.debug_mode,self.step_mode) self.memory.load_cartridge(cartridge_data) if self.memory.boot_rom is None: self.cpu.register.skip_boot_rom() # Instantiates the emulator screen. It will assume control of the main thread, so the emulator main loop must be # triggered by the Screen itself, as a scheduled method call. self.screen.run() def print_cartridge_info(self, cartridge_data: bytes): """ Prints the cartridge header info. See: http://gbdev.gg8.se/files/docs/mirrors/pandocs.html#thecartridgeheader """ title = cartridge_data[0x0134:0x143].replace(b'\x00', b'').decode("ascii") self.logger.info("Title: %s", title) cartridge_type_dict = {0x00:"ROM ONLY",0x01:"MBC1",0x02:"MBC1+RAM",0x03:"MBC1+RAM+BATTERY", 0x05:"MBC2",0x06:"MBC2+BATTERY",0x08:"ROM+RAM",0x09:"ROM+RAM+BATTERY", 0x0B:"MMM01",0x0C:"MMM01+RAM",0x0D:"MMM01+RAM+BATTERY", 0x0F:"MBC3+TIMER+BATTERY",0x10:"MBC3+TIMER+RAM+BATTERY",0x11:"MBC3", 0x12:"MBC3+RAM",0x13:"MBC3+RAM+BATTERY",0x15:"MBC4",0x16:"MBC4+RAM", 0x17:"MBC4+RAM+BATTERY",0x19:"MBC5",0x1A:"MBC5+RAM",0x1B:"MBC5+RAM+BATTERY", 0x1C:"MBC5+RUMBLE",0x1D:"MBC5+RUMBLE+RAM",0x001E:"MBC5+RUMBLE+RAM+BATTERY", 0xFC:"POCKET CAMERA",0xFD:"BANDAI TAMA5",0xFE:"HuC3",0xFF:"HuC1+RAM+BATTERY"} self.logger.info("Cartridge: %s",cartridge_type_dict[cartridge_data[0x0147]]) rom_size = {0x00:"32KByte (no ROM banking)",0x01:"64KByte (4 banks)",0x02:"128KByte (8 banks)", 0x03:"256KByte (16 banks)",0x04:"512KByte (32 banks)", 0x05:"1MByte (64 banks) - only 63 banks used by MBC1", 0x06:"2MByte (128 banks) - only 125 banks used by MBC1", 0x07:"4MByte (256 banks)",0x52:"1.1MByte (72 banks)",0x53:"1.2MByte (80 banks)", 0x54:"1.5MByte (96 banks)"} self.logger.info("ROM Size: %s", rom_size[cartridge_data[0x0148]]) ram_size = {0x00:"None",0x01:"2 KBytes",0x02:"8 Kbytes",0x03:"32 KBytes (4 banks of 8KBytes each)"} self.logger.info("RAM Size: %s", ram_size[cartridge_data[0x0149]]) destination = {0x00:"Japanese",0x01:"Non-Japanese"} self.logger.info("Destination: %s", destination[cartridge_data[0x014A]]) self.logger.info("Version: %d", cartridge_data[0x014C]) def debug(self): """ Prints debug info to console. """ self.cpu.debug() self.memory.debug() # Makes execution really slow self.interrupts.debug() self.gpu.debug() self.logger.debug("---")
def show_status(adapter_list=None): adapter_info = get_adapter_info() output = [] for index, info in enumerate(adapter_info): if adapter_list is None or index in adapter_list: gpu = GPU(); ###ADD TO GPU OBJECT gpu.index = index gpu.name = info.strAdapterName gpu.display = info.strDisplayName activity = ADLPMActivity() activity.iSize = sizeof(activity) if ADL_Overdrive5_CurrentActivity_Get(info.iAdapterIndex, byref(activity)) != ADL_OK: raise ADLError("ADL_Overdrive5_CurrentActivity_Get failed.") ###ADD TO GPU OBJECT gpu.engine = (activity.iEngineClock/100.0) gpu.memory = (activity.iMemoryClock/100.0) gpu.volts = (activity.iVddc/1000.0) gpu.performance = (activity.iCurrentPerformanceLevel) gpu.utilization = (activity.iActivityPercent) fan_speed = {} for speed_type in (ADL_DL_FANCTRL_SPEED_TYPE_PERCENT, ADL_DL_FANCTRL_SPEED_TYPE_RPM): fan_speed_value = ADLFanSpeedValue() fan_speed_value.iSize = sizeof(fan_speed_value) fan_speed_value.iSpeedType = speed_type if ADL_Overdrive5_FanSpeed_Get(info.iAdapterIndex, 0, byref(fan_speed_value)) != ADL_OK: fan_speed[speed_type] = None continue fan_speed[speed_type] = fan_speed_value.iFanSpeed user_defined = fan_speed_value.iFlags & ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED if bool(fan_speed[ADL_DL_FANCTRL_SPEED_TYPE_PERCENT]) and bool(fan_speed[ADL_DL_FANCTRL_SPEED_TYPE_RPM]): ###ADD TO GPU OBJECT gpu.fan = fan_speed[ADL_DL_FANCTRL_SPEED_TYPE_PERCENT] elif bool(fan_speed[ADL_DL_FANCTRL_SPEED_TYPE_PERCENT]): ###ADD TO GPU OBJECT gpu.fan = fan_speed[ADL_DL_FANCTRL_SPEED_TYPE_PERCENT] elif bool(fan_speed[ADL_DL_FANCTRL_SPEED_TYPE_RPM]) is True: ###ADD TO GPU OBJECT gpu.fan = fan_speed[ADL_DL_FANCTRL_SPEED_TYPE_RPM] else: print " unable to get fan speed" temperature = ADLTemperature() temperature.iSize = sizeof(temperature) if ADL_Overdrive5_Temperature_Get(info.iAdapterIndex, 0, byref(temperature)) != ADL_OK: raise ADLError("ADL_Overdrive5_Temperature_Get failed.") ###ADD TO GPU OBJECT gpu.temp = temperature.iTemperature/1000.0 # Powertune level powertune_level_value = c_int() dummy = c_int() if ADL_Overdrive5_PowerControl_Get(info.iAdapterIndex, byref(powertune_level_value), byref(dummy)) != ADL_OK: raise ADLError("ADL_Overdrive5_PowerControl_Get failed.") ###ADD TO GPU OBJECT gpu.power = powertune_level_value.value gpu.setObject(); if gpu is not None: output.append(gpu) return output
start_time = time.time() while True: try: delay = self.delay - (time.time() - start_time) if delay > 0: print('Delay for: {:.2f}s'.format(delay)) time.sleep(delay) except Exception as e: print(e) start_time = time.time() self.append_csv(self.csv) if __name__ == '__main__': gpu = GPU() usage = Usage(3) headers = [ usage.cpu_header, usage.disk_header, usage.mem_ram_header, usage.mem_swap_header, gpu.header ] values = [ usage.cpu_csv, usage.disk_csv, usage.mem_ram_csv, usage.mem_swap_csv, gpu.csv ] path = os.path.dirname(os.path.realpath(__file__)) logger = Logger(path, delay=5) logger.set_header(headers) logger.set_csv(values)
def post(self): self.response.headers['Content-Type'] = 'text/html' action = self.request.get('button') if action == 'Add Information': name = self.request.get('name') manufacturer = self.request.get('manufacturer') #date_gpu = datetime.strptime(self.request.get('dateissued'), '%Y-%m-%d').date() #date_gpu_calender = calendar.timegm(date_gpu.timetuple()) #dateissued = datetime.utcfromtimestamp(date_gpu_calender) dateissued = self.request.get('dateissued') #logger.info(dateissued) geometryShader = self.request.POST.get('geometryShader') == 'on' tesselationShader = self.request.POST.get('tesselationShader') == 'on' shaderInt16 = self.request.POST.get('shaderInt16') == 'on' sparseBinding = self.request.POST.get('sparseBinding') == 'on' textureCompressionETCS = self.request.POST.get('textureCompressionETCS') == 'on' vertexPipelineStoresAndAtomics = self.request.POST.get('vertexPipelineStoresAndAtomics') == 'on' if name == '': self.redirect('/') elif manufacturer == '': self.redirect('/') elif dateissued == '': self.redirect('/') else: gpu_list = GPU.query() if geometryShader: gpu_list.filter(GPU.geometryShader == True) if tesselationShader: gpu_list.filter(GPU.tesselationShader==True) if shaderInt16: gpu_list.filter(GPU.shaderInt16==True) if sparseBinding: gpu_list.filter(GPU.sparseBinding==True) if textureCompressionETCS: gpu_list.filter(GPU.textureCompressionETCS==True) if vertexPipelineStoresAndAtomics: gpu_list.filter(GPU.vertextPipelineStoresandAtomics==True) gpu_list = gpu_list.fetch() user = users.get_current_user() mygpu_key = ndb.Key('GPU', name) mygpu = mygpu_key.get() mygpu.gpu.append(gpu_list) mygpu.put() template_values = { 'gpu_list': gpu_list, 'geometryShader': geometryShader, 'tesselationShader': tesselationShader, 'shaderInt16': shaderInt16, 'sparseBinding': sparseBinding, 'textureCompressionETCS': textureCompressionETCS, 'vertextPipelineStoresandAtomics': vertexPipelineStoresAndAtomics } template = JINJA_ENVIRONMENT.get_template('mainpage.html') self.response.write(template.render(template_values))
def post(self): self.response.headers['Content - Type'] = 'text / html' action = self.request.get('button') if action == 'Add Information': name = self.request.get('name') manufacturer = self.request.get('manufacturer') dateissued = self.request.get('dateissued') geometryShader = self.request.POST.get('geometryShader') == 'on' tesselationShader = self.request.POST.get('tesselationShader') == 'on' shaderInt16 = self.request.POST.get('shaderInt16') == 'on' sparseBinding = self.request.POST.get('sparseBinding') == 'on' textureCompressionETCS = self.request.POST.get('textureCompressionETCS') == 'on' vertexPipelineStoresAndAtomics = self.request.POST.get('vertexPipelineStoresAndAtomics') == 'on' if name == '': self.redirect('/') elif manufacturer == '': self.redirect('/') elif dateissued =='': self.redirect('/') else: user = users.get_current_user() myuser_key = ndb.Key('MyUser', user.user_id()) myuser = myuser_key.get() gpu_key = ndb.Key('GPU',name) my_gpu = gpu_key.get() if my_gpu == None: new_gpu = GPU(id=name,name=name, manufacturer=manufacturer,dateissued=datetime.strptime(dateissued,'%Y-%m-%d').date(),geometryShader=geometryShader, tesselationShader=tesselationShader, shaderInt16=shaderInt16, sparseBinding=sparseBinding, textureCompressionETCS=textureCompressionETCS, vertexPipelineStoresAndAtomics=vertexPipelineStoresAndAtomics) myuser.gpu.append(new_gpu) new_gpu.put() self.redirect('/') else: template_values = { 'msg' : 'Sorry !! You cannot create GPU with already existing name. ' } template= JINJA_ENVIRONMENT.get_template('mainpage.html') self.response.write(template.render(template_values)) elif action == 'Gpu Feature Index': geometryShader = bool(self.request.get('geometryShader')) tesselationShader = bool(self.request.get('tesselationShader')) shaderInt16 = bool(self.request.get('shaderInt16')) sparseBinding = bool(self.request.get('sparseBinding')) textureCompressionETCS = bool(self.request.get('textureCompressionETCS')) vertexPipelineStoresAndAtomics = bool(self.request.get('vertexPipelineStoresAndAtomics')) my_gpu = GPU.query() if geometryShader: my_gpu = my_gpu.filter(GPU.geometryShader == True) if tesselationShader: my_gpu = my_gpu.filter(GPU.tesselationShader == True) if shaderInt16: my_gpu = my_gpu.filter(GPU.shaderInt16 == True) if sparseBinding: my_gpu = my_gpu.filter(GPU.sparseBinding == True) if textureCompressionETCS: my_gpu = my_gpu.filter(GPU.textureCompressionETCS == True) if vertexPipelineStoresAndAtomics: my_gpu = my_gpu.filter(GPU.vertexPipelineStoresAndAtomics == True) template_values = { 'gpu': my_gpu } template = JINJA_ENVIRONMENT.get_template('mainpage.html') self.response.write(template.render(template_values)) elif action == 'Compare': my_gpu = {} gpu_id = self.request.get('gpuID', allow_multiple=True) for i in range(len(gpu_id)): my_gpu[i] = GPU.query() my_gpu[i] = my_gpu[i].filter(GPU.name == gpu_id[i]) template_values = { 'gpu' : my_gpu, 'val' : len(gpu_id) } template = JINJA_ENVIRONMENT.get_template('Compare.html') self.response.write(template.render(template_values))
def __init__(self, miner, mode, devIds, fanSpeeds, steps, nbrOfShares, nbrOfDatapoints, marginInMH, coreUCs, memOCs, powerLimits, powerCost, dollarPerMHash): # give the worker a name to separate data on pool self.workerName = socket.gethostname().replace("-", "").replace( ".", "").replace("_", "") # anonymize workerName self.workerName = hashlib.md5( self.workerName.encode('utf-8')).hexdigest() # initialize logging utils self.log = Log("DEBUG") # prepare unix systems if platform.system() != "Windows": subprocess.call(["chmod", "a+x", "./prepareUnix.sh"]) subprocess.call(["./prepareUnix.sh"]) # utility if mode < 0 or mode > 2: self.log.Warning( "invalid optimization mode \"%i\" - will default to best efficiency" % mode) mode = 0 self.modeText = "BEST EFFICIENCY" if mode == 1: self.modeText = "BEST SPEED" if mode == 2: self.modeText = "BEST PROFITABILITY" # save base hw settings self.devIds = devIds # if no devices were specified, take all if devIds == None: devIds = range(64) # collect all GPUs connected and initialize self.gpus = [] self.overheatedGPUs = [] ids = [] for i in range(len(devIds)): # set default values if no enough values were specified if len(powerLimits) <= i: powerLimits.append(None) if len(memOCs) <= i: memOCs.append(0) if len(coreUCs) <= i: coreUCs.append(0) if len(fanSpeeds) <= i: fanSpeeds.append(70) gpu = GPU(self.log, devIds[i], mode, memOCs[i], coreUCs[i], fanSpeeds[i], steps, powerLimits[i], nbrOfShares, nbrOfDatapoints, marginInMH, powerCost, dollarPerMHash) if gpu.found: ids.append(devIds[i]) self.gpus.append(gpu) # set starting power level self.gpus[i].SetPowerLimit(powerLimits[i]) self.log.Info("found GPU%i - %s" % (i, self.gpus[i].name)) else: fanSpeeds.remove(fanSpeeds[len(fanSpeeds) - 1]) self.log.Warning( "could not find GPU%i - will work with the found onces" % i) break self.log.Info("initialized %i GPUs" % len(self.gpus)) # if we could not find any GPU, we will exit if len(self.gpus) == 0: self.log.Error( "could not find any GPUs with given IDs %s - cannot tune clocks and will exit now" % devIds) exit(1) # initialize mining software starter self.ms = StartMiner(self.log, self.workerName, miner, ids, fanSpeeds) # initialize mining data requester - miner is not started yet, so we cannot request any data for now self.req = MinerDataRequester(self.log, miner, self.ms) # first tune mem and core clocks until all GPUs are finished tuningDone = 0 self.requiresRestart = True saveOldData = False self.ResetGPUs(False, False) while tuningDone < len(self.gpus): # test if running mining Software has crashed crashed = False if self.ms.isRunning and self.ms.ProcessesChanged(): self.log.Info("Mining Software seems to have crashed/changed") self.MiningSoftwareCrashed() crashed = True # start mining software if it is not running if not self.ms.isRunning: self.ReStartMiningSoftware() # if all GPUs are finished with tuning, or software crashed, restart if (self.requiresRestart and self.GPUsFinishedTuning()) or crashed: self.ResetGPUs(saveOldData, crashed) self.RemoveOverheatingGPUs() self.ReStartMiningSoftware() elif self.requiresRestart: self.log.Debug( "restart pending but not all GPUs are finished with their tuning" ) saveOldData = True minerData = self.req.getData() # tune GPUs tuningDone = self.OC(minerData) # wait some time between runs time.sleep(sleepBetweenTuningRuns) # as we are complete, print resultdata of each GPU self.log.Info( "### TUNING COMPLETED FOR %i GPUS with Mode: \"%s\" ###" % (len(self.gpus), self.modeText)) for gpu in self.gpus: plPerc = math.ceil(100.0 / gpu.defaultPowerLimit * gpu.powerLimit) self.log.Info( "GPU%i: Max Memory Overclock: %i\tMax Core UnderClock: %i\tMin Power Limit: %iW (%i%%)\tFan: %i%%" % (gpu.id, gpu.memOC, gpu.coreUC, gpu.powerLimit, plPerc, gpu.fanSpeed)) for gpu in self.overheatedGPUs: plPerc = math.ceil(100.0 / gpu.defaultPowerLimit * gpu.powerLimit) self.log.Info( "!OVERHEATED! GPU%i: Memory Overclock: %i\tCore UnderClock: %i\tPower Limit: %iW (%i%%)\tFan: %i%%" % (gpu.id, gpu.memOC, gpu.coreUC, gpu.powerLimit, plPerc, gpu.fanSpeed)) # stop mining if no GPU is left if len(self.gpus) == 0: self.ms.Stop()
return rom bios_path = input("Enter a relative path to the BIOS: ") bios = load_rom(bios_path) # Load bios rom_path = input("Enter a relative path to the ROM: ") rom = load_rom(rom_path) # Load rom mem = Memory(0x10000, rom=rom, bios=bios) mem.load_bios() mem.load_rom() # TODO: Account for ROM banking cpu = CPU(mem) # Initialize CPU. To see stuff happen, make silent=False gpu = GPU(mem, scale=2) # Initialize GPU mem.cpu = cpu gpu.start() clock_rate = 4194304 gpu_rate = 456 cycles = 0 highest_pc = -1 while True: cycles += cpu.cycle() if cycles > gpu_rate: cycles -= gpu_rate cpu.mem[LY] += 1 if cpu.mem[LY] == 144: gpu.update()