A call to the GET_NTHREAD utility subroutine returns either the value of NTHREADS in the PREFERENCES statement, or the value one (1).
If the returned value is one (1), it means the user did not set the NTHREAD option in the PREFERENCES statement (the code will therefore run serially.)
If the environment variable MDI_ADAMS_FORCE_NTHREAD is set, then GET_NTHREAD will return the value of the environment variable. (The environment variable MDI_ADAMS_FORCE_NTHREAD overrides the option in the PREFERENCES statement.)
The GET_NTHREAD utility subroutine may be used from within any user-written subroutine. However, if the call is made from within a CBKSUB, the returned value will be equal to one (1) during the event ev_MODEL_INPUT_BEG. The reason is that the event ev_MODEL_INPUT_BEG signals the start of parsing the model data from the database, hence no information about threading is available at that time. The correct value of NTHREADS (or the value of the environment variable above) will be returned at any other event.
Use
Called By
Any user-written subroutine. Calls from within a CBKSUB shall not be made at event ev_MODEL_INPUT_BEG.
Calling Sequence in FORTRAN
INTEGER N
CALL GET_NTHREAD(N)
Function Prototype (C/C++)
void c_get_nthread( int *n);
Input Arguments
None
Output Arguments
N (or n) | An INTEGER (or a pointer to int) that returns the value of the NTHREAD option in the PREFERENCES statement. If the environment variable MDI_ADAMS_FORCE_NTHREAD is set, the utility will return the value of the environment variable. |
Extended Definition
Calls to GET_NTHREAD may allow the user to write a more efficient parallel code or may allow parallelizing existing serial code. The typical scenario is the case of initializations for user-written subroutines running in parallel. Those initializations can be safely done within a CBKSUB during the event ev_MODEL_INPUT_END.
Note: | Avoid making a call to GET_NTHREAD from a CBKSUB during the event ev_MODEL_INPUT_BEG. |
Example 1 (FORTRAN)
SUBROUTINE CBKSUB(TIME, EVENT, MODE)
C
include 'slv_cbksub.inc'
C Callback subroutine
INTEGER EVENT, MODE(3)
DOUBLE PRECISION TIME
C
C Work variables
INTEGER NTHREADS
CHARACTER*200 MESSAGE
C
C Case of event ev_MODEL_INPUT_END
IF( EVENT.EQ.ev_MODEL_INPUT_END ) THEN
CALL GET_NTHREAD(NTHREADS)
WRITE(MESSAGE, '(A,I5)') 'NTHREADS is ', NTHREADS
CALL USRMES(.TRUE.,MESSAGE, 0, 'INFO')
C Initializations
….
ENDIF
C
…
Example 2 (C/C++)
void Cbksub(const struct sAdamsCbksub *sos, double Time, int Event, int *Info)
{
char buffer[1024];
int nthreads;
if (Event == ev_MODEL_INPUT_END)
{
/* Get the number of threads */
c_get_nthread(&nthreads);
sprintf(buffer, "\n\nThe number of threads read is %d\n", nthreads);
c_usrmes(1, buffer, 0, "INFO");
/* Initialize data for each thread */
....
}
....