Question: I have a program that calculates the Fibonacci number. When I set a large number of passes, it runs extremely slow because it only works with 1 CPU core, I want to know how to make it consume 100% of the CPU?
I already know about Parallel.For
, but this approach does not allow me to save the result of previous numbers, at least I tried, and absolutely nothing worked for me! I use this code to calculate:
static public class Operations
{
static public BigInteger calculateFib(int iterations)
{
BigInteger a = 0;
BigInteger b = 1;
for (int i = 0; i < iterations; i++)
{
BigInteger temp = a;
a = b;
b = temp + b;
}
return a;
}
}
I want to get the number of user cores and distribute the load between them, please help.
Please see the comments on the question.
Given the Fibonacci sequence algorithm you want to use, the utilization of additional threads or CPI cores cannot improve its performance in principle. This algorithm cannot be parallelized.
Let me explain some background. There is no such thing as CPU thread. Threads are created dynamically in code, directly by the application developer or not. An example of indirect thread management is TPL, it implements parallelism based on Tasks and Parallel.
When a thread is scheduled for execution, the system assigns one of the CPU cores to execute a slice of a thread code. This assignment can change with time, it depends on the system scheduler algorithm and available resources. Even for single-code CPUs, threading is extremely important. Nevertheless, the application developer can indirectly affect this core assignment using processor affinity or limiting maximum degree of parallelism.
But nothing can organize even the partially parallel execution of an algorithm that is not parallel by its nature, like the sequential calculation of the Fibonacci sequence. I think just the term Fibonacci sequence should suggest it. The next member of the Fibonacci sequence is calculated using a recurrent relation to two previous members. The notions of parallel and sequential are opposite.
Now, I'm not saying that using threads for this algorithm is impossible. It is possible but would create a huge threading misuse. Threading and parallelism are not directly related. You can create and use threads, but you can use them sequentially. A typical effect of such a wrong design is a correct calculation with considerable performance losses. Why? In the case of Fibonacci, you can use two or more threads waiting for each other for the data required for the next recurring step. Only one thread at a time can be not in a wait state. It would be equivalent to single-threaded computing, but it is actually much worse, because of the waste of resources required for the support of the thread operation, thread synchronization, and inter-thread communication.
As a matter of fact, even the performance of some parallel algorithms can be compromised by using Tasks
or Parallel
. It can happen when the volume of calculations and the level of parallelism don't justify the resource costs of supporting parallelism mechanisms. In particular, it can happen when the number of available CPU cores is insufficient to shift the balance in favor of the parallel execution. In this case, even a potentially parallel algorithm can show better performance in a single-threaded implementation.