[sac-user] English Documentation for Calling C from SAC

Herhut, Stephan s.a.herhut at herts.ac.uk
Fri Aug 6 16:23:10 CEST 2010


Dear Salem,

unfortunately no such documentation exists at the moment. There is a German master's thesis but that is outdated, too.

We have not tried to use a BLAS C implementation in SAC. However, we have implemented some of the BLAS routines in SAC itself. It would be great to see how the two approaches compare. From our experience, if one compares single primitives, BLAS C implementations will almost always win as they have been tuned to the extreme. The power of an optimising compiler like sac2c only plays out as soon as multiple primitives are called in sequence. Only then additional optimisation potential arises.

You can find our BLAS implementation in the standard library (modules/numerical/blas).

Let my try to give you a brief intro into the C interface. This should suffice to integrate BLAS. As an example, consider the string module in the standard library (modules/structures/String.sac). In that file, the SAC function strtoi is defined as follows:

external int, string strtoi(string S, int BASE);

As the keyword external gives away, the function is not implemented in SAC but some other language. The compiler does not actually care what language that is, as long as it uses C calling conventions. To declare an external functions, one simply provides its signature prefixed with the keyword external.
By just using the line above, the SAC compiler will make some assumptions on how to map the function to an actual C function. Firstly, it will map return values and arguments from the SAC side to arguments only on the C side. This is done to map the multiple return values of SAC to the C word. So, for the above example, the resulting C prototype would be

void strtoi( int *res1, char **res2, char *S, int BASE);

As you can see, the return values have been mapped into reference arguments. Often, this is not desired, like in the example of BLAS where the signatures are fixed. For those cases, we provide a means to remap SAC results/arguments to C results/arguments. For the above example, the following pragma is used in the standard library:

    #pragma linksign [0,1,2,3]

Pragmas in general directly follow an external function declaration. In this case, we use the linksign pragma. It maps each result/argument in SAC to the corresponding C result/arguments by means of a permutation vector. So, to achieve the desired C signature of strtoi, we want to have the first SAC result to be mapped to the C result and the arguments to be mapped in the same order. This is done using the permutation vector [0,1,2,3].  Position zero thereby represents the C function return value and numbers from one onwards are the function arguments. Using that vector, we get

int strtoi( char **res1, char *S, int BASE)

which is the standard C signature for that function. As you might guess, SAC by default uses the permutation vector [1,2,...].

There are some more tuneable settings:

    #pragma linkname " SACstrtoi"

The linkname pragma allows to use a different C function name. Using the above pragma, we would get

int SACstrtoi( char **res1, char *S, int BASE);

Further two pragmas handle linking:

    #pragma linkobj "src/String/strtoi.o"

would instruct the compiler to link the given file whenever the function is required in a program. Similarly

    #pragma linkobj "mylib"

would make the compiler link the final executable against the specified library. The library is looked for in the search path configured in the sac2crc resource file. The default paths for system wide libraries are preconfigured. By editing or creating a .sac2crc file in your home directory and adding the following lines

target default:
EXTLIBPATH       := "path1:path2:pathn"

you can add additional paths to the search. A description of all valid field in that file can be found in its master copy in the setup subdirectory of the sac2c compiler.

Lastly, there is a the

#pragma refcounting [0,1,2]

pragma. This configures whether parameters are passed by value or reference. Per default, all parameters are passed by value and thus are immutable. If you want to modify a parameter, you have to use a slightly more complex calling convention. I will cover that subject as demand arises.

I hope this suffices to get you started. If you have any further questions, please feel free to reply to this list. Please use the list instead of contacting me directly as this will increase the audience and ensure a timely reply

Regards
  Stephan

On 05/08/2010 04:24, "Salem Reyen" <salemreyen at yahoo.com> wrote:

Hi,

  Is there any documentation (in English) about how to call C functions from a SAC Module?  More importantly, has anyone successfully tried to call BLAS/LAPACK (eg, Intel MKL or AMD AML) from SAC?  Any help will be greatly appreciated.

Salem


--
Stephan Herhut
Centre for Computer Science and Informatics Research
Science and Technology Research Institute
University of Hertfordshire

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.sac-home.org/pipermail/sac-user/attachments/20100806/9dd3eb05/attachment-0002.html>


More information about the sac-user mailing list