--sas

apr29-parteplo.c

May 4th, 2022
960
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.23 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <omp.h>
  5.  
  6. int main (int argc, char *argv[]) {
  7.     omp_set_num_threads(omp_get_num_procs());
  8.  
  9.     double *u, *unew, delta, maxdelta;
  10.     double eps = 1.0e-6;
  11.     double h, tau;
  12.  
  13.     int N;
  14.     int i;
  15.  
  16.     FILE *ff;
  17.  
  18.     if (argc != 2) {
  19.         printf("Usage: exefile npoints\n");
  20.         exit(-1);
  21.     }
  22.  
  23.     N = atoi(argv[1]);
  24.     if (N == 0) {
  25.         printf("Set N to 1000\n");
  26.         N = 1000;
  27.     }
  28.     else {
  29.         printf("Set N to %d\n", N);
  30.     }
  31.  
  32.     if ((u = malloc((N + 1) * sizeof(double))) == NULL) {
  33.         printf("Can't allocate memory for u\n");
  34.         exit(-1);
  35.     }
  36.  
  37.     if ((unew = malloc((N + 1) * sizeof(double))) == NULL) {
  38.         printf("Can't allocate memory for unew\n");
  39.         free(u);
  40.         exit(-1);
  41.     }
  42.  
  43.     // begin & bound values
  44.  
  45.     for (i = 1; i < N; i++)
  46.         u[i] = 0;
  47.  
  48.     unew[0] = u[0] = 1.0;
  49.     unew[N] = u[N] = 0.0;
  50.  
  51.     h = 1.0 / (double) N;
  52.     tau = 0.5 * (h * h);
  53.  
  54. #pragma omp parallel shared(u, unew, maxdelta, eps, h, tau, N) private(delta, i)
  55.     {
  56.         while (1) {
  57. #pragma omp for
  58.             for (i = 1; i < N; i++) {
  59.                 unew[i] = u[i] + (tau / (h * h)) * (u[i - 1] - 2 * u[i] + u[i + 1]);
  60.             }
  61.  
  62. #pragma omp single
  63.             {
  64.                 maxdelta = 0.0;
  65.             }
  66.  
  67. #pragma omp for
  68.             for (i = 1; i < N; i++) {
  69.                 delta = fabs(unew[i] - u[i]);
  70.                 if (delta > maxdelta) {
  71. #pragma omp critical
  72.                     {
  73.                         if (delta > maxdelta) {
  74.                             maxdelta = delta;
  75.                         }
  76.                     }
  77.                 }
  78.             }
  79.  
  80. #pragma omp barrier
  81.             if (maxdelta < eps) {
  82.                 break;
  83.             }
  84.  
  85. #pragma omp for
  86.             for (i = 1; i < N; i++) {
  87.                 u[i] = unew[i];
  88.             }
  89.  
  90. #pragma omp barrier
  91.         }
  92.     }
  93.  
  94.     if ((ff = fopen("apr29-parteplo.dat", "w+")) == NULL) {
  95.         printf("Can't open file\n");
  96.         free(u);
  97.         free(unew);
  98.         exit(-1);
  99.     }
  100.  
  101.     for (i = 0; i < N + 1; i++)
  102.         fprintf(ff, "%f ", unew[i]);
  103.  
  104.     fclose(ff);
  105.     free(u);
  106.     free(unew);
  107.  
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment