stencil5.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 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 <stdlib.h>
  17. #include <math.h>
  18. #ifndef STARPU_GCC_PLUGIN
  19. # error must be compiled with the StarPU GCC plug-in
  20. #endif
  21. /* Definition of the StarPU task and its CPU implementation. */
  22. static void stencil5(float *xy, const float *xm1y, const float *xp1y, const float *xym1, const float *xyp1)
  23. __attribute__ ((task));
  24. static void stencil5_cpu(float *xy, const float *xm1y, const float *xp1y, const float *xym1, const float *xyp1)
  25. __attribute__ ((task_implementation ("cpu", stencil5)));
  26. static void stencil5_cpu(float *xy, const float *xm1y, const float *xp1y, const float *xym1, const float *xyp1)
  27. {
  28. *xy = (*xy + *xm1y + *xp1y + *xym1 + *xyp1) / 5;
  29. }
  30. #define NITER_DEF 20000
  31. #define X 10
  32. #define Y 10
  33. int display = 0;
  34. int niter = NITER_DEF;
  35. static void parse_args(int argc, char **argv)
  36. {
  37. int i;
  38. for (i = 1; i < argc; i++) {
  39. if (strcmp(argv[i], "-iter") == 0) {
  40. char *argptr;
  41. niter = strtol(argv[++i], &argptr, 10);
  42. }
  43. if (strcmp(argv[i], "-display") == 0) {
  44. display = 1;
  45. }
  46. }
  47. }
  48. static float my_rand (void)
  49. {
  50. return (float) rand () / (float) RAND_MAX;
  51. }
  52. int main(int argc, char **argv)
  53. {
  54. int x, y;
  55. float mean=0;
  56. float matrix[X][Y];
  57. parse_args(argc, argv);
  58. srand (time (NULL));
  59. for(x = 0; x < X; x++) {
  60. for (y = 0; y < Y; y++) {
  61. matrix[x][y] = my_rand () * 100;
  62. mean += matrix[x][y];
  63. }
  64. }
  65. mean /= (x*y);
  66. if (display) {
  67. fprintf(stdout, "mean=%f\n", mean);
  68. for(x = 0; x < X; x++) {
  69. for (y = 0; y < Y; y++) {
  70. fprintf(stdout, "%3f ", matrix[x][y]);
  71. }
  72. fprintf(stdout, "\n");
  73. }
  74. }
  75. #pragma starpu initialize
  76. for(x = 0; x < X; x++) {
  77. for (y = 0; y < Y; y++) {
  78. #pragma starpu register &matrix[x][y] 1
  79. }
  80. }
  81. while(niter--) {
  82. for (x = 1; x < X-1; x++) {
  83. for (y = 1; y < Y-1; y++) {
  84. stencil5(&matrix[x][y], &matrix[x-1][y], &matrix[x+1][y],
  85. &matrix[x][y-1], &matrix[x][y+1]);
  86. }
  87. }
  88. }
  89. #pragma starpu wait
  90. for(x = 0; x < X; x++) {
  91. for (y = 0; y < Y; y++) {
  92. #pragma starpu unregister &matrix[x][y]
  93. }
  94. }
  95. #pragma starpu shutdown
  96. if (display) {
  97. fprintf(stdout, "mean=%f\n", mean);
  98. for(x = 0; x < X; x++) {
  99. for (y = 0; y < Y; y++) {
  100. fprintf(stdout, "%3f ", matrix[x][y]);
  101. }
  102. fprintf(stdout, "\n");
  103. }
  104. }
  105. return EXIT_SUCCESS;
  106. }