Julia 中是否有本地保存的数据(如 Fortran 的)? [英] Is there locally saved data (like Fortran's) in Julia?

查看:30
本文介绍了Julia 中是否有本地保存的数据(如 Fortran 的)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试将整个 Fortran 77 代码重新编写到 Julia 中.在此 Fortran 代码中,有各种具有 SAVE 属性的局部变量(来自 SAVE 语句或在 DATA 语句中显式初始化时).

I am currently trying to re-write an entire Fortran 77 code into Julia. Within this Fortran code, there are various local variables with the SAVE attribute (from a SAVE statement or when explicitly initialized in a DATA statement).

问题是:我无法重现在 Fortran 中使用这些保存的变量所期望的相同结果.例如,该代码有许多从 Numerical Recipies 中提取的随机生成器程序.特别是ran3.f,不仅从主程序中调用,而且从其中的许多不同子例程中调用.

The problem is: I haven't been able to reproduce the same outcome that one would expect with these saved variables in Fortran. For example, the code have many random generator programs extracted from the Numerical Recipies. In particular, ran3.f, called not only from within the main program, but from many different subroutines within it.

ran3.f 声明:

FUNCTION ran3(idum)

c Returns a uniform random deviate between 0.0 and 1.0. 
c Set idum to any negative value to initialize or reinitialize the sequence.

INTEGER idum

INTEGER MBIG,MSEED,MZ

REAL ran3,FAC

PARAMETER (MBIG=1000000000,MSEED=161803398,MZ=0,FAC=1./MBIG)

INTEGER i,iff,ii,inext,inextp,k
INTEGER mj,mk,ma(55)    

SAVE iff,inext,inextp,ma

DATA iff /0/

if(idum.lt.0.or.iff.eq.0)then

CODE ... Etc, etc...

return
end

我已经能够在给定特定种子的情况下重现相同的结果(随机数),并且还可以根据预期在整个程序和子例程中运行,但这只是通过使用大量返回变量和输入变量.

I have been able to reproduce the same results (random numbers) given a particular seed, and also to behave across the entire program and subroutines according to what is expected, but it was only by using a lot of returning variables and input variables.

这两行怎么办

SAVE iff,inext,inextp,ma
DATA iff /0/

在 Julia 中被替换以完成相同的行为?

be replaced in Julia in order to acomplish the same behavior?

推荐答案

为了尽可能接近使用 SAVEed 数据或 COMMON 块来翻译代码,我们可能可以使用一个常量、全局、可变结构变量(放置在顶部-级别范围),例如

To translate the code using SAVEed data or COMMON blocks as closely as possible, we can probably use a constant, global, mutable struct variable (placed in the top-level scope), e.g.

mutable struct myfunc_common_t     # or simply "type" in older versions
    num :: Int
    # constructors if necessary
end
const myfunc_common = myfunc_common_t( 0 )

function myfunc( idum )
    com = myfunc_common

    if idum < 0
        com.num = 100
    else
        com.num += 1
    end
    @show com.num
end

function myshow()
    @show myfunc_common.num
end

myfunc( -1 )
myfunc( 123 )
myfunc( 456 )
myfunc( 789 )

myshow()

给了

com.num = 100
com.num = 101
com.num = 102
com.num = 103
myfunc_common.num = 103

这种常量全局变量的使用是类型稳定的(从@code_warntype 可以看出),但它可能不适合并行计算(所以要小心...).如果可能的话,我认为将一些类型变量显式传递给函数会很好(并在函数内对其进行变异,或者从函数返回一个新的类型变量).确实,idum"就像一个状态"变量,所以我们可以将它替换为类型变量:)

The use of such constant globals are type-stable (as seen from @code_warntype) but it may be not suitable for parallel calculations (so be careful...). If possible, I think it would be nice to pass some type variable to a function explicitly (and mutate it within a function, or return a new type variable from the function). Indeed, "idum" is like a "state" variable, so we can replace it as a type variable :)

这篇关于Julia 中是否有本地保存的数据(如 Fortran 的)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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