Poplar and PopLibs
poplar::program::Switch Class Reference

A program that runs one of many programs depending on the value of a tensor. More...

#include <Program.hpp>

Inheritance diagram for poplar::program::Switch:
poplar::program::Program

Public Member Functions

 Switch (Tensor control, const std::vector< std::pair< std::int32_t, Program > > &cases, const DebugContext &debugContext={})
 Construct a switch with the specified set of cases and an empty default case. More...
 
 Switch (Tensor control, const std::vector< std::pair< std::int32_t, Program > > &cases, const Program &defaultCaseBody, const DebugContext &debugContext={})
 Construct a switch with the specified set of cases and default case. More...
 
 Switch (Tensor control, const DebugContext &debugContext={})
 Construct a switch with no cases and an empty default case. More...
 
 Switch (Tensor control, const Program &defaultCaseBody, const DebugContext &debugContext={})
 Construct a switch with no cases and the specified default case. More...
 
Switchadd (std::int32_t value, const Program &body)
 Add a case with the specified case value and body. More...
 

Static Public Member Functions

static Switch switchWithBoundsChecking (Tensor control, const std::vector< std::pair< std::int32_t, Program > > &cases, const DebugContext &debugContext={})
 A helper function that causes the default case to throw an error.
 
static Switch switchWithUnreachableDefault (Tensor control, const DebugContext &debugContext={})
 This function lets the compiler assume the default case is unreachable. More...
 

Detailed Description

A program that runs one of many programs depending on the value of a tensor.

The controlling tensor must be a scalar of type INT or UNSIGNED_INT.

A switch contains of a number of switch cases, each with a case value and a case body and a default case. The case values must be unique. If the value of the controlling tensor matches the case value of a case the corresponding case body is run, otherwise the default case is run.

Constructor & Destructor Documentation

◆ Switch() [1/4]

poplar::program::Switch::Switch ( Tensor  control,
const std::vector< std::pair< std::int32_t, Program > > &  cases,
const DebugContext debugContext = {} 
)

Construct a switch with the specified set of cases and an empty default case.

Parameters
controlThe controlling tensor.
casesThe cases of the switch: value and program to run.
debugContextOptional DebugId and program name.

◆ Switch() [2/4]

poplar::program::Switch::Switch ( Tensor  control,
const std::vector< std::pair< std::int32_t, Program > > &  cases,
const Program defaultCaseBody,
const DebugContext debugContext = {} 
)

Construct a switch with the specified set of cases and default case.

Parameters
controlThe controlling tensor.
casesThe cases of the switch: value and program to run.
defaultCaseBodyThe body of the default case.
debugContextOptional DebugId and program name.

◆ Switch() [3/4]

poplar::program::Switch::Switch ( Tensor  control,
const DebugContext debugContext = {} 
)

Construct a switch with no cases and an empty default case.

The add() method can be used to add cases after the switch is constructed.

Parameters
controlThe controlling tensor.
debugContextOptional DebugId and program name.

◆ Switch() [4/4]

poplar::program::Switch::Switch ( Tensor  control,
const Program defaultCaseBody,
const DebugContext debugContext = {} 
)

Construct a switch with no cases and the specified default case.

The add() method can be used to add cases after the switch is constructed.

Parameters
controlThe controlling tensor.
defaultCaseBodyThe body of the default case.
debugContextOptional DebugId and program name.

Member Function Documentation

◆ add()

Switch & poplar::program::Switch::add ( std::int32_t  value,
const Program body 
)

Add a case with the specified case value and body.

Parameters
valueThe case value.
bodyThe case body.
Returns
A reference to the switch program.

◆ switchWithUnreachableDefault()

static Switch poplar::program::Switch::switchWithUnreachableDefault ( Tensor  control,
const DebugContext debugContext = {} 
)
static

This function lets the compiler assume the default case is unreachable.

If the control value is something other than one of the cases, it results in undefined behaviour (although there is some very minimal error checking at runtime).


The documentation for this class was generated from the following file: