Using dtk within Fortran code and outlook

In the context of the ADT Simon related to the Inria Project Lab (IPL) C2S@Exa dealing with High Performance Computations (HPC), a lot of efforts were made by the dtk team to develop both API and efficient back-ends for parallel sparse linear algebra. Simultaneously, Tristan Cabel modified the Fortran code TRACES from the Andra Institute in order to reap the benefit of these developments (see this post).

In order to achieve such integration, Tristan has written Fortran modules using iso_c_binding features. These modules declare some interfaces to C++ functions that are in charge of handling C++ concepts from the dtkLinearAlgebraSparse layer.

Code snippet

Here follows a code snippet of such a module:

module mod_dtkLinearAlgebraSparse
  use,intrinsic :: iso_c_binding
  implicit none

  interface

     function dtkSparseMatrixResize(matrix, nb_row, nb_col) result(error) &
         bind(C, name="dtkSparseMatrixResize")
       use, intrinsic :: iso_c_binding
       type(c_ptr), intent(in), value :: matrix
       integer(c_long_long), intent(in), value :: nb_row
       integer(c_long_long), intent(in), value :: nb_col
       integer(c_int) error 
     end function dtkSparseMatrixResize

     function dtkSparseMatrixSetAt(matrix, row_id, col_id, value) result(error) &
         bind(C, name="dtkSparseMatrixSetAt")
       use, intrinsic :: iso_c_binding
       type(c_ptr), intent(in), value :: matrix
       integer(c_long_long), intent(in), value :: row_id
       integer(c_long_long), intent(in), value :: col_id
       real(c_double) :: val
       integer(c_int) :: error
     end function dtkSparseMatrixSetAt

     function dtkSparseMatrixAt(matrix, row_id, col_id) result(val) &
         bind(C, name="dtkSparseMatrixAt")
       use, intrinsic :: iso_c_binding
       type(c_ptr), intent(in), value :: matrix
       integer(c_long_long), intent(in), value :: row_id
       integer(c_long_long), intent(in), value :: col_id
       real(c_double) :: val
     end function dtkSparseMatrixAt

   end interface
end module mod_dtkLinearAlgebraSparse

The C++ functions retrieve the pointers, cast them into the right dtk concept (here the pointer is casted into dtkSparseMatrix) and then they carry out the required functionality:

extern "C" int dtkSparseMatrixResize(void *matrix, long long int nb_row, 
                                                   long long int nb_col)
{
   dtkSparseMatrix *mat = static_cast < dtkSparseMatrix < double > *>(matrix);
   mat->resize(nb_row, nb_col);
   return 0;
}
extern "C" int dtkSparseMatrixSetAt(void *dtk_sparse_matrix, long long int row_id, 
                                                             long long int col_id, 
                                                                    double value)
{
    dtkSparseMatrix *mat = static_cast < dtkSparseMatrix < double > *>(matrix);
    mat->setAt(row_id, col_id, value);

    return 0;
}
extern "C" double dtkSparseMatrixAt(void *dtk_sparse_matrix, long long int row_id, 
                                                             long long int col_id)
{
    dtkSparseMatrix *mat = static_cast < dtkSparseMatrix < double > *>(matrix);
    return mat->at(row_id, col_id);
}

Using this module into the Fortran code enables to handle the sparse matrix: resizing it, setting values at some coordinates, getting values at some coordinates.

Outlook

Several ADT involving Fortran codes will start by the end of the year. Using dtk in such contexts is not a straightforward task. People from these Teams are not often on the edge as far as the software development is concerned and it would take them too much time and too many efforts to do so.

A likely solution would be to generate in a quasi automatic way, the Fortran interfaces of dtk so that the end-users could embed it directly in their own code. Such a proposition will be submitted to the dtk committee.

About Thibaud Kloczko

Graduated in CFD, Thibaud Kloczko is a software engineer at Inria. He is involved in the development of the meta platform dtk that aims at speeding up life cycle of business codes into research teams and at sharing software components between teams from different scientific fields (such as medical and biological imaging, numerical simulation, geometry, linear algebra, computational neurology).

Leave a Reply

Your email address will not be published.