GridTools
      • 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
    # GT4Py Brainstorming: stencil calls, staggering, halo exchanges ### GT Stencil Calls / stencil composition We have a stencil that can be called from other stencils here ```python= @stencil(...) def callee(...): ... # gets compiled to callee.cpp, callee.hpp, m_callee.py ``` Users expect to be able to call this normally, so it has to be optimized and compiled into it's own cpp (target lang) unit. Potentially that could be switched off by doing something like ```python= @stencil(standalone=False, ...) def callee(...): ... ``` We could allow calling it with control flow, for example: ```python @stencil(...) def caller(...): for i in range(X): callee(...) # convergence test ??? for i in range(X): error = inf while error > MIN_ERROR: callee(...) with computation(...): a = field[...] + field[...] error = reduce(field_a - field_b, 0) # Optimizer could unrol calls in IR, for example for i_range in range(0, X, 8): callee(...) callee(...) callee(...) callee(...) callee(...) callee(...) callee(...) callee(...) # questions: # gets compiled to caller.cpp, which #includes callee.hpp and calls callee on cpp level? # where does it know from where callee.cpp sits (folder name contains a hash) # how does it know callee was or was not updated (caching?) # are we writing a build system at this point? ``` Now questions start arising: * should this use the pre-compiled `callee` or inline callee and re-optimize it as part of the whole? * compile time in large stencil libraries? * optimization benefits from inlining? * duplicating binary code when inlining and recompiling * if pre-compiled, then we start implementing parts of a build system with dependent target lang units (can we avoid this?) * implementing control flow raises testability questions * Should the control flow be lifted outside the target language and be implemented in python? We could restrict to simply calling: ```python= @stencil(...): def simple_calling(...): with computation(...), interval(...): callee(...) with computation(...), interval(...): other_callee(...) for _ in range(X): simple_calling(...) ``` Here we are not allowing control flow statements, but we are allowing essentially calling different `apply` functions of the callee stencil. How does the runtime determined domain get passed to stencils (in GT)? ### How to deal with Staggering if at all (To stagger or not to stagger) In scientific domain semantics, some fields contain values on grid points, some contain values on edges (between grid points, so one value less in each dimension). Fields relating to between grid points are called **staggered**. Expressing semantics of operations that deal with both in an agnostic GTScript (only deals with fields, not with where they are in 3d space relative to each other) is certainly possible by doing transformations before / after running stencils. However, can and / or should we provide any help with that in GTScript? Related link (?): https://en.wikipedia.org/wiki/Arakawa_grids #### Staggering code example ```python field_a = numpy.zeros((5, 5, 5)) field_b = numpy.zeros((4, 5, 5)) # staggered in i-direction ??? ``` normal field ``` xxxxx x000x x000x x000x xxxxx ``` staggered field (in i-direction) ``` xxxxx x00xx x00xx x00xx xxxxx ``` is equivalent to ``` xxxx x00x x00x x00x xxxx ``` #### Proposals: * pass domain per field to stencils * keep passing domain for all fields but per-field staggering * explicitly define domain per assignment? ## Halo exchanges data layout [ exclusive region ][first halo shell][second halo shell][third halo shell] ### on meshes The following are my (Till) personal notes on Halo exchanges for meshes. The notation is descriptive and is only meant for demonstration purposes. Example stencil: ```python def example(): with location(Cell) as c: field_tmp = sum(field_in for c in cells(c)) # stmt 1 field_out = sum(field_tmp for c in cells(c)) # stmt 2 ``` ### Naive approach Use the new storages API to initiate halo exchanges by initiating an exchange before each stencil call. This would require Python bindings for ghex. As debugging distributed code with a debug backend is useful anyway, the effort to write the bindings is not lost if we do exchanges "inside" the stencils at some point. Ghex: Pattern initialization is global and synchronized __Synchronous exchanges__ ```python class DistributedStorage(): # ... def acquire(): # exchange field values on the halo region # ... # init stoages in_field = ... out_field = ... stencil(in_field, out_field) ``` __Asynchronous exchanges__ ```python class DistributedStorage(): # ... def acquire(): # wait for wait exchanges # ... def release(): # initiate halo exchange # ... # init stoages in_field = ... out_field = ... stencil(in_field, out_field) ``` Disadvantages: Exchanges can only be initiated after computation is done. ```python with computation(FORWARD): field = field[-1, 0, 0] + field[-1, 0, 1] ``` ### Approach 1 ### Approach 2 This approach is more complex, but allows for overlapping communication and computation even across time stepping loops. 1. Decompose domain into `halo`, `foreign_halo`, `internal`. Computations on halos with external extend are never carried out, but must be replaced with halo exchanges. Halo computations without external extend may be computed on the same node. __Code for decomposition__ Requirements: - the global domain decomposition must provide cells which are foreign - or boundary cells, vertices, edges must be known (can always be computed) ```python tag_cells(mesh, "halo", lambda c: c2c2c[c] in mesh.tagged_cells("foreign")) tag_cells(mesh, "foreign_halo", lambda c: c2c2c[c] in mesh.tagged_cells("halo")) tag_cells(mesh, "internal", lambda c: c in mesh.tagged_cells("halo") and not c in mesh.tagged_cells("foreign_halo")) ``` __Stencil after decomposition__ ```python def example(): with location(Cell, tag="halo") as c: field_tmp = sum(field_in for c in cells(c)) with location(Cell, tag="foreign_halo") as c: field_tmp = sum(field_in for c in cells(c)) with location(Cell, tag="internal") as c: field_tmp = sum(field_in for c in cells(c)) with location(Cell, tag="halo", dependency={"field_tmp": ["external", "foreign_halo", "halo"]}) as c: field_tmp = sum(field_in for c in cells(c)) with location(Cell, tag="foreign_halo", dependency={"field_tmp": ["halo", "foreign_halo", "internal"]}) as c: field_out = sum(field_tmp for c in cells(c)) with location(Cell, tag="internal", dependency={"field_tmp": ["foreign_halo", "internal"]}) as c: field_out = sum(field_tmp for c in cells(c)) ``` 2. Merge statements with the same tag and highest expected return: ```python def example(): with location(Cell, tag="halo") as c: field_tmp = sum(field_in for c in cells(c)) with location(Cell, tag="foreign_halo") as c: field_tmp = sum(field_in for c in cells(c)) with location(Cell, tag="halo", dependency={"field_tmp": ["external", "foreign_halo"]}) as c: field_out = sum(field_tmp for c in cells(c)) with location(Cell, tag="foreign_halo", dependency={"field_tmp": ["halo", "foreign_halo"]}) as c: field_out = sum(field_tmp for c in cells(c)) with location(Cell, tag="internal", dependency={"field_tmp": ["foreign_halo"]}) as c: field_tmp = sum(field_in for c in cells(c)) field_out = sum(field_tmp for c in cells(c)) ``` 3. Computations on halos can now be replaced by a halo exchange. ```python def example(): with location(Cell, tag="foreign_halo") as c: field_tmp = sum(field_in for c in cells(c)) halo_exchange(field_tmp, ["foreign_halo", "halo"]) with location(Cell, tag="foreign_halo", dependency={"field_tmp": ["halo", "foreign_halo"]}) as c: field_out = sum(field_tmp for c in cells(c)) halo_exchange(field_out, ["foreign_halo", "halo"]) with location(Cell, tag="internal", dependency={"field_tmp": ["foreign_halo"]}) as c: field_tmp = sum(field_in for c in cells(c)) field_out = sum(field_tmp for c in cells(c)) ``` The halo exchange can be replaced by a seperate send and recieve to overlap communication and computations. ### Open questions - How to ensure halos are contiguous in memory? 1. Additional information added to the tag 2. Generate code for contiguous and non-contiguous case - Where to store the "patterns" object and avoid initialization on subsequent stencil calls - How to do loop merging on the halo region exactly? - Can we start computing when only part of the input fields have already been exchanged - Contiguous halos require support from storages (e.g. Atlas) problematic for internal halo

    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