Hello Mat

 找回密码
 立即注册
查看: 1923|回复: 0

进程绑定到cpu逻辑内核上

[复制链接]

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
发表于 2022-12-14 16:53:02 来自手机 | 显示全部楼层 |阅读模式
将线程绑定在某个具体的cpu逻辑内核上运行,[url]https://blog.csdn.net/fm0517/article/details/125470768[/url]
  1. using System.Runtime.InteropServices;
  2. using System.Diagnostics;
  3. using System.Management;

  4. namespace BandThreadToCPU
  5. {
  6.     class Program
  7.     {
  8.         //SetThreadAffinityMask: Set hThread run on logical processer(LP:) dwThreadAffinityMask
  9.         [DllImport("kernel32.dll")]
  10.         static extern UIntPtr SetThreadAffinityMask(IntPtr hThread, UIntPtr dwThreadAffinityMask);

  11.         //Get the handler of current thread
  12.         [DllImport("kernel32.dll")]
  13.         static extern IntPtr GetCurrentThread();

  14.         //The real function
  15.         public static void ChangeValue(object lpIdx)
  16.         {
  17.             //Bind current thread to a specific logical processer
  18.             ulong LpId = SetCpuID((int)lpIdx);
  19.             SetThreadAffinityMask(GetCurrentThread(), new UIntPtr(LpId));

  20.             // Let program run for a while
  21.             Stopwatch stopwatch = new Stopwatch();
  22.             stopwatch.Start();
  23.             for (int i = 0; i < 1000000; i++)
  24.             {
  25.                 for (int j = 0; j < 1000000; j++)
  26.                 {
  27.                     int _data = j;
  28.                 }
  29.             }
  30.             stopwatch.Stop();
  31.             Console.WriteLine("Running Time: " + stopwatch.ElapsedMilliseconds.ToString());
  32.         }

  33.         //Get CPU id. index:0 -> id:1, 1->2, 2->4, 3->8, 4->16, ...
  34.         static ulong SetCpuID(int lpIdx)
  35.         {
  36.             ulong cpuLogicalProcessorId = 0;
  37.             if (lpIdx < 0 || lpIdx >= System.Environment.ProcessorCount)
  38.             {
  39.                 lpIdx = 0;
  40.             }
  41.             cpuLogicalProcessorId |= 1UL << lpIdx;
  42.             return cpuLogicalProcessorId;
  43.         }

  44.         static void Main(string[] args)
  45.         {
  46.             // Specify the cpu logical processor index you want. CPU lp index start from 0
  47.             int LOGICAL_PROCESSOR_IDX = 4;

  48.             // Get the ManagementClass object
  49.             ManagementClass mc = new ManagementClass(new ManagementPath("Win32_Processor"));
  50.             // Get the properties in the class
  51.             ManagementObjectCollection moc = mc.GetInstances();

  52.             foreach (ManagementObject mo in moc)
  53.             {
  54.                 PropertyDataCollection properties = mo.Properties;
  55.                 // Print CPU properties
  56.                 foreach (PropertyData property in properties)
  57.                 {
  58.                     Console.WriteLine( property.Name + ": " + property.Value);
  59.                 }
  60.             }

  61.             Stopwatch stopwatch = new Stopwatch();
  62.             Thread thread = new Thread(new ParameterizedThreadStart(ChangeValue));
  63.             stopwatch.Start();
  64.             thread.Start(LOGICAL_PROCESSOR_IDX);
  65.             thread.Join();
  66.             stopwatch.Stop();
  67.             Console.WriteLine("Total running time: " + stopwatch.ElapsedMilliseconds.ToString());
  68.             Console.ReadKey();
  69.         }
  70.     }
  71. }
复制代码



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-4-26 13:28 , Processed in 0.233074 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表