Changeset 3185 in Sophya
- Timestamp:
- Feb 20, 2007, 4:24:29 PM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaProg/Tests/zthr.cc
r3178 r3185 31 31 // --- Structure d'argument pour fonction d'execution dans les threads de test 32 32 typedef struct { 33 int_4 thid ;34 int_4 M ;33 int_4 thid, NTh; 34 int_4 M, VSz; 35 35 } ztarg; 36 36 … … 120 120 ~MTVecPool() { } 121 121 // Renvoie un pointeur de vecteur pour thread tid 122 TVector<int_8>* GetVec(uint_4 tid, uint_4& idx) 123 { 124 if (tid >= _nth) ParmError("MTVecPool::getvec() tid > _nth"); 122 TVector<int_8>* GetVecP(uint_4 tid, uint_4& idx) 123 { 124 if (tid >= _nth) ParmError("MTVecPool::GetVecP() tid > _nth"); 125 //DBG cout << "DBG-GetVecP(tid= " << tid << ")" << endl; 126 if (tid == 0) { 127 mex.lock(); 128 St_VecPool stv; 129 idx = _vecs.size(); 130 stv.vv = _vmx.Column(idx); 131 stv.busy = true; 132 stv.stat = 0; 133 _vecs.push_back(stv); 134 mex.unlock(); 135 //DBG cout << "DBG-GetVecP(tid= " << tid << ") -> Idx=" << idx << " VecSz=" << &(_vecs[idx].vv) << endl; 136 return (&(_vecs[idx].vv)); 137 } 138 else { 139 mex.lock(); 140 bool found = false; 141 while (!found) { 142 for(uint_4 k=0; k<_vecs.size(); k++) { 143 if ( (_vecs[k].stat == tid) && (! _vecs[k].busy) ) { 144 found = true; idx = k; 145 _vecs[k].stat = tid; _vecs[k].busy = true; 146 break; 147 } 148 } 149 if (found) { 150 mex.unlock(); 151 //DBG cout << "DBG-GetVecP(tid= " << tid << ") -> nv=" << hex << rv << dec << endl; 152 return (&(_vecs[idx].vv)); 153 } 154 else { 155 mex.broadcast(); 156 mex.wait(); 157 } 158 } 159 } 160 } 161 // Renvoie un vecteur pour thread tid 162 TVector<int_8> GetVec(uint_4 tid, uint_4& idx) 163 { 164 if (tid >= _nth) ParmError("MTVecPool::GetVec() tid > _nth"); 125 165 //DBG cout << "DBG-GetVec(tid= " << tid << ")" << endl; 126 166 if (tid == 0) { … … 134 174 mex.unlock(); 135 175 //DBG cout << "DBG-GetVec(tid= " << tid << ") -> Idx=" << idx << " VecSz=" << &(_vecs[idx].vv) << endl; 136 return ( &(_vecs[idx].vv));176 return (_vecs[idx].vv); 137 177 } 138 178 else { … … 150 190 mex.unlock(); 151 191 //DBG cout << "DBG-GetVec(tid= " << tid << ") -> nv=" << hex << rv << dec << endl; 152 return ( &(_vecs[idx].vv));192 return (_vecs[idx].vv); 153 193 } 154 194 else { … … 207 247 static MTVecPool* mtvp = NULL; 208 248 249 // --- fonction de test avec synchronisation entre threads en utilisant pointeur de vecteurs 250 void syncp_funzt(void *arg) 251 { 252 ztarg * za = (ztarg *)arg; 253 cout << ">>>> syncp_funzt(ThId=" << za->thid << ") - NVec/NLoop= " << za->M << endl; 254 255 if (mtvp == NULL) 256 throw NullPtrError("syncp_funzt: MTVecPool* mtvp = NULL"); 257 258 int_4 L = za->M; 259 int_4 VS = za->VSz; 260 int_8 p2 = 1; 261 uint_4 k, ii, tid; 262 tid = za->thid; 263 for(k=0; k<tid; k++) p2 *= 2; 264 265 char buff[128]; 266 sprintf(buff, "syncp_funzt(ThId=%d) StarOfLoop", za->thid); 267 PrtTim(buff); 268 uint_4 idx; 269 for(k=0; k<L; k++) { 270 *(mtvp->GetVecP(tid, idx)) += p2; 271 //DBG cout << "DBG-syncp_funzt(tid=" << tid << ", idx=" << idx << endl; 272 mtvp->RetVec(idx); 273 } 274 sprintf(buff, "syncp_funzt(ThId=%d) EndOfLoop", za->thid); 275 PrtTim(buff); 276 return; 277 } 209 278 // --- fonction de test avec synchronisation entre threads 210 279 void sync_funzt(void *arg) … … 217 286 218 287 int_4 L = za->M; 288 int_4 VS = za->VSz; 219 289 int_8 p2 = 1; 220 uint_4 k, tid;290 uint_4 k, ii, tid; 221 291 tid = za->thid; 222 292 for(k=0; k<tid; k++) p2 *= 2; … … 227 297 uint_4 idx; 228 298 for(k=0; k<L; k++) { 229 *(mtvp->GetVec(tid, idx)) += p2;299 mtvp->GetVec(tid, idx) += p2; 230 300 //DBG cout << "DBG-sync_funzt(tid=" << tid << ", idx=" << idx << endl; 231 301 mtvp->RetVec(idx); … … 247 317 static int N = 1; 248 318 static int M = 5; 249 static int VSZ = 128;319 static int VSZ = 32; 250 320 251 321 int main(int narg, char *arg[]) … … 260 330 cout << " select= clk -> Mutex lock count " << endl; 261 331 cout << " select= sync -> Thread synchronisation using ZMutex" << endl; 332 cout << " select= syncp -> Thread synchronisation using ZMutex , Vector pointers" << endl; 262 333 cout << " N= Number of threads (sl/mtx) or CountLock " << endl; 263 334 cout << " LM = Loop limit (sl/sync) or Matrix size (mtx) " << endl; 264 cout << " Sz = Vector size for select=sync (default=256) " << endl;335 cout << " Sz = Vector size for select=sync,syncp (default=32) " << endl; 265 336 return(1); 266 337 } … … 268 339 string sel = arg[1]; 269 340 if ((sel != "sl") && (sel != "mtx") && (sel != "arr") && 270 (sel != "sync") && (sel != " clk")) {341 (sel != "sync") && (sel != "syncp") && (sel != "clk")) { 271 342 cout << "zthr/erreur argument sel (!= sl / mtx / arr / clk) " << endl; 272 343 return 2; … … 287 358 try { 288 359 ResourceUsage res(ResourceUsage::RU_All); 289 if ((sel == "mtx") || (sel == "arr") || (sel == "sl") || (sel == "sync") ) {290 if ( sel == "sync")mtvp = new MTVecPool(N,VSZ,M);360 if ((sel == "mtx") || (sel == "arr") || (sel == "sl") || (sel == "sync") || (sel == "syncp")) { 361 if ( (sel == "sync") || (sel == "syncp")) mtvp = new MTVecPool(N,VSZ,M); 291 362 vector<ztarg *> vza; 292 363 vector<ZThread *> vzth; … … 298 369 // ATTENTION : il faut que le thid = 0 ... N-1 (et pas 1) 299 370 zap->thid = i; zap->M = M; 371 zap->NTh = N; zap->VSz = VSZ; 300 372 vza.push_back(zap); 301 373 if (sel == "mtx") pzt->setAction(mtx_funzt, vza[i]); 302 374 else if (sel == "arr") pzt->setAction(arr_funzt, vza[i]); 303 375 else if (sel == "sync") pzt->setAction(sync_funzt, vza[i]); 376 else if (sel == "syncp") pzt->setAction(syncp_funzt, vza[i]); 304 377 else pzt->setAction(funzt, vza[i]); 305 378 } … … 313 386 for(int i=0; i<N; i++) vzth[i]->join(); 314 387 cout << "***zthr Threads Z1 ... Z" << N << " Finished OK" << endl; 388 cout << " zthr/Resusage: getDataSize() = " << res.getDataSize() << " getStackSize()=" 389 << res.getStackSize() << endl; 315 390 cout << res; 316 cout << " Resu: getDataSize() = " << res.getDataSize() << " getStackSize()="317 << res.getStackSize() << endl;318 391 for(int i=0; i<N; i++) { 319 392 delete vzth[i];
Note:
See TracChangeset
for help on using the changeset viewer.