Skip to content

Commit 08da417

Browse files
committed
Move download_and_uncompress() to the mnist submodule
1 parent fc9a994 commit 08da417

File tree

2 files changed

+51
-49
lines changed

2 files changed

+51
-49
lines changed

src/mod_io_submodule.f90

+13-49
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,20 @@
33
implicit none
44

55
integer, parameter :: message_len = 128
6-
contains
76

8-
subroutine download_and_uncompress()
9-
character(len=*), parameter :: download_mechanism = 'curl -LO '
10-
character(len=*), parameter :: base_url='https://github.com/modern-fortran/neural-fortran/files/8498876/'
11-
character(len=*), parameter :: download_filename = 'mnist.tar.gz'
12-
character(len=*), parameter :: download_command = download_mechanism // base_url //download_filename
13-
character(len=*), parameter :: uncompress_file= 'tar xvzf ' // download_filename
14-
character(len=message_len) :: command_message
15-
character(len=:), allocatable :: error_message
16-
integer exit_status, command_status
17-
exit_status=0
18-
call execute_command_line(command=download_command, &
19-
wait=.true., exitstat=exit_status, cmdstat=command_status, cmdmsg=command_message)
20-
if (any([exit_status, command_status]/=0)) then
21-
error_message = 'command "' // download_command // '" failed'
22-
if (command_status/=0) error_message = error_message // " with message " // trim(command_message)
23-
error stop error_message
24-
end if
25-
call execute_command_line(command=uncompress_file , &
26-
wait=.true., exitstat=exit_status, cmdstat=command_status, cmdmsg=command_message)
27-
if (any([exit_status, command_status]/=0)) then
28-
error_message = 'command "' // uncompress_file // '" failed'
29-
if (command_status/=0) error_message = error_message // " with message " // trim(command_message)
30-
error stop error_message
31-
end if
32-
end subroutine
7+
contains
338

349
module subroutine read_binary_file_1d(filename, dtype, nrec, array)
3510
character(len=*), intent(in) :: filename
3611
integer(ik), intent(in) :: dtype, nrec
3712
real(rk), allocatable, intent(in out) :: array(:)
3813
integer(ik) :: fileunit
39-
character(len=message_len) io_message, command_message
40-
integer io_status
41-
io_status=0
42-
open(newunit=fileunit, file=filename, access='direct',&
43-
action='read', recl=dtype * nrec, status='old', iostat=io_status)
44-
if (io_status/=0) then
45-
call download_and_uncompress
46-
open(newunit=fileunit, file=filename, access='direct',&
47-
action='read', recl=dtype * nrec, status='old', iostat=io_status, iomsg=io_message)
48-
if (io_status/=0) error stop trim(io_message)
49-
end if
14+
character(len=message_len) :: io_message
15+
integer :: io_status
16+
io_status = 0
17+
open(newunit=fileunit, file=filename, access='direct', action='read', &
18+
recl=dtype * nrec, status='old', iostat=io_status, iomsg=io_message)
19+
if (io_status /= 0) error stop trim(io_message)
5020
allocate(array(nrec))
5121
read(fileunit, rec=1) array
5222
close(fileunit)
@@ -57,19 +27,13 @@ module subroutine read_binary_file_2d(filename, dtype, dsize, nrec, array)
5727
integer(ik), intent(in) :: dtype, dsize, nrec
5828
real(rk), allocatable, intent(in out) :: array(:,:)
5929
integer(ik) :: fileunit, i
60-
character(len=message_len) io_message, command_message
61-
integer io_status
62-
open(newunit=fileunit, file=filename, access='direct',&
63-
action='read', recl=dtype * nrec, status='old', iostat=io_status)
64-
if (io_status/=0) then
65-
call download_and_uncompress
66-
open(newunit=fileunit, file=filename, access='direct',&
67-
action='read', recl=dtype * nrec, status='old', iostat=io_status, iomsg=io_message)
68-
if (io_status/=0) error stop trim(io_message)
69-
end if
30+
character(len=message_len) :: io_message
31+
integer :: io_status
32+
io_status = 0
33+
open(newunit=fileunit, file=filename, access='direct', action='read', &
34+
recl=dtype * dsize, status='old', iostat=io_status, iomsg=io_message)
35+
if (io_status /= 0) error stop trim(io_message)
7036
allocate(array(dsize, nrec))
71-
open(newunit=fileunit, file=filename, access='direct',&
72-
action='read', recl=dtype * dsize, status='old')
7337
do i = 1, nrec
7438
read(fileunit, rec=i) array(:,i)
7539
end do

src/mod_mnist_submodule.f90

+38
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,41 @@
1010

1111
implicit none
1212

13+
integer, parameter :: message_len = 128
14+
1315
contains
1416

17+
subroutine download_and_uncompress()
18+
character(len=*), parameter :: download_mechanism = 'curl -LO '
19+
character(len=*), parameter :: base_url='https://github.com/modern-fortran/neural-fortran/files/8498876/'
20+
character(len=*), parameter :: download_filename = 'mnist.tar.gz'
21+
character(len=*), parameter :: download_command = download_mechanism // base_url // download_filename
22+
character(len=*), parameter :: uncompress_file = 'tar xvzf ' // download_filename
23+
character(len=message_len) :: command_message
24+
character(len=:), allocatable :: error_message
25+
integer :: exit_status, command_status
26+
27+
exit_status=0
28+
call execute_command_line(command=download_command, wait=.true., &
29+
exitstat=exit_status, cmdstat=command_status, cmdmsg=command_message)
30+
31+
if (any([exit_status, command_status] /= 0)) then
32+
error_message = 'command "' // download_command // '" failed'
33+
if (command_status /= 0) error_message = error_message // " with message " // trim(command_message)
34+
error stop error_message
35+
end if
36+
37+
call execute_command_line(command=uncompress_file, wait=.true., &
38+
exitstat=exit_status, cmdstat=command_status, cmdmsg=command_message)
39+
40+
if (any([exit_status, command_status] /= 0)) then
41+
error_message = 'command "' // uncompress_file // '" failed'
42+
if (command_status /= 0) error_message = error_message // " with message " // trim(command_message)
43+
error stop error_message
44+
end if
45+
46+
end subroutine download_and_uncompress
47+
1548
pure module function label_digits(labels) result(res)
1649
real(rk), intent(in) :: labels(:)
1750
real(rk) :: res(10, size(labels))
@@ -42,6 +75,11 @@ module subroutine load_mnist(tr_images, tr_labels, te_images,&
4275
integer(ik), parameter :: tr_nimages = 50000
4376
integer(ik), parameter :: te_nimages = 10000
4477
integer(ik), parameter :: va_nimages = 10000
78+
logical :: file_exists
79+
80+
! Check if MNIST data is present and download it if not.
81+
inquire(file='mnist_training_images.dat', exist=file_exists)
82+
if (.not. file_exists) call download_and_uncompress()
4583

4684
call read_binary_file('mnist_training_images.dat',&
4785
dtype, image_size, tr_nimages, tr_images)

0 commit comments

Comments
 (0)