问题
I have a FORTRAN program written for the parallel computing. The program takes the arguments and the number of threads can be defined as the argument. The sample code is as follows:
COUNT = NARGS()
NTHREADS = 1
! *** GET THE COMMAND LINE ARGUMENTS, IF ANY
IF(COUNT.GT.1)THEN
! *** ARGUMENT 1
CALL GETARG(1, BUFFER, iStatus)
IF (Buffer(1:4).EQ.'-NOP'.OR.Buffer(1:4).EQ.'-nop') THEN
PAUSEIT=.FALSE.
ENDIF
IF (Buffer(1:3).EQ.'-NT'.OR.Buffer(1:3).EQ.'-nt') THEN
READ(Buffer(4:10),*) NTHREADS
ENDIF
IF(COUNT.GT.2)THEN
! *** ARGUMENT 2
CALL GETARG(2, BUFFER, iStatus)
IF (Buffer(1:4).EQ.'-NOP'.OR.Buffer(1:4).EQ.'-nop') THEN
PAUSEIT=.FALSE.
ENDIF
IF (Buffer(1:3).EQ.'-NT'.OR.Buffer(1:3).EQ.'-nt') THEN
READ(Buffer(4:10),*) NTHREADS
ENDIF
ENDIF
ENDIF
Let's say my compiled file name is "hellofortran". I can define the number of threads as
./hellofortran -nt4
My program will read the program with 4 threads. The problem is that I can run with as many cores in any computer. Lets say I have dual core processor. I have only two cores but I can still run with 6-8 threads or any number. How can I properly define the number of threads in this particular instance ?
I hope I explained my problem. Looking forward to hearing soon on how can I improve my program. Thanks.
Jdbaba
回答1:
If you're using OpenMP and just looking to set up how many threads to use, I would just specify the number of threads in the environment:
OMP_NUM_THREADS=4
./hellofortran
and write your OpenMP code as you would normally. There are programmatic ways of setting thread counts but this is likely more straightforward for you.
来源:https://stackoverflow.com/questions/14637538/is-this-the-proper-way-to-define-number-of-threads-in-an-fortran-program