Skip to content

Commit e9b9d85

Browse files
committed
More code refactorings
1 parent fc13f65 commit e9b9d85

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

NeuralNetwork.NET/Extensions/SpanExtensions.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Runtime.CompilerServices;
32
using System.Threading.Tasks;
43
using JetBrains.Annotations;
54

@@ -30,14 +29,14 @@ public static unsafe void Fill<T>(this Span<T> span, [NotNull] Func<T> provider)
3029
Parallel.For(0, cores, i =>
3130
{
3231
T* p = pc + i * batch;
33-
for (int j = 0; j < batch; j++, p++)
34-
Unsafe.Write(p, provider());
32+
for (int j = 0; j < batch; j++)
33+
p[j] = provider();
3534
}).AssertCompleted();
3635

3736
// Remaining values
3837
if (mod < 1) return;
3938
for (int i = span.Length - mod; i < span.Length; i++)
40-
Unsafe.Write(pc + i, provider());
39+
pc[i] = provider();
4140
}
4241
}
4342

@@ -72,7 +71,7 @@ public static unsafe int GetContentHashCode<T>(this Span<T> span) where T : unma
7271
unchecked
7372
{
7473
for (int i = 0; i < span.Length; i++)
75-
hash = hash * 23 + Unsafe.Read<T>(p0 + i).GetHashCode();
74+
hash = hash * 23 + p0[i].GetHashCode();
7675
return hash;
7776
}
7877
}

NeuralNetwork.NET/SupervisedLearning/Data/BatchesCollection.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,21 @@ public int BatchSize
6868
public long ByteSize => sizeof(float) * Count * (InputFeatures + OutputFeatures);
6969

7070
/// <inheritdoc/>
71-
public int Id
71+
public unsafe int Id
7272
{
7373
get
7474
{
7575
int[] temp = new int[Count];
76+
int
77+
wx = InputFeatures,
78+
wy = OutputFeatures;
7679
Parallel.For(0, Count, i =>
7780
{
7881
ref readonly SamplesBatch batch = ref Batches[i / BatchSize];
7982
int offset = i % BatchSize;
80-
temp[i] = batch.X.Slice(offset).GetContentHashCode() ^ batch.Y.Slice(offset).GetContentHashCode();
83+
fixed (float* px = batch.X, py = batch.Y)
84+
temp[i] = new Span<float>(px + offset * wx, wx).GetContentHashCode() ^
85+
new Span<float>(py + offset * wy, wy).GetContentHashCode();
8186
}).AssertCompleted();
8287
Array.Sort(temp);
8388
return temp.AsSpan().GetContentHashCode();

NeuralNetwork.NET/SupervisedLearning/Parameters/DatasetBase.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,22 @@ public DatasetSample this[int i]
4343
}
4444

4545
/// <inheritdoc/>
46-
public int Id
46+
public unsafe int Id
4747
{
4848
get
4949
{
5050
int[] temp = new int[Count];
51-
Parallel.For(0, Count, i => temp[i] = Dataset.X.Slice(i).GetContentHashCode() ^ Dataset.Y.Slice(i).GetContentHashCode()).AssertCompleted();
51+
int
52+
wx = Dataset.X.GetLength(1),
53+
wy = Dataset.Y.GetLength(1);
54+
fixed (float* px0 = Dataset.X, py0 = Dataset.Y)
55+
{
56+
float* px = px0, py = py0;
57+
Parallel.For(0, Count, i =>
58+
{
59+
temp[i] = new Span<float>(px + i * wx, wx).GetContentHashCode() ^ new Span<float>(py + i * wy, wy).GetContentHashCode();
60+
}).AssertCompleted();
61+
}
5262
Array.Sort(temp);
5363
return temp.AsSpan().GetContentHashCode();
5464
}

0 commit comments

Comments
 (0)