Skip to content

Conversation

@NamjaeChoi
Copy link
Contributor

@NamjaeChoi NamjaeChoi commented Jan 28, 2026

Refs #30655.

You are very welcome to check this out and test the function parser to find corner cases. It reports Abstract Syntax Tree (AST) of the expression and its Reverse Polish Notation (RPN) to be used on GPUs. For example:

[parsed]
  type = KokkosParsedFunction
  expression = 'if( (-1+2*3) > 6, sin(0), cos(0) ) + 1 / 2'
[]
Abstract Syntax Tree (AST) of KokkosParsedFunction parsed:

Binary (+)
  Call (
    Callee:
      Identifier (if)
    Arguments:
      Binary (>)
        Binary (+)
          Unary (-)
            Number (1)
          Binary (*)
            Number (2)
            Number (3)
        Number (6)
      Call (
        Callee:
          Identifier (sin)
        Arguments:
          Number (0)
      )
      Call (
        Callee:
          Identifier (cos)
        Arguments:
          Number (0)
      )
  )
  Binary (/)
    Number (1)
    Number (2)

Reverse Polish Notation (RPN) of KokkosParsedFunction parsed:

1.000000 - 2.000000 3.000000 * + 6.000000 > 0.000000 sin 0.000000 cos if 1.000000 2.000000 / +

Error handling examples:

*** ERROR ***
Kokkos parsed function error: expected ')'.
if( (-1+2*3) > 6, sin(0), cos(0) + 1 / 2
                                       ^

*** ERROR ***
Kokkos parsed function error: extra ')'.
if( (-1+2*3) > 6, sin(0), cos(0) )) + 1 / 2
                                  ^

*** ERROR ***
Kokkos parsed function error: invalid function call syntax.
if( (-1+2*3) > 6, sin(0), 42(0) ) + 1 / 2
                           ^

*** ERROR ***
Kokkos parsed function error: unsupported function 'foo'.
if( (-1+2*3) > 6, sin(0), foo(0) ) + 1 / 2
                            ^

*** ERROR ***
Kokkos parsed function error: if() expects 3 argument(s).
if( (-1+2*3) > 6, sin(0)) + 1 / 2
 ^

*** ERROR ***
Kokkos parsed function error: unexpected character ';'.
if( (-1+2*3) > 6, sin(0), cos(0) ); + 1 / 2
                                  ^

*** ERROR ***
Kokkos parsed function error: expected an expression.
if( (-1+2*3) > 6, sin(0), cos(0) ) ++ 1 / 2
                                    ^

*** ERROR ***
Kokkos parsed function error: variable name 'log' is reserved for function.
if( (-1+2*3) > 6, sin(0), cos(0) ) + log / 2
                                       ^

@NamjaeChoi NamjaeChoi force-pushed the kokkos_fparser branch 3 times, most recently from 1a1d9f2 to 3d4354b Compare January 28, 2026 06:11
@NamjaeChoi NamjaeChoi moved this to In progress in NEAMS MP CONNECT/GPU 26 Feb 2, 2026
@dschwen
Copy link
Member

dschwen commented Feb 2, 2026

I suggest using the PEG parser already shipped with MOOSE to parse the functions.

@dschwen
Copy link
Member

dschwen commented Feb 2, 2026

Alternatively you can use FParser (and get algebraic optimization) and access the tape and transform that into GPU operations.

@dschwen
Copy link
Member

dschwen commented Feb 2, 2026

Here is an example for a GPU function parser (that generates torch compute graphs):

https://github.com/idaholab/marlin/blob/devel/include/utils/MarlinExpressionParser.h
https://github.com/idaholab/marlin/blob/devel/src/utils/MarlinExpressionParser.C

and uses peg::parser (grammar is at the bottom of the .h file).

@NamjaeChoi
Copy link
Contributor Author

access the tape

How do I access the tape? Using FParser will be probably better for consistency.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In progress

Development

Successfully merging this pull request may close these issues.

2 participants