1818#include < adolc/revolve.h>
1919#include < adolc/adalloc.h>
2020
21+ #include < mutex>
22+
2123#ifdef ADOLC_MEDIPACK_SUPPORT
2224#include " medipacksupport_p.h"
2325#endif
@@ -341,10 +343,21 @@ vector<TapeInfos *> ADOLC_TAPE_INFOS_BUFFER_DECL;
341343stack<TapeInfos *> ADOLC_TAPE_STACK_DECL;
342344
343345/* the main tape info buffer and its fallback */
346+ #if defined(_OPENMP)
347+
344348TapeInfos ADOLC_CURRENT_TAPE_INFOS_DECL;
345349TapeInfos ADOLC_CURRENT_TAPE_INFOS_FALLBACK_DECL;
346350
347- /* global taping variables */
351+ #else
352+
353+ std::mutex tape_mutex;
354+
355+ thread_local TapeInfos ADOLC_CURRENT_TAPE_INFOS_DECL;
356+ thread_local TapeInfos ADOLC_CURRENT_TAPE_INFOS_FALLBACK_DECL;
357+
358+ #endif
359+
360+ /* global tapeing variables */
348361GlobalTapeVars ADOLC_GLOBAL_TAPE_VARS_DECL;
349362
350363#if defined(_OPENMP)
@@ -419,6 +432,10 @@ int initNewTape(short tapeID) {
419432 ADOLC_OPENMP_THREAD_NUMBER;
420433 ADOLC_OPENMP_GET_THREAD_NUMBER;
421434
435+ #if !defined(_OPENMP)
436+ std::lock_guard<std::mutex> tape_lock (tape_mutex);
437+ #endif
438+
422439 /* check if tape is in use */
423440 vector<TapeInfos *>::iterator tiIter;
424441 if (!ADOLC_TAPE_INFOS_BUFFER.empty ()) {
@@ -537,6 +554,10 @@ void openTape(short tapeID, char mode) {
537554 ADOLC_OPENMP_THREAD_NUMBER;
538555 ADOLC_OPENMP_GET_THREAD_NUMBER;
539556
557+ #if !defined(_OPENMP)
558+ std::lock_guard<std::mutex> tape_lock (tape_mutex);
559+ #endif
560+
540561 /* check if tape information exist in memory */
541562 vector<TapeInfos *>::iterator tiIter;
542563 if (!ADOLC_TAPE_INFOS_BUFFER.empty ()) {
@@ -617,6 +638,10 @@ void releaseTape() {
617638 ADOLC_CURRENT_TAPE_INFOS.inUse = 0 ;
618639 }
619640
641+ #if !defined(_OPENMP)
642+ std::lock_guard<std::mutex> tape_lock (tape_mutex);
643+ #endif
644+
620645 ADOLC_GLOBAL_TAPE_VARS.currentTapeInfosPtr ->copy (
621646 ADOLC_CURRENT_TAPE_INFOS);
622647 ADOLC_GLOBAL_TAPE_VARS.currentTapeInfosPtr = ADOLC_TAPE_STACK.top ();
@@ -636,6 +661,10 @@ TapeInfos *getTapeInfos(short tapeID) {
636661 ADOLC_OPENMP_THREAD_NUMBER;
637662 ADOLC_OPENMP_GET_THREAD_NUMBER;
638663
664+ #if !defined(_OPENMP)
665+ std::lock_guard<std::mutex> tape_lock (tape_mutex);
666+ #endif
667+
639668 /* check if TapeInfos for tapeID exist */
640669 if (!ADOLC_TAPE_INFOS_BUFFER.empty ()) {
641670 for (tiIter=ADOLC_TAPE_INFOS_BUFFER.begin ();
@@ -680,6 +709,10 @@ void cachedTraceTags(std::vector<short>& result) {
680709 ADOLC_OPENMP_THREAD_NUMBER;
681710 ADOLC_OPENMP_GET_THREAD_NUMBER;
682711
712+ #if !defined(_OPENMP)
713+ std::lock_guard<std::mutex> tape_lock (tape_mutex);
714+ #endif
715+
683716 result.resize (ADOLC_TAPE_INFOS_BUFFER.size ());
684717 if (!ADOLC_TAPE_INFOS_BUFFER.empty ()) {
685718 for (tiIter=ADOLC_TAPE_INFOS_BUFFER.begin (), tIdIter=result.begin ();
@@ -699,6 +732,10 @@ void setTapeInfoJacSparse(short tapeID, SparseJacInfos sJinfos) {
699732 ADOLC_OPENMP_THREAD_NUMBER;
700733 ADOLC_OPENMP_GET_THREAD_NUMBER;
701734
735+ #if !defined(_OPENMP)
736+ std::lock_guard<std::mutex> tape_lock (tape_mutex);
737+ #endif
738+
702739 /* check if TapeInfos for tapeID exist */
703740 if (!ADOLC_TAPE_INFOS_BUFFER.empty ()) {
704741 for (tiIter=ADOLC_TAPE_INFOS_BUFFER.begin ();
@@ -740,6 +777,10 @@ void setTapeInfoHessSparse(short tapeID, SparseHessInfos sHinfos) {
740777 ADOLC_OPENMP_THREAD_NUMBER;
741778 ADOLC_OPENMP_GET_THREAD_NUMBER;
742779
780+ #if !defined(_OPENMP)
781+ std::lock_guard<std::mutex> tape_lock (tape_mutex);
782+ #endif
783+
743784 /* check if TapeInfos for tapeID exist */
744785 if (!ADOLC_TAPE_INFOS_BUFFER.empty ()) {
745786 for (tiIter=ADOLC_TAPE_INFOS_BUFFER.begin ();
@@ -830,6 +871,10 @@ void cleanUp() {
830871 ADOLC_OPENMP_THREAD_NUMBER;
831872 ADOLC_OPENMP_GET_THREAD_NUMBER;
832873
874+ #if !defined(_OPENMP)
875+ std::lock_guard<std::mutex> tape_lock (tape_mutex);
876+ #endif
877+
833878 TapeInfos** tiIter;
834879 clearCurrentTape ();
835880 while (!ADOLC_TAPE_INFOS_BUFFER.empty ()) {
@@ -981,20 +1026,29 @@ int removeTape(short tapeID, short type) {
9811026 ADOLC_OPENMP_THREAD_NUMBER;
9821027 ADOLC_OPENMP_GET_THREAD_NUMBER;
9831028
984- /* check if TapeInfos for tapeID exist */
985- if (!ADOLC_TAPE_INFOS_BUFFER.empty ()) {
1029+ #if !defined(_OPENMP)
1030+ {
1031+ std::lock_guard<std::mutex> tape_lock (tape_mutex);
1032+ #endif
1033+
1034+ /* check if TapeInfos for tapeID exist */
1035+ if (!ADOLC_TAPE_INFOS_BUFFER.empty ()) {
9861036 for (tiIter = ADOLC_TAPE_INFOS_BUFFER.begin ();
987- tiIter != ADOLC_TAPE_INFOS_BUFFER.end ();
988- ++tiIter)
1037+ tiIter != ADOLC_TAPE_INFOS_BUFFER.end ();
1038+ ++tiIter)
9891039 {
990- if ((*tiIter)->tapeID == tapeID) {
991- tapeInfos = *tiIter;
992- if (tapeInfos->tapingComplete == 0 ) return -1 ;
993- ADOLC_TAPE_INFOS_BUFFER.erase (tiIter);
994- break ;
995- }
1040+ if ((*tiIter)->tapeID == tapeID) {
1041+ tapeInfos = *tiIter;
1042+ if (tapeInfos->tapingComplete == 0 ) return -1 ;
1043+ ADOLC_TAPE_INFOS_BUFFER.erase (tiIter);
1044+ break ;
1045+ }
9961046 }
1047+ }
1048+
1049+ #if !defined(_OPENMP)
9971050 }
1051+ #endif
9981052
9991053 if (tapeInfos == NULL ) { // might be on disk only
10001054 tapeInfos = new TapeInfos (tapeID);
0 commit comments