Skip to content

Predefined Functions

Adrian edited this page Sep 13, 2021 · 1 revision

Predefined Functions

To support additional features certain functions are predefined within the Parallel Pattern Language.

List initialization

List can be initialized be explizitly providing all elements for the list. This is especially problematic for large lists, therefore, the function init_List can be used to initialize the list with a certain shape. Additionally, it is possible to define the default element for the list in the second argument, but it is not necessary. The following example initializes 3 lists with the init_List function. The variable vector is initialized as a list of Integers with 1000 elements. The variable matrix is initialized as a two dimensional list of integers (a matrix) with 1000 row and 200 columns. The variable ones is initialized as a list of integers with 10 elements, where each elements has a value of 1.

seq main () : Int{
var [Int] vector = init_List([1000])
var [[Int]] matrix = init_List([1000 ,200])
var [Int] ones = init_List([10] ,1)
return 0
}

List Concatenation

A function taking two lists as arguments and returns the list resulting, when concatenating the two arguments as a new list. The function concat take the list vector1./i> and vector2./i> as arguments. The concat function copies the values of vector1./i> and stores them in vector3./i>. Afterwards, the values of vector2./i> are appended to vector3./i>.

seq main () : Int{
var [Int] vector1 = init_List([1000])
var [Int] vector2 = init_List([1000])
var [Int] vector3 = init_List([2000])

vector3 = concat(vector1, vector2)

return 0
}

List Copy

A function taking a lists as arguments and returning a copy of that list. The function concat take the list vector1 and vector2 as arguments. The copy function copies the values of vector1 and stores them in vector3.

seq main () : Int{
var [Int] vector1 = init_List([1000])
var [Int] vector2 = init_List([1000])

vector2 = copy(vector1)

return 0
}

IO-Functions

Input

The read function allows the user to read potentially multi-dimensional lists from a file to a variable. Since, the static analysis needs information on the shape of the variable, it is necessary to properly instantiate the variable beforehand e.g. using the init_List function discussed earlier. The previous instantiation allows for a correctly work static analysis and optimization, as well as mitigating errors while generating the parallel patterns. The following example shows the variable readFromFile, which is a list of 1000 Integers (Due to the initialization with init_List). Thus, the list being read from exampleDirectory.csv to readFromFile also has to contain exactly 1000 Integer elements, to ensure a correct static optimization and parallel pattern generation.

seq main () : Int{
var [Int] readFromFile = init_List([1000])
readFromFile = read("exampleDirectory.csv")

...

return 0
}

Output

Console

The output is handled by the print function. The print function prints its parameters as console output. After each print a new line is started. It is also possible to print the results of expressions and String in the print statement. To print the value of an expression it needs to be enclosed in curly brackets. It is not necessary to include comata between the arguments. The following example prints 3 lines on the console window with addition expressions to evaluate in the second two print expressions.

seq main () : Int{
print("Results:")
print("Your Score: " {getPersonalScore()} " You have passed.")
print("avg Score: " {getAverageScore()})
return 0
}

Output:

Results:
Your Score: 79 You have passed.
avg Score: 50

File

A differnt method to generate output is the write function. The write function prints its second parameters to the file specified in the first parameter. The printed values are defined the same way as for the console output, as seen above. The following example writes the exact same statements as the print call from the example above into the file "example.txt".

seq main () : Int{
write("example.txt", "Results:")
write("example.txt", "Your Score: " {getPersonalScore()} " You have passed.")
write("example.txt", "avg Score: " {getAverageScore()})
return 0
}

example.txt:

Results:
Your Score: 79 You have passed.
avg Score: 50

Timer

A function to take the current system time is not yet implemented.