Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. /* Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions
  5. * are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of NVIDIA CORPORATION nor the names of its
  12. * contributors may be used to endorse or promote products derived
  13. * from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
  16. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  23. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27.  
  28. #include <math.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include <omp.h>
  32. #include <stdio.h>
  33.  
  34. double simplest_checksum(int imax, int jmax, double (*in)[jmax+2]) {
  35. double checksum = 0.0;
  36. int i;
  37. int j;
  38. for (i=1; i<(imax+1); i++){
  39. for (j=1; j<(jmax+1); j++){
  40. checksum+=in[i][j]*((double)((i)*jmax)+(j));
  41. }
  42. }
  43.  
  44. return checksum;
  45. }
  46.  
  47. void print_file(int imax, int jmax, double (*in)[jmax+2]) {
  48. char fname[10];
  49. sprintf(fname,"out.txt");
  50. FILE *f=fopen(fname,"w");
  51. int i;
  52. int j;
  53. for (i=0; i<(imax+2); i++){
  54. for (j=0; j<(jmax+2); j++){
  55. fprintf(f,"%g ",in[i][j]);
  56. }
  57. fprintf(f,"\n");
  58. }
  59. fclose(f);
  60. }
  61.  
  62. void func(int imax, int jmax, double (*restrict A)[jmax+2], double (*restrict Anew)[jmax+2]) {
  63. #pragma acc parallel loop present(A,Anew) collapse(2)
  64. for( int i = 1; i < imax+1; i++ )
  65. {
  66. for( int j = 1; j < jmax+1; j++)
  67. {
  68. A[i][j] = Anew[i][j];
  69. }
  70. }
  71. }
  72.  
  73. int main(int argc, char** argv)
  74. {
  75. //Size along y
  76. int jmax = 4094;
  77. //Size along x
  78. int imax = 4094;
  79. int iter_max = 1000;
  80.  
  81. const double pi = 2.0 * asin(1.0);
  82. const double tol = 1.0e-5;
  83. double error = 1.0;
  84.  
  85. double (*restrict A)[jmax+2];
  86. double (*restrict Anew)[jmax+2];
  87. double *restrict y0;
  88.  
  89. A = (double (*)[jmax+2])malloc((imax+2) * (jmax+2) * sizeof(double));
  90. Anew = (double (*)[jmax+2])malloc((imax+2) * (jmax+2) * sizeof(double));
  91. y0 = (double *)malloc((imax+2) * sizeof(double));
  92.  
  93. memset(A, 0, (imax+2) * (jmax+2) * sizeof(double));
  94.  
  95. // set boundary conditions
  96. for (int j = 0; j < jmax+2; j++)
  97. A[0][j] = 0.0;
  98.  
  99. for (int j = 0; j < jmax+2; j++)
  100. A[imax+1][j] = 0.0;
  101.  
  102. for (int i = 0; i < imax+2; i++)
  103. {
  104. y0[i] = sin(pi * (i) / (imax+1));
  105. A[i][0] = y0[i];
  106. }
  107.  
  108. for (int i = 0; i < imax+2; i++)
  109. {
  110. y0[i] = sin(pi * (i) / (imax+1));
  111. A[i][jmax+1] = y0[i]*exp(-pi);
  112. }
  113.  
  114. printf("Jacobi relaxation Calculation: %d x %d mesh\n", imax+2, jmax+2);
  115.  
  116. double t1 = omp_get_wtime();
  117. int iter = 0;
  118.  
  119. for (int j = 1; j < jmax+2; j++)
  120. Anew[0][j] = 0.0;
  121.  
  122. for (int j = 1; j < jmax+2; j++)
  123. Anew[imax+1][j] = 0.0;
  124.  
  125. for (int i = 1; i < imax+2; i++)
  126. Anew[i][0] = y0[i];
  127.  
  128. for (int i = 1; i < imax+2; i++)
  129. Anew[i][jmax+1] = y0[i]*expf(-pi);
  130.  
  131. #pragma acc data copy(A[0:imax+2] [0:jmax+2], Anew[0:imax+2] [0:jmax+2])
  132. while ( error > tol && iter < iter_max )
  133. {
  134. error = 0.0;
  135. #pragma acc parallel loop reduction(max:error)
  136. for( int i = 1; i < imax+1; i++ )
  137. {
  138. for( int j = 1; j < jmax+1; j++)
  139. {
  140. Anew[i][j] = 0.25f * ( A[i][j+1] + A[i][j-1]
  141. + A[i-1][j] + A[i+1][j]);
  142. error = fmax( error, fabs(Anew[i][j]-A[i][j]));
  143. }
  144. }
  145.  
  146. func(imax, jmax, A, Anew);
  147.  
  148. if(iter % 100 == 0) printf("%5d, %0.8f\n", iter, error);
  149. iter++;
  150. }
  151. double checksum = simplest_checksum(imax,jmax,A);
  152. printf("Checksum: %4.8f\n",checksum);
  153. double runtime = omp_get_wtime()-t1;
  154.  
  155. printf(" total: %f s\n", runtime);
  156.  
  157. return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement