Run the same executable under different directories

General OpenMP discussion

Run the same executable under different directories

Postby UYNIX » Thu Apr 26, 2012 1:18 pm

My first experiment is to run a model, I call it mymodel.exe, under multiple directories. Each directory contains a different set of input files and the executable.

I wrote the following code. It runs in the first round. The second time I run the code, it shows
"The process cannot access the file because it is being used by another process". I guess I am doing something fundementally wrong here. Can someone please give a clue?

Thanks,
Cindy

!--------------------------------------------
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 ompHelloWorld

!Run a model
SUBROUTINE RUNMODEL(ID)
INTEGER ID
CHARACTER*200 CPATH
write(*,*) 'Run model using thread#',ID
! Change current path
WRITE(CPATH,100) ID+1
100 FORMAT(I2)
CPATH='c:\share\test'//ADJUSTL(CPATH)
WRITE(*,*) TRIM(CPATH)
CALL CHDIR(CPATH)
WRITE(*,*) 'The current path is ', TRIM(CPATH)
CALL SYSTEM('MYMODEL.EXE >lst')
END SUBROUTINE RUNMODEL
UYNIX
 
Posts: 8
Joined: Thu Apr 26, 2012 11:58 am

Re: Run the same executable under different directories

Postby UYNIX » Wed May 02, 2012 12:43 pm

I just found something related to my problem, this is about perl but I guess it is the same.

http://search.cpan.org/~jdhedden/thread ... threads.pm

Current working directory
On all platforms except MSWin32, the setting for the current working directory is shared among all threads such that changing it in one thread (e.g., using chdir()) will affect all the threads in the application.
On MSWin32, each thread maintains its own the current working directory setting.
Environment variables
Currently, on all platforms except MSWin32, all system calls (e.g., using system() or back-ticks) made from threads use the environment variable settings from the main thread. In other words, changes made to %ENV in a thread will not be visible in system calls made by that thread.
To work around this, set environment variables as part of the system call. For example:
my $msg = 'hello';
system("FOO=$msg; echo \$FOO"); # Outputs 'hello' to STDOUT
On MSWin32, each thread maintains its own set of environment variables.
UYNIX
 
Posts: 8
Joined: Thu Apr 26, 2012 11:58 am

Re: Run the same executable under different directories

Postby UYNIX » Wed May 02, 2012 12:46 pm

So it appears my way did not work out. Anybody has any good idea? basically, I need to run a third-party executable multiple times, each run with a different set of input files, then extract information from the output files. Each run stays under its own folder. This is very common in Monte Carlo analysis.
UYNIX
 
Posts: 8
Joined: Thu Apr 26, 2012 11:58 am

Re: Run the same executable under different directories

Postby ftinetti » Wed May 02, 2012 3:02 pm

Hi

A not-so-elegant workaround: add a .bat in the current directory, just like this

>type cdandrun.bat
cd %1
MYMODEL.EXE > lst

and, in the program,

Code: Select all
program ompHelloWorld
  use omp_lib
  implicit none
  integer NTHREADS, TID

  !$OMP PARALLEL PRIVATE(TID) SHARED(NTHREADS) NUM_THREADS(2)
  TID = OMP_GET_THREAD_NUM()
  PRINT *, 'Hello World from thread = ', TID
  CALL RUNMODEL(TID)
  !$OMP END PARALLEL
end program ompHelloWorld


!-------------------------------------------------------------------
!Run a model
SUBROUTINE RUNMODEL(ID)
  implicit none
  INTEGER ID
  CHARACTER*200 CPATH

  write(*,*) 'Run model using thread#',ID
  ! Change current path
  WRITE(CPATH,100) ID+1
100 FORMAT(I2)
!  CPATH='c:\share\test'//ADJUSTL(CPATH)
  CPATH='cdandrun.bat dir'//ADJUSTL(CPATH)

  WRITE(*,*) TRIM(CPATH)
!  CALL CHDIR(CPATH)
!  WRITE(*,*) 'The current path is ', TRIM(CPATH)
  CALL SYSTEM(CPATH)
END SUBROUTINE RUNMODEL


I've used NUM_THREADS(2) just to experiment with the minimum number of directories... and the output is

Hello World from thread = 0
Hello World from thread = 1
Run model using thread# 1
cdandrun.bat dir2
Run model using thread# 0
cdandrun.bat dir1

(lines do not appear always in the same order...)

HTH,

Fernando.
ftinetti
 
Posts: 581
Joined: Wed Feb 10, 2010 2:44 pm

Re: Run the same executable under different directories

Postby UYNIX » Mon May 07, 2012 11:56 am

Thank you Fernando, it worked! But as you have said, this is not a so elegant way. I am still puzzled as to why CHDIR() does not work, but CD in .bat file worked. I thought they are just equivalent.
UYNIX
 
Posts: 8
Joined: Thu Apr 26, 2012 11:58 am

Re: Run the same executable under different directories

Postby ftinetti » Tue May 08, 2012 2:05 am

I am still puzzled as to why CHDIR() does not work, but CD in .bat file worked. I thought they are just equivalent.

Well, I do not have much (well... any) experience in Windows environments, so I did not expect it worked, since the current working directory is a "property" of a process, not a thread property in UNIX/Linux/(any-but-Windows?) OS. As such, changing the current working directory in a thread will affect every other thread current working directory. As a matter of fact I found (very) surprising
On MSWin32, each thread maintains its own the current working directory setting.


And it's good to know things work for you, even when not-so-elegant :)

Fernando.
ftinetti
 
Posts: 581
Joined: Wed Feb 10, 2010 2:44 pm


Return to Using OpenMP

Who is online

Users browsing this forum: Google [Bot] and 6 guests