User Tools

Site Tools


doc:lara:intro

Introduction to LARA scripts

LARA scripts can either be plain JavaScript files with the latest EcmaScript syntax (.js files) or EcmaScript 5 (2009) extended with special constructs (.lara files). Currently the LARA framework supports EcmaScript 2021 for JS files.

LARA scripts have been moving away from special constructs (e.g., select, apply) in favor of JavaScript-based APIs (e.g., weaver.Query).

Aspects

Aspects are regions of code where LARA syntax can be used, and are similar to functions. They are also the entry point of a LARA script, the first aspect of the script is executed by default.

Aspectdef definition

// The main aspect is the first declared in the file
aspectdef MyFirstAspect
   // aspect code
end
 
aspectdef ASecondAspect // An aspect that can be called in the main aspect
   // aspect code
end

Aspect Inputs and Outputs

Input parameter definition:

input
    funcs,
    opt
end

Input parameter definition with default values:

input
    funcs = ['kernel'],
    opt = ['unroll', 'interchange', 'tile']
end

Output definition. Output cannot be initialized inside the output block:

output
    optFuncs,
    code
end
 
optFuncs = [];
code = '';

Calling Aspects

Simple aspect call:

call OptimizeProgram();

Calling an aspect with arguments:

call OptimizeFunctions(functions, optimizations);

Calling an aspect with named arguments:

call OptimizeFunctions(opt: optimizations, funcs: ['gridIterate']);

Calling an aspect and retrieving the outputs:

// Current syntax
var optimizer = call OptimizeFunctions(functions, optimizations);
 
// Previous syntax
call optimizer : OptimizeFunctions(functions, optimizations);
 
var changedFuncs = optimizer.optFuncs;
var finalCode = optimizer.code;

Assigning an aspect call to a variable for later use. Can still use input arguments and outputs as shown before:

var optimizer = new OptimizeFunctions(functions, optimizations);
 
call optimizer(); // or optimizer.call()
 
var changedFuncs = optimizer.optFuncs;
var finalCode = optimizer.code;

Aspects can also be called inside Javascript files (.js). You need to build the aspect object and reference the complete path, separated by dollar signs ($):

// Calling aspect MeasureEnergy from inside a .js file
 
(new lara$profiling$Energy$EnergyTest()).call();

Using LARA Actions

Actions are used inside apply statements. There are two default actions, insert and def. Then, it is possible to use weaver-specific actions, which are called with the exec keyword:

select function{'kernel'} end
apply
    insert before '/* Creating a clone. */';  // add a comment before the function
    def name = 'original_kernel';  // redefine the name of the kernel function
    exec clone('cloned_kernel');  // use a MANET-specific action to create a 'kernel' clone named 'cloned_kernel'
end

When calling an action we can specify any join point in the chain to be the action target:

select function.loop end
apply
    $loop.exec tile(8);
    $function.insert before '/* This function was transformed. */';
end

It is possible to omit the target, the last join point in the chain is used:

select function.loop end
apply
    exec unroll(2);  // performed on the same loop as below
    $loop.exec tile(8);  // performed on the same loop as above
end

If the action returns a result, we can use the following syntax:

   var result = $jp.exec <action_name>;

If 'result' is a variable that already exists, 'var' can be omitted:

   result = $jp.exec <action_name>;

The keyword '.exec' can be omitted if you prefix the name of the action with the target join point, and add parenthesis:

  result = $jp.<action_name>();

Calling shell commands

You can invoke shell commands from LARA using the function cmd. The first argument is the command, and the second an array with the arguments for the command:

cmd("ls", ["-l", "-v"]);

Using LARA from the Command Line

Include Folder

To add include folders, use the flag -i. To add more than one folder, use double quotes and separate the folders with the file separator character of your current system

larai -i "libs1;libs2"

Passing Arguments to the Aspect

To pass arguments to the top-level aspect use the flag -av. The input is a JSON representation of the input:

larai -av "{inputFile:'data.json',execute:true,iterations:10}"

Codedef Sections

To insert sections of code that span several lines, you can define codedef sections, which act as templates. Example:

codedef CodeTemplate(param1, param2) %{
// This code is inserted as-is, without escaping
 
// To apply the values of parameters, use [[]]
var [[param1]] = [[param2]];
 
}% end

Declared codedefs can then be used in the code as functions:

code = CodeTemplate("varName", 2);
doc/lara/intro.txt · Last modified: 2022/01/15 21:02 by jbispo