SingleChannelBeoServer.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "Parallel/SingleChannelBeoServer.H"
00039
00040 #include "Beowulf/Beowulf.H"
00041 #include "Beowulf/BeowulfOpts.H"
00042 #include "Channels/BlueYellowChannel.H"
00043 #include "Channels/ColorBandChannel.H"
00044 #include "Channels/DirectionChannel.H"
00045 #include "Channels/FlickerChannel.H"
00046 #include "Channels/GaborChannel.H"
00047 #include "Channels/IntensityChannel.H"
00048 #include "Channels/RedGreenChannel.H"
00049 #include "Channels/SingleChannel.H"
00050 #include "Component/OptionManager.H"
00051 #include "Component/ParamMap.H"
00052 #include "Image/PyramidTypes.H"
00053 #include "Neuro/SingleChannelBeo.H"
00054 #include "Neuro/VisualCortexSurprise.H"
00055 #include "Util/Assert.H"
00056 #include "Util/sformat.H"
00057
00058 #include <sstream>
00059
00060
00061 SingleChannelBeoServer::SingleChannelBeoServer(OptionManager& mgr,
00062 const std::string& descrName,
00063 const std::string& tagName) :
00064 ModelComponent(mgr, descrName, tagName),
00065 itsQuickMode(&OPT_SingleChannelBeoServerQuickMode, this),
00066 itsBeo(new Beowulf(mgr))
00067 {
00068
00069 addSubComponent(itsBeo);
00070
00071 VisualCortexSurprise::registerSurpriseTypes(mgr);
00072 }
00073
00074
00075 SingleChannelBeoServer::~SingleChannelBeoServer()
00076 { }
00077
00078
00079 void SingleChannelBeoServer::check()
00080 {
00081
00082 TCPmessage rmsg;
00083 int32 rframe, raction, rnode = -1;
00084
00085 if (itsBeo->receive(rnode, rmsg, rframe, raction, 50))
00086 switch(raction) {
00087 case BEO_INIT:
00088 {
00089
00090 removeAllSubComponents();
00091
00092 }
00093 break;
00094 case BEO_SCHANCONF:
00095 {
00096
00097
00098 VisualFeature vs = static_cast<VisualFeature>(rframe);
00099 ParamMap params, coeffs;
00100
00101 const std::string dnam = rmsg.getElementString();
00102 const std::string tnam = rmsg.getElementString();
00103
00104 std::string buf = rmsg.getElementString();
00105 std::stringstream sparam(buf);
00106 params.load(sparam);
00107
00108 buf = rmsg.getElementString();
00109 std::stringstream scoeff(buf);
00110 coeffs.load(scoeff);
00111
00112
00113 nub::soft_ref<SingleChannel> ch = channel(rnode);
00114 if (ch.isValid())
00115 {
00116 LINFO("Deleting '%s' of node %d", ch->descriptiveName().c_str(),
00117 rnode);
00118 ch->stop();
00119 removeSubComponent(*ch);
00120 ch.reset(NULL);
00121 }
00122
00123
00124
00125
00126
00127
00128 switch(vs)
00129 {
00130 case INTENS: ch.reset(new IntensityChannel(getManager())); break;
00131 case BY: ch.reset(new BlueYellowChannel(getManager())); break;
00132 case RG: ch.reset(new RedGreenChannel(getManager())); break;
00133 case FLICKER: ch.reset(new FlickerChannel(getManager())); break;
00134 case COLBAND: ch.reset(new ColorBandChannel(getManager(), 0)); break;
00135 case ORI: ch.reset(new GaborChannel(getManager(),0, 0.0)); break;
00136 case MOTION: ch.reset(new DirectionChannel(getManager(), 0, 0.0,
00137 Gaussian9)); break;
00138 default: LFATAL("Unsupported VisualFeature %d", rframe);
00139 }
00140
00141
00142 ch->setDescriptiveName(dnam);
00143 ch->setTagName(tnam);
00144
00145
00146
00147 ch->readParamsFrom(params, false);
00148
00149
00150
00151
00152 ch->setTagName(sformat("%d", rnode));
00153
00154
00155 ch->start();
00156
00157
00158 ch->readFrom(coeffs);
00159
00160
00161 addSubComponent(ch);
00162
00163
00164 LINFO("Created '%s' for %s [node %d]",
00165 ch->descriptiveName().c_str(), itsBeo->nodeName(rnode), rnode);
00166 }
00167 break;
00168 case BEO_SCHANINPUT:
00169 {
00170
00171 nub::soft_ref<SingleChannel> ch = channel(rnode);
00172 if (ch.isInvalid())
00173 LERROR("Input from node %d but no channel for it -- IGNORING",rnode);
00174 else
00175 {
00176 Timer tim(1000000);
00177 Dims dims;
00178
00179
00180
00181 const double t = rmsg.getElementDouble();
00182 Image<float> im = rmsg.getElementFloatIma();
00183 Image<byte> cm = rmsg.getElementByteIma();
00184 ch->input(InputFrame::fromGrayFloat(&im, SimTime::SECS(t), &cm));
00185 dims = im.getDims();
00186
00187
00188
00189
00190 TCPmessage msg(rframe, BEO_SCHANOUTPUT);
00191 if (itsQuickMode.getVal() == false)
00192 msg.reset(rframe, BEO_SCHANALLOUT);
00193
00194 msg.addDouble(t);
00195
00196
00197
00198 msg.addImage(ch->getOutput());
00199
00200 if (itsQuickMode.getVal() == false) {
00201
00202
00203
00204 if (ch->hasPyramid()) {
00205 msg.addImageSet(ch->pyramid(0));
00206
00207 uint ns = ch->numSubmaps(); ImageSet<float> submaps(ns);
00208 for (uint i = 0; i < ns; i ++) submaps[i] = ch->getSubmap(i);
00209 msg.addImageSet(submaps);
00210
00211 msg.addImageSet(ch->clipPyramid());
00212 } else {
00213 ImageSet<float> empty;
00214 msg.addImageSet(empty);
00215 msg.addImageSet(empty);
00216 msg.addImageSet(empty);
00217 }
00218 }
00219
00220
00221 itsBeo->send(rnode, msg);
00222 LINFO("%sProcessed %dx%d input %d in %lluus",
00223 itsQuickMode.getVal()?"Quick":"", dims.w(), dims.h(),
00224 rframe, tim.get());
00225 }
00226 }
00227 break;
00228 default:
00229 LERROR("Received unknown message from node %d -- IGNORING", rnode);
00230 }
00231 }
00232
00233
00234 nub::soft_ref<SingleChannel> SingleChannelBeoServer::channel(const int32 node)
00235 {
00236
00237
00238 const std::string name = sformat("%d", node);
00239
00240 nub::soft_ref<SingleChannel> chan;
00241 if (hasSubComponent(name)) dynCastWeakToFrom(chan, subComponent(name));
00242
00243 return chan;
00244 }
00245
00246
00247
00248
00249
00250