Ambiguous reference to function error when compiling - fortran

So I have a fortran .f90 file (part of the Monte-Carlo tool called SWEEP) that basically contains two main modules for the implementation of a variable length list using two types. The two modules are as follows:
Module LIST_DP_T
Implicit None
Public
Type LIST_DP
Integer :: Count = 0
Double Precision, Pointer :: Items(:)
End Type
Interface Assignment(=)
Module Procedure SetP
End Interface
Interface Operator(+)
Module Procedure Add
Module Procedure AddItem
Module Procedure AddItems
End Interface
Contains
! -------------------------------------------------------
Subroutine Destroy(list)
! DESCRIPTION
! Clears memory associated with a list type.
! ARGUMENTS.
Type(LIST_DP), Intent(INOUT) :: list
! VARIABLES.
Integer :: err = 0
! EXECUTABLE CODE.
list%Count = 0
Deallocate(list%Items, STAT=err)
Nullify(list%Items)
End Subroutine
! -------------------------------------------------------
Subroutine SetP(new, old)
! DESCRIPTION
! Assigns the list of one list type to another
! by passing the list pointer.
! ARGUMENTS.
Type(LIST_DP), Intent(INOUT) :: new
Type(LIST_DP), Intent(IN) :: old
! EXECUTABLE CODE.
new%Count = old%Count
new%Items => old%Items
End Subroutine
! -------------------------------------------------------
Subroutine Copy(new, old)
! DESCRIPTION
! Copies the list of one list type to another.
! ARGUMENTS.
Type(LIST_DP), Intent(INOUT) :: new
Type(LIST_DP), Intent(IN) :: old
! EXECUTABLE CODE.
new%Count = old%Count
new%Items = old%Items
End Subroutine
! -------------------------------------------------------
Function Add(list1, list2) Result (sumlist)
! DESCRIPTION
! Adds together two list and
! returns a new list.
! ARGUMENTS.
Type(LIST_DP), Intent(IN) :: list1
Type(LIST_DP), Intent(IN) :: list2
Type(LIST_DP) :: sumlist
! VARIABLES.
Integer :: err = 0
Double Precision :: items(list1%Count+list2%Count)
! EXECUTABLE CODE.
! Save current list.
If (list1%Count > 0) items(1:list1%Count) = list1%Items
If (list2%Count > 0) items(list1%Count+1:) = list2%Items
! Resize list.
Deallocate(sumlist%Items, STAT=err)
sumlist%Count = list1%Count + list2%Count
Allocate(sumlist%Items(sumlist%Count), STAT=err)
! Assign back the list.
sumlist%Items = items
End Function
! -------------------------------------------------------
Function AddItem(list, item) Result (newlist)
! DESCRIPTION
! Adds a number to the end of the list and
! returns a new list.
! ARGUMENTS.
Type(LIST_DP), Intent(IN) :: list
Double Precision, Intent(IN) :: item
Type(LIST_DP) :: newlist
! VARIABLES.
Integer :: err = 0
Double Precision :: items(list%Count+1)
! EXECUTABLE CODE.
! Save current list.
If (list%Count > 0) items(1:list%Count) = list%Items
items(list%Count+1) = item
! Resize list.
Deallocate(newlist%Items, STAT=err)
newlist%Count = list%Count + 1
Allocate(newlist%Items(newlist%Count), STAT=err)
! Assign back the list.
newlist%Items = items
End Function
! -------------------------------------------------------
Function AddItems(list, items) Result (newlist)
! DESCRIPTION
! Adds some numbers to the end of the list and
! returns a new list.
! ARGUMENTS.
Type(LIST_DP), Intent(IN) :: list
Double Precision, Intent(IN) :: items(:)
Type(LIST_DP) :: newlist
! VARIABLES.
Integer :: err = 0
Double Precision :: allitems(list%Count+Size(items))
! EXECUTABLE CODE.
! Save current list.
If (list%Count > 0) allitems(1:list%Count) = list%Items
allitems(list%Count+1:) = items
! Resize list.
Deallocate(newlist%Items, STAT=err)
newlist%Count = list%Count + Size(items)
Allocate(newlist%Items(newlist%Count), STAT=err)
! Assign back the list.
newlist%Items = allitems
End Function
! -------------------------------------------------------
Double Precision Function SumList(list)
! DESCRIPTION
! Returns the sum of the list items.
Type(LIST_DP), Intent(IN) :: list
SumList = Sum(list%Items)
End Function
End Module
and the other module which is for an integer list:
Module LIST_INT_T
Implicit None
Public
Type LIST_INT
Integer :: Count = 0
Integer, Pointer :: Items(:)
End Type
Interface Assignment(=)
Module Procedure SetP
End Interface
Interface Operator(+)
Module Procedure Add
Module Procedure AddItem
Module Procedure AddItems
End Interface
Contains
! -------------------------------------------------------
Subroutine Destroy(list)
! DESCRIPTION
! Clears memory associated with a list type.
! ARGUMENTS.
Type(LIST_INT), Intent(INOUT) :: list
! VARIABLES.
Integer :: err = 0
! EXECUTABLE CODE.
list%Count = 0
Deallocate(list%Items, STAT=err)
Nullify(list%Items)
End Subroutine
! -------------------------------------------------------
Subroutine SetP(new, old)
! DESCRIPTION
! Assigns the list of one list type to another
! by passing the list pointer.
! ARGUMENTS.
Type(LIST_INT), Intent(INOUT) :: new
Type(LIST_INT), Intent(IN) :: old
! EXECUTABLE CODE.
new%Count = old%Count
new%Items => old%Items
End Subroutine
! -------------------------------------------------------
Subroutine Copy(new, old)
! DESCRIPTION
! Copies the list of one list type to another.
! ARGUMENTS.
Type(LIST_INT), Intent(INOUT) :: new
Type(LIST_INT), Intent(IN) :: old
! EXECUTABLE CODE.
new%Count = old%Count
new%Items = old%Items
End Subroutine
! -------------------------------------------------------
Function Add(list1, list2) Result (sumlist)
! DESCRIPTION
! Adds together two list and
! returns a new list.
! ARGUMENTS.
Type(LIST_INT), Intent(IN) :: list1
Type(LIST_INT), Intent(IN) :: list2
Type(LIST_INT) :: sumlist
! VARIABLES.
Integer :: err = 0
Integer :: items(list1%Count+list2%Count)
! EXECUTABLE CODE.
! Save current list.
If (list1%Count > 0) items(1:list1%Count) = list1%Items
If (list2%Count > 0) items(list1%Count+1:) = list2%Items
! Resize list.
Deallocate(sumlist%Items, STAT=err)
sumlist%Count = list1%Count + list2%Count
Allocate(sumlist%Items(sumlist%Count), STAT=err)
! Assign back the list.
sumlist%Items = items
End Function
! -------------------------------------------------------
Function AddItem(list, item) Result (newlist)
! DESCRIPTION
! Adds a number to the end of the list and
! returns a new list.
! ARGUMENTS.
Type(LIST_INT), Intent(IN) :: list
Integer, Intent(IN) :: item
Type(LIST_INT) :: newlist
! VARIABLES.
Integer :: err = 0
Integer :: items(list%Count+1)
! EXECUTABLE CODE.
! Save current list.
If (list%Count > 0) items(1:list%Count) = list%Items
items(list%Count+1) = item
! Resize list.
Deallocate(newlist%Items, STAT=err)
newlist%Count = list%Count + 1
Allocate(newlist%Items(newlist%Count), STAT=err)
! Assign back the list.
newlist%Items = items
End Function
! -------------------------------------------------------
Function AddItems(list, items) Result (newlist)
! DESCRIPTION
! Adds some numbers to the end of the list and
! returns a new list.
! ARGUMENTS.
Type(LIST_INT), Intent(IN) :: list
Integer, Intent(IN) :: items(:)
Type(LIST_INT) :: newlist
! VARIABLES.
Integer :: err = 0
Integer :: allitems(list%Count+Size(items))
! EXECUTABLE CODE.
! Save current list.
If (list%Count > 0) allitems(1:list%Count) = list%Items
allitems(list%Count+1:) = items
! Resize list.
Deallocate(newlist%Items, STAT=err)
newlist%Count = list%Count + Size(items)
Allocate(newlist%Items(newlist%Count), STAT=err)
! Assign back the list.
newlist%Items = allitems
End Function
! -------------------------------------------------------
Integer Function SumList(list)
! DESCRIPTION
! Returns the sum of the list items.
Type(LIST_INT), Intent(IN) :: list
SumList = Sum(list%Items)
End Function
End Module
and a third module to make things easier when using the modules:
Module LIST_T
Use LIST_DP_T
Use LIST_INT_T
Implicit None
Public
End Module
The module LIST_T is then called from in a different module called Profile_Driver_Input located a different .f90 file (profile_driver_input.f90) as follows:
Module Profile_Driver_Input
Use LIST_T
Use Sweep
Implicit None
Public
Integer, Parameter :: UIN = 102
Integer, Parameter :: MAX_POINTS=1000, MAX_VARS=200
Type Settings
! Control parameters.
Logical :: Solve=.True., PostProcess=.True.
! Input files.
Character(LEN=200) :: MechFile="" ! Particle mechanism file name.
Character(LEN=200) :: ChemFile="" ! Chemistry profile file name.
! Time stepping variables.
Type(LIST_DP) :: Times ! Times between PSL outputs (first entry is start time).
Type(LIST_INT) :: Steps ! Steps between output times.
....
The code then continues to declare more variables and subroutines.
Upon compilation, the following error appears at the end of the Profile_Driver_Input module:
Error: Name 'additem' at (1) is an ambiguous reference to 'additem' from module 'list_dp_t'
Error: Name 'setp' at (1) is an ambiguous reference to 'setp' from module 'list_dp_t'
Error: Name 'setp' at (1) is an ambiguous reference to 'setp' from module 'list_dp_t'
Error: Name 'additem' at (1) is an ambiguous reference to 'additem' from module 'list_dp_t'
Error: Name 'setp' at (1) is an ambiguous reference to 'setp' from module 'list_dp_t'
Error: Name 'setp' at (1) is an ambiguous reference to 'setp' from module 'list_dp_t'
Error: Name 'additem' at (1) is an ambiguous reference to 'additem' from module 'list_dp_t'
Error: Name 'setp' at (1) is an ambiguous reference to 'setp' from module 'list_dp_t'
I cannot seem to figure out how to resolve this issue. Any help/advice would be appreciated.

You have multiple symbols that are named the same way. They come from different modules, but it is still a problem. For example, a subroutine named additem exists in both modules you show.
If you do not actually need to call the subroutines themselves and you only want to call the overloaded operators (+) and assignments (=), you can change the default accessibility from public to private inside the module and only leave public what actually needs to be accessible.
If both additem and destroy and other subroutines will need to be accessible from both modules at the same time, you will have to rename them to be unique. For example: additem_list_int and additem_list_dp.

Related

Errors #6259, #7715, #6355, #6303 in defining Derived Type specific assignment/operator procedures

First of all, Hi to everyone ! Wish you all a good starting week :)
In enhancing (as well as simplifying some syntax) a previously developed Derived Type, I was (and some still) encountering some errors as in the title specification (NOTE: for details about full messages and their cause, please read the comment lines in the code below :) ).
Among them, the ones I was not understand the most (of the why I am getting them) are error #6355: This binary operation is invalid for this data type. for the operator(==), and error #6303: The assignment operation or the binary expression operation is invalid for the data types of the two operands. for the assignment(=).
Here is an example of source code (NOTE: in order to reproduce those errors, comment out the type-bound procedure part, and uncomment out the two interface bodies):
module MPolicyMod
implicit none
private
public :: MPolicy
type, public :: MPolicy_t
real(kind = 8) :: delta_fI_fct_
real(kind = 8) :: delta_fJ_fct_
real(kind = 8) :: interp_I_fct_
real(kind = 8) :: interp_J_fct_
integer :: id_pol_
contains
generic :: assignment(=) => MPolicy_fromPol_sub, MPolicy_fromID_sub
procedure, private, pass :: MPolicy_fromID_sub, MPolicy_fromPol_sub
generic :: operator(==) => MPolicy_isID
procedure, pass, private :: MPolicy_isID
end type MPolicy_t
integer, public, parameter :: MPolicy_NULL = 0
integer, public, parameter :: MPolicy_DEF = 1
integer, public, parameter :: MPolicy_CONST = 2
interface MPolicy_t
module procedure MPolicy_constructor_integer
module procedure MPolicy_constructor_real
module procedure MPolicy_fromID
end interface
! interface assignment(=)
! module procedure MPolicy_fromPol_sub
! module procedure MPolicy_fromID_sub
! end interface assignment(=)
! interface operator(==)
! module procedure MPolicy_isID
! end interface operator(==)
contains
!> Main default constructor.
function MPolicy_constructor_real(dfi, dfj, interpi, interpj, id) result(pol)
real(kind = 8), intent(in) :: dfi, dfj, interpi, interpj
integer, intent(in) :: id
type(MPolicy_t) :: pol
print *, ' Default constructor (as compiler)...'
pol%delta_fI_fct_ = dfi
pol%delta_fJ_fct_ = dfj
pol%interp_I_fct_ = interpi
pol%interp_J_fct_ = interpj
pol%id_pol_ = id
end function MPolicy_constructor_real
function MPolicy_constructor_integer(dfi, dfj, interpi, interpj, id) result(pol)
integer, intent(in) :: dfi, dfj, interpi, interpj, id
type(MPolicy_t) :: pol
print *, ' Default constructor (integer version)...'
pol = MPolicy_t(real(dfi, 8), &
real(dfj, 8), &
real(interpi, 8), &
real(interpj, 8), id)
end function
function MPolicy_fromID(id) result(pol)
integer, intent(in) :: id
type(MPolicy_t) :: pol
print *, ' Constructor from ID...'
select case (id)
case (MPolicy_NULL)
return
case (MPolicy_DEF)
pol = MPolicy_t(2, 2, 2, 2, MPolicy_DEF)
case (MPolicy_CONST)
pol = MPolicy_t(1, 1, 1, 1, MPolicy_CONST)
case default
block
character(len=3) :: fmt
write(fmt, '(i)'), id
stop ' [ERROR] Unknow policy identifier "id"= '//fmt
end block
end select
end function MPolicy_fromID
subroutine MPolicy_fromPol_sub(lhs, rhs)
class(MPolicy_t), intent(in) :: rhs
class(MPolicy_t), intent(out) :: lhs
lhs%delta_fI_fct_ = rhs%delta_fI_fct_
lhs%delta_fJ_fct_ = rhs%delta_fJ_fct_
lhs%interp_I_fct_ = rhs%interp_I_fct_
lhs%interp_J_fct_ = rhs%interp_J_fct_
lhs%id_pol_ = rhs%id_pol_
end subroutine MPolicy_fromPol_sub
subroutine MPolicy_fromID_sub(lhs, rhs)
class(MPolicy_t), intent(out) :: lhs
integer, intent(in) :: rhs
lhs = MPolicy_t(rhs)
end subroutine MPolicy_fromID_sub
function MPolicy(mpol) result(pol)
integer :: mpol
type(MPolicy_t) :: pol
pol = MPolicy_t(mpol)
end function MPolicy
pure function MPolicy_isID(pol, id) result(isID)
class(MPolicy_t), intent(in) :: pol
integer, intent(in) :: id
logical :: isID
isID = pol%id_pol_ == id
end function MPolicy_isID
end module
Example program:
program test
use MPolicyMod
implicit none
! ! NOTE: generates
! ! error #6259: This array or function or substring is invalid in constant expressions. [MPOLICYMOD^MPOLICY_FROMID]
! ! error #7715: Replacing invalid structure constructor by integer 1. [<NULL_STRING>]
! type(MPolicy_t) :: pol = MPolicy_t(MPolicy_NULL)
type(MPolicy_t) :: pol
! =========================================================================
print *, pol
pol = MPolicy_t(MPolicy_NULL)
print *, pol
! ! NOTE: generates (without TYPE-BOUND specification)
! ! error #6355: This binary operation is invalid for this data type.
print *, pol == MPolicy_NULL
pol = MPolicy_t(1, 1, 1, 1, 2)
print *, pol
! ! NOTE: generates (without TYPE-BOUND specification)
! ! error #6303: The assignment operation or the binary expression operation is invalid for the data types of the two operands.
pol = MPolicy_DEF
print *, pol
end program test
Then, after reading through the ISO Fortran Standard 2003 Draft document, at Section 4.5.4 Type-bound procedures, Rule R452 states that we could specify assignment(=) as a generic-spec, in a generic-binding type of proc-binding-stmt. There I realised I was missing the type-bound specifications for those operator/assignment procedures.
Why, then, only the interface body was not enough to make it work?
After this, I also wonder why the initialisation statement type(MPolicy_t) :: pol = MPolicy_t(MPolicy_NULL) generates errors #6259 and #7715.
For that, I still didn't search by my own. But if you come with an asnwer before I might find (hopefully) an explanation, it's always good to have constructive as well as polite discussions with You all.
I now go back to my searching. Have a nice day :)

How to choose a desired code for a Subroutine among various options during the start of a run

Let's say I want to perform an operation in my main program (in Fortran). And lets say that operation is finding minimum number in a 1D array. I wish to do so by passing the array into the call subroutine and the subroutine will print the minimum value on the screen. There are different ways or algorithms to find minimum value in an array. Lets say I have 100 different methods: Method1, Method2..... Method100. Now I want to try using each one of these methods separately (I don't want to try all of them at once, but one method in each run). I don't want to create 100 different subroutines and change the code every time to decide which one to call, rather I want to mention in the input file which one I want to choose. So basically, the computer has to read the input file (to know which method to use) and perform the task using the specified method amongst different methods available.
I can write a Subroutine dump all the methods into that subroutine and put an IF condition to choose among various methods. But IF conditions are in efficient particularly on GPUs, I want to know the most efficient way of doing this.
MAIN PROGRAM
INTEGER Method !will be read from input file
Array = [12,5,3,4,1,7,4,3]
call print_Minimum(Array)
END PROGRAM
SUBROUTINE print_Minimum(Array)
IF (METHOD == 1)
<method 1 code>
ELSE IF (METHOD == 2)
<method 2 code>
:
:
:
:
ELSE IF (METHOD == 100)
<method100 code>
END IF
END SUBROUTINE
Thanks in advance.
This is probably best done using a function pointer and/or functions as arguments.
You can set a function pointer to a certain function and do this in your nested ifs and you can pass functions as arguments.
Both methods are implemented in the following example.
module minimum_mod
implicit none
private
public :: get_min_t, naive_min, time_min_function
abstract interface
integer pure function get_min_t(X)
integer, intent(in) :: X(:)
end function
end interface
contains
subroutine time_min_function(f, X)
procedure(get_min_t) :: f
integer, intent(in) :: X(:)
integer :: res
res = f(X)
write(*, *) res
end subroutine
integer pure function naive_min(X)
integer, intent(in) :: X(:)
integer :: i
naive_min = huge(naive_min)
do i = 1, size(X)
naive_min = min(naive_min, X(i))
end do
end function
end module
program time_min_finders
use minimum_mod, only: get_min_t, naive_min, time_min_function
implicit none
integer, parameter :: test_set(5) = [1, 10, 3, 5, 7]
procedure(get_min_t), pointer :: f
f => naive_min
call time_min_function(f, test_set)
end program
PS: Note that you can now do all the timinig logic inside time_min_function.
You can create an array of a derived type that contains a function pointer, effectively an array of function pointers. Then in principle you could initialize the function pointers to point at all of your test functions so that you could refer to each function by its index without having to test with a SELECT CASE of IF block: this is the typical Fortran way. However, either I've got the syntax for initialization wrong or my old version of gfortran just isn't capable, so I had to initialize one at a time. Sigh.
module minfuncs
implicit none
abstract interface
function func(array)
integer, intent(in) :: array(:)
integer func
end function func
end interface
type func_node
procedure(func), NOPASS, pointer :: f
end type func_node
! type(func_node) :: method(5) = [func_node(min_1),func_node(min_2), &
! func_node(min_3),func_node(min_4),func_node(min_5)]
contains
function min_1(array)
integer, intent(in) :: array(:)
integer min_1
integer i
min_1 = array(1)
do i = 2, size(array)
min_1 = min(min_1,array(i))
end do
end function min_1
function min_2(array)
integer, intent(in) :: array(:)
integer min_2
integer i
min_2 = array(1)
do i = 8, size(array), 7
min_2 = min(min_2,array(i-6),array(i-5),array(i-4), &
array(i-3),array(i-2),array(i-1),array(i))
end do
do i = i-6, size(array)
min_2 = min(min_2, array(i))
end do
end function min_2
function min_3(array)
integer, intent(in) :: array(:)
integer min_3
integer i
min_3 = array(1)
do i = 2, size(array)
min_3 = min_3-dim(min_3,array(i))
end do
end function min_3
function min_4(array)
integer, intent(in) :: array(:)
integer min_4
integer ymm(8)
integer i
if(size(array) >= 8) then
ymm = array(1:8)
do i = 16, size(array), 8
ymm = min(ymm,array(i-7:i))
end do
min_4 = minval([ymm,array(i-7:size(array))])
else
min_4 = minval(array)
end if
end function min_4
function min_5(array)
integer, intent(in) :: array(:)
integer min_5
min_5 = minval(array)
end function min_5
end module minfuncs
program test
use minfuncs
implicit none
integer, parameter :: N = 75
integer i
integer :: A(N) = modulo(5*[(i,i=1,N)]**2,163)
! type(func_node) :: method(5) = [func_node(min_1),func_node(min_2), &
! func_node(min_3),func_node(min_4),func_node(min_5)]
type(func_node) method(5)
method(1)%f => min_1
method(2)%f => min_2
method(3)%f => min_3
method(4)%f => min_4
method(5)%f => min_5
do i = 1, size(method)
write(*,*) method(i)%f(A)
end do
end program test
Output:
2
2
2
2
2

How to write a large array (1D or 2D) to hdf5 file at each time step for a simulation in Fortran

My simulation (written in Fortran 90) produces an array (either 1D, 2D, or 3D) at each time step. I would like to output these arrays into a single HDF5 file that contains the arrays produced for all the time steps. Note that since the output array at each time step has the same rank and dimensions, it is possible to combine these arrays together by adding an extra time dimension.
For now, I just create a buffer array to combine the output array at each time step, and then write the buffer array to a dataset in a HDF5 file at the end of simulation. But if the output array at each time step gets larger, the buffer can only hold the data for a few time steps. So I need to flush the data to HDF5 every a few time steps.
I looked at many posts and documentation and found that they mentioned some techniques like chunked dataset and hyperslab selection for efficient output to HDF5. But I am still not sure how I can apply these to my case. Could someone give me an example with Fortran 90?
After reading some of the documentation on hyperslab, I got the procedure to write to hdf5 file at each time step working. The following is the demo code. Hope you find it is useful.
program test_hyperslab
use HDF5
implicit none
integer :: error ! error flag
character(len=9), parameter :: filename = "subset.h5"
character(len=8), parameter :: dsetname = "IntArray"
integer(HID_T) :: file_id ! file identifier
integer(HID_T) :: dset_id ! dataset identifier
integer(HID_T) :: dataspace ! dataspace identifier
integer(HID_T) :: memspace ! memspace identifier
integer(HSIZE_T), dimension(2) :: dimsm = (/3, 1/)
integer, dimension(3) :: sdata ! subset buffer
integer :: dim0_sub = 3
integer :: dim1_sub = 1
integer(HSIZE_T), dimension(2) :: count = (/3, 1/)
integer(HSIZE_T), dimension(2) :: offset
integer(HSIZE_T), dimension(2) :: stride = (/1, 1/)
integer(HSIZE_T), dimension(2) :: block = (/1, 1/)
integer(HSIZE_T), dimension(2) :: dimsf = (/3, 10/)
integer, dimension(3, 10) :: rdata ! data to read
integer :: rank = 2
integer :: dim0 = 3
integer :: dim1 = 10
integer :: i
! initialize fortran interface
call h5open_f(error)
! create a new file with default properties
call h5fcreate_f(filename, H5F_ACC_TRUNC_F, file_id, error)
! create the data space for the dataset
call h5screate_simple_f(rank, dimsf, dataspace, error)
! create the dataset with default properties
call h5dcreate_f(file_id, dsetname, H5T_NATIVE_INTEGER, dataspace, &
dset_id, error)
! create memory dataspace
call h5screate_simple_f(rank, dimsm, memspace, error)
offset(1) = 0
do i = 1, 10
offset(2) = i - 1
sdata = i
! select subset
call h5sselect_hyperslab_f(dataspace, H5S_SELECT_SET_F, &
offset, count, error, stride, block)
! write subset to dataset
call h5dwrite_f(dset_id, H5T_NATIVE_INTEGER, sdata, dimsm, error, &
memspace, dataspace)
enddo
! read entire dataset back
call h5dread_f(dset_id, H5T_NATIVE_INTEGER, rdata, dimsf, error)
write(*, '(A)') "Data in file after subset written: "
do i = 1, dim0
write(*, '(100(1X,I0,1X))') rdata(i, 1:dim1)
enddo
! close everything opened
call h5sclose_f(dataspace, error)
call h5sclose_f(memspace, error)
call h5dclose_f(dset_id, error)
call h5fclose_f(file_id, error)
! close fortran interface
call h5close_f(error)
end program test_hyperslab
I think you need additional library for that.
Please look at the link to the Fortran library here: https://support.hdfgroup.org/HDF5/doc/fortran/index.html and some examples here: https://support.hdfgroup.org/HDF5/examples/f-src.html

Pointers in pure functions

In order to traverse a linked list in Fortran, I use a pointer to the current element that is moved to the next one inside a loop. Trying to apply this inside a pure function that operates on said linked list results in an error.
Example:
module list
implicit none
! Node
type n_list
integer :: val
type(n_list),pointer :: next => NULL()
end type
! Linked list
type t_list
type(n_list),pointer :: head
end type
contains
pure function in_list( list, val ) result(res)
implicit none
class(t_list),intent(in) :: list
integer,intent(in) :: val
logical :: res
type(n_list),pointer :: cur
res = .true.
! Traverse the list
cur => list%head
do while ( associated(cur) )
if ( cur%val == val ) return
cur => cur%next
enddo
! Not found
res = .false.
end function
end module
Results in
cur => list%head
1
Error: Bad target in pointer assignment in PURE procedure at (1)
I am aware of the rationale behind the error/warning, and that it is difficult to ensure that the arguments of the function are not changed when using pointers (Fortran 2008, ch. 12.7 "Pure procedures", esp. C1283). In this case, though, list is never changed.
Is it possible to tell the compiler (ifort and gfortran) that intent(in) is not violated?
The relevant part of the constraint you come up against (C12831) is
In a pure subprogram any designator with a base object that is .. a dummy argument with the INTENT (IN) attribute .. shall not be used
..
as the data-target in a pointer-assignment-stmt
The note below that constraint description motivates it
The above constraints are designed to guarantee that a pure procedure is free from side effects
What you want to say is "I guarantee that there are no side effects, we don't need the constraints for that". The constraints are sufficient but not necessary for this guarantee and you can analyse your code well.
However, a conforming processor/compiler must be able to detect breaches of constraints not just the overall goal of the constraints, so you don't just need to say "it's pure really", but also "and I don't need to be told of violations of C1283". That seems like a lot of effort for the compiler vendor to go to for very little benefit.
I guess, then, that the answer is "no": there isn't a way to compile your code. This isn't definitive, as we're really into implementation-specific areas. You asked about gfortran and ifort in particular, so a "use -nocheck c1283" refutes my "answer".
Now, if there is an option you're in the realms of "trust me" (and non-standard Fortran). So, let's go there anyway. It's just that we're going to lie. As usual, interface blocks will be our means.
module list_mod
implicit none
! Node
type n_list
integer :: val
type(n_list),pointer :: next => NULL()
end type
! Linked list
type t_list
type(n_list),pointer :: head
end type
interface
pure logical function in_list(list, val)
import t_list
class(t_list), intent(in) :: list
integer, intent(in) :: val
end function
end interface
end module
! Interface mismatch in the external function
function in_list(list, val) result(res)
use list_mod, only : t_list, n_list
implicit none
class(t_list),intent(in) :: list
integer,intent(in) :: val
logical :: res
type(n_list),pointer :: cur
res = .true.
! Traverse the list
cur => list%head
do while ( associated(cur) )
if ( cur%val == val ) return
cur => cur%next
enddo
! Not found
res = .false.
end function
use list_mod
type(t_list) testlist
type(n_list), pointer :: ptr
integer i
logical :: res(5) = .FALSE.
allocate(testlist%head)
ptr => testlist%head
do i=1,5
allocate(ptr%next)
ptr => ptr%next
ptr%val = i
end do
! in_list is pure, isn't it?
forall(i=1:5:2) res(i)=in_list(testlist,i)
print*, res
end
This is pure nastiness and is limiting: you no longer have a module procedure; you're not standard conforming; the compiler may be clever and check interfaces (even though it needn't). If the compiler hates you as a result you have only yourself to blame.
Finally, it's all rather a lot of effort to get the procedure pure.
1 This is in Fortran 2008 corresponding to the language revision at the time of asking. In Fortran 2018 the corresponding constraint is C1594.
I have found a solution using recursive functions that is at least Standard conforming. It is neither elegant nor fast, and is limited be the stack depth, but it is working. I'll post it as an answer, although I hope some-one has a better solution...
module list
implicit none
! Node
type n_list
integer :: val
type(n_list),pointer :: next => NULL()
end type
! Linked list
type t_list
type(n_list),pointer :: head
end type
contains
pure function in_list( list, val ) result(res)
implicit none
class(t_list),intent(in) :: list
integer,intent(in) :: val
logical :: res
if ( associated(list%head) ) then
res = in_list_node( list%head, val )
else
res = .false.
endif
end function
recursive pure function in_list_node( node, val ) result(res)
implicit none
class(n_list),intent(in) :: node
integer,intent(in) :: val
logical :: res
if ( node%val == val ) then
res = .true.
elseif ( associated(node%next) ) then
! Recurse
res = in_list_node( node%next, val )
else
res = .false.
endif
end function
end module
program test
use list
implicit none
integer,parameter :: MAXELEM = 100000
integer :: i
type(t_list) :: lst
type(n_list),pointer :: cur
! Fill list
lst%head => NULL()
allocate( lst%head )
lst%head%val = 1
cur => lst%head
do i=2,MAXELEM
allocate( cur%next )
cur%next%val = i
cur => cur%next
enddo !i
print *,'is MAXELEM/2 in list? ', in_list( lst, MAXELEM/2 )
print *,'is MAXELEM+1 in list? ', in_list( lst, MAXELEM+1 )
end program
OK, I found a solution using the transfer intrinsic. The main idea is to clone the list struct (without the data, I checked), and use the pointer to the first node (unchanged) as a start value. Yeah, it is a loop-hole, but both ifort and gfortran accept this without warnings.
module list_mod
implicit none
! Node
type n_list
integer :: val
type(n_list),pointer :: next => NULL()
end type
! Linked list
type t_list
type(n_list),pointer :: head
end type
contains
pure function getHead(list) result(res)
implicit none
class(t_list),intent(in) :: list
type(n_list),pointer :: res
type(t_list),pointer :: listPtr
! Create a copy of pointer to the list struct
allocate( listPtr )
listPtr = transfer( list, listPtr )
! Set the pointer
res => listPtr%head
! Free memory
deallocate( listPtr )
end function
pure function in_list( list, val ) result(res)
implicit none
class(t_list),intent(in) :: list
integer,intent(in) :: val
logical :: res
type(n_list),pointer :: cur
res = .true.
! Traverse the list
cur => getHead(list)
do while ( associated(cur) )
if ( cur%val == val ) return
cur => cur%next
enddo
! Not found
res = .false.
end function
end module
program test
use list_mod
implicit none
integer,parameter :: MAXELEM = 10000000
integer :: i
type(t_list) :: list
type(n_list),pointer :: cur
! Fill list
list%head => NULL()
allocate( list%head )
list%head%val = 1
cur => list%head
do i=2,MAXELEM
allocate( cur%next )
cur%next%val = i
cur => cur%next
enddo !i
print *,'is MAXELEM/2 in list? ', in_list( list, MAXELEM/2 )
print *,'is MAXELEM+1 in list? ', in_list( list, MAXELEM+1 )
end program

DATA Statement issue : Not enough variables

I'm facing difficulties to figure out why my code is giving me this error
error 281 - Not enough variables in DATA statement
I am using the latest Silverfrost on Windows 8. The relevant piece of my module is,
...
INTEGER, parameter :: maxExampleTypes = 5
TYPE ExampleInfo
CHARACTER (len=50) :: ExDeckName
INTEGER A(maxExampleTypes)
INTEGER ExUnits
ENDTYPE ExampleInfo
TYPE(ExampleInfo) :: Example(10)
DATA Example(1)%ExDeckName/'test.dck'/
DATA Example(1)%A/1,2,3,4,5/
...
Curiously, when I only specify one variable for A with
DATA Example(1)%A/1/
the error disappears.
Have you got any idea where it could come from?
I would never use the DATA statement in modern Fortran. Try
...
INTEGER, parameter :: maxExampleTypes = 5
TYPE ExampleInfo
CHARACTER (len=50) :: ExDeckName
INTEGER :: A(maxExampleTypes)
INTEGER :: ExUnits
ENDTYPE ExampleInfo
TYPE(ExampleInfo) :: Example(10)
Example(1)%ExDeckName = 'test.dck'
Example(1)%A = (/ 1,2,3,4,5 /)
...
If the values are supposed to be default values, put them into the type declaration:
...
INTEGER, parameter :: maxExampleTypes = 5
TYPE ExampleInfo
CHARACTER (len=50) :: ExDeckName = 'test.dck'
INTEGER :: A(maxExampleTypes) = (/ 1,2,3,4,5 /)
INTEGER :: ExUnits
ENDTYPE ExampleInfo
TYPE(ExampleInfo) :: Example(10)
...
Sample test program:
module testmod
implicit none
INTEGER, parameter :: maxExampleTypes = 5
! Type with default values
TYPE ExampleInfo
CHARACTER (len=50) :: ExDeckName = 'test.dck'
INTEGER :: A(maxExampleTypes)= (/ 1,2,3,4,5 /)
INTEGER :: ExUnits
ENDTYPE ExampleInfo
contains
subroutine init_ExampleInfo(array)
implicit none
type(ExampleInfo), intent(out):: array(:)
integer :: i
do i=1,size(array)
array(i)%ExDeckName = 'test.dck'
array(i)%A = (/ 1,2,3,4,5 /)
enddo
end subroutine
end module
program test
use testmod
implicit none
TYPE(ExampleInfo) :: Example(10)
! Initialize manually
! call init_ExampleInfo(Example)
write(*,*) Example(1)%ExDeckName, Example(1)%A
! Set new values
Example(1)%ExDeckName = 'test2.dck'
Example(1)%A = (/ 5,4,3,2,1 /)
write(*,*) Example(1)%ExDeckName, Example(1)%A
end program