Fortran

Revision as of 04:57, 13 July 2020 by Siko1056 (talk | contribs) (→‎C++ function: Fix syntax highlight.)

This page describes an example of how to call liboctave functions from a Fortran program. In the example we will load an Octave array from a file in Octave's native ASCII format, it consists of two steps:

  • write a C++ function with a C compatible interface and C linkage that reads a variable from an Octave ASCII file
  • write Fortran code using the "iso_c_binding" intrinsic module to call the C++ function

C++ function

Code: octave_file_io.cc: C++ function to load a matrix from an ASCII file in Octave native format
// Octave header
#include <octave/oct.h>
#include <octave/ls-mat-ascii.h>

// Custom header containing the C compatible interface
#include <octave_file_io.h>

//! Load a single matrix, stored in ASCII format, from a data file.
//!
//! @param file_name name of the data file.
//! @param data pointer to the read-in matrix stored as fortran vector
//!             (column-major order).
//! @param numel number of elements in @p data.

int octave_load (const char* file_name, double** data, int* numel)
{
  // Define variable to hold the read data.
  octave_value read_data;

  // Read a plain ASCII matrix from data file.
  std::ifstream in_file_stream (file_name, std::ios::binary);
  read_mat_ascii_data (in_file_stream, file_name, read_data);
  in_file_stream.close ();

  // Convert read data to numerical array (matrix).
  NDArray A = read_data.array_value ();

  // Extract number of elements in matrix A.
  *numel = A.numel ();

  // Allocate memory to pointer to returned values.
  *data  = (double*) malloc (A.numel () * sizeof (double));

  // Copy the content of matrix A to data structure Fortran can handle.
  memcpy (*data, A.fortran_vec (), A.numel () * sizeof (double));

  return 0;
}

Header file

Code: octave_file_io.h: header file with C interface to octave_file_io.cc
#ifndef OCTAVE_FILE_IO_H
#define OCTAVE_FILE_IO_H

#ifdef __cplusplus
extern "C" {
#endif
  
  int octave_load (const char* filename, const char* varname, double** data, int* numel);

#ifdef __cplusplus
}
#endif

#endif


Fortran Code

Code: octave_file_io_example.f90
program octave_file_io_example
  
  use iso_c_binding
    
  implicit none

  interface 
     function octave_load (filename, varname, data, numel) bind(c, name="octave_load")
       
       use iso_c_binding
       implicit none

       integer(c_int) :: octave_load
       
       character(kind=c_char), intent(in) :: filename(*)
       character(kind=c_char), intent(in) :: varname(*)
       
       type(c_ptr), intent(out) :: data
       integer(c_int), intent(out) :: numel
       
     end function octave_load
  end interface
  
  integer(c_int) :: res 
  type(c_ptr) :: data
  real(c_double), pointer :: fdata(:)
  integer(c_int) :: numel

  res = octave_load (c_char_'pippo.octxt' // c_null_char, c_char_'A' // c_null_char, data, numel)
  
  call c_f_pointer (data, fdata, (/numel/))
  
  write (*,*) fdata

end program octave_file_io_example



Compiling the code

 mkoctfile -I. octave_file_io.cc 
 mkoctfile -I. --mkoctfile --link-stand-alone octave_file_io_example.f90 octave_file_io.o -o octave_file_io_example