Commit 6136b15e authored by Martin Beseda's avatar Martin Beseda
Browse files

FIX: Constructors were refactored to return instances instead of pointers.

parent 93e8ea55
......@@ -21,7 +21,7 @@ module connection_m
!------------------!
!> Represents a connection between two neurons.
type :: connection_t
type, extends(abstract_base_t) :: connection_t
private
class(neuron_t), pointer :: input_neuron !< Pointer to an input neuron
......@@ -132,20 +132,18 @@ module connection_m
!> Constructor of connection_t class
!! @param[in] input_neuron Pointer to the input neuron (instance of neuron_t)
!! @param[in] output_neuron Pointer to the output neuron (instance of neuron_t)
!! @return Pointer to the instance of the class connection_t
!! @return An instance of the class connection_t
function new_connection_2(input_neuron, output_neuron) result(new_obj)
class(neuron_t), pointer, intent(in) :: input_neuron
class(neuron_t), pointer, intent(in) :: output_neuron
real(kind=real_4neuro) :: weight
type(connection_t), pointer :: new_obj
type(connection_t) :: new_obj
integer(kind=4) :: values(8) !< values(8) is used as seed
#ifdef TIME_PROFILING
real :: start_time
call time_profiling_start(start_time)
#endif
allocate(new_obj)
! Generate pseudorandom number from Gaussian distribution
! as connection weight
call date_and_time(values=values)
......@@ -161,19 +159,17 @@ module connection_m
!! @param[in] input_neuron Pointer to the input neuron (instance of neuron_t)
!! @param[in] output_neuron Pointer to the output neuron (instance of neuron_t)
!! @param[in] weight Weight of the connection (real number)
!! @return Pointer to the instance of the class connection_t
!! @return An instance of the class connection_t
function new_connection_3(input_neuron, output_neuron, weight) result(new_obj)
class(neuron_t), pointer, intent(in) :: input_neuron
class(neuron_t), pointer, intent(in) :: output_neuron
real(kind=real_4neuro), intent(in) :: weight
type(connection_t), pointer :: new_obj
type(connection_t) :: new_obj
#ifdef TIME_PROFILING
real :: start_time
call time_profiling_start(start_time)
#endif
allocate(new_obj)
call new_obj%init_components(input_neuron, output_neuron, weight)
#ifdef TIME_PROFILING
call time_profiling_stop(start_time, 'new_connection_3')
......@@ -359,19 +355,17 @@ module connection_m
!> Constructor of interval_connection_t class
!! @param[in] input_neuron Pointer to the input neuron (instance of neuron_t)
!! @param[in] output_neuron Pointer to the output neuron (instance of neuron_t)
!! @return Pointer to the instance of the class interval_connection_t
!! @return An instance of the class interval_connection_t
function new_interval_connection_2(input_neuron, output_neuron) result(new_obj)
class(neuron_t), pointer, intent(in) :: input_neuron
class(neuron_t), pointer, intent(in) :: output_neuron
real(kind=real_4neuro) :: weight
type(interval_connection_t), pointer :: new_obj
type(interval_connection_t) :: new_obj
integer(kind=4) :: values(8)
#ifdef TIME_PROFILING
real :: start_time
call time_profiling_start(start_time)
#endif
allocate(new_obj)
call date_and_time(values=values)
weight = r4_normal_01(values(8))
......@@ -385,18 +379,16 @@ module connection_m
!! @param[in] input_neuron Pointer to the input neuron (instance of neuron_t)
!! @param[in] output_neuron Pointer to the output neuron (instance of neuron_t)
!! @param[in] weight Weight of the connection (real number)
!! @return Pointer to the instance of the class interval_connection_t
!! @return An instance of the class interval_connection_t
function new_interval_connection_3(input_neuron, output_neuron, weight) result(new_obj)
class(neuron_t), pointer, intent(in) :: input_neuron
class(neuron_t), pointer, intent(in) :: output_neuron
real(kind=real_4neuro), intent(in) :: weight
type(interval_connection_t), pointer :: new_obj
type(interval_connection_t) :: new_obj
#ifdef TIME_PROFILING
real :: start_time
call time_profiling_start(start_time)
#endif
allocate(new_obj)
call new_obj%init_components(input_neuron, output_neuron, weight)
#ifdef TIME_PROFILING
......
......@@ -4,25 +4,25 @@ program connection_mem_leak_test
use normal_m
use data_kinds_4neuro_m
type(mock_neuron_t), pointer :: n1_p
type(mock_neuron_t), pointer :: n2_p
class(connection_t), pointer :: con1, con2
type(mock_neuron_t), target :: n1, n2
type(mock_neuron_t), pointer :: n1_p, n2_p
type(connection_t) :: con1, con2
print *, '+---------------------------------------------------------+'
print *, '| STARTING MEMORY LEAK TESTING OF THE MODULE CONNECTION_M |'
print *, '+---------------------------------------------------------+'
print *, 'Creating instances of the class neuron_t...'
n1_p => mock_neuron_t()
n2_p => mock_neuron_t()
n1 = mock_neuron_t()
n2 = mock_neuron_t()
n1_p => n1
n2_p => n2
print *, 'Creating an instance of the class interval_connection_t with 2-parameters constructor...'
con2 => connection_t(input_neuron=n1_p, output_neuron=n2_p)
con2 = connection_t(input_neuron=n1_p, output_neuron=n2_p)
print *, 'Creating an instance of the class interval_connection_t with 3-parameters constructor...'
con1 => connection_t(input_neuron=n1_p, output_neuron=n2_p, weight=real(5.25, real_4neuro))
con1 = connection_t(input_neuron=n1_p, output_neuron=n2_p, weight=real(5.25, real_4neuro))
deallocate(con1)
deallocate(con2)
end program connection_mem_leak_test
......@@ -86,7 +86,7 @@ module neuron_m
interface mock_neuron_t
!> Constructor of mock_neuron_t class
!! @return Returns pointer to the instance of the class mock_neuron_t
!! @return Returns an instance of the class mock_neuron_t
module procedure :: new_mock_neuron
end interface mock_neuron_t
......@@ -113,12 +113,12 @@ module neuron_m
interface binary_neuron_t
!> Non-parametric constructor of binary_neuron_t class (threshold
!! will be initialized by a random number from Gaussian distribution)
!! @return Pointer to the instance of the class binary_neuron_t
!! @return An instance of the class binary_neuron_t
module procedure :: new_binary_neuron
!> Parametric constructor of binary_neuron_t class
!! @param[in] threshold Threshold for the unit-step activation function
!! @return Pointer to the instance of the class binary_neuron_t
!! @return An instance of the class binary_neuron_t
module procedure :: new_binary_neuron_1
end interface binary_neuron_t
......@@ -143,13 +143,13 @@ module neuron_m
interface linear_neuron_t
!> Non-parametric constructor of linear_neuron_t class - a coef. is 1, b coef. is 0
!! @return Pointer to the instance of the class linear_neuron_t
!! @return An instance of the class linear_neuron_t
module procedure :: new_linear_neuron
!> Constructor of linear_neuron_t class
!! @param[in] a_coef a coef. of the linear activation function
!! @param[in] b_coef b coef. of the linear activation fucntion
!! @return Pointer to the instance of the class linear_neuron_t
!! @return An instance of the class linear_neuron_t
module procedure :: new_linear_neuron_2
end interface linear_neuron_t
......@@ -174,12 +174,12 @@ module neuron_m
interface logistic_neuron_t
!> Non-parametric constructor of logistic_neuron_t class
!! Alpha coefficient is set to 1
!! @return Pointer to the instance of the class logistic_neuron_t
!! @return An instance of the class logistic_neuron_t
module procedure :: new_logistic_neuron
!> Constructor of the logistic_neuron_t class
!! @param[in] alpha_coef Alpha coefficient in the logistic activation function
!! @return Pointer to the instance of the class logistic_neuron_t
!! @return An instance of the class logistic_neuron_t
module procedure :: new_logistic_neuron_1
end interface logistic_neuron_t
......@@ -337,14 +337,13 @@ module neuron_m
!--------------!
!> Constructor of mock_neuron_t class
!! @return Returns pointer to the instance of the class mock_neuron_t
!! @return Returns an instance of the class mock_neuron_t
function new_mock_neuron() result(new_obj)
class(mock_neuron_t), pointer :: new_obj
type(mock_neuron_t) :: new_obj
#ifdef TIME_PROFILING
real :: start_time
call time_profiling_start(start_time)
#endif
allocate(new_obj)
call new_obj%init_components()
#ifdef TIME_PROFILING
call time_profiling_stop(start_time, 'new_mock_neuron')
......@@ -396,15 +395,14 @@ module neuron_m
!> Non-parametric constructor of binary_neuron_t class (threshold
!! will be initialized by a random number from Gaussian distribution)
!! @return Pointer to the instance of the class binary_neuron_t
!! @return An instance of the class binary_neuron_t
function new_binary_neuron() result(new_obj)
type(binary_neuron_t), pointer :: new_obj
integer(kind=4) :: values(8)
type(binary_neuron_t) :: new_obj
integer(kind=4) :: values(8)
#ifdef TIME_PROFILING
real :: start_time
real :: start_time
call time_profiling_start(start_time)
#endif
allocate(new_obj)
call new_obj%init_components()
! Get current time and use milliseconds (values(8))
......@@ -421,16 +419,14 @@ module neuron_m
!> Constructor of binary_neuron_t class
!! @param[in] threshold Threshold used by unit-step activation function
!! @return Pointer to the instance of the class binary_neuron_t
!! @return An instance of the class binary_neuron_t
function new_binary_neuron_1(threshold) result(new_obj)
real(kind=real_4neuro), intent(in) :: threshold
type(binary_neuron_t), pointer :: new_obj
type(binary_neuron_t) :: new_obj
#ifdef TIME_PROFILING
real :: start_time
call time_profiling_start(start_time)
#endif
allocate(new_obj)
call new_obj%init_components()
new_obj%threshold = threshold
#ifdef TIME_PROFILING
......@@ -473,15 +469,13 @@ module neuron_m
!--------------!
!> Non-parametric constructor of linear_neuron_t class - a coef. is 1, b coef. is 0
!! @return Pointer to the instance of the class linear_neuron_t
!! @return An instance of the class linear_neuron_t
function new_linear_neuron() result(new_obj)
type(linear_neuron_t), pointer :: new_obj
type(linear_neuron_t) :: new_obj
#ifdef TIME_PROFILING
real :: start_time
real :: start_time
call time_profiling_start(start_time)
#endif
allocate(new_obj)
call new_obj%init_components()
new_obj%a_coef = 1
......@@ -495,18 +489,16 @@ module neuron_m
!> Constructor of linear_neuron_t class
!! @param[in] a_coef a coef. of the linear activation function
!! @param[in] b_coef b coef. of the linear activation fucntion
!! @return Pointer to the instance of the class linear_neuron_t
!! @return An instance of the class linear_neuron_t
function new_linear_neuron_2(a_coef, b_coef) result(new_obj)
real(kind=real_4neuro), intent(in) :: a_coef
real(kind=real_4neuro), intent(in) :: b_coef
type(linear_neuron_t), pointer :: new_obj
type(linear_neuron_t) :: new_obj
#ifdef TIME_PROFILING
real :: start_time
real :: start_time
call time_profiling_start(start_time)
#endif
allocate(new_obj)
call new_obj%init_components()
new_obj%a_coef = a_coef
......@@ -546,15 +538,13 @@ module neuron_m
!> Non-parametric constructor of logistic_neuron_t class
!! Alpha coefficient is set to 1
!! @return Pointer to the instance of the class logistic_neuron_t
!! @return An instance of the class logistic_neuron_t
function new_logistic_neuron() result(new_obj)
type(logistic_neuron_t), pointer :: new_obj
type(logistic_neuron_t) :: new_obj
#ifdef TIME_PROFILING
real :: start_time
real :: start_time
call time_profiling_start(start_time)
#endif
allocate(new_obj)
call new_obj%init_components()
new_obj%alpha_coef = 1
......@@ -565,16 +555,14 @@ module neuron_m
!> Constructor of the logistic_neuron_t class
!! @param[in] alpha_coef Alpha coefficient in the logistic activation function
!! @return Pointer to the instance of the class logistic_neuron_t
!! @return An instance of the class logistic_neuron_t
function new_logistic_neuron_1(alpha_coef) result(new_obj)
real(kind=real_4neuro), intent(in) :: alpha_coef
type(logistic_neuron_t), pointer :: new_obj
type(logistic_neuron_t) :: new_obj
#ifdef TIME_PROFILING
real :: start_time
call time_profiling_start(start_time)
#endif
allocate(new_obj)
call new_obj%init_components()
new_obj%alpha_coef = alpha_coef
......@@ -591,7 +579,7 @@ module neuron_m
subroutine logistic_activate_impl(this)
class(logistic_neuron_t), intent(inout) :: this
#ifdef TIME_PROFILING
real :: start_time
real :: start_time
call time_profiling_start(start_time)
#endif
this%state = (1.0 + exp(-1*this%potential))**(-1*this%alpha_coef)
......@@ -610,13 +598,11 @@ module neuron_m
!> Constructor for an instance of the class tanh_neuron_t
function new_tanh_neuron() result(new_obj)
class(tanh_neuron_t), pointer :: new_obj
type(tanh_neuron_t) :: new_obj
#ifdef TIME_PROFILING
real :: start_time
real :: start_time
call time_profiling_start(start_time)
#endif
allocate(new_obj)
call new_obj%init_components()
#ifdef TIME_PROFILING
......@@ -657,13 +643,11 @@ module neuron_m
!> Constructor for an instance of the class arctan_neuron_t
function new_arctan_neuron() result(new_obj)
class(arctan_neuron_t), pointer :: new_obj
type(arctan_neuron_t) :: new_obj
#ifdef TIME_PROFILING
real :: start_time
real :: start_time
call time_profiling_start(start_time)
#endif
allocate(new_obj)
call new_obj%init_components()
#ifdef TIME_PROFILING
......
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