[rebuid]use dynamic wrapping instead of static wrapping
This commit is contained in:
@@ -29,12 +29,13 @@ using System;
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
namespace StgSharp.Collections
|
namespace StgSharp.Collections
|
||||||
{
|
{
|
||||||
public sealed class BidirectionalDictionary<TFirst, TSecond> : IBidirectionalDictionary<TFirst, TSecond>
|
public sealed class BidirectionalDictionary<TFirst, TSecond> : IBidirectionalDictionary<TFirst, TSecond>
|
||||||
|
where TFirst: notnull
|
||||||
|
where TSecond: notnull
|
||||||
{
|
{
|
||||||
|
|
||||||
private readonly Dictionary<TFirst, TSecond> _forward;
|
private readonly Dictionary<TFirst, TSecond> _forward;
|
||||||
@@ -42,25 +43,21 @@ namespace StgSharp.Collections
|
|||||||
|
|
||||||
public BidirectionalDictionary()
|
public BidirectionalDictionary()
|
||||||
{
|
{
|
||||||
_forward = new Dictionary<TFirst, TSecond>();
|
_forward = [];
|
||||||
_reverse = new Dictionary<TSecond, TFirst>();
|
_reverse = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
public TFirst this[TSecond key]
|
public TFirst this[TSecond key]
|
||||||
{
|
{
|
||||||
get
|
get => _reverse.TryGetValue(key, out TFirst value) ?
|
||||||
{
|
value :
|
||||||
if (_reverse.TryGetValue(key, out TFirst value)) {
|
throw new ArgumentException($"Cannot find key {key} in dictionary.");
|
||||||
return value;
|
|
||||||
}
|
|
||||||
throw new ArgumentException($"Cannot find key {key!.ToString()} in dictionary.");
|
|
||||||
}
|
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
if (_reverse.TryGetValue(key, out TFirst origin))
|
if (_reverse.TryGetValue(key, out TFirst origin))
|
||||||
{
|
{
|
||||||
_reverse[key] = value;
|
_reverse[key] = value;
|
||||||
_forward.Remove(origin);
|
_ = _forward.Remove(origin);
|
||||||
} else
|
} else
|
||||||
{
|
{
|
||||||
_reverse.Add(key, value);
|
_reverse.Add(key, value);
|
||||||
@@ -71,19 +68,15 @@ namespace StgSharp.Collections
|
|||||||
|
|
||||||
public TSecond this[TFirst key]
|
public TSecond this[TFirst key]
|
||||||
{
|
{
|
||||||
get
|
get => _forward.TryGetValue(key, out TSecond value) ?
|
||||||
{
|
value :
|
||||||
if (_forward.TryGetValue(key, out TSecond value)) {
|
throw new ArgumentException($"Cannot find key {key} in dictionary.");
|
||||||
return value;
|
|
||||||
}
|
|
||||||
throw new ArgumentException($"Cannot find key {key!.ToString()} in dictionary.");
|
|
||||||
}
|
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
if (_forward.TryGetValue(key, out TSecond origin))
|
if (_forward.TryGetValue(key, out TSecond origin))
|
||||||
{
|
{
|
||||||
_forward[key] = value;
|
_forward[key] = value;
|
||||||
_reverse.Remove(origin);
|
_ = _reverse.Remove(origin);
|
||||||
} else
|
} else
|
||||||
{
|
{
|
||||||
_forward.Add(key, value);
|
_forward.Add(key, value);
|
||||||
|
|||||||
@@ -28,7 +28,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
namespace StgSharp.Collections
|
namespace StgSharp.Collections
|
||||||
@@ -88,6 +87,13 @@ namespace StgSharp.Collections
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public ReadOnlySpan<T> PopRange(int count)
|
||||||
|
{
|
||||||
|
int i = int.Min(count, _index) - 1;
|
||||||
|
return _values[0..i];
|
||||||
|
}
|
||||||
|
|
||||||
public void Push(T item)
|
public void Push(T item)
|
||||||
{
|
{
|
||||||
_values[++_index] = _index < _values.Length - 1 ?
|
_values[++_index] = _index < _values.Length - 1 ?
|
||||||
|
|||||||
@@ -25,60 +25,60 @@
|
|||||||
//
|
//
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
using System;
|
|
||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace StgSharp.Collections
|
namespace StgSharp.Collections
|
||||||
{
|
{
|
||||||
public interface IBidirectionalDictionary<TFirst, TSecond> : IDictionary<TFirst, TSecond>
|
public interface IBidirectionalDictionary<TFirst, TSecond> : IDictionary<TFirst, TSecond>
|
||||||
{
|
{
|
||||||
|
|
||||||
public TFirst this[TSecond key] { get; set; }
|
TFirst this[TSecond key] { get; set; }
|
||||||
|
|
||||||
public ICollection<TFirst> FirstIndex { get; }
|
ICollection<TFirst> FirstIndex { get; }
|
||||||
|
|
||||||
public ICollection<TSecond> SecondIndex { get; }
|
ICollection<TSecond> SecondIndex { get; }
|
||||||
|
|
||||||
public IReadOnlyDictionary<TFirst, TSecond> Forward { get; }
|
IReadOnlyDictionary<TFirst, TSecond> Forward { get; }
|
||||||
|
|
||||||
public IReadOnlyDictionary<TSecond, TFirst> Reverse { get; }
|
IReadOnlyDictionary<TSecond, TFirst> Reverse { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Determines whether the <see cref="IBidirectionalDictionary{TFirst, TSecond}" />
|
/// Determines whether the <see cref="IBidirectionalDictionary{TFirst, TSecond}" /> contains the
|
||||||
/// contains the specified key.
|
/// specified key.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param _label="key">
|
/// <param _label="key">
|
||||||
/// The value to locate in the <see cref="IBidirectionalDictionary{TFirst, TSecond}" />.
|
/// The value to locate in the <see cref="IBidirectionalDictionary{TFirst, TSecond}" />.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// true if the <see cref="IBidirectionalDictionary{TFirst, TSecond}" /> contains an
|
/// true if the <see cref="IBidirectionalDictionary{TFirst, TSecond}" /> contains an element with
|
||||||
/// element with the specified key; otherwise, false.
|
/// the specified key; otherwise, false.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
public bool Contains(TSecond key);
|
bool Contains(TSecond key);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Determines whether the <see cref="IBidirectionalDictionary{TFirst, TSecond}" />
|
/// Determines whether the <see cref="IBidirectionalDictionary{TFirst, TSecond}" /> contains the
|
||||||
/// contains the specified key.
|
/// specified key.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param _label="key">
|
/// <param _label="key">
|
||||||
/// The value to locate in the <see cref="IBidirectionalDictionary{TFirst, TSecond}" />.
|
/// The value to locate in the <see cref="IBidirectionalDictionary{TFirst, TSecond}" />.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// true if the <see cref="IBidirectionalDictionary{TFirst, TSecond}" /> contains an
|
/// true if the <see cref="IBidirectionalDictionary{TFirst, TSecond}" /> contains an element with
|
||||||
/// element with the specified key; otherwise, false.
|
/// the specified key; otherwise, false.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
public bool Contains(TFirst key);
|
bool Contains(TFirst key);
|
||||||
|
|
||||||
public bool TryGetValue(TSecond key, out TFirst value);
|
bool TryGetValue(TSecond key, out TFirst value);
|
||||||
|
|
||||||
#pragma warning disable CA1033
|
#pragma warning disable CA1033
|
||||||
ICollection<TFirst> IDictionary<TFirst, TSecond>.Keys => FirstIndex;
|
ICollection<TFirst> IDictionary<TFirst, TSecond>.Keys => FirstIndex;
|
||||||
|
|
||||||
ICollection<TSecond> IDictionary<TFirst, TSecond>.Values => SecondIndex;
|
ICollection<TSecond> IDictionary<TFirst, TSecond>.Values => SecondIndex;
|
||||||
|
|
||||||
bool IDictionary<TFirst, TSecond>.ContainsKey(TFirst key) => Contains(key);
|
bool IDictionary<TFirst, TSecond>.ContainsKey(TFirst key)
|
||||||
|
{
|
||||||
|
return Contains(key);
|
||||||
|
}
|
||||||
#pragma warning restore CA1033
|
#pragma warning restore CA1033
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,174 +0,0 @@
|
|||||||
//-----------------------------------------------------------------------
|
|
||||||
// -----------------------------------------------------------------------
|
|
||||||
// file="PartialDictionary"
|
|
||||||
// Project: StgSharp
|
|
||||||
// AuthorGroup: Nitload
|
|
||||||
// Copyright (c) Nitload. All rights reserved.
|
|
||||||
//
|
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
// of this software and associated documentation files (the "Software"), to deal
|
|
||||||
// in the Software without restriction, including without limitation the rights
|
|
||||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
// copies of the Software, and to permit persons to whom the Software is
|
|
||||||
// furnished to do so, subject to the following conditions:
|
|
||||||
//
|
|
||||||
// The above copyright notice and this permission notice shall be included in all
|
|
||||||
// copies or substantial portions of the Software.
|
|
||||||
//
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
// SOFTWARE.
|
|
||||||
//
|
|
||||||
// -----------------------------------------------------------------------
|
|
||||||
// -----------------------------------------------------------------------
|
|
||||||
using System;
|
|
||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Diagnostics.CodeAnalysis;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Net.Http.Headers;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace StgSharp.Collections
|
|
||||||
{
|
|
||||||
public partial class PartialDictionary<TKey, TValue> : IDictionary<TKey, TValue>
|
|
||||||
{
|
|
||||||
|
|
||||||
private readonly Dictionary<TKey, TValue> _source;
|
|
||||||
private readonly HashSet<TKey> _keys;
|
|
||||||
private readonly PartialDictionaryValues _values;
|
|
||||||
|
|
||||||
public PartialDictionary(Dictionary<TKey, TValue> source, TKey[] index)
|
|
||||||
{
|
|
||||||
_source = source;
|
|
||||||
_keys = new HashSet<TKey>(index);
|
|
||||||
foreach (TKey item in _keys)
|
|
||||||
{
|
|
||||||
if (!_source.ContainsKey(item)) {
|
|
||||||
_source.Add(item, default!);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_values = new PartialDictionaryValues(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public PartialDictionary(Dictionary<TKey, TValue> source, HashSet<TKey> keys)
|
|
||||||
{
|
|
||||||
_source = source;
|
|
||||||
_keys = keys;
|
|
||||||
foreach (TKey item in _keys)
|
|
||||||
{
|
|
||||||
if (!_source.ContainsKey(item)) {
|
|
||||||
_source.Add(item, default!);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_values = new PartialDictionaryValues(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public PartialDictionary(Dictionary<TKey, TValue> source, TKey[] index, IHashMultiplexer<TKey> multiplexer)
|
|
||||||
{
|
|
||||||
_source = source;
|
|
||||||
_keys = new HashSet<TKey>(index);
|
|
||||||
foreach (TKey item in _keys)
|
|
||||||
{
|
|
||||||
if (!_source.ContainsKey(item)) {
|
|
||||||
_source.Add(item, default!);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_values = new PartialDictionaryValues(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public TValue this[TKey key]
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
if (_keys.Contains(key)) {
|
|
||||||
return _source[key];
|
|
||||||
}
|
|
||||||
throw new KeyNotFoundException();
|
|
||||||
}
|
|
||||||
set
|
|
||||||
{
|
|
||||||
if (_keys.Contains(key)) {
|
|
||||||
_source[key] = value;
|
|
||||||
}
|
|
||||||
throw new KeyNotFoundException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public ICollection<TKey> Keys => _keys;
|
|
||||||
|
|
||||||
public ICollection<TValue> Values => _values;
|
|
||||||
|
|
||||||
public int Count => _keys.Count;
|
|
||||||
|
|
||||||
public bool Contains(KeyValuePair<TKey, TValue> item)
|
|
||||||
{
|
|
||||||
return _keys.Contains(item.Key) && _source.Contains(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool ContainsKey(TKey key)
|
|
||||||
{
|
|
||||||
return _keys.Contains(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
|
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
|
|
||||||
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
|
|
||||||
{
|
|
||||||
foreach (TKey item in _keys) {
|
|
||||||
yield return new KeyValuePair<TKey, TValue>(item, _source[item]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool TryGetValue(TKey key, out TValue value)
|
|
||||||
{
|
|
||||||
if (_keys.Contains(key))
|
|
||||||
{
|
|
||||||
value = _source[key];
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
value = default!;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ICollection<KeyValuePair<TKey, TValue>>.Clear()
|
|
||||||
{
|
|
||||||
throw new NotSupportedException();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
|
|
||||||
{
|
|
||||||
throw new NotSupportedException();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly => true;
|
|
||||||
|
|
||||||
void IDictionary<TKey, TValue>.Add(TKey key, TValue value)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IDictionary<TKey, TValue>.Remove(TKey key)
|
|
||||||
{
|
|
||||||
throw new NotSupportedException();
|
|
||||||
}
|
|
||||||
|
|
||||||
IEnumerator IEnumerable.GetEnumerator()
|
|
||||||
{
|
|
||||||
return GetEnumerator();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,102 +0,0 @@
|
|||||||
//-----------------------------------------------------------------------
|
|
||||||
// -----------------------------------------------------------------------
|
|
||||||
// file="PartialDictionaryValues"
|
|
||||||
// Project: StgSharp
|
|
||||||
// AuthorGroup: Nitload
|
|
||||||
// Copyright (c) Nitload. All rights reserved.
|
|
||||||
//
|
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
// of this software and associated documentation files (the "Software"), to deal
|
|
||||||
// in the Software without restriction, including without limitation the rights
|
|
||||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
// copies of the Software, and to permit persons to whom the Software is
|
|
||||||
// furnished to do so, subject to the following conditions:
|
|
||||||
//
|
|
||||||
// The above copyright notice and this permission notice shall be included in all
|
|
||||||
// copies or substantial portions of the Software.
|
|
||||||
//
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
// SOFTWARE.
|
|
||||||
//
|
|
||||||
// -----------------------------------------------------------------------
|
|
||||||
// -----------------------------------------------------------------------
|
|
||||||
using System;
|
|
||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace StgSharp.Collections
|
|
||||||
{
|
|
||||||
public partial class PartialDictionary<TKey, TValue>
|
|
||||||
{
|
|
||||||
|
|
||||||
internal sealed class PartialDictionaryValues : ICollection<TValue>
|
|
||||||
{
|
|
||||||
|
|
||||||
private readonly PartialDictionary<TKey, TValue> _values;
|
|
||||||
|
|
||||||
public PartialDictionaryValues(PartialDictionary<TKey, TValue> value)
|
|
||||||
{
|
|
||||||
_values = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool IsReadOnly => true;
|
|
||||||
|
|
||||||
public int Count => _values.Count;
|
|
||||||
|
|
||||||
public bool Contains(TValue item)
|
|
||||||
{
|
|
||||||
foreach (KeyValuePair<TKey, TValue> kvp in _values)
|
|
||||||
{
|
|
||||||
if (kvp.Value != null && kvp.Value.Equals(item)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void CopyTo(TValue[] array, int arrayIndex)
|
|
||||||
{
|
|
||||||
foreach (KeyValuePair<TKey, TValue> kvp in _values)
|
|
||||||
{
|
|
||||||
array[arrayIndex] = kvp.Value;
|
|
||||||
arrayIndex++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public IEnumerator<TValue> GetEnumerator()
|
|
||||||
{
|
|
||||||
foreach (TKey key in _values._keys) {
|
|
||||||
yield return _values._source[key];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ICollection<TValue>.Add(TValue item)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ICollection<TValue>.Clear()
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ICollection<TValue>.Remove(TValue item)
|
|
||||||
{
|
|
||||||
throw new NotSupportedException();
|
|
||||||
}
|
|
||||||
|
|
||||||
IEnumerator IEnumerable.GetEnumerator()
|
|
||||||
{
|
|
||||||
return GetEnumerator();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -28,10 +28,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace StgSharp.Collections
|
namespace StgSharp.Collections
|
||||||
{
|
{
|
||||||
@@ -176,7 +172,10 @@ namespace StgSharp.Collections
|
|||||||
Array.Resize(ref _array, newCapacity);
|
Array.Resize(ref _array, newCapacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
IEnumerator IEnumerable.GetEnumerator()
|
||||||
|
{
|
||||||
|
return GetEnumerator();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -102,8 +102,8 @@ namespace StgSharp.Collections
|
|||||||
|
|
||||||
public int RemainedMultiplexCount
|
public int RemainedMultiplexCount
|
||||||
{
|
{
|
||||||
get => _multiplexers.Value.RemainedMultiplexCount;
|
get => _multiplexers.Value!.RemainedMultiplexCount;
|
||||||
set => _multiplexers.Value.RemainedMultiplexCount = value;
|
set => _multiplexers.Value!.RemainedMultiplexCount = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void CacheHash(string obj, int count)
|
public void CacheHash(string obj, int count)
|
||||||
|
|||||||
@@ -25,6 +25,7 @@
|
|||||||
//
|
//
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
|
using StgSharp.Collections;
|
||||||
using StgSharp.HighPerformance;
|
using StgSharp.HighPerformance;
|
||||||
using StgSharp.Internal;
|
using StgSharp.Internal;
|
||||||
using System;
|
using System;
|
||||||
@@ -48,8 +49,8 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
long count = (long)left.KernelColumnLength * left.KernelRowLength;
|
long count = (long)left.KernelColumnLength * left.KernelRowLength;
|
||||||
if (count <= 1024)
|
if (count <= 1024)
|
||||||
{
|
{
|
||||||
MatrixParallelPool pool = MatrixParallel.LeadParallel();
|
CapacityFixedStack<MatrixParallelThread> pool = MatrixParallel.LeadParallel();
|
||||||
if (!pool.TryRentIndividualHandle(out MatrixParallelWrap.Handle? handle))
|
if (!pool.TryPop(out MatrixParallelThread thread))
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* wait
|
* wait
|
||||||
@@ -82,8 +83,9 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
long count = (long)left.KernelColumnLength * left.KernelRowLength;
|
long count = (long)left.KernelColumnLength * left.KernelRowLength;
|
||||||
if (count <= 1024)
|
if (count <= 1024)
|
||||||
{
|
{
|
||||||
MatrixParallelPool pool = MatrixParallel.LeadParallel();
|
// TODO: this is very awkward, blocking thread renting, need a better way to handle small tasks
|
||||||
if (!pool.TryRentIndividualHandle(out MatrixParallelWrap.Handle? handle))
|
CapacityFixedStack<MatrixParallelThread> pool = MatrixParallel.LeadParallel();
|
||||||
|
if (!pool.TryPop(out MatrixParallelThread thread))
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* wait
|
* wait
|
||||||
@@ -95,6 +97,7 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
NativeIntrinsic.Intrinsic.f32_sub(leftPtr + i, rightPtr + i, ansPtr + i);
|
NativeIntrinsic.Intrinsic.f32_sub(leftPtr + i, rightPtr + i, ansPtr + i);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ans;
|
return ans;
|
||||||
}
|
}
|
||||||
Stack<MatrixParallelWrap> taskGroup = MatrixParallel.PublicTask(left.GetColumnEnumeration(), right.GetColumnEnumeration(), ans.GetColumnEnumeration(),
|
Stack<MatrixParallelWrap> taskGroup = MatrixParallel.PublicTask(left.GetColumnEnumeration(), right.GetColumnEnumeration(), ans.GetColumnEnumeration(),
|
||||||
|
|||||||
@@ -1,40 +0,0 @@
|
|||||||
//-----------------------------------------------------------------------
|
|
||||||
// -----------------------------------------------------------------------
|
|
||||||
// file="MatrixMarshal.cs"
|
|
||||||
// Project: StgSharp
|
|
||||||
// AuthorGroup: Nitload Space
|
|
||||||
// Copyright (c) Nitload Space. All rights reserved.
|
|
||||||
//
|
|
||||||
// Permission is hereby granted, free of charge, to any person
|
|
||||||
// obtaining a copy of this software and associated documentation
|
|
||||||
// files (the “Software”), to deal in the Software without restriction,
|
|
||||||
// including without limitation the rights to use, copy, modify, merge,
|
|
||||||
// publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
// and to permit persons to whom the Software is furnished to do so,
|
|
||||||
// subject to the following conditions:
|
|
||||||
//
|
|
||||||
// The above copyright notice and
|
|
||||||
// this permission notice shall be included in all copies
|
|
||||||
// or substantial portions of the Software.
|
|
||||||
//
|
|
||||||
// THE SOFTWARE IS PROVIDED “AS IS”,
|
|
||||||
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
||||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
||||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
||||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
||||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH
|
|
||||||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
//
|
|
||||||
// -----------------------------------------------------------------------
|
|
||||||
// -----------------------------------------------------------------------
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace StgSharp.Mathematics
|
|
||||||
{
|
|
||||||
public readonly ref struct MatrixMarshal { }
|
|
||||||
}
|
|
||||||
@@ -837,6 +837,8 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
{
|
{
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
throw new InvalidOperationException("Someone steal parallel wraps from the pool.");
|
throw new InvalidOperationException("Someone steal parallel wraps from the pool.");
|
||||||
|
#else
|
||||||
|
break;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -854,6 +856,8 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
{
|
{
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
throw new InvalidOperationException("Zero tasks published.");
|
throw new InvalidOperationException("Zero tasks published.");
|
||||||
|
#else
|
||||||
|
break;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -54,7 +54,6 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
public static partial class MatrixParallel
|
public static partial class MatrixParallel
|
||||||
{
|
{
|
||||||
|
|
||||||
private static Thread[] _threads;
|
|
||||||
private static readonly (int a, int b)[] LookupTable = [
|
private static readonly (int a, int b)[] LookupTable = [
|
||||||
(1, 1), // 7
|
(1, 1), // 7
|
||||||
(0, 2), // 8 (no non-zero solution)
|
(0, 2), // 8 (no non-zero solution)
|
||||||
@@ -73,6 +72,8 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
];
|
];
|
||||||
private static bool _meaningful;
|
private static bool _meaningful;
|
||||||
|
|
||||||
|
private static CapacityFixedStack<MatrixParallelThread> _threads;
|
||||||
|
|
||||||
private static volatile int RemainThreadCount = 0;
|
private static volatile int RemainThreadCount = 0;
|
||||||
private static readonly object _lock = new();
|
private static readonly object _lock = new();
|
||||||
private static ThreadPriority _AfterWork = ThreadPriority.Lowest;
|
private static ThreadPriority _AfterWork = ThreadPriority.Lowest;
|
||||||
@@ -118,46 +119,16 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int count = Environment.ProcessorCount - 4;
|
int count = Environment.ProcessorCount - 4;
|
||||||
count = count == 5 ? 4 : count;
|
_threads = new(count);
|
||||||
_threads = new Thread[count];
|
for (int i = 0; i < count; i++) {
|
||||||
if (count < 6)
|
_threads.Push(new());
|
||||||
{
|
|
||||||
if (count == 3)
|
|
||||||
{
|
|
||||||
Pool = new(3, [ new MatrixParallelWrap(0, count) ]);
|
|
||||||
(LargeWrapCount, SmallWrapCount) = (0, 1);
|
|
||||||
} else
|
|
||||||
{
|
|
||||||
count = 4;
|
|
||||||
Pool = new(4, [ new MatrixParallelWrap(0, count) ]);
|
|
||||||
(LargeWrapCount, SmallWrapCount) = (1, 0);
|
|
||||||
}
|
|
||||||
} else if (count == 6)
|
|
||||||
{
|
|
||||||
Pool = new(6, [
|
|
||||||
new MatrixParallelWrap(0, 3),
|
|
||||||
new MatrixParallelWrap(3, 3)
|
|
||||||
]);
|
|
||||||
(LargeWrapCount, SmallWrapCount) = (0, 2);
|
|
||||||
} else
|
|
||||||
{
|
|
||||||
(int _3wrap, int _4wrap) = FastDecompose(count);
|
|
||||||
CapacityFixedStack<MatrixParallelWrap> stack = new(_4wrap + _3wrap);
|
|
||||||
int id = 0, idx = 0;
|
|
||||||
for (; idx < _3wrap; idx++, id += 3) {
|
|
||||||
stack.Push(new MatrixParallelWrap(idx, 3));
|
|
||||||
}
|
|
||||||
for (; idx < _3wrap + _4wrap; idx++, id += 4) {
|
|
||||||
stack.Push(new MatrixParallelWrap(id, 4));
|
|
||||||
}
|
|
||||||
Pool = new MatrixParallelPool(count, stack);
|
|
||||||
}
|
}
|
||||||
IsInitialized = true;
|
IsInitialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void LaunchParallel(IEnumerable<MatrixParallelWrap> wraps, SleepMode afterWork)
|
public static void LaunchParallel(IEnumerable<MatrixParallelWrap> wraps, SleepMode afterWork)
|
||||||
{
|
{
|
||||||
RemainThreadCount = _threads.Length;
|
RemainThreadCount = _threads.Count;
|
||||||
_AfterWork = (ThreadPriority)afterWork;
|
_AfterWork = (ThreadPriority)afterWork;
|
||||||
ResetThreadCount();
|
ResetThreadCount();
|
||||||
foreach (MatrixParallelWrap w in wraps) {
|
foreach (MatrixParallelWrap w in wraps) {
|
||||||
@@ -165,36 +136,25 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public static unsafe void MatrixParallelLeaderWork()
|
|
||||||
{
|
|
||||||
MatrixParallelWrap.ExecuteTaskPackage(LeaderTask);
|
|
||||||
_ = Interlocked.Decrement(ref RemainThreadCount);
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static void ResetThreadCount()
|
public static void ResetThreadCount()
|
||||||
{
|
{
|
||||||
RemainThreadCount = _threads.Length;
|
RemainThreadCount = _threads.Count;
|
||||||
}
|
}
|
||||||
|
|
||||||
#region wrap field
|
#region wrap field
|
||||||
|
|
||||||
private static MatrixParallelPool Pool;
|
public static CapacityFixedStack<MatrixParallelThread> LeadParallel()
|
||||||
private static int LargeWrapCount;
|
|
||||||
private static int SmallWrapCount;
|
|
||||||
|
|
||||||
public static MatrixParallelPool LeadParallel()
|
|
||||||
{
|
{
|
||||||
#if NET9_0_OR_GREATER
|
#if NET9_0_OR_GREATER
|
||||||
#else
|
#else
|
||||||
Monitor.Enter(_lock);
|
Monitor.Enter(_lock);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return Pool;
|
return _threads;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void ReleaseParallelLeadership(ref MatrixParallelPool pool)
|
public static void ReleaseParallelLeadership(ref CapacityFixedStack<MatrixParallelThread> pool)
|
||||||
{
|
{
|
||||||
#if NET9_0_OR_GREATER
|
#if NET9_0_OR_GREATER
|
||||||
#else
|
#else
|
||||||
|
|||||||
@@ -51,77 +51,89 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
}
|
}
|
||||||
|
|
||||||
[StructLayout(LayoutKind.Sequential)]
|
[StructLayout(LayoutKind.Sequential)]
|
||||||
internal readonly unsafe struct MatrixParallelTaskPackageNonGeneric
|
internal unsafe struct MatrixParallelTaskPackageNonGeneric
|
||||||
{
|
{
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static unsafe void Copy(
|
||||||
|
MatrixParallelTaskPackageNonGeneric* source,
|
||||||
|
MatrixParallelTaskPackageNonGeneric* target)
|
||||||
|
{
|
||||||
|
Buffer.MemoryCopy(
|
||||||
|
source,
|
||||||
|
target,
|
||||||
|
sizeof(MatrixParallelTaskPackageNonGeneric),
|
||||||
|
sizeof(MatrixParallelTaskPackageNonGeneric));
|
||||||
|
}
|
||||||
|
|
||||||
#region source
|
#region source
|
||||||
|
|
||||||
// Group 1: Left/Right matrix kernel base address
|
// Group 1: Left/Right matrix kernel base address
|
||||||
public readonly IntPtr Left; // 8
|
public IntPtr Left; // 8
|
||||||
public readonly IntPtr Right; // 8
|
public IntPtr Right; // 8
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region ans
|
#region ans
|
||||||
|
|
||||||
// Group 2: Result kernel base address + reserved
|
// Group 2: Result kernel base address + reserved
|
||||||
public readonly IntPtr Result; // 8
|
public IntPtr Result; // 8
|
||||||
public readonly int ElementSize;
|
public int ElementSize;
|
||||||
public readonly int ReservedPtr0; // 4 (extension/reserved)
|
public int ReservedPtr0; // 4 (extension/reserved)
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region left enum
|
#region left enum
|
||||||
|
|
||||||
public readonly int LeftPrimOffset; // 4
|
public int LeftPrimOffset; // 4
|
||||||
public readonly int LeftPrimStride; // 4
|
public int LeftPrimStride; // 4
|
||||||
public readonly int LeftSecOffset; // 4
|
public int LeftSecOffset; // 4
|
||||||
public readonly int LeftSecStride; // 4
|
public int LeftSecStride; // 4
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region right enum
|
#region right enum
|
||||||
|
|
||||||
public readonly int RightPrimOffset; // 4
|
public int RightPrimOffset; // 4
|
||||||
public readonly int RightPrimStride; // 4
|
public int RightPrimStride; // 4
|
||||||
public readonly int RightSecOffset; // 4
|
public int RightSecOffset; // 4
|
||||||
public readonly int RightSecStride; // 4
|
public int RightSecStride; // 4
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region ans enum
|
#region ans enum
|
||||||
|
|
||||||
public readonly int ResultPrimOffset; // 4
|
public int ResultPrimOffset; // 4
|
||||||
public readonly int ResultPrimStride; // 4
|
public int ResultPrimStride; // 4
|
||||||
public readonly int ResultSecOffset; // 4
|
public int ResultSecOffset; // 4
|
||||||
public readonly int ResultSecStride; // 4
|
public int ResultSecStride; // 4
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region scalar/compute
|
#region scalar/compute
|
||||||
|
|
||||||
public readonly ScalarPacket* Scalar; // 8
|
public ScalarPacket* Scalar; // 8
|
||||||
public readonly IntPtr ComputeHandle; // 8
|
public IntPtr ComputeHandle; // 8
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region global profile
|
#region global profile
|
||||||
|
|
||||||
public readonly int PrimCount; // 4
|
public int PrimCount; // 4
|
||||||
public readonly int ComputeMode; // 4
|
public int ComputeMode; // 4
|
||||||
public readonly int SecCount; // 4
|
public int SecCount; // 4
|
||||||
private readonly int ReservedInt0; // 4
|
private int ReservedInt0; // 4
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region count offset
|
#region count offset
|
||||||
|
|
||||||
public readonly int PrimTileOffset; // 4
|
public int PrimTileOffset; // 4
|
||||||
public readonly int SecTileOffset; // 4
|
public int SecTileOffset; // 4
|
||||||
public readonly int PrimCountInTile; // 4
|
public int PrimCountInTile; // 4
|
||||||
public readonly int SecCountInTile; // 4
|
public int SecCountInTile; // 4
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
[StructLayout(LayoutKind.Sequential)]
|
[StructLayout(LayoutKind.Sequential)]
|
||||||
|
|||||||
@@ -0,0 +1,167 @@
|
|||||||
|
//-----------------------------------------------------------------------
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
// file="MatrixParallelThread"
|
||||||
|
// Project: StgSharp
|
||||||
|
// AuthorGroup: Nitload
|
||||||
|
// Copyright (c) Nitload. All rights reserved.
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
//
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace StgSharp.Mathematics.Numeric
|
||||||
|
{
|
||||||
|
public class MatrixParallelThread : IDisposable
|
||||||
|
{
|
||||||
|
|
||||||
|
private bool disposedValue;
|
||||||
|
private ManualResetEventSlim _resetEvent;
|
||||||
|
private Thread _managedThread;
|
||||||
|
|
||||||
|
public MatrixParallelThread()
|
||||||
|
{
|
||||||
|
_managedThread = new Thread(MatrixParallelWorker);
|
||||||
|
_resetEvent = new ManualResetEventSlim(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ~MatrixComputeThread()
|
||||||
|
// {
|
||||||
|
// Dispose(disposing: false);
|
||||||
|
// }
|
||||||
|
|
||||||
|
public ThreadPriority AfterWork
|
||||||
|
{
|
||||||
|
get => field;
|
||||||
|
set => field = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
Dispose(disposing:true);
|
||||||
|
GC.SuppressFinalize(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static unsafe void ExecuteTaskPackage(Queue<IntPtr> taskQueue)
|
||||||
|
{
|
||||||
|
while (taskQueue.TryDequeue(out nint handle))
|
||||||
|
{
|
||||||
|
MatrixParallelTaskPackageNonGeneric* p = (MatrixParallelTaskPackageNonGeneric*)handle;
|
||||||
|
switch (p->ComputeMode)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
MatrixComputeModel.Compute_Unary(p);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
MatrixComputeModel.Compute_UnaryScalar(p);
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
MatrixComputeModel.Compute_Binary(p);
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
MatrixComputeModel.Compute_BinaryScalar(p);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Restart()
|
||||||
|
{
|
||||||
|
_resetEvent.Set();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual void Dispose(bool disposing)
|
||||||
|
{
|
||||||
|
if (!disposedValue)
|
||||||
|
{
|
||||||
|
if (disposing)
|
||||||
|
{
|
||||||
|
_resetEvent.Dispose();
|
||||||
|
_managedThread.Join();
|
||||||
|
}
|
||||||
|
|
||||||
|
disposedValue = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private unsafe void MatrixParallelWorker()
|
||||||
|
{
|
||||||
|
// Thread self = Thread.CurrentThread;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
_resetEvent.Wait();
|
||||||
|
_resetEvent.Reset();
|
||||||
|
_managedThread.Priority = ThreadPriority.Highest;
|
||||||
|
switch (Role)
|
||||||
|
{
|
||||||
|
case 0://worker
|
||||||
|
// _ = Interlocked.Decrement(ref RemainThreadCount);
|
||||||
|
ExecuteTaskPackage(Tasks);
|
||||||
|
break;
|
||||||
|
case 1://scheduler
|
||||||
|
BindedWrap.PublishTaskToWorkers(Tasks);
|
||||||
|
BindedWrap.SetAllWorker();
|
||||||
|
ExecuteTaskPackage(Tasks);
|
||||||
|
|
||||||
|
break;
|
||||||
|
case 2://leader, error because leader should be at outer thread but not here
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
_managedThread.Priority = AfterWork;
|
||||||
|
} while (true);
|
||||||
|
}
|
||||||
|
|
||||||
|
#region wrap belong
|
||||||
|
|
||||||
|
internal MatrixParallelWrap BindedWrap { get; set; } = null!;
|
||||||
|
|
||||||
|
internal Queue<IntPtr> Tasks { get; } = [];
|
||||||
|
|
||||||
|
internal int Role
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if ((Role & (~3u)) == 0)
|
||||||
|
{
|
||||||
|
field = value;
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
#if DEBUG
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(Role));
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -39,44 +39,13 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
public unsafe class MatrixParallelWrap
|
public unsafe class MatrixParallelWrap
|
||||||
{
|
{
|
||||||
|
|
||||||
private readonly Handle[] _resetHandle;
|
private readonly MatrixParallelThread[] _threads;
|
||||||
private readonly Thread[] _threads;
|
|
||||||
|
|
||||||
public MatrixParallelWrap(int id, int wrapSize)
|
public MatrixParallelWrap()
|
||||||
{
|
{
|
||||||
ID = id;
|
_threads = new MatrixParallelThread[5];
|
||||||
_threads = new Thread[wrapSize];
|
|
||||||
_resetHandle = new Handle[wrapSize];
|
|
||||||
for (int i = 0; i < wrapSize; i++)
|
|
||||||
{
|
|
||||||
_threads[i] = new Thread(MatrixParallelWorker)
|
|
||||||
{
|
|
||||||
IsBackground = true,
|
|
||||||
Priority = ThreadPriority.BelowNormal,
|
|
||||||
Name = $"MPT#{ID}_{i}"
|
|
||||||
};
|
|
||||||
Handle handle = new Handle(i, this);
|
|
||||||
_resetHandle[i] = handle;
|
|
||||||
_threads[i].Start(handle);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IsAllAvailable
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
foreach (Handle item in _resetHandle)
|
|
||||||
{
|
|
||||||
if (item.Role != 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public int ID { get; init; }
|
|
||||||
|
|
||||||
public static SleepMode AfterWork
|
public static SleepMode AfterWork
|
||||||
{
|
{
|
||||||
get;
|
get;
|
||||||
@@ -87,27 +56,39 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
|
|
||||||
internal ManualResetEventSlim SchedulerReset { get; } = new(false);
|
internal ManualResetEventSlim SchedulerReset { get; } = new(false);
|
||||||
|
|
||||||
|
private int Count { get; set; }
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void SetAllWorker()
|
public void SetAllWorker()
|
||||||
{
|
{
|
||||||
foreach (Handle item in _resetHandle) {
|
foreach (MatrixParallelThread t in _threads) {
|
||||||
item.ResetEvent.Set();
|
t.Restart();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal bool TryGetLeaderHandle(out Handle? handle)
|
internal void PublishTaskToWorkers(Queue<IntPtr> tasks)
|
||||||
{
|
{
|
||||||
foreach (Handle item in _resetHandle)
|
while (tasks.TryDequeue(out nint handle))
|
||||||
{
|
{
|
||||||
if (item.Role != 2)
|
MatrixParallelTaskPackageNonGeneric* task = (MatrixParallelTaskPackageNonGeneric*)handle;
|
||||||
{
|
PublishTaskToWorkers(task);
|
||||||
handle = item;
|
}
|
||||||
item.Role = 2;
|
}
|
||||||
return true;
|
|
||||||
}
|
internal void PublishTaskToWorkers(MatrixParallelTaskPackageNonGeneric* taskPackage)
|
||||||
|
{
|
||||||
|
int primCount = taskPackage->PrimCount;
|
||||||
|
int secCount = taskPackage->SecCount;
|
||||||
|
|
||||||
|
int primAverage = primCount / Count;
|
||||||
|
int secAverage = secCount / Count;
|
||||||
|
bool shouldSliceSec = secAverage > 512;
|
||||||
|
for (int i = 0; i < Count; i++)
|
||||||
|
{
|
||||||
|
Queue<IntPtr> tasks = _threads[i].Tasks;
|
||||||
|
MatrixParallelTaskPackageNonGeneric* subTask = MatrixParallelFactory.FromExistPackage(taskPackage);
|
||||||
|
subTask->PrimTileOffset = i * primAverage;
|
||||||
}
|
}
|
||||||
handle = null;
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Handle(int inWrapIndex, MatrixParallelWrap wrap)
|
public class Handle(int inWrapIndex, MatrixParallelWrap wrap)
|
||||||
@@ -141,71 +122,6 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
get => _threads.Length;
|
get => _threads.Length;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
|
|
||||||
#region ThreadOperation
|
|
||||||
|
|
||||||
private static unsafe void MatrixParallelWorker([AllowNull] object obj)
|
|
||||||
{
|
|
||||||
Handle handle = (obj as Handle)!;
|
|
||||||
MatrixParallelWrap wrap = handle.Wrap;
|
|
||||||
int inWrapIndex = handle.InWrapIndex;
|
|
||||||
Thread self = Thread.CurrentThread;
|
|
||||||
ManualResetEventSlim reset = handle.ResetEvent;
|
|
||||||
do
|
|
||||||
{
|
|
||||||
reset.Wait();
|
|
||||||
reset.Reset();
|
|
||||||
Queue<IntPtr> taskQueue = handle.TaskCache;
|
|
||||||
self.Priority = ThreadPriority.Highest;
|
|
||||||
switch (handle.Role)
|
|
||||||
{
|
|
||||||
case 0://worker
|
|
||||||
// _ = Interlocked.Decrement(ref RemainThreadCount);
|
|
||||||
ExecuteTaskPackage(handle.TaskCache);
|
|
||||||
break;
|
|
||||||
case 1://scheduler
|
|
||||||
// TODO task publication to worker in wrap
|
|
||||||
wrap.SetAllWorker();
|
|
||||||
ExecuteTaskPackage(taskQueue);
|
|
||||||
|
|
||||||
break;
|
|
||||||
case 2://leader, error because leader should be at outer thread but not here
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
self.Priority = (ThreadPriority)AfterWork;
|
|
||||||
} while (true);
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public static unsafe void ExecuteTaskPackage(Queue<IntPtr> taskQueue)
|
|
||||||
{
|
|
||||||
while (taskQueue.TryDequeue(out nint handle))
|
|
||||||
{
|
|
||||||
MatrixParallelTaskPackageNonGeneric* p = (MatrixParallelTaskPackageNonGeneric*)handle;
|
|
||||||
switch (p->ComputeMode)
|
|
||||||
{
|
|
||||||
case 1:
|
|
||||||
MatrixComputeModel.Compute_Unary(p);
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
MatrixComputeModel.Compute_UnaryScalar(p);
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
MatrixComputeModel.Compute_Binary(p);
|
|
||||||
break;
|
|
||||||
case 4:
|
|
||||||
MatrixComputeModel.Compute_BinaryScalar(p);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -217,18 +133,4 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
DeepSleep = ThreadPriority.Lowest,
|
DeepSleep = ThreadPriority.Lowest,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class MatrixComputeResource
|
|
||||||
{
|
|
||||||
|
|
||||||
public IEnumerable<MatrixParallelWrap> Wraps { get; set; }
|
|
||||||
|
|
||||||
public int WorkingThreadCount { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Closure task leader should do to collect results.
|
|
||||||
/// </summary>
|
|
||||||
public unsafe IntPtr LeaderClosurePackage { get; set; }
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,30 +1,27 @@
|
|||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// file="MatrixParallel.TaskFactory.cs"
|
// file="MatrixTaskFactory"
|
||||||
// Project: StgSharp
|
// Project: StgSharp
|
||||||
// AuthorGroup: Nitload Space
|
// AuthorGroup: Nitload
|
||||||
// Copyright (c) Nitload Space. All rights reserved.
|
// Copyright (c) Nitload. All rights reserved.
|
||||||
//
|
//
|
||||||
// Permission is hereby granted, free of charge, to any person
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
// obtaining a copy of this software and associated documentation
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
// files (the “Software”), to deal in the Software without restriction,
|
// in the Software without restriction, including without limitation the rights
|
||||||
// including without limitation the rights to use, copy, modify, merge,
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
// publish, distribute, sublicense, and/or sell copies of the Software,
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
// and to permit persons to whom the Software is furnished to do so,
|
// furnished to do so, subject to the following conditions:
|
||||||
// subject to the following conditions:
|
|
||||||
//
|
//
|
||||||
// The above copyright notice and
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
// this permission notice shall be included in all copies
|
// copies or substantial portions of the Software.
|
||||||
// or substantial portions of the Software.
|
|
||||||
//
|
//
|
||||||
// THE SOFTWARE IS PROVIDED “AS IS”,
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH
|
// SOFTWARE.
|
||||||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
//
|
//
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
@@ -85,6 +82,15 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static unsafe MatrixParallelTaskPackageNonGeneric* FromExistPackage(
|
||||||
|
MatrixParallelTaskPackageNonGeneric* exist)
|
||||||
|
{
|
||||||
|
MatrixParallelTaskPackageNonGeneric* ptr = (MatrixParallelTaskPackageNonGeneric*)_wrapAllocator.Allocate();
|
||||||
|
MatrixParallelTaskPackageNonGeneric.Copy(exist, ptr);
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
|
||||||
public static void Release(ScalarPacket* source)
|
public static void Release(ScalarPacket* source)
|
||||||
|
|||||||
Reference in New Issue
Block a user