Bonjour
Comment peut-on lancer un programme codé en C++ plusieurs fois sur un serveur en choisissant chaque fois un processeur du serveur ? (Parallélisme)
Merci par avance
Cordialement
Bonjour
Comment peut-on lancer un programme codé en C++ plusieurs fois sur un serveur en choisissant chaque fois un processeur du serveur ? (Parallélisme)
Merci par avance
Cordialement
Salut,
Biensur mais ce n'est pas si simple. tu n'a pas de méthode standard, mais OS spécific.
Regarde du coté de
SetThreadAffinityMask pour windows
pthread_setaffinity_np pour linux
pour les autres il faut regarder la documentation![]()
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113 class Program { [DllImport("ntdll.dll", CharSet = CharSet.Auto)] public static extern uint NtGetCurrentProcessorNumber(); static void Main(string[] args) { //Set affinity to first CPU //UIntPtr currentaffinity = ProcessorAffinity.SetThreadAffinity(0); //Console.WriteLine("Running on CPU #{0}", NtGetCurrentProcessorNumber()); ////Restore last affinity //ProcessorAffinity.SetThreadAffinityMask(currentaffinity); //Cycle through all logical CPUs for (int cpuid = 0; cpuid < Environment.ProcessorCount; cpuid++) { using (ProcessorAffinity.BeginAffinity(cpuid)) { Console.WriteLine("Running on CPU #{0}", NtGetCurrentProcessorNumber()); } } Console.ReadLine(); } } public static class ProcessorAffinity { static class Win32Native { //GetCurrentThread() returns only a pseudo handle. No need for a SafeHandle here. [DllImport("kernel32.dll")] public static extern IntPtr GetCurrentThread(); [HostProtectionAttribute(SelfAffectingThreading = true)] [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern UIntPtr SetThreadAffinityMask(IntPtr handle, UIntPtr mask); } public struct ProcessorAffinityHelper : IDisposable { UIntPtr lastaffinity; internal ProcessorAffinityHelper(UIntPtr lastaffinity) { this.lastaffinity = lastaffinity; } #region IDisposable Members public void Dispose() { if (lastaffinity != UIntPtr.Zero) { Win32Native.SetThreadAffinityMask(Win32Native.GetCurrentThread(), lastaffinity); lastaffinity = UIntPtr.Zero; } } #endregion } static ulong maskfromids(params int[] ids) { ulong mask = 0; foreach (int id in ids) { if (id < 0 || id >= Environment.ProcessorCount) throw new ArgumentOutOfRangeException("CPUId", id.ToString()); mask |= 1UL << id; } return mask; } /// <summary> /// Sets a processor affinity mask for the current thread. /// </summary> /// <param name="mask">A thread affinity mask where each bit set to 1 specifies a logical processor on which this thread is allowed to run. /// <remarks>Note: a thread cannot specify a broader set of CPUs than those specified in the process affinity mask.</remarks> /// </param> /// <returns>The previous affinity mask for the current thread.</returns> public static UIntPtr SetThreadAffinityMask(UIntPtr mask) { UIntPtr lastaffinity = Win32Native.SetThreadAffinityMask(Win32Native.GetCurrentThread(), mask); if (lastaffinity == UIntPtr.Zero) throw new Win32Exception(Marshal.GetLastWin32Error()); return lastaffinity; } /// <summary> /// Sets the logical CPUs that the current thread is allowed to execute on. /// </summary> /// <param name="CPUIds">One or more logical processor identifier(s) the current thread is allowed to run on.<remarks>Note: numbering starts from 0.</remarks></param> /// <returns>The previous affinity mask for the current thread.</returns> public static UIntPtr SetThreadAffinity(params int[] CPUIds) { return SetThreadAffinityMask(((UIntPtr)maskfromids(CPUIds))); } /// <summary> /// Restrict a code block to run on the specified logical CPUs in conjuction with /// the <code>using</code> statement. /// </summary> /// <param name="CPUIds">One or more logical processor identifier(s) the current thread is allowed to run on.<remarks>Note: numbering starts from 0.</remarks></param> /// <returns>A helper structure that will reset the affinity when its Dispose() method is called at the end of the using block.</returns> public static ProcessorAffinityHelper BeginAffinity(params int[] CPUIds) { return new ProcessorAffinityHelper(SetThreadAffinityMask(((UIntPtr)maskfromids(CPUIds)))); } }
Partager