Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looking for Beginner Info/Shortcuts to Optimizing Code

Status
Not open for further replies.

Lookingforhelponline

Technical User
May 10, 2012
5
US
Hello. This is my first time posting on this forum and it is my first time working with any sort of code. i don't have a programming foundation because I was just given this assignment as a test, and I can't seem to find any good info on how to program with Fortran 95 for a complete beginner. I'd appreciate that or some tips on how I can optimize the code. If anyone needs the code, I'll go ahead and post it. Thanks in advance.
 
Well, I am writing into a box that opened, when I wanted to answer to a thread. Hmmm. Something wrong here....

The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Okay, the last post was a test, where the posting ends when I proceed. (the input-box looking very strange!)

Lookingforhelponline:

I guess nobody from us here really is in need of your code. But if you want tips on how to improve your code, it would not be too bad an idea if we could see it.

As to programming skills: If you navigate to amazon and search for fortran books, you get a long list of books you could purchase. My favorite is Stephen J. Chapman Fortran 90/95 for Scientists and Engineers. It is well written and starts at beginner level.

Norbert


The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Thanks for the reply gummibaer/Norbert. I know no one has need of the code, but I meant in case anyone was willing to take a look at it. Also, thanks for your recommendation on the book. I'll try to get it ASAP.

As for the code:

subroutine cylinderdist_full(bc1,bc2,r1,r2,mu_a,mu_b,dist,ID)
real,intent(in)::bc1(2,3),bc2(2,3),r1,r2
real,intent(out)::mu_a(1,3),mu_b(1,3),dist
integer,intent(out)::ID
real::A(1,3),B(1,3),C(1,3),D(1,3),M1(3,2),M2(3,2)
real::p(1,2),q(1,2),r(1,2),s(1,2),n1(1,2),n2(1,2)
real::d1,d2,u,v
real::left,right
logical judge1, judge2
interface mean
function mean1(v,d)
real,intent(in)::v:))
integer,intent(in)::d
real::mean1
endfunction mean1
function mean2(v,d)
real,intent(in)::v:),:)
integer,intent(in)::d
real,target::mean21(1,size(v,2)),mean22(size(v,1),1)
real,pointer::mean2:),:)
endfunction mean2
function mean3(v,d)
real,intent(in)::v:),:,:)
integer,intent(in)::d
real,target::mean31(1,size(v,2),size(v,3)),mean32(size(v,1),1,size(v,3))&
&,mean33(size(v,1),size(v,2),1)
real,pointer::mean3:),:,:)
endfunction mean3
function mean4(v,d)
real,intent(in)::v:),:,:,:)
integer,intent(in)::d
real,target::mean41(1,size(v,2),size(v,3),size(v,4)),mean42(size(v,1),1,size(v,3),size(v,4))&
&,mean43(size(v,1),size(v,2),1,size(v,4)),mean44(size(v,1),size(v,2),size(v,3),1)
real,pointer::mean4:),:,:,:)
endfunction mean4
endinterface mean
A=reshape((/bc1(1,:)/),(/1,3/))
B=reshape((/bc1(2,:)/),(/1,3/))
C=reshape((/bc2(1,:)/),(/1,3/))
D=reshape((/bc2(2,:)/),(/1,3/))
M1=null_3b3(A-B);
r=matmul((C-A),M1);
s=matmul((D-A),M1);
n1=matmul((s-r),reshape((/0,1,-1,0/),(/2,2/)))
n1=n1/norm_bo(n1)
v=dot(n1,s)
d1=norm_bo(v*n1-r)/norm_bo(s-r)*cos_bo((v*n1-r),(s-r))
j=0
if(d1>1.0 .or. d1==1.0) then
d1=1.0
j=j+1
elseif (d1<0.0 .or. d1==0.0) then
d1=0.0
j=j+1
endif
mu_b=C+d1*(D-C)
M2=null_3b3(D-C)
p=matmul((A-D),M2)
q=matmul((B-D),M2)
n2=matmul((q-p),reshape((/0,1,-1,0/),(/2,2/)))
n2=n2/norm_bo(n2)
u=dot(n2,q);
d2=norm_bo(u*n2-p)/norm_bo(q-p)*cos_bo((u*n2-p),(q-p))
if (d2>1.0.or.d2==1.0)then
d2=1.0
j=j+2
elseif (d2<0.0 .or. d2==0.0)then
d2=0.0
j=j+2
endif
mu_a=A+d2*(B-A)
select case(j)
case(1)
d2=dot((mu_b-A),(B-A))/(norm_bo(B-A))**2
if (d2>1.0 .or.d2==1.0)then
d2=1.0
j=j+2
elseif (d2<0.0 .or. d2==0)then
d2=0.0
j=j+2
endif
mu_a=A+d2*(B-A)
case(2)
d1=dot((mu_a-C),(D-C))/(norm_bo(D-C))**2;
if (d1>1.0.or.d1==1)then
d1=1.0
j=j+1
elseif (d1<0.0.or.d1==0)then
d1=0.0
j=j+1
endif
mu_b=C+d1*(D-C)
case(3)
d2=dot((mu_b-A),(B-A))/(norm_bo(B-A))**2
d1=dot((mu_a-C),(D-C))/(norm_bo(D-C))**2
if (d2<1)then
if(d2>0)then
j=j-2
mu_a=A+d2*(B-A)
endif
endif
if (d1<1)then
if(d1>0)then
j=j-1
mu_b=C+d1*(D-C)
endif
endif
endselect
ID=0
if (norm_bo(mu_a-mu_b)<(r1+r2))then
select case(j)
case(0)
ID=1
case(1)
if ((norm_bo(mu_b-mu_a)-r1)<r2*sin_bo((mu_b-mu_a),(C-D)))then
ID=1
endif
case(2)
if ((norm_bo(mu_b-mu_a)-r2)<r1*sin_bo((mu_b-mu_a),(A-B)))then
ID=1
endif
case(3)
if(abs(dot((mu_b-mu_a),(C-D)))/norm_bo(C-D)<r1)then
if(abs(dot((mu_b-mu_a),(A-B)))/norm_bo(A-B)<r2)then
if(norm_bo(mu_a-mu_b)<sqrt(r1**2+r2**2+2*r1*r2*cos_bo((mu_a-mean(bc1,1)),(mu_bmean(
bc2,1)))))then
ID=1
endif
endif
endif
endselect
endif
dist=norm_bo(mu_a-mu_b)
endsubroutine cylinderdist_full

As you can see, I have no idea how I'm supposed to get around to optimizing this. I understand some of the equations, but the programming aspect is not something I have preparation in. I went ahead and downloaded Silverfrost FTN95 so I can try and see how this works.

From here on, I count on the guidance of the wise ones on the forums...
 
Please enclose your code in tags

Code:
then enter your code
[/code

Close this last tag again with a ]. I did not do this here, for then you would not see what I did.

And, if you want to optimize your code: What would be your target that you want to reach by optimizing ? And please understand, we are willing to help you, but the work is yours to do.

On first view I would say, this code cannot be executed on its own. It is just one subroutine, without a main program and a few functions missing that are named in the interfaces.


Norbert


The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
That's fine. I know that I have to do the work. But what I was looking to make this amount of code faster. Right now, I didn't even know that code was part of something bigger, and that's one of the reasons I came here. What can I do to make the code faster without sacrificing accuracy? I was fiddling around with the compiler and ran it and it seems to show some errors, but I have no idea how that works either...

On a seperate note, sorry about not putting that in tags. I'll remember next time. It seems like you're unable to edit posts on this forum...
 
Don't know how much you know or how much you don't know; but let me just say that often times is rather difficult to help somebody who does not what he/she is supposed to do in the first place.

In any case, here are a few things to think about.

OP: You want to optimize this...
do you have any idea how to measure such optimization?
do you know how fast the code is 'as-is'?
how are you going to know if your changes are making faster or slower?

OP or anybody else:
I have been programming in F77 for a while, but I am yet to take full advantage of F90 matrix intrinsic functions...I know they are convenient, but are they good? fast? The code above has really small matrics (1,3), (2,3)...it wonder if writing such operations explicitly may save some time.

Anybody have any idea what the over head of these intrinsic functions might be? At what size does it really pay to use them?

my 2 cents.

Germán
 
Who came to the idea to optimize this code for speed anyway ? Just on my first impression there is nothing that really accounts for a slow running code. As Germán said, the matrices are small. There are a lot of ifs but nothing cycling, it is pretty straight through executable (unless I oversee something hidden). There are no time consuming deep pointer operations.

The only thing I see is that there are quite a few reshape operations. These could be avoided if you code the matrix operations so that you do not need to reshape the matrices. But you may get some index computations as a penalty for that. But if there is much gain in time is more than doubtful.

As to trading accuracy for speed: This routine is done in default real kind, which I assume is either 32 or 64 bit accuracy. No sense to convert these data to half accuracy to gain computation time.

All this holds true for the subroutine that we see here. The functions mean1 to mean4 get interfaced but are never called there. But there seem to be other entities (norm_bo(), dot(), cos_bo () ...) without type declaration, so I take them to be external functions. Some of them are called quite frequently and what potential might be in there remains to be seen.

Norbert

The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Sorry for the delay but you guys were right. I don't know much about the code, but I managed to find something better I could use than this that might be applicable to what I'm trying to do, and hopefully when I make the code I'll go ahead and post it on here. Thanks again for your help.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top