Skip to content
Snippets Groups Projects
Commit 8cf3bf07 authored by Martin Beseda's avatar Martin Beseda
Browse files

ENH: Fortran codes removed to the folder 'fortran2008' and CMakeLists.txt edited accordingly.

parent 3973612d
No related branches found
No related tags found
No related merge requests found
......@@ -2,17 +2,6 @@ cmake_minimum_required(VERSION 3.0)
project(4neuro)
#message ("Before enable language")
##enable_language(Fortran)
#if (WIN32)
# message ("cmake for " ${CMAKE_Fortran_COMPILER})
# set (CMAKE_FORTRAN_COMPILER ${CMAKE_Fortran_COMPILER})
# project(4Neuro)
#else ()
# project(4Neuro)
#endif ()
#message ("Start cmakeList")
#-------------------------------#
# Default installation location #
#-------------------------------#
......@@ -39,7 +28,6 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall" )
#--------------------#
# Automatic settings #
#--------------------#
#get_filename_component (Fortran_COMPILER_NAME ${CMAKE_Fortran_COMPILER} NAME)
# Processing user variables
if (WITH_TIME_PROFILING)
......@@ -53,31 +41,29 @@ if (WIN32)
endif()
# Write compiler variables to the file - to pass them to test script
#file(WRITE compilers.env "export FC=${CMAKE_Fortran_COMPILER}\n")
file(APPEND compilers.env "export CXX=${CMAKE_CXX_COMPILER}\n")
file(APPEND compilers.env "export CC=${CMAKE_C_COMPILER}\n")
#----------------#
# User variables #
#----------------#
set(SRC_DIR src)
set(BUILD_DIR build)
set(LIB_DIR lib)
set(PROJECT_BINARY_DIR build)
#--------------------#
# Building libraries #
#--------------------#
#link_directories("${BUILD_DIR}/${LIB_DIR}")
include_directories("${BUILD_DIR}/${LIB_DIR}")
add_subdirectory("${SRC_DIR}" "${LIB_DIR}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY bin)
add_subdirectory(${SRC_DIR} ${PROJECT_BINARY_DIR})
message ("Current directory:" ${PWD})
message ("SRC_DIR: " ${SRC_DIR})
message ("BUILD_DIR:" ${BUILD_DIR})
message ("LIB_DIR: " ${LIB_DIR})
if (WIN32)
message ("Windows")
......
......@@ -68,12 +68,7 @@ case `uname -s` in
esac
#-------------------------------------------------------------------------
echo "Creating folder 'build'...";
mkdir -p build/lib;
echo "Folder 'build' was created'";
cd build;
#cmake -G "MSYS Makefiles" -DCMAKE_Fortran_COMPILER=${FORTRAN_COMPILER} -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DWITH_TIME_PROFILING:BOOLEAN=${WITH_TIME_PROFILING} ..
cmake -G "${MAKEFILE_TYPE}" -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DWITH_TIME_PROFILING:BOOLEAN=${WITH_TIME_PROFILING} ..
rm -rf build;
cmake -G "${MAKEFILE_TYPE}" -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DWITH_TIME_PROFILING:BOOLEAN=${WITH_TIME_PROFILING} .
make VERBOSE=1 && echo "Build complete." || exit -1;
#make install;
......@@ -5,7 +5,7 @@
#include "NeuronBinary.h"
NeuronBinary::NeuronBinary(double threshold) {
this->n_activation_function_parameters = 2;
this->activation_function_parameters = new double[1];
this->activation_function_parameters[0] = threshold;
......
!> Module containing a class abstract_base_t, which is a parental
!! class for all other classes in 4neuro. It makes creating a
!! common container for all classes possible.
!!
!! @author Martin Beseda
!! @date 2018
module abstract_base_m
implicit none
public
!------------------!-----------------------------------------
! Type definitions !
!------------------!
!-----------------------!------------------------------------
! class abstract_base_t !
!-----------------------!
!> Abstract class covering all the other classes in 4neuro
type, abstract :: abstract_base_t
end type
end module abstract_base_m
This diff is collapsed.
program connection_mem_leak_test
use connection_m
use neuron_m
use normal_m
use data_kinds_4neuro_m
type(mock_neuron_t), target :: n1, n2
class(neuron_t), pointer :: n1_p, n2_p
class(connection_t), allocatable :: n_t
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 = 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)
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))
open(123,file='connection.test', form='unformatted')
write(123) con1
close(123)
!open(123,file='connection.test', form='unformatted')
! read(123) n_t
!close(123)
end program connection_mem_leak_test
!> Module containing declaration of a container type 'container_t'.
!! This class "wraps" pointers, so it's possible to store them
!! into arrays.
!!
!! @author Martin Beseda
!! @date 2018
module container_m
use abstract_base_m
use data_kinds_4neuro_m
implicit none
public
!> Represents a container for a single container_t pointer
type :: container_t
! Variable content is public to make usage of this class with one
! specific purpose as simple as possible.
class(*), pointer, public :: content
contains
!> Scalar desctructor for single instances of the class container_t
final :: destroy_container
!> Array desctructor for arrays of instances of the class container_t
final :: destroy_container_array
end type container_t
interface container_t
!> Constructor of container_t class
!! @return Instance of the class container_t with nullified content
module procedure :: new_container_empty
!> Constructor of container_t class
!! @param[in] content_in container to be contained (pointer)
!! @return Pointer to the instance of the class container_t with assigned content
module procedure :: new_container_assigned
end interface container_t
contains
!--------------!------------------------------------------------------------------------
! Constructors !
!--------------!
!> Constructor of container_t class
!! @return Instance of the class container_t with nullified content
function new_container_empty() result(new_obj)
type(container_t) :: new_obj
#ifdef TIME_PROFILING
real :: start_time
call time_profiling_start(start_time)
#endif
new_obj%content => null()
#ifdef TIME_PROFILING
call time_profiling_stop(start_time, 'new_container_empty')
#endif
end function new_container_empty
!> Constructor of container_t class
!! @param[in] content_in connection to be contained (pointer)
!! @return Pointer to the instance of the class connection_t with assigned content
function new_container_assigned(content_in) result(new_obj)
class(abstract_base_t), pointer, intent(in) :: content_in
type(container_t) :: new_obj
#ifdef TIME_PROFILING
real :: start_time
call time_profiling_start(start_time)
#endif
new_obj%content => content_in
#ifdef TIME_PROFILING
call time_profiling_stop(start_time, 'new_container_assigned')
#endif
end function new_container_assigned
!--------------!------------------------------------------------------------------------
! Destructors !
!--------------!
!> Scalar desctructor for single instances of the class container_t
subroutine destroy_container(this)
type(container_t), intent(inout) :: this
#ifdef TIME_PROFILING
real :: start_time
call time_profiling_start(start_time)
#endif
nullify(this%content)
#ifdef TIME_PROFILING
call time_profiling_stop(start_time, 'destroy_container')
#endif
end subroutine destroy_container
!> Array desctructor for arrays of instances of the class container_t
subroutine destroy_container_array(this)
type(container_t), intent(inout) :: this(:)
integer(kind=integer_4neuro) :: i
#ifdef TIME_PROFILING
real :: start_time
call time_profiling_start(start_time)
#endif
do i = 1, size(this)
nullify(this(i)%content)
end do
#ifdef TIME_PROFILING
call time_profiling_stop(start_time, 'destroy_container_array')
#endif
end subroutine destroy_container_array
end module container_m
#define SINGLE 4
#define DOUBLE 8
# define PREC DOUBLE
module data_kinds_4neuro_m
integer, parameter :: integer_4neuro = PREC
integer, parameter :: real_4neuro = PREC
end module data_kinds_4neuro_m
!> Module containing classes representing the whole
!! neural network.
!!
!! It uses 'neuron_m' module.
!!
!! @author Martin Beseda
!! @date 2018
module net_m
use data_kinds_4neuro_m
use time_measurement_m
use neuron_m
use connection_m
use container_m
implicit none
public
!-------------!------------------------------------------------------------------------------
! class net_t !
!-------------!
type :: container
class(*), pointer :: content
end type
!> Class representing a general network
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
class(container_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.
procedure :: print_info => print_info_impl
!> 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
!! @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
!------------------!---------------------------------------------------------------------
! class mock_net_t !
!------------------!
!> Mock net_t class
type, extends(net_t) :: mock_net_t
end type mock_net_t
interface mock_net_t
!> Non-parametric constructor of mock_net_t class
!! @return An instance of the class mock_net_t
module procedure :: new_mock_net
end interface mock_net_t
contains
!------------------------!---------------------------------------------------------------
! Method implementations !
!------------------------!
!-------------!--------------------------------------------------------------------------
! class net_t !
!-------------!
!--------------!-------------------------------------------------------------------------
! Constructors !
!--------------!
!> Constructor of net_t class
!! Loads complete info about network from Fortran binary file
!! @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
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
open(unit=11, file=filepath, form="unformatted", access="stream")
read(11) new_obj
close(11)
print *,new_obj%net_type
!TODO dopsat
#ifdef TIME_PROFILING
call time_profiling_stop(start_time, 'new_net')
#endif
end function new_net_1
!> Prints information about the network to the standard output.
subroutine print_info_impl(this)
class(net_t), intent(in) :: this
#ifdef TIME_PROFILING
real :: start_time
call time_profiling_start(start_time)
#endif
write(*,*) '+--------------+'
write(*,*) '| Network info |'
write(*,*) '+--------------+'
write(*,*) 'Type: ', this%net_type
write(*,*) 'Number of neurons: ', this%num_of_neurons
!TODO dopsat
#ifdef TIME_PROFILING
call time_profiling_stop(start_time, 'print_info_impl')
#endif
end subroutine print_info_impl
!----------------!-----------------------------------------------------------------------
! Common methods !
!----------------!
!> 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
#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
!> 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)
select type(tmp => this%neuron_arr)
type is(container_t)
select type(tmp2 => this%connection_arr)
type is(connection_t)
write(unit, iostat=iostat, iomsg=iomsg) (tmp(i)%content%get_id(), &
tmp(i)%get_potential(), &
tmp(i)%get_state(), &
i=1,SIZE(this%neuron_arr)), &
this%input_neuron_arr, &
this%output_neuron_arr, &
(tmp2(i)%get_input_neuron(), &
tmp2(i)%get_output_neuron(), &
tmp2(i)%get_weight(), &
i=1,SIZE(this%connection_arr))
end select
end select
#ifdef TIME_PROFILING
call time_profiling_stop(start_time, 'write_net_sample')
#endif
end subroutine write_sample_impl
! 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
! 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.
select type(tmp => this%neuron_arr)
type is(container_t)
select type(tmp2 => this%connection_arr)
type is(connection_t)
read(unit, iostat=iostat, iomsg=iomsg) (tmp(i)%id, &
tmp(i)%potential, &
tmp(i)%state, &
i=1, SIZE(this%neuron_arr)), &
this%input_neuron_arr, &
this%output_neuron_arr, &
(tmp2(i)%input_neuron, &
tmp2(i)%output_neuron, &
tmp2(i)%weight, &
i=1, SIZE(this%connection_arr))
end select
end select
#ifdef TIME_PROFILING
call time_profiling_stop(start_time, 'read_net_sample')
#endif
end subroutine read_sample_impl
!------------------!---------------------------------------------------------------------
! class mock_net_t !
!------------------!
!--------------!-------------------------------------------------------------------------
! Constructors !
!--------------!
!> Contructor of mock_net_t class
!! @return An instance of the class mock_net_t
function new_mock_net() result(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(:)
type(mock_neuron_t), target :: n_tmp
#ifdef TIME_PROFILING
real :: start_time
call time_profiling_start(start_time)
#endif
new_obj%net_type = 'MOCK NETWORK'
new_obj%num_of_neurons = 5
new_obj%training_method = 'MOCK TRAINING'
! Init object
allocate(container_t :: new_obj%neuron_arr(5))
select type(tmp => new_obj%neuron_arr)
type is (container_t)
do i=1,5
n_tmp = mock_neuron_t()
tmp(i) = container_t(n_tmp)
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')
#endif
end function new_mock_net
end module net_m
program net_mem_leak_test
use data_kinds_4neuro_m
use net_m
class(net_t), allocatable :: net1, net2
print *, '+--------------------------------------------------+'
print *, '| STARTING MEMORY LEAK TESTING OF THE MODULE NET_M |'
print *, '+--------------------------------------------------+'
print *, 'Creating instances of the class net_t...'
allocate(net1)
select type(tmp => net1)
type is (mock_net_t)
tmp = mock_net_t()
end select
call net1%print_info()
call net1%save_net_bin("stored_net.dat")
!allocate(net2)
!net2 = net_t("stored_net.dat")
!call net2%print_info()
end program net_mem_leak_test
This diff is collapsed.
program neuron_m_mem_leak_test
use neuron_m
implicit none
type(linear_neuron_t) :: ln
type(linear_neuron_t), allocatable :: ln2
ln = linear_neuron_t()
allocate(ln2)
open(123,file='neuron.test', form='unformatted')
write(123) ln
close(123)
open(123,file='neuron.test', form='unformatted')
read(123) ln2
close(123)
write(*,*) ln2%get_potential(), ln2%get_state(), ln2%get_id()
end program neuron_m_mem_leak_test
This diff is collapsed.
!> Module containing function for time measurement, mostly
!! for the time profiling regime
!!
!! @author Martin Beseda
!! @date 2017
module time_measurement_m
use data_kinds_4neuro_m
implicit none
public
contains
!> Subroutine for starting time profiling
!! @param[out] start_time Parameter, where current time will be saved (real number)
subroutine time_profiling_start(start_time)
real(kind=real_4neuro), intent(inout) :: start_time
call cpu_time(start_time)
end subroutine time_profiling_start
!> Subroutine for stopping time profiling and
!! printing the result.
!!
!! @param[in] start_time Parameter containing starting time (real number)
!! @param[in] region_name Name of the measured region (string)
subroutine time_profiling_stop(start_time, region_name)
real(kind=real_4neuro), intent(in) :: start_time
character(len=*) :: region_name
real :: stop_time
character(len=35) :: s
s = '| TIME PROFILING | Function '
call cpu_time(stop_time)
write(*, "(A)") '+----------------+'
write(*, "(A,A,A,E15.7,A)") s,region_name,'() was running for ',stop_time-start_time,'s.'
write(*, "(A)") '+----------------+'
end subroutine time_profiling_stop
end module time_measurement_m
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment