Skip to content

Create a tagarray

Cheol Ho Choi edited this page Mar 24, 2024 · 14 revisions

What is tagarray?

A tagarray is an array of any type with a tag such that it can be accessed from any Language with the tag. It is especially useful when one needs to pass data between different computer languages. The source code is located in external/libtagarray subdirectory. The general tagarrays that OQP utilizes are defined in source/tagarray_driver.F90.

Creating a tagarray in Python and passed to Fortran

Note that the oqp.Fortran_example should be provided by the process described in Creating a new module of Fortran.

  1. Creating a tagarray of Python_matrix in a Python script using numpy format with the tag of OQP::Python_matrix. The oqp.example is a Fortran subroutine.
    mol.data["OQP::Python_matrix"] = np.reshape([1, 2.0, 3, 4.0, 5, 6], (2,3)) 
    oqp.Fortran_example(mol)
  1. A Fortran code that utilizes the "Python_matrix" defined in the Python script.
module example_mod
  implicit none
  character(len=*), parameter :: module_name = "example_mod"
  public Fortran_example
contains
  subroutine Fortran_example(infos, alog)   .....
    use oqp_tagarray_driver, only: data_has_tags, tagarray_get_data, ta_type_real64
   .....
    character(len=*), parameter :: subroutine_name = "Fortran_example"
  ! Tagarray
    character(len=*), parameter :: tags_general(*) = (/ character(len=80) :: &
        "OQP::Python_matrix" /)
    real(kind=dp), contiguous, pointer :: Python_matrix(:,:)

  ! Load data that was previously allocated from the Python script.
    call data_has_tags(infos%dat, tags_general, module_name, subroutine_name, with_abort)
    call tagarray_get_data(infos%dat, "OQP::Python_matrix", Python_matrix)

    do i = 1, ubound(Python_matrix, 1)
      write(*,*) i, (Python_matrix(i,j),j = 1,ubound(Python_matrix, 2))
    end do
   .....

Creating a tagarray in Fortran and passed to Python

Note that the oqp.Fortran_example should be provided by the process described in Creating a new module of Fortran.

  1. A Fortran code that creates a tagarray of "Fortran_matrix".
module example_mod
  implicit none
  character(len=*), parameter :: module_name = "example_mod"
  public Fortran_example
contains
  subroutine Fortran_example(infos, alog)   .....
    use oqp_tagarray_driver, only: data_has_tags, tagarray_get_data, ta_type_real64
   .....
    character(len=*), parameter :: subroutine_name = "Fortran_example"
    character(len=*), parameter :: tags_alloc(*) = (/ character(len=80) :: &
        "OQP::Fortran_matrix" /)
    real(kind=dp), contiguous, pointer :: Fortran_matrix(:,:)

! a) Define the size
    call infos%dat%remove_records(tags_alloc)
    call infos%dat%reserve_data("OQP::Fortran_matrix", ta_type_real64, &
          2*3, (/ 2, 3 /), comment="An example of alloc data in Fortran")
! b) Allocate memory
    call data_has_tags(infos%dat, tags_alloc, module_name, subroutine_name, with_abort)
    call tagarray_get_data(infos%dat, "OQP::Fortran_matrix", Fortran_matrix)
    if (ok/=0) call show_message('Cannot allocate memory', with_abort)
! c) Assign data
    chc_matrix_f = reshape([0.1d0, 0.2d0, 0.3d0, 0.4d0, 0.5d0, 0.6d0], [2, 3])
    write(iw, fmt="(/16X,43('=')/16X,a/16X,43('='))") &
        'This is an example of tagarray defined in Fortran'
   .....
  1. Load the tagarray of Fortran_matrix in a Python script.
    result = mol.data["OQP::Fortran_matrix"]

Going back to Howtos

Clone this wiki locally