s***k 发帖数: 23 | 1 【 以下文字转载自 Programming 讨论区,原文如下 】
发信人: stock (Microsoft is dead), 信区: Programming
标 题: Perl for programmers(9): subroutines
发信站: The unknown SPACE (Thu May 25 15:41:31 2000) WWW-POST
In perl, Subroutine and function are in the same mechanisms.
Definitions:
sub abc;
sub abc {}
$ABC = sub{};
calls:
abc($a,$b);
abc $a,$b;
&abc; #parameters passing through @_to subroutines
&$ABC($a,$b);
or &$ABC($a,$b);
subroutine passes data into and out of the block through a
@_ list, its parameters are $_[0], $_ |
|
c******o 发帖数: 8 | 2 I need to install a Fast Fourier Transform subroutine in dec
alpha machine. comlib.sgimath has FFT subroutine, but it is
for sgi machine. Can anyone recommend any web site to
download a FFT subroutine for alpha machine? Thank you. |
|
y**l 发帖数: 90 | 3 Who has The Engineering and Scientific Subroutine Library (ESSL). I
need some codes:
DGEF - General Matrix Factorization
DGES - General Matrix Solver
SCRFT/DCRFT - Complex to Real Forier Transform
SRCFT/DRCFT - Real to Complex Fourier Transform
Or if you have those codes. Thank you very much! |
|
p******t 发帖数: 228 | 4 代码白痴请教fortran问题Orz
自己在试验一个算法,因为以前这方面的程序都是用fortran95写的,所以我现在借用
一下看看。
可是编译时就出现错误:(1) procedures must be either all SUBROUTINEs or all
FUNCTIONs
这个gfortran是gcc4.6自带的,在mac上运行
另外如果在linux下运行gcc4.6自带的gfortran去编译这个程序居然就通过了,不清楚
时啥原因造成的。。。
谢谢 |
|
t******1 发帖数: 334 | 5 大家有在C里面call lapack subroutine么?
我在学校的超级计算机上编译,include跟linker的flag都设好了,但是还是出现
undefined reference to这个错误,不知道哪儿出问题了,有人知道么?
google结果,据说可能是一个libg2c的库的问题,但我一个新手,啥都不懂,能知道
linker已经觉得很牛鼻了,这个问题对我而言很棘手。
希望懂的同志能帮一下手。谢谢。 |
|
c******e 发帖数: 56 | 6 遇到一个麻烦事,原来写的是subroutine,和主程序在同一个文件, 现在要把sub转到
一个module里去。总是不成功,求教。 例程如下:
@var3_array_out=();
mySubTest(\@var1_array_in, $var2_scale_in, \@var3_array_out);
for ($j=1; $j<=100; $j++){
printf outfile $j."\t".$var3_array_out[$j]."\n";
}
...
sub mySubTest{
my($var1_array_in, $var2_scale_in, $var3_array_out) = @_;
my($i, $j, $k);
my(@var1_array)=@{$var1_array_in};
my(@var3_array)=@{$var3_array_out};
...
@{var3_array_out} = @var3_array;
}
我是直接把mySubTest拷贝到myModule.pm里面,然后在主程序里面调 |
|
f******y 发帖数: 2971 | 7 For example, I dont want the following script to compile. One way to do it
is to put all subroutines to the head of the file. That is not convenient
most of time. I tried to use another package, it still can read $a.
use strict;
my $a = 0;
package Function; #This does not stop it.
sub read {
return($a + 1);
} |
|
y**l 发帖数: 90 | 8 Who has The Engineering and Scientific Subroutine Library (ESSL). I
need some codes:
DGEF - General Matrix Factorization
DGES - General Matrix Solver
SCRFT/DCRFT - Complex to Real Forier Transform
SRCFT/DRCFT - Real to Complex Fourier Transform
Or if you have those codes. Thank you very much! |
|
z*h 发帖数: 22 | 9 hi, guys,
I just wonder how I could fork a subroutine in unix other than the whole
copy of current process. any idea? thanks. |
|
x*y 发帖数: 364 | 10 Does anybody has a subroutine which does the same thing as function luinc in
Matlab? I need luinc as preconditioner in my code. Other incomplete LU from
Sparskit doesn't work, only luinc from Matlab works perfectly, but luinc in
Matlab is a built-in function, I can't see how does it work? |
|
p******t 发帖数: 228 | 11 代码白痴请教fortran问题Orz
自己在试验一个算法,因为以前这方面的程序都是用fortran95写的,所以我现在借用
一下看看。
可是编译时就出现错误:(1) procedures must be either all SUBROUTINEs or all
FUNCTIONs
这个gfortran是gcc4.6自带的,在mac上运行
另外如果在linux下运行gcc4.6自带的gfortran去编译这个程序居然就通过了,不清楚
时啥原因造成的。。。
谢谢 |
|
f**d 发帖数: 768 | 12 这是一本计算神经科学的优秀著作,全文拷贝这里(图和公式缺),有兴趣的同学可以
阅读
如需要,我可以分享PDF文件(--仅供个人学习,无商业用途)
From Computer to Brain
William W. Lytton
From Computer to Brain
Foundations of Computational Neuroscience
Springer
William W. Lytton, M.D.
Associate Professor, State University of New York, Downstato, Brooklyn, NY
Visiting Associate Professor, University of Wisconsin, Madison
Visiting Associate Professor, Polytechnic University, Brooklyn, NY
Staff Neurologist., Kings County Hospital, Brooklyn, NY
In From Computer to Brain: ... 阅读全帖 |
|
gw 发帖数: 2175 | 13 有一段大概这样的Fortran code,
subroutine A
Part B
do i=1,N1
Part C1
enddo
do i=1,N2
Part C2
enddo
contains
function d1
subrounine e1
endsubroutine A
这里面 part B, C1, C2都用到了contains里的functions and subroutines.其中C1和
C2是一段比较长的代码,并且有很大的相似性,但功能有一点不同。直接这样写,看着
这个程序总有点别扭,因为C1,C2两大段总让人有重复的感觉。于是想写一个新的
subroutine C来取代这两部分,使整个程序变成这样的。
subroutine A
Part B
do i=1,N1
call C(1)
enddo
do i=1,N2
call C(2)
enddo
contains
function d1
su |
|
O******e 发帖数: 734 | 14 module blk1
integer,parameter::n=. . .
real,dimension(1:n),public::w1,w2,w3
end module blk1
subroutine a(. . .)
use blk1,only:v1=>w1,v2=>w2,v3=>w3
. . .
return
end subroutine a
subroutine b(. . .)
use blk1,only:u1=>w1,u2=>w2,u3=>w3
. . .
return
end subroutine b |
|
K******C 发帖数: 230 | 15 我的
code 基本上是用frotran 77 和mpi library 写的.以前用GNU 的编译的,一直没有什
么问题:
mpif77 -r8 -o code.f
现在我把code 移到了blue gene上,用了xl fortran编译,就出现在问题了
mpixlf77 -O2 -qrealsize=8 code.f
我在code里面用了common block 定义全局变量
有一个全局变量不应该在 运行过程中变化的,后来我把这个变量 在所有的subroutine
里面print out。发现每当call 某个 subroutine以后,这个值就变化了,奇怪的是在
这个subroutine 里面 ,我没有定义这个变量所在的common block. 而且这个值从以来
的的‘4‘,一下变成了 ’2XXXX‘ ,而且每次call这个subroutine 就继续变大。
我估计是不在编译的时候设定环境变量有问题,但不知道怎么改。 不是高手有什么好
主意?
BTW:我的code 里面还定义了 double presicion的变量 |
|
s*****l 发帖数: 167 | 16 I wrote a program with 2 subroutines, say I have
program main
contains
subroutine pg1
...
real(8) :: x(20)
call pg2
end subroutien pg1
subroutine pg2
...
print*, x(10)
end subroutine pg2
Even if I donot pass x to from pg1 to pg2,
and I donot have any variable x in pg2,
I can still find x in pg2!
actually I can print it, or compute it.
what is wrong? |
|
y***r 发帖数: 1845 | 17 你说错了。他是因为没有使用显式声明。试一试下面的程序
program test
implicit none
call p1
contains
subroutine p1
implicit none
real x
x = 20
call p2
end subroutine p1
subroutine p2
implicit none
print *, x
end subroutine p2
end program test
You will meet a severe err if your compiler is okay. |
|
f*******d 发帖数: 339 | 18 What I said was correct (see your F90 manual description about "contains").
But it is also true that if you use implicit none in the subroutine,
it will prevent the internal subroutines to reference other internal
subroutine.
Nevertheless, even in that case they can still access the variables in the
main program.
Instead of the example you give, try the following:
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
program test
implicit none
real x
call p1
contains
subroutine p1
implicit none
x |
|
w*w 发帖数: 3 | 19 I read a fortran90 book. It says subroutines and functions
can be overloaded in fortran90. But it seems this is not
true overloading. It can just realize polymorphism.
Say if I inherit a module from a parent module and I want
to overload a subroutine in the parent module but still
keepping the shape of the subroutine, i.e. arguments of the
subroutines are the same. Can this be done in fortran90? |
|
S**I 发帖数: 15689 | 20 a stack frame is the memory space allocated to a called subroutine on the
call stack. It may contain arguments passed to the subroutine, local
variables of the subroutine, saved registers, etc., layout of the stack
frame depends on system ABI/calling convention. |
|
m*******s 发帖数: 22 | 21 本人得到一个fortran subroutine,现需利用该subroutine将本人的1460
个数据文件转换成另一种格式,可惜本人对fortran所知寥寥,故特此求
各位高人指教,谢谢!
我的初步想法是:用"do......end"语句反复读入待转换的数据文件,
用subroutine处理后,写入指定的输出文件直至最后一个文件处理完毕。
本人的输入文件名为:xx_i_xxx,i=1,2,...,1460,即:各个文件仅i
一处不同,请问如何将文件名按如下分段串起来,从而实现循环读入
============================================================
第一部分:xx_
第二部分:i
第三部分:_xxx
============================================================
文件的功能?
再次感谢! |
|
k**********g 发帖数: 989 | 22
Object oriented design / object oriented programming
Namely, how to design and implement objects (classes) correctly in your (
employer's) choice of programming language.
Elementary level: learn the must-follow guidelines in designing objects
* Design of interfaces
* Object Consistency / Invariants (how to ensure the object works
correctly when its methods are called over and over again)
* Visibility (public vs private)
* It is convenient and sometimes necessary to split up a public m... 阅读全帖 |
|
O******e 发帖数: 734 | 23 Dynamic allocation in heap memory using Fortran 90/95:
integer,dimension(:),allocatable::p2i
integer::size
integer::alloc_stat
read(some_file,some_format)size
allocate(p2i(1:size),stat=alloc_stat)
if(alloc_stat.ne.0)stop"error allocating p2i"
...
deallocate(p2i,stat=alloc_stat)
if(alloc_stat.ne.0)stop"error deallocating p2i"
If you want to call a subroutine to allocate p2i, return from that
subroutine, use p2i elsewhere, then call another subroutine to deallocate
p2i, you will need to declare p2 |
|
L***s 发帖数: 1148 | 24 先不谈不同OS层面的机制,IOCP/select/epoll神马的
在nodejs层面,简单地说就是一个大的event loop,单线程
每个async subroutine都注册到这个event loop上,逐个轮询
某个subroutine一旦io阻塞,就把CPU控制权yield回event loop
event loop再把CPU控制权交给下一个subroutine |
|
m*******s 发帖数: 22 | 25 本人得到一个fortran subroutine,现需利用该subroutine将本人的1460
个数据文件转换成另一种格式,可惜本人对fortran所知寥寥,故特此求
各位高人指教,谢谢!
我的初步想法是:用"do......end"语句反复读入待转换的数据文件,
用subroutine处理后,写入指定的输出文件直至最后一个文件处理完毕。
本人的输入文件名为:xx_i_xxx,i=1,2,...,1460,即:各个文件仅i
一处不同,请问如何将文件名按如下分段串起来,从而实现循环读入
============================================================
第一部分:xx_
第二部分:i
第三部分:_xxx
============================================================
文件的功能?
再次感谢! |
|
f***d 发帖数: 101 | 26 【 以下文字转载自 Programming 讨论区 】
【 原文由 felid 所发表 】
Hi,
I am working on a fortran program which includes several source files, with
multiple subroutines in each source file. For some of the subroutines, I have
conditional branches for decision making.
I am trying to use gprof on SunOs 4 to make some performance analysis.
In some of the subroutines, I have if block like this:
if (a.eq.b) then
call f1()
else
call f2()
end if
I am pretty sure that f2 will never be reached, but the gprof k |
|
f*******d 发帖数: 339 | 27 I think the idea is that if a subroutine does not need to access the
variables in the host, it should not be made internal (i.e. "contains" should not be used).
Also, if some variables and routines are often used at the same time, you should make them
a module, and explicitely specify "use" this module when necessary. Occasional use of
local variable in the internal subroutine can be declared by making the implicit none statement
in the subroutine, as in yager's example. |
|
w**d 发帖数: 2334 | 28 The array is small. The codes only take around 20M memory.
I found out what caused the problem, but don't know why.
Whenever I declare an array like a(0:node) in a subroutine
with node being passed from other subroutines or main, there will
be bus error.
But if I declare a(node) or a(0:node) with node being given in the
current subroutine, it works fine. |
|
m********n 发帖数: 71 | 29 谢谢longtian,你是说module吗?我是想保留原来的subroutines,不想改动原程序太
多。
我知道数个subroutines可以被编译成一个subroutine从而能被调用, 但不清楚具体细
节。恳请指教, 谢谢! |
|
l***i 发帖数: 38 | 30 what 's the difference between Macro and user defined subroutine. To define
the material properties for each element, can I use ANSYS macro or material
subroutine to do it? BTW, user subroutine has to be written in Fortran? Hope I
can't get help. Not many peope in my department knows how to answer this
question. |
|
S***y 发帖数: 186 | 31 SUBROUTINE SB(LMAX,Z,JL)
INTEGER::LMAX
COMPLEX::JL(0:LMAX),Z
INTEGER::L
COMPLEX(KIND=8)::DJL(0:LMAX),NORM
DJL(LMAX)=(0.0D0,0.0D0)
DJL(LMAX-1)=(1.0D0,0.0D0)
DO L=LMAX-2,0,-1
DJL(L)=(L+L+3)*DJL(L+1)/Z-DJL(L+2)
END DO
NORM=DJL(0)
DJL(0)=SIN(Z)/Z
NORM=DJL(0)/NORM
DO L=1,LMAX
DJL(L)=DJL(L)*NORM
END DO
JL=DJL
END SUBROUTINE SB
This subroutine tabulates Spherical Bessel Function in JL(0 |
|
s******2 发帖数: 282 | 32 不知道string排序是什么意思
假设就是10G LW 整数,给一个4G内存空间对10G数排序,也假设有个subroutine能把1G
整数在4G内存中排序
1. read 1st 1G numbers from disk
2. sort using subroutine
3. read numbers from disk, make sure the smallest 1G number in ram and
write the larger ones back in disk
4. write the smallest 1G numbers back in disk
repeat 1 ~4 for the second 1G number till the end
Notes:
You may rescale the 4G memory to 1G or any other size
string 排序 要是指有index之类的结构 (比方每个string做hash),又可以基于此方
法对index操作 |
|
t******l 发帖数: 10908 | 33 但图灵机也没解决所有的问题,毕竟那纸带又臭又长,跟裹脚布的差别也不大。
然后出现了一个逃学还振振有词的浪子哥们 -- “信息隐藏原则”。人的口号是:
(1)如果这段纸带我不用改,我不需要看,我直接拿来用,call subroutine
就完事了。学啥学?学个屁!我不学也给老板交差了。
(话说 subroutine 也是一个 on-demand 高级操作符号不是。不过简单,
这高中生都能看懂那个名字和参数不是?)
(2)如果这段纸带我需要改,我也不需要看。我直接扔掉那段旧纸带,然后按
要求和接口标准重新做一条新纸带。旧纸带我根本不看!
结论,所有的纸带我都不需要看、也不需要懂、更不需要学! |
|
h**********c 发帖数: 4120 | 34 贴的code,行首的空格和tab都被吃了回扣了,
不过visual studio下把code, CTRL-K,CTRL-F很容易就从新FORMAT
根据liuhulan 同志介绍的MSDN上, KEN KERR的文章,
对单线程做了一些改动,主要还是基于LOCALITY的思想:
1. 不在SUBROUTINE 中使用NEW, DELETE,能不用DYNAMIC MEMALLOC尽量不用,坚决不要
在循环体中使用.
基于这一点,原来单线程900秒,提高到300秒!!
2. 所有SUBROUTINE 改成INLINE,300->287秒.
3. 在循环体内不进行console print,影响不大.
因为我把程序改成把要输出的结果存到一个array,并且要一个counter,因此会有race
condition,需要cs or mutex. multithreading 测试待续. |
|
O******e 发帖数: 734 | 35 It declares a one-dimensional integer array that is allocatable (you can
allocated whatever number of elements or amount of memory is needed) and
is also saved between allocations of the function or subroutine (the
array will not be deallocated when the subroutine or function returns). |
|
H***a 发帖数: 735 | 36 Thx for the suggestion. Yes my data are in a structure exactly like you
described, some integers, followed by some complex numbers, and some
integers, some complex numbers, and so on. I found Matlab did not
perform well in reading this way.
I had no idea about C-mex before you talking about it. To me understanding,
C-Mex files are applied to dynamically link subroutines in C source code so
in Matlab it can utilize those subroutines as builit-in functions after
compling them, righ |
|
a***a 发帖数: 92 | 37 程序的结构
SUBROUTINE A
DIMENSION DSTRESS(4)
COMMON DSTRESS
CALL KMLT1(DDSDDE,DESTRAN,DSTRESS,NTENS)
END
SUBROUTINE KMLT1(DM1,DM2,DM,NTENS)
DIMENSION DM(4)
COMMON DM
END
A中要call KMLT1,可是运行出现错误Error: Conflicting attributes or multiple
declaration of name. [DM]
可是dstress 没有出现错误。是什么问题? |
|
x*******a 发帖数: 11067 | 38 想法很简单,就是写一个程序用所有机器上的processor来跑同一个模型。模型的输入文
件不一样,结果也就不一样。不同的输入文件在不同的文件夹下面。
我的程序如下(是Fortran的),第一次跑了没问题,但是再跑一次,就出现了这样的错
误信息:
"The process cannot access the file because it is being used by another
process".
请问这个问题是怎么产生的,怎么解决呢?
谢谢!
!--------------------------------------------
program ompHelloWorld
use omp_lib
integer NTHREADS, TID,
!$OMP PARALLEL PRIVATE(TID), SHARED(NTHREADS)
TID = OMP_GET_THREAD_NUM()
PRINT *, 'Hello World from thread = ', TID
CALL RUNMODEL(TID)
!$OMP END PARALLEL
end program om... 阅读全帖 |
|
q*****m 发帖数: 73 | 39 Do you want to call a "C" subroutine _from_ Fortran,
or do you want to call a Fortran subroutine _from_ C? |
|
g**d 发帖数: 28 | 40 我这样做过.
我写的copyright是属于我们实验室的.
author是我.
但那家伙那了我的东西后,把所有的title都删掉了.
我问他为什么删.
他说每个subroutine都有, 为了打印方便.
我release两个版本的subroutine都是这样.
这个家伙对人特客气.
当该怎么还是怎么.我也没治.
翻脸也不合适.大家都是混饭吃,
所以自己保护一下自己了.
有没有别的办法加密程序. |
|
w*****g 发帖数: 47 | 41 Suppose I first define a subroutine A(d), where d's dimension and values are
to be specified. Now in another subroutine B(p, d), I want to call subrountine
A if and only if d's dimension is p, and the values of d satisfy d(i)>=d(j),
and 1<=d(i)<=10, where 1<=i
Thanks! |
|
f*******d 发帖数: 339 | 42 "contains" declare the subroutine or function as internal, which has access to all
variables of the module or other internal subroutines in the same module. If you
don't want this behavior, do not use "contains" |
|
S***y 发帖数: 186 | 43 你如果用Fortran, 试试这个:
SUBROUTINE ZGEINV (N,A)
IMPLICIT NONE
INTEGER :: N
COMPLEX*16 :: A(N,N)
INTEGER :: IPIV(N)
INTEGER :: INFO, LWORK
COMPLEX*16, ALLOCATABLE :: WORK(:)
LWORK=N*ILAENV(1,'ZGETRI',' ',N,-1,-1,-1)
ALLOCATE(WORK(LWORK))
CALL ZGETRF(N,N,A,N,IPIV,INFO)
CALL ZGETRI(N,A,N,IPIV,WORK,LWORK,INFO)
DEALLOCATE(WORK)
END SUBROUTINE ZGEINV
这个是算复数矩阵的,如果你是实数矩阵,
把complex*16都换成real*8, ZGE都换成DGE.
当然这个是算普通矩阵的,对于对称正定矩阵,
可能还有更好的方法。
最后,要用LAPACK, 一定要用optimised BLAS,
否则对大矩阵,慢上个3,4倍都很正常。 |
|
x*y 发帖数: 364 | 44 The compiler group of CS in my university are developing HPCVIEW to optimize
the code. I tested it on my Fortran code on a SGI origin machine last year,
it's really cool, code can even be much fater if we pass arrays from
subroutine to subroutine in good way. It will be really great if this tool
come to be in use. |
|
s*****l 发帖数: 167 | 45 I wrote some mpi-fortran code, and I discovered something funny.
Everytime I take the number of grid points to be some power of 2, the program
runs smoothly, and produces good results. But if I take the number to be
something else, the program will be terminated after running for a while.
Here # of grid points = # of processors \times # of subroutines run on each
processor, and the execution of each subroutine is completely independent, and
the only mpi commands I use are simply mpi_scatter and |
|
w**d 发帖数: 2334 | 46 I asked the same problem on a Fortran newsgroup. Somebody suspected it might
be caused by a compiler bug. I don't know what that means.
That guy also thinks I used F90 compiler. He said the automatic array is
illegal in F77.
Then another guy suggests me to do as follows:
program main
real*8 a(0:10)
call sub(a)
end
subroutine sub(b)
real*8 b(*)
b(0) = 7
end
It is just like those subroutines in Lapack. But there are lots of
work arrays in my code, I don't know how realistic this way is. |
|
l**g 发帖数: 5 | 47 1。这里应该有错:
real*8 b(*) == real*8 b(1:)
所以这里 b(0) 是 bound violation,a(0)的值并不会改变。
2。我觉得你的程序是不是不自觉地引用指针,引起内存访问出错。比如
subroutine sub(b1,N)
real*8 b1(0:N)
b(0) = 7
end
subroutine sub(N)
real*8 b2(0:N)
b(0) = 7
end
其中b1是双精度数组;但fortran把b2解释为local指针数组,指向double.
前者是对的,后者就会出错。
命令g77/f77执行的可能是f90 或 f95
if |
|
w**d 发帖数: 2334 | 48 does anybody have numerical recipe in c++? I need the subroutine to solve
A x = b with A being symmetric.
I tried to use clapack. The software won't allow me to call the BLAS fortran
subroutines. Thanks ahead. |
|
a******s 发帖数: 232 | 49 【 以下文字转载自 Linux 讨论区 】
【 原文由 achillis 所发表 】
程序里面有很多subroutine,有没有办法知道各个subroutine计算的时候占用了
多少cpu时间呢?
thanks |
|
i****d 发帖数: 255 | 50 Depending on the compiler, the definition of an array like
COMPLEX TUINV(NROWS,NROWS),TDLDA(NROWS)
with NROWS passed by COMMON, is problematic. Just think
of what happens if NROWS is modified in your subroutine.
Add NROWS to the arguments of the subroutine.
程
但
是
运 |
|