Levi Liang
    • Create new note
    • Create a note from template
      • 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
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me 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
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Área de transferĂȘncia
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • HTML puro
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Ajuda
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Área de transferĂȘncia
Export to
Dropbox Google Drive Gist
Download
Markdown HTML HTML puro
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
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me 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 Área de transferĂȘncia
       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
    # 4 Advanced OpenMP and Changes since OpenMP 5.0 ###### tags: `SS2021-IN2147-PP` ### Important Runtime Routines ```c // Determine the number of threads for parallel regions omp_set_num_threads(count) // Query the maximum number of threads for team creation maxthreads = omp_get_max_threads() // Query number of threads in the current team numthreads = omp_get_num_threads() // Query own thread number (0..n-1) iam = omp_get_thread_num() // Query the number of processors numprocs = omp_get_num_procs() ``` ### Relevant Environment Variables (ICVs) ```shell ## ICVs = Internal Control Variables ## Number of threads in a team of a parallel region OMP_NUM_THREADS=4 ## Selects scheduling strategy to be applied at runtime ## Schedule clause in the code takes precedence OMP_SCHEDULE=”dynamic” OMP_SCHEDULE=”GUIDED,4“ ## Allow runtime system to determine the number of threads OMP_DYNAMIC=TRUE ## Allow nesting of parallel regions ## If supported by the runtime OMP_NESTED=TRUE ``` ## Data Races ### Types of Races #### Read after Write (RAW) ```c // T1: x = ... // T2: ... = x ``` #### Write after Read (WAR) ```c // T1: ... = x // T2: x = ... ``` #### Write after Write (WAW) ```c // T1: x = ... // T2: x = ... ``` ### Race Detection Tools * **`Helgrind`** (open source) * Dynamic, based on `valgrind tool` suite * **`Intel Inspector`** (commercial) * Part of Intel’s development suite * **`Thread Sanitizer`** (open source) * Static instrumentation via LLVM * **`Archer`** (open source) * Combines `Thread Sanitizer`’s approach with OpenMP semantics ## Loop Dependencies ### Dependence and Dependence Graph #### Control Dependence ```c S1: if (t == 5) S2: r=5.0 else S3: r=3.0 ``` ```mermaid graph LR; S1-->S2; S1-->S3; ``` #### Data Dependence ```c S1: pi=3.14 S2: r=5.0 S3: area=pi*r**2 ``` ```mermaid graph LR; S1-->S3; S2-->S3; ``` ### Types of Data Dependencies * I(S) * set of memory locations read (input) * O(S) * set of memory locations written (output) * True Dependence (Flow Dependence) * $O(S1) ∩ I(S2) ≠ ∅$ * $(S1\ \delta\ S2)$ * Anti Dependence * $I(S1) ∩ O(S2) ≠ ∅$ * $(S1\ \delta^{-1}\ S2)$ * Output Dependence * $O(S1) ∩ O(S2) ≠ ∅$ * $(S1\ \delta^{0}\ S2)$ ### Loop Dependencies #### Loop Independent Dependencies ```c for (i = 0; i < 4; i++) S1: b[i] = 8; S2: a[i] = b[i] + 10; ``` #### Loop Carried Dependencies ```c for (i = 0; i < 4; i++) S1: b[i] = 8; S2: a[i] = b[i-1] + 10; ``` ### Aliasing ```c void Foo(int A[], int B[], n) { for (int i=0; i<n; i++) A[i] = 5 * B[i] + A[i] } int main() { int A[100], B[100]; // A != B Foo(A, B, 100); // aliasing!!! Foo(A, A, 100); return 0; } ``` ## Loop Transformation ### Transformation 1: Loop Interchange ```c /* Assuming no aliasing */ // original do l=1,10 do m=1,10 do k=1,10 A(l,m,k)=A(l,m,k)+B enddo enddo enddo // Loop Interchange do l=1,10 do k=1,10 do m=1,10 A(l,m,k)=A(l,m,k)+B enddo enddo enddo ``` ### Transformation 2: Loop Distribution / Loop Fission ```c /* Assuming no aliasing */ // original do j=2,n S1: a(j)= b(j)+2 S2: c(j)= a(j-1) * 2 enddo // Loop Distribution / Loop Fission // * reduces the granularity // * increase parallelism do j=2,n S1: a(j)= b(j)+2 enddo do j=2,n S2: c(j)= a(j-1) * 2 enddo ``` ### Transformation 3: Loop Fusion ```c /* Assuming no aliasing */ // original do i=1,n a(i)= b(i)+2 enddo do i=1,n c(i)= d(i+1) * a(i) enddo // Loop Fusion: // * increases granularity // * reduce parallelism do i=1,n a(i)= b(i)+2 c(i)= d(i+1) * a(i) enddo ``` ### Transformation 4: Loop Alignment ```c // original do i=2,n S1: a(i)= b(i)+2 S2: c(i)= a(i-1) * 2 enddo // Loop Alignment do i=1,n S1: if (i>1) a(i)= b(i)+2 S2: if (i<n) c(i+1)= a(i) * 2 enddo ``` ![](https://i.imgur.com/HUkEaBV.png) ### Summary Loop Transformations * Eliminate carried dependences * Loop distribution * Loop alignment * Improve efficiency * Loop fusion * Loop interchange ## Advanced OpenMP / Tasks ### Drawbacks of Work Sharing * possible ***imbalance*** caused by **`workload`** * possible ***imbalance*** caused by **`machine`** * **`limited programming flexibility`** ### Explicit Tasking (since OpenMP 3.0) * ***Tied***: once a task starts it will remain on the same thread * Default behavior * Easy to reason about * ***Untied***: tasks can move to a different thread * Execution can be interrupted and the task moved * Advantage: more flexibility and better resource utilization ### The OpenMP Task Construct ```c #pragma omp parallel { #pragma omp single { for ( elem = l->first; elem; elem = elem->next) // Tasks can be executed by any thread in the team #pragma omp task process(elem) } // Barrier: all tasks are complete by this point } ``` ### Tasking Syntax in OpenMP ```c #pragma omp task [clause list] { ... } /* Select clauses */ // FALSE: Execution starts immediately by the creating thread if (scalar-expression) // Task is not tied to the thread starting its execution untied // Default is firstprivate Default (shared|none), private, firstprivate, shared // Hint to influence order of execution priority(value) // Waits for completion #pragma omp taskwait { ... } // The current task can be suspended // Explicit task scheduling point #pragma omp taskyield { ... } ``` :::warning **Implicit task scheduling points** * Task creation * End of a task * Taskwait * Barrier synchronization ::: ### Task Dependencies (since OpenMP 4.0) ```c /* Influences scheduling order */ // Out: variables produced by this task // In: variables consumed by this task // Inout: variables is both in and out // Example #pragma omp task shared(x, ...) depend(out: x) // T1 preprocess_some_data(...); #pragma omp task shared(x, ...) depend(in: x) // T2 do_something_with_data(...); #pragma omp task shared(x, ...) depend(in: x) // T3 do_something_independent_with_data(...); ``` * [c++ - OpenMP Task Dependency Ignored? - Stack Overflow](https://stackoverflow.com/questions/54990886/openmp-task-dependency-ignored) ### Performance Considerations for Tasking #### Advantages * Implicit load balancing * Simple programming model * Many complications and bookkeeping pushed to runtime #### Consideration 1: Task granularity * **Fine grained** allow for more resource utilization * more overhead * **Coarse grained** tasks reduce overhead * schedule fragmentation * **Right granularity!!!** #### Consideration 2: NUMA optimization * Modern runtimes provide optimizations * NUMA aware scheduling ## OpenMP Memory Model ### Memory Models #### Memory/Cache Coherency * Snoop-based protocols * Directory-based protocols #### Memory Consistency * Sequential Consistency * Relaxed Consistency * Processor Consistency (HW Threads Consistency) * Writes by any thread are seen by all threads in the order they were issued * But different threads may see a different order * Weak Consistency * Synchronization operations * Release Consistency * subdivide synchronization operations into “acquire” and “release” * [Memory-Consistency](https://hackmd.io/Bz0_tLu0S8STNPiAiY6tjw?view#Memory-Consistency) ### The OpenMP Memory Model * **`Weak consistency`** * Memory synchronization points (or flush points) * Entry and exit of **`parallel regions`** * **`Barriers`** (implicit and explicit) * Entry and exit of **`critical regions`** * Use of **`OpenMP runtime locks`** * Every **`task scheduling point`** ### OpenMP’s Flush Directive ```c /* Synchronizes data of the executing thread with main memory */ // Load/stores executed before the flush have to be finished // Load/stores following the flush are not allowed to be executed early #pragma omp flush [(list)] ``` ## OpenMP 5.1 * Full support for C11, C\++11, C\++14, C\++17, C\++20 and Fortran 2008 * The `OMP_PLACES` syntax was extended * `omp_display_env` runtime routine to provide information about ICVs ### Masked Region (~~Master Region~~) ```c // Only the primary thread executes the code block #pragma omp masked block // A region that only the threads execute that are specified in the integer expression #pragma omp masked [filter(integer-expression)] block ```

    Importar da ĂĄrea de transferĂȘncia

    Cole seu markdown ou pĂĄgina web aqui...

    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.

    Esta nota estĂĄ bloqueada

    Desculpe, somente o dono pode editar esta nota.

    Alcançou o limite

    Desculpe, vocĂȘ alcançou o tamanho mĂĄximo que esta nota pode ter.
    Por favor reduza o conteĂșdo ou divida em mais de uma nota, obrigado!

    Importar de um Gist

    Importar de Snippet

    ou

    Exportar para Snippet

    Tem certeza?

    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.

    Esta pĂĄgina precisa ser recarregada

    VocĂȘ tem uma versĂŁo incompatĂ­vel do cliente.
    Recarregar para atualizar.
    Nova versĂŁo disponĂ­vel!
    Veja notas de lançamento aqui
    Atualize para usar as novas funçÔes.
    O estado do seu usuĂĄrio mudou.
    Atualize para carregar o novo estado do usuĂĄrio.

    Sign in

    Forgot password

    ou

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

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

    New to HackMD? Sign up

    Ajuda

    • English
    • äž­æ–‡
    • Français
    • Deutsch
    • æ—„æœŹèȘž
    • Español
    • CatalĂ 
    • ΕλληΜÎčÎșÎŹ
    • PortuguĂȘs
    • italiano
    • TĂŒrkçe
    • РуссĐșĐžĐč
    • Nederlands
    • hrvatski jezik
    • język polski
    • ĐŁĐșŃ€Đ°Ń—ĐœŃŃŒĐșа
    • à€čà€żà€šà„à€Šà„€
    • svenska
    • Esperanto
    • dansk

    Documentos

    Help & Tutorial

    How to use Book mode

    Exemplo de Apresentação

    API Docs

    Edit in VSCode

    Install browser extension

    Contatos

    Feedback

    Discord

    Envie-nos um email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Dicas

    Syntax Exemplo Reference
    # Cabeçalho Cabeçalho ćŸșæœŹæŽ’ç‰ˆ
    - Lista nĂŁo ordenada
    • Lista nĂŁo ordenada
    1. Lista ordenada
    1. Lista ordenada
    - [ ] Lista de tarefas
    • Lista de tarefas
    > Citação
    Citação
    **Fonte negrito** Fonte negrito
    *Fonte itĂĄlico* Fonte itĂĄlico
    ~~Tachado~~ Tachado
    19^th^ 19th
    H~2~O H2O
    ++Texto inserido++ Texto inserido
    ==Texto marcado== Texto marcado
    [link text](https:// "title") Ligação
    ![image alt](https:// "title") Imagem
    `CĂłdigo` CĂłdigo ćœšç­†èš˜äž­èČŒć…„çš‹ćŒçąŒ
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externos
    $L^aT_eX$ LaTeX
    :::info
    Esta Ă© uma ĂĄrea de alerta.
    :::

    Esta Ă© uma ĂĄrea de alerta.

    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