Skip to content

Commit caf2965

Browse files
author
lukas.molzberger
committed
- ref counting fixes
- removed some verbose debug output
1 parent b016594 commit caf2965

File tree

9 files changed

+36
-37
lines changed

9 files changed

+36
-37
lines changed

core/src/main/java/network/aika/Document.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ public Document(Model m, String content) {
9090
absoluteBeginChar = m.getN();
9191

9292
m.registerDocument(this);
93-
94-
LOG.info("Registered Document: " + id);
9593
}
9694

9795
public long getNewVisitorId() {
@@ -167,8 +165,6 @@ public void register(Neuron n, PreActivation<? extends Activation> acts) {
167165

168166
if(existingPreAct != null)
169167
LOG.error("Attempted to overwrite existing PreAct: (doc:" + id + " n:" + n.getId() + ")");
170-
171-
System.out.print("i" + id + ":" + n.getId() + ":" + n.hashCode() + ", ");
172168
}
173169

174170
public Range getCharRange() {
@@ -195,15 +191,12 @@ public void disconnect() {
195191
act.disconnect()
196192
);
197193

198-
String report = actsPerNeuron.values()
194+
actsPerNeuron.values()
199195
.stream()
200196
.map(PreActivation::getNeuron)
201-
.map(n -> n.removePreActivation(this))
202-
.collect(Collectors.joining(", "));
197+
.forEach(n -> n.removePreActivation(this));
203198

204199
isStale = true;
205-
206-
LOG.info("Disconnected Document: " + id + " Report:" + report);
207200
}
208201

209202
@Override

core/src/main/java/network/aika/Model.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
import java.util.function.Supplier;
3737
import java.util.stream.Stream;
3838

39-
import static network.aika.elements.neurons.RefType.NEURON_EXTERNAL;
40-
import static network.aika.elements.neurons.RefType.OTHER;
39+
import static network.aika.elements.neurons.RefType.*;
4140

4241
/**
4342
*
@@ -116,6 +115,8 @@ public <N extends Neuron> N lookupNeuronByLabel(String label, N template) {
116115
n.setAllowTraining(false);
117116

118117
registerLabel(n, template);
118+
119+
n.getProvider().decreaseRefCount(TEMPLATE);
119120
return n;
120121
}
121122

core/src/main/java/network/aika/elements/neurons/Neuron.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public void updatePropagable(NeuronProvider np, boolean isPropagable) {
127127
private void addPropagable(NeuronProvider np) {
128128
provider.outputLock.acquireWriteLock();
129129
if(propagable.add(np))
130-
np.increaseRefCount(PROPAGABLE);
130+
np.increaseRefCount(PROPAGABLE_IN);
131131

132132
provider.outputLock.releaseWriteLock();
133133
np.addPropagableRef(provider);
@@ -136,7 +136,7 @@ private void addPropagable(NeuronProvider np) {
136136
private void removePropagable(NeuronProvider np) {
137137
provider.outputLock.acquireWriteLock();
138138
if(propagable.remove(np))
139-
np.decreaseRefCount(PROPAGABLE);
139+
np.decreaseRefCount(PROPAGABLE_IN);
140140

141141
provider.outputLock.releaseWriteLock();
142142
np.removePropagableRef(provider);
@@ -206,14 +206,11 @@ public PreActivation<A> getPreActivation(Document doc) {
206206
}
207207
}
208208

209-
public String removePreActivation(Document doc) {
210-
StringBuilder sb = new StringBuilder("d" + doc.getId() + ":" + getId() + ":" + hashCode() + ":");
211-
209+
public void removePreActivation(Document doc) {
212210
synchronized (activations) {
213211
PreActivation removedPreAct = activations.remove(doc.getId());
214212
removedPreAct.disconnect();
215213
}
216-
return sb.toString();
217214
}
218215

219216
public SortedSet<A> getActivations(Document doc) {
@@ -392,27 +389,28 @@ public void suspend() {
392389
for (Synapse s : provider.getInputSynapsesStoredAtOutputSide()) {
393390
NeuronProvider in = s.getPInput();
394391

395-
in.decreaseRefCount(SYNAPSE);
396-
provider.decreaseRefCount(SYNAPSE);
392+
in.decreaseRefCount(SYNAPSE_IN);
393+
provider.decreaseRefCount(SYNAPSE_OUT);
397394

398395
provider.removeInputSynapse(s);
399396
in.removeOutputSynapse(s);
400397
}
401398
for (Synapse s : provider.getOutputSynapsesStoredAtInputSide()) {
402399
NeuronProvider out = s.getPOutput();
403400

404-
out.decreaseRefCount(SYNAPSE);
405-
provider.decreaseRefCount(SYNAPSE);
401+
out.decreaseRefCount(SYNAPSE_OUT);
402+
provider.decreaseRefCount(SYNAPSE_IN);
406403

407404
provider.removeOutputSynapse(s);
408405
out.removeInputSynapse(s);
409406
}
410407

411408
for(NeuronProvider np: propagable) {
412-
np.decreaseRefCount(PROPAGABLE);
409+
np.decreaseRefCount(PROPAGABLE_IN);
413410

414411
np.removePropagableRef(provider);
415412
}
413+
propagable = null;
416414

417415
isStale = true;
418416
}

core/src/main/java/network/aika/elements/neurons/NeuronProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,15 +364,15 @@ public Synapse selectInputSynapse(Predicate<? super Synapse> predicate) {
364364
public void addPropagableRef(NeuronProvider np) {
365365
inputLock.acquireWriteLock();
366366
if(propagableRefs.add(np))
367-
np.increaseRefCount(PROPAGABLE);
367+
np.increaseRefCount(PROPAGABLE_OUT);
368368

369369
inputLock.releaseWriteLock();
370370
}
371371

372372
public void removePropagableRef(NeuronProvider np) {
373373
inputLock.acquireWriteLock();
374374
if(propagableRefs.remove(np))
375-
np.decreaseRefCount(PROPAGABLE);
375+
np.decreaseRefCount(PROPAGABLE_OUT);
376376

377377
inputLock.releaseWriteLock();
378378
}

core/src/main/java/network/aika/elements/neurons/RefType.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ public enum RefType {
44
NEURON_EXTERNAL,
55
NEURON,
66
PERSISTENCE,
7-
SYNAPSE,
8-
PROPAGABLE,
7+
SYNAPSE_IN,
8+
SYNAPSE_OUT,
9+
PROPAGABLE_IN,
10+
PROPAGABLE_OUT,
911
TEMPLATE,
12+
CATEGORY,
1013
TEMPLATE_MODEL,
1114
OTHER
1215
}

core/src/main/java/network/aika/elements/neurons/types/BindingNeuron.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
import static network.aika.elements.Type.BINDING;
3636
import static network.aika.elements.activations.bsslots.BSSlotDefinition.*;
37-
import static network.aika.elements.neurons.RefType.TEMPLATE;
37+
import static network.aika.elements.neurons.RefType.CATEGORY;
3838

3939

4040
/**
@@ -89,14 +89,15 @@ public static BindingNeuron create(Model m, String label) {
8989

9090
@Override
9191
public BindingCategoryInputSynapse makeAbstract() {
92-
BindingCategoryNeuron bindingCategory = new BindingCategoryNeuron(getModel(), TEMPLATE)
92+
BindingCategoryNeuron bindingCategory = new BindingCategoryNeuron(getModel(), CATEGORY)
9393
.setLabel(getLabel() + CATEGORY_LABEL);
9494

9595
BindingCategoryInputSynapse s = new BindingCategoryInputSynapse()
9696
.link(bindingCategory, this);
9797

9898
s.setInitialCategorySynapseWeight(1.0);
9999

100+
bindingCategory.getProvider().decreaseRefCount(CATEGORY);
100101
return s;
101102
}
102103

core/src/main/java/network/aika/elements/neurons/types/InhibitoryNeuron.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import static network.aika.elements.Type.INHIBITORY;
3232
import static network.aika.elements.activations.bsslots.BSSlotDefinition.SINGLE_INPUT;
33+
import static network.aika.elements.neurons.RefType.CATEGORY;
3334
import static network.aika.elements.neurons.RefType.TEMPLATE;
3435

3536
/**
@@ -52,14 +53,15 @@ public InhibitoryNeuron(Model m, RefType rt) {
5253

5354
@Override
5455
public InhibitoryCategoryInputSynapse makeAbstract() {
55-
InhibitoryCategoryNeuron inhibCategory = new InhibitoryCategoryNeuron(getModel(), TEMPLATE)
56+
InhibitoryCategoryNeuron inhibCategory = new InhibitoryCategoryNeuron(getModel(), CATEGORY)
5657
.setLabel(getLabel() + CATEGORY_LABEL);
5758

5859
InhibitoryCategoryInputSynapse s = new InhibitoryCategoryInputSynapse()
5960
.link(inhibCategory, this);
6061

6162
s.setInitialCategorySynapseWeight(1.0);
6263

64+
inhibCategory.getProvider().decreaseRefCount(CATEGORY);
6365
return s;
6466
}
6567

core/src/main/java/network/aika/elements/neurons/types/PatternNeuron.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@
4040
import static network.aika.elements.Type.PATTERN;
4141
import static network.aika.elements.activations.bsslots.BSSlotDefinition.MULTI_INPUT;
4242
import static network.aika.elements.activations.bsslots.BSSlotDefinition.SINGLE_SAME;
43-
import static network.aika.elements.neurons.RefType.NEURON_EXTERNAL;
44-
import static network.aika.elements.neurons.RefType.TEMPLATE;
43+
import static network.aika.elements.neurons.RefType.*;
4544
import static network.aika.enums.sign.Sign.POS;
4645
import static network.aika.text.Range.length;
4746

@@ -78,14 +77,15 @@ public static PatternNeuron create(Model m, String label) {
7877

7978
@Override
8079
public PatternCategoryInputSynapse makeAbstract() {
81-
PatternCategoryNeuron patternCategory = new PatternCategoryNeuron(getModel(), TEMPLATE)
80+
PatternCategoryNeuron patternCategory = new PatternCategoryNeuron(getModel(), CATEGORY)
8281
.setLabel(getCategoryLabel(getLabel()));
8382

8483
PatternCategoryInputSynapse s = new PatternCategoryInputSynapse()
8584
.link(patternCategory, this);
8685

8786
s.setInitialCategorySynapseWeight(1.0);
8887

88+
patternCategory.getProvider().decreaseRefCount(CATEGORY);
8989
return s;
9090
}
9191

core/src/main/java/network/aika/elements/synapses/Synapse.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@
5454

5555
import static network.aika.elements.Timestamp.MAX;
5656
import static network.aika.elements.Timestamp.MIN;
57-
import static network.aika.elements.neurons.RefType.SYNAPSE;
57+
import static network.aika.elements.neurons.RefType.SYNAPSE_IN;
58+
import static network.aika.elements.neurons.RefType.SYNAPSE_OUT;
5859
import static network.aika.elements.synapses.SynapseTypeHolder.getHolder;
5960
import static network.aika.queue.Phase.TRAINING;
6061
import static network.aika.utils.Utils.TOLERANCE;
@@ -238,12 +239,12 @@ public void count(L l) {
238239

239240
public void setInput(I input) {
240241
this.input = input.getProvider();
241-
this.input.increaseRefCount(SYNAPSE);
242+
this.input.increaseRefCount(SYNAPSE_IN);
242243
}
243244

244245
public void setOutput(O output) {
245246
this.output = output.getProvider();
246-
this.output.increaseRefCount(SYNAPSE);
247+
this.output.increaseRefCount(SYNAPSE_OUT);
247248
}
248249

249250
public S instantiateTemplate(I input, O output) {
@@ -459,8 +460,8 @@ public static Synapse read(DataInput in, Model m) throws IOException {
459460
@Override
460461
public void readFields(DataInput in, Model m) throws IOException {
461462
synapseId = in.readInt();
462-
input = m.lookupNeuronProvider(in.readLong(), SYNAPSE);
463-
output = m.lookupNeuronProvider(in.readLong(), SYNAPSE);
463+
input = m.lookupNeuronProvider(in.readLong(), SYNAPSE_IN);
464+
output = m.lookupNeuronProvider(in.readLong(), SYNAPSE_OUT);
464465

465466
weight.readFields(in, m);
466467
instantiable = in.readBoolean();

0 commit comments

Comments
 (0)