A call to the GET_THREAD_INDEX utility subroutine returns the index of the parallel thread currently making the call, or zero when the main thread executes the call.
The returned value may be zero (0) or a value in the range 1 to NTHREADS, where NTHREADS is the value set in the PREFERENCES statement. When the returned value is zero (0) it means the main thread (the main process) executes the call. If the value of NTHREADS is bigger than 1, and the code making the call to this utility is being executed in parallel, the returned value is in the range 1 to NTHREADS.
Notice that even in case when the value of NTHREADS is bigger than 1, the call to this utility subroutine may return zero (0). For example, placing the call to GET_NTHREAD_INDEX inside a VARSUB, the call may return zero (0) during the mapping of dependencies because the main execution thread does the mapping of dependencies serially.
Also, notice that a value of NTHREADS=1 means no parallel threads are used in the Adams Solver (C++) simulation. Parallel threads are used only if the value of NTHREADS is bigger than 1. When NTHREADS is set to 1, GET_THREAD_INDEX will always return zero (0).
Use
Called By
Any user-written subroutine.
Calling Sequence in FORTRAN
INTEGER N
CALL GET_THREAD_INDEX(N)
Function Prototype (C/C++)
void c_get_thread_index( int *n);
Input Arguments
None
Output Arguments
N (or n) | An INTEGER (or a pointer to int) that returns the index of the thread making the call. If the returned value is zero (0), it means the call is made by the main thread (main process). Otherwise the index is in the range 1 to NTHREADS (NTHREADS is the value set in the PREFERENCES statement.) |
Extended Definition
Calls to GET_THREAD_INDEX may allow the user to write a more efficient parallel code or may allow parallelizing existing serial code. The returned value is zero (0) whenever the main execution thread (main process) makes the call; this can happen for operations done by the main thread, for example, mapping dependencies. If a parallel thread makes the call to GET_THREAD_INDEX, the returned value is in the range 1 to NTHREADS, where NTHREADS is the value set in the PREFERENCES statement.
Note: | Make sure your subroutine makes the call to the utility subroutine ADAMS_DECLARE_THREADSAFE or c_adams_declare_threadsafe() to have it running in parallel. |
Example 1 (FORTRAN)
SUBROUTINE VARSUB(ID, TIME, PAR, NPAR, DFLAG, INITFL, VALUE)
C
INTEGER ID, I, TINDEX
INTEGER NPAR, NSIZE, IPAR(4), NSTATES
DOUBLE PRECISION TIME
DOUBLE PRECISION PAR(*)
DOUBLE PRECISION VALUE
LOGICAL INITFL
LOGICAL DFLAG
DOUBLE PRECISION STATE, STATES(6)
LOGICAL ERROR
CHARACTER*10 MID, MINDEX
C Make sure this subroutine runs in parallel
CALL ADAMS_DECLARE_THREADSAFE()
C Print the ID of the VARIABLE into a string
WRITE(MID, 20) ID
20 FORMAT(I4)
C Get the thread index and print it into a string
CALL GET_THREAD_INDEX(TINDEX)
WRITE(MINDEX, 20) TINDEX
C Print the strings above
CALL USRMES(.TRUE., 'VARSUB '//MID//' THREAD '//MINDEX, 0,
& 'INFO')
…
Example 2 (C/C++)
void Varsub(const struct sAdamsVariable* variable, double TIME, int DFLAG, int IFLAG, double* VALUE)
{
double state, states[6];
int i, tindex, nthread, solver;
char buffer[512];
int nsize, ipar[4];
int id, error;
/* ID of the VARIABLE*/
id = variable->ID;
/* Make sure this subroutine runs in parallel */
c_adams_declare_threadsafe();
/* Get the thread index */
c_get_thread_index(&tindex);
/* Print the ID and the thread index */
sprintf(buffer, "VARSUB %5d THREAD %5d", id, tindex);
c_usrmes(1, buffer, id, "INFO");
…