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 << ") Start computing - NbExec= " << nbex << endl;
|
---|
109 | Vector vx = mxa.Row(tid);
|
---|
110 | r_8* x = vx.Data();
|
---|
111 | r_8* y = mxb.Row(tid).Data();
|
---|
112 | for(sa_size_t j=0; j<vx.Size(); j++)
|
---|
113 | y[j] = sin(x[j])+sqrt(x[j])+cos(x[j]);
|
---|
114 | // mxb.Row(tid) = Sin(x)+Sqrt(x)+Cos(x);
|
---|
115 | cout << " ---- TParTaskA::execute( " << tid << "," << nbex << ") DONE " << endl;
|
---|
116 | return 0;
|
---|
117 | }
|
---|
118 |
|
---|
119 | Matrix& mxa;
|
---|
120 | Matrix& mxb;
|
---|
121 | int nbex;
|
---|
122 | };
|
---|
123 |
|
---|
124 | /* --Fonction-- */
|
---|
125 | int parex_testA()
|
---|
126 | {
|
---|
127 | sa_size_t NCOLS = SIZE*1000;
|
---|
128 | Matrix a(NTHR, NCOLS);
|
---|
129 | Matrix b(NTHR, NCOLS);
|
---|
130 | Matrix c(NTHR, NCOLS);
|
---|
131 |
|
---|
132 | cout << " parex_testA/Info: " << a.InfoString() << endl;
|
---|
133 | a = RegularSequence(0.25,0.003);
|
---|
134 | PrtTim("tparlexA[1] Done init ");
|
---|
135 | cout << "tparlexA[1] Start b=Sin(a)+Sqrt(a)+Cos[a]" << endl;
|
---|
136 | r_8* x = a.Data();
|
---|
137 | r_8* y = b.Data();
|
---|
138 | for(sa_size_t j=0; j<a.Size(); j++)
|
---|
139 | y[j] = sin(x[j])+sqrt(x[j])+cos(x[j]);
|
---|
140 | // b = Sin(a)+Sqrt(a)+Cos(a);
|
---|
141 | PrtTim(">>tparlexA[1.b] Done ");
|
---|
142 | // char ans[64];
|
---|
143 | // cout << " A/ CR to continue ... " << endl; gets(ans);
|
---|
144 |
|
---|
145 | TParTaskA ptask(a,c);
|
---|
146 | ParallelExecutor pex(ptask, NTHR);
|
---|
147 | pex.start();
|
---|
148 | int rce=0;
|
---|
149 | for(int i=0; i<NBPEXC; i++) {
|
---|
150 | cout << " tparlexA[II=" << i+1 << " Start ParallelExecution c=Sin(a)+Sqrt(a)+Cos[a]" << endl;
|
---|
151 | rce = pex.execute();
|
---|
152 | PrtTim(">>>>tparlexA: End ParallelExecution ");
|
---|
153 | }
|
---|
154 | cout << " Rc=pex.execute() = " << rce << endl;
|
---|
155 | Matrix d = b-c;
|
---|
156 | double dmin, dmax;
|
---|
157 | d.MinMax(dmin, dmax);
|
---|
158 | cout << ">>tparlexA[3] Diff d=b-c, dmin=" << dmin << " dmax=" << dmax << endl;
|
---|
159 | // cout << " B/ CR to continue ... " << endl; gets(ans);
|
---|
160 |
|
---|
161 | // cout << " C/ CR to continue ... " << endl; gets(ans);
|
---|
162 | return 0;
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | //--------------------------------------------------------------------
|
---|
167 | // Classe implementant la fonction d'execution parallele
|
---|
168 | // ParallelTaskInterface::execute() mxc= mxa * mxb
|
---|
169 | class TParTaskB : public ParallelTaskInterface {
|
---|
170 | public:
|
---|
171 | TParTaskB(Matrix& a, Matrix& b, Matrix& c, int nth)
|
---|
172 | : mxa(a), mxb(b), mxc(c), nthread(nth), nbex(0)
|
---|
173 | {
|
---|
174 | }
|
---|
175 | virtual int execute(int tid)
|
---|
176 | {
|
---|
177 | nbex++;
|
---|
178 | cout << " ---- TParTaskB::execute(tid=" << tid << ") Start computing - NbExec= " << nbex << endl;
|
---|
179 | sa_size_t sz = mxb.NRows();
|
---|
180 | // On s'arrange pour que chaque thread calcule une partie de la matrice resultat
|
---|
181 | // Il faut etre un peu malin et eviter que differents threads accedent les memes zones memoire
|
---|
182 | mxc.SubMatrix(Range(sz*tid, sz*(tid+1)-1), Range::all() ) =
|
---|
183 | mxa.SubMatrix(Range(sz*tid, sz*(tid+1)-1), Range::all()) * mxb;
|
---|
184 | /* Une maniere plus compliquee pour MxA(NTH*SZ , SZ) * MxB(SZ, NTH*SZ)
|
---|
185 | mais cela n'apporte rien ...
|
---|
186 | for(sa_size_t j=0; j<nthread; j++) {
|
---|
187 | sa_size_t jj = (j+tid)%nthread;
|
---|
188 | mxc.SubMatrix(Range(sz*tid, sz*(tid+1)-1), Range(sz*jj, sz*(jj+1)-1)) =
|
---|
189 | mxa.SubMatrix(Range(sz*tid, sz*(tid+1)-1), Range::all()) *
|
---|
190 | mxb.SubMatrix(Range::all(), Range(sz*jj, sz*(jj+1)-1));
|
---|
191 | }
|
---|
192 | */
|
---|
193 | cout << " ---- TParTaskB::execute( " << tid << "," << nbex << ") DONE " << endl;
|
---|
194 | return 0;
|
---|
195 | }
|
---|
196 |
|
---|
197 | Matrix& mxa;
|
---|
198 | Matrix& mxb;
|
---|
199 | Matrix& mxc;
|
---|
200 | int nthread;
|
---|
201 | int nbex;
|
---|
202 | };
|
---|
203 |
|
---|
204 | /* --Fonction-- */
|
---|
205 | int parex_testB()
|
---|
206 | {
|
---|
207 | // On se met dans les conditions optimales pour la multiplication matricielle
|
---|
208 | Matrix a(NTHR*SIZE, SIZE, BaseArray::CMemoryMapping);
|
---|
209 | Matrix b(SIZE, SIZE, BaseArray::FortranMemoryMapping);
|
---|
210 | Matrix c(NTHR*SIZE, SIZE);
|
---|
211 |
|
---|
212 | cout << " parex_testB/Info: a.InfoString(): " << a.InfoString() << endl;
|
---|
213 | cout << " parex_testB/Info: b.InfoString(): " << b.InfoString() << endl;
|
---|
214 |
|
---|
215 | a = RegularSequence(0.25,0.003);
|
---|
216 | b = RegularSequence(1.2,0.0423);
|
---|
217 |
|
---|
218 | PrtTim("tparlexA[1] Done init ");
|
---|
219 |
|
---|
220 | cout << "tparlexB[1] Start cc=a*b" << endl;
|
---|
221 | // Matrix cc(NTHR*SIZE, SIZE);
|
---|
222 | // cc = a*b;
|
---|
223 | Matrix cc = a*b;
|
---|
224 | PrtTim(">>tparlexB[1.b] Done ");
|
---|
225 |
|
---|
226 | TParTaskB ptask(a,b,c,NTHR);
|
---|
227 | ParallelExecutor pex(ptask, NTHR);
|
---|
228 | pex.start();
|
---|
229 | int rce=0;
|
---|
230 | for(int i=0; i<NBPEXC; i++) {
|
---|
231 | cout << " tparlexB[II=" << i+1 << " Start ParallelExecution c=a*b" << endl;
|
---|
232 | rce = pex.execute();
|
---|
233 | PrtTim(">>tparlexB: End ParallelExecution ");
|
---|
234 | }
|
---|
235 | cout << " Rc=pex.execute() = " << rce << endl;
|
---|
236 | Matrix d = cc-c;
|
---|
237 | double dmin, dmax;
|
---|
238 | d.MinMax(dmin, dmax);
|
---|
239 | cout << ">>tparlexB[3] Diff d=b-c, dmin=" << dmin << " dmax=" << dmax << endl;
|
---|
240 | return 0;
|
---|
241 | }
|
---|