Changeset 3392 in Sophya for trunk/SophyaLib
- Timestamp:
- Nov 22, 2007, 7:25:40 PM (18 years ago)
- Location:
- trunk/SophyaLib/HiStats
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/HiStats/basedtable.cc
r3181 r3392 1 1 #include "basedtable.h" 2 2 #include <ctype.h> 3 #include "sopnamsp.h"4 3 #include "pexceptions.h" 4 #include "thsafeop.h" 5 6 namespace SOPHYA { 5 7 6 8 /*! 7 \class SOPHYA::DataTableRow9 \class DataTableRow 8 10 \ingroup HiStats 9 11 This class is intented to be used with datatable classes … … 58 60 59 61 /*! 60 \class SOPHYA::BaseDataTable62 \class BaseDataTable 61 63 \ingroup HiStats 62 64 Base class for data tables. Each line represent a record 63 65 and the column contains a given data type. 66 \warning 67 Thread safe fill operation can be activated using the method SetThreadSafe() 68 Default mode is NON thread-safe fill. 64 69 */ 65 70 … … 147 152 mVarMTV = NULL; 148 153 mInfo = NULL; 154 mThS = NULL; 149 155 } 150 156 … … 154 160 if (mVarMTV) delete[] mVarMTV; 155 161 if (mInfo) delete mInfo; 156 } 162 if (mThS) delete mThS; 163 } 164 165 /*! 166 \brief Activate or deactivate thread-safe \b AddRow() operations 167 168 If fg==true, create an ThSafeOp object in order to insure atomic AddRow() 169 and GetRow()/GetColumn() operations. if fg==false, the ThSafeOp object (mThS) 170 of the target DataTable is destroyed. 171 172 When activated, the following operations are thread-safe : 173 174 - AddRow(const r_8* data) 175 - AddRow(const MuTyV* data) 176 - AddRow(DataTableRow const& data) 177 - GetRow(sa_size_t n, DataTableRow& row) 178 - GetRow(sa_size_t n, MuTyV* mtvp) 179 - GetColumnD(sa_size_t k) 180 181 \warning The default AddRow() operation mode for DataTables is NOT thread-safe. 182 Please note also that the thread-safety state is NOt saved to PPF (or FITS) streams. 183 */ 184 void BaseDataTable::SetThreadSafe(bool fg) 185 { 186 if (fg) { 187 if (mThS) return; 188 else mThS = new ThSafeOp(); 189 } 190 else { 191 if (mThS) delete mThS; 192 mThS = NULL; 193 } 194 } 195 196 197 /*! \cond 198 \class DT_TSOP_SYNC 199 \ingroup HiStats 200 Classe utilitaire pour faciliter la gestion de lock pour 201 operations thread-safe BaseDataTable 202 */ 203 class DT_TSOP_SYNC { 204 public: 205 explicit DT_TSOP_SYNC(ThSafeOp* ths) 206 { 207 ths_ = ths; 208 if (ths_) ths_->lock(); 209 } 210 ~DT_TSOP_SYNC() 211 { 212 if (ths_) ths_->unlock(); 213 } 214 inline ThSafeOp* NOp() { return ths_; } 215 protected: 216 ThSafeOp* ths_; 217 }; 218 /*! \endcond */ 157 219 158 220 sa_size_t BaseDataTable::CopyStructure(BaseDataTable const & a) … … 273 335 sa_size_t BaseDataTable::AddRow(const r_8* data) 274 336 { 337 DT_TSOP_SYNC dttss(mThS); dttss.NOp(); // Thread-safe operation 338 275 339 if (NVar() == 0) 276 340 throw ParmError("BaseDataTable::AddRow(const r_8*) Table has no column !"); … … 309 373 sa_size_t BaseDataTable::AddRow(const MuTyV* data) 310 374 { 375 DT_TSOP_SYNC dttss(mThS); dttss.NOp(); // Thread-safe operation 376 311 377 if (NVar() == 0) 312 378 throw ParmError("BaseDataTable::AddRow(const MuTyV*) Table has no column !"); … … 379 445 Return a reference to the input \b row object. 380 446 Generate an exception if the input \b row object has the wrong size. 381 This method is slower(less efficient) than the GetRow(n) method.382 447 */ 383 448 DataTableRow& BaseDataTable::GetRow(sa_size_t n, DataTableRow& row) const … … 385 450 if ( row.Size() != NCols() ) 386 451 throw SzMismatchError(" BaseDataTable::GetRow(n, row) - row.Size() != NCols() "); 387 MuTyV* rmtv = GetRow(n); 388 for(sa_size_t k=0; k<NCols(); k++) 389 row[k] = rmtv[k]; 452 GetRow(n, row.MTVPtr()); 390 453 return row; 391 454 } 392 455 393 MuTyV* BaseDataTable::GetRow(sa_size_t n) const 394 { 456 /*! 457 For thread-safe operation, specify a valid \b mtvp pointer (!= NULL) 458 */ 459 MuTyV* BaseDataTable::GetRow(sa_size_t n, MuTyV* mtvp) const 460 { 461 DT_TSOP_SYNC dttss(mThS); dttss.NOp(); // Thread-safe operation 462 395 463 if ((n < 0) || (n >= NEntry())) 396 464 throw RangeCheckError("BaseDataTable::GetRow() out of range line index n"); 397 if (mVarMTV == NULL) mVarMTV = new MuTyV[NVar()]; 398 465 if (mtvp == NULL) { 466 if (mVarMTV == NULL) mVarMTV = new MuTyV[NVar()]; 467 mtvp = mVarMTV; 468 } 399 469 sa_size_t bid = n/mSegSz; 400 470 sa_size_t off = n%mSegSz; 401 471 for(sa_size_t k=0; k<mIColsP.size(); k++) 402 m VarMTV[mIColIdx[k]] = mIColsP[k]->GetCstSegment(bid)[off];472 mtvp[mIColIdx[k]] = mIColsP[k]->GetCstSegment(bid)[off]; 403 473 for(sa_size_t k=0; k<mLColsP.size(); k++) 404 m VarMTV[mLColIdx[k]] = mLColsP[k]->GetCstSegment(bid)[off];474 mtvp[mLColIdx[k]] = mLColsP[k]->GetCstSegment(bid)[off]; 405 475 for(sa_size_t k=0; k<mFColsP.size(); k++) 406 m VarMTV[mFColIdx[k]] = mFColsP[k]->GetCstSegment(bid)[off];476 mtvp[mFColIdx[k]] = mFColsP[k]->GetCstSegment(bid)[off]; 407 477 for(sa_size_t k=0; k<mDColsP.size(); k++) { 408 478 if (GetColumType(mDColIdx[k]) == DateTimeField) 409 m VarMTV[mDColIdx[k]] = TimeStamp(mDColsP[k]->GetCstSegment(bid)[off]);410 else m VarMTV[mDColIdx[k]] = mDColsP[k]->GetCstSegment(bid)[off];479 mtvp[mDColIdx[k]] = TimeStamp(mDColsP[k]->GetCstSegment(bid)[off]); 480 else mtvp[mDColIdx[k]] = mDColsP[k]->GetCstSegment(bid)[off]; 411 481 } 412 482 for(sa_size_t k=0; k<mYColsP.size(); k++) 413 m VarMTV[mYColIdx[k]] = mYColsP[k]->GetCstSegment(bid)[off];483 mtvp[mYColIdx[k]] = mYColsP[k]->GetCstSegment(bid)[off]; 414 484 for(sa_size_t k=0; k<mZColsP.size(); k++) 415 m VarMTV[mZColIdx[k]] = mZColsP[k]->GetCstSegment(bid)[off];485 mtvp[mZColIdx[k]] = mZColsP[k]->GetCstSegment(bid)[off]; 416 486 for(sa_size_t k=0; k<mSColsP.size(); k++) 417 m VarMTV[mSColIdx[k]] = mSColsP[k]->GetCstSegment(bid)[off];418 419 return m VarMTV;487 mtvp[mSColIdx[k]] = mSColsP[k]->GetCstSegment(bid)[off]; 488 489 return mtvp; 420 490 } 421 491 … … 424 494 TVector<r_8> BaseDataTable::GetColumnD(sa_size_t k) const 425 495 { 496 DT_TSOP_SYNC dttss(mThS); dttss.NOp(); // Thread-safe operation 497 426 498 if ((k < 0) || (k >= NVar())) 427 499 throw RangeCheckError("BaseDataTable::GetColumnD() out of range column index k"); … … 968 1040 } 969 1041 1042 } // FIN namespace SOPHYA 1043 -
trunk/SophyaLib/HiStats/basedtable.h
r2962 r3392 63 63 { row.Print(s); return(s); } 64 64 65 // Forward class declaration for Thread-safe operations 66 class ThSafeOp; 67 65 68 //! Interface definition for classes handling data in a table. 66 69 class BaseDataTable : public AnyDataObj , public NTupleInterface { … … 81 84 BaseDataTable(sa_size_t segsz=512); 82 85 virtual ~BaseDataTable(); 86 87 // Activate/deactivate thread-safe AddRow()/GetRow() operation 88 virtual void SetThreadSafe(bool fg=true); 89 //! Return true if AddRow()/GetRow() operations are thread-safe 90 inline bool IsThreadSafe() const { return ((mThS)?true:false); } 83 91 84 92 //! Adds a column holding integer, named \b cnom … … 193 201 virtual DataTableRow& GetRow(sa_size_t n, DataTableRow& row) const ; 194 202 //! Return the information stored in line (row) \b n of the table 195 virtual MuTyV * GetRow(sa_size_t n ) const ;203 virtual MuTyV * GetRow(sa_size_t n, MuTyV* mtvp=NULL) const ; 196 204 //! Return the information stored in line \b n of the table. Alias of GetLine 197 205 inline MuTyV * GetLine(sa_size_t n) const … … 316 324 std::vector< SegDBInterface<string> * > mSColsP; 317 325 std::vector< sa_size_t > mSColIdx; 326 327 ThSafeOp* mThS; // != NULL -> Thread safe operations 318 328 319 329 }; -
trunk/SophyaLib/HiStats/datatable.cc
r2849 r3392 1 1 #include "datatable.h" 2 #include "sopnamsp.h"3 2 #include "strutil.h" 4 3 #include "pexceptions.h" 5 4 #include "fiosegdb.h" 6 5 #include "thsafeop.h" 6 7 namespace SOPHYA { 7 8 8 9 /*! 9 \class SOPHYA::DataTable10 \class DataTable 10 11 \ingroup HiStats 11 12 This class can be used to organize data in table (row-column) form. … … 61 62 The copy constructur shares the data if \b share=true. 62 63 Otherwise, the Clone() method is called to make a complete copy. 64 This constructor copies also the thread safety state from \b a . 63 65 */ 64 66 DataTable::DataTable(DataTable const & a, bool share) … … 71 73 if (a.mInfo) mInfo = new DVList(*(a.mInfo)); 72 74 } 73 //! Copy the table structure from \b a and shares the data (columns content) 75 /*! 76 Copy the table structure from \b a and shares the data (columns content). 77 The tread-safety state is copied from \b a . 78 */ 74 79 void DataTable::Share(DataTable const & a) 75 80 { 76 81 // On copie la structure de table 77 82 CopyStructure(a); 83 if (a.IsThreadSafe()) SetThreadSafe(true); 84 else SetThreadSafe(false); 78 85 // Et on partage les donnees des colonnes 79 86 for (size_t kk=0; kk<mNames.size(); kk++) { … … 115 122 // On copie la structure de table 116 123 CopyStructure(a); 117 // Et on partage les donnees des colonnes 124 if (a.IsThreadSafe()) SetThreadSafe(true); 125 else SetThreadSafe(false); 126 // Et on copie les donnees des colonnes 118 127 for (size_t kk=0; kk<mNames.size(); kk++) { 119 128 sa_size_t sk = mNames[kk].ser; … … 161 170 if (mInfo) delete mInfo; 162 171 mInfo = NULL; 172 if (mThS) delete mThS; 173 mThS = NULL; 163 174 mMin.clear(); 164 175 mMax.clear(); … … 278 289 } 279 290 291 } // FIN namespace SOPHYA -
trunk/SophyaLib/HiStats/ntuple.cc
r3236 r3392 5 5 #include "perrors.h" 6 6 #include "ntuple.h" 7 #include "thsafeop.h" 7 8 8 9 namespace SOPHYA { … … 17 18 Simple NTuple class (a Table or 2-D data set) with double or 18 19 single precision floating point value columns. 20 \warning 21 Thread safe fill operation can be activated using the method SetThreadSafe() 22 Default mode is NON thread-safe fill. 19 23 \sa SOPHYA::ObjFileIO<NTuple> 20 24 … … 65 69 mFgDouble = true; 66 70 mInfo = NULL; 71 mThS = NULL; 67 72 } 68 73 … … 89 94 mVarD = NULL; 90 95 mInfo = NULL; 96 mThS = NULL; 91 97 if (nvar <= 0) throw ParmError("NTuple::NTuple(nvar<=0) "); 92 98 mNVar = nvar; … … 126 132 mVarD = NULL; 127 133 mInfo = NULL; 134 mThS = NULL; 128 135 int nvar = noms.size(); 129 136 if (nvar <= 0) throw ParmError("NTuple::NTuple(nvar<=0) "); … … 150 157 } 151 158 152 //! Copy constructor - Copies the table definition and associated data 159 /*! 160 Copy constructor - Copies the table definition and associated data, 161 as well as the tread safety state 162 */ 153 163 // cmv 8/10/99 154 164 //++ … … 158 168 //-- 159 169 : mNVar(0), mNEnt(0), mBlk(0), mNBlk(0) 160 , mVar(NULL), mVarD(NULL), mInfo(NULL)170 , mVar(NULL), mVarD(NULL), mInfo(NULL),mThS(NULL) 161 171 { 162 172 if(NT.mNVar<=0) return; // cas ou NT est cree par defaut … … 185 195 186 196 if(NT.mInfo!=NULL) {mInfo = new DVList; *mInfo = *(NT.mInfo);} 187 197 mThS = NULL; 198 if(NT.mThS!=NULL) SetThreadSafe(true); 188 199 return; 189 200 } 190 201 191 /* --Methode-- 192 //! Constructor with table initialized from a PPF file 193 //++ 194 NTuple::NTuple(char *flnm) 195 // 196 // Createur lecture fichier ppersist. 197 //-- 198 { 199 mNVar = mNEnt = mBlk = mNBlk = 0; 200 mVar = NULL; 201 mVarD = NULL; 202 mNames = NULL; 203 mInfo = NULL; 204 PInPersist s(flnm); 205 ObjFileIO<NTuple> fiont(this); 206 fiont.Read(s); 207 } 202 203 /* --Methode-- */ 204 NTuple::~NTuple() 205 { 206 Clean(); 207 } 208 /* --Methode-- */ 209 /*! 210 \brief Activate or deactivate thread-safe \b Fill() operations 211 If fg==true, create an ThSafeOp object in order to insure atomic Fill() 212 operations. if fg==false, the ThSafeOp object (mThS) of the target NTuple 213 is destroyed. 214 \warning The default Fill() operation mode for NTuples is NOT thread-safe. 215 Please note also that the thread-safety state is NOt saved to PPF (or FITS) streams. 208 216 */ 209 210 /* --Methode-- */ 211 NTuple::~NTuple() 212 { 213 Clean(); 217 void NTuple::SetThreadSafe(bool fg) 218 { 219 if (fg) { 220 if (mThS) return; 221 else mThS = new ThSafeOp(); 222 } 223 else { 224 if (mThS) delete mThS; 225 mThS = NULL; 226 } 214 227 } 215 228 … … 221 234 if (mVarD) delete[] mVarD; 222 235 if (mInfo) delete mInfo; 236 if (mThS) delete mThS; 223 237 int i; 224 238 if(mNBlk>0) { … … 232 246 mVarD = NULL; 233 247 mInfo = NULL; 248 mThS = NULL; 234 249 return; 235 250 } … … 283 298 //-- 284 299 { 300 if (mThS) mThS->lock(); // Thread-safe operation 285 301 int numb = mNEnt/mBlk; 286 302 if (numb >= mNBlk) { … … 302 318 else memcpy((mPtr[numb]+offb*mNVar), x, mNVar*sizeof(r_4)); 303 319 mNEnt++; 320 if (mThS) mThS->unlock(); // Thread-safe operation 304 321 return; 305 322 } … … 316 333 //-- 317 334 { 335 if (mThS) mThS->lock(); // Thread-safe operation 318 336 int numb = mNEnt/mBlk; 319 337 if (numb >= mNBlk) { … … 337 355 338 356 mNEnt++; 357 if (mThS) mThS->unlock(); // Thread-safe operation 339 358 return; 340 359 } -
trunk/SophyaLib/HiStats/ntuple.h
r3205 r3392 21 21 // Forward class declaration for Fits handler 22 22 class FITS_NTuple; 23 // Forward class declaration for Thread-safe operations 24 class ThSafeOp; 23 25 24 26 //! Class for creation and management of double or float data sets in a table … … 31 33 NTuple(const NTuple& NT); 32 34 virtual ~NTuple(); 35 36 // Activate/deactivate thread-safe Fill operation 37 void SetThreadSafe(bool fg=true); 38 //! Return true if Fill operations are thread-safe 39 inline bool IsThreadSafe() const { return ((mThS)?true:false); } 33 40 34 41 NTuple& operator = (const NTuple& NT); … … 102 109 DVList* mInfo; // Infos (variables) attachees au NTuple 103 110 111 ThSafeOp* mThS; // != NULL -> Thread safe operations 104 112 }; 105 113 -
trunk/SophyaLib/HiStats/swppfdtable.cc
r3031 r3392 1 1 #include "swppfdtable.h" 2 #include "sopnamsp.h"3 2 #include "pexceptions.h" 4 3 #include "thsafeop.h" 4 5 namespace SOPHYA { 5 6 6 7 /*! 7 \class S OPHYA::SwPPFDataTable8 \class SwPPFDataTable 8 9 \ingroup HiStats 9 10 This class can be used to organize data in table (row-column) form. … … 96 97 } 97 98 98 //! copy constructor - shares the data 99 //! copy constructor - shares the data (and copies the thread safety state) 100 99 101 SwPPFDataTable::SwPPFDataTable(SwPPFDataTable const & a) 100 102 : BaseDataTable(a.SegmentSize()), … … 137 139 // On copie la structure de table 138 140 CopyStructure(a); 141 if (a.IsThreadSafe()) SetThreadSafe(true); 142 else SetThreadSafe(false); 139 143 140 144 // … … 229 233 if (mInfo) delete mInfo; 230 234 mInfo = NULL; 235 if (mThS) delete mThS; 236 mThS = NULL; 231 237 mMin.clear(); 232 238 mMax.clear(); … … 355 361 // On est oblige de calculer les min-max lors du remplissage 356 362 // On ne peut pas en effet 'relire' le swap pendant l'ecriture 363 if (mThS) mThS->lock(); // tread-safety 357 364 for(sa_size_t k=0; k<NVar(); k++) { 358 365 double x = data[k]; … … 361 368 mMinMaxNEnt[k]++; 362 369 } 370 if (mThS) mThS->unlock(); // tread-safety 363 371 return BaseDataTable::AddRow(data); 364 372 } … … 373 381 // On est oblige de calculer les min-max lors du remplissage 374 382 // On ne peut pas en effet 'relire' le swap pendant l'ecriture 383 if (mThS) mThS->lock(); // tread-safety 375 384 for(sa_size_t k=0; k<NVar(); k++) { 376 385 double x = (double)data[k]; … … 379 388 mMinMaxNEnt[k]++; 380 389 } 390 if (mThS) mThS->unlock(); // tread-safety 381 391 return BaseDataTable::AddRow(data); 382 392 } … … 394 404 return AddRow(data.MTVPtr()); 395 405 } 406 407 } // FIN namespace SOPHYA
Note:
See TracChangeset
for help on using the changeset viewer.