From: Eddie R. <er...@bm...> - 2008-08-28 23:05:29
|
On Thu 28/08/08 5:25 PM , Albert Graef Dr....@t-... sent: > Coming to think of it, it's even better if you write a wrapper function > for each type of matrix in C, which takes a list (as a pure_expr*) as > argument and returns a matrix. That would execute *much* faster since > you only have a single function call per matrix. (You don't initialize a > big matrix with n^2 function calls in C either, or do you? If GSL > doesn't provide a direct way to initialize a big matrix then that'd be a > major performance hog.) It has functions for initializing to zero, idenity, or all the entries the same value. However, since the data is laid out as an array of doubles, ints for gsl_matrix_int, (you get the picture) all I have to do is flatten a list of lists and memcpy'em in. struct { unsigned int size1; //number of rows; unsigned int size2; //number of cols; unsigned int tda; //number of entries per row double * data; //here is where the data is ... } gsl_matrix; A matrix m's cell is accessed like m->data[row*size2+col]. > Yes, it is. The Q modules work that way, too. When you have a complex C > library, almost invariably you need some additional support routines for > accessing the data, and that stuff is best written in C. Then you have a > little Pure module which hides away all the complexity, loads the > necessary C libraries and provides any high-level definitions > (overloading operators etc.) that you want. All the Pure programmer ever > sees is that Pure script. Ok. You made up my mind for me. The C wrapper is not difficult to make for Pure. You are right it is still faster to make a call to a call to a call than go through all the support within Pure. I noticed that even optimized gcc makes a call for a cos operation when it could plainly use a direct CPU fcos call. e.r. |
From: Albert G. <Dr....@t-...> - 2008-08-29 00:24:41
|
Eddie Rucker wrote: > It has functions for initializing to zero, idenity, or all the entries the same value. However, since the data is laid out as an > array of doubles, ints for gsl_matrix_int, (you get the picture) Yes, I see. So it's just a blob with dimensions and stride attached to it. Well, I could easily generate data compatible with that in Pure, along with the usual access operations and support in the runtime. Memory management would be provided by Pure. I could then add the necessary marshalling so that these thingies could be passed either as is to GSL, or the underlying gsl_block to the BLAS routines, or just the pointer to the blob to int*, double* etc., so that they could be used with graphics and audio software just as easily. Of course we need to fix a syntax for those arrays. I'd suggest the following: {x1,x2,...} is a vector (compatible with gsl_vector struct layout), {x11,x12,...; x21,x22,...; ...} is a matrix (compatible with gsl_matrix struct layout). Elements would be either integers or doubles, promoting all ints to double in the mixed case. We can think about complex numbers later. Using some macro hackery, it should also be possible to provide some kind of array comprehensions. I still have to think about this. Does that proposal sound reasonable? Albert -- Dr. Albert Gr"af Dept. of Music-Informatics, University of Mainz, Germany Email: Dr....@t-..., ag...@mu... WWW: http://www.musikinformatik.uni-mainz.de/ag |
From: Eddie R. <er...@bm...> - 2008-08-29 14:14:45
|
On Fri, 2008-08-29 at 02:26 +0200, Albert Graef wrote: > Yes, I see. So it's just a blob with dimensions and stride attached to > it. Well, I could easily generate data compatible with that in Pure, > along with the usual access operations and support in the runtime. > Memory management would be provided by Pure. > > I could then add the necessary marshalling so that these thingies could > be passed either as is to GSL, or the underlying gsl_block to the BLAS > routines, or just the pointer to the blob to int*, double* etc., so that > they could be used with graphics and audio software just as easily. > > Of course we need to fix a syntax for those arrays. I'd suggest the > following: {x1,x2,...} is a vector (compatible with gsl_vector struct > layout), {x11,x12,...; x21,x22,...; ...} is a matrix (compatible with > gsl_matrix struct layout). > > Elements would be either integers or doubles, promoting all ints to > double in the mixed case. We can think about complex numbers later. > Using some macro hackery, it should also be possible to provide some > kind of array comprehensions. I still have to think about this. > > Does that proposal sound reasonable? Using the method GSL uses internally for vectors and matrices seems reasonable and would probably allow for fast routines and make it easy for interfacing to other libraries and/or hardware. I think that maybe some of the primitive operations like addition, subtraction, multiplication, and a reciprocal function might be handled best internally in Pure though. Before you say no too quickly, let me point out that GSL_BLAS has a bunch of steps involved just to multiply two matrices. There is a routine to directly add two matrices but not for multiplication. For example to multiply to matrices with double entries, I have to use the following procedure (there is a more sophisticated version that deals with order (row major vs column major order like in FORTRAN) and some other things. int gsl_blas_dgemm (CBLAS_TRANSPOSE_t TransA, CBLAS_TRANSPOSE_t TransB, double alpha, const gsl_matrix * A, const gsl_matrix * B, double beta, gsl_matrix * C) Thus, the matrix operation follows C = alpha*A*B + beta*C; TransA and TransB have to be set to CblasNoTrans for regular multiplication, other choices are CblasTrans and CblasConjTrans. e.r. |
From: Albert G. <Dr....@t-...> - 2008-08-29 20:41:28
|
Eddie Rucker wrote: > Using the method GSL uses internally for vectors and matrices seems > reasonable and would probably allow for fast routines and make it easy > for interfacing to other libraries and/or hardware. To avoid having Pure depend on GSL, I can hopefully just replicate the memory allocation and view functionality from GSL and massage them for my needs (I probably need to add a reference counter to the gsl_matrix and vector structs in order to support contiguous slices in an efficient way). I'll also expose the interface to the Pure matrix and vector data in the public runtime API, so that you have easy access to it from C, too. I still have some other stuff on my TODO list right now (specifically, I want to add a basic stream implementation a la SICP to the library), but I can hopefully start working on the vector/matrix stuff some time next week. > I think that maybe some of the primitive operations like addition, > subtraction, multiplication, and a reciprocal function might be handled > best internally in Pure though. Before you say no too quickly, let me > point out that GSL_BLAS has a bunch of steps involved just to multiply > two matrices. If we want to replicate this kind of functionality, it can be done just as well in the Pure GSL wrapper. I really think that all linear algebra functionality belongs there, so that we have a clear cut between the interpreter's basic matrix/vector container support and the GSL wrapper's more advanced functionality. Otherwise we'll just end up duplicating stuff. So in the Pure vector/matrix support, I want to concentrate on the basic container functionality, including efficient element access and slicing. Pure programmers will then be able to do basic vector/matrix stuff using these operations and the array comprehensions, or they can fire up the GSL wrapper once it's available, to do more advanced stuff and take advantage of the speedy C implementation. Albert -- Dr. Albert Gr"af Dept. of Music-Informatics, University of Mainz, Germany Email: Dr....@t-..., ag...@mu... WWW: http://www.musikinformatik.uni-mainz.de/ag |
From: Eddie R. <er...@bm...> - 2008-08-29 20:57:31
|
On Fri, 2008-08-29 at 22:43 +0200, Albert Graef wrote: > To avoid having Pure depend on GSL, I can hopefully just replicate the > memory allocation and view functionality from GSL and massage them for > my needs (I probably need to add a reference counter to the gsl_matrix > and vector structs in order to support contiguous slices in an efficient > way). I'll also expose the interface to the Pure matrix and vector data > in the public runtime API, so that you have easy access to it from C, too. > > I still have some other stuff on my TODO list right now (specifically, I > want to add a basic stream implementation a la SICP to the library), but > I can hopefully start working on the vector/matrix stuff some time next > week. > > > I think that maybe some of the primitive operations like addition, > > subtraction, multiplication, and a reciprocal function might be handled > > best internally in Pure though. Before you say no too quickly, let me > > point out that GSL_BLAS has a bunch of steps involved just to multiply > > two matrices. > > If we want to replicate this kind of functionality, it can be done just > as well in the Pure GSL wrapper. I really think that all linear algebra > functionality belongs there, so that we have a clear cut between the > interpreter's basic matrix/vector container support and the GSL > wrapper's more advanced functionality. Otherwise we'll just end up > duplicating stuff. > > So in the Pure vector/matrix support, I want to concentrate on the basic > container functionality, including efficient element access and slicing. > Pure programmers will then be able to do basic vector/matrix stuff using > these operations and the array comprehensions, or they can fire up the > GSL wrapper once it's available, to do more advanced stuff and take > advantage of the speedy C implementation. All sounds good to me. While you work toward that end, I'm going to go a head and wrap the statistical distribution functions. I need those for work. Would you be offended if I tried to port your Q ODBC module to Pure? e.r. |
From: Albert G. <Dr....@t-...> - 2008-08-29 23:07:17
|
Eddie Rucker wrote: > Would you be offended if I tried to port your Q ODBC module to Pure? Hell, no! :) I'd appreciate that very much. -- Dr. Albert Gr"af Dept. of Music-Informatics, University of Mainz, Germany Email: Dr....@t-..., ag...@mu... WWW: http://www.musikinformatik.uni-mainz.de/ag |