00001
00002
00003
00004
00005
00006 #include "liveViz.h"
00007 #include "liveViz_impl.h"
00008 #include "ImageData.h"
00009
00010 PUPbytes(liveVizConfig)
00011
00012 liveVizConfig lv_config;
00013 CkReduction::reducerType image_combine_reducer;
00014 CProxy_liveVizGroup lvG;
00015 CProxy_LiveVizBoundElement lvBoundArray;
00016 bool usingBoundArray;
00017 CkCallback clientGetImageCallback;
00018
00019
00020 void liveVizInit(const liveVizConfig &cfg, CkArrayID a, CkCallback c)
00021 {
00022 if (CkMyPe()!=0) CkAbort("liveVizInit must be called only on processor 0!");
00023 clientGetImageCallback=c;
00024
00025
00026
00027
00028 usingBoundArray = false;
00029 lvG = CProxy_liveVizGroup::ckNew(cfg);
00030 }
00031
00032
00033 void liveVizInit(const liveVizConfig &cfg, CkArrayID a, CkCallback c, CkArrayOptions &opts)
00034 {
00035 if (CkMyPe()!=0) CkAbort("liveVizInit must be called only on processor 0!");
00036 clientGetImageCallback=c;
00037
00038
00039
00040
00041 usingBoundArray = true;
00042 CkArrayOptions boundOpts;
00043 int dimension = opts.getNumInitial().dimension;
00044 switch(dimension){
00045 case 1: boundOpts.setNumInitial(opts.getNumInitial().data()[0]); break;
00046 case 2: boundOpts.setNumInitial(opts.getNumInitial().data()[0],opts.getNumInitial().data()[1]); break;
00047 case 3: boundOpts.setNumInitial(opts.getNumInitial().data()[0],opts.getNumInitial().data()[1],opts.getNumInitial().data()[2]); break;
00048 default: CmiAbort("Arrays with more than 3 dimensions are not currently supported by liveViz");
00049 }
00050 boundOpts.bindTo(a);
00051 lvBoundArray = CProxy_LiveVizBoundElement::ckNew(boundOpts);
00052 lvG = CProxy_liveVizGroup::ckNew(cfg);
00053 }
00054
00055
00056
00057 void liveVizInitComplete(void *rednMessage) {
00058 delete (CkReductionMsg *)rednMessage;
00059 liveViz0Init(lv_config);
00060 }
00061
00062 liveVizRequestMsg *liveVizRequestMsg::buildNew(const liveVizRequest &req,const void *data,int dataLen)
00063 {
00064 liveVizRequestMsg *m=new (dataLen,0) liveVizRequestMsg();
00065 m->req=req;
00066 memcpy(m->data,data,dataLen);
00067 m->dataLen=dataLen;
00068 return m;
00069 }
00070
00071
00072
00073
00074 void liveViz0Get(const liveVizRequest &req,void *data,int dataLen)
00075 {
00076 clientGetImageCallback.send(liveVizRequestMsg::buildNew(req,data,dataLen));
00077 }
00078
00079
00080
00081
00082
00083 static byte *overflowArray=CkImage::newClip();
00084
00085 #if 1
00086
00087
00088
00089
00090
00091
00092
00093
00094
00097 void liveVizDeposit(const liveVizRequest &req,
00098 int startx, int starty,
00099 int sizex, int sizey, const byte * src,
00100 ArrayElement* client,
00101 liveVizCombine_t combine)
00102 {
00103 if (lv_config.getVerbose(2))
00104 CkPrintf("liveVizDeposit> Deposited image at (%d,%d), (%d x %d) pixels, on pe %d \n",startx,starty,sizex,sizey,CkMyPe());
00105
00106 ImageData imageData (lv_config.getBytesPerPixel ());
00107 CkReductionMsg* msg = CkReductionMsg::buildNew(imageData.GetBuffSize (startx,
00108 starty,
00109 sizex,
00110 sizey,
00111 req.wid,
00112 req.ht,
00113 src),
00114 NULL, image_combine_reducer);
00115 imageData.WriteHeader(combine,&req,(byte*)(msg->getData()));
00116 imageData.AddImage (req.wid, (byte*)(msg->getData()));
00117
00118
00119 msg->setCallback(CkCallback(vizReductionHandler));
00120
00121 if(usingBoundArray){
00122 lvBoundArray[client->thisIndexMax].deposit(msg);
00123 }else {
00124 client->contribute(msg);
00125 }
00126 }
00127
00128
00134 CkReductionMsg *imageCombineReducer(int nMsg,CkReductionMsg **msgs)
00135 {
00136 if (nMsg==1) {
00137 if (lv_config.getVerbose(2))
00138 CkPrintf("imageCombine> Skipping combine on pe %d\n",CkMyPe());
00139
00140 CkReductionMsg *ret=msgs[0];
00141 msgs[0]=NULL;
00142 return ret;
00143 }
00144
00145 if (lv_config.getVerbose(2))
00146 CkPrintf("imageCombine> image combine on pe %d\n",CkMyPe());
00147
00148 ImageData imageData (lv_config.getBytesPerPixel ());
00149
00150 CkReductionMsg* msg = CkReductionMsg::buildNew(imageData.CombineImageDataSize (nMsg,
00151 msgs),
00152 NULL, image_combine_reducer);
00153
00154 imageData.CombineImageData (nMsg, msgs, (byte*)(msg->getData()));
00155 return msg;
00156 }
00157
00158
00159
00160 void vizReductionHandler(void *r_msg)
00161 {
00162 CkReductionMsg *msg = (CkReductionMsg*)r_msg;
00163 ImageData imageData (lv_config.getBytesPerPixel ());
00164 liveVizRequest req;
00165 byte *image = imageData.ConstructImage ((byte*)(msg->getData ()), req);
00166
00167 if (lv_config.getVerbose(2))
00168 CkPrintf("vizReductionHandler> Assembled image on PE %d \n", CkMyPe());
00169
00170 if (lv_config.getBytesPerPixel()!=lv_config.getNetworkBytesPerPixel())
00171 {
00172
00173 int row=req.wid*lv_config.getBytesPerPixel();
00174 int netRow=req.wid*lv_config.getNetworkBytesPerPixel();
00175 byte *netImage=new byte[req.ht*netRow];
00176 liveVizFloatToRGB(req, (float *)image, netImage, req.wid*req.ht);
00177 delete[] image;
00178 image=netImage;
00179 }
00180
00181 liveViz0Deposit(req,image);
00182
00183 delete[] image;
00184 delete msg;
00185 }
00186
00187 static void liveVizNodeInit(void) {
00188 image_combine_reducer=CkReduction::addReducer(imageCombineReducer, false, "imageCombineReducer");
00189 }
00190
00191 #else
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206 CkReductionMsg *allocateImageMsg(const liveVizRequest &req,const CkRect &r,
00207 byte **imgDest)
00208 {
00209 imageHeader hdr(req,r);
00210 CkReductionMsg *msg=CkReductionMsg::buildNew(
00211 sizeof(imageHeader)+r.area()*lv_config.getBytesPerPixel(),
00212 NULL,imageCombineReducer);
00213 byte *dest=(byte *)msg->getData();
00214 *(imageHeader *)dest=hdr;
00215 *imgDest=dest+sizeof(hdr);
00216 return msg;
00217 }
00218
00219
00220 void liveVizDeposit(const liveVizRequest &req,
00221 int startx, int starty,
00222 int sizex, int sizey, const byte * src,
00223 ArrayElement* client)
00224 {
00225 if (lv_config.getVerbose(2))
00226 CkPrintf("liveVizDeposit> Deposited image at (%d,%d), (%d x %d) pixels, on pe %d\n",
00227 startx,starty,sizex,sizey,CkMyPe());
00228
00229
00230 CkRect r(startx,starty, startx+sizex,starty+sizey);
00231 r=r.getIntersect(CkRect(req.wid,req.ht));
00232 if (r.isEmpty()) r.zero();
00233 int bpp=lv_config.getBytesPerPixel();
00234 byte *dest;
00235 CkReductionMsg *msg=allocateImageMsg(req,r,&dest);
00236
00237
00238 if (!r.isEmpty()) {
00239
00240 CkImage srcImage(sizex,sizey,bpp,(byte *)src);
00241 srcImage.window(r.getShift(-startx,-starty));
00242 CkImage destImage(r.wid(),r.ht(),bpp,dest);
00243 destImage.put(0,0,srcImage);
00244 }
00245
00246
00247 msg->setCallback(CkCallback(vizReductionHandler));
00248
00249 client->contribute(msg);
00250 }
00251
00252
00253
00254
00255 CkReductionMsg *imageCombine(int nMsg,CkReductionMsg **msgs)
00256 {
00257 if (nMsg==1) {
00258 if (lv_config.getVerbose(2))
00259 CkPrintf("imageCombine> Skipping combine on pe %d\n",CkMyPe());
00260 CkReductionMsg *ret=msgs[0];
00261 msgs[0]=NULL;
00262 return ret;
00263 }
00264 int m;
00265 int bpp=lv_config.getBytesPerPixel();
00266 imageHeader *firstHdr=(imageHeader *)msgs[0]->getData();
00267
00268
00269 CkRect destRect; destRect.makeEmpty();
00270 for (m=0;m<nMsg;m++) destRect=destRect.getUnion(((imageHeader *)msgs[m]->getData())->r);
00271
00272
00273 byte *dest;
00274 CkReductionMsg *msg=allocateImageMsg(firstHdr->req,destRect,&dest);
00275
00276
00277
00278
00279 CkImage destImage(destRect.wid(),destRect.ht(),bpp,dest);
00280 destImage.clear();
00281 for (m=0;m<nMsg;m++) {
00282 byte *src=(byte *)msgs[m]->getData();
00283 imageHeader *mHdr=(imageHeader *)src;
00284 src+=sizeof(imageHeader);
00285 if (lv_config.getVerbose(2))
00286 CkPrintf("imageCombine> pe %d image %d is (%d,%d, %d,%d)\n",
00287 CkMyPe(),m,mHdr->r.l,mHdr->r.t,mHdr->r.r,mHdr->r.b);
00288 CkImage srcImage(mHdr->r.wid(),mHdr->r.ht(),bpp,src);
00289 destImage.addClip(mHdr->r.l-destRect.l,mHdr->r.t-destRect.t,srcImage,overflowArray);
00290 }
00291
00292 return msg;
00293 }
00294
00295
00296
00297
00298
00299 void vizReductionHandler(void *r_msg)
00300 {
00301 CkReductionMsg *msg = (CkReductionMsg*)r_msg;
00302 imageHeader *hdr=(imageHeader *)msg->getData();
00303 byte *srcData=sizeof(imageHeader)+(byte *)msg->getData();
00304 int bpp=lv_config.getBytesPerPixel();
00305 CkRect destRect(0,0,hdr->req.wid,hdr->req.ht);
00306 if (destRect==hdr->r) {
00307 liveViz0Deposit(hdr->req,srcData);
00308 }
00309 else
00310 {
00311 CkImage src(hdr->r.wid(),hdr->r.ht(),bpp,srcData);
00312 CkAllocImage dest(hdr->req.wid,hdr->req.ht,bpp);
00313 dest.clear();
00314 dest.put(hdr->r.l,hdr->r.t,src);
00315 liveViz0Deposit(hdr->req,dest.getData());
00316 }
00317 delete msg;
00318 }
00319 #endif
00320
00321
00322 #include "liveViz.def.h"