65 grpIdPost_ = grpIdPost;
66 monitorId_ = monitorId;
73 needToWriteFileHeader_ =
true;
75 connFileSignature_ = 202029319;
76 connFileVersion_ = 0.3f;
81 connFileTimeIntervalSec_ = 1;
95 maxWt_ = fabs(connInfo.
maxWt);
107 for (
int i = 0; i < nNeurPre_; i++) {
108 std::vector<float> wt;
109 for (
int j = 0; j < nNeurPost_; j++) {
112 wtMat_.push_back(wt);
113 wtMatLast_.push_back(wt);
117 updateStoredWeights();
121 if (connFileId_!=NULL) {
123 if (connFileTimeIntervalSec_ > 0) {
133 needToWriteFileHeader_ =
true;
141 updateStoredWeights();
142 std::vector< std::vector<float> > wtChange(nNeurPre_, std::vector<float>(nNeurPost_));
145 for (
int i=0; i<nNeurPre_; i++) {
146 for (
int j=0; j<nNeurPost_; j++) {
147 wtChange[i][j] = wtMat_[i][j] - wtMatLast_[i][j];
157 for (
int i=0; i<nNeurPre_; i++) {
158 for (
int j=0; j<nNeurPost_; j++) {
160 wtMatLast_[i][j] = NAN;
167 assert(neurPostId<nNeurPost_);
169 for (
int i=0; i<nNeurPre_; i++) {
170 if (!isnan(wtMat_[i][neurPostId])) {
179 assert(neurPreId<nNeurPre_);
181 for (
int j=0; j<nNeurPost_; j++) {
182 if (!isnan(wtMat_[neurPreId][j])) {
190 float maxVal = minWt_;
192 updateStoredWeights();
195 for (
int i=0; i<nNeurPre_; i++) {
196 for (
int j=0; j<nNeurPost_; j++) {
198 if (isnan(wtMat_[i][j]))
201 if (wtMat_[i][j] > maxVal) {
202 maxVal = wtMat_[i][j];
215 float minVal = maxWt_;
217 updateStoredWeights();
220 for (
int i=0; i<nNeurPre_; i++) {
221 for (
int j=0; j<nNeurPost_; j++) {
223 if (isnan(wtMat_[i][j]))
226 if (wtMat_[i][j] < minVal) {
227 minVal = wtMat_[i][j];
241 assert(minAbsChange>=0.0);
245 for (
int i=0; i<nNeurPre_; i++) {
246 for (
int j=0; j<nNeurPost_; j++) {
248 if (isnan(wtMat_[i][j]))
251 if (fabs(wtChange[i][j]) >= minAbsChange) {
261 assert(maxVal>=minVal);
263 updateStoredWeights();
271 for (
int i=0; i<nNeurPre_; i++) {
272 for (
int j=0; j<nNeurPost_; j++) {
274 if (isnan(wtMat_[i][j]))
277 if (wtMat_[i][j]>=minVal && wtMat_[i][j]<=maxVal) {
299 double wtTotalChange = 0.0;
300 for (
int i=0; i<nNeurPre_; i++) {
301 for (
int j=0; j<nNeurPost_; j++) {
303 if (isnan(wtMat_[i][j]))
305 wtTotalChange += fabs(wtChange[i][j]);
308 return wtTotalChange;
312 updateStoredWeights();
314 KERNEL_INFO(
"(t=%.3fs) ConnectionMonitor ID=%d: %d(%s) => %d(%s)",
320 std::stringstream header, header2;
321 header <<
" pre\\post |";
322 header2 <<
"----------|";
323 for (
int j=0; j<nNeurPost_; j++) {
324 header << std::setw(9) << std::setfill(
' ') << j <<
" |";
325 header2 <<
"-----------";
330 for (
int i=0; i<nNeurPre_; i++) {
331 std::stringstream line;
332 line << std::setw(9) << std::setfill(
' ') << i <<
" |";
333 for (
int j=0; j<nNeurPost_; j++) {
334 line << std::fixed << std::setprecision(4) << (isnan(wtMat_[i][j])?
" ":(wtMat_[i][j]>=0?
" ":
" "))
335 << wtMat_[i][j] <<
" ";
342 assert(neurPostId<nNeurPost_);
344 assert(connPerLine>0);
347 std::vector< std::vector<float> > wtNew, wtOld;
348 long int timeNew, timeOld;
349 if (!storeNewSnapshot) {
354 timeOld = wtTimeLast_;
357 updateStoredWeights();
358 KERNEL_INFO(
"(t=%.3fs) ConnectionMonitor ID=%d %d(%s) => %d(%s): [preId,postId] wt (+/-wtChange in %ldms) "
364 if (neurPostId==
ALL) {
366 postZ = nNeurPost_ - 1;
372 std::vector< std::vector<float> > wtChange;
377 std::stringstream line;
379 int maxIntDigits = ceil(log10((
double)std::max(nNeurPre_,nNeurPost_)));
380 for (
int i=0; i<nNeurPre_; i++) {
381 for (
int j = postA; j <= postZ; j++) {
386 if (!isnan(wtMat_[i][j])) {
387 line <<
"[" << std::setw(maxIntDigits) << i <<
"," << std::setw(maxIntDigits) << j <<
"] "
388 << std::fixed << std::setprecision(4) << wtMat_[i][j];
390 line <<
" (" << ((wtChange[i][j]<0)?
"":
"+");
391 line << std::setprecision(4) << wtChange[i][j] <<
")";
394 if (!(++nConn % connPerLine)) {
396 line.str(std::string());
402 if (nConn % connPerLine)
405 if (!storeNewSnapshot) {
409 wtTimeLast_ = timeOld;
416 if (connFileId_!=NULL)
417 KERNEL_ERROR(
"ConnectionMonitorCore: setConnectFileId has already been called.");
419 connFileId_=connFileId;
421 if (connFileId_==NULL) {
422 needToWriteFileHeader_ =
false;
426 needToWriteFileHeader_ =
true;
427 writeConnectFileHeader();
432 assert(intervalSec==-1 || intervalSec>=1);
433 connFileTimeIntervalSec_ = intervalSec;
437 void ConnectionMonitorCore::updateStoredWeights() {
441 wtTimeLast_ = wtTime_;
450 updateStoredWeights();
457 void ConnectionMonitorCore::writeConnectFileHeader() {
460 if (!needToWriteFileHeader_)
464 if (!fwrite(&connFileSignature_,
sizeof(
int),1,connFileId_))
465 KERNEL_ERROR(
"ConnectionMonitorCore: writeConnectFileHeader has fwrite error");
468 if (!fwrite(&connFileVersion_,
sizeof(
float),1,connFileId_))
469 KERNEL_ERROR(
"ConnectionMonitorCore: writeConnectFileHeader has fwrite error");
472 if (!fwrite(&connId_,
sizeof(
short int),1,connFileId_))
473 KERNEL_ERROR(
"ConnectionMonitor: writeConnectFileHeader has fwrite error");
477 if (!fwrite(&grpIdPre_,
sizeof(
int),1,connFileId_))
478 KERNEL_ERROR(
"ConnectionMonitor: writeConnectFileHeader has fwrite error");
479 if (!fwrite(&(gridPre.
numX),
sizeof(
int),1,connFileId_))
480 KERNEL_ERROR(
"ConnectionMonitor: writeConnectFileHeader has fwrite error");
481 if (!fwrite(&(gridPre.
numY),
sizeof(
int),1,connFileId_))
482 KERNEL_ERROR(
"ConnectionMonitor: writeConnectFileHeader has fwrite error");
483 if (!fwrite(&(gridPre.
numZ),
sizeof(
int),1,connFileId_))
484 KERNEL_ERROR(
"ConnectionMonitor: writeConnectFileHeader has fwrite error");
488 if (!fwrite(&grpIdPost_,
sizeof(
int),1,connFileId_))
489 KERNEL_ERROR(
"ConnectionMonitor: writeConnectFileHeader has fwrite error");
490 if (!fwrite(&(gridPost.
numX),
sizeof(
int),1,connFileId_))
491 KERNEL_ERROR(
"ConnectionMonitor: writeConnectFileHeader has fwrite error");
492 if (!fwrite(&(gridPost.
numY),
sizeof(
int),1,connFileId_))
493 KERNEL_ERROR(
"ConnectionMonitor: writeConnectFileHeader has fwrite error");
494 if (!fwrite(&(gridPost.
numZ),
sizeof(
int),1,connFileId_))
495 KERNEL_ERROR(
"ConnectionMonitor: writeConnectFileHeader has fwrite error");
498 if (!fwrite(&nSynapses_,
sizeof(
int),1,connFileId_))
499 KERNEL_ERROR(
"ConnectionMonitor: writeConnectFileHeader has fwrite error");
502 if (!fwrite(&isPlastic_,
sizeof(
bool),1,connFileId_))
503 KERNEL_ERROR(
"ConnectionMonitor: writeConnectFileHeader has fwrite error");
506 if (!fwrite(&minWt_,
sizeof(
float),1,connFileId_))
507 KERNEL_ERROR(
"ConnectionMonitorCore: writeConnectFileHeader has fwrite error");
508 if (!fwrite(&maxWt_,
sizeof(
float),1,connFileId_))
509 KERNEL_ERROR(
"ConnectionMonitorCore: writeConnectFileHeader has fwrite error");
514 needToWriteFileHeader_ =
false;
519 if ((
long long)simTimeMs <= wtTimeWrite_ || connFileId_==NULL) {
523 wtTimeWrite_ = (
long long)simTimeMs;
526 if (!fwrite(&wtTimeWrite_,
sizeof(
long long),1,connFileId_))
527 KERNEL_ERROR(
"ConnectionMonitor: writeConnectFileSnapshot has fwrite error");
530 for (
int i=0; i<nNeurPre_; i++) {
531 for (
int j=0; j<nNeurPost_; j++) {
532 if (!fwrite(&wts[i][j],
sizeof(
float),1,connFileId_)) {
533 KERNEL_ERROR(
"ConnectionMonitor: writeConnectFileSnapshot has fwrite error");