stencil5.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011, 2013 Centre National de la Recherche Scientifique
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <math.h>
  19. #ifndef STARPU_GCC_PLUGIN
  20. # error must be compiled with the StarPU GCC plug-in
  21. #endif
  22. /* Definition of the StarPU task and its CPU implementation. */
  23. static void stencil5(float *xy, const float *xm1y, const float *xp1y, const float *xym1, const float *xyp1)
  24. __attribute__ ((task));
  25. static void stencil5_cpu(float *xy, const float *xm1y, const float *xp1y, const float *xym1, const float *xyp1)
  26. __attribute__ ((task_implementation ("cpu", stencil5)));
  27. static void stencil5_cpu(float *xy, const float *xm1y, const float *xp1y, const float *xym1, const float *xyp1)
  28. {
  29. *xy = (*xy + *xm1y + *xp1y + *xym1 + *xyp1) / 5;
  30. }
  31. #ifdef STARPU_QUICK_CHECK
  32. # define NITER_DEF 5
  33. # define X 3
  34. # define Y 3
  35. #else
  36. # define NITER_DEF 500
  37. # define X 20
  38. # define Y 20
  39. #endif
  40. int display = 0;
  41. int niter = NITER_DEF;
  42. static void parse_args(int argc, char **argv)
  43. {
  44. int i;
  45. for (i = 1; i < argc; i++) {
  46. if (strcmp(argv[i], "-iter") == 0) {
  47. char *argptr;
  48. niter = strtol(argv[++i], &argptr, 10);
  49. }
  50. if (strcmp(argv[i], "-display") == 0) {
  51. display = 1;
  52. }
  53. }
  54. }
  55. static float my_rand (void)
  56. {
  57. return (float) rand () / (float) RAND_MAX;
  58. }
  59. int main(int argc, char **argv)
  60. {
  61. int x, y;
  62. float mean=0;
  63. float matrix[X][Y];
  64. parse_args(argc, argv);
  65. srand (time (NULL));
  66. for(x = 0; x < X; x++) {
  67. for (y = 0; y < Y; y++) {
  68. matrix[x][y] = my_rand () * 100;
  69. mean += matrix[x][y];
  70. }
  71. }
  72. mean /= (x*y);
  73. if (display) {
  74. fprintf(stdout, "mean=%f\n", mean);
  75. for(x = 0; x < X; x++) {
  76. for (y = 0; y < Y; y++) {
  77. fprintf(stdout, "%3f ", matrix[x][y]);
  78. }
  79. fprintf(stdout, "\n");
  80. }
  81. }
  82. #pragma starpu initialize
  83. for(x = 0; x < X; x++) {
  84. for (y = 0; y < Y; y++) {
  85. #pragma starpu register &matrix[x][y] 1
  86. }
  87. }
  88. while(niter--) {
  89. for (x = 1; x < X-1; x++) {
  90. for (y = 1; y < Y-1; y++) {
  91. stencil5(&matrix[x][y], &matrix[x-1][y], &matrix[x+1][y],
  92. &matrix[x][y-1], &matrix[x][y+1]);
  93. }
  94. }
  95. }
  96. #pragma starpu wait
  97. for(x = 0; x < X; x++) {
  98. for (y = 0; y < Y; y++) {
  99. #pragma starpu unregister &matrix[x][y]
  100. }
  101. }
  102. #pragma starpu shutdown
  103. if (display) {
  104. fprintf(stdout, "mean=%f\n", mean);
  105. for(x = 0; x < X; x++) {
  106. for (y = 0; y < Y; y++) {
  107. fprintf(stdout, "%3f ", matrix[x][y]);
  108. }
  109. fprintf(stdout, "\n");
  110. }
  111. }
  112. return EXIT_SUCCESS;
  113. }