如何在Fortran中实现数组结构而不是结构数组? [英] How to implement Structures of Arrays instead of Arrays of Structures in Fortran?

查看:97
本文介绍了如何在Fortran中实现数组结构而不是结构数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Fortran编写有关CFD主题的代码.与计算机科学领域的一些朋友讨论后,他们告诉我,如果在其代码上实施数组结构(SoA)而不是结构数组(AoS),则可以缩短计算时间.

I'm writing my code on CFD topic with Fortran. After discussing with some friends from computer science, they told me that one could speed up the computation time if one implements Structures of Arrays (SoA) instead of Arrays of Structures (AoS) on his/her code.

我已经看到许多有关此主题的实现的示例,但大多数都是C或C ++的. (例如 https://software.intel.com/zh-cn/articles/how-to-manipulate-data-structure-to-optimize-memory-use-on-32-bit-intel-architecture ).

There are many examples I've seen about this topic's implementation, but most of them are in C or C++. (e.g. https://software.intel.com/en-us/articles/how-to-manipulate-data-structure-to-optimize-memory-use-on-32-bit-intel-architecture).

任何人都可以向我展示或指导我一些基本想法或示例,以了解如何在Fortran中实现SoA而不是AoS吗?

Could anyone show me or guide me some basic ideas or examples how to implement SoA instead of AoS in Fortran?

推荐答案

这个概念确实没有什么困难.

There is really nothing difficult about this concept.

代替

type struct
  real x, y, z
end type

type(struct), allocatable :: array(:)

您使用

type(struct2)
  real, dimension(:), allocatable :: x, y, z
end ype

type(struct2) :: arrays

它实际上只是C或C ++示例的逐行翻译.即使有关本主题的示例使用其他语言,您可以阅读的有关该主题的内容或多或少仍然适用于Fortran.

It is really just a line by line translation of a C or C++ example. More or less everything what you can read about this topic is still applicable to Fortran even if they use other language for their examples.

实际上,在过去,Fortran没有任何结构,最自然的操作方法是声明变量:

Actually, in the old days Fortran didn't have any structures and the most natural way how to do stuff was just to declare variables:

real x(bigN)
real y(bigN)
real z(bigN)

,您也可以通过这种方式获得数组结构的所有这些性能优势.对于Fortranner来说,有人只知道结构数组,这听起来很奇怪.

and you get all those performance benefits of structures of arrays this way too. It sounds almost strange to a Fortranner that someone knows only arrays of structures.

这篇关于如何在Fortran中实现数组结构而不是结构数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆