Skip to content

Commit

Permalink
work on using more portable type constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Oct 2, 2024
1 parent 03e18e4 commit 56a6af7
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 32 deletions.
31 changes: 31 additions & 0 deletions docs/src/fortran/lib/constants.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
constants.f90
=============

View source code :source:`fortran/include/constants.f90`

.. f:module:: constants
.. f:variable:: i1t
:type: integer
.. f:variable:: i4t
:type: integer
.. f:variable:: i19t
:type: integer

A set of digit type constraints that denote how many decimal digits a number can support.
They are given as ``i<len>t`` or ``r<len>_<exp>t``

.. f:variable:: errort
:type: integer(i1t)
.. f:variable:: int64t
:type: integer(i1t)
.. f:variable:: stringt
:type: integer(i1t)

Denotes the type of an :f:type:`AnswerT`

.. literalinclude:: ../../../../fortran/src/include/constants.f90
:language: Fortran
:linenos:

.. tags:: constants
17 changes: 4 additions & 13 deletions docs/src/fortran/lib/utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,20 @@ View source code :source:`fortran/include/utils.f90`
Return the answer to a given problem, as represented in ``/_data/answers.tsv``

:p integer(kind=4) id:
:p integer(i4d) id:
:returns answer:
:rtype answer: AnswerT

.. f:type:: AnswerT
This stores the answer to a generic problem, storing multiple potential types. If the type field contains
:f:var:`errort`, there was an error in generating the answer. If it is :f:var:`int64t`, it holds data of
type ``integer(kind=8)``. If it is :f:var:`stringt`, it holds an allocatable character array.
type ``integer(i19t)``. If it is :f:var:`stringt`, it holds an allocatable character array.

:f integer(kind=8) int_value:
:f integer(i19t) int_value:
:f character(len=:) string_value:
:fattrs string_value: allocatable
:f integer(kind=1) type:

.. f:variable:: errort
:type: integer(kind=1)
.. f:variable:: int64t
:type: integer(kind=1)
.. f:variable:: stringt
:type: integer(kind=1)

Denotes the type of an :f:type:`AnswerT`
:f integer(i1d) type:

.. literalinclude:: ../../../../fortran/src/include/utils.f90
:language: Fortran
Expand Down
7 changes: 5 additions & 2 deletions fortran/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ help:
@echo " $(BLUE)test_auto$(NC) Run through all Fortran tests (parallel execution not implemented)."
@echo " $(BLUE)clean$(NC) Clean up any stray files."

$(BUILD_DIR)/%.o: $(SRC_DIR)/%.f90
$(BUILD_DIR)/include/constants.o: $(SRC_DIR)/include/constants.f90
@mkdir -p $(BUILD_DIR)/include
$(FC) $(opt_args) $(cov_args) -c $< -o $@
$(FC) $(opt_args) $(cov_args) -c $(SRC_DIR)/include/constants.f90 -o $(BUILD_DIR)/include/constants.o

$(BUILD_DIR)/%.o: $(SRC_DIR)/%.f90 $(BUILD_DIR)/include/constants.o
$(FC) $(opt_args) $(cov_args) $(BUILD_DIR)/include/constants.o -c $< -o $@

test_runner: ../LICENSE
@$(MAKE) $(OBJ_FILES) $(MFLAGS) -j --no-print-directory
Expand Down
14 changes: 14 additions & 0 deletions fortran/src/include/constants.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module constants
implicit none

integer, parameter :: i1t = selected_int_kind(1)
integer, parameter :: i4t = selected_int_kind(4)
integer, parameter :: i19t = selected_int_kind(19)

integer(i1t), parameter :: errort = 0
integer(i1t), parameter :: int64t = 1
integer(i1t), parameter :: stringt = 2

contains
end module constants

18 changes: 8 additions & 10 deletions fortran/src/include/utils.f90
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
module utils
implicit none
use constants

integer(kind=1), parameter :: errort = 0
integer(kind=1), parameter :: int64t = 1
integer(kind=1), parameter :: stringt = 2
implicit none

type :: AnswerT
integer(kind=8) :: int_value
integer(i19t) :: int_value
character(len=:), allocatable :: string_value
integer(kind=1) :: type
integer(i1t) :: type
end type AnswerT

logical(kind=1) :: cache_inited = .false.
logical :: cache_inited = .false.
type(AnswerT), dimension(1024) :: cached_answers

contains
Expand Down Expand Up @@ -52,9 +50,9 @@ end function get_data_file

function get_answer(id) result(answer)
type(AnswerT) :: answer
integer(kind=4), intent(in) :: id
integer(kind=4) :: ios, row_start, row_end, line_length
integer(kind=8) :: i, j
integer(i4t), intent(in) :: id
integer(i19t) :: i, j
integer :: ios, row_start, row_end, line_length
character(len=:), allocatable :: text
character(len=32) :: val
character(len=4) :: id_, type_, length
Expand Down
14 changes: 7 additions & 7 deletions fortran/test.f90
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
program test
use constants
use utils
use Problem0001
use Problem0002
Expand All @@ -10,8 +11,8 @@ program test
use Problem0836

implicit none
integer(kind=4), dimension(:), allocatable :: problem_ids
logical(kind=1), dimension(:), allocatable :: long_runtime
integer(i4t), dimension(:), allocatable :: problem_ids
logical, dimension(:), allocatable :: long_runtime
integer :: num_problems

num_problems = 8
Expand Down Expand Up @@ -48,11 +49,10 @@ program test
contains

subroutine process_problems(problem_ids, long_runtime)
integer(kind=4), dimension(:), intent(in) :: problem_ids
logical(kind=1), dimension(:), intent(in) :: long_runtime
integer(i4t), dimension(:), intent(in) :: problem_ids
logical, dimension(:), intent(in) :: long_runtime
type(AnswerT) :: expected, answer
integer(kind=4) :: i
integer :: first_count, second_count, count_rate, count_max, tmp
integer :: i, first_count, second_count, count_rate, count_max, tmp
real :: time_elapsed

! Loop through each problem
Expand Down Expand Up @@ -119,7 +119,7 @@ subroutine process_problems(problem_ids, long_runtime)
end subroutine process_problems

type(AnswerT) function select_function(problem_id) result(answer)
integer(kind=4), intent(in) :: problem_id
integer(i4t), intent(in) :: problem_id

answer%type = int64t
select case (problem_id)
Expand Down

0 comments on commit 56a6af7

Please sign in to comment.