def create(self, romFilePath): """Creates a new project from a ROM.""" r = ROM(romFilePath) if not r: return False self.files = {} for f in map(lambda x: loadFile(x, self), r.getFiles()): if f: self.files[f.label] = f r.close() for b, projectFile in self.files.items(): projectFile.finishSetup() return True
def finish(self): self.ROM = ROM.LagrangeROM(self.histories['soln'], self.histories['solwt'], self.histories['probs'], self.varDict, self.histories['varVals'], self.indexSet, self.quadrule, self.numprocs) super(SC, self).finish()
def testApplyPatchesConsistentlyAndCorrectly(self): """ Test applying a patch against all types of clean ROMs, and ensuring that the produced ROM is identical to the source ROM. """ metadata = json.dumps({"patcher": "EBPatcher", "author": "x", "title": "y", "description": "z"}) cleanBaseRom = self.cleanRoms[0] for modifiedRom in self.modifiedRoms: # Create a patch of this modified ROM # We can use any arbitrary clean rom as the base since patches are # created consistently, as proven in the above test. if isfile(self.TMP_EBP_FNAME): remove(self.TMP_EBP_FNAME) patch = EBPPatch(self.TMP_EBP_FNAME, new=True) patch.createFromSource(cleanBaseRom, modifiedRom, metadata) del patch patch = EBPPatch(self.TMP_EBP_FNAME) # Create a copy of the modified rom without a header modifiedRomCopy = ROM(modifiedRom.romPath) if modifiedRomCopy.checkHeader() > 0: modifiedRomCopy.removeHeader() for cleanRom in self.cleanRoms: # Create a new temporary rom if isfile(self.TMP_ROM_FNAME): remove(self.TMP_ROM_FNAME) copyfile(cleanRom.romPath, self.TMP_ROM_FNAME) targetRom = ROM(self.TMP_ROM_FNAME) # Apply the patch to the temporary ROM patch.applyToTarget(targetRom) # Compare the patched ROM vs the de-headered modified ROM inputChecksum = checksumOfRom(modifiedRomCopy) outputChecksum = checksumOfRom(targetRom) print(cleanRom.romPath, "(", inputChecksum[:5], ") *", modifiedRom.romPath, "=", outputChecksum[:5]) self.assertEqual(inputChecksum, outputChecksum)
def load_state_dict(self, state_dict, mode='train'): # mode can be 'train' to continue training or 'predict' to do inference with the model self.dtype = state_dict['dtype'] self.rom = ROM.ROM() self.rom.load_state_dict(state_dict['rom_state_dict']) self.rom_autograd = self.rom.get_autograd_fun() if mode == 'train': self.data = dta.StokesData(state_dict['supervised_samples'], state_dict['unsupervised_samples']) self.data.read_data() self.data.reshape_microstructure_image() self.dim_z = state_dict['dim_z'] self.z_mean = state_dict['z_mean'] self.lr_z = state_dict['lr_z'] self.zOpt = optim.Adam([self.z_mean], lr=self.lr_z, betas=(.3, .5)) self.zOpt.load_state_dict(state_dict['zOpt_state_dict']) self.zOptSched = optim.lr_scheduler.ReduceLROnPlateau(self.zOpt, factor=.2, verbose=True) self.zOptSched.load_state_dict(state_dict['zOptSched_state_dict']) self.batch_size_z = state_dict['batch_size_z'] self.pfNet = PfNet(self.dim_z, self.data.img_resolution**2) self.pfNet.load_state_dict(state_dict['pfNet_state_dict']) self.pfOpt = optim.Adam(self.pfNet.parameters()) self.pfOpt.load_state_dict(state_dict['pfNet_optimizer_state_dict']) self.pfOptSched = optim.lr_scheduler.ReduceLROnPlateau(self.zOpt, factor=.2, verbose=True, min_lr=1e-9) self.pfOptSched.load_state_dict(state_dict['pfOptSched_state_dict']) self.batch_size_N_thetaf = state_dict['batch_size_N_thetaf'] self.batch_size_N_lambdac = state_dict['batch_size_N_lambdac'] self.pcNet = PcNet(self.dim_z, self.rom.mesh.n_cells) self.pcNet.load_state_dict(state_dict['pcNet_state_dict']) self.pcOpt = optim.Adam(self.pcNet.parameters()) self.pcOpt.load_state_dict(state_dict['pcNet_optimizer_state_dict']) self.thetacSched = optim.lr_scheduler.ReduceLROnPlateau(self.pcOpt, factor=.2, verbose=True, min_lr=1e-9) self.thetacSched.load_state_dict(state_dict['thetacSched_state_dict']) self.tauc = state_dict['tauc'] self.batch_size_N_thetac = state_dict['batch_size_N_thetac'] self.log_lambdac_mean = state_dict['log_lambdac_mean'] self.log_lambdac_mean_tensor = state_dict['log_lambdac_mean_tensor'] self.taucf = state_dict['taucf'] self.training_iterations = state_dict['training_iterations'] if __debug__ and self.writeParams: try: self.writer = state_dict['writer'] except KeyError: # If no writer was saved, set writer to None self.writer = None
def main(): parser = argparse.ArgumentParser(description="NES Emulator.") parser.add_argument('rom_path', metavar='ROM path', type=str, help='path to nes rom') args = parser.parse_args() print("Path to ROM: " + args.rom_path) rom = ROM() rom.read_data(args.rom_path) for i in rom.read_bytes(0, 100): print(i) ram = RAM() cpu = CPU() cpu.start_up(ram) cpu.A = 0 cpu.PC = 16 instr = ADC() cpu.ram.write_bytes(0, bytearray([5, 7])) print("Before execution: " + str(cpu.A)) cpu.executeProgram(rom.read_bytes(16, 4)) print("After execution: " + str(cpu.A))
def write_fetch_cycles(): # Go through all possible instructinos and flags for i in range(1 << 11): for step, conrtol_lines in enumerate(FETCH): address = i << 4 | step if ROM_NUM == 0: byte = 0 for signal in conrtol_lines: if signal in CTRL_LINES: byte = byte | CTRL_LINES[signal] # Invert active low signals (ALU_OUT, HLT, NXT) byte = byte ^ 0b11100000 else: # no In and OUT line is 0xff byte = 0xff for signal in conrtol_lines: if signal in IN_OUT_LINES: byte = byte & IN_OUT_LINES[signal] ROM.write(address, byte)
def setUp(self): # Some parameters lin_dim_rom = 2 # Linear number of rom elements a = np.array([1, 1, 0]) # Boundary condition function coefficients dtype = torch.float # Tensor data type supervised_samples = {n for n in range(8)} unsupervised_samples = {n for n in range(8, 16)} dim_z = 3 self.mesh = PoissonFEM.RectangularMesh( np.ones(lin_dim_rom) / lin_dim_rom) def origin(x): return np.abs(x[0]) < np.finfo(float).eps and np.abs( x[1]) < np.finfo(float).eps def ess_boundary_fun(x): return 0.0 self.mesh.set_essential_boundary(origin, ess_boundary_fun) def domain_boundary(x): # unit square return np.abs(x[0]) < np.finfo(float).eps or np.abs(x[1]) < np.finfo(float).eps or \ np.abs(x[0]) > 1.0 - np.finfo(float).eps or np.abs(x[1]) > 1.0 - np.finfo(float).eps self.mesh.set_natural_boundary(domain_boundary) def flux(x): q = np.array([a[0] + a[2] * x[1], a[1] + a[2] * x[0]]) return q # Specify right hand side and stiffness matrix rhs = PoissonFEM.RightHandSide(self.mesh) rhs.set_natural_rhs(self.mesh, flux) K = PoissonFEM.StiffnessMatrix(self.mesh) rhs.set_rhs_stencil(self.mesh, K) trainingData = dta.StokesData(supervised_samples, unsupervised_samples) trainingData.read_data() trainingData.reshape_microstructure_image() # define rom rom = ROM.ROM(self.mesh, K, rhs, trainingData.output_resolution**2) # finally set up model self.model = GenerativeSurrogate(rom, trainingData, dim_z=dim_z)
def compile(self, romFilePath): """Compiles the project to a ROM.""" r = ROM(romFilePath) if not r: return False self.beginSave() projectFiles = {} for b, f in self.files.items(): projectFiles[DATA["BLOCKS"][b]] = f r.updateFiles(projectFiles) r.updateCRCs() return r.writeToFile(romFilePath)
def INFLOOP(): global LOOP # DEFINITONS OPERATION = "NONE" global RAD global RBD global LRD global INSR global ROD global STEP INSR = ROM.Read(LRD) OPCODE = INSR[0] + INSR[1] + INSR[2] + INSR[3] OPERAND = INSR[4] + INSR[5] + INSR[6] + INSR[7] # INSTRUCTIONS if OPCODE == "0000": ERR = "OK" OPERATION = "NONE" elif OPCODE == "0001": ERR = "OK" OPERATION = "LDA" RAD = RAM.Read(OPERAND) elif OPCODE == "0010": ERR = "OK" OPERATION = "LDB" RBD = RAM.Read(OPERAND) elif OPCODE == "0011": ERR = "OK" OPERATION = "STA" RAM.Write(OPERAND, RAD) elif OPCODE == "0100": ERR = "OK" OPERATION = "STB" RAM.Write(OPERAND, RBD) elif OPCODE == "0101": ERR = "OK" OPERATION = "STO" RAM.Write(OPERAND, ROD) elif OPCODE == "0110": ERR = "OK" OPERATION = "LDAN" RAD = OPERAND elif OPCODE == "0111": ERR = "OK" OPERATION = "LDBN" RBD = OPERAND elif OPCODE == "1000": ERR = "OK" OPERATION = "ADD" ROD = ALU.Add4(RAD, RBD)['OUT'] elif OPCODE == "1001": ERR = "OK" OPERATION = "SUB" ROD = ALU.Sub4(RAD, RBD)['OUT'] elif OPCODE == "1010": ERR = "OK" OPERATION = "JMP" LRD = OPERAND elif OPCODE == "1111": ERR = "OK" OPERATION = "END" LOOP = False else: ERR = "OPCODEERR" OPERATION = "UNKOWN" print('__________________') print('\nREGISTER INFO\n') print('RAD | ' + RAD) print('RBD | ' + RBD) print('LRD | ' + LRD) print('INSR | ' + INSR) print('ROD | ' + ROD) print('STEP | ' + STEP) print('\nOPERATION INFO\n') print('OP | ' + INSR) print('OPCODE | ' + OPCODE) print('OPERAND | ' + OPERAND) print('INS | ' + OPERATION) print('\nRAM INFO\n') COUNTERD = "0000" COUNTERF = "" OP = {'FLAG': 'OK', 'OUT': '0000'} while COUNTERF != "OVERFLOW": COUNTERF = OP['FLAG'] COUNTERD = OP['OUT'] print(COUNTERD + ' : ' + RAM.Read(COUNTERD)) if COUNTERD == "1111": break else: OP = ALU.Add4(COUNTERD, "0001") if not LOOP: print('\n') else: if OPERATION != "JMP": LRD = ALU.Add4(LRD, "0001")['OUT'] return LOOP
byte = byte | CTRL_LINES[signal] # Invert active low signals (ALU_OUT, HLT, NXT) byte = byte ^ 0b11100000 else: # no In and OUT line is 0xff byte = 0xff for signal in conrtol_lines: if signal in IN_OUT_LINES: byte = byte & IN_OUT_LINES[signal] ROM.write(address, byte) ROM.init() if CLEAR: # Clear the ROM (Fill it with HLTs) if ROM_NUM == 0: byte = CTRL_LINES["HLT"] # Invert active low signals (ALU_OUT, HLT, NXT) byte = byte ^ 0b11100000 ROM.fill(byte) else: # All 1s is no IN and not OUT ROM.fill(0xff) if WRITE_FETCH_CYCLES: write_fetch_cycles()
"PUSH A": 0xa0, "PUSH #": 0xa1, "PUSH a": 0xa2, "PUSH w": 0xa6, "PUSH PC+3": 0xa7, "POP A": 0xa8, "POP PC": 0xae, } assert len( sys.argv ) > 1, "You have to specify a file to write to memory as an argument" file = open(sys.argv[1], "r") ROM.init() if CLEAR: ROM.fill(0xff) address = 0 key_points = {} placeholders = {} for line in file: line = line.strip() # Skip empty line and lines with only whitespaces if not line: continue if line.endswith(":"):
def createROMs(self): print '' print 'Beginning HDMR term calculations...' self.ROMs = {} ie = InputEditor.HDMR_IO() #reference run chlist, ident = self.makeCase({}) runfile = ie.writeInput(self.unc_inp_file, chlist, ident) inp_file = GetPot(Filename=runfile) ex = Executor.ExecutorFactory('SC', {}, inp_file) ex.run(verbose=False) os.system('rm ' + runfile) self.ROMs[ident] = ex.ROM # rest of runs numruns = {} for i in range(1, self.hdmr_level + 1): nr = 0 print '\n====================================' print ' STARTING %i-INPUT INTERACTIONS' % i print '====================================\n' new = self.createROMLevel(i, ie) for key, value in new.iteritems(): nr += 1 self.ROMs[key] = value print '\nnumber of order %i runs: %i' % (i, nr) numruns[i] = nr xs = {} for key, value in self.varDict.iteritems(): xs[key] = 1 #for key,value in self.ROMs.iteritems(): # print 'mean',key,':',value.moment(1) print '\nROMs per level:' for key, value in numruns.iteritems(): print ' ', key, value #for rom in self.ROMs.values(): # pk.dump(rom.serializable(),file('hdmr_'+rom.case()+'.pk','w')) self.HDMR_ROM = ROM.HDMR_ROM(self.ROMs, self.varDict) print 'Total Det. Runs:', self.HDMR_ROM.numRunsToCreate() print 'HDMR sampled', self.HDMR_ROM.sample(xs, self.hdmr_level)[1] #store output case = 'hdmr' case += '_' + self.input_file('Sampler/SC/indexSet', '') case += '_N' + str(len(self.varDict)) case += '_H' + str(self.hdmr_level) #case+= '_L'+self.input_file('Sampler/SC/expOrd','') mean = self.HDMR_ROM.moment(self.hdmr_level, r=1, verbose=False) if self.input_file('HDMR/anova', 0) > 0: secm, contribs = self.HDMR_ROM.moment(self.hdmr_level, r=2, anova=True) anovaFileName = case + '.anova' anovaFile = file(anovaFileName, 'w') anovaFile.writelines('Variables,Contribution,Percent\n') for i, j in contribs.iteritems(): name = '-'.join(i.split('_')[1:]) value = str(j**2 / secm) anovaFile.writelines(name + ',' + str(j**2) + ',' + value + '\n') anovaFile.close() print 'ANOVA analysis written to', anovaFileName else: secm = self.HDMR_ROM.moment(self.hdmr_level, r=2, verbose=False) outFile = file(case + '.out', 'a') outFile.writelines('\nRuns,SC Level,Mean\n') outFile.writelines(str(self.HDMR_ROM.numRunsToCreate()) + ',') outFile.writelines(self.input_file('Sampler/SC/expOrd', '') + ',') outFile.writelines('%1.15e,%1.15e \n' % (mean, secm - mean * mean)) outFile.close()
import from myhdl * import ROM import concat_signed import numpy as np def coeff_gen(syncIn, Out,clkIn,CONTENT,FFTSize,FFTStage): w1 = Signal(intbv(0)[FFTStage:]) re = Signal(intbv(0, min=-2**(DATA_WIDTH - 1), max=(2**(DATA_WIDTH-1 )))) im = Signal(intbv(0, min=-2**(DATA_WIDTH - 1), max=(2**(DATA_WIDTH-1 )))) count_out = Signal(intbv()[FFTSize:]) updown2 = 1 counter_1 = counter_d.counter_d(count_out, clkIn, 1, syncIn, updown2,1, 0, 2**FFTStage) slice_1 = slice.slice(dout = w1, clk = clkIn, slice_mode = 1, SLICE_WIDTH = FFTStage, din = count_out) real_coeffs = np.round(CONTENT.real*2**(DATA_WIDTH-2)).astype('int') imag_coeffs = np.round(CONTENT.imag*2**(DATA_WIDTH-2)).astype('int') realContent = tuple(real_coeffs) imagContent = tuple(imag_coeffs ) ROM_1 = ROM.ROM(dout = re, addr = w1, CONTENT = realContent) ROM_2 = ROM.ROM(dout = im, addr = w1, CONTENT = imagContent) concat_2 = concat_signed.concat_signed(dout = Out, lsb= re, msb = im, clk = clkIn) return ROM_1, ROM_2,concat_2 ,counter_1, slice_1 #, coeff
return q #Specify right hand side and stiffness matrix rhs = PoissonFEM.RightHandSide(mesh) rhs.set_natural_rhs(mesh, flux) K = PoissonFEM.StiffnessMatrix(mesh) rhs.set_rhs_stencil(mesh, K) trainingData = dta.StokesData(supervised_samples, unsupervised_samples) trainingData.read_data() # trainingData.plotMicrostruct(1) trainingData.reshape_microstructure_image() # define rom rom = ROM.ROM(mesh, K, rhs, trainingData.output_resolution**2) model = gs.GenerativeSurrogate(rom, trainingData, dim_z=parsed_args.dim_z) # optimize lambdac first for n in range(model.data.n_supervised_samples): print('sample == ', n) model.log_lambdac_mean[n].max_iter = 3e4 model.log_lambdac_mean[n].converge(model, model.data.n_supervised_samples, mode=n) model.fit(n_steps=50, with_precisions=False, z_iterations=200, thetac_iterations=10000, lambdac_iterations=500)