117 lines
5.1 KiB
C#
117 lines
5.1 KiB
C#
//-----------------------------------------------------------------------
|
|
// -----------------------------------------------------------------------
|
|
// file="MatrixParallelThreadTest"
|
|
// 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 StgSharp.Mathematics;
|
|
using System;
|
|
using System.Threading;
|
|
|
|
namespace StgSharp
|
|
{
|
|
/*
|
|
/// <summary>
|
|
/// 矩阵计算并发调度器线程调度测试 修复的问题: 1. 添加了ResetThreadCount()方法,在每轮任务调度前重置线程计数 2.
|
|
/// 调度器线程不再递减RemainThreadCount,只有工作线程才递减 3. 改进了控制台输出,显示更清晰的线程活动信息 使用方法:在Program.cs的Main方法中调用
|
|
/// MatrixParallelThreadTest.TestMatrixParallelThreadScheduling(); 或者通过命令行运行程序时调用此方法
|
|
/// </summary>
|
|
public class MatrixParallelThreadTest
|
|
{
|
|
|
|
/// <summary>
|
|
/// 测试矩阵并行计算线程调度
|
|
/// </summary>
|
|
public static void TestMatrixParallelThreadScheduling()
|
|
{
|
|
Console.WriteLine("=== 矩阵并行计算线程调度测试开始 ===");
|
|
Console.WriteLine($"CPU核心数: {Environment.ProcessorCount}");
|
|
Console.WriteLine($"支持并行计算: {MatrixParallel.SupportParallel}");
|
|
Console.WriteLine($"并行计算有意义: {MatrixParallel.IsParallelMeaningful}");
|
|
|
|
if (!MatrixParallel.SupportParallel)
|
|
{
|
|
Console.WriteLine("当前系统不支持并行计算,跳过测试");
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
// 初始化矩阵并行计算系统
|
|
Console.WriteLine("\n--- 初始化矩阵并行计算系统 ---");
|
|
MatrixParallel.Init();
|
|
Console.WriteLine("矩阵并行计算系统初始化完成");
|
|
|
|
// 启动并行计算
|
|
Console.WriteLine("\n--- 启动并行计算 ---");
|
|
MatrixParallel.LaunchParallel(SleepMode.QuickWakeup);
|
|
Console.WriteLine("并行计算已启动");
|
|
|
|
// 获取领导者句柄
|
|
MatrixParallel.MatrixParallelWrap.Handle leaderHandle = MatrixParallel.LeaderHandle;
|
|
Console.WriteLine($"领导者句柄获取成功: Wrap ID {leaderHandle.Wrap.ID}");
|
|
|
|
// 模拟一些任务调度
|
|
Console.WriteLine("\n--- 开始模拟任务调度 ---");
|
|
for (int i = 0; i < 3; i++)
|
|
{
|
|
Console.WriteLine($"\n第 {i + 1} 轮任务调度:");
|
|
|
|
// 开始并行计算
|
|
MatrixParallel.LeadParallel();
|
|
Console.WriteLine("已获取并行计算锁");
|
|
|
|
// 设置所有Wrap
|
|
MatrixParallel.SetAllWrap();
|
|
Console.WriteLine("已通知所有Wrap开始工作");
|
|
|
|
// 发布任务(当前为空实现)
|
|
// MatrixParallel.PublishTask();
|
|
Console.WriteLine("任务发布完成");
|
|
|
|
// 等待一段时间让线程执行
|
|
Thread.Sleep(100);
|
|
|
|
// 结束并行计算
|
|
MatrixParallel.EndParallel();
|
|
Console.WriteLine("已释放并行计算锁");
|
|
|
|
// 等待一段时间再进行下一轮
|
|
Thread.Sleep(200);
|
|
}
|
|
|
|
Console.WriteLine("\n--- 测试完成 ---");
|
|
Console.WriteLine("线程调度测试已成功完成,请观察上述输出中的线程活动信息");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"测试过程中发生异常: {ex.Message}");
|
|
Console.WriteLine($"异常详情: {ex}");
|
|
}
|
|
}
|
|
|
|
}
|
|
/**/
|
|
}
|