Changeset 3080 in Sophya for trunk/SophyaProg
- Timestamp:
- Sep 16, 2006, 2:39:50 PM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaProg/Tests/zthr.cc
r2615 r3080 1 1 #include "sopnamsp.h" 2 2 #include "zthread.h" 3 #include "resusage.h" 3 4 4 5 #include <iostream> 6 #include <vector> 7 8 #include "tmatrix.h" 9 #include "tarrinit.h" 5 10 6 11 #include <stdlib.h> 7 12 #include <stdio.h> 8 13 9 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 10 22 #include <time.h> 11 23 #include <unistd.h> … … 14 26 15 27 16 void fun2(void * arg) 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) 17 36 { 18 37 time_t t0, t1; 19 38 int i; 20 char *strg; 21 strg = (char*)arg;39 40 ztarg * za = (ztarg *)arg; 22 41 23 42 t0 = time(NULL); 24 printf(" **** Entry to fun2 (arg= %s) ***** \n", strg);25 int imax = atoi(strg);43 printf("+++++ funzt(ThId=%d) Entry to funzt (za.M=%d) +++++\n", za->thid, za->M); 44 int imax = za->M; 26 45 for(i=0; i<imax; i++) 27 46 { 28 sleep( 4);47 sleep(3); 29 48 t1 = time(NULL)-t0; 30 printf(" ######## Fun2 [%d] Dt= %d \n", i, (int)t1);49 printf("++funzt(ThId=%d) Dt= %d \n", za->thid, (int)t1); 31 50 } 32 51 … … 34 53 } 35 54 36 void fun1(void *arg) 55 // --- fonction de test simple avec calcul matriciel 56 void mtx_funzt(void *arg) 37 57 { 38 time_t t0, t1; 39 int i; 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 } 40 91 41 char * strg = (char *)arg;42 92 43 t0 = time(NULL);44 printf("++++ Entry to fun1 (arg= %s) +++++ \n", strg);45 int imax = atoi(strg);46 for(i=0; i<imax; i++)47 {48 sleep(3);49 t1 = time(NULL)-t0;50 printf("%%%% Fun1 [%d] Dt= %d \n", i, (int)t1);51 }52 53 return;54 }55 93 56 94 class CountLock : public ZMutex { … … 63 101 64 102 103 static int N = 1; 104 static int M = 5; 105 65 106 int main(int narg, char *arg[]) 66 107 … … 68 109 69 110 if (narg < 4) { 70 cout << " Usage: zthr N1 N2 NLock" << endl; 71 exit(1); 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); 72 119 } 73 120 74 cout << " zthr-main() arg[1] = " << arg[1] << " arg[2]= " << arg[2] << endl; 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 75 133 InitTim(); 134 SophyaInit(); 76 135 77 CountLock clk; 78 int kk; 79 for(kk=0; kk<atoi(arg[3]); kk++) { 80 clk.Count(); 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 } 81 177 } 82 cout << " End Count= " << clk.Count() << endl; 83 PrtTim("EndOfCount1 "); 84 85 cout << ">>> Creating Thread Z1 and Z2 " << endl; 86 ZThread zt1; 87 zt1.setAction(fun1, arg[1]); 88 ZThread zt2; 89 zt2.setAction(fun2, arg[2]); 90 cout << ">>> Starting Thread Z1 and Z2 " << endl; 91 zt1.start(); 92 zt2.start(); 93 94 PrtTim("BeginOfCount2 "); 95 for(kk=0; kk<atoi(arg[3]); kk++) { 96 clk.Count(); 97 } 98 cout << " End Count= " << clk.Count() << endl; 99 PrtTim("EndOfCount2 "); 100 101 // sleep(5); 102 cout << ">>> Joining Thread Z1 and Z2 " << endl; 103 104 zt1.join(); 105 zt2.join(); 106 cout << ">>> Thread Z1 and Z2 Finished " << endl; 107 108 return(0); 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); 109 188 110 189 } 190 191 192
Note:
See TracChangeset
for help on using the changeset viewer.