tag:blogger.com,1999:blog-12976992471517668142017-04-27T06:04:41.837-07:00GNU Octave SOCIS16 : Improve iterative methods for sparse linear systemsCristiano Dorigonoreply@blogger.comBlogger6125tag:blogger.com,1999:blog-1297699247151766814.post-80540199787064802502016-08-30T09:34:00.000-07:002016-08-30T09:34:48.637-07:00CGS, TFQMR and final revision of all the codesDear all,<br /><br />During these weeks I worked on improving CGS, which I rewrite from scratch. Then I implemented TFQMR, which is not available in Octave. Finally I made a revision on the codes that I improved the past weeks.<br />All the file revisioned (gmres, pcg, bicg, bicgstab) and the new ones (cgs and tfqmr) are available here:<br /><br /> https://CrisDorigo@bitbucket.org/CrisDorigo/socis16-octave-iter_meth<br /><br />in the folder definitive_codes <br /><br /><b>CGS </b><br /><br />As written in my second post, the actual Octave's CGS implementation is not very clear. Indeed I didn't understand<b> </b>most of the code (I was not able to recognize how the is was adapted). Moreover I tested it and the results are not correct (in my second post there is an example of this).<br /><b></b>Then I decided to rewrite if from scratch following closely the algorithm given by Saad in the reference book suggested.<br />I wrote it following the pattern that I used in the other codes (i.e. to wrote three subscripts, each for the different cases: all matrices, all functions handle or mixed). Iit has also the same characteristics of the other methods improved, for example: the approximation returned is the one with the minimum residual, there are five types of flag (0 convergence, 1 maximum number of iterations reached, 2 preconditioner singular, 3 stagnation, 4 division by zero during the process)... I don't go in deep about this details to make less boring this post, since they are already explained in my previous posts. <br /><br /><b>TFQMR</b><br /><br />In my last post I wrote that the mentors told me to implement a new algorithm not available in Octave. Since I had no suggestion about which method write, and the community had no particular needs, I chose the transpose-free qmr algorithm because it is the only methods treated in the Saad book.<br />Also with this algorithm, I use the same pattern of the others (i.e. the three subscripts) and synchronized in such a way it has the same type of input and output as the others.<br /><br />One mention to the number of iterations effectively performed. In the Saad book, the method has different behaviours if the iteration number is even or if it is odd, but at every iteration the approximation is computed. Making some test in Matlab with its tfmqr, I noticed that if the method converges, the vector of the residuals in long two times the number of iteration that Matlab tells that are necessary to convergence. Then I think that Matlab counts as one iteration the block (odd iteration + even iteration), i.e. I think that Matlab_it = odd_Saad_it + even_Saad_it. In this way the iterations performed by Matlab is two times the effective iterations performed. I decided to count the iterations in this way to be compatible.<br /><br /><b>LAST REVISION</b><br /><br />Finally I checked (one more time) the codes that I improved in these weeks (i.e. pcg, gmres, bicg, bicgstab).<br />Indeed I updated the documentation (for example: making all similar, giving more details about the preconditioning, adding the reference book,...).<br />Then I checked the codes and I tried to make it more readable and to adapt it at the Octave conventions.<br />Then I added some tests in all the codes, mostly to check if all the subscripts works (i.e. checked every combination of A matrix/function, M1 matrix/function, M2 matrix/function), but also to check that the flags works well (I checked only the flag 0, 1 and 2, since the others are difficult to reproduce) and also to check that these methods solves also complex linear systems.<br /><br />The mentors suggest me to make a patch with the methods improved (pcg, gmres, bicg, bicgstab and cgs), in such a way it can be easily the review of them and if it is in time (maybe) they can be included in Octave 4.2.<br />This patch is available here<br /><br />https://CrisDorigo@bitbucket.org/CrisDorigo/socis16-octave-iter_meth<br /><br />and it is the file SoCiS16_Improve_iterative_methods.diff<br /><br />I made this patch from the Octave changeset 22406:dc4e2203cd15 of the Octave 4.1.0+ version.<br /><br /><b> </b><br /><b>THANKS TO OCTAVE</b><br /><br /><b> </b><br />Since the official deadline for the SoCiS is tomorrow (the 31-th of August), this is my last post in this blog.<br /><br />To make a small summary, during this summer of codes I make an improving on<br />most of the iterative methods to solve linear systems. In particular I improved pcg, gmres, bicg, bicgstab and cgs.<br />I wrote for all of these methods three subscripts to make them more efficient, dividing the particular cases "matrix", "function handle" and "mixed", in such a way that, according to the input data, the method uses the appropriate subscripts to make computations.<br />I tried to make these methods more compatible with Matlab (for example noticing that Matlab gives as output the approximation with the minimum residual and not the last performed, as used in Octave, or adding the possibility of "flag =2", i.e. singular preconditioner).<br />I tried to fix some "non-clear" situation that I noticed, for example pcg does not noticed if the matrix was not Hermitian positive definite if it was a complex matrix, or to face the problem of a division by zero in bicg, bicgstab and cgs.<br />Then I implemented tfqmr which is available in Matlab, but not in Octave. <br /><br />Unfortunately, there are other two methods that needs an improve (pcr and qmr), but due to unexpected problems with the above methods I had not enough time to improve also these. When my scholastic commitments are not so big, I want to try to improve also these two methods.<br /><br />I want to thank all the Octave community that gives me this fantastic opportunity to face a big project as this and then to learn much theoretically and practical things about these iterative solvers for linear systems.<br />I want to thank specially the mentors, who were always available to give suggestions, advices and ideas of how to proceed in the work, mostly when came some unexpected problems.<br />I hope that this improving will be useful to Octave, and to contact me if there is something not clear or wrong.<br />I also hope to continue to help the Octave community in the following times. <br /><br />One more thanks to all of you.Cristiano Dorigohttps://plus.google.com/103961108712693178106noreply@blogger.com0tag:blogger.com,1999:blog-1297699247151766814.post-36087813917599643342016-08-02T05:52:00.001-07:002016-08-02T05:52:24.338-07:00BICG and BICGSTABDear all,<br /><br />In this last period I worked on the BICG and BICGSTAB algorithm.<br />You can find the codes in the folder bicg_bicgstab at<br />https://CrisDorigo@bitbucket.org/CrisDorigo/socis16-octave-iter_meth<br /><br />Since the main loop of both of them have more or less 30-40 lines of code, I wrote them from scratch, because in my opinion was easier instead to modify and to adapt the original code at the many situations that occurred.<br /><br />The strategy that I used is the same of PCG and GMRES: for each algorithm I wrote three sub-scripts to subdivide the cases which: (a) the matrix and the preconditioners are all matrices, (b) all functions handle or (c) some are matrices and some functions handle.<br /><br />As for PCG and GMRES, I tried to make these codes compatible with Matlab.<br /><br />In my BICG and BICGSTAB codes there are not actually the documentation, but I'll add them as soon as possible<br /><br /><br /><b>Main modifications on BICGSTAB and BICG</b><br /><br />Many of the changes that I applied at the BICGSTAB and BICG were quite similar to those that I made in PCG and GMRES. I make a little list to summarize them: <b> </b><br /><br /><ul><li>Now the approximation of the solution X returned is the one that has the minimum residual and not the last one computed by the algorithm. Also the output variable ITER contains the iteration in which X was computed, not the number of iterations performed.</li><li>In the original Octave BICGSTAB code the stagnation criterion is<br /><i>resvec(end) == resvec(end - 1)</i>. <br />Instead in the BICG code it is: <i>res0 <= res1 (</i>where res1 is the actual residual and res0 the previous)<br />I changed this criterion with: <br /><i>norm(x - x_pr) <= norm(x)*eps <br /> </i>(where x_pr is the previous iteration approximation)<br />This criterion is more efficient, since it checks the variation of the approximations and not the variation of the residual, and it seems more similar to the "stagnation" described in the Matlab help. Moreover in BICGSTAB (and in BICG) there are many fluctuations of the residuals (they are not decrescent in general), so it's better to check directly the approximations.<br />Due to these fluctuactions, the BICG criterion is wrong, since in this manner the Octave BICG usually stops also if it is not necessary.</li><li>I added the "flag = 2" criterion, the one that Matlab describes as "preconditioner matrix ill-conditioned", that in Octave is skipped. The check that I implemented is for the singularity of the preconditioner matrix M, since seems that in Matlab this flag appears only in this situation.</li></ul>Now I will focus on some situations regarding BICGSTAB:<br /><ul><li> I added the "half iterations" as in Matlab: studying the Saad implementation, I noticed that the approximation X is made in two steps at every iteration, indeed it is defined as:<br /><i>x = x + alpha*p + omega*s</i><br />Then to be compatible with Matlab (since p/alpha and s/omega are not related and computed in different situations), I set x = x + alpha*p (and its residual) as the half iteration, and x = x + alpha*p + omega*s as the complete iteration.<br />In this way the "real" total number of iteration performed (recalling that ITER contains the iteration which X was computed) are <br />(length(RESVEC) - 1) / 2. </li><li>There was a problem when the matrices A, M1, M2 and/or the right-hand-side b are complex, indeed many times the residual blow up and the method does not converge. In the Saad reference book (where it was taken the Octave BICGSTAB, I think), the method is formulated only for the real case. The only difference is that using this formulation, some inner product are inverted (e.g. it uses <v, w>, instead of <w, v>). In the real case there is no problem since the inner real product is symmetric, but in the complex not, indeed the result of <v, w> is the complex conjugate of <w, v>.</li><li>In the Matlab help I noticed that there is the possibility of "flag = 4". I tried to reproduce it, then I used the same input data in the Octave BICGSTAB: the approximation X was all composed by NaN, and also for the residuals. Then I study in deep the Octave code and the reference book. I discovered that ih this situation there was some diviosn by zero probably caused by the Non-Symmetric Lanzcos Biorthogonalization (that is used in both BICG and BICGSTAB).<br /> With this Lanczos algorithm is possible the situation of a "serious breakdown" (not going into deep details, but it happens when a certain inner product is zero, instead of a non-zero number), and there is not easy examples to reproduce it. The refernce book suggest two possibility to avoid this situation: to restart the Lanczos biortho, or to use a difficult variant called "Look-Ahead Lanczos". <br />I tried to study the "Look-Ahead" but in the Saad book is only mentioned the general idea, and it is not simple to implement and to understand quickly. Then I tried the second strategy it is not efficient (an easy experiment to try this strategy is: run BICGSTAB to find the last iteration without NaN, then run another time the BICGSTAB with the output X as initial guess), indeed the method does not converge and the residual does not decrease. <br />Then (since in Matlab is not mentioned if it uses or not the Look Ahead) in the BICGSTAB implementation that I wrote I don't use any strategy and I set "flag = 4" when the guilty inner product is zero. <br />To see an example of this situation in the Octave BICGSTAB see the file example_bicgistab.m </li></ul>Instead, during the implementation of BICG there was not (for now) strange situations. For both BICG and BICGSTAB I tried the same tests that are given in their Octave implementations and I add some test to try the "flag =2" and "flag = 4" situations.<br /><br /><b>DURING THE REMAINING TIME</b><br /><br />I know that to fix PCG, GMRES, BICG and BICGSTAB I used a lot of time. This because in their implementation emerged a lot of strange situations that needed a deep revision of these algorithms and many times also a discussion (with the mentors or the community) of how to proceed to solve particular problems. Also the mentors told me that they expected less time/work to improve and make a revision of the codes already implemented.<br />They also told me to spend the remaning time of the SoCiS to implement some methods not present in Octave. For this, I want to ask you if there is one of the suggested algorithms (minres, symmlq, symmetr, tfqmr, bicgstabl or lsqr) that you prefer to implement.<br /><br />As usual, if there is something wrong/not clear, please contact me via e-mail or write a comment.<br /><br /> <b> </b><br /><br /><br /><br /><br />Cristiano Dorigohttps://plus.google.com/103961108712693178106noreply@blogger.com0tag:blogger.com,1999:blog-1297699247151766814.post-59571674556625927882016-07-18T06:58:00.000-07:002016-07-18T06:58:38.191-07:00Pcg and GmresHi all,<br /><br />during these two weeks I tried to fix some problems in pcg and gmres.<br />You can find the codes at <br /><a href="https://bitbucket.org/CrisDorigo/socis16-octave-iter_meth/src" target="_blank">https://bitbucket.org/CrisDorigo/socis16-octave-iter_meth/src</a><br />in the folder "pcg_gmres".<br /><br /><u><b>PCG</b></u> <br /><br /><b>Question on my previous post</b><br /><br />In my previous post I had some problem in pcg with the check if the matrix was or not positive definite. Indeed the check done in the Octave code was not precise, especially if involved complex numbers. (To recall: in the algorithm is computed alpha = (r' * r) / (p' * (A*p)) and the control was: if alpha <= 0, break. The problem is that also if alpha was complex the algorithm must stop, moreover for Octave every complex number is greater than 0, so pcg didn't notice if alpha has an imaginary part).<br /><br />After a (brief) discussion with the Octave community I changed the control on alpha. First of all it checks separately the numerator and the denominator of alpha (because if there is a preconditioner matrix then the definition of alpha "slightly" changes and also the numerator can be negative and/or complex), then the algorithm stops if <br /><br />(abs (imag (num)) >= eps (real (num))*tol/eps) or real (num) <= 0<br /><br />(the same control is made for den).<br /><br />In this way the algorithm stops if really the numerator (or denominator) of alpha is negative or if it has an imaginary part "too big" (this is relative to the tolerance, because we thought that it is not the case to stop the algorithm if the imaginary part is, for example, 1e-16). If this happen then the FLAG output is setted as 4.<br /><br />Unfortunately, alpha can be real and positive at every iteration also if the matrix is not positive definite, so there are cases when this check doesn't works, but there are nothing that we can do.<br /><br /><b>Other changes</b><br /><br />I made other changes to make the code more compatible with Matlab. The most relevant are:<br /><ul><li>Making some tests with Matlab I noticed that the output approximation X is not the last computed, but the one with the minimum residual and the output variable IT gives the iteration which X was computed. The Matlab help is not very explicit about this. I noticed this fact because controlling its outputs, sometimes lenght(RESVEC) is greater than IT, where RESVEC is the vector of the residuals. So at first I thought that there was something wrong (it was strange that there are 10, 20 or 30 residuals more than iterations), then I noticed this "minimum" relation. Then I modified the pcg in such a way also the Octave one has this output.</li><li>I added the check about the stagnation. Indeed there is the possibility that the algorithm stagnates (in general when the (preconditioned) matrix A is not positive definite but the algorithm doesn't notice it). So I add the control:<br /><i>if (norm (x - x_pr) <= eps*norm (x))</i> , where x_pr is the approximation of the previous iteration, then the algorithm stagnates and the FLAG output is setted as 3.</li><li>I added a check to control if the preconditioner matrix is singular, and if this happens then FLAG = 2. To do this, I use a try-catch approach:<br /><i><br />try<br /><br />warning("error","Octave:singular-matrix");<br /><br />. . . (pcg computations)<br /><br />catch<br /> flag = 2;<br />end_try_catch</i><br /><br />In this way the warning "Octave:singular-matrix" is setted as an error, and with the try-catch I control if this error appears.<br />I used this approach because in Matlab and Octave there are different results when we try to solve M \ b, with M singular: <br />In Matlab there is always a warning and the output has some NaN or Inf;<br />I Octave the warning appears only the first time that we try to solve it and the output is the least squares minimization.<br />Unfortunately, since in Octave this warning appears only the first time, if we use pcg for a second time (without quitting Octave) with the same linear system with singular M, then FLAG = 2 is not setted.<br />There is another case where this approach doesn't work: when the matrix is setted as "Diagonal Matrix" then this warning doesn't appear. For example:<br /><br /><i>octave-cli:3> M1 = eye(3);<br />octave-cli:4> M1(3,3) = 0<br />M1 =<br /><br />Diagonal Matrix<br /><br /> 1 0 0<br /> 0 1 0<br /> 0 0 0<br /><br />octave-cli:5> M2 = [0 0 0;0 1 0; 0 0 1]<br />M2 =<br /><br /> 0 0 0<br /> 0 1 0<br /> 0 0 1<br /><br />octave-cli:6> M1 \ ones(3,1)<br />ans =<br /><br /> 1<br /> 1<br /> 0<br /><br />octave-cli:7> M2 \ ones(3,1)<br />warning: matrix singular to machine precision<br />ans =<br /><br /> 0<br /> 1<br /> 1</i><br /><br />In this case M1 and M2 are different singular matrices, but if I try to solve a linear system with them there is a warning only with M2. </li><li>I setted the output flag as in Matlab:<br />FLAG = 0 : algorithm converged<br />FLAG = 1 : maximum number of iterations reached<br />FLAG = 2 : preconditioner matrix is singular<br />FLAG = 3 : stagnation<br />FLAG = 4: (preconditioned) matrix A not positive definite<br /><br />About FLAG = 2 and FLAG = 4:<br /><br />"FLAG = 2": the Matlab help tell that this flag appears when M is ill-conditioned, but the only case in which I can reproduce this flag is when M is singular. But singular and ill-conditioned matrix are different concepts, (ill-conditioned when the condition number is high, singular when det(A) is zero). Indeed, for example, Hilbert matrices are ill-conditioned but not singular. Since in Matlab I had FLAG = 2 only when M singular, in the code and in my pcg help is written that this flag appears only when M is singular, and not when M ill-conditioned.<br /><br />"FLAG = 4": Also with this flag Matlab is not very clear. In the help is written that this flag appears when the algorithm can not continue due to some values too big or too small. But I can reproduce it only when A is not positive definite, so in my pcg help I wrote that it appears only when the algorithm detect that A is not positive definite.</li><li>I fixed the output strings that appears when there are asked less than 2 outputs and I wrote them in such a way they are similar to the Matlab ones.</li><li>I update the pcg help, in such a way it is synchronized with these changes.</li></ul><br /><b> GMRES</b><br /><br />More or less I made the same changes of the pcg algorithm also in gmres (the discussion about the positive definitiveness of the matrix doesn't care here, since the gmres algorithms must works with any matrix, so there are 3 types of flag and not 4 as in the pcg). <br />One little things about the variable IT. In both Matlab and Octave, there are written in the help that in the variable IT are memorized the inner and the outer iterations, but they are not so clear about what they are. For this reason in the help I added a little deep explanation about what they are and how they are related with the total number of iterations. In particular the inner iterations are the iteration before that the restart is applied, instead the outer iterations count how many times the restart is applied. Then the total number of iterations are:<br />(Outer Iterations)*RESTART + (Inner Iterations).<br /><br /><br />I think that the most of the work about these two algorithms is done. Up to small changes and/or bugs that I can find trying my codes I think that they are more or less definitive.<br /><br />If you find some bugs, you find something not clear, or have some advices please contact me. <br /><br /><br /><br />Cristiano Dorigohttps://plus.google.com/103961108712693178106noreply@blogger.com0tag:blogger.com,1999:blog-1297699247151766814.post-58791528167390202332016-07-03T02:59:00.002-07:002016-07-03T02:59:58.427-07:00Pcg analysisDear all,<br /><br />In this second and half week I worked on the pcg to try to improve it.<br />All the codes can be found at <br /><a href="https://bitbucket.org/CrisDorigo/socis16-octave-iter_meth/src" target="_blank">https://bitbucket.org/CrisDorigo/socis16-octave-iter_meth/src</a> <br /> in the folder "pcg_codes". <br /><br />The first thing that I made is the private function __checkandstring__ (the name is not definitive).<br />This function checks if the input argument A, b, M1 and M2 are consistent, i.e. if A, M1, M2 are matrices or function handle, and if A is a matrix, checks also that the number of columns of A are equal to the number of rows of b.<br />In addition, this function has as output two strings: is_prec and type.<br />The first one can be "prec" if M1 is not empty, or "no_prec" otherwise (I check only M1 because in the pcg documentation is written that if M1 is empty then no preconditioner is applied).<br />Instead the string type can be "matrix", "function_handle" or "mixed". If M1 empty then type is "matrix" if A is a matrix, "function handle" otherwise.<br />If M1 is not empty then the string type is "matrix" if A, M1, M2 matrices, "function_handle" if they are all function handle or "mixed" if not all of them are matrices or function handle.<br />I made this function as general as possible to use it also for the other algorithms that need an improvement in my project. <br />You can see the code in the function __checkandstring__.m. <br /><br />After that I take the pcg Octave code and I adapted it to the different situation:<br />I made a switch-case that treat differently the three cases mentioned before ("matrix", "function handle" and "mixed"). Moreover the cases "matrix" and "function handle", have an if block that check if there exist the preconditioners: if they are not passed, then applies the unpreconditioned conjugate gradient, otherwise applies the preconditioned one.<br />For the "mixed" case instead, differently from my previous post (in which I said that if A, M1 and M2 are not of the same type, then I set them as functions handle) I check every time the type of A / M1 / M2 and if it is a matrix then I apply the simple matrix-vector product, if it is a function handle then I made the evaluation.<br />You can see this code in the function pcg_tmp_02.m.<br /><br />I made this choice because I noticed that it is more (time) efficient than to transform all in functions handle. Indeed, before the two codes mentioned above, I made a function similar to __checkandstring__ that in case A, M1 and M2 not of the same type then transform them all in function handle, and then I made a version of pcg that works with this function (you can see these code in the files __check02__ and pcg_tmp_01 respectively). Then I tested the octave pcg, pcg_tmp_01 and pcg_tmp_02 20 times on the same example, with and without preconditioners, and passing all the variables as matrices, as function handles and not all of the same type. At the end I plotted the times and I compared it and I noticed that the better performs are given form pcg_tmp_02.<br />You can see this example in the file example_pcg.m.<br /><br />Talking with the mentors, they told me that (using as example pcg_tmp_02) the code is too long. This because the pcg code is "repeated" 5 times:<br /><ul><li>matrix case without preconditioners</li><li>matrix case with preconditioners</li><li>function handle case without preconditioners</li><li>function handle case with preconditioners</li><li>mixed case (it is only with preconditiones because otherwise there is only A and it is or a matrix or a function handle)</li></ul>They suggested me to substitute this switch-case approach with another approach, for example to use nested function or to insert some scripts in the private folder. They told me to ask to all the maintainers which is your preferred approach or to suggest me another one in case.<br /><br /><b>Complex case</b><br /><br />Making some tests with complex matrices, I noticed strange behaviour of the pcg. <br />For example if I test the Octave pcg with the matrix<br /><br />A = [2 , 2+1i, 4;<br /> 2-1i , 3, 1i;<br /> 4 , -1i , 1 ] <br /><br />that is an hermitian matrix, but not positive definite (indeed its eigenvalue are not all positive: -3.16512506440744e+00 2.85307928516935e+00 6.31204577923809e+00 )<br />and with right-hand-side<br /><br />b = [ 5.59693687377178e-01;<br /> 2.42878791850453e-02;<br /> 9.77795880620014e-01 ]<br /><br />the Octave pcg doesn't notice that this matrix is not positive definite.<br />Indeed the result is:<br /><br />x = pcg(A,b)<br />warning: pcg: maximum number of iterations (3) reached<br />warning: the initial residual norm was reduced 2.12736e+15 times.<br />x =<br /><br /> 2.04219909327648e-01 - 3.21781116721895e-02i<br /> -1.11630690554651e-01 + 5.38299446813016e-02i<br /> 1.07086298628122e-01 + 1.70817561341068e-02i<br /> <br />Studying the code, I notice that the only check for the positive definitiveness of the matrix is on alpha. Not going into deep details, this alpha is necessary to compute the approximant solution at every step (indeed at every iteration, x = x + alpha*p for a certain p).<br />If there are no preconditioners (as in our example) alpha is defined as:<br />alpha = < r, r > / < p, A*p ><br />(where < , > is the vector scalar product, and r is the residual of the previous iteration).<br />Then, since we suppose that A is positive definite, alpha must be real and positive (both in case A real or complex matrix).<br />Since the numerator is positive for every r, if alpha is negative then surely A is not positive definite. Indeed the check in the Octave pcg code is:<br /><br /> if (alpha <= 0.0)<br /> ## Negative matrix.<br /> matrix_positive_definite = false;<br /> endif<br /><br />If A is not positive definite then < p, A*p> is not always negative, so if we are "lucky", if we pass a not positive definite matrix in pcg and from the computation alpha is positive at every iteration, there is nothing that we can do.<br />But if we use A and b definite as previous, pcg makes three iterations and the alpha are:<br /><br />alpha = 2.11061741184511e-01 - 6.08511684384459e-20i<br />alpha = -5.59991838158184e-01 + 1.05798887633281e-17i<br />alpha = 1.48434184396776e-01 + 8.13133738226767e-18i<br /><br />(the function pcg01.m in the folder "pcg_codes" is the octave version of pcg that print at every iteration alpha) <br /><br />We notice that these three alpha have an imaginary part, but it is under the machine precision, so they are not computationally relevant.<br />But the second alpha is (obviously) negative, and the pcg doesn't notice it!<br /><br />This because for Octave: <br /><br />octave4.1-cli:29> -5.59991838158184e-01 + 1.05798887633281e-17i < 0<br />ans = 0<br /><br />and<br /><br />octave4.1-cli:30> -5.59991838158184e-01 + 1.05798887633281e-17i > 0<br />ans = 1<br /><br />Making some tests I notice that a number with an imaginary part is greater than 0, also if this imaginary part is negative, indeed<br /><br />octave4.1-cli:31> -3-1i > 0<br />ans = 1<br /><br />I don't understand very well what Octave do to compare two complex numbers (I think that it compares the modules, i.e. if we want to verify if a < b, where at least one of a or b is complex, then it compares abs(a) and abs(b), but I'm not sure).<br />I compared some complex numbers also in Matlab, and I think that it compares only the real part (and "forgetting" the imaginary part).<br /><br />I talked with the mentors about this fact, but there are some questions:<br /><ul><li>Is it correct to check only the real part of alpha?</li><li>Also if the imaginary part of alpha is relevant? <br />(if for example we use the matrix <br /><br />A = [ 0.71290 + 0.59353i 0.97470 + 0.36591i 0.50060 + 0.53652i;<br /> 0.37411 + 0.11662i 0.38904 + 0.43489i 0.03555 + 0.23431i;<br /> 0.35482 + 0.23601i 0.44859 + 0.31402i 0.54356 + 0.72676i]<br /><br />that is a matrix not symmetric, and not positive definite,<br />and b = ones(3,1), we obtain as alpha:<br /><br />alpha = 0.47882 - 0.51052i<br />alpha = 1.4276 - 1.0314i<br />alpha = 0.43535 - 0.36971i<br /><br />and the pcg doesn't give any flag of this "non-correctness" of A, but also if we check only the real part of alpha the code doesn't give any flag or any error)</li><li>Do we break the algorithm if alpha is complex? Also if the imaginary part is under the machine precision? <br />(it is possible that also if A is positive definite and symmetric but the alpha are complex, because of computations and the machine precision)</li></ul>The mentors suggested me two things:<br /><ul><li>First of all to check both numerator and denominator of alpha: this because if there are preconditioners also the numerator can be negative, since it becomes <M*r, r>, where M is the inverse of M1*M2.</li><li>To break the algorithm if the imaginary part of (the numerator or the denominator of) alpha is relevant. But, how to decide if it is relevant?<br />For example, 1e-15 is relevant? And 1e-14?<br />So they suggested me a possible criterion to use to determine the relevancy of this imaginary part:<br /><br />if real(alpha) + (eps / tol)*imag(alpha) == real(alpha)<br /> break<br />endif <br /><br />The motivation of this criterion is because, since we want to find a solution within a tolerance (tol), we "normalize" the relevancy of this imaginary part with this tolerance. </li></ul><ul></ul>The mentors also told me to ask also to all the maintainers, because this is their suggestion, but can be different from the direction that wants the Octave community.<br />Then I please ask from the community some advices of how to proceed for this situation. <br /><br />Actually (and until I'll have some advices) I'm working on the gmres, and I'll try to improve it in the same manner of the first part of this post for the pcg.<br /><br />Please contact me with some feedback via e-mail at cristiano.dorigo@hotmail.it or as a comment under this post.Cristiano Dorigohttps://plus.google.com/103961108712693178106noreply@blogger.com0tag:blogger.com,1999:blog-1297699247151766814.post-66845988482855294502016-06-20T06:45:00.000-07:002016-06-20T06:45:09.262-07:00First week - Studying codes and some problemsDear all,<br /><br />In this first and half week, as written in the first post, I studied the algorithms and also their codes in Octave. I recall that the algorithms are gmres, pcg, bicg, bicgstab, qmr, cgs and pcr.<br /><br />The codes that I used that are not in Octave can be found at<br /><a href="https://bitbucket.org/CrisDorigo/socis16-octave-iter_meth">https://bitbucket.org/CrisDorigo/socis16-octave-iter_meth</a><br />in the folder "First_week". <br /> <br /><br /><b>Octave codes</b><br /><br />Studying the implementations I noticed problems in some of them:<br /><br /><ol><li><b>pcg - </b>Trying to solve the simple problem 1*x = 1, with initial guess 1, there is the following output:<br /><br />octave4.1-cli:29> x = pcg(1,1,[ ],[ ],[ ],[ ],1)<br />warning: division by zero<br />warning: called from<br /> pcg at line 367 column 10<br />pcg: converged in 0 iterations. pcg: the initial residual norm was reduced NaN times.<br />x = 1<br /><br />We can see that in 0 iteration there is a warning for a division by zero and that the residual is reduced NaN times. Then this output text needs to be fixed.</li><li><b>bicg - </b>I tried to solve the small linear system A*x = b, with <br />A = [7 7 8 4 4; 7 0 6 3 4; 3 2 3 7 6; 6 0 9 7 7; 1 0 0 1 7];<br />b = sum(A,2);<br />(I obtained this matrix with floor(10*rand(5)) so it is a simple integer matrix, and b computed as the sum of the columns of A in such a way the exact solution is the vector x = [1; 1; 1; 1; 1])<br />With this configuration I discovered that the algorithm stops before the convergence, because of stagnation. Indeed the output is:<br /><br />octave4.1-cli:39> [x, flag, relres, iter, resvec] = bicg(A,b)<br />bicg stopped at iteration 4 without converging to the desired tolerance 1.000000e-06<br />because the method stagnated.<br />The iterate returned (number 3) has relative residual 3.772464e-02<br />x =<br /><br /> 1.67650<br /> 1.04764<br /> 0.47523<br /> 0.92726<br /> 0.97121<br /><br />flag = 3<br />relres = 0.038052<br />iter = 4<br />resvec =<br /><br /> 0.070581 0.042765 0.037725<br /><br />As we can see the last residual (relres = 0.038052) is greater than the third (that is the third component of resvec, i.e. 0.037725). Then the algorithm detect a stagnation and stops.<br />I tried to solve the same linear system with Matlab, and I noticed that also its bicg has the fourth iteration with the residual greater than the third one, but it doesn't stop and at the fifth iteration it converges at the solution.<br />So I tried to comment the part of the stagnation (i.e. lines from 187 to 195 in the Octave's bicg) and tried to solve this linear system, obtaining:<br /><br />octave4.1-cli:41> [x,flag,relres,iter,resvec] = bicg_1(A,b)<br />bicg stopped at iteration 5 without converging to the desired tolerance 1.000000e-06<br />because the maximum number of iterations was reached. The iterate returned (number 5) has relative residual 3.371209e-14<br />x =<br /><br /> 1.0000<br /> 1.0000<br /> 1.0000<br /> 1.0000<br /> 1.0000<br /><br />flag = 1<br />relres = 3.3712e-14<br />iter = 5<br />resvec =<br /><br /> 0.070581 0.042765 0.037725 0.038052<br /><br />Except that an error text that says that the maximum number of iterations was reached, we can see that at the fifth iteration the algorithm converges with tolerance smaller than the default one 1e-6.<br />The bicg with the "stagnation" commented is the file bigc_1.m, instead the matrix A and the test with bigc and bicg_1, can be found in example_bicg.m, in the folder First_week .</li><li><b>cgs - </b>I studied the algorithm from the Saad's book, and then I tried to study the codes, but I noticed that its Octave implementation was not taken from this book. Since in the help there is not the reference book I can't compare this implementation with the reference. <br />I also tried to solve the same linear system discussed in the bicg section, and I noticed that the algorithm does not converge. The obtained output is:<br /><br />octave4.1-cli:56> [x,flag,relres,iter,resvec] = cgs(A,b)<br />x =<br /><br /> 0.9799298<br /> -0.0060874<br /> 0.7904128<br /> 0.6945865<br /> 1.8885042<br /><br />flag = 1<br />relres = 0.17700<br />iter = 5<br />resvec =<br /><br /> 1.000000<br /> 0.070581<br /> 0.129415<br /> 0.158692<br /> 0.159693<br /> 0.177003<br /><br />We can see that the solution given by the algorithm is far from the exact solution, also the flag equal to 1 indicates that the algorithm reached the maximum number of iterations without convergence.<br />Then I tried to make a simple and basic implementation from scratch using the formulation in the Saad book, I tried to solve the linear system with this implementation and this implementation converges. The output is:<br /><br /> octave4.1-cli:58> [x,resvec] = cgs_1(A,b)<br />x =<br /><br /> 1.0000<br /> 1.0000<br /> 1.0000<br /> 1.0000<br /> 1.0000<br /><br />resvec =<br /><br /> 5.1604e+01<br /> 3.1724e+00<br /> 3.3616e+00<br /> 1.4778e+00<br /> 3.0564e+00<br /> 3.6586e-10<br /><br />To be sure that is not a pathological case I tried to solve with the Octave implementation and mine the linear system A*x = b, with<br />A = [6 6 8 7 2; 5 4 1 0 3; 4 1 2 6 3; 6 9 3 8 3; 3 5 6 9 4];<br />b = sum(A,2);<br />obtaining similar results.<br /><br />Then, after a discussion with the mentors, we decided that maybe it is easier to implement this method from scratch, instead to search the problems, due to the fact that there is no reference for its implementation.<br />My simple and basic cgs implementation is the file cgs_1.m, and the test with the second matrix can be found in the file example_cgs.m, in the folder First_week.</li><li><b>bicgstab - </b>I noticed that in Matlab are displayed also the "half iteration" with the "half residual", but not in Octave. Then talking with the mentors we decided to display them also in Octave.</li></ol><br /> <b>Matrix-vector product in the algorithms</b><br /><br />Since all of the algorithms of the project are solvers for linear systems, the matrix-vector product is essential in all of their implementation, but since the matrix A and the (eventual) preconditioners can be passed as matrices or as function handle, it is not easy to decide how to compute all these matrix-vector products.<br /><br />For example in the gmres algorithm the strategy is to transform A, M1, M2 into function handle, i.e. (tanking A as example) to create the function handle<br /><br />Ax = @(x) A*x; if A is a matrix<br />Ax = A; if A is a function handle<br /><br />and then every time that there is a matrix-vector product, for example A*v, it is simply computed as Ax(v) or feval(Ax, v).<br /><br />Instead, in the pcg implementation, every time that there is a matrix-vector product it is checked if A is a matrix or a function handle. For example if we need to compute A*v then there are the following lines of codes:<br /><br />if (isnumeric(A))<br /> w = A*v;<br />else<br /> w = feval(A,v);<br />endif<br /><br />(the other algorithms use one of these two strategies, or similar variations).<br /><br />Then it is natural the question: which is the best strategy?<br />To answer, I simulate these strategy and the simple matrix-vector product, then I computed the times, using sparse matrices of different dimensions (n = 100,<br />n = 200, n = 500, n= 1000, n = 2000) in this way <br /><br /> A = sprand(n, n, rand(1));<br /> Ax = @(x) A*x;<br /> w = rand(length(A),1);<br /><br /><span style="color: lime;"> <span style="color: #93c47d;"># Estimation of the simply A*w</span></span><br /> tic<br /> for j =1:100<br /> u = A*w;<br /> endfor<br /> t1 = toc;<br /><br /> <span style="color: #93c47d;"># Estimation of feval(Ax, w)</span><br /> tic<br /> for j = 1:100<br /> u = feval (Ax, w);<br /> endfor<br /> t2 = toc;<br /><br /><span style="color: #93c47d;"># Estimation of the if block with A as matrix</span> <br /> tic<br /> for j = 1:100<br /> if (isnumeric (A))<br /> u = A*w;<br /> else<br /> u = feval(A,w); <br /> endif<br /> endfor<br /> t3 = toc;<br /><br /><span style="color: #93c47d;"># Estimation of the if block with Ax the function handle </span><br /> tic<br /> for j = 1:100<br /> if (isnumeric (Ax))<br /> u = Ax*w;<br /> else<br /> u = feval(Ax,w); <br /> endif<br /> endfor<br /> t4 = toc;<br /><br />obtaining (for example, since I used random matrices) as results:<br /><br />n = 100<br />t1 = 0.0038838<br />t2 = 0.0068600<br />t3 = 0.0049579<br />t4 = 0.0076430<br /><br />n = 200<br />t1 = 0.0051990<br />t2 = 0.0083339<br />t3 = 0.0062981<br />t4 = 0.0091600<br /><br />n = 500<br />t1 = 0.0042169<br />t2 = 0.0071800<br />t3 = 0.0051830<br />t4 = 0.0081360<br /><br />n = 1000<br />t1 = 0.055708<br />t2 = 0.059194<br />t3 = 0.056590<br />t4 = 0.060054<br /><br />n = 2000<br />t1 = 0.15554<br />t2 = 0.16691<br />t3 = 0.16327<br />t4 = 0.16677<br /><br /><br /><br />This test with different matrices can be found in the file quicktest.m in the folder First_week.<br /><br />We can see that if we consider n<=1000, the best strategy is to use the simple matix-vector product (indeed t1 is the less value). Instead if we consider the two strategies of gmres and pcg, we notice that the "if block" used in pcg takes more time than the simple matrix-vector product and the evaluation of the function handle.<br />Instead the case n = 2000 is not a good example to see the differences of these approaches, probably because the matrix is too big and requires a lot of operations.<br />Then, after a discussion with the mentors, we decided that the "best strategy" is to write in every algorithm two nested function "ad hoc" to the type of the data, and if A, M1 and M2 are not of the same type, to transform all of them in function handle. To be clear, for example, as sketch of how it will be:<br /><br />function [x, flag, relres, iter, resvec] = pcg (A, b, tol, maxit, M1, M2, x0)<br /><br /> if A, M1, M2 are matrices<br /> [x, flag, relres, iter, resvec] = pcg_matrix (A, b, tol, maxit, M1, M2, x0)<br /><br /> if A, M1, M2 are function handle<br /> [x, flag, relres, iter, resvec] = pcg_function (A, b, tol, maxit, M1, M2, x0)<br /><br /> if A, M1, M2 are not of the same type<br /> [x, flag, relres, iter, resvec] = pcg_mixed (A, b, tol, maxit, M1, M2, x0)<br /><br />endfunction <br /><br /> <b>Priority of the algorithms</b><br /><br />The last part of this post is about the chosen priority of the improvements of the algorithms for the next weeks.<br />The mentors suggested me to improve the algorithms in the following order:<br /><ol><li>pcg</li><li>gmres</li><li>bicgstab</li><li>pcr</li><li>qmr</li><li>cgs</li><li>bicg</li></ol>This order is suggested because pcg, gmres and bicgstab are the most used to solve iteratively linear systems.<br />Then follows pcr because it is the only method already implemented in Octave that solve linear system when the input matrix A is Hermitian (bicgstab and gmres work for general matrices, instead pcg works for SPD matrices).<br />And in the last follows qmr, cgs and bicg because they are the less used methods, with cgs above bicg because it has to be written from scratch.<br /><b></b><br /><b></b><br />If there are something that is not clear or if there are some suggestions/advices, comment below or contact me via email at cristiano.dorigo@hotmail.it<br /><b></b><br /><b></b><br /><b></b><br /><b></b><br /><b></b><br /><b></b><br /><b></b><br /><b></b><br /><b></b><br /><b></b><br /><b></b><br /><b></b><br /><b></b><br /><b><br /></b><b><br /></b><br /><ol></ol>Cristiano Dorigohttps://plus.google.com/103961108712693178106noreply@blogger.com0tag:blogger.com,1999:blog-1297699247151766814.post-3938180768550672782016-06-08T02:08:00.000-07:002016-06-08T02:08:17.032-07:00Presentation of the Project and TimelineDear all,<br /><br />I'm Cristiano Dorigo, a bachelor graduated student in Applied Mathematics in Verona, Italy, and now I'm a student of Master Degree in Mathematics in the University of Verona.<br /><br />I was selected as partecipant student for the SOCIS 2016 under GNU Octave for the project "Improve iterative methods for sparse linear systems".<br /><br />The idea of this project is at first to improve the methods actually implemented in Octave to solve linear systems as gmres, pcg, bicg, bicgstab, qmr, pcr. Ideed these algorithms have, for example, a similar structure in the initial part (where the inputs are checked). Other possible improvement can be the synchronization of their error messages or to check the documentation. Another critical point is to check the compatibility of the results with Matlab, i.e. to check, for example, if the quality of the solution with the same method is equal both in Matlab and Octave, to see if they use the same number of iterations, same residual,...<br /><br />The second step is to implement, as much as possible, some iterative methods that, for example, are in Matlab, but not in Octave. The suggested methods are minres, symmlq, tfqmr, bicgstabl, lsqr. The order of the implementation can be decided with the mentors giving priority to the useful or the necessary ones. <br /><b> </b><br /><b>Timeline:</b><br /><br />I think to split the work in the same two step mentioned before, i.e. improvements and implementation.<br /><br /><ul><li>The first two weeks I want to study the methods already implemented but that I haven't faced off during my student career, i.e. bicg, bicgstab, qmr and pcr and to study their codes.</li><li>The second two/three weeks I want at first to consult the mentors to decide where and how to improve these methods, then to apply these ideas. </li><li>The remaining time will be spent to implement the new methods. I want to implement these methods one by one, after a discussion with the mentors about which methods have priority.</li></ul><br />The reference book is "Iterative Methods for sparse linear systems" 2-nd Edition by Yousef Saad, which is available here <a href="http://www-users.cs.umn.edu/~saad/IterMethBook_2ndEd.pdf">www-users.cs.umn.edu/~saad/IterMethBook_2ndEd.pdf</a>Cristiano Dorigohttps://plus.google.com/103961108712693178106noreply@blogger.com0