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 #include "../include/PCMContext.h"
00030 
00031 #include "../include/PCMProcess.h"
00032 
00033 #include "../include/PCMXDisplay.h"
00034 
00035 #include "../include/PCMHost.h"
00036 
00037 #include "../include/PCMStringConverter.h"
00038 
00039 namespace ParCompMark
00040 {
00041 
00042   
00043   
00044   
00045 
00046   Context::Context()
00047         
00048         
00049         
00050         
00051         
00052         
00053         
00054         
00055         
00056         
00057         
00058         
00059         
00060         
00061         
00062         
00063         
00064         
00065         
00066         
00067         
00068         
00069         
00070   {
00071         Except(INVALID_OPERATION_ERROR, "Context::Context()",
00072            "Default constructor for Squirrel compatibility. Calling this constructor always raises an exception.");
00073   }
00074 
00075  
00076 
00077   Context::Context(Process * parent):
00078         
00079    mParent(parent               
00080         )
00081         
00082         
00083         
00084         
00085         
00086         
00087         
00088         
00089         
00090         
00091         
00092         
00093         
00094         
00095         
00096         
00097         
00098         
00099         
00100         
00101         
00102         
00103         
00104   {
00105         Assert(parent, NULL_POINTER_ERROR, "Context::Context()");
00106 
00107         mUseGLFrameletEXT = true;
00108         mContextType = SLAVE;
00109         mFrameID = 0;
00110         mFrameWidth = 256;
00111         mFrameHeight = 256;
00112         mColourFormat = PC_PF_BGRA8;
00113         mDepthFormat = PC_PF_Z32I;
00114         mPixelFormat = mColourFormat | mDepthFormat;
00115         mCompositeType = PC_COMP_DEPTH;
00116         mCompressionHint = PC_COMPRESSION_NONE;
00117         mRetainOutputCount = 1;
00118         mVolatileFrameletCount = 0;
00119         mOutputDepth = true;
00120         mProcesses = 0;
00121         mProcessCount = -1;
00122         mHostIndex = -1;
00123         mNetworkID = PC_ID_DEFAULT;
00124         mContext = 0;
00125         mInitialized = false;
00126 
00127         Logger::getInstance()->log(Logger::NOTICE,
00128                                  "Context has been created for process `" + mParent->getName() + "\'");
00129   }
00130 
00131  
00132 
00133   Context::~Context()
00134   {
00135         
00136 
00137         if(mInitialized)
00138         {
00139          finalize();
00140         }
00141 
00142         if(mProcesses)
00143         {
00144          for(int i = 0; i < mProcessCount; i++)
00145                 if(mProcesses[i])
00146                  delete mProcesses[i];
00147 
00148          delete mProcesses;
00149         }
00150 
00151         Logger::getInstance()->log(Logger::NOTICE,
00152                                  "Context has been destroyed for process `" + mParent->getName() + "\'");
00153   }
00154 
00155  
00156 
00157   
00158   
00159   
00160 
00161   void Context::setProcesses(const char *processList)
00162   {
00163         std::string processListStr(processList);
00164 
00165         
00166         std::string::size_type index = 0, i = 0;
00167         mProcessCount = 1;
00168 
00169         while((index = processListStr.find(";", index)) != std::string::npos)
00170         {
00171          mProcessCount++;
00172          index++;
00173         }
00174         mProcesses = new PCstring[mProcessCount + 1];
00175 
00176         
00177         while((index = processListStr.find(";")) != std::string::npos)
00178         {
00179          std::string processName = processListStr.substr(0, index);
00180          mProcesses[i] = strdup(processName.c_str());
00181 
00182          processListStr = processListStr.substr(++index);
00183          i++;
00184         }
00185         mProcesses[i] = strdup(processListStr.c_str());
00186 
00187         mProcesses[mProcessCount] = 0;
00188   }
00189 
00190  
00191 
00192   void Context::initialize()
00193   {
00194         Assert(!mInitialized, INVALID_OPERATION_ERROR, "Context::initialize()");
00195 
00196         const int max = 64;
00197 
00198         if(mUseGLFrameletEXT)
00199         {
00200          
00201 #ifndef PC_EXT_CUR_GFX_CTX
00202          Except(INTERNAL_ERROR, "Context::initialize()",
00203                  "PC_EXT_CUR_GFX_CTX extension is not present in your PC interface.");
00204 #endif
00205          
00206 
00212         }
00213 
00214         switch (mContextType)
00215         {
00216                 
00217                 
00218                 
00219          case MASTER:
00220                 {
00221 
00222                  
00223                  int i = 0;
00224                  PCint attribs[max];
00225 
00226                  attribs[i++] = PC_FRAME_WIDTH;
00227                  attribs[i++] = mFrameWidth;
00228                  attribs[i++] = PC_FRAME_HEIGHT;
00229                  attribs[i++] = mFrameHeight;
00230                  attribs[i++] = PC_COMPOSITE_TYPE;
00231                  attribs[i++] = mCompositeType;
00232                  attribs[i++] = PC_PIXEL_FORMAT;
00233                  
00234                  attribs[i++] = (mCompositeType == PC_COMP_ALPHA_SORT ? mColourFormat : mPixelFormat);
00235                  attribs[i++] = PC_COMPRESSION_HINT;
00236                  attribs[i++] = mCompressionHint;
00237                  attribs[i++] = PC_NETWORK_ID;
00238                  attribs[i++] = mNetworkID;
00239                  attribs[i++] = PC_OUTPUT_DEPTH;
00240                  attribs[i++] = (PCint) (mOutputDepth ? 1 : 0);
00241                  attribs[i++] = PC_RETAIN_OUTPUT;
00242                  attribs[i++] = mRetainOutputCount;
00243                  attribs[i++] = PC_VOLATILE_FRAMELET;
00244                  attribs[i++] = mVolatileFrameletCount;
00245                  attribs[i++] = PC_PROPERTY_END;
00246 
00252                  if(PCerr err =
00253                    pcContextCreateMaster(attribs, mProcesses, mProcesses[mProcessIndex], &mContext) != PC_NO_ERROR)
00254                  {
00255                         
00256                         Except(INTERNAL_ERROR, "Context::initialize()", pcGetErrorString(err));
00257                  }
00258                 }
00259 
00260                 
00261 
00262                 break;
00263 
00264                 
00265                 
00266                 
00267          case SLAVE:
00268                 {
00269 
00270                  
00271 
00272                  if(PCerr err = pcContextCreate(mNetworkID, mProcesses[mProcessIndex], &mContext) != PC_NO_ERROR)
00273                  {
00274                         
00275                         Except(INTERNAL_ERROR, "Context::initialize()", pcGetErrorString(err));
00276                  }
00277                  
00278                  
00279 
00280                  Logger::getInstance()->log(Logger::NOTICE,
00281                                            "Slave PC context has been initialized for process `" +
00282                                            mParent->getName() + "\'");
00283                 }
00284         }
00285 
00286         
00287 
00288         
00289         if(PCerr err = pcContextSync(mContext) != PC_NO_ERROR)
00290          Except(INTERNAL_ERROR, "Context::initialize()", pcGetErrorString(err));
00291         mParent->setStartTime(Timer::getSystemTime());
00292 
00293         
00294         if(PCerr err = pcContextGetInteger(mContext, PC_HOSTINDEX, PC_LOCALHOST_INDEX, &mHostIndex) != PC_NO_ERROR)
00295          Except(INTERNAL_ERROR, "Context::initialize()", pcGetErrorString(err));
00296 
00297         
00298         if(mContextType == SLAVE)
00299         {
00300          if(PCerr err = pcContextGetInteger(mContext, PC_NUM_HOSTS, PC_ID_DEFAULT, &mProcessCount) != PC_NO_ERROR)
00301                 Except(INTERNAL_ERROR, "Context::initialize()", pcGetErrorString(err));
00302 
00303          if(PCerr err = pcContextGetInteger(mContext, PC_FRAME_WIDTH, PC_ID_DEFAULT, &mFrameWidth) != PC_NO_ERROR)
00304                 Except(INTERNAL_ERROR, "Context::initialize()", pcGetErrorString(err));
00305 
00306          if(PCerr err = pcContextGetInteger(mContext, PC_FRAME_HEIGHT, PC_ID_DEFAULT, &mFrameHeight) != PC_NO_ERROR)
00307                 Except(INTERNAL_ERROR, "Context::initialize()", pcGetErrorString(err));
00308 
00309          if(PCerr err = pcContextGetInteger(mContext, PC_PIXEL_FORMAT, PC_ID_DEFAULT, &mPixelFormat) != PC_NO_ERROR)
00310                 Except(INTERNAL_ERROR, "Context::initialize()", pcGetErrorString(err));
00311         }
00312 
00313         mInitialized = true;
00314 
00315         
00316         Logger::getInstance()->log(Logger::NOTICE,
00317                                  "Benchmark timer started at " + StringConverter::toString(mParent->getStartTime()) +
00318                                  " for process " + mParent->getName());
00319   }
00320 
00321  
00322 
00323   void Context::finalize()
00324   {
00325         Assert(mInitialized, INVALID_OPERATION_ERROR, "Context::finalize()");
00326 
00327         if(PCerr err = pcContextDestroy(mContext) != PC_NO_ERROR)
00328          Except(INTERNAL_ERROR, "Context::finalize()", pcGetErrorString(err));
00329 
00330         mInitialized = false;
00331 
00332         Logger::getInstance()->log(Logger::DEBUG,
00333                                  "PC context has been finalized for process `" + mParent->getName() + "\'");
00334 
00335   }
00336 
00337  
00338 
00339 }