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 "tarrinit.h"
|
---|
10 |
|
---|
11 | #include <stdlib.h>
|
---|
12 | #include <stdio.h>
|
---|
13 |
|
---|
14 | /* -------------------------------------------------
|
---|
15 | Programme de test des classes de threads de SOPHYA
|
---|
16 | SOPHYA::ZThread SOPHYA::ZMutex ...
|
---|
17 | Exemples d'execution
|
---|
18 | csh> time zthr mtx 2 500
|
---|
19 | csh> time zthr arr 2 500
|
---|
20 | */
|
---|
21 |
|
---|
22 | #include <time.h>
|
---|
23 | #include <unistd.h>
|
---|
24 |
|
---|
25 | #include "timing.h"
|
---|
26 |
|
---|
27 |
|
---|
28 | // --- Structure d'argument pour fonction d'execution dans les threads de test
|
---|
29 | typedef struct {
|
---|
30 | int thid;
|
---|
31 | int M;
|
---|
32 | } ztarg;
|
---|
33 |
|
---|
34 | // --- fonction de test simple avec boucle de sleep
|
---|
35 | void funzt(void *arg)
|
---|
36 | {
|
---|
37 | time_t t0, t1;
|
---|
38 | int i;
|
---|
39 |
|
---|
40 | ztarg * za = (ztarg *)arg;
|
---|
41 |
|
---|
42 | t0 = time(NULL);
|
---|
43 | printf("+++++ funzt(ThId=%d) Entry to funzt (za.M=%d) +++++\n", za->thid, za->M);
|
---|
44 | int imax = za->M;
|
---|
45 | for(i=0; i<imax; i++)
|
---|
46 | {
|
---|
47 | sleep(3);
|
---|
48 | t1 = time(NULL)-t0;
|
---|
49 | printf("++funzt(ThId=%d) Dt= %d \n", za->thid, (int)t1);
|
---|
50 | }
|
---|
51 |
|
---|
52 | return;
|
---|
53 | }
|
---|
54 |
|
---|
55 | // --- fonction de test simple avec calcul matriciel
|
---|
56 | void mtx_funzt(void *arg)
|
---|
57 | {
|
---|
58 | ztarg * za = (ztarg *)arg;
|
---|
59 | cout << ">>>> mtx-funzt(ThId=" << za->thid << ") - Matrix size= " << za->M << endl;
|
---|
60 |
|
---|
61 | sa_size_t m = za->M;
|
---|
62 | Matrix a1(m,m), a2(m,m), mxprod;
|
---|
63 | a1 = RandomSequence(RandomSequence::Gaussian, 0., 4.);
|
---|
64 | a2 = RandomSequence(RandomSequence::Gaussian, 0., 3.);
|
---|
65 | char buff[128];
|
---|
66 | sprintf(buff, "mtx-funzt(ThId=%d) EndOfInit", za->thid);
|
---|
67 | PrtTim(buff);
|
---|
68 | mxprod = a1*a2;
|
---|
69 | sprintf(buff, "mtx-funzt(ThId=%d) EndOfMxProd", za->thid);
|
---|
70 | PrtTim(buff);
|
---|
71 | return;
|
---|
72 | }
|
---|
73 | // --- fonction de test simple avec calcul matriciel
|
---|
74 | void arr_funzt(void *arg)
|
---|
75 | {
|
---|
76 | ztarg * za = (ztarg *)arg;
|
---|
77 | cout << ">>>> arr-funzt(ThId=" << za->thid << ") - Matrix size= " << za->M << endl;
|
---|
78 |
|
---|
79 | sa_size_t m = za->M;
|
---|
80 | TMatrix<int_4> a1(m,m), a2(m,m), ares;
|
---|
81 | a1 = RegularSequence(1.,1.);
|
---|
82 | a2 = RegularSequence(5.,3.);
|
---|
83 | char buff[128];
|
---|
84 | sprintf(buff, "arr-funzt(ThId=%d) EndOfInit", za->thid);
|
---|
85 | PrtTim(buff);
|
---|
86 | ares = 4*a1*12*a2;
|
---|
87 | sprintf(buff, "arr-funzt(ThId=%d) EndOfOper", za->thid);
|
---|
88 | PrtTim(buff);
|
---|
89 | return;
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 |
|
---|
94 | class CountLock : public ZMutex {
|
---|
95 | int count;
|
---|
96 | public:
|
---|
97 | CountLock() { count = 0; }
|
---|
98 | inline int Count() { lock(); int rc = ++count; unlock(); return(rc);
|
---|
99 | }
|
---|
100 | };
|
---|
101 |
|
---|
102 |
|
---|
103 | static int N = 1;
|
---|
104 | static int M = 5;
|
---|
105 |
|
---|
106 | int main(int narg, char *arg[])
|
---|
107 |
|
---|
108 | {
|
---|
109 |
|
---|
110 | if (narg < 4) {
|
---|
111 | cout << " Usage: zthr select N LM" << endl;
|
---|
112 | cout << " select= sl -> simple loop with sleep " << endl;
|
---|
113 | cout << " select= mtx -> matrix init and multiply mx1*mx2" << endl;
|
---|
114 | cout << " select= arr -> array/matrix init and operation c1*a1+c2*a2 " << endl;
|
---|
115 | cout << " select= clk -> Mutex lock count " << endl;
|
---|
116 | cout << " N= Number of threads (sl/mtx) or CountLock " << endl;
|
---|
117 | cout << " LM = Loop limit (sl) or Matrix size (mtx) " << endl;
|
---|
118 | return(1);
|
---|
119 | }
|
---|
120 |
|
---|
121 | string sel = arg[1];
|
---|
122 | if ((sel != "sl") && (sel != "mtx") && (sel != "arr") && (sel != "clk")) {
|
---|
123 | cout << "zthr/erreur argument sel (!= sl / mtx / arr / clk) " << endl;
|
---|
124 | return 2;
|
---|
125 | }
|
---|
126 |
|
---|
127 | //-- Decodage arguments
|
---|
128 | N = atoi(arg[2]);
|
---|
129 | M = atoi(arg[3]);
|
---|
130 | cout << "zthr/Info: select=" << sel << " N=" << N << " M= " << M << endl;
|
---|
131 |
|
---|
132 |
|
---|
133 | InitTim();
|
---|
134 | SophyaInit();
|
---|
135 |
|
---|
136 | int rc = 0;
|
---|
137 | try {
|
---|
138 | ResourceUsage res;
|
---|
139 | if ((sel == "mtx") || (sel == "arr") || (sel == "sl")) {
|
---|
140 | vector<ztarg *> vza;
|
---|
141 | vector<ZThread *> vzth;
|
---|
142 | for(int i=0; i<N; i++) {
|
---|
143 | cout << "*****zthr: Creating Thread " << i+1 << " /" << N << endl;
|
---|
144 | ZThread * pzt = new ZThread();
|
---|
145 | ztarg* zap = new ztarg;
|
---|
146 | vzth.push_back(pzt);
|
---|
147 | zap->thid = i+1; zap->M = M;
|
---|
148 | vza.push_back(zap);
|
---|
149 | if (sel == "mtx") pzt->setAction(mtx_funzt, vza[i]);
|
---|
150 | else if (sel == "arr") pzt->setAction(arr_funzt, vza[i]);
|
---|
151 | else pzt->setAction(funzt, vza[i]);
|
---|
152 | }
|
---|
153 | cout << "***zthr: Starting threads ... " << endl;
|
---|
154 | PrtTim("***zthr/StarThr");
|
---|
155 | for(int i=0; i<N; i++) vzth[i]->start();
|
---|
156 | sleep(1);
|
---|
157 | cout << "***ResourceUsage before thr[i].join()" << endl;
|
---|
158 | cout << res;
|
---|
159 | cout << "***zthr Joining Threads ..." << endl;
|
---|
160 | for(int i=0; i<N; i++) vzth[i]->join();
|
---|
161 | cout << "***zthr Threads Z1 ... Z" << N << " Finished OK" << endl;
|
---|
162 | cout << res;
|
---|
163 | for(int i=0; i<N; i++) {
|
---|
164 | delete vzth[i];
|
---|
165 | delete vza[i];
|
---|
166 | }
|
---|
167 | }
|
---|
168 | else {
|
---|
169 | PrtTim("BeginOfCount");
|
---|
170 | CountLock clk;
|
---|
171 | int kk;
|
---|
172 | for(kk=0; kk<atoi(arg[3]); kk++) {
|
---|
173 | clk.Count();
|
---|
174 | }
|
---|
175 | cout << " End CountLock-Test Count= " << clk.Count() << endl;
|
---|
176 | }
|
---|
177 | }
|
---|
178 | catch (PThrowable exc) {
|
---|
179 | cerr << "zthr: catched Exception " << exc.Msg() << endl;
|
---|
180 | rc = 77;
|
---|
181 | }
|
---|
182 | catch (...) {
|
---|
183 | cerr << " catched unknown (...) exception (lpk.cc) " << endl;
|
---|
184 | rc = 78;
|
---|
185 | }
|
---|
186 |
|
---|
187 | return(rc);
|
---|
188 |
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 |
|
---|