Debian Codegraph @

external/clapack/READ_ME.TXT

raw
1 Making Clapack 3.1.1.1 ready for Praat
2
3 20200321, 20200417 David Weenink
4
5 I Intro.
6
7 We downloaded the latest C version, 3.1.1.1, of LAPACK from netlib as a compressed
8 clapack-3.1.1.1.tgz file.
9 It contains C versions of everything needed to run the LAPACK routines. All routines have
10 four different versions, i.e. for single precision (float), for double precision (double),
11 for single-precision complex (complex) and for double-precision complex. In general, the names
12 of the routines then start with s, d, c and z, respectively. For example, the routine for
13 singular value decomposition has four varieties: sgesvd.c, cgesvd.c dgesvd.c and zgesvd.c.
14 Because all C versions were machine-translated with a program called f2c from the original Fortran
15 sources, there is an additional collection of small helper files which is in the F2CLIBS directory.
16 A heavy battery of testing code is also available, both for Clapack and for Cblas.
17 After unpacking, the first-level directories are TESTING, INSTALL, F2CLIBBS, BLAS, SRC and INCLUDE.
18 SRC contains the basic Clapack sources; BLAS contains the C version of Blas.
19 BLAS, F2CLIBS and TESTING contain subdirectories.
20
21 II what did we change?
22
23 The Praat version of Clapack is located in external/clapack and has only two subdirectories:
24 external/clapack/lapack and external/clapack/blas. Into these directories all necessary files were
25 copied that are needed to use the double precision LAPACK and Blas versions (mostly the file
26 names that start with a 'd'.)
27
28 We made many syntactic changes in all files (most of them automatically, but many by hand).
29
30 1. We renamed all source files from *.c to *.cpp because we want the C++ compiler to work on them.
31 2. We replaced all 'char*' with 'const char*'. In a couple of routines we therefore needed a const_cast<char *>.
32 (dgesvd, dhseqr, dnormbr, dormlq, dormqr, dormrq, dormtr and dtrtri).
33 3. We placed the contents of all needed F2CLIBS code as static inline code in external/clapack/f2cP.h.
34 4. We removed all 'extern' function definitions from all source files.
35 5. The xerbla routine, which is used in Clapack and Cblas to notice exceptions was replaced by our own version
36 which uses the Praat exception mechanism.
37 6. The malloc(...) and free(...) calls in s_cat were replaced by Melder_malloc and Melder_free.
38 7. We freed the code from all kinds of typedefs that existed in the original f2c.h header file.
39 Thanks to good regular expressions support in kdevelop we could replace
40 'doublereal' by 'double'
41 'logical' by 'bool'
42 'real' by 'float'
43 'ftnlen' by 'integer'
44 'logical (*L_fp)(...)' by 'bool (*select)(const char*, const char*[,const char*])
45 'min/max by 'std::min/max'
46 The rest of the typedefs and defines were not needed.
47 8. The interface files "clapack.h" and "cblas.h" only contain the 'double' interfaces.
48 The interface for the helper routines is in the separate header file external/clapack/clapackP.h.
49
50 The Praat interface to the Clapack code is through dwsys/NUMlapack.h. In this file, only the routines that are
51 directly used in Praat have gotten a simpler C++-like interface. Pointers were removed as much as was possible.
52
53
54
55 III Some remarks.
56
57 -- Change 'doublereal' to 'double' is simple with regex
58
59 Pattern: doublereal
60 Template: \b%s\b
61 Replacement template: %s
62 Replace: double
63
64
65 -- Change 'ftnlen' to 'integer'
66 This could in principle be done as above but we did somewhat more as almost all uses were casts
67 like (ftnlen)2 and (ftlen)3.
68 We changed patterns like (ftnlen)2 and (ftlen)3 to 2_integer and 3_integer, respectively.
69
70 Pattern: \(ftnlen\)2
71 Template: \b%s\b
72 Replacement template: %s
73 Replace: 2_integer
74
75
76 -- Change the 'real' to 'float'; we first did:
77 This was more complicated because we only want to change real types and
78 not the word 'real' in comments!
79 We first changed all 'real' to 'float' as above
80 Pattern: real
81 Template: \b%s\b
82 Replacement template: %s
83 Replace: float
84
85 Next we change back the 'float's in comments which always start with "/*"
86 and end with "*/"
87
88 Pattern: float
89 Template: (^[/*][*].*\b)%s(\b.*[*/]$)
90 Replacement template: \1%s\2
91 Replace: real
92
93 Regular expressions are very powerful. Nowadays computers are very fast.
94 You can change a lot of files very easily and very fast. By using regular expressions
95 you can very easily make a mess of hundreds of files. A 'git reset' is very useful
96 for these cases.
97
98 +++++ Upgrading to clapack version 3.2.1 ++++++
99 To test this version we first had to add the -fno-stack-protector to the CFLAGS. Without this flag the testing routines throw "stack smashing errors" and the testing cannot be completed. Adding this flag made all tests succeed.
100
101 This stack smashing error occured in dlaqr0.c where an "array" dimensioned as char jbcmpz[1] was used as follows:
102
103 char jbcmpz[1];
104 ....
105 if (*wantz) {
106 *(unsigned char *)jbcmpz[1] = 'V'; // illegal write to memory into one byte of the array
107 } else {
108 *(unsigned char *)jbcmpz[1] = 'N'; // illegal write to memory
109 }
110 nwr = ilaenv_(&c__13, "DLAQR0", jbcmpz, n, ilo, ihi, lwork);
111
112 Clearly jbcmpz[1] = .. is outside the boundary. This string is passed as the third argument of the ilaenv_ routine where a strlen is executed on it. So 'jbcmpz' is not properly closed either.
113 To correct these bugs we have to dimension jbcmpz[3] and always close the string with a null byte.
114
115 char jbcmpz[3];
116 ....
117 if (*wantz) {
118 *(unsigned char *)jbcmpz[1] = 'V';
119 } else {
120 *(unsigned char *)jbcmpz[1] = 'N';
121 }
122 jbcmpz [2] = '\0';
123 nwr = ilaenv_(&c__13, "DLAQR0", jbcmpz, n, ilo, ihi, lwork);
124
125 There are more occasions in clapack that don't properly close the third argument passed to ilaenv. In routines dhseqr, dgesvd, dormbr, dormqr, dormlq, dormql, dormqr, dormrq, dormtr, and dormrz, character arrays of dimension 2 are passed without proper closing the string with a null byte. We corrected this too.
126
127 Two other bugs were found by Paul Boersma:
128 The return value in dlamc3_ has to be declared volatile, otherwise the whole routine is optimized away and a wrong result may occur.
129 double dlamc3_(double *a, double *b) {
130 volatile double ret_val;
131 ret_val = *a + *b;
132 return ret_val;
133 }
134 The same of course for the single and complex routines.