顯示具有 Fortran 標籤的文章。 顯示所有文章
顯示具有 Fortran 標籤的文章。 顯示所有文章

2011年11月6日 星期日

慘遭助教修理的一道程式習題 冼鏡光

慘遭助教修理的一道程式習題 冼鏡光
http://blog.dcview.com/article.php?a=ADgAZgJlVGQ%3D

Fortran筆記2

http://www.chem.ucl.ac.uk/resources/history/people/vanmourik/images/Fortran%2095-manual.pdf
In C:


void foo(void)
{
    static int a = 5;
    int const b = 6;
}


In Fortran90/95:


SUBROUTINE FOO()
IMPLICIT NONE
INTEGER, SAVE :: A = 5
INTEGER, PARAMETER :: B = 5
END





Array assignment statements



coords(1) = 3.5 
block(3,2) = 7 


do i = 1,3 
     coords(i) = 0.0 
 end do 


coords = (/ 1.3, 5.6, 0.0 /)



coords = (/ (2.0*i, i = 1, 3) /)  ! yields (2.0, 4.0, 6.0) 
odd_ints = (/ (i, i = 1, 10, 2) /)  ! yields (1, 3, 5, 7, 9) 
integer, dimension (8), parameter :: primes = (/ 1, 2, 3, 7, 11, 13, 17, 19 /) 



For example, a two-dimensional array b(2,3) can be added to the array section a(2:3, 1:3) 
of the array a of the previous section.  If the array c is an array of dimension (2,3), then 
the expression 
c = a(2:3,1:3) + b 
causes the elements of the array c to have the following values: 
 c(1,1) = a(2,1) + b(1,1) 
 c(2,1) = a(3,1) + b(2,1) 
 c(1,2) = a(2,2) + b(1,2) 
 c(2,2) = a(3,2) + b(2,2) 
 c(1,3) = a(2,3) + b(1,3) 
 c(2,3) = a(3,3) + b(2,3)

The same can be achieved by using a do loop: 
 do i = 1, 3 
     do j = 1, 2 
         c(j,i) = a(j+1,i) + b(j,i) 
     end do 
 end do 
But the expression c = a(2:3,1:3) + b is clearly more concise.   

2011年11月5日 星期六

Fortran 筆記

Fortran 77


Data Type


IMPLICIT NONE


INTEGER
[sign][[base]#]constant
1. sign: "+" or "-",省略代表正
2. [[base]#]省略,表十進制。[base]省略,#保留,表16進制。
base:2~36
2#11011
36#2DM8F


REAL
-.28E2
.54D+3 (倍精度)


DOUBLE PRECISION


COMPLEX


(REAL,REAL)


LOGICAL


.TRUE.
.FALSE.


CHARACTER


CHARACTER A
CHARACTER B*10
CHARACTER*20 C*10, D
CHARACTER*(2*3) E
A 1
B 10
C 10
D 20
E 6


PARAMETER( 變數名稱=常數[,變數名稱=常數...] )




Fortran 90/95


INTEGER*1
INTEGER*2
INTEGER*4
INTEGER(4)
INTEGER(KIND=4)


REAL (4 btyes, 單精度)
REAL*4 (4 btyes, 單精度)
ERAL*8 (8 bytes, 雙精度)

2011年10月7日 星期五

C與Fortran誰快,我該用哪種

關於Fortran較快的論點:
http://stackoverflow.com/questions/146159/is-fortran-faster-than-c

Fortran不允許兩個Pointer指向同一記憶體,C允許,所以C須重複load同一塊記憶體
除非C加上keyword returict:
http://en.wikipedia.org/wiki/Restrict

但只有C99支援
且還有其他因素使Fortran快於C,但其他因素是什麼?

 作者  yoco315 (眠月)                                         看板  C_and_CPP
 標題  Re: [問題] 為何公認fortran速度略快於C ?
 時間  Fri May  1 02:38:34 2009
───────────────────────────────────────


※ 引述《Carbontube (碳管)》之銘言:
: 大體上,就多數人認知,C與Fortran速度是有差的
:   小弟實在想不透這點,為何fortran可以比較快。


這邊有解釋
http://stackoverflow.com/questions/146159/is-fortran-faster-than-c


簡單來說就是 fortran 可以作一些 C 沒辦法(自動)做的最佳化
至於為什麼沒辦法自動作上面這篇跟下面 [1] 都有講
但是我們還是可以提示 compiler 作這個最佳化,看 [1]


這邊有兩個 C 的加速手段
[1] http://tinyurl.com/dygwpb __restrict__
[2] http://tinyurl.com/d55t9f __builtin_prefetch


只用上 [1] 的話大概打平手或是小贏,
[1] 跟 [2] 都用上的話,C 就贏了,我沒試過,只是合理推論
不過 [2] 不是標準,[1] 也只是 C99 的標準,C++ 沒的用
(雖然說還是有 extension 支援)


--
To iterate is human, to recurse, divine.
遞迴只應天上有, 凡人該當用迴圈.                  L. Peter Deutsch


--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.160.106.42
推 vip82:推!!                                                      05/01 05:12
推 sjgau:fortran have common block, it is global valriable         05/01 07:51

另一個Fortran的優勢是有前人寫的LAPACK等數值運算函式庫
以上為Fortran77

但Fortran90出現POINTER,Fortran2003全面支援OOP
是否使Fortran的速度不再快於C?

又有人說Fortran2008不再支援OOP,是真的嗎?

 作者  DrStein (交換關聯)                                     看板  C_and_CPP
 標題  [閒聊] 怒吼,PTT怎沒有fortran版
 時間  Sun Sep  7 17:06:15 2008
───────────────────────────────────────
...略
※ 發信站: 批踢踢實業坊(ptt.cc)
...略
→ DrStein:Fortran優勢有三: 1.有lapack等數值函式庫                 09/07 20:28
→ DrStein:2.前人的科學計算程式,大多是用fortran來寫               09/07 20:28
→ DrStein:3.好學 比起C/C++那種指標強制學習制(開檔必要用指標)      09/07 20:29
→ DrStein:fortran無疑是非專業程設者的第一選則                     09/07 20:30
→ DrStein:(4.) fortran 2008直接支援平行,那更是目前的趨勢         09/07 20:31
→ DrStein:fortran2003胎死負中的原因 就是因為全面支援OOP           09/07 20:31
→ DrStein:後來會議結論是OOP不該存在於fortran中                    09/07 20:32
→ DrStein:也就是fortran完全定位為科學計算程式                     09/07 20:32

http://shootout.alioth.debian.org/u32q/benchmark.php?test=all&lang=gcc&lang2=ifc
上面的測試結果是用Fortran2003? 還是其他的版本?
是否有測試Fortran的強項?
Fortran的強項是什麼?(陣列? 迴圈?)

http://gcc.gnu.org/ml/fortran/2007-01/msg00619.html
g77的前身是g2c,也就是說用g77編譯Fortran時,她會先把Fortran翻成C
於是若拿g77當compiler,Fortran在速度上對C毫無優勢
那麼gfortran的編譯方式也是透過g2c嗎?

目前合法免費的Fortran compiler就只有gfortran?
須要用付費的Fortran compiler才能享有Fortran的高速嗎?

以下來個問題總結:
與C比較,Fortran的強項是什麼?(陣列? 迴圈? 複數? 禁止重複?)
該強項是比C快的原因是什麼?
哪些版本的Fortran快於C?
須要用付費的Fortran compiler才能完全享有Fortran的高速或強項嗎?

2011年9月28日 星期三

Fortran Filename extension

http://www.ats.ucla.edu/clusters/common/computing/compilers.htm

Fortran 90/Fortran 77 Commands associated with Compilers

CompilerSerial
* fileName extension, not command used, determines language version (can be changed by command flags)
Parallel
* See the MPI doc.
CommandsFortran 90 fileName extensionsFixed Format fileName extensionsFortran 90Fortran 77
Intelifort.f90 (.F90 preprocessed).f, .for, .ftn (.F, .FOR, .FTN, .FPP preprocessed)mpif90mpf77
GNUgfortran.f90 (.F90 preprocessed).f, .for, .ftn


http://software.intel.com/sites/products/documentation/hpc/compilerpro/en-us/fortran/lin/compiler_f/bldaps_for/common/bldaps_under_inpext.htm
Filename
Interpretation
Action


filename.f
filename.for
filename.ftn
filename.i
Fortran fixed-form source
Compiled by the Intel® Fortran compiler.
filename.fpp
and, on Linux, filenames with the following uppercase extensions: .FPP, .F,.FOR, .FTN
Fortran fixed-form source
Automatically preprocessed by the Intel Fortran preprocessor fpp; then compiled by the Intel Fortran compiler.
filename.f90
filename.i90
Fortran free-form source
Compiled by the Intel Fortran compiler.
filename.F90 (Linux OS and Mac OS X)
Fortran free-form source
Automatically preprocessed by the Intel Fortran preprocessor fpp; then compiled by the Intel Fortran compiler.


Filename ExtensionsSource Form/Preprocessing
.f, .fix, .for, .fpp, .ftn
.F, .F77, .FIX, .FOR, .FTN, .FPP, .fpp
Fixed source form
.f08, .f03, .f95, .f90Free source form with INCLUDE lines
.F08, .F03, .F95, .F90Free source form with C preprocessor directives