<commit_before>/*
 * ANNNormalizedErrorLayer.cpp
 *
 *  Created on: Jun 21, 2013
 *      Author: gkenyon
 */

#include "ANNNormalizedErrorLayer.hpp"
#include <fstream>

namespace PV {

  ANNNormalizedErrorLayer::ANNNormalizedErrorLayer()
  {
    initialize_base();
  }

  ANNNormalizedErrorLayer::ANNNormalizedErrorLayer(const char * name, HyPerCol * hc)
  {
    initialize_base();
    initialize(name, hc);
  }

  ANNNormalizedErrorLayer::~ANNNormalizedErrorLayer()
  {
    if (parent->getDtAdaptFlag() && parent->getWriteTimescales()){
      timeScaleStream.close();
    }
  }

  int ANNNormalizedErrorLayer::initialize_base()
  {
    timeScale = 1;
    return PV_SUCCESS;
  }

  int ANNNormalizedErrorLayer::initialize(const char * name, HyPerCol * hc)
  {
    int status = ANNErrorLayer::initialize(name, hc);
   
    if (parent->icCommunicator()->commRank()==0 && parent->getDtAdaptFlag() && parent->getWriteTimescales()){
      char * outputPath = parent->getOutputPath();
      size_t timeScaleFileNameLen = strlen(outputPath) + strlen("/") + strlen(name) + strlen("_timescales.txt");
      char timeScaleFileName[timeScaleFileNameLen+1];
      int charsneeded = snprintf(timeScaleFileName, timeScaleFileNameLen+1, "%s/%s_timescales.txt", outputPath, name);
      assert(charsneeded<=timeScaleFileNameLen);
      timeScaleStream.open(timeScaleFileName);
    }
    return status;
  }

  double ANNNormalizedErrorLayer::getTimeScale(){
    return timeScale;
  }

  double ANNNormalizedErrorLayer::calcTimeScale(){
    if (parent->getDtAdaptFlag()){
      timescale_timer->start();
      InterColComm * icComm = parent->icCommunicator();
      //int num_procs = icComm->numCommColumns() * icComm->numCommRows();
      int num_neurons = getNumNeurons();
      double errorMag = 0;
      double inputMag = 0;
      pvdata_t * gSynExc = getChannel(CHANNEL_EXC);
      pvdata_t * gSynInh = getChannel(CHANNEL_INH);
#ifdef PV_USE_OPENMP_THREADS
#pragma omp parallel for reduction(+ : errorMag, inputMag)
#endif
      for (int i = 0; i < num_neurons; i++){
         errorMag += (gSynExc[i] - gSynInh[i]) * (gSynExc[i] - gSynInh[i]);
         inputMag += gSynExc[i] * gSynExc[i]; 
        // if(i==1){
        //    fprintf(stdout, "ErrorMag: %f  inputMag: %f\ngSynExc[i]: %f\ngSynInh[i]: %f\n", errorMag, inputMag, gSynExc[i], gSynInh[i]);
         //}
      }
#ifdef PV_USE_MPI
      //Sum all errMag across processors
      MPI_Allreduce(MPI_IN_PLACE, &errorMag, 1, MPI_DOUBLE, MPI_SUM, icComm->communicator());
      MPI_Allreduce(MPI_IN_PLACE, &inputMag, 1, MPI_DOUBLE, MPI_SUM, icComm->communicator());
#endif // PV_USE_MPI
      //errorMag /= num_neurons * num_procs;
      //inputMag /= num_neurons * num_procs;
      timeScale = errorMag > 0 ? sqrt(inputMag / errorMag) : parent->getTimeScaleMin();
      //fprintf(stdout, "timeScale: %f\n", timeScale);
      timescale_timer->stop();
      if (parent->getWriteTimescales() && parent->icCommunicator()->commRank()==0){
         timeScaleStream << "sim_time = " << parent->simulationTime() << ", " << "timeScale = " << timeScale << std::endl;
      }
      return timeScale;
    }
    else{
      return HyPerLayer::calcTimeScale();
    }
  }


} /* namespace PV */
<commit_msg>ANNNormalizedErrorLayer exits with an error if errorMag is not-a-number or negative<commit_after>/*
 * ANNNormalizedErrorLayer.cpp
 *
 *  Created on: Jun 21, 2013
 *      Author: gkenyon
 */

#include "ANNNormalizedErrorLayer.hpp"
#include <fstream>

namespace PV {

ANNNormalizedErrorLayer::ANNNormalizedErrorLayer()
{
   initialize_base();
}

ANNNormalizedErrorLayer::ANNNormalizedErrorLayer(const char * name, HyPerCol * hc)
{
   initialize_base();
   initialize(name, hc);
}

ANNNormalizedErrorLayer::~ANNNormalizedErrorLayer()
{
   if (parent->getDtAdaptFlag() && parent->getWriteTimescales()){
      timeScaleStream.close();
   }
}

int ANNNormalizedErrorLayer::initialize_base()
{
   timeScale = 1;
   return PV_SUCCESS;
}

int ANNNormalizedErrorLayer::initialize(const char * name, HyPerCol * hc)
{
   int status = ANNErrorLayer::initialize(name, hc);

   if (parent->icCommunicator()->commRank()==0 && parent->getDtAdaptFlag() && parent->getWriteTimescales()){
      char * outputPath = parent->getOutputPath();
      size_t timeScaleFileNameLen = strlen(outputPath) + strlen("/") + strlen(name) + strlen("_timescales.txt");
      char timeScaleFileName[timeScaleFileNameLen+1];
      int charsneeded = snprintf(timeScaleFileName, timeScaleFileNameLen+1, "%s/%s_timescales.txt", outputPath, name);
      assert(charsneeded<=timeScaleFileNameLen);
      timeScaleStream.open(timeScaleFileName);
   }
   return status;
}

double ANNNormalizedErrorLayer::getTimeScale(){
   return timeScale;
}

double ANNNormalizedErrorLayer::calcTimeScale(){
   if (parent->getDtAdaptFlag()){
      timescale_timer->start();
      InterColComm * icComm = parent->icCommunicator();
      //int num_procs = icComm->numCommColumns() * icComm->numCommRows();
      int num_neurons = getNumNeurons();
      double errorMag = 0;
      double inputMag = 0;
      pvdata_t * gSynExc = getChannel(CHANNEL_EXC);
      pvdata_t * gSynInh = getChannel(CHANNEL_INH);
#ifdef PV_USE_OPENMP_THREADS
#pragma omp parallel for reduction(+ : errorMag, inputMag)
#endif
      for (int i = 0; i < num_neurons; i++){
         errorMag += (gSynExc[i] - gSynInh[i]) * (gSynExc[i] - gSynInh[i]);
         inputMag += gSynExc[i] * gSynExc[i]; 
      }
      if (isnan(errorMag)) {
         fprintf(stderr, "Layer \"%s\": errorMag on process %d is not a number.\n", getName(), getParent()->columnId());
         exit(EXIT_FAILURE);
      }
      else if (errorMag < 0) {
         fprintf(stderr, "Layer \"%s\": errorMag on process %d is negative.  This should absolutely never happen.\n", getName(), getParent()->columnId());
         exit(EXIT_FAILURE);
      }
#ifdef PV_USE_MPI
      //Sum all errMag across processors
      MPI_Allreduce(MPI_IN_PLACE, &errorMag, 1, MPI_DOUBLE, MPI_SUM, icComm->communicator());
      MPI_Allreduce(MPI_IN_PLACE, &inputMag, 1, MPI_DOUBLE, MPI_SUM, icComm->communicator());
#endif // PV_USE_MPI
      //errorMag /= num_neurons * num_procs;
      //inputMag /= num_neurons * num_procs;
      timeScale = errorMag > 0 ? sqrt(inputMag / errorMag) : parent->getTimeScaleMin();
      //fprintf(stdout, "timeScale: %f\n", timeScale);
      timescale_timer->stop();
      if (parent->getWriteTimescales() && parent->icCommunicator()->commRank()==0){
         timeScaleStream << "sim_time = " << parent->simulationTime() << ", " << "timeScale = " << timeScale << std::endl;
      }
      return timeScale;
   }
   else{
      return HyPerLayer::calcTimeScale();
   }
}


} /* namespace PV */

{'fraction_non_alphanumeric': 0.14189994044073853, 'fraction_numerical': 0.005360333531864205, 'mean_word_length': 3.733615221987315, 'pattern_counts': {'":': 2, '<': 34, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5549158', 'n_tokens_mistral': 2348, 'n_tokens_neox': 2244, 'n_words': 423}