| 1 | #include <stdlib.h> | 
|---|
| 2 | #include <stdio.h> | 
|---|
| 3 | #include <string.h> | 
|---|
| 4 | #include <time.h> | 
|---|
| 5 | #include <unistd.h> | 
|---|
| 6 |  | 
|---|
| 7 | #include <iostream> | 
|---|
| 8 | #include <vector> | 
|---|
| 9 |  | 
|---|
| 10 | #include "sopnamsp.h" | 
|---|
| 11 | #include "tmatrix.h" | 
|---|
| 12 | #include "tvector.h" | 
|---|
| 13 | #include "matharr.h" | 
|---|
| 14 | #include "tarrinit.h" | 
|---|
| 15 | #include "randr48.h" | 
|---|
| 16 |  | 
|---|
| 17 | #include "parlex.h" | 
|---|
| 18 | #include "resusage.h" | 
|---|
| 19 | #include "timing.h" | 
|---|
| 20 | #include "ctimer.h" | 
|---|
| 21 |  | 
|---|
| 22 |  | 
|---|
| 23 | /* ------------------------------------------------------------- | 
|---|
| 24 | Programme de test des classes d'execution parallele de SOPHYA | 
|---|
| 25 | SOPHYA::ParallelExecutor ... | 
|---|
| 26 | Exemples d'execution: | 
|---|
| 27 | Usage: tparlex SEL [Size=500] [NThreads=2] [NbExecuteCall=1] | 
|---|
| 28 | csh> time tparlex A 2000 2 | 
|---|
| 29 | csh> time tparlex B 500 2 | 
|---|
| 30 | csh> time tparlex A 2000 2 4 | 
|---|
| 31 | csh> time tparlex B 500 2 3 | 
|---|
| 32 | */ | 
|---|
| 33 |  | 
|---|
| 34 | // Declaration des fonctions de test | 
|---|
| 35 | int parex_testA(); | 
|---|
| 36 | int parex_testB(); | 
|---|
| 37 |  | 
|---|
| 38 |  | 
|---|
| 39 | static sa_size_t SIZE = 500; | 
|---|
| 40 | static unsigned int NTHR = 2; | 
|---|
| 41 | static unsigned int NBPEXC = 1; | 
|---|
| 42 |  | 
|---|
| 43 | //-------------------------------------------------------------- | 
|---|
| 44 | //----------------------   MAIN PROGRAM ------------------------ | 
|---|
| 45 | int main(int narg, char *arg[]) | 
|---|
| 46 | { | 
|---|
| 47 |  | 
|---|
| 48 | if ((narg<2)||((narg > 1)&&(strcmp(arg[1],"-h")==0))) { | 
|---|
| 49 | cout << " tparlex Test of SOPHYA parallel execution classes \n" | 
|---|
| 50 | << " Usage: tparlex SEL [Size=500] [NThreads=2] [NbExecuteCall=1] \n" | 
|---|
| 51 | << "  - SEL : A or B \n " | 
|---|
| 52 | << "  - Size : Matrix size (see below) \n " | 
|---|
| 53 | << "  - NThreads : number of threads \n " | 
|---|
| 54 | << "  - NbExecuteCall : number of call to parallel execution function \n " | 
|---|
| 55 | << "   A -> Sin(mx)+Sqrt(mx)+Cos(mx) , mx(NThr,1000*Size) \n" | 
|---|
| 56 | << "   B -> mxa(NThr*Size, Size) * mxb(Size,Size)  " << endl; | 
|---|
| 57 | return(1); | 
|---|
| 58 | } | 
|---|
| 59 | InitTim(); | 
|---|
| 60 | ResourceUsage res(ResourceUsage::RU_All); | 
|---|
| 61 |  | 
|---|
| 62 | char sel = *arg[1]; | 
|---|
| 63 | if (narg > 2) SIZE = atol(arg[2]); | 
|---|
| 64 | if (narg > 3) NTHR = atoi(arg[3]); | 
|---|
| 65 | if (narg > 4) NBPEXC = atoi(arg[4]); | 
|---|
| 66 | if (SIZE<100)  SIZE=100; | 
|---|
| 67 | if (NTHR<1) NTHR=1; | 
|---|
| 68 | if (NBPEXC<1) NBPEXC=1; | 
|---|
| 69 |  | 
|---|
| 70 | cout << " tparlex/starting, SEL=" << sel << " Size=" << SIZE << "  NTHR=NRows=" << NTHR | 
|---|
| 71 | << " NbParExCall=" << NBPEXC << endl; | 
|---|
| 72 | BaseArray::SetDefaultMemoryMapping(BaseArray::CMemoryMapping); | 
|---|
| 73 |  | 
|---|
| 74 | int rc = 0; | 
|---|
| 75 | try { | 
|---|
| 76 | ResourceUsage res(ResourceUsage::RU_All); | 
|---|
| 77 | if (sel=='A')   rc = parex_testA(); | 
|---|
| 78 | else  rc = parex_testB(); | 
|---|
| 79 | cout << res; | 
|---|
| 80 | } | 
|---|
| 81 | catch (std::exception exc) { | 
|---|
| 82 | cerr << "tparlex: catched std::exception " << exc.what() << endl; | 
|---|
| 83 | rc = 77; | 
|---|
| 84 | } | 
|---|
| 85 | catch (...) { | 
|---|
| 86 | cerr << "tparlex: catched unknown (...) exception " << endl; | 
|---|
| 87 | rc = 78; | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | PrtTim(">>> tparlex: END <<< "); | 
|---|
| 91 | cout << " ------------ End execution tparlex -------------- " << endl; | 
|---|
| 92 | return(rc); | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 |  | 
|---|
| 96 | //-------------------------------------------------------------------- | 
|---|
| 97 | //   Classe implementant la fonction d'execution parallele | 
|---|
| 98 | //   ParallelTaskInterface::execute()  mxb=sin(mxa)+sqrt(mxa)+cos(mxa) | 
|---|
| 99 | class TParTaskA : public ParallelTaskInterface { | 
|---|
| 100 | public: | 
|---|
| 101 | TParTaskA(Matrix& a, Matrix& b) | 
|---|
| 102 | : mxa(a), mxb(b), nbex(0) | 
|---|
| 103 | { | 
|---|
| 104 | } | 
|---|
| 105 | virtual int    execute(int tid) | 
|---|
| 106 | { | 
|---|
| 107 | nbex++; | 
|---|
| 108 | cout << " ---- TParTaskA::execute(tid=" << tid << ") NbParallelThr=" << getNbParallelThreads() | 
|---|
| 109 | << " Start computing - NbExec= " << nbex << endl; | 
|---|
| 110 |  | 
|---|
| 111 | Vector vx = mxa.Row(tid); | 
|---|
| 112 | r_8* x = vx.Data(); | 
|---|
| 113 | r_8* y = mxb.Row(tid).Data(); | 
|---|
| 114 | for(sa_size_t j=0; j<vx.Size(); j++) | 
|---|
| 115 | y[j] = sin(x[j])+sqrt(x[j])+cos(x[j]); | 
|---|
| 116 | // mxb.Row(tid) = Sin(x)+Sqrt(x)+Cos(x); | 
|---|
| 117 | cout << " ---- TParTaskA::execute( " << tid << "," << nbex << ")  DONE " << endl; | 
|---|
| 118 | return 0; | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | Matrix& mxa; | 
|---|
| 122 | Matrix& mxb; | 
|---|
| 123 | int nbex; | 
|---|
| 124 | }; | 
|---|
| 125 |  | 
|---|
| 126 | /* --Fonction-- */ | 
|---|
| 127 | int parex_testA() | 
|---|
| 128 | { | 
|---|
| 129 | sa_size_t NCOLS = SIZE*1000; | 
|---|
| 130 | Matrix a(NTHR, NCOLS); | 
|---|
| 131 | Matrix b(NTHR, NCOLS); | 
|---|
| 132 | Matrix c(NTHR, NCOLS); | 
|---|
| 133 |  | 
|---|
| 134 | cout << " parex_testA/Info: " << a.InfoString() << endl; | 
|---|
| 135 | a = RegularSequence(0.25,0.003); | 
|---|
| 136 | PrtTim("tparlexA[1] Done init "); | 
|---|
| 137 | cout << "tparlexA[1] Start b=Sin(a)+Sqrt(a)+Cos[a]" << endl; | 
|---|
| 138 | r_8* x = a.Data(); | 
|---|
| 139 | r_8* y = b.Data(); | 
|---|
| 140 | for(sa_size_t j=0; j<a.Size(); j++) | 
|---|
| 141 | y[j] = sin(x[j])+sqrt(x[j])+cos(x[j]); | 
|---|
| 142 | //    b = Sin(a)+Sqrt(a)+Cos(a); | 
|---|
| 143 | PrtTim(">>tparlexA[1.b] Done "); | 
|---|
| 144 | // char ans[64]; | 
|---|
| 145 | // cout << " A/ CR to continue ... " << endl;  gets(ans); | 
|---|
| 146 |  | 
|---|
| 147 | TParTaskA ptask(a,c); | 
|---|
| 148 | ParallelExecutor pex(ptask, NTHR); | 
|---|
| 149 | pex.start(); | 
|---|
| 150 | int rce=0; | 
|---|
| 151 | for(int i=0; i<NBPEXC; i++) { | 
|---|
| 152 | cout << " tparlexA[II=" << i+1 << "  Start ParallelExecution c=Sin(a)+Sqrt(a)+Cos[a]" << endl; | 
|---|
| 153 | rce = pex.execute(); | 
|---|
| 154 | PrtTim(">>>>tparlexA:  End ParallelExecution "); | 
|---|
| 155 | } | 
|---|
| 156 | cout << " Rc=pex.execute() = " << rce << endl; | 
|---|
| 157 | Matrix d = b-c; | 
|---|
| 158 | double dmin, dmax; | 
|---|
| 159 | d.MinMax(dmin, dmax); | 
|---|
| 160 | cout << ">>tparlexA[3] Diff d=b-c, dmin=" << dmin << " dmax=" << dmax << endl; | 
|---|
| 161 | // cout << " B/ CR to continue ... " << endl; gets(ans); | 
|---|
| 162 |  | 
|---|
| 163 | // cout << " C/ CR to continue ... " << endl;  gets(ans); | 
|---|
| 164 | return 0; | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 |  | 
|---|
| 168 | //-------------------------------------------------------------------- | 
|---|
| 169 | //   Classe implementant la fonction d'execution parallele | 
|---|
| 170 | //   ParallelTaskInterface::execute()  mxc= mxa * mxb | 
|---|
| 171 | class TParTaskB : public ParallelTaskInterface { | 
|---|
| 172 | public: | 
|---|
| 173 | TParTaskB(Matrix& a, Matrix& b, Matrix& c, int nth) | 
|---|
| 174 | : mxa(a), mxb(b), mxc(c), nthread(nth), nbex(0) | 
|---|
| 175 | { | 
|---|
| 176 | } | 
|---|
| 177 | virtual int    execute(int tid) | 
|---|
| 178 | { | 
|---|
| 179 | nbex++; | 
|---|
| 180 | cout << " ---- TParTaskB::execute(tid=" << tid << ") NbParallelThr=" << getNbParallelThreads() | 
|---|
| 181 | << " Start computing - NbExec= " << nbex << endl; | 
|---|
| 182 | sa_size_t sz = mxb.NRows(); | 
|---|
| 183 | // On s'arrange pour que chaque thread calcule une partie de la matrice resultat | 
|---|
| 184 | // Il faut etre un peu malin et eviter que differents threads accedent les memes zones memoire | 
|---|
| 185 | mxc.SubMatrix(Range(sz*tid, sz*(tid+1)-1), Range::all() ) = | 
|---|
| 186 | mxa.SubMatrix(Range(sz*tid, sz*(tid+1)-1), Range::all()) * mxb; | 
|---|
| 187 | /*  Une maniere plus compliquee pour MxA(NTH*SZ , SZ) * MxB(SZ, NTH*SZ) | 
|---|
| 188 | mais cela n'apporte rien ... | 
|---|
| 189 | for(sa_size_t j=0; j<nthread; j++) { | 
|---|
| 190 | sa_size_t jj = (j+tid)%nthread; | 
|---|
| 191 | mxc.SubMatrix(Range(sz*tid, sz*(tid+1)-1), Range(sz*jj, sz*(jj+1)-1)) = | 
|---|
| 192 | mxa.SubMatrix(Range(sz*tid, sz*(tid+1)-1), Range::all()) * | 
|---|
| 193 | mxb.SubMatrix(Range::all(), Range(sz*jj, sz*(jj+1)-1)); | 
|---|
| 194 | } | 
|---|
| 195 | */ | 
|---|
| 196 | cout << " ---- TParTaskB::execute( " << tid << "," << nbex << ")  DONE " << endl; | 
|---|
| 197 | return 0; | 
|---|
| 198 | } | 
|---|
| 199 |  | 
|---|
| 200 | Matrix& mxa; | 
|---|
| 201 | Matrix& mxb; | 
|---|
| 202 | Matrix& mxc; | 
|---|
| 203 | int nthread; | 
|---|
| 204 | int nbex; | 
|---|
| 205 | }; | 
|---|
| 206 |  | 
|---|
| 207 | /* --Fonction-- */ | 
|---|
| 208 | int parex_testB() | 
|---|
| 209 | { | 
|---|
| 210 | // On se met dans les conditions optimales pour la multiplication matricielle | 
|---|
| 211 | Matrix a(NTHR*SIZE, SIZE, BaseArray::CMemoryMapping); | 
|---|
| 212 | Matrix b(SIZE, SIZE, BaseArray::FortranMemoryMapping); | 
|---|
| 213 | Matrix c(NTHR*SIZE, SIZE); | 
|---|
| 214 |  | 
|---|
| 215 | cout << " parex_testB/Info: a.InfoString(): " << a.InfoString() << endl; | 
|---|
| 216 | cout << " parex_testB/Info: b.InfoString(): " << b.InfoString() << endl; | 
|---|
| 217 |  | 
|---|
| 218 | a = RegularSequence(0.25,0.003); | 
|---|
| 219 | b = RegularSequence(1.2,0.0423); | 
|---|
| 220 |  | 
|---|
| 221 | PrtTim("tparlexA[1] Done init "); | 
|---|
| 222 |  | 
|---|
| 223 | cout << "tparlexB[1] Start cc=a*b" << endl; | 
|---|
| 224 | // Matrix cc(NTHR*SIZE, SIZE); | 
|---|
| 225 | // cc = a*b; | 
|---|
| 226 | Matrix cc = a*b; | 
|---|
| 227 | PrtTim(">>tparlexB[1.b] Done "); | 
|---|
| 228 |  | 
|---|
| 229 | TParTaskB ptask(a,b,c,NTHR); | 
|---|
| 230 | ParallelExecutor pex(ptask, NTHR); | 
|---|
| 231 | pex.start(); | 
|---|
| 232 | int rce=0; | 
|---|
| 233 | for(int i=0; i<NBPEXC; i++) { | 
|---|
| 234 | cout << " tparlexB[II=" << i+1 << "  Start ParallelExecution c=a*b" << endl; | 
|---|
| 235 | rce = pex.execute(); | 
|---|
| 236 | PrtTim(">>tparlexB:  End ParallelExecution "); | 
|---|
| 237 | } | 
|---|
| 238 | cout << " Rc=pex.execute() = " << rce << endl; | 
|---|
| 239 | Matrix d = cc-c; | 
|---|
| 240 | double dmin, dmax; | 
|---|
| 241 | d.MinMax(dmin, dmax); | 
|---|
| 242 | cout << ">>tparlexB[3] Diff d=b-c, dmin=" << dmin << " dmax=" << dmax << endl; | 
|---|
| 243 | return 0; | 
|---|
| 244 | } | 
|---|