Skip to content

Examples

Miguel Ramalho edited this page Sep 16, 2017 · 7 revisions

Examples

Between each example only the lines with comments represent new information from the previous examples.

Filenames for LARA have the .lara extension.

Lat "Hello World"

example01.lara

import lat.Lat; //make Lat accessible in this Lara file

var lat = new Lat("myFirstLat"); //this is just the name that the tuning operation will have
var x = new LatVarList("x", [10, 20, 30]); //a simple LatVarList variable, 'x' should be the name of a variable in your source code
lat.addVariable(x); //add 'x' to the current tuning operation

//LARA select and apply is used to capture a loop joinpoint from your source code
select loop end
apply 
    lat.setScope($loop); //set the lat scope as the loop 
    break; //use just the first loop found, in case there is more than one
end

lat.tune(); //generate the variants by changing x to 10, 20 and 30, and generating a report with the results

Lat with custom CMaker makeCommand

import lat.Lat;

...

lat.loadCMaker(); //loads the default cmaker variable into lat.cmaker
lat.cmaker.setMakeCommand("nmake"); //assume you don't have make installed, but you have nmake
lat.tune(); 

Dependent searchType

import lat.Lat;

...

var x = new LatVarList("x", [10, 20, 30]); //values: default, 10, 20, 30
var y = new LatVarRange("y", 1, 5);        //values: default, 1, 2, 3, 4

//option 1
lat.setVariables([x, y]); //add both variables for the tuning operation
lat.setSearchType(LatConst.SEARCH_TYPE.DEPENDENT);//by default lat.searchType is independent
//option 2
lat.setSearchGroups([[x, y]]); //add both variables for the tuning operation, in a dependent searchGroup

lat.tune(); 

Independent searchType

import lat.Lat;

...

var x = new LatVarList("x", [10, 20, 30]); //values: default, 10, 20, 30
var y = new LatVarRange("y", 1, 5);        //values: default, 1, 2, 3, 4

//option 1
lat.setVariables([x, y]); //add both variables for the tuning operation
//option 2
lat.setSearchGroups([[x], [y]]); //add both variables for the tuning operation, in an independent searchGroup

lat.tune(); 

Lat with OpenMP variables

import lat.Lat;

...

var omp_chunks = new LatVarRange("", 1, 3); //values: default, 1, 2 (no name required)
var omp_threads = new LatVarRange("", 0, 10, 5); //values: default, 0, 5, 10 (no name required)
//create LatVarOmp - schedules dynamic and static
var omp = new LatVarOmp("lat_omp", ["dynamic", "static"], omp_chunks, omp_threads);	

lat.tune(); 

Lat with a lot of configurations specified

import lat.Lat;

...

lat.setNumTests(5); //execute each variant 5 times to get more accurate results
lat.setVerbose(true); //set verbose logs
lat.setOutputFolder("../latResults"); //change OutputFolder
lat.toConfig("latConfig"); //save the configurations in latConfig.json (the extension is optional)
//you can later do lat.fromConfig("latConfig.json) to load the custom values

//since the setters return the lat instance the above code is the same as:
lat.setNumTests(5).setVerbose(true).setOutputFolder("../latResults").toConfig("latConfig");

lat.tune(); 

Lat playing with search groups

import lat.Lat;

...

var x = new LatVarList("x", [10, 20, 30]);
var y = new LatVarRange("y", 1, 5);

var omp_chunks = new LatVarRange("", 1, 3);
var omp_threads = new LatVarRange("", 0, 10, 5);
var omp = new LatVarOmp("lat_omp", ["dynamic", "static"], omp_chunks, omp_threads);	

lat.setSearchGroups([[x]]);
println(lat.countVariants()); //prints: 4

lat.setSearchGroups([[y]]);
println(lat.countVariants()); //prints: 5

lat.setSearchGroups([[omp_chunks]]);//this will not work afterwards because omp_chunks has no name
println(lat.countVariants()); //prints: 3

lat.setSearchGroups([[omp_threads]]);//this will not work afterwards because omp_threads has no name
println(lat.countVariants()); //prints: 3

lat.setSearchGroups([[omp]]);
println(lat.countVariants()); //prints: 8 = 2 * 2 * 2 (the defaults are not considered)

lat.setSearchGroups([[x], [y]]);
println(lat.countVariants()); //prints: 9 = 4 + 5

lat.setSearchGroups([[x, y]]);
println(lat.countVariants()); //prints: 20 = 4 * 5

lat.setSearchGroups([[x, y], [omp], [x]]);
println(lat.countVariants()); //prints: 32 = (4 * 5) + 8 + 4

lat.setSearchGroups([[x, y, omp]]);
println(lat.countVariants()); //prints: 160 = (4 * 5) * 8

lat.tune(); //only uses the last setSearchGroups
Clone this wiki locally