Fortran 中的 STL 模拟

2022-01-07 00:00:00 fortran c++ stl

基础:我有非常大的并行 Fortran90/MPI 程序,代表复杂的物理模型.我想为其添加新功能:例如,我需要组织消息队列,在某处引入归并排序并使用哈希表.

Basis: i have very big parallel Fortran90/MPI program which represent complex physical model. I want to add new functionality to it: for example, i need to organize queue of messages, introduce mergesort somewhere and use hash tables.

问题:我自己知道如何编写哈希表、创建队列和代码归并排序,但我认为发明自行车不是一个好主意.

Problem: i know how write hash table, create queue and code mergesort by my self, but i don't think it is a good idea to invent a bicycle.

问题:在这种情况下,Fortran 大师应该怎么做?我应该从 Fortran 构建到 C++ 类的绑定并使用 STL 实现逻辑,还是您可以建议一些类似于 Fortran STL 的库?谢谢.

Question: what Fortran guru should do in such situation? Should i build binds to C++ classes from Fortran and realize logic there using STL or you can suggest some Fortran STL-like libraries? Thank you.

推荐答案

Fortran 中没有模板,因此也没有 STL.您可以尝试 FLIBS 获取一些通用库.它一般使用transfer()技巧来实现泛型编程.

There are no templates in Fortran and hence no STL. You can try FLIBS for some generic libraries. It generally uses transfer() tricks to achieve generic programming.

有一个预处理器,它为 Fortran 添加了一些模板并带有一些小的 STL,你也可以尝试使用它命名为 PyF95++.如果您可以通过某个图书馆访问学术论文,则可以在此处阅读相关信息.

There is a preprocessor which adds some templates to Fortran and comes with some small STL, you can try that too named PyF95++. If you have access to academic papers through some library, you can read about it here.

在这种特定情况下,我会避免将它与 C++ 混合使用,尽管它可以做到.您必须单独实例化每个案例,并使用包装器(bind(C)iso_c_binding)将其与 Fortran 接口.只有当您想要使用的算法数量非常有限时,才值得这样做.

I would avoid mixing it with C++ in this specific case although it can be done. You must instantiate each case separately and interface it to Fortran using a wrapper (bind(C) and iso_c_binding). Only if you have a very limited number of types you want to use the algorithms for it could be worth it.

您还可以尝试使用 Fortran 中的 C 预处理器来实现一些穷人的模板,对于较小的库,它可以工作,但对于复杂的事物可能变得难以维护或丑陋.作为一个例子,你可以看到我对链表的实现 https://github.com/LadaF/fortran-list .

You can also try to implement some poor-man's templates using the C-preprocessors in Fortran, For smaller libraries it works, but can become too difficult to maintain or ugly for complex things. As an example you can see my implementation of a linked list https://github.com/LadaF/fortran-list .

一般来说,没有明确正确的方法或答案,你总是要从更多的可能性中进行选择.

Generally, there is no clearly right approach or answer, you always have to choose from more possibilities.

相关文章