Commit fb925c53 authored by Martin Beseda's avatar Martin Beseda
Browse files

ENH: Added overloaded 'write' and 'read' implementations

to connection_m, net_m and neuron_m to make serializing instances of connection_t, net_t and neuron_t
possible despite having allocatable and pointer components. NOT WORKING YET!

Partly implemented method new_mock_net() in the module net_m. NOT FULLY DONE YET!
parent 6136b15e
......@@ -64,6 +64,17 @@ module connection_m
!! to an output neuron.
procedure :: pass_signal => pass_signal_impl
!> Implementation of write function enabling the storage of
!! allocatable arrays.
procedure :: write_sample => write_sample_impl
!> Implementation of read function enabling to read
!! the whole instance of net_t stored as binary file.
procedure :: read_sample => read_sample_impl
generic :: write(unformatted) => write_sample
generic :: read(unformatted) => read_sample
!> Scalar desctructor for single instances of the class connection_t
final :: destroy_connection
......@@ -336,12 +347,31 @@ module connection_m
#endif
end subroutine pass_signal_impl
!----------------!---------------------------------------------------------------------
! Common methods !
!----------------!
! empty !
!----------------!
!> Implementation of write function enabling the storage of
!! allocatable arrays.
!! @param[in] unit Unit (handler) where the file is written
!! @param[out] iostat Diagnostic value "returned" by the subroutine
!! @param[inout] iomsg Explaining note about error
subroutine write_sample_impl(this, unit, iostat, iomsg)
class(connection_t), intent(in) :: this
integer, intent(in) :: unit
integer, intent(out) :: iostat
character(*), intent(inout) :: iomsg
integer :: i !< Counter
#ifdef TIME_PROFILING
real :: start_time
call time_profiling_start(start_time)
#endif
! Write a record giving sizes for the allocation
write(unit, iostat=iostat, iomsg=iomsg) SIZE(this%input_neuron), &
SIZE(this%output_neuron)
write(unit, iostat=iostat, iomsg=iomsg) this%input_neuron, &
this%output_neuron, &
this%weight
#ifdef TIME_PROFILING
call time_profiling_stop(start_time, 'write_connection_sample')
#endif
end subroutine write_sample_impl
!-----------------------------!
......
......@@ -6,11 +6,11 @@
!! @author Martin Beseda
!! @date 2018
module net_m
!use interconnection_m
use data_kinds_4neuro_m
use time_measurement_m
use neuron_m
use connection_m
use container_m
implicit none
......@@ -24,14 +24,14 @@ module net_m
type :: net_t
private
character(:), allocatable :: net_type !< Type of the net
integer(kind=integer_4neuro) :: num_of_neurons !< Number of neurons in the net
character(:), allocatable :: training_method !< Used training method
character(:), allocatable :: net_type !< Type of the net
integer(kind=integer_4neuro) :: num_of_neurons !< Number of neurons in the net
character(:), allocatable :: training_method !< Used training method
class(neuron_t), allocatable :: neuron_arr !< Array containing all neurons
integer(kind=integer_4neuro), allocatable :: input_neuron_arr !< Array of input neuron indices
integer(kind=integer_4neuro), allocatable :: output_neuron_arr !< Array of output neuron indices
class(connection_t), allocatable :: connection_arr !< Array of all connections
class(neuron_t), allocatable :: neuron_arr(:) !< Array containing all neurons
integer(kind=integer_4neuro), allocatable :: input_neuron_arr(:) !< Array of input neuron indices
integer(kind=integer_4neuro), allocatable :: output_neuron_arr(:) !< Array of output neuron indices
class(connection_t), allocatable :: connection_arr(:) !< Array of all connections
contains
!> Prints information about the network to the standard output.
......@@ -40,13 +40,23 @@ module net_m
!> Saves the network instance to the Fortran binary file
procedure :: save_net_bin => save_net_bin_impl
!> Implementation of write function enabling the storage of
!! allocatable arrays.
procedure :: write_sample => write_sample_impl
!> Implementation of read function enabling to read
!! the whole instance of net_t stored as binary file.
procedure :: read_sample => read_sample_impl
generic :: write(unformatted) => write_sample
generic :: read(unformatted) => read_sample
end type net_t
interface net_t
!> Constructor of net_t class
!! Loads complete info about network from file
!! @oaram[in] filepath Path to the file with network configuration
!! @return Returns pointer to the new instance of the class net_t
!! @param[in] filepath Path to the file with network configuration
!! @return An instance of the class net_t
module procedure :: new_net_1
end interface net_t
......@@ -61,7 +71,7 @@ module net_m
interface mock_net_t
!> Non-parametric constructor of mock_net_t class
!! @return Returns pointer to the instance of the class mock_net_t
!! @return An instance of the class mock_net_t
module procedure :: new_mock_net
end interface mock_net_t
......@@ -80,24 +90,21 @@ contains
!> Constructor of net_t class
!! Loads complete info about network from Fortran binary file
!! @oaram[in] filepath Path to the JSON file with network configuration
!! @return Returns pointer to the instance of the class net_t
!! @param[in] filepath Path to the JSON file with network configuration
!! @return An instance of the class net_t
function new_net_1(filepath) result(new_obj)
character(len=*), intent(in) :: filepath
class(net_t), pointer :: new_obj
type(net_t) :: new_obj
character(len=50) :: str !TODO udelat dynamicky, ne s fixni delkou
#ifdef TIME_PROFILING
real :: start_time
call time_profiling_start(start_time)
#endif
allocate(new_obj)
open(unit=11, file=filepath, form="unformatted", access="stream")
read(11) new_obj
close(11)
open(unit=11, file=filepath, form="unformatted", status="old")
read(11) str
read(11) new_obj%num_of_neurons
print *,str
new_obj%net_type = str
print *,new_obj%net_type
!TODO dopsat
#ifdef TIME_PROFILING
......@@ -114,7 +121,7 @@ contains
#endif
write(*,*) '+--------------+'
write(*,*) '| Network info |'
write(*,*) '+--------------|'
write(*,*) '+--------------+'
write(*,*) 'Type: ', this%net_type
write(*,*) 'Number of neurons: ', this%num_of_neurons
!TODO dopsat
......@@ -128,18 +135,94 @@ contains
!----------------!
!> Saves the network instance to the Fortran binary file
!! @param[in] filename Name of the file, where the net will be saved into
subroutine save_net_bin_impl(this, filename)
class(net_t), intent(in) :: this !< Instance of the class 'net_t'
character(len=*), intent(in) :: filename !< Name of the file, where the net will be saved into
character(len=*), intent(in) :: filename
#ifdef TIME_PROFILING
real :: start_time
call time_profiling_start(start_time)
#endif
open(unit=11, file=filename, form="unformatted", access='stream', position='append')
write(11) this
close(unit=11)
#ifdef TIME_PROFILING
call time_profiling_stop(start_time, 'save_net_bin_impl')
#endif
end subroutine save_net_bin_impl
open(unit=11, file=filename, form="unformatted")
!> Implementation of write function enabling the storage of
!! allocatable arrays.
!! @param[in] unit Unit (handler) where the file is written
!! @param[out] iostat Diagnostic value "returned" by the subroutine
!! @param[inout] iomsg Explaining note about error
subroutine write_sample_impl(this, unit, iostat, iomsg)
class(net_t), intent(in) :: this
integer, intent(in) :: unit
integer, intent(out) :: iostat
character(*), intent(inout) :: iomsg
integer :: i !< Counter
#ifdef TIME_PROFILING
real :: start_time
call time_profiling_start(start_time)
#endif
! Write a record giving sizes for the allocation
write(unit, iostat=iostat, iomsg=iomsg) SIZE(this%neuron_arr), &
SIZE(this%input_neuron_arr), &
SIZE(this%output_neuron_arr), &
SIZE(this%connection_arr)
write(unit, iostat=iostat, iomsg=iomsg) (this%neuron_arr(i)%get_id(), &
this%neuron_arr(i)%get_potential(), &
this%neuron_arr(i)%get_state(), &
i=1,SIZE(this%neuron_arr)), &
this%input_neuron_arr, &
this%output_neuron_arr, &
(this%connection_arr(i)%get_input_neuron(), &
this%connection_arr(i)%get_output_neuron(), &
this%connection_arr(i)%get_weight(), &
i=1,SIZE(this%connection_arr))
#ifdef TIME_PROFILING
call time_profiling_stop(start_time, 'write_net_sample')
#endif
end subroutine write_sample_impl
write(11) this%net_type
write(11) this%num_of_neurons
! Unformatted reading for the sample derived type
subroutine read_sample_impl(this, unit, iostat, iomsg)
class(net_t), intent(inout) :: this
integer, intent(in) :: unit
integer, intent(out) :: iostat
character(*), intent(inout) :: iomsg
integer :: i
integer :: s1, s2, s3, s4 !< Sizes of stored allocatable properties
#ifdef TIME_PROFILING
real :: start_time
call time_profiling_start(start_time)
#endif
! We first have a record telling us the sizes of components
read(unit, iostat=iostat, iomsg=iomsg) s1, s2, s3, s4
close(unit=11)
end subroutine save_net_bin_impl
! So we do the allocation
allocate(this%neuron_arr(s1), &
this%input_neuron_arr(s2), &
this%output_neuron_arr(s3), &
this%connection_arr(s4))
! And then finally the reading.
read(unit, iostat=iostat, iomsg=iomsg) (this%neuron_arr(i)%id, &
this%neuron_arr(i)%potential, &
this%neuron_arr(i)%state, &
i=1, SIZE(this%neuron_arr)), &
this%input_neuron_arr, &
this%output_neuron_arr, &
(this%connection_array(i)%input_neuron, &
this%connection_array(i)%output_neuron, &
this%connection_array(i)%weight, &
i=1, SIZE(this%connection_array))
#ifdef TIME_PROFILING
call time_profiling_stop(start_time, 'read_net_sample')
#endif
end subroutine read_sample_impl
!------------------!---------------------------------------------------------------------
! class mock_net_t !
......@@ -150,17 +233,41 @@ contains
!--------------!
!> Contructor of mock_net_t class
!! @return Returns pointer to the instance of the class mock_net_t
!! @return An instance of the class mock_net_t
function new_mock_net() result(new_obj)
class(net_t), pointer :: new_obj
type(mock_net_t) :: new_obj
integer :: i !< Counter
class(neuron_t), pointer :: n_p1, n_p2
class(neuron_t), pointer :: n_p(:)
#ifdef TIME_PROFILING
real :: start_time
real :: start_time
call time_profiling_start(start_time)
#endif
allocate(new_obj)
new_obj%net_type = 'MOCK NETWORK'
new_obj%num_of_neurons = 5
new_obj%training_method = 'MOCK TRAINING'
! Init object
allocate(mock_neuron_t :: new_obj%neuron_arr(5))
select type(tmp => new_obj%neuron_arr)
type is (mock_neuron_t)
do i=1,5
tmp(i) = mock_neuron_t()
end do
end select
new_obj%input_neuron_arr = (/1, 2/)
new_obj%output_neuron_arr = (/ 5 /)
allocate(connection_t :: new_obj%connection_arr(4))
! n_p => new_obj%neuron_arr
! new_obj%connection_arr(1) = connection_t(n_p(1), n_p(3))
! connection_arr_tmp(2)%content => connection_t(neuron_arr_tmp(2)%content, neuron_arr_tmp(4))
! connection_arr_tmp(3)%content => connection_t(neuron_arr_tmp(3)%content, neuron_arr_tmp(5))
! connection_arr_tmp(4)%content => connection_t(neuron_arr_tmp(4)%content, neuron_arr_tmp(5))
!TODO dopsat init zbylych polozek
#ifdef TIME_PROFILING
call time_profiling_stop(start_time, 'new_mock_net')
......
......@@ -52,6 +52,26 @@ module neuron_m
!! @return Neuron ID
procedure :: get_id => get_id_impl
!> Setter to the 'potential' component
procedure :: set_potential => set_potential_impl
!> Setter to the 'state' component
procedure :: set_state => set_state_impl
!> Setter to the 'id' component
procedure :: set_id => set_id_impl
!> Implementation of write function enabling the storage of
!! allocatable arrays.
procedure :: write_sample => write_sample_impl
!> Implementation of read function enabling to read
!! the whole instance of net_t stored as binary file.
procedure :: read_sample => read_sample_impl
generic :: write(unformatted) => write_sample
generic :: read(unformatted) => read_sample
end type neuron_t
abstract interface
......@@ -224,9 +244,9 @@ module neuron_m
! class neuron_t !
!----------------!
!---------!------------------------------------------------------------------------------------
! Getters !
!---------!
!-------------------!--------------------------------------------------------------------------
! Getters & Setters !
!-------------------!
!> Getter to the 'potential' component
!! @return Value of the neuron's potential (real number)
......@@ -274,6 +294,52 @@ module neuron_m
#endif
end function get_id_impl
!> Setter to the 'potential' component
!! @param[in] potential The value of potential to be set
subroutine set_potential_impl(this, potential)
class(neuron_t), intent(in) :: this
real(kind=real_4neuro), intent(in) :: potential
#ifdef TIME_PROFILING
real :: start_time
call time_profiling_start(start_time)
#endif
this%potential = potential
#ifdef TIME_PROFILING
call time_profiling_stop(start_time, 'set_potential_impl')
#endif
end subroutine set_potential_impl
!> Setter to the 'state' component
!! @param[in] state The state to be set
subroutine set_state_impl(this, state)
class(neuron_t), intent(in) :: this
real(kind=real_4neuro), intent(in) :: state
#ifdef TIME_PROFILING
real :: start_time
call time_profiling_start(start_time)
#endif
this%state = state
#ifdef TIME_PROFILING
call time_profiling_stop(start_time, 'set_state_impl')
#endif
end subroutine set_state_impl
!> Setter to the 'id' component
!! @param[in] id The ID of neuron to be set
subroutine set_id_impl(this, id)
class(neuron_t), intent(in) :: this
integer(kind=integer_4neuro), intent(in) :: id
#ifdef TIME_PROFILING
real :: start_time
call time_profiling_start(start_time)
#endif
this%id = id
#ifdef TIME_PROFILING
call time_profiling_stop(start_time, 'set_id_impl')
#endif
end subroutine set_id_impl
!----------------!-----------------------------------------------------------------------------
! Common methods !
!----------------!
......@@ -311,6 +377,29 @@ module neuron_m
#endif
end subroutine adjust_potential_impl
!> Implementation of write function enabling the storage of
!! allocatable arrays.
!! @param[in] unit Unit (handler) where the file is written
!! @param[out] iostat Diagnostic value "returned" by the subroutine
!! @param[inout] iomsg Explaining note about error
subroutine write_sample_impl(this, unit, iostat, iomsg)
class(neuron_t), intent(in) :: this
integer, intent(in) :: unit
integer, intent(out) :: iostat
character(*), intent(inout) :: iomsg
integer :: i !< Counter
#ifdef TIME_PROFILING
real :: start_time
call time_profiling_start(start_time)
#endif
write(unit, iostat=iostat, iomsg=iomsg) this%potential, &
this%state, &
this%id
#ifdef TIME_PROFILING
call time_profiling_stop(start_time, 'write_neuron_sample')
#endif
end subroutine write_sample_impl
!---------------------!
! class mock_neuron_t !
!---------------------!
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment