Skip to content

Commit 0dfb6e7

Browse files
author
Christoph Hansknecht
committed
Added initial synchronization code
1 parent 0d999e0 commit 0dfb6e7

File tree

2 files changed

+75
-15
lines changed

2 files changed

+75
-15
lines changed

ADOL-C/src/tape_handling.cpp

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
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;
341343
stack<TapeInfos *> ADOLC_TAPE_STACK_DECL;
342344

343345
/* the main tape info buffer and its fallback */
346+
#if defined(_OPENMP)
347+
344348
TapeInfos ADOLC_CURRENT_TAPE_INFOS_DECL;
345349
TapeInfos 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 */
348361
GlobalTapeVars 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);

ADOL-C/src/taping_p.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#ifdef __cplusplus
1818
#include "storemanager.h"
19+
#else
20+
#include <threads.h>
1921
#endif
2022
#include <adolc/internal/common.h>
2123
#include <adolc/taping.h>
@@ -378,6 +380,10 @@ extern int isParallel();
378380
#define ADOLC_CHECKPOINTS_STACK ADOLC_checkpointsStack[ADOLC_threadNumber]
379381
#define REVOLVE_NUMBERS revolve_numbers[ADOLC_threadNumber]
380382

383+
extern TapeInfos ADOLC_CURRENT_TAPE_INFOS_DECL;
384+
extern TapeInfos ADOLC_CURRENT_TAPE_INFOS_FALLBACK_DECL;
385+
extern GlobalTapeVars ADOLC_GLOBAL_TAPE_VARS_DECL;
386+
381387
#else
382388

383389
#define ADOLC_TAPE_INFOS_BUFFER_DECL tapeInfosBuffer
@@ -401,12 +407,12 @@ extern int isParallel();
401407
#define ADOLC_CHECKPOINTS_STACK ADOLC_checkpointsStack
402408
#define REVOLVE_NUMBERS revolve_numbers
403409

404-
#endif /* _OPENMP */
405-
406-
extern TapeInfos ADOLC_CURRENT_TAPE_INFOS_DECL;
407-
extern TapeInfos ADOLC_CURRENT_TAPE_INFOS_FALLBACK_DECL;
410+
extern thread_local TapeInfos ADOLC_CURRENT_TAPE_INFOS_DECL;
411+
extern thread_local TapeInfos ADOLC_CURRENT_TAPE_INFOS_FALLBACK_DECL;
408412
extern GlobalTapeVars ADOLC_GLOBAL_TAPE_VARS_DECL;
409413

414+
#endif /* _OPENMP */
415+
410416
/****************************************************************************/
411417
/* C Function interfaces */
412418
/****************************************************************************/

0 commit comments

Comments
 (0)