Chapter 4 Example 4: repetition and library in algorithm
From the principal window,
choose File / Save as
and save your fourth application under your tutorial folder
with the name example4.
4.1 An algorithm with repetition without any library
In this section, we create a multiplication function
of a N elements vector by a scalar
by repeating N times a multiplication function on scalars.
4.1.1 Definition of the scalar ins and the function mul on scalars
In a new algorithm window:
- create a new sensor definition
named ins
with an integer output port o;
- create a new function definition
named mul
with two integer input ports a and b
and an integer ouput port o).
4.1.2 Definition of the vectors inv and outv
To create the vectors:
- to create the definition inv:
- from the algorithm window,
create a new sensor definition named inv,
- from the inv definition window,
type N in the Parameters textfield
of its Definition Properties
(it has N elements),
- create its integer output port named o
with length N: ! int[N] o;
- to create the definition outv:
- from the algorithm window,
create a new actuator definition outv,
- in outv definition window,
type N in the Parameters textfield
of its Definition Properties
(it has N elements),
- create its integer input port named i
with length N: ? int[N] i.
4.1.3 Definition of the algorithm AlgorithmMain1
To create the AlgorithmMain1 algorithm:
- from the algorithm window,
create a new function definition named AlgorithmMain1
and from its definition window, define it as main;
- in the AlgorithmMain1 definition mode:
- create a reference s_input
to the scalar ins,
- create a reference v_input<N>
to the vector inv,
- create a reference mul
to the function mul
and type N in the Repeat textfield
of its Reference Properties
(it is repeated N times),
- create a reference v_output<N>
to the vector outv;
- create dependences between the references,
in order to obtain the main algorithm
(cf. figure 4.2);
- type N in the Parameters textfield
of the Definition Properties of the main algorithm
and 3 in the Values textfield
(cf. figure 4.1).
Notice that this value is keeped
as long as the algorithm remains the main one.
Figure 4.1: Parameters Values |
The repetition consists in multiplying
each of the 3 elements of the v_input vector
with the s_input scalar
and placing the result
in the 3 elements v_output vector.
The parameter N is here the repetition factor of the
mul function.
Figure 4.2: AlgorithmMain1 of the Example 4 |
4.2 An algorithm with repetition with the int library
In this section, we create a multiplication function
of a vector by a scalar by using the int library.
4.2.1 Inclusion of the library int
From the principal window choose File / Specify Library Directories
and add the SYNDEXPATH/libs where SYNDEXPATH
is the absolute path of the SynDEx distribution.
From the principal window,
choose File / Included Libraries / int
(cf. figure 4.3).
Figure 4.3: File / Included Libraries / int |
4.2.2 Definition of the algorithm AlgorithmMain2
Notice that this library
contains input, mul,
and output definitions
parameterized with length.
We will need to set it to 1
for the scalar and the multiplication function,
and to N for the vectors:
- from the algorithm window,
create the function definition AlgorithmMain2
and define it as main;
- in AlgorithmMain2 definition mode:
- drag and drop the sensor definition int/input
from the Definition list
to the AlgorithmMain2 window
→ dialog window: s_input<1>
(cf. figure 4.4)
(it is a scalar),
Figure 4.4: Create Reference to int/input |
- drag and drop the sensor definition int/input
→ dialog window: v_input<N>
(it has N elements),
- drag and drop the function definition int/Arit_mul
→ dialog window: mul<1>
then type N in the Repeat textfield
of its Reference Properties,
(it is a multiplication on scalars, repeated N times),
- drag and drop the actuator definition int/output
→ dialog window: v_output<N>
(it has N elements);
- create dependences between the references
in order to obtain the main algorithm
(cf. figure 4.5);
- type N in the Parameters textfield
of the Definition Properties,
and 3 in the Values textfield.
Notice the difference of the mul reference
when it is seen
from the AlgorithmMain2 definition mode
or from the main mode (Main button).
Figure 4.5: AlgorithmMain2 of the Example 4 |
4.3 An algorithm with repetition with the float library
In this section, we create a multiplication function
of a N*M matrix
by a M elements vector
by repeating N times a multiplication function on vectors.
4.3.1 Inclusion of the library float
Include the library float
(File / Included Libraries / Float).
4.3.2 Definition of the function dpacc
This function is a multiplication function on scalars with an accumulator:
- create a new function definition named dpacc;
- create a reference mul<1>
to the function float/Arit_mul
(the reference works on scalars);
- create a reference add<1>
to the function float/Arit_add
(the reference works on scalars);
- add it three input ports and one output port:
? float s1 ? float s2 ? float acc ! float acc;
- then create dependences to obtain an algorithm
(cf. figure 4.6).
Notice that acc
is an input port and an output port of the function.
It will be used as an accumulator to store the partial sum.
Figure 4.6: Algorithm of the function dpacc |
4.3.3 Definition of the function dp
This function is a multiplication function on vectors with an accumulator:
- create a new function definition named dp;
- add it a parameter dpaccn;
- create a reference zero<{0}>
to the constant float/cst
(it is the {0} scalar);
- create a reference dpacc
to the function dpacc
then type dpaccn in the Repeat textfield
of its Reference Properties
(it is repeated dpaccn times);
- add it two input ports:
? float[dpaccn] v1 ? float[dpaccn] v2
and one output port: ! float dp
(vectors have dpaccn elements);
- create dependences to obtain an algorithm
(cf. figure 4.7).
To build the dependence
between the output port acc of dpacc
and its input port acc,
choose Iterate on the dialog window
(it is the connection between two successive calls of the function).
Figure 4.7: Algorithm of the function dp |
The repetition consists in multiplying
two dpaccn elements vectors
by calling dpaccn times
the dpacc multiplication function
on scalars with accumulator.
The initial value of its accumulator
is given by the zero constant
and the following are given by the accumulator itself.
4.3.4 Definition of the function prodmatvec
This function is a multiplication function of a matrix by a vector:
- create a new function definition named prodmatvec;
- type a;b in the Parameters textfield
of its Definition Properties;
- create a reference dotprod<b>
to the function dp
(input vectors have b elements)
and type a in the Repeat textfield
of its Reference Properties
(it is repeated a times);
- add it two input ports:
? float[a*b] inm ? float[b] inv
and one output port: ! float[a] outv;
- then create dependences to obtain an algorithm
(cf. figure 4.8).
Figure 4.8: Algorithm of the function prodmatvec |
The repetition consists in multiplying
a a*b matrix by a b elements vector
by calling a times
the dp multiplication function on vectors.
4.3.5 Definition of the algorithm AlgorithmMain3
To create the AlgorithmMain3 algorithm:
- create the definition of the sensor inm,
with two parameters names N and M,
and with an output port: ! float [N*M] o;
- create a new function definition named AlgorithmMain3
and define it as main;
- add it two parameters:N;M
with values 3;4;
- create a reference m1<N;M>
to the matrix inm;
- create a reference inv<N>
to the vector float/input;
- create a reference matprodvec<N;M>
to the function prodmatvec;
- create a reference outv<M>
to the vector float/output;
- then create dependences to obtain an algorithm
(cf. figure 4.9).
Figure 4.9: AlgorithmMain3
of the Example 4 |
From the principal window,
choose File / Close.
In the dialog window, click on the Save button.