I know that setting thread priority is a bit of a taboo topic when stack overflowing, but I am convinced that my application is a good candidate for increasing priority. To justify this, I explained the context below. The question is HOW to do this effectively?
The application is a .NET 4 console application (C #), which performs a complex algorithm with a runtime of about five hours. The algorithm does not require intensive work with memory at all, but just an intensive processor. It crunches and does not perform any I / O, database connection, network connection, etc. The output of the application is just ONE number that it writes to the console at the end. In other words, the algorithm is completely autonomous and has no dependencies.
The application runs on its own dedicated 16-bit 64-bit machine running Windows Server with much more free RAM than required (8 GB). By special, I mean that the server was purchased to run this application EXCLUSIVELY.
I have already optimized the code as much as I could, with extensive profiling, fancy math shortcuts and bit tricks.
Here is the general structure in pseudocode:
public static void Main () { Process.GetCurrentProcess().PriorityBoostEnabled = true; Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime; // Of course this only affects the main thread rather than child threads. Thread.CurrentThread.Priority = ThreadPriority.Highest; BigInteger seed = SomeExtremelyLargeNumber; // Millions of digits. // The following loop takes [seed] and processes some numbers. result1 = Parallel.For(/* With thread-static variables. */); while (true) // Main loop that cannot be parallelized. { // Processes result1. result2 = Parallel.For(/* With thread-static variables. */); // Processes result2. result1 = Parallel.For(/* With thread-static variables. */); if (result1 == criteria) break; // Note: This loop does not need to sleep or care about system responsiveness. } }
Now, based on issues related to thread priority on SO, I understand that using ThreadPool should not be confused with priority. Therefore, if I need to switch to manual flows, so be it.
Question:
- How do I change the code above to manual threading to benefit from increasing the priority of the thread (without using the thread pool, etc.)?
- Does setting the highest priority on all child threads help? I mean, will the child threads fight each other or will this give them an edge over the external tasks of the OS?
- Given that there are 16 cores, should I run 16 or 15 threads? Is there a general rule for this?
- Will the tuning process be prioritized for real-time help?
source share