[Avida-cvs] [avida-svn] r547 - in development/source: cpu main tools

brysonda@myxo.css.msu.edu brysonda at myxo.css.msu.edu
Thu Mar 30 19:42:41 PST 2006


Author: brysonda
Date: 2006-03-30 22:42:34 -0500 (Thu, 30 Mar 2006)
New Revision: 547

Modified:
   development/source/cpu/cHardwareSMT.cc
   development/source/cpu/cHardwareSMT.h
   development/source/cpu/cHardwareSMT_Thread.cc
   development/source/cpu/cHardwareSMT_Thread.h
   development/source/main/cOrganism.cc
   development/source/main/cOrganism.h
   development/source/main/cPopulationCell.cc
   development/source/tools/tManagedPointerArray.h
Log:
Add SMT Net instructions.  Rework SMT Thread creation and deletion.   Added smart remove method to tManagedPointerArray.

Modified: development/source/cpu/cHardwareSMT.cc
===================================================================
--- development/source/cpu/cHardwareSMT.cc	2006-03-30 19:10:05 UTC (rev 546)
+++ development/source/cpu/cHardwareSMT.cc	2006-03-31 03:42:34 UTC (rev 547)
@@ -87,11 +87,15 @@
     cInstEntry("Push-Comp", &cHardwareSMT::Inst_PushComplement), // 30
     cInstEntry("Val-Delete", &cHardwareSMT::Inst_ValDelete), // 31
     cInstEntry("Val-Copy", &cHardwareSMT::Inst_ValCopy), // 32
-    cInstEntry("ThreadFork", &cHardwareSMT::Inst_ForkThread), // 33
-    cInstEntry("ThreadKill", &cHardwareSMT::Inst_KillThread), // 34
+    cInstEntry("Thread-Create", &cHardwareSMT::Inst_ThreadCreate), // 33
+    cInstEntry("Thread-Exit", &cHardwareSMT::Inst_ThreadExit), // 34
     cInstEntry("IO", &cHardwareSMT::Inst_IO), // 35
     cInstEntry("Inject", &cHardwareSMT::Inst_Inject), // 36
-    cInstEntry("Apoptosis", &cHardwareSMT::Inst_Apoptosis)
+    cInstEntry("Apoptosis", &cHardwareSMT::Inst_Apoptosis), // 37
+    cInstEntry("Net-Get", &cHardwareSMT::Inst_NetGet), // 38
+    cInstEntry("Net-Send", &cHardwareSMT::Inst_NetSend), // 39
+    cInstEntry("Net-Receive", &cHardwareSMT::Inst_NetReceive), // 40
+    cInstEntry("Net-Last", &cHardwareSMT::Inst_NetLast) // 41
   };
 	
   const int n_size = sizeof(s_n_array)/sizeof(cNOPEntry);
@@ -162,6 +166,8 @@
     inst_ft_cost[i] = m_inst_set->GetFTCost(cInstruction(i));
   }
 #endif	
+  
+  organism->NetReset();
 }
 
 // This function processes the very next command in the genome, and is made
@@ -953,18 +959,14 @@
 	
   // Make room for the new thread.
   m_threads.Resize(num_threads + 1);
-	
-  //IP().Advance();
-	
-  // Initialize the new thread to the same values as the current one.
-  m_threads[num_threads] = m_threads[m_cur_thread]; 
-	
-  // Find the first free bit in thread_id_chart to determine the new
-  // thread id.
+  
+  // Find the first free bit in thread_id_chart to determine the new thread id.
   int new_id = 0;
   while ( (thread_id_chart >> new_id) & 1 == 1) new_id++;
-  m_threads[num_threads].SetID(new_id);
   thread_id_chart |= (1 << new_id);
+
+  // Setup this thread into the current selected memory space (Flow Head)
+  m_threads[num_threads].Reset(this, new_id, GetHead(nHardware::HEAD_FLOW).GetMemSpace());
 	
   return true;
 }
@@ -984,21 +986,14 @@
   // Note the current thread and set the current back one.
   const int kill_thread = m_cur_thread;
   PrevThread();
+  if (m_cur_thread > kill_thread) m_cur_thread--;
   
   // Turn off this bit in the thread_id_chart...
   thread_id_chart ^= 1 << m_threads[kill_thread].GetID();
 	
-  // Copy the last thread into the kill position
-  const int last_thread = GetNumThreads() - 1;
-  if (last_thread != kill_thread) {
-    m_threads[kill_thread] = m_threads[last_thread];
-  }
-	
   // Kill the thread!
-  m_threads.Resize(GetNumThreads() - 1);
+  m_threads.Remove(kill_thread);	
 	
-  if (m_cur_thread > kill_thread) m_cur_thread--;
-	
   return true;
 }
 
@@ -1955,7 +1950,7 @@
 }
 
 //30
-bool cHardwareSMT::Inst_ForkThread(cAvidaContext& ctx)
+bool cHardwareSMT::Inst_ThreadCreate(cAvidaContext& ctx)
 {
   if (!ForkThread()) 
     Fault(FAULT_LOC_THREAD_FORK, FAULT_TYPE_FORK_TH);
@@ -1974,7 +1969,7 @@
 }
 
 //35
-bool cHardwareSMT::Inst_KillThread(cAvidaContext& ctx)
+bool cHardwareSMT::Inst_ThreadExit(cAvidaContext& ctx)
 {
   if (!KillThread()) Fault(FAULT_LOC_THREAD_KILL, FAULT_TYPE_KILL_TH);
   else AdvanceIP() = false;
@@ -2024,3 +2019,42 @@
   
   return true;
 }
+
+bool cHardwareSMT::Inst_NetGet(cAvidaContext& ctx)
+{
+  const int dst = FindModifiedStack(nHardwareSMT::STACK_BX);
+#ifdef SMT_FULLY_ASSOCIATIVE
+  const int seq_dst = FindModifiedNextStack(dst);
+#else
+  const int seq_dst = FindNextStack(dst);
+#endif
+  int val, seq;
+  organism->NetGet(ctx, val, seq);
+  Stack(dst).Push(val);
+  Stack(seq_dst).Push(seq);
+  
+  return true;
+}
+
+bool cHardwareSMT::Inst_NetSend(cAvidaContext& ctx)
+{
+  const int src = FindModifiedStack(nHardwareSMT::STACK_BX);
+  organism->NetSend(ctx, Stack(src).Pop());
+  return true;
+}
+
+bool cHardwareSMT::Inst_NetReceive(cAvidaContext& ctx)
+{
+  const int dst = FindModifiedStack(nHardwareSMT::STACK_BX);
+  int val;
+  bool success = organism->NetReceive(val);
+  Stack(dst).Push(val);
+  return success;
+}
+
+bool cHardwareSMT::Inst_NetLast(cAvidaContext& ctx)
+{
+  const int dst = FindModifiedStack(nHardwareSMT::STACK_CX);
+  Stack(dst).Push(organism->NetLast());
+  return true;
+}

Modified: development/source/cpu/cHardwareSMT.h
===================================================================
--- development/source/cpu/cHardwareSMT.h	2006-03-30 19:10:05 UTC (rev 546)
+++ development/source/cpu/cHardwareSMT.h	2006-03-31 03:42:34 UTC (rev 547)
@@ -80,7 +80,7 @@
   tHashTable<int, int> m_mem_lbls;
 
   // Threads
-  tArray<cHardwareSMT_Thread> m_threads;
+  tManagedPointerArray<cHardwareSMT_Thread> m_threads;
   int thread_id_chart;
   int m_cur_thread;
 	
@@ -274,12 +274,16 @@
   bool Inst_PushComplement(cAvidaContext& ctx);
   bool Inst_ValDelete(cAvidaContext& ctx);
   bool Inst_ValCopy(cAvidaContext& ctx);
-  bool Inst_ForkThread(cAvidaContext& ctx);
+  bool Inst_ThreadCreate(cAvidaContext& ctx);
   bool Inst_IfLabel(cAvidaContext& ctx);
-  bool Inst_KillThread(cAvidaContext& ctx);
+  bool Inst_ThreadExit(cAvidaContext& ctx);
   bool Inst_IO(cAvidaContext& ctx);
   bool Inst_Inject(cAvidaContext& ctx);
   bool Inst_Apoptosis(cAvidaContext& ctx);
+  bool Inst_NetGet(cAvidaContext& ctx);
+  bool Inst_NetSend(cAvidaContext& ctx);
+  bool Inst_NetReceive(cAvidaContext& ctx);
+  bool Inst_NetLast(cAvidaContext& ctx);
 };
 
 

Modified: development/source/cpu/cHardwareSMT_Thread.cc
===================================================================
--- development/source/cpu/cHardwareSMT_Thread.cc	2006-03-30 19:10:05 UTC (rev 546)
+++ development/source/cpu/cHardwareSMT_Thread.cc	2006-03-31 03:42:34 UTC (rev 547)
@@ -11,12 +11,12 @@
 
 using namespace std;
 
-cHardwareSMT_Thread::cHardwareSMT_Thread(cHardwareBase * in_hardware, int _id)
+cHardwareSMT_Thread::cHardwareSMT_Thread(cHardwareBase* in_hardware, int _id)
 {
   Reset(in_hardware, _id);
 }
 
-cHardwareSMT_Thread::cHardwareSMT_Thread(const cHardwareSMT_Thread & in_thread, int _id)
+cHardwareSMT_Thread::cHardwareSMT_Thread(const cHardwareSMT_Thread& in_thread, int _id)
 {
 	id = _id;
 	if (id == -1) id = in_thread.id;
@@ -29,9 +29,7 @@
 	owner = in_thread.owner;
 }
 
-cHardwareSMT_Thread::~cHardwareSMT_Thread() {}
-
-void cHardwareSMT_Thread::operator=(const cHardwareSMT_Thread & in_thread)
+void cHardwareSMT_Thread::operator=(const cHardwareSMT_Thread& in_thread)
 {
   id = in_thread.id;
   for (int i = 0; i < nHardwareSMT::NUM_LOCAL_STACKS; i++) {
@@ -43,12 +41,12 @@
   owner = in_thread.owner;
 }
 
-void cHardwareSMT_Thread::Reset(cHardwareBase * in_hardware, int _id)
+void cHardwareSMT_Thread::Reset(cHardwareBase* in_hardware, int _id, int mem_space)
 {
   id = _id;
 	
   for (int i = 0; i < nHardwareSMT::NUM_LOCAL_STACKS; i++) local_stacks[i].Clear();
-  for (int i = 0; i < nHardware::NUM_HEADS; i++) heads[i].Reset(0, in_hardware);
+  for (int i = 0; i < nHardware::NUM_HEADS; i++) heads[i].Reset(mem_space, in_hardware);
 	
   cur_head = nHardware::HEAD_IP;
   read_label.Clear();

Modified: development/source/cpu/cHardwareSMT_Thread.h
===================================================================
--- development/source/cpu/cHardwareSMT_Thread.h	2006-03-30 19:10:05 UTC (rev 546)
+++ development/source/cpu/cHardwareSMT_Thread.h	2006-03-31 03:42:34 UTC (rev 547)
@@ -25,19 +25,14 @@
 #include "nHardwareSMT.h"
 #endif
 
-/**
-* This class is needed to run several threads on a single genome.
- *
- * @see cCPUStack, cHeadMultiMem, cHardwareSMT
- **/
-
 class cHardwareBase;
 class cInjectGenotype;
 
-struct cHardwareSMT_Thread
+class cHardwareSMT_Thread
 {
 private:
   int id;
+
 public:
   cHeadMultiMem heads[nHardware::NUM_HEADS];
   unsigned char cur_head;
@@ -53,11 +48,11 @@
   
   cHardwareSMT_Thread(cHardwareBase* in_hardware = NULL, int _id = -1);
   cHardwareSMT_Thread(const cHardwareSMT_Thread& in_thread, int _id = -1);
-  ~cHardwareSMT_Thread();
-	
+  ~cHardwareSMT_Thread() { ; }
+
   void operator=(const cHardwareSMT_Thread& in_thread);
 	
-  void Reset(cHardwareBase* in_hardware, int _id);
+  void Reset(cHardwareBase* in_hardware, int _id, int mem_space = 0);
   int GetID() const { return id; }
   void SetID(int _id) { id = _id; }
 };

Modified: development/source/main/cOrganism.cc
===================================================================
--- development/source/main/cOrganism.cc	2006-03-30 19:10:05 UTC (rev 546)
+++ development/source/main/cOrganism.cc	2006-03-31 03:42:34 UTC (rev 547)
@@ -339,7 +339,18 @@
   return true;
 }
 
+void cOrganism::NetReset()
+{
+  if (m_net) {
+    while (m_net->pending.GetSize()) delete m_net->pending.Pop();
+    for (int i = 0; i < m_net->received.GetSize(); i++) delete m_net->received[i];
+    m_net->received.Resize(0);
+    m_net->sent.Resize(0);
+    m_net->seq.Resize(0);
+  }
+}
 
+
 bool cOrganism::InjectParasite(const cGenome & injected_code)
 {
   assert(m_interface);

Modified: development/source/main/cOrganism.h
===================================================================
--- development/source/main/cOrganism.h	2006-03-30 19:10:05 UTC (rev 546)
+++ development/source/main/cOrganism.h	2006-03-31 03:42:34 UTC (rev 547)
@@ -166,6 +166,7 @@
   bool NetValidate(cAvidaContext& ctx, int value);
   bool NetRemoteValidate(cAvidaContext& ctx, int value);
   int NetLast() { return m_net->last_seq; }
+  void NetReset();
 
   bool InjectParasite(const cGenome & genome);
   bool InjectHost(const cCodeLabel & in_label, const cGenome & genome);

Modified: development/source/main/cPopulationCell.cc
===================================================================
--- development/source/main/cPopulationCell.cc	2006-03-30 19:10:05 UTC (rev 546)
+++ development/source/main/cPopulationCell.cc	2006-03-31 03:42:34 UTC (rev 547)
@@ -20,6 +20,7 @@
 cPopulationCell::cPopulationCell()
   : m_world(NULL)
   , organism(NULL)
+  , mutation_rates(NULL)
   , cur_input(0)
   , organism_count(0)
 {

Modified: development/source/tools/tManagedPointerArray.h
===================================================================
--- development/source/tools/tManagedPointerArray.h	2006-03-30 19:10:05 UTC (rev 546)
+++ development/source/tools/tManagedPointerArray.h	2006-03-31 03:42:34 UTC (rev 547)
@@ -109,6 +109,39 @@
     for (int i = old_size; i < new_size; i++) *m_data[i] = empty_value;
   }
   
+  void Remove(const int index)
+  {
+    assert(m_size != 0 || index < m_size || index >= 0);
+
+    int new_size = m_size - 1;
+    
+    // If new size is 0, clean up and go!
+    if (new_size == 0) {
+      for (int i = 0; i < m_size; i++) delete m_data[i];
+      delete [] m_data;
+      m_data = NULL;
+      m_size = 0;
+      return;
+    }
+    
+    T** new_data = new T*[new_size];
+    assert(new_data != NULL); // Memory Allocation Error: Out of Memory?
+    
+    delete m_data[index];
+    
+    // Copy over old data...
+    for (int i = 0; i < index; i++) {
+      new_data[i] = m_data[i];
+    }
+    for (int i = index + 1; i < m_size; i++) {
+      new_data[i - 1] = m_data[i];
+    }
+    delete [] m_data;
+    m_data = new_data;
+
+    m_size = new_size;
+  }
+  
   T& operator[](const int index)
   {
     assert(index >= 0);    // Lower Bounds Error




More information about the Avida-cvs mailing list