Files
stgsharp/StgSharpDebug/Program.cs
T

218 lines
7.7 KiB
C#

//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Program"
// 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 StgSharp.Collections;
using StgSharp.Controlling.UsrActivity;
using StgSharp.Geometries;
using StgSharp.Graphics;
using StgSharp.HighPerformance;
using StgSharp.HighPerformance.Memory;
using StgSharp.Mathematics;
using StgSharp.Mathematics.Numeric;
using StgSharp.Model.Step;
using StgSharp.MVVM;
using StgSharp.MVVM.ViewModel;
using StgSharp.Script;
using StgSharp.Script.Express;
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
using System.Threading;
using System.Xml.XPath;
namespace StgSharp
{
internal static partial class Program
{
private static string testExpPath = @"D:\\Programmer\\stepcode\\data\\ap203\\ap203.exp";
internal static unsafe void Main(string[] args)
{
try
{
World.Init();
// New test with synchronized start:
// - 8 producer threads, each pushing 1000 unique integers
// - 8 consumer threads, each attempting to pop 1000 times
// - Consumers print one line per successful pop
// - Finally, print popped/remaining counts and values
const int producerThreads = 8;
const int consumerThreads = 8;
const int perProducer = 1000;
const int perConsumerAttempts = 1000;
ConcurrentBufferStack<int> stack = new ConcurrentBufferStack<int>();
Thread[] producers = new Thread[producerThreads];
Thread[] consumers = new Thread[consumerThreads];
ConcurrentBag<int> popped = new ConcurrentBag<int>();
int totalThreads = producerThreads + consumerThreads;
using CountdownEvent ready = new CountdownEvent(totalThreads);
using ManualResetEventSlim startGate = new ManualResetEventSlim(false);
for (int c = 0; c < consumerThreads; c++)
{
consumers[c] = new Thread(() => {
ready.Signal();
startGate.Wait();
for (int k = 0; k < perConsumerAttempts; k++)
{
if (stack.TryPop(out int v))
{
popped.Add(v);
Console.WriteLine($"pop: {v}");
} else
{
Thread.Yield();
}
}
})
{
IsBackground = true
};
}
for (int p = 0; p < producerThreads; p++)
{
int captured = p;
producers[p] = new Thread(() => {
ready.Signal();
startGate.Wait();
int begin = captured * perProducer;
for (int j = 0; j < perProducer; j++)
{
stack.Push(begin + j);
if ((j & 0xFF) == 0) {
Thread.Yield();
}
}
})
{
IsBackground = true
};
}
foreach (Thread t in consumers) {
t.Start();
}
foreach (Thread t in producers) {
t.Start();
}
// Ensure all threads are ready, then start simultaneously
ready.Wait();
startGate.Set();
foreach (Thread t in producers) {
t.Join();
}
foreach (Thread t in consumers) {
t.Join();
}
// Gather remaining data
List<int> remaining = new System.Collections.Generic.List<int>();
while (stack.TryPop(out int value)) {
remaining.Add(value);
}
Console.WriteLine($"Popped count: {popped.Count}");
Console.WriteLine("Popped values:");
foreach (int v in popped)
{
Console.Write(v);
Console.Write('\t');
}
Console.WriteLine();
Console.WriteLine($"Remaining count: {remaining.Count}");
Console.WriteLine("Remaining values:");
foreach (int v in remaining)
{
Console.Write(v);
Console.Write('\t');
}
Console.WriteLine();
// Sanity check
Console.WriteLine($"Produced: {producerThreads * perProducer}, Popped+Remaining: {popped.Count + remaining.Count}");
/*
// TestMatrixParallel();
// 原有的矩阵测试代码
HybridLayerSegregatedFitAllocator hlsf = new HybridLayerSegregatedFitAllocator(64 * 400 * 400);
Matrix<float> m = Matrix<float>.FromHlsf(400, 200, hlsf);
int i = 0, ii = 0;
foreach (MatrixSegment<float> lines in m.GetRowEnumeration())
{
i = 0;
foreach (MatrixKernel<float>* item in lines)
{
i++;
Console.WriteLine(i);
}
ii++;
Console.WriteLine($"============================{ii}");
}
/**/
}
catch (Exception ex)
{
Console.WriteLine(ex);
/*
World.WriteLog($"{ex}", LogType.Error);
if( true ) { }
Dialogue.PostError( ex );
/**/
World.Terminate(-1);
/**/
}
finally
{
World.Terminate(0);
}
/**/
}
}
}