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

Commit 06d63c0

Browse files
iomaganarispramodk
andcommitted
Discard spikes from forwardskip and improve for spike sorting (#195)
- Clear spike vectors after forwardskip to avoid spikes generated with negative timestamp - Changed strcat to memcpy based to improve with millions of spikes Co-Authored-By: Pramod Kumbhar <[email protected]>
1 parent b122751 commit 06d63c0

File tree

5 files changed

+104
-2
lines changed

5 files changed

+104
-2
lines changed

coreneuron/nrniv/main1.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,9 @@ void handle_forward_skip(double forwardskip, int prcellgid) {
355355
dt = savedt;
356356
t = savet;
357357
dt2thread(-1.);
358+
359+
// clear spikes generated during forward skip (with negative time)
360+
clear_spike_vectors();
358361
}
359362

360363
const char* nrn_version(int) {

coreneuron/nrniv/output_spikes.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ THE POSSIBILITY OF SUCH DAMAGE.
4040
#include "coreneuron/nrniv/nrnmutdec.h"
4141
#include "coreneuron/nrnmpi/nrnmpi_impl.h"
4242
#include "coreneuron/nrnmpi/nrnmpidec.h"
43+
#include "coreneuron/utils/string_utils.h"
4344

4445
namespace coreneuron {
4546
std::vector<double> spikevec_time;
@@ -176,9 +177,10 @@ void output_spikes_parallel(const char* outpath) {
176177

177178
// populate buffer with all spike entries
178179
char spike_entry[SPIKE_RECORD_LEN];
180+
unsigned spike_data_offset = 0;
179181
for (unsigned i = 0; i < num_spikes; i++) {
180-
snprintf(spike_entry, 64, "%.8g\t%d\n", spikevec_time[i], spikevec_gid[i]);
181-
strcat(spike_data, spike_entry);
182+
int spike_entry_chars = snprintf(spike_entry, 64, "%.8g\t%d\n", spikevec_time[i], spikevec_gid[i]);
183+
spike_data_offset = strcat_at_pos(spike_data, spike_data_offset, spike_entry, spike_entry_chars);
182184
}
183185

184186
// calculate offset into global file. note that we don't write
@@ -250,6 +252,15 @@ void output_spikes(const char* outpath) {
250252
#endif
251253
}
252254

255+
void clear_spike_vectors() {
256+
auto spikevec_time_capacity = spikevec_time.capacity();
257+
auto spikevec_gid_capacity = spikevec_gid.capacity();
258+
spikevec_time.clear();
259+
spikevec_gid.clear();
260+
spikevec_time.reserve(spikevec_time_capacity);
261+
spikevec_gid.reserve(spikevec_gid_capacity);
262+
}
263+
253264
void validation(std::vector<std::pair<double, int> >& res) {
254265
for (unsigned i = 0; i < spikevec_gid.size(); ++i)
255266
if (spikevec_gid[i] > -1)

coreneuron/nrniv/output_spikes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ void mk_spikevec_buffer(int);
3838
extern std::vector<double> spikevec_time;
3939
extern std::vector<int> spikevec_gid;
4040

41+
void clear_spike_vectors();
4142
void validation(std::vector<std::pair<double, int> >& res);
4243

4344
void spikevec_lock();

coreneuron/utils/string_utils.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Copyright (c) 2019, Blue Brain Project
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without modification,
6+
are permitted provided that the following conditions are met:
7+
1. Redistributions of source code must retain the above copyright notice,
8+
this list of conditions and the following disclaimer.
9+
2. Redistributions in binary form must reproduce the above copyright notice,
10+
this list of conditions and the following disclaimer in the documentation
11+
and/or other materials provided with the distribution.
12+
3. Neither the name of the copyright holder nor the names of its contributors
13+
may be used to endorse or promote products derived from this software
14+
without specific prior written permission.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26+
THE POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
29+
#include <cstring>
30+
31+
unsigned strcat_at_pos(char* dest, unsigned start_position, char* src, unsigned src_length) {
32+
memcpy(dest + start_position, src, src_length);
33+
dest[start_position + src_length] = '\0';
34+
return start_position + src_length;
35+
}

coreneuron/utils/string_utils.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Copyright (c) 2019, Blue Brain Project
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without modification,
6+
are permitted provided that the following conditions are met:
7+
1. Redistributions of source code must retain the above copyright notice,
8+
this list of conditions and the following disclaimer.
9+
2. Redistributions in binary form must reproduce the above copyright notice,
10+
this list of conditions and the following disclaimer in the documentation
11+
and/or other materials provided with the distribution.
12+
3. Neither the name of the copyright holder nor the names of its contributors
13+
may be used to endorse or promote products derived from this software
14+
without specific prior written permission.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26+
THE POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
29+
/**
30+
* @file string_utils.h
31+
* @brief Utility functions for strings
32+
*
33+
*/
34+
35+
#ifndef CORENEURON_STRING_UTILS_HPP
36+
#define CORENEURON_STRING_UTILS_HPP
37+
38+
/** @brief Appends a copy of the source string to the destination string.
39+
*
40+
* A null-character is included at the end of the new string formed by the concatenation of both in destination.
41+
* It has similar behavior to strcat but better performance in case that it is needed to append a char array to
42+
* another very large char array.
43+
*
44+
* @param dest Destination string
45+
* @param start_position Position of dest to start writing src
46+
* @param src Source string
47+
* @param src_length Length of src to append to dest
48+
* @return Position of the final character of dest after appending src (including the null terminating character)
49+
*/
50+
unsigned strcat_at_pos(char* dest, unsigned start_position, char* src, unsigned src_length);
51+
52+
#endif /* ifndef CORENEURON_STRING_UTILS_HPP */

0 commit comments

Comments
 (0)