Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 在FORTRAN中调用子例程而不阻塞主程序_Multithreading_Fortran_Blocking - Fatal编程技术网

Multithreading 在FORTRAN中调用子例程而不阻塞主程序

Multithreading 在FORTRAN中调用子例程而不阻塞主程序,multithreading,fortran,blocking,Multithreading,Fortran,Blocking,假设使用mpif90编译的过于简化的FORTRAN代码为: program main ! use mpi implicit none integer:: j, numtasks, taskid, ierr integer:: master = 0 ! call mpi_init(ierr) call mpi_comm_rank(mpi_comm_world, taskid, ierr) ! if (taskid .eq. master) then

假设使用mpif90编译的过于简化的FORTRAN代码为:

program main
!
   use mpi
   implicit none
   integer:: j, numtasks, taskid, ierr
   integer:: master = 0
!
   call mpi_init(ierr)
   call mpi_comm_rank(mpi_comm_world, taskid, ierr)   
!
   if (taskid .eq. master) then
      j = 5
      call child (j)
  !   do stuff
   end if
   call mpi_finalize(ierr)
!
end program main

subroutine child(j)
!
   implicit none
   integer, intent(in):: j
!  do some stuff with j
end subroutine child

默认情况下,主CPU等待子CPU完成计算。但是,我希望它在调用子对象后继续执行其任务,同时子对象也在执行其任务。我希望子对象成为main的子例程,因为我需要将一些数据从main传递给子对象(但不是相反)。我想知道这在FORTRAN中是否可行(可能通过使用某种非阻塞子例程调用或多线程,如mpi_comm_spawn)。

我会使用POSIX线程来实现这一点。可能也是OpenMP任务,但我的经验有限。我假设您没有在
子对象中调用任何MPI过程

用C语言编写了一个简单的接口

#include <pthread.h>

void pthread_create_opaque(pthread_t *threadptr, void *procptr, void *dataptr, int *err){
//   creates a new thread using an opaque pointer to the pthread_t structure
  *err = pthread_create(threadptr, NULL, procptr, dataptr);
}

void pthread_join_opaque(pthread_t *threadptr, int *err) {
//  joines a thread using an opaque pointer to the pthread_t structure
 *err = pthread_join(*threadptr, NULL);
}
如果是C可互操作的,则可以调用子级

subroutine child(j) bind(C)
!
   implicit none
   integer, intent(in):: j
!  do some stuff with j
end subroutine child
简单地说

type(c_ptr) :: thread
integer :: err

call pthread_create_opaque(thread, c_funloc(join), loc(j), err)
然后在某个方便的地方(在程序结束之前或任何地方)等待它完成它的工作

call pthread_join_opaque(thread, err)
我在一个MPI并行程序中成功地将其用于时间步长数据的异步输出

call pthread_join_opaque(thread, err)