Skip to content

Commit

Permalink
GitHub issue #47: add test and fix
Browse files Browse the repository at this point in the history
  • Loading branch information
friflo committed Jan 1, 2025
1 parent 8a01189 commit a6d3950
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/ECS/Query/Chunk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,23 @@ public readonly struct Chunk<T>
/// </code>
/// </remarks>
public Span<TTo> AsSpan256<TTo>() where TTo : struct
=> MemoryMarshal.Cast<T, TTo>(new Span<T>(ArchetypeComponents, 0, (Length + StructPadding<T>.PadCount256) & 0x7fff_ffe0));
=> MemoryMarshal.Cast<T, TTo>(new Span<T>(ArchetypeComponents, start, (Length + StructPadding<T>.PadCount256) & 0x7fff_ffe0));

/// <summary>
/// Return the components as a <see cref="Span{TTo}"/> of type <typeparamref name="TTo"/>.<br/>
/// The returned <see cref="Span{TTo}"/> contains padding elements on its tail to enable assignment to Vector128{TTo}.<br/>
/// See <a href="https://friflo.gitbook.io/friflo.engine.ecs/documentation/query-optimization#query-vectorization-simd">Example.</a>.
/// </summary>
public Span<TTo> AsSpan128<TTo>() where TTo : struct
=> MemoryMarshal.Cast<T, TTo>(new Span<T>(ArchetypeComponents, 0, (Length + StructPadding<T>.PadCount128) & 0x7fff_fff0));
=> MemoryMarshal.Cast<T, TTo>(new Span<T>(ArchetypeComponents, start, (Length + StructPadding<T>.PadCount128) & 0x7fff_fff0));

/// <summary>
/// Return the components as a <see cref="Span{TTo}"/> of type <typeparamref name="TTo"/>.<br/>
/// The returned <see cref="Span{TTo}"/> contains padding elements on its tail to enable assignment to Vector512.<br/>
/// See <a href="https://friflo.gitbook.io/friflo.engine.ecs/documentation/query-optimization#query-vectorization-simd">Example.</a>.
/// </summary>
public Span<TTo> AsSpan512<TTo>() where TTo : struct
=> MemoryMarshal.Cast<T, TTo>(new Span<T>(ArchetypeComponents, 0, (Length + StructPadding<T>.PadCount512) & 0x7fff_ffc0));
=> MemoryMarshal.Cast<T, TTo>(new Span<T>(ArchetypeComponents, start, (Length + StructPadding<T>.PadCount512) & 0x7fff_ffc0));

/// <summary>
/// The step value in a for loop when converting a <see cref="AsSpan128{TTo}"/> value to a Vector128{T}.
Expand Down
35 changes: 35 additions & 0 deletions src/Tests/ECS/Github/Test_GitHub_47.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using Friflo.Engine.ECS;
using NUnit.Framework;
using Tests.Utils;

// ReSharper disable InconsistentNaming
namespace Tests.ECS.Github
{
/// https://github.com/friflo/Friflo.Engine.ECS/issues/47
public static class Test_GitHub_47
{
[Test]
public static void Parallel_QueryJob_using_AsSpan()
{
const int threads = 2;
Console.WriteLine("Threads count: "+ threads);
var runner = new ParallelJobRunner(threads);
var store = new EntityStore() {JobRunner = runner};
for (int n = 0; n < 64; n++) {
store.CreateEntity(new MyComponent1 { a = n });
}

var query = store.Query<MyComponent1>();
var queryJob = query.ForEach((chunk, _) =>
{
Span<int> values = chunk.AsSpan256<int>();
for (int i = 0; i < values.Length; i++) {
Mem.AreEqual(chunk[i].a, values[i]);
}
});
queryJob.MinParallelChunkLength = 16;
queryJob.RunParallel();
}
}
}

0 comments on commit a6d3950

Please sign in to comment.