Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#40 IT++/GSL/C Vector/Matrix data conversion

open
nobody
None
3
2012-09-15
2006-03-27
Frank
No

I'm working with several vector and matrix routines in
one program. So, sometimes it's necessary to convert
the data between the math libraries.

It would be helpful, if IT++ would provide a reliable
interface to conventional C vector/matrix data
structures, the templated C++ structures and the
formats of the GNU Scientific Library GSL.

For vectors, it's only a data copy, but matrices are
organized differently (row-major vs. column-major
format). At least data copy between IT++ and GSL messed
up the matrix order (one direction, I forgot which one).

I would suggest something like this:
vec myITmatrix;
double* c_matrix;
c_matrix = myITmatrix.cdata()
c_size = myITmatrix.cdata_size()

Additionally, dedicated GSL-interfaces would be
comfortable. I used these functions to convert IT++/GSL:

// conversion, implementation in diss_gsl.cpp

gsl_vector vec2gslvec(const vec &iv) {
gsl_vector
gv;
gv=gsl_vector_alloc(iv.size());
memcpy(gv->data,iv._data(),gv->size*sizeof(double));
return(gv);
}

vec gslvec2vec(gsl_vector gv){
vec iv;
if (gv==NULL) return(iv);
iv.set_size(gv->size,false);
memcpy(iv._data(),gv->data,gv->size
sizeof(double));
return(iv);
}

//auch complex matrix, TBD

gsl_matrix mat2gslmat(const mat &im){
gsl_matrix
gm;
int rows=im.rows(), cols=im.cols();
gm=gsl_matrix_alloc(rows,cols);
//memcpy(gm->data,im._data(),im._datasize()*sizeof(double));
for (int ir=0; ir<rows; ir++) for (int ic=0; ic<cols;
ic++) gsl_matrix_set(gm,ir,ic,im(ir,ic));
return(gm);
}

mat gslmat2mat(gsl_matrix gm){
if (gm==NULL) {
mat im;
return(im);
} else {
int rows=gm->size1, cols=gm->size2;

mat im(rows,cols);
//memcpy(im._data(),gm->data,rows
cols*sizeof(double));
//some more efficient way here?
for (int ir=0; ir<rows; ir++) for (int ic=0; ic<cols;
ic++)
im(ir,ic) = gsl_matrix_get(gm,ir,ic);
return(im);
};
}

Discussion

  • Logged In: YES
    user_id=1600582
    Originator: NO

    Hi Frank,

    We're currently looking into providing a more elaborate interface to/from GSL.

     
  • Frank
    Frank
    2006-12-29

    Logged In: YES
    user_id=1247172
    Originator: YES

    Ok, fine. I would suggest, first to implement the vector/matrix conversion routines like the ones above. I wrote these to use GSL-functions on IT++ data structures. For the rest of GSL, a C++ wrapper class would be nice.

    There are a few examples:
    GSLwrap http://sourceforge.net/projects/gslwrap/
    GSLmm http://cholm.home.cern.ch/cholm/misc/#gslmm
    Zoom http://www.fnal.gov/docs/working-groups/fpcltf/Pkg/SpecialFunctions/doc/html/0SpecialFunctions.html

    It would be interesting to have a common understanding of the complex data type. In C it's not very comfortable to have special function names for every data type combination. If using the complex<double> for GSL wrapper functions, it would simplify the function handling very much.

    Of course, performance is a big concern in numerical applications. For performance critical functions, I prefer to circumvent such wrapper functions and operate on vectors, matrices and functions as directly as possible. So, please leave the possibility to access the internal data structures of classes. The situation now is not comfortable:
    in Matrix: "Num_T *_data() internal data structure. Don't use. May be changed!"

    Better "attention: subject to change, currently implemented as CBLAS compatible matrix" !!!

    So you know how to access this CBLAS field in the current version and take care for future IT++ releases. This is very essential if you want to maximize performance and access thoses matrices directly. For safety, do something like

    define MATRIX_TYPE CBLAS_ROW_MAJOR

    So in IT++ application sources, you can check if the types are compatible with your direct access. But the row-major/column-major type has to be distinguished. I was a bit confused about the matrix organization in memory. Please make a detailed definition in the Docs how you implemented the matrix, even if it might change later.

    Furthermore, the BOOST library aims to become a kind of a standard library for C++, with uBLAS numerics.
    http://www.boost.org/
    It would be useful to have at least vector/matrix data conversion between BOOST and IT++.