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

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

extension programme zthr.cc (sync/syncp) , Reza 20/02/2007

File size: 11.3 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 TVector<int_8> vv;
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// -------------------------------------------------------------------
107class MTVecPool {
108public:
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 cout << "-- MTVecPool(nth=" << nth << ")" << endl;
118 _vmx.Show();
119 }
120 ~MTVecPool() { }
121 // Renvoie un pointeur de vecteur pour thread tid
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");
165 //DBG cout << "DBG-GetVec(tid= " << tid << ")" << endl;
166 if (tid == 0) {
167 mex.lock();
168 St_VecPool stv;
169 idx = _vecs.size();
170 stv.vv = _vmx.Column(idx);
171 stv.busy = true;
172 stv.stat = 0;
173 _vecs.push_back(stv);
174 mex.unlock();
175 //DBG cout << "DBG-GetVec(tid= " << tid << ") -> Idx=" << idx << " VecSz=" << &(_vecs[idx].vv) << endl;
176 return (_vecs[idx].vv);
177 }
178 else {
179 mex.lock();
180 bool found = false;
181 while (!found) {
182 for(uint_4 k=0; k<_vecs.size(); k++) {
183 if ( (_vecs[k].stat == tid) && (! _vecs[k].busy) ) {
184 found = true; idx = k;
185 _vecs[k].stat = tid; _vecs[k].busy = true;
186 break;
187 }
188 }
189 if (found) {
190 mex.unlock();
191 //DBG cout << "DBG-GetVec(tid= " << tid << ") -> nv=" << hex << rv << dec << endl;
192 return (_vecs[idx].vv);
193 }
194 else {
195 mex.broadcast();
196 mex.wait();
197 }
198 }
199 }
200 }
201
202 // Retourne l'index du vecteur au gestionnaire, qui le marque comme disponible
203 void RetVec(uint_4 idx)
204 {
205 //DBG cout << "DBG-RetVec(idx= " << idx << ")" << endl;
206 ZSync zs(mex, 2);
207 _vecs[idx].busy = false; _vecs[idx].stat++;
208 zs.NOp();
209 }
210
211 // Verifie l'etat memoire de tous les vecteurs et fait des print
212 int Check()
213 {
214 cout << "MTVecPool::Check() NVec=" << _vecs.size() << " VSz="
215 << _vsz << " NThreads=" << _nth << endl;
216 int nerr = 0;
217 int_8 sum = 0;
218 int_8 p2 = 1;
219 int_8 min,max;
220 for(int i=0; i<_nth; i++) { sum += p2; p2 *= 2; }
221 for(uint_4 k=0; k<_vecs.size(); k++) {
222 if ( (_vecs[k].busy) || (_vecs[k].stat != _nth) ) {
223 cout << " Check()/Pb Busy Or Stat for k=" << k << endl;
224 nerr++;
225 }
226 _vecs[k].vv -= sum;
227 _vecs[k].vv.MinMax(min, max);
228 if ((min!=0) || (max!=0)) {
229 cout << " Check()/Pb vec[k=" << k << "] != (sum=" << sum << ")" << endl;
230 nerr++;
231 }
232 }
233 if (nerr == 0) cout << "MTVecPool::Check() - OK (NErr=0)" << endl;
234 else cout << "MTVecPool::Check() PB NErr=" << nerr << endl;
235 return nerr;
236 }
237
238 // ... variables membres
239 ZMutex mex;
240 uint_4 _vsz;
241 uint_4 _nth;
242 TMatrix<int_8> _vmx;
243 vector< St_VecPool> _vecs;
244};
245
246
247static MTVecPool* mtvp = NULL;
248
249// --- fonction de test avec synchronisation entre threads en utilisant pointeur de vecteurs
250void 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}
278// --- fonction de test avec synchronisation entre threads
279void sync_funzt(void *arg)
280{
281 ztarg * za = (ztarg *)arg;
282 cout << ">>>> sync_funzt(ThId=" << za->thid << ") - NVec/NLoop= " << za->M << endl;
283
284 if (mtvp == NULL)
285 throw NullPtrError("sync_funzt: MTVecPool* mtvp = NULL");
286
287 int_4 L = za->M;
288 int_4 VS = za->VSz;
289 int_8 p2 = 1;
290 uint_4 k, ii, tid;
291 tid = za->thid;
292 for(k=0; k<tid; k++) p2 *= 2;
293
294 char buff[128];
295 sprintf(buff, "sync_funzt(ThId=%d) StarOfLoop", za->thid);
296 PrtTim(buff);
297 uint_4 idx;
298 for(k=0; k<L; k++) {
299 mtvp->GetVec(tid, idx) += p2;
300 //DBG cout << "DBG-sync_funzt(tid=" << tid << ", idx=" << idx << endl;
301 mtvp->RetVec(idx);
302 }
303 sprintf(buff, "sync_funzt(ThId=%d) EndOfLoop", za->thid);
304 PrtTim(buff);
305 return;
306}
307
308class CountLock : public ZMutex {
309 int count;
310public:
311 CountLock() { count = 0; }
312 inline int Count() { lock(); int rc = ++count; unlock(); return(rc);
313 }
314};
315
316
317static int N = 1;
318static int M = 5;
319static int VSZ = 32;
320
321int main(int narg, char *arg[])
322
323{
324
325 if (narg < 4) {
326 cout << " Usage: zthr select N LM [Sz] " << endl;
327 cout << " select= sl -> simple loop with sleep " << endl;
328 cout << " select= mtx -> matrix init and multiply mx1*mx2" << endl;
329 cout << " select= arr -> array/matrix init and operation c1*a1+c2*a2 " << endl;
330 cout << " select= clk -> Mutex lock count " << endl;
331 cout << " select= sync -> Thread synchronisation using ZMutex" << endl;
332 cout << " select= syncp -> Thread synchronisation using ZMutex , Vector pointers" << endl;
333 cout << " N= Number of threads (sl/mtx) or CountLock " << endl;
334 cout << " LM = Loop limit (sl/sync) or Matrix size (mtx) " << endl;
335 cout << " Sz = Vector size for select=sync,syncp (default=32) " << endl;
336 return(1);
337 }
338
339 string sel = arg[1];
340 if ((sel != "sl") && (sel != "mtx") && (sel != "arr") &&
341 (sel != "sync") && (sel != "syncp") && (sel != "clk")) {
342 cout << "zthr/erreur argument sel (!= sl / mtx / arr / clk) " << endl;
343 return 2;
344 }
345
346 //-- Decodage arguments
347 N = atoi(arg[2]);
348 M = atoi(arg[3]);
349 if (narg > 4) VSZ = atoi(arg[4]);
350 cout << "zthr/Info: select=" << sel << " N=" << N << " M= " << M
351 << " VSz=" << VSZ << endl;
352
353
354 InitTim();
355 SophyaInit();
356
357 int rc = 0;
358 try {
359 ResourceUsage res(ResourceUsage::RU_All);
360 if ((sel == "mtx") || (sel == "arr") || (sel == "sl") || (sel == "sync") || (sel == "syncp")) {
361 if ( (sel == "sync") || (sel == "syncp")) mtvp = new MTVecPool(N,VSZ,M);
362 vector<ztarg *> vza;
363 vector<ZThread *> vzth;
364 for(int i=0; i<N; i++) {
365 cout << "*****zthr: Creating Thread " << i+1 << " /" << N << endl;
366 ZThread * pzt = new ZThread();
367 ztarg* zap = new ztarg;
368 vzth.push_back(pzt);
369 // ATTENTION : il faut que le thid = 0 ... N-1 (et pas 1)
370 zap->thid = i; zap->M = M;
371 zap->NTh = N; zap->VSz = VSZ;
372 vza.push_back(zap);
373 if (sel == "mtx") pzt->setAction(mtx_funzt, vza[i]);
374 else if (sel == "arr") pzt->setAction(arr_funzt, vza[i]);
375 else if (sel == "sync") pzt->setAction(sync_funzt, vza[i]);
376 else if (sel == "syncp") pzt->setAction(syncp_funzt, vza[i]);
377 else pzt->setAction(funzt, vza[i]);
378 }
379 cout << "***zthr: Starting threads ... " << endl;
380 PrtTim("***zthr/StarThr");
381 for(int i=0; i<N; i++) vzth[i]->start();
382 sleep(1);
383 cout << "***ResourceUsage before thr[i].join()" << endl;
384 cout << res;
385 cout << "***zthr Joining Threads ..." << endl;
386 for(int i=0; i<N; i++) vzth[i]->join();
387 cout << "***zthr Threads Z1 ... Z" << N << " Finished OK" << endl;
388 cout << " zthr/Resusage: getDataSize() = " << res.getDataSize() << " getStackSize()="
389 << res.getStackSize() << endl;
390 cout << res;
391 for(int i=0; i<N; i++) {
392 delete vzth[i];
393 delete vza[i];
394 }
395 if (mtvp) {
396 Timer tm("MTVecPool::Check()");
397 mtvp->Check();
398 tm.Nop();
399 delete mtvp;
400 }
401 }
402 else {
403 PrtTim("BeginOfCount");
404 CountLock clk;
405 int kk;
406 for(kk=0; kk<atoi(arg[3]); kk++) {
407 clk.Count();
408 }
409 cout << " End CountLock-Test Count= " << clk.Count() << endl;
410 }
411 }
412 catch (PThrowable exc) {
413 cerr << "zthr: catched Exception " << exc.Msg() << endl;
414 rc = 77;
415 }
416 catch (...) {
417 cerr << " catched unknown (...) exception (lpk.cc) " << endl;
418 rc = 78;
419 }
420
421 return(rc);
422
423}
424
425
426
Note: See TracBrowser for help on using the repository browser.