Git Product home page Git Product logo

bardiparsi / memoizationmultithreading Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 504 KB

Sample project demonstrating the use of memoization, dynamic programming, and multi-threading to efficiently handle repeated heavy tasks and optimize performance. Results are cached for quick retrieval, and Multi-Threaded processing accelerates computation.

License: GNU General Public License v3.0

C++ 100.00%
algorithms cpp cpp17 cpp20 data-structures dynamic-programming machine-learning machine-learning-algorithms memoization memory-management multi-threading multiprocessing multithreading parallel-computing parallel-programming

memoizationmultithreading's Introduction

Multi-threaded Fibonacci Calculation with Memoization

Overview

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.

Features

  • 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.

Requirements

  • 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>

Installation

Using MinGW G++

  1. Ensure MinGW is installed on your system.
  2. Open the command prompt and navigate to the project directory.
  3. Compile the code using the following command:
    g++ -std=c++20 -o fib_multithread main.cpp -pthread

Using Microsoft Visual Studio

  1. Open Microsoft Visual Studio.
  2. Create a new C++ project and add the main.cpp file to the project.
  3. Configure the project to use C++17 or C++20.
  4. Build and run the project using the built-in compiler.

Usage

Run the compiled executable. The program calculates the Fibonacci number for given inputs using memoization and multi-threading.

Sample Output

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

Code Explanation

Header Files

  • : 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.

Functions

  • 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.

Multi-threading

  • Mutex: Used to synchronize access to shared data (memoization cache).
  • Condition Variable: Used to synchronize the completion of tasks.

License

This project is licensed under the NUI License.

Contributing

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.

Contact

For any questions or suggestions, please contact Masoud Farsi at [email protected]

memoizationmultithreading's People

Contributors

bardiparsi avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.