Operators: Multi-threaded function support
FISH operators are a special class of function designed to be executed in a multi-threaded environment.
On a repeated function call made using splitting, if the symbol was declared as an operator these separate executions will be distributed on all available threads (see the program threads
command). On a typical modern multi-core computer and a large set of data this can result in a very large increase in speed.
Because the functions need to be safe when multiple threads are running simultaneously, they operate in a restricted environment. There are a number of rules that make operators different from normal functions:
Operators must always take at least one argument.
Operators can not call normal FISH functions. They can call other operators.
Operators can only call FISH library functions if they are tagged as being thread safe. Library functions are tagged as thread safe if the ‘=’ sign is preceded by a ‘:’ in the documentation description.
Operators can only write to global symbols when the lock statement is used.
Operators cannot modify the values passed as arguments.
Operators cannot call library functions on assignment (on the left hand side of an equality operator) and pass a pointer to an object, unless that object was passed in as an argument.
Operators cannot call other operators and pass a pointer to an object, unless that object was passed in as an argument.
Be aware that reading or writing to global symbols must be synchronized across all executions running simultaneously, and can therefore severely affect overall performance. It is recommended that local variables be used exclusively where possible.
Operators are created using the fish operator
command, with arguments following just like fish define
.
The FISH lines in the definition are the same as for a normal function, subject to the restrictions above.
In order to give examples of operators, we will first generate some data to operate upon. The following FISH function will generate 100,000 data scalars at random points in a 10x10x10 cube and give them random values from 0.0 to 10.0.
The following example operator determines if the x-coordinate of a particular scalar object falls within a given range, and if so it both assigns it to the group inside in slot mark and returns true, or assigns it to the group outside and returns false.
To execute the operator, call it as a normal FISH symbol with a split argument. Since it is an operator, the repeated calls will be executed on all available threads.
Note the use of the return statement in the operator. Since an operator is also a global symbol (just like a function), assigning a value to it would require the use of a lock statement and incur significant synchronization overhead. Instead, operators should always use the return statement to return values from the operation. As with all splitting, the return values are collected into a list.
There are occasions when reading and/or writing to a global symbol is unavoidable. In such cases, the fwd statment needs to be used inside an operator to allow a global symbol to be written to. The following example uses a FISH operator to scan the scalar data for values inside a box given by a cartesian extent, and returns the maximum, minimum, accumulated, and last value found in global symbols.
Finally, one of the most common and important uses of FISH operators (and indeed their primary reason for creation) is to use during cycling. Otherwise a single threaded FISH function that checks or changes all objects in a model will easily dominate the run time of the system.
To assign an operator to execute during cycling, use either the fish callback
command or the fish-call keyword to the model solve
command.
Specify the name of the operator, followed by the argument(s) that will be passed for each execution.
Generally the argument assigning the list of objects to iterate over is indicated using a FISH library function, which is not directly recognized by the command processor. This must therefore be indicated using inline fish. The :: prefix must be used to indicate that the argument is to be split, as always (see Splitting). Note that these values are evaluated when the command is processed, and stored for execution during cycling.
An example of this is below:
Was this helpful? ... | Itasca Software © 2024, Itasca | Updated: Sep 26, 2024 |