Created
February 27, 2021 23:09
-
-
Save DSCF-1224/dd7835d059bd3d6a804d403dd6b68d62 to your computer and use it in GitHub Desktop.
Fortran 90の組み込み関数 `COUNT` の使用方法の確認
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
! ================================================================================================================================== | |
! | |
! # objective # | |
! Check how to use an intrinsic funcion: `count` | |
! | |
! # reference # | |
! https://gcc.gnu.org/onlinedocs/gfortran/COUNT.html | |
! | |
! # compiler version # | |
! gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04) | |
! | |
! ================================================================================================================================== | |
program main | |
! <module>s to import | |
use, intrinsic :: iso_fortran_env | |
! require all variables to be explicitly declared | |
implicit none | |
! constant(s) for this <program> | |
integer(INT32) , parameter :: num_samples = 10_INT32 ** 8_INT32 | |
real(REAL64) , parameter :: boundary = 0.5_REAL64 | |
! variable(s) for this <program> | |
integer(INT32) :: iter | |
logical , allocatable :: mask(:) | |
integer(INT32) :: retval_count_func | |
integer(INT32) :: retval_count_loop | |
real(REAL64) , allocatable :: sample(:) | |
! STEP.01 | |
! allocate the array to store samples | |
allocate( sample (num_samples) ) | |
allocate( mask (num_samples) ) | |
! STEP.02 | |
! generate samples | |
call random_number( harvest= sample(:) ) | |
! STEP.03 | |
! adjust the range of samples | |
sample(:) = 2 * sample(:) - 1.0_REAL64 | |
! STEP.04 | |
! test the intrinsic function `count` | |
mask(:) = abs( sample(:) ) .le. boundary | |
retval_count_func = count( mask= mask(:) ) | |
retval_count_loop = 0_INT32 | |
do iter = 1_INT32 , num_samples , 1_INT32 | |
if ( mask(iter) ) retval_count_loop = retval_count_loop + 1_INT32 | |
end do | |
! STEP.05 | |
! show the result | |
print *, 'total : ' , num_samples | |
print *, 'func : ' , retval_count_func | |
print *, 'loop : ' , retval_count_loop | |
end program main | |
! ================================================================================================================================== | |
! EOF | |
! ================================================================================================================================== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FCFLAGS = -ffree-line-length-none -O2 -pedantic -std=f2008 -Wall -Wextra # -fbacktrace -fbounds-check | |
OBJS = ./main.o | |
main.exe: $(OBJS) | |
gfortran $(FCFLAGS) -o ./main.exe $(OBJS) | |
%.o: %.f08 | |
gfortran $(FCFLAGS) -c $< | |
clean: | |
rm ./*.mod ./*.o |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment