[Avida-SVN] r2283 - development/source/cpu
brysonda at myxo.css.msu.edu
brysonda at myxo.css.msu.edu
Sun Jan 27 17:51:17 PST 2008
Author: brysonda
Date: 2008-01-27 20:51:17 -0500 (Sun, 27 Jan 2008)
New Revision: 2283
Modified:
development/source/cpu/cHardwareBase.cc
development/source/cpu/cHardwareBase.h
development/source/cpu/cHardwareCPU.cc
development/source/cpu/cHardwareExperimental.cc
development/source/cpu/cHardwareGX.cc
development/source/cpu/cHardwareSMT.cc
development/source/cpu/cHardwareTransSMT.cc
Log:
Redesign per-execution instruction cost handling in cHardwareBase::SingleProcess_PayCosts so that it is significantly faster in terms of execution speed. Also reduces memory usage slightly.
Modified: development/source/cpu/cHardwareBase.cc
===================================================================
--- development/source/cpu/cHardwareBase.cc 2008-01-27 20:41:10 UTC (rev 2282)
+++ development/source/cpu/cHardwareBase.cc 2008-01-28 01:51:17 UTC (rev 2283)
@@ -838,7 +838,6 @@
bool cHardwareBase::SingleProcess_PayCosts(cAvidaContext& ctx, const cInstruction& cur_inst)
{
#if INSTRUCTION_COSTS
- assert(cur_inst.GetOp() < inst_cost.GetSize());
if(m_world->GetConfig().ENERGY_ENABLED.Get() > 0) {
// TODO: Get rid of magic number. check avaliable energy first
@@ -876,18 +875,49 @@
}
// Next, look at the per use cost
- if (m_has_costs && m_inst_set->GetCost(cur_inst) > 0) {
- if (inst_cost[cur_inst.GetOp()] > 1) { // if isn't paid off (>1)
- inst_cost[cur_inst.GetOp()]--; // dec cost
- return false;
- } else { // else, reset cost array
- inst_cost[cur_inst.GetOp()] = m_inst_set->GetCost(cur_inst);
- }
+
+ if (m_inst_cost > 1) { // Current cost being paid, decrement and return false
+ m_inst_cost--;
+ return false;
}
+ if (!m_inst_cost && m_has_costs && m_inst_set->GetCost(cur_inst) > 1) {
+ // no current cost, but there are costs active, and this instruction has a cost, setup the counter and return false
+ m_inst_cost = m_inst_set->GetCost(cur_inst) - 1;
+ return false;
+ }
+
+ // If we fall to here, reset the current cost count to zero
+ m_inst_cost = 0;
+
if (m_world->GetConfig().ENERGY_ENABLED.Get() > 0) {
inst_energy_cost[cur_inst.GetOp()] = m_inst_set->GetEnergyCost(cur_inst); // reset instruction energy cost
}
#endif
return true;
}
+
+void cHardwareBase::ResetInstructionCosts()
+{
+ const int num_inst_cost = m_inst_set->GetSize();
+
+ m_inst_cost = 0;
+
+ inst_ft_cost.Resize(num_inst_cost);
+ inst_energy_cost.Resize(num_inst_cost);
+
+ m_has_costs = false;
+ m_has_ft_costs = false;
+ m_has_energy_costs = false;
+
+ for (int i = 0; i < num_inst_cost; i++) {
+ if (!m_has_costs && m_inst_set->GetCost(cInstruction(i))) m_has_costs = true;
+
+ inst_ft_cost[i] = m_inst_set->GetFTCost(cInstruction(i));
+ if (!m_has_ft_costs && inst_ft_cost[i]) m_has_ft_costs = true;
+
+ inst_energy_cost[i] = m_inst_set->GetEnergyCost(cInstruction(i));
+ if(!m_has_energy_costs && inst_energy_cost[i]) m_has_energy_costs = true;
+ }
+
+}
Modified: development/source/cpu/cHardwareBase.h
===================================================================
--- development/source/cpu/cHardwareBase.h 2008-01-27 20:41:10 UTC (rev 2282)
+++ development/source/cpu/cHardwareBase.h 2008-01-28 01:51:17 UTC (rev 2283)
@@ -59,22 +59,17 @@
cHardwareTracer* m_tracer; // Set this if you want execution traced.
// Instruction costs...
-//#if INSTRUCTION_COSTS
- tArray<int> inst_cost;
+ int m_inst_cost;
tArray<int> inst_ft_cost;
tArray<int> inst_energy_cost;
bool m_has_costs;
bool m_has_ft_costs;
bool m_has_energy_costs;
-//#endif
virtual int GetExecutedSize(const int parent_size);
virtual int GetCopiedSize(const int parent_size, const int child_size) = 0;
bool Divide_CheckViable(cAvidaContext& ctx, const int parent_size, const int child_size);
-public: //@JEB
- unsigned Divide_DoMutations(cAvidaContext& ctx, double mut_multiplier = 1.0, const int maxmut = INT_MAX);
- bool Divide_TestFitnessMeasures(cAvidaContext& ctx);
protected:
unsigned Divide_DoExactMutations(cAvidaContext& ctx, double mut_multiplier = 1.0, const int pointmut = INT_MAX);
@@ -87,6 +82,10 @@
int TriggerMutations_ScopeGlobal(cAvidaContext& ctx, const cMutation* cur_mut,
cCPUMemory& target_memory, cHeadCPU& cur_head, const double rate);
+ virtual bool SingleProcess_PayCosts(cAvidaContext& ctx, const cInstruction& cur_inst);
+ void ResetInstructionCosts();
+
+
cHardwareBase(); // @not_implemented
cHardwareBase(const cHardwareBase&); // @not_implemented
cHardwareBase& operator=(const cHardwareBase&); // @not_implemented
@@ -107,8 +106,10 @@
// -------- Core Functionality --------
virtual void Reset() = 0;
virtual void SingleProcess(cAvidaContext& ctx) = 0;
- virtual bool SingleProcess_PayCosts(cAvidaContext& ctx, const cInstruction& cur_inst); //should be protected
virtual void ProcessBonusInst(cAvidaContext& ctx, const cInstruction& inst) = 0;
+
+ unsigned Divide_DoMutations(cAvidaContext& ctx, double mut_multiplier = 1.0, const int maxmut = INT_MAX);
+ bool Divide_TestFitnessMeasures(cAvidaContext& ctx);
// -------- Helper methods --------
Modified: development/source/cpu/cHardwareCPU.cc
===================================================================
--- development/source/cpu/cHardwareCPU.cc 2008-01-27 20:41:10 UTC (rev 2282)
+++ development/source/cpu/cHardwareCPU.cc 2008-01-28 01:51:17 UTC (rev 2283)
@@ -427,7 +427,7 @@
, m_executedmatchstrings(hardware_cpu.m_executedmatchstrings)
{
#if INSTRUCTION_COSTS
- inst_cost = hardware_cpu.inst_cost;
+ m_inst_cost = hardware_cpu.m_inst_cost;
inst_ft_cost = hardware_cpu.inst_ft_cost;
inst_energy_cost = hardware_cpu.inst_energy_cost;
m_has_costs = hardware_cpu.m_has_costs;
@@ -454,27 +454,8 @@
m_mal_active = false;
m_executedmatchstrings = false;
-#if INSTRUCTION_COSTS
- // instruction cost arrays
- const int num_inst_cost = m_inst_set->GetSize();
- inst_cost.Resize(num_inst_cost);
- inst_ft_cost.Resize(num_inst_cost);
- inst_energy_cost.Resize(num_inst_cost);
- m_has_costs = false;
- m_has_ft_costs = false;
- m_has_energy_costs = false;
-
- for (int i = 0; i < num_inst_cost; i++) {
- inst_cost[i] = m_inst_set->GetCost(cInstruction(i));
- if (!m_has_costs && inst_cost[i]) m_has_costs = true;
-
- inst_ft_cost[i] = m_inst_set->GetFTCost(cInstruction(i));
- if (!m_has_ft_costs && inst_ft_cost[i]) m_has_ft_costs = true;
- inst_energy_cost[i] = m_inst_set->GetEnergyCost(cInstruction(i));
- if(!m_has_energy_costs && inst_energy_cost[i]) m_has_energy_costs = true;
- }
-#endif
+ ResetInstructionCosts();
// Promoter model
if (m_world->GetConfig().PROMOTERS_ENABLED.Get())
Modified: development/source/cpu/cHardwareExperimental.cc
===================================================================
--- development/source/cpu/cHardwareExperimental.cc 2008-01-27 20:41:10 UTC (rev 2282)
+++ development/source/cpu/cHardwareExperimental.cc 2008-01-28 01:51:17 UTC (rev 2283)
@@ -192,7 +192,7 @@
, m_executedmatchstrings(hardware_cpu.m_executedmatchstrings)
{
#if INSTRUCTION_COSTS
- inst_cost = hardware_cpu.inst_cost;
+ m_inst_cost = hardware_cpu.m_inst_cost;
inst_ft_cost = hardware_cpu.inst_ft_cost;
inst_energy_cost = hardware_cpu.inst_energy_cost;
m_has_costs = hardware_cpu.m_has_costs;
@@ -217,28 +217,8 @@
m_mal_active = false;
m_executedmatchstrings = false;
-#if INSTRUCTION_COSTS
- // instruction cost arrays
- const int num_inst_cost = m_inst_set->GetSize();
- inst_cost.Resize(num_inst_cost);
- inst_ft_cost.Resize(num_inst_cost);
- inst_energy_cost.Resize(num_inst_cost);
- m_has_costs = false;
- m_has_ft_costs = false;
- m_has_energy_costs = false;
-
- for (int i = 0; i < num_inst_cost; i++) {
- inst_cost[i] = m_inst_set->GetCost(cInstruction(i));
- if (!m_has_costs && inst_cost[i]) m_has_costs = true;
-
- inst_ft_cost[i] = m_inst_set->GetFTCost(cInstruction(i));
- if (!m_has_ft_costs && inst_ft_cost[i]) m_has_ft_costs = true;
+ ResetInstructionCosts();
- inst_energy_cost[i] = m_inst_set->GetEnergyCost(cInstruction(i));
- if(!m_has_energy_costs && inst_energy_cost[i]) m_has_energy_costs = true;
- }
-#endif
-
// Promoter model
if (m_world->GetConfig().PROMOTERS_ENABLED.Get()) {
m_promoter_index = -1; // Meaning the last promoter was nothing
Modified: development/source/cpu/cHardwareGX.cc
===================================================================
--- development/source/cpu/cHardwareGX.cc 2008-01-27 20:41:10 UTC (rev 2282)
+++ development/source/cpu/cHardwareGX.cc 2008-01-28 01:51:17 UTC (rev 2283)
@@ -406,27 +406,7 @@
m_mal_active = false;
m_executedmatchstrings = false;
-#if INSTRUCTION_COSTS
- // instruction cost arrays
- const int num_inst_cost = m_inst_set->GetSize();
- inst_cost.Resize(num_inst_cost);
- inst_ft_cost.Resize(num_inst_cost);
- inst_energy_cost.Resize(num_inst_cost);
- m_has_costs = false;
- m_has_ft_costs = false;
- m_has_energy_costs = false;
-
- for (int i = 0; i < num_inst_cost; i++) {
- inst_cost[i] = m_inst_set->GetCost(cInstruction(i));
- if (!m_has_costs && inst_cost[i]) m_has_costs = true;
-
- inst_ft_cost[i] = m_inst_set->GetFTCost(cInstruction(i));
- if (!m_has_ft_costs && inst_ft_cost[i]) m_has_ft_costs = true;
-
- inst_energy_cost[i] = m_inst_set->GetEnergyCost(cInstruction(i));
- if(!m_has_energy_costs && inst_energy_cost[i]) m_has_energy_costs = true;
- }
-#endif
+ ResetInstructionCosts();
}
/*! In cHardwareGX, SingleProcess is something of a misnomer. Each time this method
Modified: development/source/cpu/cHardwareSMT.cc
===================================================================
--- development/source/cpu/cHardwareSMT.cc 2008-01-27 20:41:10 UTC (rev 2282)
+++ development/source/cpu/cHardwareSMT.cc 2008-01-28 01:51:17 UTC (rev 2283)
@@ -167,28 +167,8 @@
Stack(i).Clear();
}
-#if INSTRUCTION_COSTS
- // instruction cost arrays
- const int num_inst_cost = m_inst_set->GetSize();
- inst_cost.Resize(num_inst_cost);
- inst_ft_cost.Resize(num_inst_cost);
- inst_energy_cost.Resize(num_inst_cost);
- m_has_costs = false;
- m_has_ft_costs = false;
- m_has_energy_costs = false;
+ ResetInstructionCosts();
- for (int i = 0; i < num_inst_cost; i++) {
- inst_cost[i] = m_inst_set->GetCost(cInstruction(i));
- if (!m_has_costs && inst_cost[i]) m_has_costs = true;
-
- inst_ft_cost[i] = m_inst_set->GetFTCost(cInstruction(i));
- if (!m_has_ft_costs && inst_ft_cost[i]) m_has_ft_costs = true;
-
- inst_energy_cost[i] = m_inst_set->GetEnergyCost(cInstruction(i));
- if(!m_has_energy_costs && inst_energy_cost[i]) m_has_energy_costs = true;
- }
-#endif
-
organism->NetReset();
}
Modified: development/source/cpu/cHardwareTransSMT.cc
===================================================================
--- development/source/cpu/cHardwareTransSMT.cc 2008-01-27 20:41:10 UTC (rev 2282)
+++ development/source/cpu/cHardwareTransSMT.cc 2008-01-28 01:51:17 UTC (rev 2283)
@@ -164,28 +164,8 @@
Stack(i).Clear();
}
-#if INSTRUCTION_COSTS
- // instruction cost arrays
- const int num_inst_cost = m_inst_set->GetSize();
- inst_cost.Resize(num_inst_cost);
- inst_ft_cost.Resize(num_inst_cost);
- inst_energy_cost.Resize(num_inst_cost);
- m_has_costs = false;
- m_has_ft_costs = false;
- m_has_energy_costs = false;
+ ResetInstructionCosts();
- for (int i = 0; i < num_inst_cost; i++) {
- inst_cost[i] = m_inst_set->GetCost(cInstruction(i));
- if (!m_has_costs && inst_cost[i]) m_has_costs = true;
-
- inst_ft_cost[i] = m_inst_set->GetFTCost(cInstruction(i));
- if (!m_has_ft_costs && inst_ft_cost[i]) m_has_ft_costs = true;
-
- inst_energy_cost[i] = m_inst_set->GetEnergyCost(cInstruction(i));
- if(!m_has_energy_costs && inst_energy_cost[i]) m_has_energy_costs = true;
- }
-#endif
-
organism->ClearParasites();
organism->NetReset();
}
More information about the Avida-cvs
mailing list