STL analogue in Fortran(Fortran 中的 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.
这篇关于Fortran 中的 STL 模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Fortran 中的 STL 模拟
基础教程推荐
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01