add scheduler method; fixing buffer parallel errors
This commit is contained in:
+4
-116
@@ -62,133 +62,20 @@ namespace StgSharp
|
||||
try
|
||||
{
|
||||
World.Initialize().UseModule<NumericalModule>().UseModule<GraphicModule>().InitializeModule();
|
||||
TestRender();
|
||||
/*
|
||||
// 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;
|
||||
// TestRender();
|
||||
|
||||
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 + 10 * 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 = [];
|
||||
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}");
|
||||
|
||||
/*
|
||||
|
||||
// 原有的矩阵测试代码
|
||||
HybridLayerSegregatedFitAllocator hlsf = new HybridLayerSegregatedFitAllocator(64 * 400 * 400);
|
||||
|
||||
Matrix<float> m = Matrix<float>.FromHlsf(20, 20, hlsf);
|
||||
Matrix<float> m = Matrix<float>.FromHlsf(200, 200, hlsf);
|
||||
m.Fill(1.0f);
|
||||
Matrix<float> n = m.Add(m);
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
for (int j = 0; j < 20; j++) {
|
||||
Console.Write($"{n[i, j].ToString("0.000")}\t");
|
||||
Console.Write($"{n[i, j]:0.0E+00} \t");
|
||||
}
|
||||
Console.WriteLine();
|
||||
}
|
||||
@@ -198,6 +85,7 @@ namespace StgSharp
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex);
|
||||
Console.ReadLine();
|
||||
/*
|
||||
World.WriteLog($"{ex}", LogType.Error);
|
||||
if( true ) { }
|
||||
|
||||
Reference in New Issue
Block a user