Fortran Coder

楼主: andy8496

[讨论] 一个数组运算的问题

[复制链接]

490

帖子

4

主题

0

精华

大宗师

F 币
3298 元
贡献
1948 点

水王勋章元老勋章热心勋章

发表于 2017-8-6 21:28:33 | 显示全部楼层
andy8496 发表于 2017-8-6 18:30
这个是实际应用的需要。

症结即在于此呀,没有看出来?

123

帖子

32

主题

0

精华

宗师

F 币
1562 元
贡献
795 点
 楼主| 发表于 2017-8-6 21:40:23 | 显示全部楼层
本帖最后由 andy8496 于 2017-8-6 21:41 编辑
pasuka 发表于 2017-8-6 21:28
症结即在于此呀,没有看出来?

真看不出来。我试验用的那段代码,咋就能按照我的理解运行呢?索引数组也是有重复元素的:
program main
implicit none
   integer :: ia(5),ib(9)
   ia = [1,5,5,5,2]
   ib = 0
   ib(ia) = ib(ia) + [1,1,1,1,1]
   write(*,*) ib
   stop
end program
aaa.jpg

130

帖子

10

主题

0

精华

大师

F 币
617 元
贡献
372 点

贡献勋章管理勋章帅哥勋章元老勋章星光勋章规矩勋章

发表于 2017-8-6 23:20:26 | 显示全部楼层
a many-one array section must NOT Assign To NOR Read In
Compiler may not Check at Runtime owing to the Performance Cost

many-one-array.pdf

74.9 KB, 下载次数: 13

123

帖子

32

主题

0

精华

宗师

F 币
1562 元
贡献
795 点
 楼主| 发表于 2017-8-7 09:15:14 | 显示全部楼层
好吧,总算看到一点依据了。虽然心中不服,比如:五楼的大哥的方法为什么又可行?
还有两点疑问,还要继续麻烦大家指点:
chiangtp附件中提到的,
①a many-one array section must NOT Assign To NOR Read In Compiler may not Check at Runtime owing to the Performance Cost
VS+IVF中,Debug中如何打开这个检查?
②the use of vector subscripting is very inefficient and should not be used unless absolutely necessary (although easy to coding)
这句话该如何理解?数据片段只是写起来方便,但是运行效率低?

490

帖子

4

主题

0

精华

大宗师

F 币
3298 元
贡献
1948 点

水王勋章元老勋章热心勋章

发表于 2017-8-7 11:04:45 | 显示全部楼层
本帖最后由 pasuka 于 2017-8-7 11:17 编辑
andy8496 发表于 2017-8-6 21:40
真看不出来。我试验用的那段代码,咋就能按照我的理解运行呢?索引数组也是有重复元素的:
program main
  ...

左端赋值包含重复元素的切片这样的写法本身就是不正确的,起码IBM XL和Intel Fortran的用户手册都明确强调这点
传送门1: http://w3.pppl.gov/~hammett/comp/f90tut/f90.tut5.html
Note that a vector subscript with duplicate values cannot appear on the left-hand side of an assignment as it would be ambiguous. Thus,
       b( (/ 1, 7, 3, 7 /) ) = (/ 1, 2, 3, 4 /)
is illegal.
传送门2: https://www.ibm.com/support/know ... ectorsubscript.html
An array section with a vector subscript in which two or more elements of the vector subscript have the same value is called a many-one section. Such a section must not:
Appear on the left side of the equal sign in an assignment statement传送门3: https://software.intel.com/en-us/node/678553
An array section with a vector subscript that has two or more elements with the same value is called a many-one array section. For example:
  REAL A(3, 3), B(4)  INTEGER K(4)  ! Vector K has repeated values  K = (/3, 1, 1, 2/)  ! Sets all elements of A to 5.0  A = 5.0  B = A(3, K)
The array section A(3,K) consists of the elements:
  A(3, 3)  A(3, 1)  A(3, 1)  A(3, 2)
A many-one section must not appear on the left of the equal sign in an assignment statement, or as an input item in a READ statement.


130

帖子

10

主题

0

精华

大师

F 币
617 元
贡献
372 点

贡献勋章管理勋章帅哥勋章元老勋章星光勋章规矩勋章

发表于 2017-8-7 13:03:49 | 显示全部楼层
1. 15F, posuka見多識廣, 佩服
2. whole array的assignment(多個要做的規則), 定義上是in-any-order+parallel(如果能), runtime時 "五楼的大哥的方法", "Debug打开检查" 都是compiler/compile-time-options/... dependent
3. "vector subscripting is very inefficient "應可理解(?), 我沒有理想的實證

130

帖子

10

主题

0

精华

大师

F 币
617 元
贡献
372 点

贡献勋章管理勋章帅哥勋章元老勋章星光勋章规矩勋章

发表于 2017-8-7 13:05:55 | 显示全部楼层
抱歉, pasuka誤為posuka

490

帖子

4

主题

0

精华

大宗师

F 币
3298 元
贡献
1948 点

水王勋章元老勋章热心勋章

发表于 2017-8-7 13:52:26 | 显示全部楼层
本帖最后由 pasuka 于 2017-8-7 14:01 编辑

再补充一些内容
Fortran 2003 handbook的第6章,第184页如是说:
A many-one section must not appear on the left of the equal sign in an assignment statement or as an input item in a READ statement. The reason is that the result will depend on the order of evaluation of the subscripts, which is not specified by the language. The results would not be predictable and the program containing such a statement would not be portable.

也即这个属于Fortran规范的盲点,lz有心的话,不妨邮件咨询RAL的J K Reid爵士或者I Duff
btw,无需见多识广,反倒是科学上网很重要

123

帖子

32

主题

0

精华

宗师

F 币
1562 元
贡献
795 点
 楼主| 发表于 2017-8-7 14:52:45 | 显示全部楼层
多谢各位了!
现在补救的办法就是依赖编译器检查了,不知道可行否?在哪里设置?


2. whole array的assignment(多個要做的規則), 定義上是in-any-order+parallel(如果能), runtime時 "五楼的大哥的方法", "Debug打开检查" 都是compiler/compile-time-options/... dependent

这个设置我没找到在哪儿……
setting.jpg

490

帖子

4

主题

0

精华

大宗师

F 币
3298 元
贡献
1948 点

水王勋章元老勋章热心勋章

发表于 2017-8-7 15:34:31 | 显示全部楼层
本帖最后由 pasuka 于 2017-8-7 15:37 编辑
andy8496 发表于 2017-8-7 14:52
多谢各位了!
现在补救的办法就是依赖编译器检查了,不知道可行否?在哪里设置?

直接改回方法1不行吗?
在ia index数组包含重复元素且不能剔除的前提下,IBM XL和Intel Fortran编译器手册均明确对方法2亮红牌,这下总得遵守规则被罚下场吧?
仍然不服气的话,建议研读g95或gfortran前端代码,自行实现需要的功能。g95的代码值得看看,既能加深Fortran的了解,也能助益C语言进阶
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-4-16 14:21

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表