hArtes es rtes hArt hA SCILAB2C Requirements Raffaele Nutricato PoliBa 1 hArtes es rtes hArt hA Agenda • Open Issues • SCILAB2C specifications and limitations 2 hArtes es rtes hArt hA Open Issues: I/O (Prof. Piazza) We need specifications concerning the input and output data management. Two approaches can be adopted: 1. Input/Output files SCI2C will support file management functions 2. Input/Output buffers SCI2C will not support file management functions. 3 hArtes Open Issues: Complex Numbers es rtes hArt hA • Need to define an implementation for complex numbers… • For now we have a strong interface (but it may decrease efficiency) 4 hArtes Open Issues: Complex Numbers es rtes hArt hA ************ We proposed 2 implementations: ************ • Standard C-99 : #include <complex.h> […] double a=1.0; double b=2.0; float c=3.0; float d=4.0; double complex z = a+b*I; float complex w = c+d*I; • Hand made Complex structure with strong interface that hide the real implementation : (Only given as example, must be improve for efficiency) #include <doubleComplex.h> #include <floatComplex.h> […] double a=1.0; double b=2.0; float c=3.0; float d=4.0; doubleComplex z = DoubleComplex(a,b); floatComplex w = FloatComplex(c,d); 5 hArtes Open Issues: Complex Numbers es rtes hArt hA ******* Implementation depends on compilation options ******* /* ** \function DoubleComplex ** \brief construct a Double Complex . */ doubleComplex DoubleComplex(double real, double imag) { doubleComplex z; #ifndef STDC99 z.real = real; /* ** Hand made Double Complex definition ** { */ struct double_complex { double real; double imag; }; typedef struct double_complex doubleComplex; /* ** } */ z.imag = imag; #else z = real + I * imag; #endif return z; } /* ** Standard C99 Complex ** { */ #include <complex.h> typedef double complex doubleComplex; /* ** } */ But we may add another implementation if it’s more efficient. /* ** Another Hand made Double Complex definition ** { */ struct double_complex { double z[2]; }; typedef struct double_complex doubleComplex; /* ** } */ 6 hArtes es rtes hArt hA Open Issues: String manipulation • Do we need support for string manipulation? Is it important for DSP? 7 hArtes es rtes hArt hA SCI2C specifications and limitations • Most of the specifications for the SCILAB2C tool come from previous meetings with other hArtes partners (mainly INRIA) • They have been proposed in order to reach the final goal (Dr. Gomez): to produce an optimized code. “Optimized" means that the generated C code does not include any part of Scilab interpreter. So it should be very small and efficient. 8 hArtes es rtes hArt hA SCI2C specifications and limitations: Memory Optimization Optimization of memory usage requires: 1. Function Arguments are passed by reference. 2. No dynamic memory allocation 9 hArtes es rtes hArt hA SCI2C specifications and limitations: Memory Optimization Function Arguments are passed by reference (in Scilab/Matlab they are passed by value). This means that: • the body of function cannot change the input parameter values. or… • a warning can be issued by the SCILAB2C tool when a function input prm is modified by the body of the function so that the user can change the code according to the warning message. Sol.: Ex.: Function c = test(a,b) c = a + b; a = rand(); c = c + a; Warning here Function c = test(a1,b) a = a1; c = a + b; a = rand(); c = c + a; 10 hArtes es rtes hArt hA SCI2C specifications and limitations: Memory Optimization No dynamic memory allocation. This means that: • No dynamic array extension Ex.: A = [A B]; NOT ALLOWED!!!! • No support for Scilab t-list type. • The code must be annotated: users must specify precision and size of data (for local/global vars and function prms) 11 hArtes es rtes hArt hA SCI2C specifications and limitations: Memory Optimization Examples of code annotation: Annotated version //SCI2C: PRECISION float; Non-Annotated version . . . c = foo3(a,b); function y = foo3(x,z) y = abs(x) + det(z); endfunction Annotations avoids dynamic memory annotation + crazy code!!! //SCI2C: a = complex2D(10,15) //SCI2C: b = real2D(10,10) //SCI2C: c = real2D(10,15) . . . c = foo3(a,b); //SCI2C: y.size = x.size //SCI2C: y.type = z.type function y = foo3(x,z) y = abs(x) + det(z); endfunction 12 hArtes es rtes hArt hA Not known the size of the memory that have to be allocated SCI2C specifications and limitations: Memory Optimization Examples of crazy code: Somtimes it can work, but if M = random… c = det(RandomMtx()); c = det(RandomMtx(M)); function B = RandomMtx() M = round(10*rand()); B = rand(M,M); endfunction function B = RandomMtx(M) B = rand(M,M); endfunction Rule: Non-crazy code is code that can be annotated by using relationships as OutPrmSize=fnc(InPrmSize). This ensures that the sizes of matrices can be computed at compile time. A = rand(10,15) C = transpose(A); OK!!! //SCI2C: B.size = [A.size(2), A.size(1)] //SCI2C: B.type = A.type function B = transpose(A) B = A.’; endfunction A = rand(10,10) C = determinant(A); OK!!! //SCI2C: B.size = [1, 1] //SCI2C: B.type = real function B = determinant(A) B = abs(det(A)); endfunction 13 hArtes es rtes hArt hA SCI2C specifications and limitations: Memory Optimization Not known the size of the memory that have to be allocated Examples of crazy code: Not known the size and type of the output matrix c = zeromatrix(2); c = ReferenceMatrix(-11); function B = zeromatrix(a) if (a==1) B = zeros(10,10); end if (a==2) B = zeros(20,20); end if (a==3) B = zeros(30,30); end endfunction function B = ReferenceMatrix(a) if (a==1) B = zeros(10,10); elseif (a==2) B = zeros(20,20); elseif (a==-1) B = ones(10,10); else B = ones(20,20)+%i*ones(20,20); end endfunction Non-crazy code is code that can be annotated by using relationships as OutPrmSize=fnc(InPrmSize) 14 hArtes es rtes hArt hA SCI2C specifications and limitations • Functions with a variable number of input/output arguments. In calling a function, users have always to specify all input/output arguments. Prof. Piazza says that it is a strong limitation. We can check (with INRIA) if it is possible to guarantee variable input/output arguments only for the SCI2C DSP library (not for all the scilab functions written by the user). • Change for the moment… • SCI2C DSP library is the library that INRIA is developing in C. • The SCI2C DSP library will be composed of the following functions:… 15 hArtes es rtes hArt hA SCI2C specifications and limitations Basic Statements Support for: •for •while •if, else, elseif •Complex forms of the for statement. From scilab help: – Simple: for j = 2:3:n-1, a(j,j) = j; end; – Complex: for e=eye(3,3),e,end From Matlab help: Long loops are more memory efficient when the colon expression appears in the FOR statement since the index vector is never created. 16 hArtes es rtes hArt hA SCI2C specifications and limitations Auxiliary functions • find • isempty • isnan • rand • Op: • size • type 17 hArtes es rtes hArt hA SCI2C specifications and limitations DSP functions • FIR • IIR • hilbert • conv • conv2d • cvexp • fft • ifft • levinson • lpc2cep • xcorrc 18 SCI2C specifications and limitations hArtes es rtes hArt hA Elementary functions • acos • acosh • asin • asinh • atanh • cos • cosh • exp • log • log10 • sin • sinh • sqrt • tan • tanh • vatan2 • vexp10v 19 hArtes es rtes hArt hA SCI2C specifications and limitations Matrix Operators eq ne lt gt le ge Relational operators. - Equal == - Not equal ~= - Less than < - Greater than > - Less than or equal <= - Greater than or equal >= and or not Logical operators. - Logical AND & - Logical OR | - Logical NOT ~ Arithmetic operators. plus minus uminus mtimes times mpower power mrdivide rdivide - Plus - Minus - Unary minus - Matrix multiply - Array multiply - Matrix power - Array power - Slash or right matrix divide - Right array divide + * .* ^ .^ / ./ • abs, det, inv, mchol, sum, trace 20 hArtes es rtes hArt hA SCI2C specifications and limitations String handling ??? • part • strcat • strcmp • strindex • strsubst 21 hArtes es rtes hArt hA SCI2C specifications and limitations File Management ??? • mclose • mget • mopen • mprintf • mseek 22 hArtes es rtes hArt hA SCI2C specifications and limitations • Max dimension is 2D • Each source file must contain only one function declaration NO multiple functions can be written into the same file. • N-ary expressions: Expressions as E=A+B+C+D will not be converted as: – E[i] = A[i] + B[i]+ C[i] + D[i]. – They will be converted as: – E = sum(A,sum(B,sum(C,D))); – To force the first implementation the user har to re-write the Scilab code as in the following example: – for i=1:N – E[i] = A[i] + B[i]+ C[i] + D[i] – end – No support for optimizations that are already available in the C compiler. 23 hArtes es rtes hArt hA SCI2C specifications and limitations • DATA TYPES SUPPORTED: – TYPES: Real/Complex – PRECISION: Float/Double – Integer precision is supported only for the counter variables of the “for” loops. – The precision of data can be float or double and it is the same for all the variables (except for counter variables in the “for loops”). 24 hArtes es rtes hArt hA Control of temp variables Support for temp variables generation control (remove dependences + src != dest): • a = (b + c + h) * (d + e); • a = b + c; There are some • a = a + h; mAgic functions • temp1 = d + e; that don’t allow src=dest • a = a * temp1; Or • temp1 = b + c; • temp2 = temp1 + h; • temp3 = d + e; • a = temp2 * temp3; 25 hArtes es rtes hArt hA End Of Presentation Thank you! Raffaele Nutricato 26