This project demonstrates the use of memoization, dynamic programming, and multi-threading to efficiently handle repeated heavy tasks and optimize performance. The results of computations are cached for quick retrieval, and parallel processing accelerates computation.
- Memoization: Stores results of expensive function calls and returns the cached result when the same inputs occur again.
- Dynamic Programming: Breaks down complex problems into simpler subproblems and solves them just once, storing their solutions.
- Multi-threading: Uses multiple threads for the computation, improving execution speed.
- C++ Standard: The code is compatible with both C++17 and C++20.
- Compiler: The project can be compiled using MinGW G++ compiler or Microsoft Visual Studio MSVC compiler.
- Libraries:
<iostream>
<unordered_map>
<exception>
<chrono>
<future>
<vector>
<mutex>
<condition_variable>
<cmath>
<stdexcept>
<cassert>
- Ensure MinGW is installed on your system.
- Open the command prompt and navigate to the project directory.
- Compile the code using the following command:
g++ -std=c++20 -o fib_multithread main.cpp -pthread
- Open Microsoft Visual Studio.
- Create a new C++ project and add the
main.cpp
file to the project. - Configure the project to use C++17 or C++20.
- Build and run the project using the built-in compiler.
Run the compiled executable. The program calculates the Fibonacci number for given inputs using memoization and multi-threading.
The total time duration to calculate the Fib of 8 is: 70 microseconds The total time duration to calculate the Fib of 21 is: 13 microseconds The number of repeated processes to find Fib of 8 is: 0 (memoized) Fibonacci(8) = 21
- : For input and output operations.
- <unordered_map>: For storing the memoization cache.
- : For handling exceptions.
- : For measuring execution time.
- : For handling asynchronous operations.
- : For storing futures.
- : For thread synchronization.
- <condition_variable>: For thread synchronization.
- : For mathematical operations.
- : For throwing runtime errors.
- : For assertions.
void fibLoop(...)
: Calculates Fibonacci numbers in chunks using multiple threads.int fib(int target, const int CPUPool, std::unordered_map<int, int>& memo)
: Calculates Fibonacci number with memoization and multi-threading.int main()
: Entry point of the program. Initializes the memoization cache, sets up the thread pool, and handles exceptions.
- Mutex: Used to synchronize access to shared data (memoization cache).
- Condition Variable: Used to synchronize the completion of tasks.
This project is licensed under the NUI License.
Feel free to fork this repository and contribute by submitting pull requests. For major changes, please open an issue first to discuss what you would like to change.
For any questions or suggestions, please contact Masoud Farsi at farsi.masoud@gmail.com