Dydaktyka
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Ćwiczenia 9, grupa śr. 10-12, 20 grudnia 2023 ###### tags: `PRW23` `ćwiczenia` `pwit` ## Deklaracje Gotowość rozwiązania zadania należy wyrazić poprzez postawienie X w odpowiedniej kolumnie! Jeśli pożądasz zreferować dane zadanie (co najwyżej jedno!) w trakcie dyskusji oznacz je znakiem ==X== na żółtym tle. **UWAGA: Tabelkę wolno edytować tylko wtedy, gdy jest na zielonym tle!** :::danger | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | | ----------------------:| --- | --- | --- | --- | --- | --- | --- | --- | -- | Mikołaj Balwicki | X | X | X | | | | | | X | Konrad Kowalczyk | X | X | X | X | X | X | | X | X | Jakub Krzyżowski | X | X | X | X | | | X | X | X | Łukasz Magnuszewski | | | | | | | | | | Marcin Majchrzyk | X | X | | | X | X | | | X | Jan Marek | X | X | X | X | X | X | | X | X | Marcin Mierzejewski | | | | | | | | | | Juan Jose Nieto Ruiz | | | | | | | | | | Konrad Oleksy | | | | | | | | | | Javier Rábago Montero | X | X | X | X | X | X | X | X | X | Michał Mękarski | X | X | X | X | X | X | | X | X | ::: :::info **Uwaga:** Po rozwiązaniu zadania należy zmienić kolor nagłówka na zielony. ::: ## Zadanie 1 :::success Autor: Javier Rábago Montero ::: ![z1](https://hackmd.io/_uploads/H12nG4lP6.png) The principle of locality of references refers to the tendency of a program to access a small, localized portion of its address space for a certain period of time. There are two main types of locality: 1. Temporal locality: if a particular memory location is accessed, it is likely to be accessed again in the near future. 2. Spatial Locality: if a particular memory location is accessed, nearby locations are also likely to be accessed. Caches use the principle of locality to improve performance. They store the data that is frequently accessed or recently used. When the CPU requests data, the cache is checked first, and if it found there the process will be much faster than having to get the data from main memory. When multiple processors share access to a common memory, there has to be cache coherence. If not there would be inconsistencies when multiple caches have copies of the same data. Without coherence changes made by one processor could not be immediately visible to others. MESI stands for: Modified: the cache line is present in the cache in a modified state, meaning that it has been altered compared to the main memory, so the changes have to be written back. Exclusive: the cache line is present in the cache in an unmodified state, and no other caches have copy of it. Shared: The cache line is unmodified and is shared wit other caches. Invalid: The cache line is invalid or not present in the cache. So for example, if a cache wants to write to a memory location, it must first ensure that no other caches have a copy in the Modified or Exclusive state. If they do the data is invalidated or updated to maintain consistency. ## Zadanie 2 :::success Autor: Marcin Majchrzyk ::: W jaki sposób mierzy się wydajność implementacji zamków? - Tworzy się program z krótką sekcją krytyczną (licznikiem), mierzy się czas wykonania zadania i porównuje w zależności od liczby wątków. ```java class TASlock { AtomicBoolean state = new AtomicBoolean(false); void lock() { while (state.getAndSet(true)) {} } void unlock() { state.set(false); } } class TTASlock { AtomicBoolean state = new AtomicBoolean(false); void lock() { while (true) { while (state.get()) {} if (!state.getAndSet(true)) return; } } } ``` Wyjaśnij, skąd bierze się różnica w wydajności zamka TAS vs. TTAS odwołując się do modelu komunikacji w systemach wieloprocesorowych ze spójnymi pamięciami podręcznymi i wspólną szyną danych. - Zamek TAS przez próbę zapisu za każdym razem unieważnia pamięć podręczną we wszystkich procesach, przez co musi być aktualizowana z pamięci współdzielonej. - Zamek TTAS tylko odczytuje stan, przez co wszystkie pamięci nie są unieważniane gdy nie ma możliwości zablokowania zamka, tylko wtedy gdy się to udaje. ## Zadanie 3 :::success Autor: Mikołaj Balwicki ::: Idea: Korzystamy z tablicy wartości boolowskiej opisującej stan pracy kolejnych wątków. Każdy wątek korzysta tylko z dwóch komórek pamięci, tej opisującej stan swojego poprzednika i tej opisującej jego stan. Wątek wiruje na komórce poprzednika tak długo jak nie zwolni on swojego zamka. Gdy wątek skończy swoją sekcję krytyczną, zwalnia on zamek w przypisanej do niego komórce. Kod: ``` public class ALock implements Lock { ThreadLocal<Integer> mySlotIndex = new ThreadLocal<Integer> (){ protected Integer initialValue() { return 0; } }; AtomicInteger tail; boolean[] flag; int size; public ALock(int capacity) { size = capacity; tail = new AtomicInteger(0); flag = new boolean[capacity]; flag[0] = true; } public void lock() { int slot = tail.getAndIncrement() % size; mySlotIndex.set(slot); while (! flag[slot]) {}; } public void unlock() { int slot = mySlotIndex.get(); flag[slot] = false; flag[(slot + 1) % size] = true; } } ``` ## Zadanie 4 :::success Autor: Jakub Krzyżowski ::: ![image](https://hackmd.io/_uploads/B1ly7UkPp.png) ![image](https://hackmd.io/_uploads/rkMe7I1DT.png) W pierwszej implementacji wszystkie wątki czekają aż licznik zabezpieczony TTASLock'iem osiągnie n. Wszystkie te wątki współdzielą ten jeden licznik, a zatem za każdym razem jak jakiś wątek zmieni jego wartość nastąpi inwalidacja linii w cache pozostałych wątków. Oznacza to, że szyna danych będzie otrzymywać bardzo dużo żądań. W drugiej implementacji mamy tablicę wartości boolowskich, gdzie każdy wątek i najpierw czeka aż komórka `b[i-1] == true` i ustawia `b[i] = true`, a następnie czeka aż `b[n-1] == true`. Tutaj występuje problem fałszywego współdzielenia, któremu można zaradzić paddingiem. Jeśli nie wykorzystamy paddingu otrzymamy niewielkie przyspieszenie względem pierwszego protokołu, bo inwalidacji nie będą zachodzić linie wszystkich wątków na raz, a jedynie paru. Jeśli natomiast zastosujemy padding to kosztem większego zużycia pamięci nie będziemy tracić czasu na dodatkowe operacje na szynie danych, a więc uzyskamy większe przyspieszenie. ## Zadanie 5 :::success Autor: Konrad Kowalczyk ::: ![zad5](https://hackmd.io/_uploads/HkOV7VgPa.png) Zasada działania zamka CLH jest podobna do zasady działania zamka kolejkowego Andersona, z tym że do kolejkowania zamiast jawnej tablicy komórek używa się wirtualnej listy wiązanej. ```java public class CLHLock implements Lock { AtomicReference<QNode> tail = new AtomicReference<QNode>(new QNode()); ThreadLocal<QNode> myPred; ThreadLocal<QNode> myNode; public CLHLock() { tail = new AtomicReference<QNode>(new QNode()); myNode = new ThreadLocal<QNode>() { protected QNode initialValue() { return new QNode(); } }; myPred = new ThreadLocal<QNode>() { protected QNode initialValue() { return null; } }; } public void lock() { QNode qnode = myNode.get(); qnode.locked = true; QNode pred = tail.getAndSet(qnode); myPred.set(pred); while (pred.locked) {} } public void unlock() { QNode qnode = myNode.get(); qnode.locked = false; myNode.set(myPred.get()); } } ``` Zmienna `tail` w tej implementacji jest referencją do komórki pamięci utworzonej przez ostatni zakolejkowany procesor. Kiedy nowy procesor chce otrzymać zamek, tworzy referencję do nowej komórki `myNode` i ustawia wartość tej komórki na `true`. Następnie przypisuje obecną wartość `tail` do dodatkowej zmiennej `pred` oraz ustawia `tail` na swoją stworzoną komórkę. Aby wejść do sekcji krytycznej, procesor czeka, aż `pred` (czyli komórka stworzona przez ostatni procesor przed nim) zostanie zmieniona na `false`. Kiedy sam wyjdzie z sekcji krytycznej, to ustawi swoją komórkę na `false`, a zatem wątek po nim będzie mógł wejść. W ten sposób tworzy się wirtualna lista wiązana, ponieważ każdy procesor trzyma referencję do komórki utworzonej przez poprzedni procesor (`pred`) oraz referencję do komórki utworzonej przez siebie (`myNode`). ![plik](https://hackmd.io/_uploads/H1iGVVlwp.png) Aby ograniczyć liczbę alokacji węzłów listy, można zamiast tworzyć nową referencję `myNode` do komórki w każdej iteracji, to wykorzystać komórkę używaną wcześniej przez poprzedni procesor (`pred`). W momencie zwalniania zamka procesor ustawia sobie `myNode = pred` (który nie jest już używany), dzięki czemu w następnej iteracji może z niej korzystać tak, jakby tworzył nową. ## Zadanie 6 :::success Autor: Jan Marek ::: ![image](https://hackmd.io/_uploads/SyQoQExva.png) Prześledźmy następujący scenariusz, w którym działa tylko wątek (A): - A.lock() po wykonaniu locka otrzymamy, że: `tail == myNode.get()` oraz `myNode.get().locked == true` - A.unlock() po wykonaniu unlocka: `tail == myNode.get()` oraz `myNode.get().locked == false` - A.lock() `qnode.locked = true`, to ponownie `tail == myNode.get()` oraz nadal `myNode.get().locked == false` Po wykonaniu `Qnode pred = tail.getAndSet(qnode)` mamy: `pred == myNode.get()` i `myNode.get().locked == true` Następnie A wchodzi do while'a i się zapętla, bo `pred.locked == true`. ## Zadanie 7 :::success Autor: Javier Rábago Montero ::: ![z7](https://hackmd.io/_uploads/HybpXExvp.png) ![timeout](https://hackmd.io/_uploads/BkzvX4gPa.png) This lock has the method tryLock() that allows the caller to specify a timeout as a limit for the time this caller is willing to wait to acquire the lock. If this time passes before acquiring the lock the attempt is abandoned. A boolean return value will indicate if the lock succeeded or not. It requires a constant number of steps, so it is considered wait-free. It behaves similarly to the CHL lock, where each thread spins on its predecessor’s node waiting for the lock to be released. If a thread leaves the queue node, it will mark it as abandoned, so if there is a preceding thread it notices it and can take over it. At first, pred field is set to null it means the associated thread has not acquired the lock or has released it. If it contains “AVAILABLE”, it means the associated thread has released the lock. If the field refers to other QNode it means the associated node has been abandoned so the current thread has to wait for the abandoned node’s predecessor. ![ej7](https://hackmd.io/_uploads/BkEqQNevp.png) ## Zadanie 8 :::success Autor: Michał Mękarski ::: ![image](https://hackmd.io/_uploads/rJKBOVeDa.png) ![image](https://hackmd.io/_uploads/SJSD_VeDT.png) ![image](https://hackmd.io/_uploads/S1bO_VeD6.png) ![image](https://hackmd.io/_uploads/HJGF_4lDT.png) ### Różnica w działaniu zamka MCS i CLH w architekturze NUMA. W architekturze NUMA często spotykanym zjawiskiem jest brak pamięci podręcznej. ### CLH W przypadku takiego właśnie braku pamięci podręcznej koszt wirowania na węźle innego wątku staje się bardzo czasochłonny. ### MCS Tutaj nie wirujemy na węźle innego wątku tylko na własnej komórce, co jest istotnie szybsze. ### Numa architecture example ![image](https://hackmd.io/_uploads/HyOQhiG_a.png) ## Zadanie 9 :::success Autor: Konrad Kowalczyk ::: ![zad9](https://hackmd.io/_uploads/ryHB7EgDp.png) <b>TAS</b>: ```java public class TASLock implements Lock { AtomicBoolean state = new AtomicBoolean(false); public void lock() { while (state.getAndSet(true)) {} } public void unlock() { state.set(false); } } ``` `isLocked()` dla <b>TAS</b>: ```java boolean isLocked() { return state.get(); } ``` <b>CLH</b>: ```java public class CLHLock implements Lock { AtomicReference<QNode> tail = new AtomicReference<QNode>(new QNode()); ThreadLocal<QNode> myPred; ThreadLocal<QNode> myNode; public CLHLock() { tail = new AtomicReference<QNode>(new QNode()); myNode = new ThreadLocal<QNode>() { protected QNode initialValue() { return new QNode(); } }; myPred = new ThreadLocal<QNode>() { protected QNode initialValue() { return null; } }; } public void lock() { QNode qnode = myNode.get(); qnode.locked = true; QNode pred = tail.getAndSet(qnode); myPred.set(pred); while (pred.locked) {} } public void unlock() { QNode qnode = myNode.get(); qnode.locked = false; myNode.set(myPred.get()); } } ``` `isLocked()` dla <b>CLH</b>: ```java boolean isLocked() { return tail.get().locked; } ``` <b>MCS</b>: ```java public class MCSLock implements Lock { AtomicReference<QNode> tail; ThreadLocal<QNode> myNode; public MCSLock() { queue = new AtomicReference<QNode>(null); myNode = new ThreadLocal<QNode>() { protected QNode initialValue() { return new QNode(); } }; } class QNode { boolean locked = false; QNode next = null; } public void lock() { QNode qnode = myNode.get(); QNode pred = tail.getAndSet(qnode); if (pred != null) { qnode.locked = true; pred.next = qnode; // wait until predecessor gives up the lock while (qnode.locked) {} } } public void unlock() { QNode qnode = myNode.get(); if (qnode.next == null) { if (tail.compareAndSet(qnode, null)) return; // wait until predecessor fills in its next field while (qnode.next == null) {} } qnode.next.locked = false; qnode.next = null; } } ``` `isLocked()` dla <b>MCS</b>: ```java boolean isLocked() { return tail.get() != null; } ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully