jacobi.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include <string.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <omp.h>
  31. #include "laplace2d.h"
  32. int main(int argc, char** argv)
  33. {
  34. int n, m, iter_max;
  35. if(argc > 1){
  36. n = atoi(argv[1]);
  37. } else {
  38. n = 4096;
  39. }
  40. if(argc > 2){
  41. m = atoi(argv[2]);
  42. } else {
  43. m = 4096;
  44. }
  45. if(argc > 3){
  46. iter_max = atoi(argv[3]);
  47. } else {
  48. iter_max = 1000;
  49. }
  50. const double tol = 1.0e-6;
  51. double error = 1.0;
  52. double *restrict A = (double*)malloc(sizeof(double)*n*m);
  53. double *restrict Anew = (double*)malloc(sizeof(double)*n*m);
  54. initialize(A, Anew, m, n);
  55. printf("Jacobi relaxation Calculation: %d x %d mesh\n", n, m);
  56. double st = omp_get_wtime();
  57. int iter = 0;
  58. #pragma acc data copy(A[:n*m]) create(Anew[:n*m])
  59. while ( error > tol && iter < iter_max )
  60. {
  61. error = calcNext(A, Anew, m, n);
  62. swap(A, Anew, m, n);
  63. if(iter % 100 == 0) printf("%5d, %0.6f\n", iter, error);
  64. iter++;
  65. }
  66. double runtime = omp_get_wtime() - st;
  67. printf(" total: %f s\n", runtime);
  68. deallocate(A, Anew);
  69. return 0;
  70. }