Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use function "get_command_as_string" #1

Closed
gha3mi opened this issue Jan 18, 2023 · 0 comments
Closed

Use function "get_command_as_string" #1

gha3mi opened this issue Jan 18, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@gha3mi
Copy link
Owner

gha3mi commented Jan 18, 2023

Problem: To get the output of a shell command, you must write it to a temporary file and then read it into Fortran.
See the current version of the subroutine find_number_of_cpus:

   subroutine find_number_of_cpus(this)
      class(linux_nodes), intent(inout) :: this
      integer                           :: nunit
      logical                           :: ex
      character(len=:), allocatable     :: file_name

      ! todo: get total number of linux cpus
      call execute_command_line ("echo $(nproc --all) > /tmp/forclust_ncpus")

      file_name = '/tmp/forclust_ncpus'
      inquire(file=file_name, exist=ex)
      if (ex) then
         open(newunit=nunit, file=file_name, action='read')
         read(nunit, *) this%ncpus
         close(nunit)
      else
         this%ncpus = 1
         ! error stop "file not found: "//file_name
      end if
   end subroutine find_number_of_cpus

Solution: Use the get_command_as_string function in the pipes_module module from here. With this function a new version looks like as follows:

 subroutine find_number_of_cpus(this)
    use :: pipes_module, only: get_command_as_string
    class(linux_nodes), intent(inout) :: this
    character(len=:), allocatable     :: ncpu_char

    ncpu_char = get_command_as_string('nproc --all')
    read(ncpu_char,*) this%ncpus
 end subroutine find_number_of_cpus
@gha3mi gha3mi changed the title Use "get_command_as_string" function Use function "get_command_as_string" Jan 18, 2023
@gha3mi gha3mi added the enhancement New feature or request label Feb 19, 2023
gha3mi added a commit that referenced this issue Apr 10, 2023
Updated find_number_of_cpus subroutine.
Updated the fpm.toml file.
@gha3mi gha3mi closed this as completed Apr 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant