def test_node_memory_relation(self): """ Unit test that verifies that the correct relation between the Memory class and the node class is built """ # We create a node node = Node() node.name = "node1" node.information_retrieved = True # We add several CPUs to it node.memories = [ Memory(11111, "bytes"), Memory(11211, "bytes") ] # We save everything to the db db.session.add(node) db.session.commit() # We retrived and verify that the gpus are there node = db.session.query(Node).filter_by(name='node1').first() self.assertEquals(2, len(node.memories)) self.assertEquals(11111, node.memories[0].size) self.assertEquals(11211, node.memories[1].size) # Lets delete a gpu directly from the session db.session.delete(node.memories[1]) db.session.commit() node = db.session.query(Node).filter_by(name='node1').first() self.assertEquals(1, len(node.memories)) self.assertEquals(11111, node.memories[0].size)
def test_crud_memory(self): """ It tests the basic CRUD operations of a Memory class """ # We verify the object is not in the db after creating it memory = Memory(11111, "bytes", "0x111", "writable") self.assertIsNone(memory.id) # We store the object in the db db.session.add(memory) # We recover the Memory from the db memory = db.session.query(Memory).filter_by(units='bytes').first() self.assertIsNotNone(memory.id) self.assertEquals(11111, memory.size) self.assertEquals("bytes", memory.units) self.assertEquals("0x111", memory.address) self.assertEquals("writable", memory.memory_type) # We update the memory memory.size = 0 db.session.commit() memory = db.session.query(Memory).filter_by(units='bytes').first() self.assertEquals(0, memory.size) # We check the deletion db.session.delete(memory) count = db.session.query(Memory).filter_by(units='bytes').count() self.assertEquals(0, count)
def test_parse_memory(self): m = torque.parse_memory("131583196kb") self.assertEqual(str(Memory(131583196, Memory.KILOBYTE)), str(m)) m = torque.parse_memory("131583mb") self.assertEqual(str(Memory(131583, Memory.MEGABYTE)), str(m)) m = torque.parse_memory("131gb") self.assertEqual(str(Memory(131, Memory.GIGABYTE)), str(m)) m = torque.parse_memory("11err") self.assertEqual(str(Memory(0, Memory.MEGABYTE)), str(m)) m = torque.parse_memory("1s1gb") self.assertEqual(str(Memory(0, Memory.MEGABYTE)), str(m))
def add_memory(self): """ Just adds a memory object for add memory and remove memory tests""" # We add one element to the node memory = Memory(12121, "kilobytes") self.node.add_memory(memory) return memory
def test_initialization(self): """Verifies that the initialization of the values of a Memory object""" # Adding default values memory_1 = Memory(111, "kilobytes", "0x111", "writetable") self.assertEquals(111, memory_1.size) self.assertEquals("kilobytes", memory_1.units) self.assertEquals("0x111", memory_1.address) self.assertEquals("writetable", memory_1.memory_type) ## Without default values memory_2 = Memory(2, "megabytes") self.assertEquals(2, memory_2.size) self.assertEquals("megabytes", memory_2.units) self.assertEquals("", memory_2.address) self.assertEquals("", memory_2.memory_type)
def __setitem__(self, key, val): try: memory = self.__get_memory__(key) memory.value = val memory.save() db_session.session.commit() except KeyError: memory = Memory(sender=self.sender_id, key=key.lower(), value=val) db_session.session.add(memory) db_session.session.commit()
def parse_memory(memory): """Parses free text from pbsnodes.status output, returning [ size, unit ]. On error, returns [0, Memory.MEGABYTE] """ sizes = { "kb": Memory.KILOBYTE, "mb": Memory.MEGABYTE, "gb": Memory.GIGABYTE } try: for key in sizes.keys(): if memory.endswith(key): size = int(memory[0:len(memory) - len(key)]) unit = sizes[key] return Memory(size=size, units=unit) except ValueError: pass # follows below return Memory(size=0, units=Memory.MEGABYTE)
def test_add_status(self): """It verifies the correct work that add an element to the status""" # We add a slurm dictorionary to status key = 'slurm' value = {'a1': 'aaa'} self.node.add_status(key, value) self.assertEquals(1, len(self.node.status.keys())) self.assertEquals(value, self.node.status[key]) # We verify that we can only use strings as keys key2 = Memory(12121, "kilobytes") self.node.add_status(key2, value) self.assertEquals(1, len(self.node.status.keys())) # We verify that we can only add dicts as values key3 = 'slurm2' value3 = Memory(12121, "kilobytes") self.node.add_status(key3, value3) self.assertEquals(1, len(self.node.status.keys())) self.assertEquals(value, self.node.status[key])
def test_add_memory_gpu(self): """Test that we can correctly add memory to a GPU moemory array""" # We create an initial GPU gpu = GPU("NVIDIA", "GF110GL") self.assertEquals(0, len(gpu.memory)) # We add a Memory element memory = Memory(1111, "kilobytes") gpu.add_memory(memory) self.assertEquals(1, len(gpu.memory)) self.assertEquals(memory, gpu.memory[0]) # We verify that we can not add memory of the wrong type gpu.add_memory("xxx") self.assertEquals(1, len(gpu.memory)) self.assertEquals(memory, gpu.memory[0])
def test_remove_memory(self): """We verify it is possible to delete a memory element""" # We create an initial GPU gpu = GPU("NVIDIA", "GF110GL") print(len(gpu.memory)) self.assertEquals(0, len(gpu.memory)) # We add a Memory element memory = Memory(1111, "kilobytes") gpu.add_memory(memory) self.assertEquals(1, len(gpu.memory)) # We remove it gpu.remove_memory(memory) self.assertEquals(0, len(gpu.memory)) # We verify that removing an inexistent elements gives no error gpu.remove_memory("xxx") self.assertEquals(0, len(gpu.memory))
def update_node_information(): """ This function gets all the testbed that are SLURM and have been configured to retrieve all information automatically and update the node information if necessary """ testbeds = query.get_slurm_online_testbeds() for testbed in testbeds: nodes_info = get_node_information(testbed) for node_info in nodes_info: if 'NodeName' in node_info: node = db.session.query(Node).filter_by( testbed_id=testbed.id, name=node_info['NodeName']).first() if node and 'State' in node_info: logging.info("Updating information for node: " + node.name + " if necessary") node.state = node_info['State'] db.session.commit() if node and 'RealMemory' in node_info: logging.info("Updating memory information for node: " + node.name) db.session.query(Memory).filter_by(node_id=node.id).delete() memory = Memory(size=node_info['RealMemory'], units=Memory.MEGABYTE) node.memories = [ memory ] db.session.commit() if node and 'Gres' in node_info: resources = parse_gre_field_info(node_info['Gres']) if 'gpu' in resources: db.session.query(GPU).filter_by(node_id=node.id).delete() logging.info("Updating gpu information for node: " + node.name) node.gpus = resources['gpu'] db.session.commit()
def parse_memory(memory: str) -> Memory: return Memory(size=memory, units=Memory.MEGABYTE)
import gym import gym_draughts from models import Actor, delete_diagonals, board_to_onehot, output_to_move, Memory, move_to_onehot, Critic, AC import torch as T import numpy as np import random actor_layers = [10, 10] critic_layers = [10, 10, 10] memory = Memory(2000) ac = AC(18, actor_layers, critic_layers, 18, 4) gamma = 0.9 learning_rate = 3e-4 optimizer = T.optim.Adam(ac.parameters(), lr = learning_rate) env = gym.make('draughts-v0') def play_against_random(random_moves=False): maxmoves = 100 num_moves = 0 env.reset() playing = True firstmove = True while playing: currentmove = env.board.player if currentmove == -1: nowstate = env.get_state() if not firstmove: memory.addstate(oldstate, loc, move, (m,i), (nowstate, reward, done, illegal)) #print('adding to memory: ', illegal) else:
env = gym.make(game) win_streak = [] frame_shape = process_frame(env.reset()).shape state_shape = (frames_number, *frame_shape) if not torch.cuda.is_available(): print('cuda not available') device = torch.device('cuda') net = DQN(state_shape, env.action_space.n).to(device) target_net = copy(net).to(device) loss = torch.nn.MSELoss() optimizer = torch.optim.Adam(net.parameters(), lr=1e-4) training_queue = Queue() memory = Memory(int(1e+4), training_queue, device) ### UTILITY FUNCTIONS ### def step(action, reset=False): if reset: state = [process_frame(env.reset())] loops = frames_number - 1 else: state = [] loops = frames_number reward = 0. for k in range(loops): for j in range(frames_to_skip): frame, r, done, info = env.step(action)