User Tools

Site Tools


tutorial_external_function

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
tutorial_external_function [2017/04/19 12:44]
waruna [Example]
tutorial_external_function [2023/05/30 07:37] (current)
lnarmour writeC and verification shouldn't be used together
Line 23: Line 23:
 ====Example==== ====Example====
 Let's take a simple example with two external functions. One is a function that computes square root, and another is some user function that returns float given an integer. Let's take a simple example with two external functions. One is a function that computes square root, and another is some user function that returns float given an integer.
-<sxh alphabets; gutter:false+<sxh alphabets; gutter:true
-float sqrt(float); +double sqrt(double); 
-float userfunc(int);+double userfunc(int);
  
 affine exFuncExample {N|N>0} affine exFuncExample {N|N>0}
 given given
- float A {i|0<=i<N};+ double A {i|0<=i<N};
 returns returns
- float C {i|0<=i<N};+ double C {i|0<=i<N};
 through through
- C[i] = sqrt(A[i]) + userfunc([i]); //  Note that userfunc is a unary pointwise op and so its argument+ C[i] = sqrt(A[i]) + userfunc([i]); // Note that userfunc is a unary pointwise op and so its argument
                                            // must be an alphabets expression, and we are constructing one with                                            // must be an alphabets expression, and we are constructing one with
                                            // the IndexExpr rule of the grammar.                                            // the IndexExpr rule of the grammar.
Line 40: Line 40:
  
 Generate code with the following script. Generate code with the following script.
-<sxh cs; gutter:false>+<sxh cs; gutter:true>
 prog = ReadAlphabets("exFuncExample.ab"); prog = ReadAlphabets("exFuncExample.ab");
  
 system = "exFuncExample"; system = "exFuncExample";
  
-generateWriteC(prog, system, "./"+system);+generateScheduledCode(prog, system, "./"+system);
 generateWrapper(prog, system, "./"+system); generateWrapper(prog, system, "./"+system);
 generateMakefile(prog, system, "./"+system); generateMakefile(prog, system, "./"+system);
 </sxh> </sxh>
  
-Along with the two C codes for ''WriteC'' and ''Wrapper'', you will find a header file, ''external_functions.h'' with the following contents. +Along with the two C codes for ''ScheduledCode'' and ''Wrapper'', you will find a header file, ''external_functions.h'' with the following contents. 
-<sxh c; gutter:false>+<sxh c; gutter:true>
 //External functions //External functions
-/***PROTECTED REGION ID(external_functions) ENABLED START***/ +double sqrt(double); 
-float sqrt(float); +double userfunc(int);
-float userfunc(int); +
-/***PROTECTED REGION END***/+
 </sxh> </sxh>
-Code surrounded by PROTECTED REGION is generated when the file or the protected region does not exist, but will be unchanged in subsequent generations.\\+Code is generated when the file does not exist, but will be unchanged in subsequent generations.\\
 \\ \\
 Now we need to provide implementations for these functions. The first function is actually in the C math library, which is linked by default. Thus we do not need to add body for this function. However, the declaration here will cause conflict with the other declaration of ''sqrt'', so this declaration needs to be commented out.\\ Now we need to provide implementations for these functions. The first function is actually in the C math library, which is linked by default. Thus we do not need to add body for this function. However, the declaration here will cause conflict with the other declaration of ''sqrt'', so this declaration needs to be commented out.\\
 \\ \\
 The other function needs a body, and it can be anything as long as it returns a floating point value. An example of the completed header file is shown below. The other function needs a body, and it can be anything as long as it returns a floating point value. An example of the completed header file is shown below.
-<sxh cttpp; gutter:false>+<sxh c; gutter:true>
 //External functions //External functions
-/***PROTECTED REGION ID(external_functions) ENABLED START***/ +//double sqrt(double); 
-//float sqrt(float); +double userfunc(int in) {
-float userfunc(int in) {+
    return -sqrt(in);    return -sqrt(in);
 } }
-/***PROTECTED REGION END***/ 
 </sxh> </sxh>
 Now try compiling and check if the code works! Now try compiling and check if the code works!
tutorial_external_function.1492627497.txt.gz · Last modified: 2017/04/19 12:44 by waruna