From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: <367D0D60.C111AB59@mit.edu> Date: Sun, 20 Dec 1998 14:44:55 +0000 From: Adam C. Powell Subject: Re: little unrolling trick In-Reply-To: <36777c8c1153002@u8455.bs.ptb.de> Thomas Bruns wrote: > Hi Adam, > > > From: "Adam C. Powell, IV" > > of a way to speed this up? By the way, Thomas (Bruns), did you notice > > any performance improvement using my little "loop unrolling" trick? > > Sorry, I did some other work on my app the last days so it took some > time to post results > > your suggestion was: > > >for (row=0; row > for(ptr=matrix_ptr[row]; ptr > vector[matrix_idx[ptr]] += matrix[ptr]*vector[matrix_idx[ptr]]; > > vector[matrix_idx[ptr+1]] += > > matrix[ptr+1]*vector[matrix_idx[ptr+1]]; vector[matrix_idx[ptr+2]] > > += matrix[ptr+2]*vector[matrix_idx[ptr+2]]; > > vector[matrix_idx[ptr+3]] += > > matrix[ptr+3]*vector[matrix_idx[ptr+3]]; vector[matrix_idx[ptr+4]] > > += matrix[ptr+4]*vector[matrix_idx[ptr+4]]; > > vector[matrix_idx[ptr+5]] += > > matrix[ptr+5]*vector[matrix_idx[ptr+5]]; } > > while(ptr > vector[matrix_idx[ptr]] += matrix[ptr]*vector[matrix_idx[ptr]]; > > ptr++; }} > > But, I think this can´t be right, since you modify the vector you > need to multiply with ??? Oops! You're absolutely right. My mistake. :-) > Anyway your `little unrolling trick´ gave me the direction to some > faster code: > > my suggestion: > > for (row=0; row { > result[row] = 0; > for (ptr=matrix_ptr[row]; ptr result[row] += matrix[ptr]*vector[matrix_idx[ptr]] > +matrix[ptr+1]*vector[matrix_idx[ptr+1]] > +matrix[ptr+2]*vector[matrix_idx[ptr+2]] > +matrix[ptr+3]*vector[matrix_idx[ptr+3]] > +matrix[ptr+4]*vector[matrix_idx[ptr+4]] > +matrix[ptr+5]*vector[matrix_idx[ptr+5]]; > > while (ptr < matrix_ptr[row+1]) > { > result[row] += matrix[ptr]*vector[matrix_idx[ptr]]; > ptr ++; > } > } > > which gave me a speedup of about 30% in that routine, which is quite > good (I think) for now, because the matrices are quite small (~50x50 > .... 100x100) with 8 to 30 non zeros in a row. That's great to hear! Perhaps there is some hope for faster sparse matrix routines after all. It's hard to know how big to make the "blocks"- six works here because your rows have at least 6 nonzeroes each. For some applications, e.g. on structured grids, it might be more efficient to treat the "matrix" as a set of vectors and use level 1 BLAS to multiply them by vectors (again hoping "vector" and "result" stay in cache while the vectors that make up the matrix get swapped out). > I did the timing with the times(....) routine of gnu-C, I don`t know > how accurate that is. That's what I use too. :-) > So thank you very much for the inspiration ! You're welcome. Zeen, -Adam "...the firmament sheweth His handywork" (Ps 19:1) /// ............................................................__ ///| Adam "Cold Fusion" Powell IV Firmament Science & Engineering\\\///_| http://lyre.mit.edu/~powell/ Standing on the Solid State Rock\XX/ |MIGA