8 #include "StatsProbe.hpp" 9 #include "../layers/HyPerLayer.hpp" 15 StatsProbe::StatsProbe(
const char *name, HyPerCol *hc) {
25 StatsProbe::~StatsProbe() {
26 int rank = parent->columnId();
27 if (rank == 0 and !mOutputStreams.empty()) {
28 iotimer->fprint_time(
output(0));
29 mpitimer->fprint_time(
output(0));
30 comptimer->fprint_time(
output(0));
44 int StatsProbe::initialize_base() {
57 nnzThreshold = (float)0;
61 int StatsProbe::initialize(
const char *name, HyPerCol *hc) {
64 assert(status == PV_SUCCESS);
66 int nbatch = parent->getNBatch();
68 fMin = (
float *)malloc(
sizeof(
float) * nbatch);
69 fMax = (
float *)malloc(
sizeof(
float) * nbatch);
70 sum = (
double *)malloc(
sizeof(
double) * nbatch);
71 sum2 = (
double *)malloc(
sizeof(
double) * nbatch);
72 avg = (
float *)malloc(
sizeof(
float) * nbatch);
73 sigma = (
float *)malloc(
sizeof(
float) * nbatch);
74 nnz = (
int *)malloc(
sizeof(
int) * nbatch);
80 void StatsProbe::resetStats() {
81 for (
int b = 0; b < parent->getNBatch(); b++) {
94 ioParam_buffer(ioFlag);
95 ioParam_nnzThreshold(ioFlag);
99 void StatsProbe::requireType(PVBufType requiredType) {
100 PVParams *params = parent->parameters();
101 if (params->stringPresent(getName(),
"buffer")) {
103 StatsProbe::ioParam_buffer(PARAMS_IO_READ);
104 if (type != requiredType) {
105 const char *requiredString = NULL;
106 switch (requiredType) {
107 case BufV: requiredString =
"\"MembranePotential\" or \"V\"";
break;
108 case BufActivity: requiredString =
"\"Activity\" or \"A\"";
break;
109 default: assert(0);
break;
112 if (parent->columnId() == 0) {
114 " Value \"%s\" is inconsistent with allowed values %s.\n",
115 params->stringValue(getName(),
"buffer"),
126 void StatsProbe::ioParam_buffer(
enum ParamsIOFlag ioFlag) {
128 if (ioFlag == PARAMS_IO_WRITE) {
130 case BufV: buffer = strdup(
"MembranePotential");
break;
131 case BufActivity: buffer = strdup(
"Activity");
134 parent->parameters()->ioParamString(
135 ioFlag, getName(),
"buffer", &buffer,
"Activity",
true );
136 if (ioFlag == PARAMS_IO_READ) {
138 size_t len = strlen(buffer);
139 for (
size_t c = 0; c < len; c++) {
140 buffer[c] = (char)tolower((
int)buffer[c]);
142 if (!strcmp(buffer,
"v") || !strcmp(buffer,
"membranepotential")) {
145 else if (!strcmp(buffer,
"a") || !strcmp(buffer,
"activity")) {
149 if (parent->columnId() == 0) {
150 const char *bufnameinparams = parent->parameters()->stringValue(getName(),
"buffer");
151 assert(bufnameinparams);
153 "%s: buffer \"%s\" is not recognized.\n", getDescription_c(), bufnameinparams);
155 MPI_Barrier(parent->getCommunicator()->communicator());
163 void StatsProbe::ioParam_nnzThreshold(
enum ParamsIOFlag ioFlag) {
164 parent->parameters()->ioParamValue(ioFlag, getName(),
"nnzThreshold", &nnzThreshold, 0.0f);
175 std::string timermessagehead;
176 timermessagehead.append(
"StatsProbe ").append(getName());
177 std::string timermessage;
179 timermessage = timermessagehead +
" I/O timer ";
180 iotimer =
new Timer(timermessage.c_str());
181 checkpointer->registerTimer(iotimer);
183 timermessage = timermessagehead +
" MPI timer ";
184 mpitimer =
new Timer(timermessage.c_str());
185 checkpointer->registerTimer(mpitimer);
187 timermessage = timermessagehead +
" Comp timer ";
188 comptimer =
new Timer(timermessage.c_str());
189 checkpointer->registerTimer(comptimer);
190 return Response::SUCCESS;
196 MPI_Comm comm = icComm->communicator();
197 int rank = icComm->commRank();
198 const int rcvProc = 0;
205 nk = getTargetLayer()->getNumNeurons();
207 int nbatch = getTargetLayer()->getLayerLoc()->nbatch;
212 for (
int b = 0; b < nbatch; b++) {
213 buf = getTargetLayer()->getV() + b * getTargetLayer()->getNumNeurons();
216 if (rank != rcvProc) {
217 return Response::SUCCESS;
221 return Response::SUCCESS;
223 for (
int k = 0; k < nk; k++) {
226 sum2[b] += (double)(a * a);
227 if (fabsf(a) > nnzThreshold) {
240 for (
int b = 0; b < nbatch; b++) {
241 buf = getTargetLayer()->
getLayerData() + b * getTargetLayer()->getNumExtended();
243 for (
int k = 0; k < nk; k++) {
244 const PVLayerLoc *loc = getTargetLayer()->getLayerLoc();
245 int kex = kIndexExtended(
259 sum2[b] += (double)(a * a);
260 if (fabsf(a) > nnzThreshold) {
272 default: pvAssert(0);
break;
281 ierr = MPI_Allreduce(MPI_IN_PLACE, sum, nbatch, MPI_DOUBLE, MPI_SUM, comm);
282 ierr = MPI_Allreduce(MPI_IN_PLACE, sum2, nbatch, MPI_DOUBLE, MPI_SUM, comm);
283 ierr = MPI_Allreduce(MPI_IN_PLACE, nnz, nbatch, MPI_INT, MPI_SUM, comm);
284 ierr = MPI_Allreduce(MPI_IN_PLACE, fMin, nbatch, MPI_FLOAT, MPI_MIN, comm);
285 ierr = MPI_Allreduce(MPI_IN_PLACE, fMax, nbatch, MPI_FLOAT, MPI_MAX, comm);
286 ierr = MPI_Allreduce(MPI_IN_PLACE, &nk, 1, MPI_INT, MPI_SUM, comm);
289 if (rank != rcvProc) {
290 return Response::SUCCESS;
297 for (
int b = 0; b < nbatch; b++) {
298 avg[b] = (float)sum[b] / divisor;
299 sigma[b] = sqrtf((
float)sum2[b] / divisor - avg[b] * avg[b]);
301 char const *avgnote =
nullptr;
302 if (type == BufActivity && getTargetLayer()->getSparseFlag()) {
303 avgval = 1000.0f * avg[b];
304 avgnote =
" Hz (/dt ms)";
311 "%st==%6.1f b==%d N==%d Total==%f Min==%f Avg==%f%s " 312 "Max==%f sigma==%f nnz==%d",
329 return Response::SUCCESS;
332 int StatsProbe::checkpointTimers(
PrintStream &timerstream) {
333 iotimer->fprint_time(timerstream);
334 mpitimer->fprint_time(timerstream);
335 comptimer->fprint_time(timerstream);
virtual int ioParamsFillGroup(enum ParamsIOFlag ioFlag) override
virtual Response::Status outputState(double timef) override
LayerProbe(const char *name, HyPerCol *hc)
virtual int ioParamsFillGroup(enum ParamsIOFlag ioFlag) override
PrintStream & output(int b)
static bool completed(Status &a)
virtual Response::Status registerData(Checkpointer *checkpointer) override
int initialize(const char *name, HyPerCol *hc)
const char * getMessage()
virtual Response::Status registerData(Checkpointer *checkpointer) override
void handleUnnecessaryStringParameter(const char *group_name, const char *param_name)
const float * getLayerData(int delay=0)
virtual void initNumValues() override