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