Goal
Allowing the user program to initialize and finalize SHMEM multiple times in order to support the scenario where SHMEM is used as the communication runtime of other libraries.
Problem Description
Current SHMEM spec defines that (1) multiple calls to shmem_init|shmem_init_thread within a program result in undefined behavior, and (2)shmem_finalize must be the last OpenSHMEM library call encountered in the OpenSHMEM portion of a program.
The above semantics allows the program to have at most one SHMEM portion, and interleaving calls to init or finalize become illegal. Thus, SHMEM is not able to be used as the communication runtime for multiple libraries. The following two examples show typical usages of init/finalize in this scenario, both of them are prohibited in current spec (FOO and BAR can be either a library or the main program).
Example-1
FOO_init(); // internally calls shmem_init
BAR_init(); // internally calls shmem_init
/* computation */
FOO_finalize(); // internally calls shmem_finalize
BAR_finalize(); // internally calls shmem_finalize
Example-2
FOO_init(); // internally calls shmem_init
/* computation 1 */
FOO_finalize(); // internally calls shmem_finalize
BAR_init(); // internally calls shmem_init
/* computation 2 */
BAR_finalize(); // internally calls shmem_finalize
Proposed Solution
Change in specification
- Multiple OpenSHMEM portions can exist within a program. An OpenSHMEM portion begins with a call to
shmem_init|shmem_init_thread call, and ends with a call toshmem_finalize.
- The first
shmem_init|shmem_init_thread call of an OpenSHMEM portion allocates and initializes resources for OpenSHMEM communication; the last shmem_finalize call of the same portion releases all resources initialized in this portion.
- Calling
shmem_init|shmem_init_thread more than once within a program is permitted. However, calling init, init, finalize, finalize in a program will only initialize a single OpenSHMEM portion, even if the calls are made by different threads.
- In an OpenSHMEM portion, a subsequent call to
shmem_init_thread|shmem_init has no subsequent effect. The thread level cannot be changed after initialization.
- The
provided parameter of shmem_init_thread|shmem_query_thread returns the thread level initialized in the current OpenSHMEM portion.
Possible implementation
- The OpenSHMEM runtime may maintain an internal
refcount global variable, which is increased at every shmem_init|shmem_init_thread call, and decreased at every shmem_finalize call.
- The resource is allocated and initialization at a
shmem_init|shmem_init_thread call only if refcount==0; the resource is released at a shmem_finalize call only if refcount==0.
Requirement to user
- The user of OpenSHMEM is responsible to ensure that the same amount of init and finalize calls are made within a program. For instance, calling
init, init, finalize may cause unreleased resource in an OpenSHMEM portion, and subsequent calls to OpenSHMEM (except the call to shmem_finalize) result in undefined behavior.
Current Progress
- Implementing the proposed solution in an OpenSHMEM implementation.
- Preparing changed specification.
This issue is separated from ticket #243 . See past discussion at #243.
Goal
Allowing the user program to initialize and finalize SHMEM multiple times in order to support the scenario where SHMEM is used as the communication runtime of other libraries.
Problem Description
Current SHMEM spec defines that (1) multiple calls to
shmem_init|shmem_init_threadwithin a program result in undefined behavior, and (2)shmem_finalizemust be the last OpenSHMEM library call encountered in the OpenSHMEM portion of a program.The above semantics allows the program to have at most one SHMEM portion, and interleaving calls to init or finalize become illegal. Thus, SHMEM is not able to be used as the communication runtime for multiple libraries. The following two examples show typical usages of init/finalize in this scenario, both of them are prohibited in current spec (
FOOandBARcan be either a library or the main program).Example-1
Example-2
Proposed Solution
Change in specification
shmem_init|shmem_init_threadcall, and ends with a call toshmem_finalize.shmem_init|shmem_init_threadcall of an OpenSHMEM portion allocates and initializes resources for OpenSHMEM communication; the lastshmem_finalizecall of the same portion releases all resources initialized in this portion.shmem_init|shmem_init_threadmore than once within a program is permitted. However, callinginit, init, finalize, finalizein a program will only initialize a single OpenSHMEM portion, even if the calls are made by different threads.shmem_init_thread|shmem_inithas no subsequent effect. The thread level cannot be changed after initialization.providedparameter ofshmem_init_thread|shmem_query_threadreturns the thread level initialized in the current OpenSHMEM portion.Possible implementation
refcountglobal variable, which is increased at everyshmem_init|shmem_init_threadcall, and decreased at everyshmem_finalizecall.shmem_init|shmem_init_threadcall only ifrefcount==0; the resource is released at ashmem_finalizecall only ifrefcount==0.Requirement to user
init, init, finalizemay cause unreleased resource in an OpenSHMEM portion, and subsequent calls to OpenSHMEM (except the call toshmem_finalize) result in undefined behavior.Current Progress
This issue is separated from ticket #243 . See past discussion at #243.