|
1 | 1 | !-----------------------------------------------------------------------------!
|
2 | 2 | ! \file example/sort.f90
|
| 3 | +! |
| 4 | +! Copyright (c) 2019 Oak Ridge National Laboratory, UT-Battelle, LLC. |
3 | 5 | !-----------------------------------------------------------------------------!
|
4 | 6 |
|
5 | 7 | program main
|
6 |
| - use ISO_FORTRAN_ENV |
7 |
| - use, intrinsic :: ISO_C_BINDING |
8 |
| - use flc |
9 |
| - use flc_algorithm, only : sort |
| 8 | + use ISO_FORTRAN_ENV |
| 9 | + use, intrinsic :: ISO_C_BINDING |
| 10 | + use flc |
| 11 | + use flc_algorithm, only : sort |
| 12 | + use flc_random, only : Engine, normal_distribution |
| 13 | + implicit none |
| 14 | + integer, parameter :: STDOUT = OUTPUT_UNIT |
| 15 | + integer :: arr_size |
| 16 | + real(c_double), dimension(:), allocatable :: x |
| 17 | + real(c_double), parameter :: MEAN = 1.0d0, SIGMA = 0.5d0 |
| 18 | + type(Engine) :: rng |
10 | 19 |
|
11 |
| - implicit none |
12 |
| - integer :: n = 10000 |
13 |
| - real(c_double), dimension(:), allocatable :: x |
| 20 | + ! Print version information |
| 21 | + write(STDOUT, "(a)") "========================================" |
| 22 | + write(STDOUT, "(a, a)") "Flibcpp version: ", get_flibcpp_version() |
| 23 | + write(STDOUT, "(a, 2(i1,'.'), (i1), a)") "(Numeric version: ", & |
| 24 | + flibcpp_version_major, flibcpp_version_minor, flibcpp_version_patch, & |
| 25 | + ")" |
| 26 | + write(STDOUT, "(a)") "========================================" |
14 | 27 |
|
15 |
| - write(*,"(a, 2(i1,'.'), (i1))") "Flibcpp version number: ", & |
16 |
| - flibcpp_version_major, flibcpp_version_minor, flibcpp_version_patch |
17 |
| - write(*,*) "Flibcpp version string: ", get_flibcpp_version() |
| 28 | + ! Get array size |
| 29 | + arr_size = read_positive_int("array size") |
| 30 | + allocate(x(arr_size)) |
18 | 31 |
|
19 |
| - allocate(x(n)) |
20 |
| - ! TODO: fill randomly |
21 |
| - x = 1 |
22 |
| - call sort(x) |
| 32 | + ! Fill randomly with normal distribution |
| 33 | + rng = Engine() |
| 34 | + call normal_distribution(MEAN, SIGMA, rng, x) |
23 | 35 |
|
24 |
| - write(*,*) "Success!" |
25 |
| -end program |
| 36 | + ! Sort the array |
| 37 | + call sort(x) |
| 38 | + |
| 39 | + ! Write output |
| 40 | + write(STDOUT, "(a, 4(f8.3,','))") "First few elements:", x(:min(4, size(x))) |
| 41 | +contains |
| 42 | + |
| 43 | +! Loop until the user inputs a positive integer. Catch error conditions. |
| 44 | +function read_positive_int(desc) result(result_int) |
| 45 | + use flc |
| 46 | + use flc_string, only : stoi |
| 47 | + use ISO_FORTRAN_ENV |
| 48 | + implicit none |
| 49 | + character(len=*), intent(in) :: desc |
| 50 | + integer, parameter :: STDOUT = OUTPUT_UNIT, STDIN = INPUT_UNIT |
| 51 | + character(len=80) :: readstr |
| 52 | + integer :: result_int, io_ierr |
| 53 | + do |
| 54 | + write(STDOUT, *) "Enter " // desc // ": " |
| 55 | + read(STDIN, "(a)", iostat=io_ierr) readstr |
| 56 | + if (io_ierr == IOSTAT_END) then |
| 57 | + ! Error condition: ctrl-D during input |
| 58 | + write(STDOUT, *) "End of line" |
| 59 | + stop 0 |
| 60 | + endif |
| 61 | + |
| 62 | + result_int = stoi(readstr) |
| 63 | + if (ierr == 0) then |
| 64 | + if (result_int <= 0) then |
| 65 | + ! Error condition: non-positive value |
| 66 | + write(STDOUT, *) "Invalid " // desc // ": ", result_int |
| 67 | + continue |
| 68 | + end if |
26 | 69 |
|
| 70 | + write(STDOUT, *) "Read " // desc // "=", result_int |
| 71 | + exit |
| 72 | + endif |
| 73 | + |
| 74 | + if (ierr == SWIG_OVERFLOWERROR) then |
| 75 | + ! Error condition: integer doesn't fit in native integer |
| 76 | + write(0,*) "Your integer is too darn big!" |
| 77 | + else if (ierr == SWIG_VALUEERROR) then |
| 78 | + ! Error condition: not an integer at all |
| 79 | + write(0,*) "That text you entered? It wasn't an integer." |
| 80 | + else |
| 81 | + write(0,*) "Unknown error", ierr |
| 82 | + end if |
| 83 | + write(0,*) "(Detailed error message: ", get_serr(), ")" |
| 84 | + |
| 85 | + ! Clear error flag so the next call to stoi succeeds |
| 86 | + ierr = 0 |
| 87 | + end do |
| 88 | +end function |
| 89 | +end program |
27 | 90 |
|
28 | 91 | !-----------------------------------------------------------------------------!
|
29 | 92 | ! end of example/sort.f90
|
|
0 commit comments