<!--
{
  "availability" : [
    "iOS: 18.0.0 -",
    "iPadOS: 18.0.0 -",
    "macCatalyst: 18.0.0 -",
    "macOS: 15.0.0 -",
    "tvOS: 18.0.0 -",
    "visionOS: 2.0.0 -",
    "watchOS: 11.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Accelerate",
  "identifier" : "/documentation/Accelerate/BNNSGraphContextExecute(_:_:_:_:_:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "Accelerate"
    ],
    "preciseIdentifier" : "c:@F@BNNSGraphContextExecute"
  },
  "title" : "BNNSGraphContextExecute(_:_:_:_:_:_:)"
}
-->

# BNNSGraphContextExecute(_:_:_:_:_:_:)

Executes the specified function with the given context.

```
func BNNSGraphContextExecute(_ context: bnns_graph_context_t, _ function: UnsafePointer<CChar>?, _ argument_count: Int, _ arguments: UnsafeMutablePointer<bnns_graph_argument_t>, _ workspace_size: Int, _ workspace: UnsafeMutablePointer<CChar>?) -> Int32
```

## Parameters

`context`

The graph context. You may only use this context on a single thread at a time.

`function`

The function. Specify as `nil` if the graph only contains one function.

`argument_count`

The number of elements in the `arguments` array.

`arguments`

An array of `bnns_graph_argument_t` objects that supply the input data and the outputs. Use the [`BNNSGraphGetArgumentPosition(_:_:_:)`](/documentation/Accelerate/BNNSGraphGetArgumentPosition(_:_:_:)) to derive the argument positions for this parameter. If you supply unallocated output arguments, this function allocates the required memory and reuses that memory for future executions.

`workspace_size`

The size of the workspace. Call [`BNNSGraphContextGetWorkspaceSize(_:_:)`](/documentation/Accelerate/BNNSGraphContextGetWorkspaceSize(_:_:)) to get the required size or pass `0` and a null `workspace` parameter to specify that this function allocates its own workspace.

`workspace`

The temporary workspace that the function uses during execution. Call [`BNNSGraphContextGetWorkspaceSize(_:_:)`](/documentation/Accelerate/BNNSGraphContextGetWorkspaceSize(_:_:)) to get the required size or pass `nil` and a zero `workspace_size` parameter to specify that this function allocates its own workspace.

## Return Value

`0` on success, nonzero on failure.

## Discussion

If the underlying model contains dynamic shaped inputs or outputs, you must set these prior to calling this routine with either [`BNNSGraphContextSetDynamicShapes(_:_:_:_:)`](/documentation/Accelerate/BNNSGraphContextSetDynamicShapes(_:_:_:_:)) or [`BNNSGraphContextSetBatchSize(_:_:_:)`](/documentation/Accelerate/BNNSGraphContextSetBatchSize(_:_:_:)). Don’t modify these shapes during execution.

This function doesn’t perform memory allocation if your code meets the following conditions:

- The workspace isn’t `nil`.
- The graph doesn’t contain tensors that BNNS couldn’t bound prior to calling this function.
- The arguments array doesn’t contain any new shape information.
- The code provides all output storage.

The following code executes the graph with the [`BNNSTensor`](/documentation/Accelerate/BNNSTensor) structures `inputTensor` and `outputTensor` as the input and output arguments, respectively:

```swift
withUnsafeMutablePointer(to: &inputTensor) { input in
    withUnsafeMutablePointer(to: &outputTensor) { output in
        
        var inputArgument = bnns_graph_argument_t()
        inputArgument.tensor = input
        
        var outputArgument = bnns_graph_argument_t()
        outputArgument.tensor = output
        
        var arguments = [outputArgument, inputArgument]
        
        BNNSGraphContextSetArgumentType(context, BNNSGraphArgumentTypeTensor)
        
        BNNSGraphContextExecute(context, nil, 2, &arguments, 0, nil)
    }
}
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)