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
    # Formal Semantics for the Parallel Model ###### tags: `Felix’s remains` **IMPORTANT NOTE: This document is about semantics only. It does not try to define any syntax. All code examples are for demonstration purposes only and not for showing real DSL syntax.** ## Goals A clear understanding of the semantics of the current parallel model and its limitations and undefined behavior. Finally, a new well-defined parallel model with clear semantics, well-defined behavior and good composability should be derived. ## Definitions * **Accessor**: a function taking offsets and returning a value. * **Stencil**: a function taking zero or more accessor arguments and returning a value. * **Grid**: set of points. * **Compute domain**: a subset of a grid. * **Fencil**: one or more pairs of a stencil closures (stencils with bound field arguments and return values) with associated compute domains. * **Lifting** (of a stencil): transformation of a stencil function to a function that returns an accessor instead of a value. ## Observations on the Current Parallel Model ### Limitations * Intended behavior of the backends is not clearly defined, often different backends behave differently. * Horizontal regions are required, but very hard to incorporate into current model (with extended compute domains etc.). * Composability is very limited. * Many advanced features like automatic halo exchanges and control flow require a better understanding and definition of the underlying behavior. ### Behavior * The current GTScript “stencils” according to the definitions above are actually fencils * Temporary assignment behaves as lifting. Note the type annotations: ```python= @gtscript.stencil(...) def d2dx2(inp, out): with computation(PARALLEL), interval(...): # we define actually a new accessor function here! tmp: accessor = inp[I + 1] - inp[I - 1] out: float = tmp[I + 1] - tmp[I - 1] ``` * Alternatively, the assignments can be interpreted as assignments to temporary arrays (this requires some notion of implicit loop for each statement). Note that this is one possible implementation of lifting. The alternative implementation is inlining/on-the-fly computation. * The current GTScript functions can be used to implement stencils (in the sense of the definition above). * GT4Py inherited some features from GridTools that increase difficulty of optimization (input, output parameters, multiple definitions of same symbol/multiple writes to same field). ## Considerations for the New Parallel Model * Arguments should be input only or output only. * Symbols should be defined exactly once. * Lifting allows for great composability of stencils: ```python= @stencil def ddx(y: accessor) -> value: return y[I + 1] - y[I - 1] # explicit lifting @stencil def d2dx2(y: accessor) -> value: return ddx(lift(ddx)(y)) # implicit lifting by temporary assignment @stencil def d2dx2(y: accessor) -> value: tmp = ddx(y) return ddx(tmp) ``` * A typical example of applying boundary conditions to a field in numpy syntax: ```python= # Approximation of first derivative in inner domain dydx[1:-1] = (y[1:] - y[:-1]) / (2 * dx) # Apply different approximation or BC at the boundaries dydx[0] = (y[1] - y[0]) / dx dydx[-1] = (y[-1] - y[-2]) / dx # Continue using dydx in next computation ... = dydx[...] ``` * This can be handled directly within stencils: ```python= @stencil def ddx(y: accessor, on_left_boundary: accessor, on_right_boundary: accessor) -> value: if on_left_boundary: return y[I + 1] - y if on_right_boundary: return y - y[I - 1] return (y[I + 1] - y[I - 1]) / 2 ``` * The composed can then be applied to an array easily using a fencil: ```python= @fencil def fency(y: Input, dydx: Output, output_domain: ComputeDomain): dydx = ddx(y) @ output_domain ``` * Handling of vertical computations can be implemented in this framework by choosing columns as the granularity for all operations. ## Appendix: Examples **Note again: this is not a syntax proposal. Some arbitrary syntax is chosen. The examples are just there to provide an idea of how slightly more complex examples can be implemented using this model.** ### Heat Equation with FTCS Scheme (1D) This is a typical example where boundary conditions are written after each stencil application. ```python= @stencil def d2dx2(y, dx): return (y[I + 1] - 2 * y + y[I - 1]) / dx**2 @stencil def step(y, dx, dt, diff): d2ydx2 = d2dx2(y) return y + dt * diff * d2ydx2 @stencil def step_with_bcs(y, dx, dt, diff, on_left_domain, on_right_domain): yp = step(y, dx, dt, diff) if on_left_domain: # zero-gradient BC return yp[I + 1] if on_right_domain: # Dirichlet BC (setting this every step is obviouly not necessary, # but done here for demonstration purposes) return 1 return yp @fencil def apply_step(y0: Input, on_left_boundary: Input, on_right_boundary: Input, y1: Output, dx: float, dt: float, diff: float, domain: ComputeDomain): y1 = step_with_bcs(y0, dx, dt, diff, on_left_boundary, on_right_boundary) ``` ### Tridiagonal Solver Having stencils taking whole columns as inputs and returning whole columns, allows to handle vertical solvers as pure stencil functions. All state required for the vertical sweeps can be encapsulated within the stencil. ```python= # note: all stencil arguments are read-only (whole columns) # read-write fields are hidden within a single stencil @stencil def forward(a, b, c, d): cp = column() dp = column() for k in krange[:1]: cp[k] = c[k] / b[k] dp[k] = d[k] / b[k] for k in krange[1:]: # note: cp and dp are read-write! cp[k] = c[k] / (b[k] - a[k] * cp[k - 1]) dp[k] = (d[k] - a[k] * dp[k - 1]) / (b[k] - a[k] * cp[k - 1]) # the return values are whole columns again return cp, dp @stencil def backward(cp, dp): for k in reversed(krange[-1:]): x[k] = dp[k] for k in reversed(krange[:-1]): x[k] = dp[k] - cp[k] * x[k + 1] return x @stencil def solve_tridiag(a, b, c, d): # the vertical solver functions can be composed as usual cp, dp = forward(a, b, c, d) x = backward(cp, dp) return x ``` ### Testability of Components Unit testing of single components is an important feature and can be easily performed using the proposed model. Here an example of how to test the heat equation solver from above. ```python= def test_d2dx2(): # trivial fencil for applying the stencil to be tested @fencil def apply_d2dx2(y: Input, d2ydx2: Output, dx: float, domain: ComputeDomain): d2ydx2 = d2dx2(y, dx) # input field: default_origin is set to avoid out-of bounds accesses # this is user responsibility! y = storage(..., default_origin=(1, 0)) np_y = np.asarray(y) d2dx2 = storage(...) apply_d2dx2(y, d2ydx2, domain=...) assert np.allclose(np.asarray(d2ydx2), (np_y[1:] - np_y[:-1]) / dx) def test_step(): @fencil def apply_d2dx2(y0: Input, y1: Output, dx: float, dt: float, diff: float, domain: ComputeDomain): y1 = step(y0, dx, dt, diff) @ domain ... ``` ### Staggering ```python= @stencil def flux(c, u): # u is a staggered field, this returns a staggered flux, that is, # corresponds to f_{i + 0.5} return c if u >= 0 else c[I + 1] # could also be defined as follows, then we would return f_{i - 0.5} # it is user responsibility to use a consistent convention # return c[I - 1] if u >= 0 else c @stencil def dfdx(c, u, dx): f = flux(c, u) # user responsibility to take correct offsets for f_{i + 0.5}, f_{i - 0.5} # (the return value is non-staggered) return (f - f[I - 1]) / dx @stencil def step(c, u, dt, dx): return c + dt * dfdx(c, u, dx) @fencil def step_with_flux_output(c0: Input, u: Input, c1: Output, f: Output, dx: float, dt: float, domain: FullDomain, flux_domain: FluxDomain): # backends can decide how they handle the redundant flux computation, # e.g. compute the flux and put it into a temporary buffer, # compute the flux twice in two separate loops (for each output), # do only one loop with OTF-computation of the flux and masked writes… f = flux(c0, u) @ FluxDomain c1 = step(c, u, dt, dx) @ FullDomain ``` ## Appendix: Notes * Handling of tuple return values in lifting: ```python= @stencil def foo(x: accessor) -> value: y, z = bar(x) return baz(y, z) @stencil def foo(x: accessor) -> value: y = lift(bar, return_index=0)(x) z = lift(bar, return_index=1)(x) return baz(y, z) ```

    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