[Avida-SVN] r1108 - in development/source: cpu main

baerb at myxo.css.msu.edu baerb at myxo.css.msu.edu
Thu Nov 30 13:05:40 PST 2006


Author: baerb
Date: 2006-11-30 16:05:30 -0500 (Thu, 30 Nov 2006)
New Revision: 1108

Modified:
   development/source/cpu/cHardwareCPU.cc
   development/source/cpu/cTestCPU.cc
   development/source/main/cInstSet.h
   development/source/main/cInstruction.h
   development/source/main/cPopulation.cc
   development/source/main/cResource.h
   development/source/main/cResourceCount.cc
   development/source/main/cResourceCount.h
   development/source/main/cSpatialResCount.cc
   development/source/main/cSpatialResCount.h
Log:

Added a bunch of error checking code to arrays in spatial resources to keep 
program from trying to write beyond the end of those arrays.  This is 
expecially important when the TestCPU tries to initialize the spatial 
resource before world dimesions are known.

There is also some debug write commands that will be removed when I figure out
why hardwareCPU code wants to work with instructions with OP's greater than
the greater instruction.



Modified: development/source/cpu/cHardwareCPU.cc
===================================================================
--- development/source/cpu/cHardwareCPU.cc	2006-11-30 06:38:58 UTC (rev 1107)
+++ development/source/cpu/cHardwareCPU.cc	2006-11-30 21:05:30 UTC (rev 1108)
@@ -498,19 +498,32 @@
     
     // Find the instruction to be executed
     const cInstruction& cur_inst = IP().GetInst();
+    // cout << "cHardwareCPU: " <<  cur_inst.GetOp() << " " << cur_inst.GetSymbol() << endl;
+    if (cur_inst.GetOp() > 26) {
+      cout << "cHardwareCPU1: " << cur_inst.GetOp() << " " << cur_inst.GetSymbol() << endl;
+    }
     
     // Test if costs have been paid and it is okay to execute this now...
     const bool exec = SingleProcess_PayCosts(ctx, cur_inst);
-    
+    if (cur_inst.GetOp() > 26) {
+      cout << "cHardwareCPU2: " << cur_inst.GetOp() << " " << cur_inst.GetSymbol() << endl;
+    }
+
     // Now execute the instruction...
     if (exec == true) {
       SingleProcess_ExecuteInst(ctx, cur_inst);
+      if (cur_inst.GetOp() > 26) {
+        cout << "cHardwareCPU3: " << cur_inst.GetOp() << " " << cur_inst.GetSymbol() << endl;
+      }
       
       // Some instruction (such as jump) may turn m_advance_ip off.  Usually
       // we now want to move to the next instruction in the memory.
       if (m_advance_ip == true) IP().Advance();
       
       // Pay the additional death_cost of the instruction now
+      if (cur_inst.GetOp() > 26) {
+        cout << "cHardwareCPU4: " << cur_inst.GetOp() << " " << cur_inst.GetSymbol() << endl;
+      }
       phenotype.IncTimeUsed(m_inst_set->GetAddlTimeCost(cur_inst));
       
     } // if exec

Modified: development/source/cpu/cTestCPU.cc
===================================================================
--- development/source/cpu/cTestCPU.cc	2006-11-30 06:38:58 UTC (rev 1107)
+++ development/source/cpu/cTestCPU.cc	2006-11-30 21:05:30 UTC (rev 1108)
@@ -61,7 +61,7 @@
                            res->GetInflowX2(), res->GetInflowY1(), 
                            res->GetInflowY2(), res->GetOutflowX1(), 
                            res->GetOutflowX2(), res->GetOutflowY1(), 
-                           res->GetOutflowY2(), res->GetCellList(),
+                           res->GetOutflowY2(), res->GetCellListPtr(),
                            world->GetVerbosity() );
   }
 }

Modified: development/source/main/cInstSet.h
===================================================================
--- development/source/main/cInstSet.h	2006-11-30 06:38:58 UTC (rev 1107)
+++ development/source/main/cInstSet.h	2006-11-30 21:05:30 UTC (rev 1108)
@@ -79,7 +79,13 @@
   const cString& GetName(const cInstruction& inst) const { return GetName(inst.GetOp()); }
   int GetCost(const cInstruction& inst) const { return m_lib_name_map[inst.GetOp()].cost; }
   int GetFTCost(const cInstruction& inst) const { return m_lib_name_map[inst.GetOp()].ft_cost; }
-  int GetAddlTimeCost(const cInstruction& inst) const { return m_lib_name_map[inst.GetOp()].addl_time_cost; }
+  // int GetAddlTimeCost(const cInstruction& inst) const { return m_lib_name_map[inst.GetOp()].addl_time_cost; }
+  int GetAddlTimeCost(const cInstruction& inst) const { if (inst.GetOp() > 30) {
+                                                          cout << "cInstSet.h " << inst.GetOp() << " " << inst.GetSymbol() << endl;
+                                                         } else {
+                                                           return m_lib_name_map[inst.GetOp()].addl_time_cost;
+                                                         }
+                                                       } 
   double GetProbFail(const cInstruction& inst) const { return m_lib_name_map[inst.GetOp()].prob_fail; }
   int GetRedundancy(const cInstruction& inst) const { return m_lib_name_map[inst.GetOp()].redundancy; }
   int GetLibFunctionIndex(const cInstruction& inst) const { return m_lib_name_map[inst.GetOp()].lib_fun_id; }

Modified: development/source/main/cInstruction.h
===================================================================
--- development/source/main/cInstruction.h	2006-11-30 06:38:58 UTC (rev 1107)
+++ development/source/main/cInstruction.h	2006-11-30 21:05:30 UTC (rev 1108)
@@ -31,6 +31,11 @@
   
   // Accessors...
   int GetOp() const { return static_cast<int>(m_operand); }
+  // int GetOp() const { return static_cast<int>(m_operand); 
+  //                     if (static_cast<int>(m_operand) > 30) { 
+  //                       cout << m_operand << "  " << static_cast<int>(m_operand) << endl; 
+  //                     }
+  //                   }
   void SetOp(int in_op) { assert(in_op < 256); m_operand = in_op; }
 
   // Operators...

Modified: development/source/main/cPopulation.cc
===================================================================
--- development/source/main/cPopulation.cc	2006-11-30 06:38:58 UTC (rev 1107)
+++ development/source/main/cPopulation.cc	2006-11-30 21:05:30 UTC (rev 1108)
@@ -153,7 +153,7 @@
                          res->GetInflowX2(), res->GetInflowY1(), 
                          res->GetInflowY2(), res->GetOutflowX1(), 
                          res->GetOutflowX2(), res->GetOutflowY1(), 
-                         res->GetOutflowY2(), res->GetCellList(),
+                         res->GetOutflowY2(), res->GetCellListPtr(),
                          world->GetVerbosity() );
     m_world->GetStats().SetResourceName(i, res->GetName());
   }

Modified: development/source/main/cResource.h
===================================================================
--- development/source/main/cResource.h	2006-11-30 06:38:58 UTC (rev 1107)
+++ development/source/main/cResource.h	2006-11-30 21:05:30 UTC (rev 1108)
@@ -88,7 +88,7 @@
   double GetXGravity() const { return xgravity; }
   double GetYDiffuse() const { return ydiffuse; }
   double GetYGravity() const { return ygravity; }
-  tArray<cCellResource> GetCellList() const { return cell_list; }
+  tArray<cCellResource> *GetCellListPtr() { return &cell_list; }
 
 
   void SetInitial(double _initial) { initial = _initial; }

Modified: development/source/main/cResourceCount.cc
===================================================================
--- development/source/main/cResourceCount.cc	2006-11-30 06:38:58 UTC (rev 1107)
+++ development/source/main/cResourceCount.cc	2006-11-30 21:05:30 UTC (rev 1108)
@@ -150,7 +150,7 @@
                            int in_inflowX2, int in_inflowY1, 
                            int in_inflowY2, int in_outflowX1, 
                            int in_outflowX2, int in_outflowY1, 
-                           int in_outflowY2, tArray<cCellResource> in_cell_list,
+                           int in_outflowY2, tArray<cCellResource> *in_cell_list_ptr,
                            int verbosity_level)
 {
   assert(id >= 0 && id < resource_count.GetSize());
@@ -205,11 +205,11 @@
   geometry[id] = in_geometry;
   spatial_resource_count[id].SetGeometry(in_geometry);
   spatial_resource_count[id].SetPointers();
-  spatial_resource_count[id].SetCellList(in_cell_list);
+  spatial_resource_count[id].SetCellList(in_cell_list_ptr);
 
   double step_decay = pow(decay, UPDATE_STEP);
   double step_inflow = inflow * UPDATE_STEP;
-
+  
   decay_precalc(id, 0) = 1.0;
   inflow_precalc(id, 0) = 0.0;
   for (int i = 1; i <= PRECALC_DISTANCE; i++) {

Modified: development/source/main/cResourceCount.h
===================================================================
--- development/source/main/cResourceCount.h	2006-11-30 06:38:58 UTC (rev 1107)
+++ development/source/main/cResourceCount.h	2006-11-30 21:05:30 UTC (rev 1108)
@@ -64,7 +64,7 @@
              double in_ydiffuse, double in_ygravity,
              int in_inflowX1, int in_inflowX2, int in_inflowY1, int in_inflowY2,
              int in_outflowX1, int in_outflowX2, int in_outflowY1, 
-             int in_outflowY, tArray<cCellResource> in_cell_list,
+             int in_outflowY, tArray<cCellResource> *in_cell_list_ptr,
              int verbosity_level);
   void Update(double in_time);
 

Modified: development/source/main/cSpatialResCount.cc
===================================================================
--- development/source/main/cSpatialResCount.cc	2006-11-30 06:38:58 UTC (rev 1107)
+++ development/source/main/cSpatialResCount.cc	2006-11-30 21:05:30 UTC (rev 1108)
@@ -197,14 +197,77 @@
 
 }
 
-void cSpatialResCount::SetCellList(tArray<cCellResource> in_cell_list) {
-  cell_list = in_cell_list;
-  for (int i = 0; i < cell_list.GetSize(); i++) {
-    Rate(cell_list[i].GetId(), cell_list[i].GetInitial());
-    State(cell_list[i].GetId());
+/* Set all the individual cells to their initial values */
+
+void cSpatialResCount::SetCellList(tArray<cCellResource> *in_cell_list_ptr) {
+  cell_list_ptr = in_cell_list_ptr;
+  for (int i = 0; i < cell_list_ptr->GetSize(); i++) {
+    int cell_id = (*cell_list_ptr)[i].GetId();
+    
+    /* Be sure the user entered a valid cell id or if the the program is loading
+       the resource for the testCPU that does not have a grid set up */
+       
+    if (cell_id >= 0 && cell_id <= grid.GetSize()) {
+      Rate((*cell_list_ptr)[i].GetId(), (*cell_list_ptr)[i].GetInitial());
+      State((*cell_list_ptr)[i].GetId());
+    }
   }
 }
 
+/* Set the rate variable for one element using the array index */
+
+void cSpatialResCount::Rate(int x, double ratein) const { 
+  if (x >= 0 && x < grid.GetSize()) {
+    grid[x].Rate(ratein);
+  }
+}
+
+/* Set the rate variable for one element using the x,y coordinate */
+
+void cSpatialResCount::Rate(int x, int y, double ratein) const { 
+  if (x >= 0 && x < world_x && y>= 0 && y < world_y) {
+    grid[y * world_x + x].Rate(ratein);
+  }
+}
+
+/* Fold the rate variable into the resource state for one element using 
+   the array index */
+   
+void cSpatialResCount::State(int x) { 
+  if (x >= 0 && x < grid.GetSize()) {
+    grid[x].State();
+  }
+}
+
+/* Fold the rate variable into the resource state for one element using 
+   the x,y coordinate */
+   
+void cSpatialResCount::State(int x, int y) { 
+  if (x >= 0 && x < world_x && y >= 0 && y < world_y) {
+    grid[y*world_x + x].State();
+  }
+}
+
+/* Get the state of one element using the array index */
+
+const double cSpatialResCount::GetAmount(int x) const { 
+  if (x >= 0 && x < grid.GetSize()) {
+    return grid[x].GetAmount(); 
+  } else {
+    return -99.9;
+  }
+}
+
+/* Get the state of one element using the the x,y coordinate */
+
+const double cSpatialResCount::GetAmount(int x, int y) const { 
+  if (x >= 0 && x < world_x && y >= 0 && y < world_y) {
+    return grid[y*world_x + x].GetAmount(); 
+  } else {
+    return -99.9;
+  }
+}
+
 void cSpatialResCount::RateAll(double ratein) {
 
   int i;
@@ -283,8 +346,15 @@
 /* Handle the inflow for a list of individual cells */
 
 void cSpatialResCount::CellInflow() const {
-  for (int i=0; i < cell_list.GetSize(); i++) {
-    Rate(cell_list[i].GetId(), cell_list[i].GetInflow());
+  for (int i=0; i < cell_list_ptr->GetSize(); i++) {
+    const int cell_id = (*cell_list_ptr)[i].GetId();
+    
+    /* Be sure the user entered a valid cell id or if the the program is loading
+       the resource for the testCPU that does not have a grid set up */
+       
+    if (cell_id >= 0 && cell_id < grid.GetSize()) {
+      Rate(cell_id, (*cell_list_ptr)[i].GetInflow());
+    }
   }
 }
 
@@ -311,9 +381,16 @@
 
   double  deltaamount;
 
-  for (int i=0; i < cell_list.GetSize(); i++) {
-    deltaamount = Max((GetAmount(cell_list[i].GetId()) *
-                       cell_list[i].GetOutflow()), 0.0);
-    Rate(cell_list[i].GetId(), -deltaamount); 
+  for (int i=0; i < cell_list_ptr->GetSize(); i++) {
+    const int cell_id = (*cell_list_ptr)[i].GetId();
+    
+    /* Be sure the user entered a valid cell id or if the the program is loading
+       the resource for the testCPU that does not have a grid set up */
+       
+    if (cell_id >= 0 && cell_id < grid.GetSize()) {
+      deltaamount = Max((GetAmount(cell_id) *
+                         (*cell_list_ptr)[i].GetOutflow()), 0.0);
+    }                     
+    Rate((*cell_list_ptr)[i].GetId(), -deltaamount); 
   }
 }

Modified: development/source/main/cSpatialResCount.h
===================================================================
--- development/source/main/cSpatialResCount.h	2006-11-30 06:38:58 UTC (rev 1107)
+++ development/source/main/cSpatialResCount.h	2006-11-30 21:05:30 UTC (rev 1108)
@@ -31,7 +31,8 @@
   int    outflowX1, outflowX2, outflowY1, outflowY2;
   int    geometry;
   int    world_x, world_y, num_cells;
-  tArray<cCellResource> cell_list;
+  /* instead of creating a new array use the existing one from cResource */
+  tArray<cCellResource> *cell_list_ptr;
   
 public:
   cSpatialResCount();
@@ -43,29 +44,25 @@
   void ResizeClear(int inworld_x, int inworld_y, int ingeometry);
   void SetPointers();
   void CheckRanges();
-  void SetCellList(tArray<cCellResource> in_cell_list);
+  void SetCellList(tArray<cCellResource> *in_cell_list_ptr);
   int GetSize() { return grid.GetSize(); }
   int GetX() { return world_x; }
   int GetY() { return world_y; }
-  int GetCellListSize() { return cell_list.GetSize(); }
+  int GetCellListSize() { return cell_list_ptr->GetSize(); }
   cSpatialCountElem Element(int x) { return grid[x]; }
-  void Rate(int x, double ratein) const { grid[x].Rate(ratein); }
-  void Rate(int x, int y, double ratein) const 
-     { grid[y * world_x + x].Rate(ratein); }
-  void State(int x) { grid[x].State(); }
-  void State(int x, int y) { grid[y*world_x + x].State(); }
-  const double GetAmount(int x) const { return grid[x].GetAmount(); }
-  const double GetAmount(int x, int y) const 
-     { return grid[y*world_x + x].GetAmount(); }
+  void Rate(int x, double ratein) const;
+  void Rate(int x, int y, double ratein) const;
+  void State(int x);
+  void State(int x, int y);
+  const double GetAmount(int x) const;
+  const double GetAmount(int x, int y) const;
   void RateAll(double ratein);
   void StateAll();
   void FlowAll();
   const double SumAll() const;
   void Source(double amount) const;
-  // void CellInflow(tArray<cCellResource> cell_list) const;
   void CellInflow() const;
   void Sink(double percent) const;
-  // void CellOutflow(tArray<cCellResource> cell_list) const;
   void CellOutflow() const;
   void SetGeometry(int in_geometry) { geometry = in_geometry; }
   void SetXdiffuse(double in_xdiffuse) { xdiffuse = in_xdiffuse; }




More information about the Avida-cvs mailing list