Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Commit 6d4f4a7

Browse files
author
Nicolas Cornu
authored
make nrn_multithread_job accept arguments (#260)
* reduce global variable for keeping state * keep openmp version that can be used in serial/multi-threaded case
1 parent aaf9d59 commit 6d4f4a7

File tree

3 files changed

+23
-31
lines changed

3 files changed

+23
-31
lines changed

coreneuron/sim/fadvance_core.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
2626
THE POSSIBILITY OF SUCH DAMAGE.
2727
*/
2828

29+
#include <functional>
30+
2931
#include "coreneuron/coreneuron.hpp"
3032
#include "coreneuron/nrnconf.h"
3133
#include "coreneuron/sim/multicore.hpp"
@@ -43,7 +45,7 @@ THE POSSIBILITY OF SUCH DAMAGE.
4345
namespace coreneuron {
4446

4547
static void* nrn_fixed_step_thread(NrnThread*);
46-
static void* nrn_fixed_step_group_thread(NrnThread*);
48+
static void* nrn_fixed_step_group_thread(NrnThread*, int, int, int&);
4749

4850
void dt2thread(double adt) { /* copied from nrnoc/fadvance.c */
4951
if (adt != nrn_threads[0]._dt) {
@@ -84,9 +86,6 @@ void nrn_fixed_step_minimal() { /* not so minimal anymore with gap junctions */
8486
integration interval before joining
8587
*/
8688
/// --> Coreneuron
87-
static int step_group_n;
88-
static int step_group_begin;
89-
static int step_group_end;
9089
static progressbar* progress;
9190

9291
void initialize_progress_bar(int nstep) {
@@ -137,13 +136,13 @@ void nrn_fixed_step_group_minimal(int total_sim_steps) {
137136
static int current_steps = 0;
138137
dt2thread(dt);
139138
nrn_thread_table_check();
140-
step_group_n = total_sim_steps;
141-
step_group_begin = 0;
142-
step_group_end = 0;
139+
int step_group_n = total_sim_steps;
140+
int step_group_begin = 0;
141+
int step_group_end = 0;
143142
initialize_progress_bar(step_group_n);
144143

145144
while (step_group_end < step_group_n) {
146-
nrn_multithread_job(nrn_fixed_step_group_thread);
145+
nrn_multithread_job(nrn_fixed_step_group_thread, step_group_n, step_group_begin, step_group_end);
147146
#if NRNMPI
148147
nrn_spike_exchange(nrn_threads);
149148
#endif
@@ -162,9 +161,9 @@ void nrn_fixed_step_group_minimal(int total_sim_steps) {
162161
finalize_progress_bar();
163162
}
164163

165-
static void* nrn_fixed_step_group_thread(NrnThread* nth) {
164+
static void* nrn_fixed_step_group_thread(NrnThread* nth, int step_group_max, int step_group_begin, int& step_group_end) {
166165
nth->_stop_stepping = 0;
167-
for (int i = step_group_begin; i < step_group_n; ++i) {
166+
for (int i = step_group_begin; i < step_group_max; ++i) {
168167
nrn_fixed_step_thread(nth);
169168
if (nth->_stop_stepping) {
170169
if (nth->id == 0) {
@@ -175,7 +174,7 @@ static void* nrn_fixed_step_group_thread(NrnThread* nth) {
175174
}
176175
}
177176
if (nth->id == 0) {
178-
step_group_end = step_group_n;
177+
step_group_end = step_group_max;
179178
}
180179
return nullptr;
181180
}

coreneuron/sim/multicore.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ THE POSSIBILITY OF SUCH DAMAGE.
3131

3232
#include "coreneuron/nrnconf.h"
3333
#include "coreneuron/sim/multicore.hpp"
34-
#include "coreneuron/mpi/nrnmpi.h"
3534
#include "coreneuron/utils/memory.h"
3635
#include "coreneuron/coreneuron.hpp"
3736
#include "coreneuron/utils/nrnoc_aux.hpp"
@@ -157,22 +156,4 @@ void nrn_thread_table_check() {
157156
ml->_thread, &nt, tml->index);
158157
}
159158
}
160-
161-
void nrn_multithread_job(void* (*job)(NrnThread*)) {
162-
int i;
163-
#if defined(_OPENMP)
164-
// clang-format off
165-
#pragma omp parallel for private(i) shared(nrn_threads, job, nrn_nthread, \
166-
nrnmpi_myid) schedule(static, 1)
167-
for (i = 0; i < nrn_nthread; ++i) {
168-
(*job)(nrn_threads + i);
169-
}
170-
// clang-format on
171-
#else
172-
for (i = 1; i < nrn_nthread; ++i) {
173-
(*job)(nrn_threads + i);
174-
}
175-
(*job)(nrn_threads);
176-
#endif
177-
}
178159
} // namespace coreneuron

coreneuron/sim/multicore.hpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ THE POSSIBILITY OF SUCH DAMAGE.
3131
#include "coreneuron/nrnconf.h"
3232
#include "coreneuron/mechanism/membfunc.hpp"
3333
#include "coreneuron/utils/memory.h"
34+
#include "coreneuron/mpi/nrnmpi.h"
3435

3536
namespace coreneuron {
3637
class NetCon;
@@ -154,7 +155,18 @@ struct NrnThread : public MemoryManaged {
154155
extern void nrn_threads_create(int n);
155156
extern int nrn_nthread;
156157
extern NrnThread* nrn_threads;
157-
extern void nrn_multithread_job(void* (*)(NrnThread*));
158+
template<typename F, typename... Args>
159+
void nrn_multithread_job(F&& job, Args&&... args) {
160+
int i;
161+
// clang-format off
162+
#pragma omp parallel for private(i) shared(nrn_threads, job, nrn_nthread, \
163+
nrnmpi_myid) schedule(static, 1)
164+
for (i = 0; i < nrn_nthread; ++i) {
165+
job(nrn_threads + i, std::forward<Args>(args)...);
166+
}
167+
// clang-format on
168+
}
169+
158170
extern void nrn_thread_table_check(void);
159171

160172
extern void nrn_threads_free(void);

0 commit comments

Comments
 (0)