source: Sophya/trunk/SophyaProg/Tests/tparlex.cc@ 3721

Last change on this file since 3721 was 3721, checked in by ansari, 16 years ago

Ajout programme tparlex.cc de testdes classes d'execution parallele (ParallelExecutor...), Reza 28/12/2009

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