Skip to content

Commit 47b438a

Browse files
committed
Add checks for tool == nullptr to *SINGLE* macros.
Formatting.
1 parent 082a179 commit 47b438a

2 files changed

Lines changed: 60 additions & 46 deletions

File tree

include/opdi/backend/macro/macros.hpp

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,43 +97,57 @@
9797
{ \
9898
bool constexpr opdiInternalBarrierIndicator = true; \
9999
bool constexpr opdiInternalBroadcastIndicator = true; \
100-
void* opdiInternalTapePosition1 = opdi::tool->allocPosition(); \
101-
opdi::tool->getTapePosition(opdi::tool->getThreadLocalTape(), opdiInternalTapePosition1); \
100+
void* opdiInternalTapePosition1 = nullptr; \
101+
if (opdi::tool != nullptr) { \
102+
opdiInternalTapePosition1 = opdi::tool->allocPosition(); \
103+
opdi::tool->getTapePosition(opdi::tool->getThreadLocalTape(), opdiInternalTapePosition1); \
104+
} \
102105
/* broadcast-related barrier */ \
103106
opdi::logic->onSyncRegion(opdi::LogicInterface::SyncRegionKind::BarrierImplementation, \
104107
opdi::LogicInterface::ScopeEndpoint::Begin); \
105108
opdi::logic->onSyncRegion(opdi::LogicInterface::SyncRegionKind::BarrierImplementation, \
106109
opdi::LogicInterface::ScopeEndpoint::End); \
107-
void* opdiInternalTapePosition2 = opdi::tool->allocPosition(); \
108-
opdi::tool->getTapePosition(opdi::tool->getThreadLocalTape(), opdiInternalTapePosition2); \
110+
void* opdiInternalTapePosition2 = nullptr; \
111+
if (opdi::tool != nullptr) { \
112+
opdiInternalTapePosition2 = opdi::tool->allocPosition(); \
113+
opdi::tool->getTapePosition(opdi::tool->getThreadLocalTape(), opdiInternalTapePosition2); \
114+
} \
109115
opdi::ImplicitBarrierTools::beginRegionWithImplicitBarrier(); \
110116
{ \
111117
opdi::SingleProbe localSingleProbe; /* worksharing events */ \
112118
OPDI_PRAGMA(omp single __VA_ARGS__) \
113119
{ \
114120
/* delay broadcast-related barrier for executor */ \
115-
opdi::tool->erase(opdi::tool->getThreadLocalTape(), opdiInternalTapePosition1, opdiInternalTapePosition2);
121+
if (opdi::tool != nullptr) \
122+
opdi::tool->erase(opdi::tool->getThreadLocalTape(), opdiInternalTapePosition1, opdiInternalTapePosition2);
116123

117124
#define OPDI_SINGLE_COPYPRIVATE_NOWAIT(...) \
118125
{ \
119126
bool constexpr opdiInternalBarrierIndicator = false; \
120127
bool constexpr opdiInternalBroadcastIndicator = true; \
121-
void* opdiInternalTapePosition1 = opdi::tool->allocPosition(); \
122-
opdi::tool->getTapePosition(opdi::tool->getThreadLocalTape(), opdiInternalTapePosition1); \
128+
void* opdiInternalTapePosition1 = nullptr; \
129+
if (opdi::tool != nullptr) { \
130+
opdiInternalTapePosition1 = opdi::tool->allocPosition(); \
131+
opdi::tool->getTapePosition(opdi::tool->getThreadLocalTape(), opdiInternalTapePosition1); \
132+
} \
123133
/* broadcast-related barrier */ \
124134
opdi::logic->onSyncRegion(opdi::LogicInterface::SyncRegionKind::BarrierImplementation, \
125135
opdi::LogicInterface::ScopeEndpoint::Begin); \
126136
opdi::logic->onSyncRegion(opdi::LogicInterface::SyncRegionKind::BarrierImplementation, \
127137
opdi::LogicInterface::ScopeEndpoint::End); \
128-
void* opdiInternalTapePosition2 = opdi::tool->allocPosition(); \
129-
opdi::tool->getTapePosition(opdi::tool->getThreadLocalTape(), opdiInternalTapePosition2); \
138+
void* opdiInternalTapePosition2 = nullptr; \
139+
if (opdi::tool != nullptr) { \
140+
opdiInternalTapePosition2 = opdi::tool->allocPosition(); \
141+
opdi::tool->getTapePosition(opdi::tool->getThreadLocalTape(), opdiInternalTapePosition2); \
142+
} \
130143
opdi::ImplicitBarrierTools::beginRegionWithImplicitBarrier(); \
131144
{ \
132145
opdi::SingleProbe localSingleProbe; \
133146
OPDI_PRAGMA(omp single nowait __VA_ARGS__) \
134147
{ \
135148
/* delay broadcast-related barrier for executor */ \
136-
opdi::tool->erase(opdi::tool->getThreadLocalTape(), opdiInternalTapePosition1, opdiInternalTapePosition2);
149+
if (opdi::tool != nullptr) \
150+
opdi::tool->erase(opdi::tool->getThreadLocalTape(), opdiInternalTapePosition1, opdiInternalTapePosition2);
137151

138152
#define OPDI_END_SINGLE \
139153
/* broadcast-related barrier */ \
@@ -145,7 +159,7 @@
145159
} \
146160
} \
147161
} \
148-
if (opdiInternalBroadcastIndicator) { \
162+
if (opdi::tool != nullptr && opdiInternalBroadcastIndicator) { \
149163
opdi::tool->freePosition(opdiInternalTapePosition1); \
150164
opdi::tool->freePosition(opdiInternalTapePosition2); \
151165
} \

tests/drivers/DriverFirstOrderForward.hpp

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,24 @@ struct DriverFirstOrderForward : public DriverBase<DriverFirstOrderForward<_Case
6363

6464
static void run() {
6565

66-
#ifndef BUILD_REFERENCE
67-
#ifdef OPDI_USE_MACRO_BACKEND
68-
opdi::backend = new opdi::MacroBackend();
69-
opdi::backend->init();
70-
#else
71-
if (omp_get_num_threads() /* trigger OMPT initialization */ && opdi::backend == nullptr) {
72-
std::cout << "Could not initialize OMPT backend. Please check OMPT support." << std::endl;
73-
exit(1);
74-
}
75-
#endif
76-
#ifdef OUTPUT_INSTRUMENT
77-
opdi::ompLogicInstruments.push_back(new opdi::OmpLogicOutputInstrument);
78-
#endif
79-
opdi::logic = new opdi::OmpLogic;
80-
opdi::logic->init();
81-
opdi::tool = new opdi::EmptyTool;
82-
opdi::tool->init();
83-
#endif
66+
#ifndef BUILD_REFERENCE
67+
#ifdef OPDI_USE_MACRO_BACKEND
68+
opdi::backend = new opdi::MacroBackend();
69+
opdi::backend->init();
70+
#else
71+
if (omp_get_num_threads() /* trigger OMPT initialization */ && opdi::backend == nullptr) {
72+
std::cout << "Could not initialize OMPT backend. Please check OMPT support." << std::endl;
73+
exit(1);
74+
}
75+
#endif
76+
#ifdef OUTPUT_INSTRUMENT
77+
opdi::ompLogicInstruments.push_back(new opdi::OmpLogicOutputInstrument);
78+
#endif
79+
opdi::logic = new opdi::OmpLogic;
80+
opdi::logic->init();
81+
opdi::tool = new opdi::EmptyTool; // EmptyTool effectively deactivates OpDiLib
82+
opdi::tool->init();
83+
#endif
8484

8585
std::array<std::array<TestReal, Case::nIn>, Case::nPoints> inputs = Case::template genPoints<TestReal>();
8686

@@ -102,9 +102,9 @@ struct DriverFirstOrderForward : public DriverBase<DriverFirstOrderForward<_Case
102102

103103
inputs[p][i].setGradient(0.0);
104104

105-
#ifndef BUILD_REFERENCE
106-
opdi::logic->reset();
107-
#endif
105+
#ifndef BUILD_REFERENCE
106+
opdi::logic->reset();
107+
#endif
108108
}
109109

110110
std::cout << "Point " << p << " :" << std::endl;
@@ -118,20 +118,20 @@ struct DriverFirstOrderForward : public DriverBase<DriverFirstOrderForward<_Case
118118
}
119119
}
120120

121-
#ifndef BUILD_REFERENCE
122-
opdi::tool->finalize();
123-
opdi::logic->finalize();
124-
#ifdef OUTPUT_INSTRUMENT
125-
delete opdi::ompLogicInstruments.front();
126-
opdi::ompLogicInstruments.clear();
127-
#endif
128-
opdi::backend->finalize();
129-
#ifdef OPDI_USE_MACRO_BACKEND
130-
delete opdi::backend;
131-
#endif
132-
delete opdi::tool;
133-
delete opdi::logic;
134-
#endif
121+
#ifndef BUILD_REFERENCE
122+
opdi::tool->finalize();
123+
opdi::logic->finalize();
124+
#ifdef OUTPUT_INSTRUMENT
125+
delete opdi::ompLogicInstruments.front();
126+
opdi::ompLogicInstruments.clear();
127+
#endif
128+
opdi::backend->finalize();
129+
#ifdef OPDI_USE_MACRO_BACKEND
130+
delete opdi::backend;
131+
#endif
132+
delete opdi::tool;
133+
delete opdi::logic;
134+
#endif
135135
}
136136
};
137137

0 commit comments

Comments
 (0)