source: Sophya/trunk/SophyaProg/Tests/zthr.cc@ 3205

Last change on this file since 3205 was 3186, checked in by ansari, 19 years ago

economie memoire zthr.cc (sync/syncp) , Reza 20/02/2007

File size: 11.4 KB
Line 
1#include "sopnamsp.h"
2#include "zthread.h"
3#include "resusage.h"
4
5#include <iostream>
6#include <vector>
7
8#include "tmatrix.h"
9#include "tvector.h"
10#include "tarrinit.h"
11
12#include <stdlib.h>
13#include <stdio.h>
14
15/* -------------------------------------------------
16 Programme de test des classes de threads de SOPHYA
17 SOPHYA::ZThread SOPHYA::ZMutex ...
18 Exemples d'execution
19 csh> time zthr mtx 2 500
20 csh> time zthr arr 2 500
21 csh> time zthr sync 4 1000
22*/
23
24#include <time.h>
25#include <unistd.h>
26
27#include "timing.h"
28#include "ctimer.h"
29
30
31// --- Structure d'argument pour fonction d'execution dans les threads de test
32typedef struct {
33 int_4 thid, NTh;
34 int_4 M, VSz;
35} ztarg;
36
37// --- fonction de test simple avec boucle de sleep
38void funzt(void *arg)
39{
40time_t t0, t1;
41int i;
42
43ztarg * za = (ztarg *)arg;
44
45t0 = time(NULL);
46printf("+++++ funzt(ThId=%d) Entry to funzt (za.M=%d) +++++\n", za->thid, za->M);
47int imax = za->M;
48for(i=0; i<imax; i++)
49 {
50 sleep(3);
51 t1 = time(NULL)-t0;
52 printf("++funzt(ThId=%d) Dt= %d \n", za->thid, (int)t1);
53 }
54
55return;
56}
57
58// --- fonction de test simple avec calcul matriciel
59void mtx_funzt(void *arg)
60{
61 ztarg * za = (ztarg *)arg;
62 cout << ">>>> mtx-funzt(ThId=" << za->thid << ") - Matrix size= " << za->M << endl;
63
64 sa_size_t m = za->M;
65 Matrix a1(m,m), a2(m,m), mxprod;
66 a1 = RandomSequence(RandomSequence::Gaussian, 0., 4.);
67 a2 = RandomSequence(RandomSequence::Gaussian, 0., 3.);
68 char buff[128];
69 sprintf(buff, "mtx-funzt(ThId=%d) EndOfInit", za->thid);
70 PrtTim(buff);
71 mxprod = a1*a2;
72 sprintf(buff, "mtx-funzt(ThId=%d) EndOfMxProd", za->thid);
73 PrtTim(buff);
74 return;
75}
76// --- fonction de test simple avec calcul matriciel
77void arr_funzt(void *arg)
78{
79 ztarg * za = (ztarg *)arg;
80 cout << ">>>> arr-funzt(ThId=" << za->thid << ") - Matrix size= " << za->M << endl;
81
82 sa_size_t m = za->M;
83 TMatrix<int_4> a1(m,m), a2(m,m), ares;
84 a1 = RegularSequence(1.,1.);
85 a2 = RegularSequence(5.,3.);
86 char buff[128];
87 sprintf(buff, "arr-funzt(ThId=%d) EndOfInit", za->thid);
88 PrtTim(buff);
89 ares = 4*a1*12*a2;
90 sprintf(buff, "arr-funzt(ThId=%d) EndOfOper", za->thid);
91 PrtTim(buff);
92 return;
93}
94
95
96// Structure de gestion utilisee par la classe MTVecPool
97typedef struct {
98 bool busy;
99 int stat;
100} St_VecPool;
101
102// -------------------------------------------------------------------
103// Structure de gestion de zones memoire partagee (des vecteurs) entre
104// threads - qui doivent operer successivement sur les vecteurs
105// -------------------------------------------------------------------
106class MTVecPool {
107public:
108 MTVecPool(uint_4 nth, uint_4 vsz, uint_4 nvec)
109 {
110 if (nth > 60) throw ParmError("MTVecPool::MTVecPool() nth > 60");
111 if ((nth < 1) || (vsz < 2))
112 throw ParmError("MTVecPool::MTVecPool() nth<1 OR vsz<2 ");
113 _vmx.SetSize(vsz, nvec);
114 _nth = nth;
115 _vsz = vsz;
116 TVector<int_8> xx(2);
117 for(int k=0; k<nth; k++) _vecp.push_back(xx);
118 cout << "-- MTVecPool(nth=" << nth << ")" << endl;
119 _vmx.Show();
120 }
121 ~MTVecPool() { }
122 // Renvoie un pointeur de vecteur pour thread tid
123 TVector<int_8>* GetVecP(uint_4 tid, uint_4& idx)
124 {
125 if (tid >= _nth) ParmError("MTVecPool::GetVecP() tid > _nth");
126 //DBG cout << "DBG-GetVecP(tid= " << tid << ")" << endl;
127 if (tid == 0) {
128 mex.lock();
129 St_VecPool stv;
130 idx = _vecs.size();
131 _vecp[tid].Share(_vmx.Column(idx));
132 stv.busy = true;
133 stv.stat = 0;
134 _vecs.push_back(stv);
135 mex.unlock();
136 //DBG cout << "DBG-GetVecP(tid= " << tid << ") -> Idx=" << idx << " VecSz=" << &(_vecs[idx].vv) << endl;
137 return (&(_vecp[tid]));
138 }
139 else {
140 mex.lock();
141 bool found = false;
142 while (!found) {
143 for(uint_4 k=0; k<_vecs.size(); k++) {
144 if ( (_vecs[k].stat == tid) && (! _vecs[k].busy) ) {
145 found = true; idx = k;
146 _vecs[k].stat = tid; _vecs[k].busy = true;
147 break;
148 }
149 }
150 if (found) {
151 _vecp[tid].Share(_vmx.Column(idx));
152 mex.unlock();
153 //DBG cout << "DBG-GetVecP(tid= " << tid << ") -> nv=" << hex << rv << dec << endl;
154 return (&(_vecp[tid]));
155 }
156 else {
157 mex.broadcast();
158 mex.wait();
159 }
160 }
161 }
162 }
163 // Renvoie un vecteur pour thread tid
164 TVector<int_8> GetVec(uint_4 tid, uint_4& idx)
165 {
166 if (tid >= _nth) ParmError("MTVecPool::GetVec() tid > _nth");
167 //DBG cout << "DBG-GetVec(tid= " << tid << ")" << endl;
168 if (tid == 0) {
169 mex.lock();
170 St_VecPool stv;
171 idx = _vecs.size();
172 stv.busy = true;
173 stv.stat = 0;
174 _vecs.push_back(stv);
175 mex.unlock();
176 //DBG cout << "DBG-GetVec(tid= " << tid << ") -> Idx=" << idx << " VecSz=" << &(_vecs[idx].vv) << endl;
177 return (_vmx.Column(idx));
178 }
179 else {
180 mex.lock();
181 bool found = false;
182 while (!found) {
183 for(uint_4 k=0; k<_vecs.size(); k++) {
184 if ( (_vecs[k].stat == tid) && (! _vecs[k].busy) ) {
185 found = true; idx = k;
186 _vecs[k].stat = tid; _vecs[k].busy = true;
187 break;
188 }
189 }
190 if (found) {
191 mex.unlock();
192 //DBG cout << "DBG-GetVec(tid= " << tid << ") -> nv=" << hex << rv << dec << endl;
193 return (_vmx.Column(idx));
194 }
195 else {
196 mex.broadcast();
197 mex.wait();
198 }
199 }
200 }
201 }
202
203 // Retourne l'index du vecteur au gestionnaire, qui le marque comme disponible
204 void RetVec(uint_4 idx)
205 {
206 //DBG cout << "DBG-RetVec(idx= " << idx << ")" << endl;
207 ZSync zs(mex, 2);
208 _vecs[idx].busy = false; _vecs[idx].stat++;
209 zs.NOp();
210 }
211
212 // Verifie l'etat memoire de tous les vecteurs et fait des print
213 int Check()
214 {
215 cout << "MTVecPool::Check() NVec=" << _vecs.size() << " VSz="
216 << _vsz << " NThreads=" << _nth << endl;
217 int nerr = 0;
218 int_8 sum = 0;
219 int_8 p2 = 1;
220 int_8 min,max;
221 for(int i=0; i<_nth; i++) { sum += p2; p2 *= 2; }
222 for(uint_4 k=0; k<_vecs.size(); k++) {
223 if ( (_vecs[k].busy) || (_vecs[k].stat != _nth) ) {
224 cout << " Check()/Pb Busy Or Stat for k=" << k << endl;
225 nerr++;
226 }
227 _vmx.Column(k) -= sum;
228 _vmx.Column(k).MinMax(min, max);
229 if ((min!=0) || (max!=0)) {
230 cout << " Check()/Pb vec[k=" << k << "] != (sum=" << sum << ")" << endl;
231 nerr++;
232 }
233 }
234 if (nerr == 0) cout << "MTVecPool::Check() - OK (NErr=0)" << endl;
235 else cout << "MTVecPool::Check() PB NErr=" << nerr << endl;
236 return nerr;
237 }
238
239 // ... variables membres
240 ZMutex mex;
241 uint_4 _vsz;
242 uint_4 _nth;
243 TMatrix<int_8> _vmx;
244 vector< St_VecPool> _vecs;
245 vector< TVector<int_8> > _vecp;
246};
247
248
249static MTVecPool* mtvp = NULL;
250
251// --- fonction de test avec synchronisation entre threads en utilisant pointeur de vecteurs
252void syncp_funzt(void *arg)
253{
254 ztarg * za = (ztarg *)arg;
255 cout << ">>>> syncp_funzt(ThId=" << za->thid << ") - NVec/NLoop= " << za->M << endl;
256
257 if (mtvp == NULL)
258 throw NullPtrError("syncp_funzt: MTVecPool* mtvp = NULL");
259
260 int_4 L = za->M;
261 int_4 VS = za->VSz;
262 int_8 p2 = 1;
263 uint_4 k, ii, tid;
264 tid = za->thid;
265 for(k=0; k<tid; k++) p2 *= 2;
266
267 char buff[128];
268 sprintf(buff, "syncp_funzt(ThId=%d) StarOfLoop", za->thid);
269 PrtTim(buff);
270 uint_4 idx;
271 for(k=0; k<L; k++) {
272 *(mtvp->GetVecP(tid, idx)) += p2;
273 //DBG cout << "DBG-syncp_funzt(tid=" << tid << ", idx=" << idx << endl;
274 mtvp->RetVec(idx);
275 }
276 sprintf(buff, "syncp_funzt(ThId=%d) EndOfLoop", za->thid);
277 PrtTim(buff);
278 return;
279}
280// --- fonction de test avec synchronisation entre threads
281void sync_funzt(void *arg)
282{
283 ztarg * za = (ztarg *)arg;
284 cout << ">>>> sync_funzt(ThId=" << za->thid << ") - NVec/NLoop= " << za->M << endl;
285
286 if (mtvp == NULL)
287 throw NullPtrError("sync_funzt: MTVecPool* mtvp = NULL");
288
289 int_4 L = za->M;
290 int_4 VS = za->VSz;
291 int_8 p2 = 1;
292 uint_4 k, ii, tid;
293 tid = za->thid;
294 for(k=0; k<tid; k++) p2 *= 2;
295
296 char buff[128];
297 sprintf(buff, "sync_funzt(ThId=%d) StarOfLoop", za->thid);
298 PrtTim(buff);
299 uint_4 idx;
300 for(k=0; k<L; k++) {
301 mtvp->GetVec(tid, idx) += p2;
302 //DBG cout << "DBG-sync_funzt(tid=" << tid << ", idx=" << idx << endl;
303 mtvp->RetVec(idx);
304 }
305 sprintf(buff, "sync_funzt(ThId=%d) EndOfLoop", za->thid);
306 PrtTim(buff);
307 return;
308}
309
310class CountLock : public ZMutex {
311 int count;
312public:
313 CountLock() { count = 0; }
314 inline int Count() { lock(); int rc = ++count; unlock(); return(rc);
315 }
316};
317
318
319static int N = 1;
320static int M = 5;
321static int VSZ = 32;
322
323int main(int narg, char *arg[])
324
325{
326
327 if (narg < 4) {
328 cout << " Usage: zthr select N LM [Sz] " << endl;
329 cout << " select= sl -> simple loop with sleep " << endl;
330 cout << " select= mtx -> matrix init and multiply mx1*mx2" << endl;
331 cout << " select= arr -> array/matrix init and operation c1*a1+c2*a2 " << endl;
332 cout << " select= clk -> Mutex lock count " << endl;
333 cout << " select= sync -> Thread synchronisation using ZMutex" << endl;
334 cout << " select= syncp -> Thread synchronisation using ZMutex , Vector pointers" << endl;
335 cout << " N= Number of threads (sl/mtx) or CountLock " << endl;
336 cout << " LM = Loop limit (sl/sync) or Matrix size (mtx) " << endl;
337 cout << " Sz = Vector size for select=sync,syncp (default=32) " << endl;
338 return(1);
339 }
340
341 string sel = arg[1];
342 if ((sel != "sl") && (sel != "mtx") && (sel != "arr") &&
343 (sel != "sync") && (sel != "syncp") && (sel != "clk")) {
344 cout << "zthr/erreur argument sel (!= sl / mtx / arr / clk) " << endl;
345 return 2;
346 }
347
348 //-- Decodage arguments
349 N = atoi(arg[2]);
350 M = atoi(arg[3]);
351 if (narg > 4) VSZ = atoi(arg[4]);
352 cout << "zthr/Info: select=" << sel << " N=" << N << " M= " << M
353 << " VSz=" << VSZ << endl;
354
355
356 InitTim();
357 SophyaInit();
358
359 int rc = 0;
360 try {
361 ResourceUsage res(ResourceUsage::RU_All);
362 if ((sel == "mtx") || (sel == "arr") || (sel == "sl") || (sel == "sync") || (sel == "syncp")) {
363 if ( (sel == "sync") || (sel == "syncp")) mtvp = new MTVecPool(N,VSZ,M);
364 vector<ztarg *> vza;
365 vector<ZThread *> vzth;
366 for(int i=0; i<N; i++) {
367 cout << "*****zthr: Creating Thread " << i+1 << " /" << N << endl;
368 ZThread * pzt = new ZThread();
369 ztarg* zap = new ztarg;
370 vzth.push_back(pzt);
371 // ATTENTION : il faut que le thid = 0 ... N-1 (et pas 1)
372 zap->thid = i; zap->M = M;
373 zap->NTh = N; zap->VSz = VSZ;
374 vza.push_back(zap);
375 if (sel == "mtx") pzt->setAction(mtx_funzt, vza[i]);
376 else if (sel == "arr") pzt->setAction(arr_funzt, vza[i]);
377 else if (sel == "sync") pzt->setAction(sync_funzt, vza[i]);
378 else if (sel == "syncp") pzt->setAction(syncp_funzt, vza[i]);
379 else pzt->setAction(funzt, vza[i]);
380 }
381 cout << "***zthr: Starting threads ... " << endl;
382 PrtTim("***zthr/StarThr");
383 for(int i=0; i<N; i++) vzth[i]->start();
384 sleep(1);
385 cout << "***ResourceUsage before thr[i].join()" << endl;
386 cout << res;
387 cout << "***zthr Joining Threads ..." << endl;
388 for(int i=0; i<N; i++) vzth[i]->join();
389 cout << "***zthr Threads Z1 ... Z" << N << " Finished OK" << endl;
390 cout << " zthr/Resusage: getDataSize() = " << res.getDataSize() << " getStackSize()="
391 << res.getStackSize() << endl;
392 cout << res;
393 for(int i=0; i<N; i++) {
394 delete vzth[i];
395 delete vza[i];
396 }
397 if (mtvp) {
398 Timer tm("MTVecPool::Check()");
399 mtvp->Check();
400 tm.Nop();
401 delete mtvp;
402 }
403 }
404 else {
405 PrtTim("BeginOfCount");
406 CountLock clk;
407 int kk;
408 for(kk=0; kk<atoi(arg[3]); kk++) {
409 clk.Count();
410 }
411 cout << " End CountLock-Test Count= " << clk.Count() << endl;
412 }
413 }
414 catch (PThrowable exc) {
415 cerr << "zthr: catched Exception " << exc.Msg() << endl;
416 rc = 77;
417 }
418 catch (...) {
419 cerr << " catched unknown (...) exception (lpk.cc) " << endl;
420 rc = 78;
421 }
422
423 return(rc);
424
425}
426
427
428
Note: See TracBrowser for help on using the repository browser.