在同一台机器上两个正在运行的 Fortran 程序之间传输数据(实数和整数数组)的最佳方法是什么?
我们目前正在使用文件 I/O,但需要更好/更快的方法。示例代码将不胜感激。
We are currently using file I/O but need a better/faster way. Sample code would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过使用文件进行传输,您已经实现了一种消息传递形式,因此我认为这对于此类程序来说是最自然的选择。现在,您可以自己编写一些东西,在可用时使用共享内存,在不可用时使用 TCP/IP 之类的东西 - 或者您可以使用已经这样做的库,例如 MPI,它广泛可用,有效,将利用共享内存如果您在同一台机器上运行,但也可以扩展到让您完全在不同的机器上运行它们,而无需更改代码。
因此,作为一个程序向第二个程序发送数据然后等待数据返回的简单示例,我们有两个程序,如下所示; first.f90
和 secondary.f90:
为了清楚起见,两个程序必须同意的东西可以在它们都使用的模块中,这里是 protocol.f90:(
用于构建可执行文件的 makefile 如下:)
和然后按如下方式运行这两个程序:
如果您向我们提供有关两个程序之间工作流程的更多信息,我们当然可以为您提供更相关的示例。
By using files for transfer, you're already implementing a form of message passing, and so I think that would be the most natural fit for this sort of program. Now, you could write something yourself that uses shared memory when available and something like TCP/IP when not - or you could just use a library that already does that, like MPI, which is widely available, works, will take advantage of shared memory if you are running on the same machine, but would then also extend to letting you run them on different machines entirely without you changing your code.
So as a simple example of one program sending data to a second and then waiting for data back, we'd have two programs as follows; first.f90
and second.f90:
For clarity, stuff that the two programs must agree on could be ina module they both use, here protocol.f90:
(A makefile for building the executables follows:)
and then you run the two programs as following:
We could certainly give you a more relevant example if you give us more information about the workflow between the two programs.
您是否使用二进制(未格式化)文件 I/O?除非数据量很大,否则应该很快。
否则,您可以使用进程间通信,但会更复杂。您可能会找到 C 代码,您可以使用 ISO C 绑定从 Fortran 调用这些代码。
Are you using binary (unformatted) file I/O? Unless the data quantity is huge, that should be fast.
Otherwise you could use interprocess communication, but it would be more complicated. You might find code in C, which you could call from Fortran using the ISO C Binding.