GpuKernelLaunch

Launches a GPU kernel.

Declaration

Syntax

GpuKernelLaunch(symbol kernel, expr grid_size, expr block_size,
    call_arg* args)

Arguments

Argument

Description

kernel

the Function to run. Its signature has exec_space = Kernel; see exec_space.

grid_size

the number of blocks to launch.

block_size

the number of threads per block.

args

the arguments passed to every thread.

Return values

None.

Description

A launch is not a call: it starts grid_size * block_size threads and, unless the backend says otherwise, returns without waiting for them. GpuSync is what waits.

The execution configuration is part of the node rather than of the kernel, because the same kernel is normally launched with different sizes.

The kernel is an ordinary Function; what makes it launchable is that its signature has exec_space = Kernel, and a kernel has no result. See exec_space.

The gpu_offload pass replaces a parallel loop assigned to the device with its launch, together with the copies the host makes before and after it; nothing of the original loop is kept. gpu_kernel_finalize records the kernel’s Function.gpu layout after shared lowering. device_launch_expand and both device emitters consume that layout. Workspace sizes are evaluated once on the host and passed as explicit scalar arguments, so host allocation and device indexing use the same values.

Before creating a launch, GPU offload checks the allocation of allocatable array results in callees that remain out of line. Nested or multiple allocation sites must agree with a buffer shape established by an unconditional fixed allocation. Otherwise the loop is rejected while the original loop is still available, which is an error: it is a lowering LFortran does not have yet. Reallocation and conditional assignments remain eligible when they preserve every extent, not just the total element count.

A loop reaches GPU offload only after the unsupported-construct check in the parallel_dispatch pass has assigned it to the device (see GPU offloading), so a loop the offload cannot lower is a compile-time error, whatever its reason, whether or not --gpu-allow-cpu-fallback is given, and also with --show-gpu-kernel-source.

Examples

(GpuKernelLaunch
  :kernel (SymbolRef 1 "zero")
  :grid_size (IntegerConstant
    :n 1
    :type (Integer
      :kind 4
    )
    :intboz_type :Decimal
  )
  :block_size (IntegerConstant
    :n 256
    :type (Integer
      :kind 4
    )
    :intboz_type :Decimal
  )
  :args [
    (call_arg
      :value (Var
        :v (SymbolRef 2 "a")
      )
    )
  ]
)

It comes from this complete ASR text document:

(TranslationUnit
  :symtab (SymbolTable
    :id 0
    :symbols {
      "m" (Module
        :symtab (SymbolTable
          :id 1
          :symbols {
            "launch" (Function
              :symtab (SymbolTable
                :id 2
                :symbols {
                  "a" (Variable
                    :parent_symtab 2
                    :name "a"
                    :dependencies []
                    :intent :InOut
                    :symbolic_value nil
                    :value nil
                    :storage :Default
                    :type (Array
                      :type (Real
                        :kind 4
                      )
                      :dims [
                        (dimension
                          :start (IntegerConstant
                            :n 1
                            :type (Integer
                              :kind 4
                            )
                            :intboz_type :Decimal
                          )
                          :length (IntegerConstant
                            :n 256
                            :type (Integer
                              :kind 4
                            )
                            :intboz_type :Decimal
                          )
                        )
                      ]
                      :physical_type :FixedSizeArray
                      :memory_space :Global
                    )
                    :type_declaration nil
                    :abi :Source
                    :access :Public
                    :presence :Required
                    :value_attr false
                    :target_attr false
                    :contiguous_attr false
                    :bindc_name nil
                    :is_volatile false
                    :is_protected false
                    :pass_attr :NotMethod
                    :self_argument nil
                    :codims []
                    :common_block nil
                  )
                }
              )
              :name "launch"
              :function_signature (FunctionType
                :arg_types [
                  (Array
                    :type (Real
                      :kind 4
                    )
                    :dims [
                      (dimension
                        :start (IntegerConstant
                          :n 1
                          :type (Integer
                            :kind 4
                          )
                          :intboz_type :Decimal
                        )
                        :length (IntegerConstant
                          :n 256
                          :type (Integer
                            :kind 4
                          )
                          :intboz_type :Decimal
                        )
                      )
                    ]
                    :physical_type :FixedSizeArray
                    :memory_space :Global
                  )
                ]
                :return_var_type nil
                :abi :Source
                :deftype :Implementation
                :bindc_name nil
                :elemental false
                :pure false
                :module false
                :inline false
                :static false
                :restrictions []
                :is_restriction false
                :exec_space :Host
              )
              :dependencies []
              :args [
                (Var
                  :v (SymbolRef 2 "a")
                )
              ]
              :body [
                (GpuKernelLaunch
                  :kernel (SymbolRef 1 "zero")
                  :grid_size (IntegerConstant
                    :n 1
                    :type (Integer
                      :kind 4
                    )
                    :intboz_type :Decimal
                  )
                  :block_size (IntegerConstant
                    :n 256
                    :type (Integer
                      :kind 4
                    )
                    :intboz_type :Decimal
                  )
                  :args [
                    (call_arg
                      :value (Var
                        :v (SymbolRef 2 "a")
                      )
                    )
                  ]
                )
                (GpuSync)
              ]
              :return_var nil
              :access :Public
              :deterministic false
              :side_effect_free false
              :module_file nil
            )
            "zero" (Function
              :symtab (SymbolTable
                :id 3
                :symbols {
                  "a" (Variable
                    :parent_symtab 3
                    :name "a"
                    :dependencies []
                    :intent :InOut
                    :symbolic_value nil
                    :value nil
                    :storage :Default
                    :type (Array
                      :type (Real
                        :kind 4
                      )
                      :dims [
                        (dimension
                          :start (IntegerConstant
                            :n 1
                            :type (Integer
                              :kind 4
                            )
                            :intboz_type :Decimal
                          )
                          :length (IntegerConstant
                            :n 256
                            :type (Integer
                              :kind 4
                            )
                            :intboz_type :Decimal
                          )
                        )
                      ]
                      :physical_type :FixedSizeArray
                      :memory_space :Global
                    )
                    :type_declaration nil
                    :abi :Source
                    :access :Public
                    :presence :Required
                    :value_attr false
                    :target_attr false
                    :contiguous_attr false
                    :bindc_name nil
                    :is_volatile false
                    :is_protected false
                    :pass_attr :NotMethod
                    :self_argument nil
                    :codims []
                    :common_block nil
                  )
                }
              )
              :name "zero"
              :function_signature (FunctionType
                :arg_types [
                  (Array
                    :type (Real
                      :kind 4
                    )
                    :dims [
                      (dimension
                        :start (IntegerConstant
                          :n 1
                          :type (Integer
                            :kind 4
                          )
                          :intboz_type :Decimal
                        )
                        :length (IntegerConstant
                          :n 256
                          :type (Integer
                            :kind 4
                          )
                          :intboz_type :Decimal
                        )
                      )
                    ]
                    :physical_type :FixedSizeArray
                    :memory_space :Global
                  )
                ]
                :return_var_type nil
                :abi :Source
                :deftype :Implementation
                :bindc_name nil
                :elemental false
                :pure false
                :module false
                :inline false
                :static false
                :restrictions []
                :is_restriction false
                :exec_space :Kernel
              )
              :dependencies []
              :args [
                (Var
                  :v (SymbolRef 3 "a")
                )
              ]
              :body []
              :return_var nil
              :access :Public
              :deterministic false
              :side_effect_free false
              :module_file nil
            )
          }
        )
        :name "m"
        :parent_module nil
        :dependencies []
        :loaded_from_mod false
        :intrinsic false
        :has_submodules false
        :global_init nil
      )
      "main" (Program
        :symtab (SymbolTable
          :id 4
          :symbols {}
        )
        :name "main"
        :dependencies []
        :body []
        :global_init nil
      )
    }
  )
  :items []
  :global_init nil
)

See Also

Function, exec_space, GpuSync, GpuThreadIndex