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

Commit 895b2fe

Browse files
iomaganarispramodk
authored andcommitted
Gap junction fix (#213)
* Avoid running the read_phase<gap> if the _fat.dat file hasn't opened * Check in phase gap if the <gid>_gap.dat exists and then try to open the .dat file * Improve output and fix issue with closing file when it doesn't exist * Class member initialisation to avoid garbage values resulting into undefined behaviour
1 parent b34ba52 commit 895b2fe

File tree

5 files changed

+37
-17
lines changed

5 files changed

+37
-17
lines changed

coreneuron/nrniv/nrn_filehandler.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ FileHandler::FileHandler(const char* filename, bool reorder) {
3636
stored_chkpnt = 0;
3737
}
3838

39+
bool FileHandler::file_exist(const char* filename) const {
40+
struct stat buffer;
41+
return (stat(filename, &buffer) == 0);
42+
}
43+
3944
void FileHandler::open(const char* filename, bool reorder, std::ios::openmode mode) {
4045
nrn_assert((mode & (std::ios::in | std::ios::out)));
4146
reorder_bytes = reorder;

coreneuron/nrniv/nrn_filehandler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ THE POSSIBILITY OF SUCH DAMAGE.
3232
#include <iostream>
3333
#include <fstream>
3434
#include <vector>
35+
#include <sys/stat.h>
3536
#include "coreneuron/utils/endianness.h"
3637
#include "coreneuron/utils/swap_endian.h"
3738
#include "coreneuron/nrniv/nrn_assert.h"
@@ -85,6 +86,8 @@ class FileHandler {
8586
return F.fail();
8687
}
8788

89+
bool file_exist(const char* filename) const;
90+
8891
/** nothing more to read */
8992
bool eof();
9093

coreneuron/nrniv/nrn_setup.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,14 @@ inline void* phase_wrapper_w(NrnThread* nt) {
131131
std::string("%s/%d_" + getPhaseName<P>() + ".dat").c_str(),
132132
data_dir, gidgroups_w[i]);
133133

134-
// if no file failed to open or not opened at all
135-
file_reader_w[i].open(fname, byte_swap_w);
134+
// Avoid trying to open the gid_gap.dat file if it doesn't exist when there are no
135+
// gap junctions in this gid
136+
if (P == gap && !file_reader_w[i].file_exist(fname)) {
137+
file_reader_w[i].close();
138+
} else {
139+
// if no file failed to open or not opened at all
140+
file_reader_w[i].open(fname, byte_swap_w);
141+
}
136142
}
137143
read_phase_aux<P>(file_reader_w[i], imult_w[i], *nt);
138144
if (!no_open) {

coreneuron/nrniv/partrans.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ typedef int sgid_t;
1616
#endif
1717

1818
struct HalfGap_Info {
19-
int layout;
20-
int type;
21-
int ix_vpre; /* AoS index for vpre from beginning of a HalfGap instance */
22-
int sz; /* size of a HalfGap instance */
19+
int layout = 0;
20+
int type = 0;
21+
int ix_vpre = 0; /* AoS index for vpre from beginning of a HalfGap instance */
22+
int sz = 0; /* size of a HalfGap instance */
2323
};
2424
extern HalfGap_Info* halfgap_info;
2525

@@ -38,13 +38,13 @@ class TransferThreadData {
3838
extern TransferThreadData* transfer_thread_data_; /* array for threads */
3939

4040
struct SetupInfo {
41-
int nsrc; // number of sources in this thread
42-
int ntar; // equal to memb_list nodecount
43-
int type;
44-
int ix_vpre;
45-
sgid_t* sid_src;
46-
int* v_indices; // increasing order
47-
sgid_t* sid_target; // aleady in memb_list order
41+
int nsrc = 0; // number of sources in this thread
42+
int ntar = 0; // equal to memb_list nodecount
43+
int type = 0;
44+
int ix_vpre = 0;
45+
sgid_t* sid_src = nullptr;
46+
int* v_indices = nullptr; // increasing order
47+
sgid_t* sid_target = nullptr; // aleady in memb_list order
4848
};
4949
extern SetupInfo* setup_info_; /* array for threads exists only during setup*/
5050

coreneuron/nrniv/partrans_setup.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,11 @@ void nrn_partrans::gap_mpi_setup(int ngroup) {
231231
// cleanup
232232
for (int tid = 0; tid < ngroup; ++tid) {
233233
SetupInfo& si = setup_info_[tid];
234-
delete[] si.sid_src;
235-
delete[] si.v_indices;
236-
delete[] si.sid_target;
234+
if (si.ntar) {
235+
delete[] si.sid_src;
236+
delete[] si.v_indices;
237+
delete[] si.sid_target;
238+
}
237239
}
238240
delete[] send_to_want;
239241
delete[] recv_from_have;
@@ -244,7 +246,11 @@ void nrn_partrans::gap_thread_setup(NrnThread& nt) {
244246
// printf("%d gap_thread_setup tid=%d\n", nrnmpi_myid, nt.id);
245247
nrn_partrans::TransferThreadData& ttd = transfer_thread_data_[nt.id];
246248

247-
ttd.halfgap_ml = nt._ml_list[halfgap_info->type];
249+
if (transfer_thread_data_[nt.id].ntar) {
250+
ttd.halfgap_ml = nt._ml_list[halfgap_info->type];
251+
} else {
252+
ttd.halfgap_ml = nullptr;
253+
}
248254
#if 0
249255
int ntar = ttd.halfgap_ml->nodecount;
250256
assert(ntar == ttd.ntar);

0 commit comments

Comments
 (0)