66 grpIdPost_ = grpIdPost;
67 monitorId_ = monitorId;
74 needToWriteFileHeader_ =
true;
76 connFileSignature_ = 202029319;
77 connFileVersion_ = 0.3f;
82 connFileTimeIntervalSec_ = 1;
96 maxWt_ = fabs(connInfo.
maxWt);
108 for (
int i = 0; i < nNeurPre_; i++) {
109 std::vector<float> wt;
110 for (
int j = 0; j < nNeurPost_; j++) {
113 wtMat_.push_back(wt);
114 wtMatLast_.push_back(wt);
118 updateStoredWeights();
122 if (connFileId_!=NULL) {
124 if (connFileTimeIntervalSec_ > 0) {
134 needToWriteFileHeader_ =
true;
142 updateStoredWeights();
143 std::vector< std::vector<float> > wtChange(nNeurPre_, std::vector<float>(nNeurPost_));
146 for (
int i=0; i<nNeurPre_; i++) {
147 for (
int j=0; j<nNeurPost_; j++) {
148 wtChange[i][j] = wtMat_[i][j] - wtMatLast_[i][j];
158 for (
int i=0; i<nNeurPre_; i++) {
159 for (
int j=0; j<nNeurPost_; j++) {
161 wtMatLast_[i][j] = NAN;
168 assert(neurPostId<nNeurPost_);
170 for (
int i=0; i<nNeurPre_; i++) {
171 if (!std::isnan(wtMat_[i][neurPostId])) {
180 assert(neurPreId<nNeurPre_);
182 for (
int j=0; j<nNeurPost_; j++) {
183 if (!std::isnan(wtMat_[neurPreId][j])) {
191 float maxVal = minWt_;
193 updateStoredWeights();
196 for (
int i=0; i<nNeurPre_; i++) {
197 for (
int j=0; j<nNeurPost_; j++) {
199 if (std::isnan(wtMat_[i][j]))
202 if (wtMat_[i][j] > maxVal) {
203 maxVal = wtMat_[i][j];
216 float minVal = maxWt_;
218 updateStoredWeights();
221 for (
int i=0; i<nNeurPre_; i++) {
222 for (
int j=0; j<nNeurPost_; j++) {
224 if (std::isnan(wtMat_[i][j]))
227 if (wtMat_[i][j] < minVal) {
228 minVal = wtMat_[i][j];
242 assert(minAbsChange>=0.0);
246 for (
int i=0; i<nNeurPre_; i++) {
247 for (
int j=0; j<nNeurPost_; j++) {
249 if (std::isnan(wtMat_[i][j]))
252 if (fabs(wtChange[i][j]) >= minAbsChange) {
262 assert(maxVal>=minVal);
264 updateStoredWeights();
272 for (
int i=0; i<nNeurPre_; i++) {
273 for (
int j=0; j<nNeurPost_; j++) {
275 if (std::isnan(wtMat_[i][j]))
278 if (wtMat_[i][j]>=minVal && wtMat_[i][j]<=maxVal) {
300 double wtTotalChange = 0.0;
301 for (
int i=0; i<nNeurPre_; i++) {
302 for (
int j=0; j<nNeurPost_; j++) {
304 if (std::isnan(wtMat_[i][j]))
306 wtTotalChange += fabs(wtChange[i][j]);
309 return wtTotalChange;
313 updateStoredWeights();
315 KERNEL_INFO(
"(t=%.3fs) ConnectionMonitor ID=%d: %d(%s) => %d(%s)",
321 std::stringstream header, header2;
322 header <<
" pre\\post |";
323 header2 <<
"----------|";
324 for (
int j=0; j<nNeurPost_; j++) {
325 header << std::setw(9) << std::setfill(
' ') << j <<
" |";
326 header2 <<
"-----------";
331 for (
int i=0; i<nNeurPre_; i++) {
332 std::stringstream line;
333 line << std::setw(9) << std::setfill(
' ') << i <<
" |";
334 for (
int j=0; j<nNeurPost_; j++) {
335 line << std::fixed << std::setprecision(4) << (std::isnan(wtMat_[i][j])?
" ":(wtMat_[i][j]>=0?
" ":
" "))
336 << wtMat_[i][j] <<
" ";
343 assert(neurPostId<nNeurPost_);
345 assert(connPerLine>0);
348 std::vector< std::vector<float> > wtNew, wtOld;
349 long int timeNew, timeOld;
350 if (!storeNewSnapshot) {
355 timeOld = wtTimeLast_;
358 updateStoredWeights();
359 KERNEL_INFO(
"(t=%.3fs) ConnectionMonitor ID=%d %d(%s) => %d(%s): [preId,postId] wt (+/-wtChange in %ldms) "
365 if (neurPostId==
ALL) {
367 postZ = nNeurPost_ - 1;
373 std::vector< std::vector<float> > wtChange;
378 std::stringstream line;
380 int maxIntDigits = ceil(log10((
double)std::max(nNeurPre_,nNeurPost_)));
381 for (
int i=0; i<nNeurPre_; i++) {
382 for (
int j = postA; j <= postZ; j++) {
387 if (!std::isnan(wtMat_[i][j])) {
388 line <<
"[" << std::setw(maxIntDigits) << i <<
"," << std::setw(maxIntDigits) << j <<
"] "
389 << std::fixed << std::setprecision(4) << wtMat_[i][j];
391 line <<
" (" << ((wtChange[i][j]<0)?
"":
"+");
392 line << std::setprecision(4) << wtChange[i][j] <<
")";
395 if (!(++nConn % connPerLine)) {
397 line.str(std::string());
403 if (nConn % connPerLine)
406 if (!storeNewSnapshot) {
410 wtTimeLast_ = timeOld;
417 if (connFileId_!=NULL)
418 KERNEL_ERROR(
"ConnectionMonitorCore: setConnectFileId has already been called.");
420 connFileId_=connFileId;
422 if (connFileId_==NULL) {
423 needToWriteFileHeader_ =
false;
427 needToWriteFileHeader_ =
true;
428 writeConnectFileHeader();
433 assert(intervalSec==-1 || intervalSec>=1);
434 connFileTimeIntervalSec_ = intervalSec;
438 void ConnectionMonitorCore::updateStoredWeights() {
442 wtTimeLast_ = wtTime_;
451 updateStoredWeights();
458 void ConnectionMonitorCore::writeConnectFileHeader() {
461 if (!needToWriteFileHeader_)
465 if (!fwrite(&connFileSignature_,
sizeof(
int),1,connFileId_))
466 KERNEL_ERROR(
"ConnectionMonitorCore: writeConnectFileHeader has fwrite error");
469 if (!fwrite(&connFileVersion_,
sizeof(
float),1,connFileId_))
470 KERNEL_ERROR(
"ConnectionMonitorCore: writeConnectFileHeader has fwrite error");
473 if (!fwrite(&connId_,
sizeof(
short int),1,connFileId_))
474 KERNEL_ERROR(
"ConnectionMonitor: writeConnectFileHeader has fwrite error");
478 if (!fwrite(&grpIdPre_,
sizeof(
int),1,connFileId_))
479 KERNEL_ERROR(
"ConnectionMonitor: writeConnectFileHeader has fwrite error");
480 if (!fwrite(&(gridPre.
numX),
sizeof(
int),1,connFileId_))
481 KERNEL_ERROR(
"ConnectionMonitor: writeConnectFileHeader has fwrite error");
482 if (!fwrite(&(gridPre.
numY),
sizeof(
int),1,connFileId_))
483 KERNEL_ERROR(
"ConnectionMonitor: writeConnectFileHeader has fwrite error");
484 if (!fwrite(&(gridPre.
numZ),
sizeof(
int),1,connFileId_))
485 KERNEL_ERROR(
"ConnectionMonitor: writeConnectFileHeader has fwrite error");
489 if (!fwrite(&grpIdPost_,
sizeof(
int),1,connFileId_))
490 KERNEL_ERROR(
"ConnectionMonitor: writeConnectFileHeader has fwrite error");
491 if (!fwrite(&(gridPost.
numX),
sizeof(
int),1,connFileId_))
492 KERNEL_ERROR(
"ConnectionMonitor: writeConnectFileHeader has fwrite error");
493 if (!fwrite(&(gridPost.
numY),
sizeof(
int),1,connFileId_))
494 KERNEL_ERROR(
"ConnectionMonitor: writeConnectFileHeader has fwrite error");
495 if (!fwrite(&(gridPost.
numZ),
sizeof(
int),1,connFileId_))
496 KERNEL_ERROR(
"ConnectionMonitor: writeConnectFileHeader has fwrite error");
499 if (!fwrite(&nSynapses_,
sizeof(
int),1,connFileId_))
500 KERNEL_ERROR(
"ConnectionMonitor: writeConnectFileHeader has fwrite error");
503 if (!fwrite(&isPlastic_,
sizeof(
bool),1,connFileId_))
504 KERNEL_ERROR(
"ConnectionMonitor: writeConnectFileHeader has fwrite error");
507 if (!fwrite(&minWt_,
sizeof(
float),1,connFileId_))
508 KERNEL_ERROR(
"ConnectionMonitorCore: writeConnectFileHeader has fwrite error");
509 if (!fwrite(&maxWt_,
sizeof(
float),1,connFileId_))
510 KERNEL_ERROR(
"ConnectionMonitorCore: writeConnectFileHeader has fwrite error");
515 needToWriteFileHeader_ =
false;
520 if ((
long long)simTimeMs <= wtTimeWrite_ || connFileId_==NULL) {
524 wtTimeWrite_ = (
long long)simTimeMs;
527 if (!fwrite(&wtTimeWrite_,
sizeof(
long long),1,connFileId_))
528 KERNEL_ERROR(
"ConnectionMonitor: writeConnectFileSnapshot has fwrite error");
531 for (
int i=0; i<nNeurPre_; i++) {
532 for (
int j=0; j<nNeurPost_; j++) {
533 if (!fwrite(&wts[i][j],
sizeof(
float),1,connFileId_)) {
534 KERNEL_ERROR(
"ConnectionMonitor: writeConnectFileSnapshot has fwrite error");