Accessing files using MPI - file-io

THIS WORKED (see comment in the code)
I am new to MPI and still learning it. I am actually trying to write a code in Fortran to read data from same set of files (already generated) on each processor at same time and perform different computations in different processors. For which I decided to use,
program...
use mpi
implicit none
.
.
.
call mpi_file_open(mpi_comm_world,filename_i,mpi_mode_rdonly,mpi_info_null,i,ierr)
to start with the program. Now, each processor calls a subroutine in which I am trying to use normal fortran command to open files, open(i,*)...(since I don't use mpi in subroutine).
First, I am not confident about this idea itself. Next, it gives this error,
_open(mpi_comm_world,filename_i,mpi_mode_rdonly,mpi_status_ignore,i,ierr) (1)
Error: There is no specific subroutine for the generic 'mpi_file_open'
at (1)
Please give your suggestions and comments.
The thing I am trying to do is very long because of subroutine, I would just include a prototype code, if this is solved, my problem will be solved. The code below gives the same error as said before. Please give your suggestions. Thanks.
program hello
use mpi
integer::ierr,num_procs,my_id,i,j,no
call MPI_INIT(ierr)
call MPI_COMM_RANK (MPI_COMM_WORLD,my_id,ierr)
call MPI_COMM_SIZE (MPI_COMM_WORLD,num_size,ierr)
open(4,file='hella') !CHANGING THIS LINE
do i=1,num_size
if(i-1 .eq. my_id)print*,"In",my_id
if(i-1 .eq. my_id)then
read(4,*)no
print*,no
endif
enddo
call mpi_finalize(ierr)
end program hello

Related

Executing additional command in Backend that takes the to be generated file

I'm currently looking for a way to execute iverilog in in Yosys, to be more exact at the write_verilog step.
I need to feed iverilog the file, which will be generated by write_verilog (reason is, I need to uphold the variable source information, which are kept in the yosys attributes).
However the execute() function only writes into the file upon function end.
If I were to call iverlog testbench.v design.v with design.v being the file which is generated through write_verilog, I get an error, telling me it's missing modules.
Is it possible to carry out commands, that depend on the file which is generated after execute() has run through, while still being in the verilog backend?
You could use a script instead, to run iverilog after write_verilog, inside a Yosys script a line beginning ! is passed to the shell:
write_verilog design.v
!iverilog testbench.v design.v

How to run a program with parameters (Pick BASIC)

In Pick BASIC source code I see lines such as
CALL SOMEPROGRAM (PARAM1, PARAM2)
How can I invoke that same line from the TCL command prompt? I've tried variations of the following but nothing seems to work.
SOMEPROGRAM ('1','2')
The only way I've found is to write and compile a program with the single line command and then run that program.
If this was your routine:
SUBROUTINE REALPROG(A,B)
PRINT "A is ":A
PRINT "B is ":B
END
To call it from command line, you'd build this routine:
PROGRAM WRAPPERPROG
COMMAND.RECEIVED = SENTENCE()
VAR1 = FIELD(COMMAND.RECEIVED,' ',2)
VAR2 = FIELD(COMMAND.RECEIVED,' ',3)
CALL REALPROG(VAR1, VAR2)
END
Assuming you typed this from the TCL/ECL command line:
WRAPPERPROG DOG CAT
VAR1 would be DOG and VAR2 would be CAT
...and would call REALPROG with those parameters and you should see
A is DOG
B is CAT
Tcl can invoke your overall program file as a subprocess using exec, but it is up to your program to turn that into a call to the program and processing of correct arguments.
The Tcl code to run the program will probably look something like this:
exec {*}[auto_execok CALLERPROGRAM]
If you were passing the arguments 1 and 2 over, you'd do this:
exec {*}[auto_execok CALLERPROGRAM] 1 2
Again, that does not say how those values get from the command line into the Pick Basic subprogram. You'll need to consult the Pick documentation for how to do that. But I know (and have tested) that Tcl will definitely have correctly provided them…
In Pick BASIC CALL statements are used to call subroutines and they can't be directly executed from TCL. Subroutines are denoted by setting the first word on the first line of the program to SUBROUTINE.
You can execute "programs" from TCL. These don't include the SUBROUTINE at the top of the source code. In some Pick BASIC variants you may need to include PROGRAM but I think most don't require that (I know D3 doesn't). These programs can be run from TCL but they don't get the command line parameters passed in automatically like subroutines do. I think you can use SENTENCE() in pretty much any Pick BASIC variant to get the command line parameters.
Here's an example program that will print the command line arguments:
PRINT SENTENCE()
END
You could use this to create a program that will take the command line arguments and pass them into a subroutine to do something for you.
I was literally typing out some elaborate answer, but your question has been answered. You cant directly call a subroutine, you need to call a program that calls the subroutine. Also subroutines are a good way to separate code from the main program to reduce clutter, but they arent always necessary. Other methods you can use are functions, or GOSUBS/GOTOS. This is an example of a GOTO below..
VAR = 'HELLO'
GOTO 10:
10:
CRT VAR
from the TCL you will call the name of your program and all of this code will be executed without calling another program.
The output will be the string hello.
I wrote a utility 30+ years ago to address this. Other Basics (QB, VB, Dartmouth) have a single command line. You are either writing lines into a program or processing single line requests. Pick did not.
I created an MD item called PRINT. It then runs a program called BP PRINT that takes the whole TCLREAD line, writes it out to another program space called BP PPRINT, compiles it and then runs it.
Incredibly useful. Thus at TCL these commands would work:
PRINT ; X=1 ; Y=2 ; CALL SOMESUB(X,Y)
PRINT ; FOR I=1 TO 12 ; PRINT (I*28)"DMA" ; NEXT I
PRINT ; OPEN "CUST" THEN READV NAME FROM "1234", 1 THEN PRINT NAME
PRINT OCONV("12345678","MD2Z,$")
PRINT DATE()
Basically anything that can be programmed within a single line of code can be typed at TCL this way. Any IF or ELSE statements must be completed in the same one line. Great for testing.
Should be part of every Pick implementation out of the box.
Mark Johnson

How do I tell Octave where to find functions without picking up other files?

I've written an octave script, hello.m, which calls subfunc.m, and which takes a single input file, a command line argument, data.txt, which it loads with load(argv(){1}).
If I put all three files in the same directory, and call it like
./hello.m data.txt
then all is well.
But if I've got another data.txt in another directory, and I want to run my script on it, and I call
../helloscript/hello.m data.txt
this fails because hello.m can't find subfunc.m.
If I call
octave --path "../helloscript" ../helloscript/hello.m data.txt
then that seems to work fine.
The problem is that if I don't have a data.txt in the directory, then the script will pick up any data.txt that is lying around in ../helloscript.
This seems a bit fragile. Is there any way to tell octave, preferably in the script itself, to get subfunctions from the same directory as the script, but to get everything else relative to the current directory.
The best robust solution I can think of at the moment is to inline the subfunction in the script, which is a bit nasty.
Is there a good way to do this, or is it just a thorny problem that will cause occasional hard to find problems and can't be avoided?
Is this in fact just a general problem with scripting languages that I've just never noticed before? How does e.g. python deal with it?
It seems like there should be some sort of library-load-path that can be set without altering the data-load-path.
Adding all your subfunctions to your program file is not nasty at all. Why would you think so? It is perfectly normal to have function definitions in your script. The only language I know that does not do this is Matlab but that's just braindead.
The other alternative you have is to check that the input file argument, data.txt exists. Like so:
fpath = argv (){1};
[info, err, msg] = stat (fpath);
if (err)
error ("could not stat `%s' : %s", fpath, msg);
endif
## continue your script knowing the file exists
But really, I would recommend you to use both. Add your subfunctions in your main program, the only reason to have it on separate file is if you plan on sharing with other programs, and always check input arguments.

Can't find libgfortran-3.dll in Fortran

I am new to mingw32-gfortran, and I am finding it hard to statically link the specific dll's. I am trying to write a code on finding large factorials,the code compiles without any errors on code::blocks but gives an error about missing dll's when opened from cmd. Is there anyway to statically link these dll's to my exe (I don't mind if it is bloated,I just want my code to be portable).
Does gfortran provide similar libraries for static linking? I have also posted my code for any reference. It would really mean much to me if someone here helped me out, I have already tried code blocks forums,and read numerous posts on other websites. Still no luck.
Note : I use Win 7 64-bit
! A fortran95 program for G95
! By WQY
program main
implicit none
integer ,allocatable,dimension (:) :: a
integer :: re_i,n,digit=1,temp=0,zero=0,i,j,beg,rate,ends
allocate(a(10000000))
a(1)=1
print*,'please enter the number'
read *, n
call system_clock(beg,rate)
do j=2,n
do i=zero+1,digit
temp=temp+a(i)*j
a(i)=temp-(temp/10)*10
temp=temp/10
end do
do while (temp>0)
digit=digit+1
a(digit) = temp-(temp/10)*10
temp = (temp/10)
end do
do while(a(zero+1)==0)
zero=zero+1
end do
end do
call system_clock(ends,rate)
write(*,1,advance='no') a(digit)
write (*,2,advance ='no') '.'
write (*,1) ( a(i),i=digit-1,digit-50,-1)
1 format(120i1)
print *,' X 10^' ,digit-1
2 format(100a1)
print '("computation time ",f6.3," seconds")',real(ends-beg)/real(rate)
re_i=system("pause")
end
Does anyone know how to compile a program in gfortan from command line? Errors seems to pop up every time I try to do so.

program to reproduce itself and be useful -- not a quine

I have a program which performs a useful task. Now I want to produce the plain-text source code when the compiled executable runs, in addition to performing the original task. This is not a quine, but is probably related.
This capability would be useful in general, but my specific program is written in Fortran 90 and uses Mako Templates. When compiled it has access to the original source code files, but I want to be able to ensure that the source exists when a user runs the executable.
Is this possible to accomplish?
Here is an example of a simple Fortran 90 which does a simple task.
program exampl
implicit none
write(*,*) 'this is my useful output'
end program exampl
Can this program be modified such that it performs the same task (outputs a string when compiled) and outputs a Fortran 90 text file containing the source?
Thanks in advance
It's been so long since I have touched Fortran (and I've never dealt with Fortran 90) that I'm not certain but I see a basic approach that should work so long as the language supports string literals in the code.
Include your entire program inside itself in a block of literals. Obviously you can't include the literals within this, instead you need some sort of token that tells your program to include the block of literals.
Obviously this means you have two copies of the source, one inside the other. As this is ugly I wouldn't do it that way, but rather store your source with the include_me token in it and run it through a program that builds the nested files before you compile it. Note that this program will share a decent amount of code with the routine that recreates the code from the block of literals. If you're going to go this route I would also make the program spit out the source for this program so whoever is trying to modify the files doesn't need to deal with the two copies.
My original program (see question) is edited: add an include statement
Call this file "exampl.f90"
program exampl
implicit none
write(*,*) "this is my useful output"
open(unit=2,file="exampl_out.f90")
include "exampl_source.f90"
close(2)
end program exampl
Then another program (written in Python in this case) reads that source
import os
f=open('exampl.f90') # read in exampl.f90
g=open('exampl_source.f90','w') # and replace each line with write(*,*) 'line'
for line in f:
#print 'write(2,*) \''+line.rstrip()+'\'\n',
g.write('write(2,*) \''+line.rstrip()+'\'\n')
f.close
g.close
# then complie exampl.f90 (which includes exampl_source.f90)
os.system('gfortran exampl.f90')
os.system('/bin/rm exampl_source.f90')
Running this python script produces an executable. When the executable is run, it performs the original task AND prints the source code.