LW Differential Equation Doc

  • Published on
    14-Oct-2014

  • View
    57

  • Download
    0

Transcript

<p> . .. </p> <p> ____________________</p> <p> ..</p> <p> Intel</p> <p> 2011</p> <p> ........................................................................................................ 3 1. 1.1. 1.2. 1.3. 1.4. ....................................................... 5 ........................................................................ 5 ............................................................................... 6 ............................................................... 6 ....................................... 7</p> <p>2. .................................................................................................... 8 3. 4. 5. 6. - .......... 9 ......................................................................... 14 ........................................ 15 .................................................. 19 6.1. - ........................................................ 19 6.2. </p> <p>31 6.3. OPENMP .................................................. 34 6.4. INTEL THREADING BUILDING BLOCKS..... 35 7. .................. 40 8. OPENMP- .................................................................... 45 9. TBB- ..................................................................................................... 50 10. 11. .................................................. 52 .................................................................................... 53</p> <p>11.1. ................................................................... 53 11.2. ................................................................. 53 11.3. ....................................................... 53</p> <p>3</p> <p> , . , . .. </p> <p>, , , . , , , . ( , ). , ( , , ), ( , ), - ( ), . , (, ). , , . , (., , [1, 2]). , . . ( ) :</p> <p>4</p> <p> (, , ); ( ) .</p> <p> , , , [3]. , . -, , () , , . , -, , . , [5]. ( , , [4]), , 1/2. ( ) . . , , . , , , [10]. . [6, 13].</p> <p>5</p> <p>1. 1.1. . : 1. [6]. 2. - , . () ( ). 3. [5] . 4. - [1] . 5. , . 6. -. 7. OpenMP [7, 11]. 8. TBB [8, 9, 12]. 9. .</p> <p>6</p> <p>10. .</p> <p>1.2. , . - . ( , , ). , [5] . .</p> <p>1.3. (. 1). 1. 2 Intel Xeon E5520 (2.27 GHz) 16 Gb MicrosoftWindows 7 Microsoft Visual Studio 2008</p> <p>, - IntelParallelStudioXE , TBB Intel Threading Building Blocks 3.0 for Windows, Update 3 ( Intel Parallel Studio XE 2011)</p> <p>7</p> <p>1.4. : 1. , . 2. . , . 3. - , , [1]. 4. - , , , . 5. . 6. . 7. . 8. , . 9. -. 10. OpenMP [11], TBB [12]. 11. . 12. .</p> <p>8</p> <p>2. (, converti ble bond) , , [6]. . , , . . , , . , , , . m* (t*k , k=1,2,...,m*) n , (ti,i=0,1,2,...,n; nm*). . T = tn t0 .</p> <p>. 1. </p> <p> , t*i. , (1). , , , , . { (1)</p> <p>9</p> <p> ( ). , . : ( ) ,</p> <p> ( , ), 1 . (2), (3), (4) (5). ( ( ) ) { { { } ( } )} (2) (3)</p> <p>( ), [ ] (5) (4)</p> <p>[</p> <p>]</p> <p>, , , , .</p> <p>3. - , , (2), (3), (4) (5) - [6]. , </p> <p>1</p> <p> [6].</p> <p>10</p> <p> . , . : (6)</p> <p>(</p> <p>) ( )</p> <p>(</p> <p>)</p> <p>(</p> <p>) (7)</p> <p> (6) (7) : ( ( ) ( ) ) (8) (9)</p> <p> (9). , . (9) (2), (3), (4) ] (5) [ . . . ( , , .. ). k- (. . 2): .</p> <p>11</p> <p>j</p> <p>. 2. </p> <p> (9) : ( ) (10) (11) ( ( ( ). ) ) (12) (13)</p> <p> , (9): ( )( . ) ( ) ) ( (14)</p> <p> :( )</p> <p>(15) (16)</p> <p>(</p> <p>)</p> <p>(17)</p> <p>12(</p> <p> )</p> <p>(18) (19)</p> <p>(</p> <p>)</p> <p>(20)</p> <p> (14) k- : (21) { } { }. :</p> <p>[</p> <p>]</p> <p>(</p> <p>)</p> <p>(</p> <p>)</p> <p>[</p> <p>]</p> <p>(</p> <p>)</p> <p>(22) ( { ) } { }.</p> <p>13</p> <p> , : { { ( } } (23) (24) ) (25) (26) , . 1. ( ] [ ). 2. , : a. (23) (24). ] b. [ : i. ( ). . ii. (25) (26). iii. . iv. , , . c. , , . , [6]:</p> <p>14</p> <p>(27) { : </p> <p>{|</p> <p>[</p> <p>]|</p> <p>|}</p> <p>.</p> <p>4. (22) (23), (24), (25) (26), , (28).</p> <p>[</p> <p>]</p> <p>(</p> <p>)</p> <p>(</p> <p>) (28)</p> <p> , , , 2. , (29). (29) (29) -e : , </p> <p> (29), </p> <p>2</p> <p> [1] , .</p> <p>15</p> <p>(30) . . .</p> <p> (29), </p> <p> (28) , .</p> <p> , . , (29). . :</p> <p> :</p> <p>5. , . . , , , . (28) 3 .</p> <p>3</p> <p> [5].</p> <p>16</p> <p> : . (28) (31).</p> <p>[</p> <p>]</p> <p>(</p> <p>)</p> <p>(31) ( )</p> <p> , (31) , .. , , . ( ) ( ). , (. 3). , .</p> <p>. 3. </p> <p> (31) :</p> <p>17</p> <p>{</p> <p>(32)</p> <p> , . . (32):</p> <p> (32):</p> <p>(</p> <p>) (33)</p> <p> (32.3) : (34) (33) (34). : (35) , (36). .</p> <p>(36)</p> <p> (36) , (31), . , , , . , , j- , - , .</p> <p>(37) . (38)</p> <p>18</p> <p>.</p> <p> : , </p> <p> (39) . . 4.</p> <p>0</p> <p>1</p> <p>2</p> <p>3</p> <p>4</p> <p>5</p> <p>6</p> <p>7</p> <p>8 9</p> <p>2</p> <p>4</p> <p>6</p> <p>4. 4. N = 8</p> <p> - .</p> <p>:</p> <p>19</p> <p> , . l- , (40) , . (40)</p> <p> . 5 . ( , , (40)). , .</p> <p>0</p> <p>1</p> <p>2</p> <p>3</p> <p>4</p> <p>5</p> <p>6</p> <p>7</p> <p>8 9</p> <p>2</p> <p>4</p> <p>6</p> <p>4. 5. N = 8</p> <p>6. 6.1. - . , (Solution), (Project) . :</p> <p>20</p> <p> Microsoft Visual Studio 2008. File NewProject. . 6, New Project Win32, Win32 Console Application, Solution 07_PDE, Name 01_Sweep, Location c:\ParallelCalculus\. OK.</p> <p>. 6. </p> <p> Win32 Application Wizard Next ( Application Settings ) Empty Project. Finish. Solution Explorer Source Files AddNewItem. Code, C++ File (.cpp), Name main. Add. Solution Explorer Source Files AddNew Item. Code, C++ File (.cpp), Name DiffEquation. Add. , .</p> <p>21</p> <p> Solution Explorer Header Files AddNew Item. Code, Header File (.h), Name DiffEquation. Add. .</p> <p> main.cpp main(), . , . ( ) .int main(int argc, char *argv[]) { // double T = 5.0; int m = 5; double sigma = 0.3; double D = 0.01; double K = 100.0; double rk_s = 0.015; double Ic = 6.0; double rf = 0.0253; int M = 100; int n = 100; int J; // clock_t start, finish; // FILE *fstockprice, *fcbprice; // , // double delta_z; // double *stockprice, *cbprice; if (argc &lt; 2) return -1; J = atoi(argv[1]); start = clock(); // // // ... finish = clock(); // duration = ((double)(finish-start))/</p> <p>22</p> <p>((double)CLOCKS_PER_SEC); printf("Time = %.5f\n", duration); // fstockprice = fopen("stockprice.csv", "w+"); if (fstockprice == NULL) { printf("File wasn't created\n"); free(stockprice); free(cbprice); return -1; } // // fcbprice = fopen("cbprice.csv", "w+"); if (fcbprice == NULL) { printf("File wasn't created\n"); free(stockprice); free(cbprice); return -1; } // for (i = 0; i &lt; n; i++) fprintf(fstockprice, "%lf\n", stockprice[i]); // delta_z = (log(MAXPRICE) - log(MINPRICE)) / J; for (i = 0; i &lt; J + 1; i++) fprintf(fcbprice, "%lf;%lf\n", exp(log(MINPRICE) + i * delta_z), cbprice[i]); // fclose(fstockprice); fclose(fcbprice); // , free(stockprice); free(cbprice); return 0; }</p> <p> : 1) ; 2) , . , , :</p> <p>23</p> <p>1. computecoeff_a(), computecoeff_b(). (15) (20).// step = T / // delta_t = // delta_z = // rk_s = // sigma - // D, rf int computecoeff_a(double sigma, double delta_t, double delta_z, double D, double rf, double *a_0, double *a_1, double *a_2) { double sqsigma = sigma * sigma; double sqdeltaz = delta_z * delta_z; (*a_0) = (sqsigma / (4.0 * sqdeltaz) (rf - D - sqsigma * 0.5) / (4.0 * delta_z)) * delta_t; (*a_1) = -1.0 - (sqsigma / (2.0 * sqdeltaz) + rf * 0.5) * delta_t; (*a_2) = (sqsigma / (4.0 * sqdeltaz) + (rf - D - sqsigma * 0.5) / (4.0 * delta_z)) * delta_t; return 0; } int computecoeff_b(double sigma, double delta_t, double delta_z, double D, double rf, double *b_0, double *b_1, double *b_2) { double sqsigma = sigma * sigma; double sqdeltaz = delta_z * delta_z; (*b_0) = (-sqsigma / (4.0 * sqdeltaz) + (rf - D - sqsigma * 0.5) / (4.0 * delta_z)) * delta_t; (*b_1) = -1.0 + (sqsigma / (2.0 * sqdeltaz) + rf * 0.5) * delta_t; (*b_2) = (sqsigma / (4.0 * sqdeltaz) + (rf - D - sqsigma * 0.5) / (4.0 * delta_z)) * (-delta_t); return 0; }</p> <p>2. rightboundcondlast() rightboundcond() (23) (24).// // // // zmin = delta_z = K Ic </p> <p>24</p> <p>int rightboundcond(double K, double Ic, double zmin, double delta_z, double rk, double *ck0j, int J, double *res) { int i; double coeff1, coeff2, fe, se, step; coeff1 = K * rk; coeff2 = K / Ic; for (i = 0; i &lt; J + 1; i++) { step = zmin + i * delta_z; fe = coeff1 + ck0j[i]; se = coeff2 * exp(step); res[i] = (fe &gt; se) ? fe : se; } return 0; } int rightboundcondlast(double K, double Ic, double zmin, double delta_z, double rn, int J, double *res) { int i; double fe, se, coeff; fe = K * (1.0 + rn); coeff = K / Ic; for (i = 0; i &lt; J + 1; i++) { se = coeff * exp(zmin + i * delta_z); res[i] = (fe &gt; se) ? fe : se; } return 0; }</p> <p>3. bottomboundcond() (25).</p> <p>// step = T / // delta_t = // rfunc // K // rf int bottomboundcond(int kind, int m, int n, double step, double K, double rf, double *rfunc, double delta_t, double *cond) { int l; double coeff, term; coeff = K * exp((-1.0) * rf * (step - m * delta_t)); term = exp((-1.0) * rf * (n - kind - 1) * step);</p> <p> *cond = 0.0; for (l = 0; l &lt; n - kind; l++) *cond += (rfunc[kind + l + 1]*exp(-rf * l * step)); (*cond) += term; (*cond) *= coeff; return 0; }</p> <p>25</p> <p>4. topboundcond() (26).// step = T / // delta_t = // K // D, Ic // zmax = int topboundcond(int m, double K, double D, double Ic, double delta_t, double step, double zmax, double *cond) { (*cond) = K * exp(zmax - D * (step - m * delta_t))/Ic; return 0; }</p> <p>5. getoptimalstockprice() (. (27)).// step = T / // delta_t = // delta_z = // zmin = // K // Ic int getoptimalstockprice(double K, double Ic, double zmin, double step, double *rfunc,double delta_z, double delta_t, int J, int kind, double *c, double *V, int *optindex) { int i, index; double gkj, rk, coeff, tmp, min; rk = rfunc[kind]; coeff = K * rk; tmp = K / Ic; min = fabs(coeff + c[0] - tmp * exp(zmin)); index = 0; for (i = 1; i &lt; J; i++) { gkj = fabs(coeff+c[i]-tmp*exp(zmin+i*delta_z)); if (gkj &lt; min) {</p> <p>26</p> <p>min = gkj; index = i; } } *V = exp(zmin + index * delta_z); *optindex = index; return 0; }</p> <p>6. matrvecmulti() (. -). , .// b_0 // b_1 // b_2 int matrvecmulti(double b_0, double b_1, double b_2, double *vec, int J, double *res) { int i; for (i = 0; i &lt; J - 1; i++) res[i] = b_0*vec[i] + b_1*vec[i+1] + b_2*vec[i+2]; return 0; }</p> <p>7. , , sweepmethod(). , , . 4.// a_0 // a_1 // a_2 // alpha // beta - int sweepmethod(double a_0, double a_1, double a_2, double *cb_next, int J, double *x, double *alpha, double *beta) { int size, i; double denominator; size = J - 1; // alpha[1] = -a_2 / a_1;</p> <p> beta[1] = cb_next[0] / a_1; for (i = 1; i &lt; size - 1; i++) { denominator = a_0 * alpha[i] + a_1; alpha[i + 1] = -a_2 / denominator; beta[i + 1] = (cb_next[i] - a_0 * beta[i]) / denominator; } // x[size - 1] = (-a_0 * beta[size - 1] + cb_next[size - 1]) / ( a_1 + a_0 * alpha[size - 1]); for (i = size - 2; i &gt;= 0; i--) { x[i] = alpha[i + 1] * x[i + 1] + beta[i + 1]; } return OPERATION_OK; }</p> <p>27</p> <p>8. r(), , (1).// n // m // m = , step = T / // delta_t = // rk_s = //EPS = 0.000001 double* r(double step, double delta_t, double rk_s, int n, int m) { int i, j; double *r = (double *)malloc(sizeof(double) * (n + 1)); for (i = 0; i = 0; kind--) { rk = rfunc[kind]; for (i = M; i &gt; 0; i--) { matrvecmulti(b_0, b_1, b_2, x_next, J, cb_next + 1); topboundcond(i, K, D, Ic, delta_t, step, zmax, &amp;topborder); bottomboundcond(kind, i, n, step, K, rf, rfunc, delta_t, &amp;bottomborder); cb_next[1] -= (a_0 * bottomborder); cb_next[size + 1] -= (a_2 * topborder); CycleReductionMethod(x_next, a_0, a_1, a_2,</p> <p>34</p> <p>a, b, c, cb_next, J, q); } // getoptimalstockprice(K, Ic, zmin, step, rfunc, delta_z, delta_t, J, kind, x_next, &amp;(V[kind]), &amp;optindex); rightboundcond(K, Ic, zmin, delta_z, rfunc[kind], x_next, J, x_next); } *cb_price = x_next; free(cb_next); free(rfunc); free(a); free(b); free(c); return 0; }</p> <p> , , -, , BuildRebuild 02_Cycle. . , 6.1 ( , . 2).</p> <p>6.3. OpenMP, OpenMP [7], 07_PDE 03_CycleParallel. , 6.2. CycleReduction.h CycleReduction.cpp. main.cpp, DiffEquation.h, DiffEquation.cpp,CycleReduction.h, CycleReduction.cpp main.cpp, DiffEquation.h, DiffEquation.cpp, CycleReduction.h, CycleReduction.cpp 02_Cycle. OpenMP. , ProjectProperties. Configuration Properties C/C++Language OpenMP Support : Generate Parallel Code (/openmp, equiv. to /Qopenmp).</p> <p>35</p> <p> , . 4 . 5. , . , , , ( 18 29 ). , OpenMP- OpenMP pragma omp parallel for [7] . . , , .// THREADSNUM , #pragma omp parallel for num_threads(THREADS_NUMBER) for (i = 0; i &lt; elementsNum; i++) { int k = start * (i + 1); f[k] = alpha*f[k-step] + f[k] + beta*f[k+step]; }</p> <p> . (THREADSNUM), . , BuildRebuild 03_CycleParallel, , 6.1 ( , . 2). , , . , , Intel Parallel Inspector ( Intel Parallel Inspector XE) Threading Errors (Threading Error Analysis Intel Parallel Inspector XE).</p> <p>6.4.</p> <p> Intel Threading Building Blocks</p> <p> , Intel TBB [8, 9] 07_PDE 04_CycleTBB. , 6.1-6.3. main.cpp, DiffEquation.h, Di...</p>

Recommended

View more >