stencil5.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011, 2013 CNRS
  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. #define NITER_DEF 10
  32. #define X 4
  33. #define Y 4
  34. int display = 0;
  35. int niter = NITER_DEF;
  36. static void parse_args(int argc, char **argv)
  37. {
  38. int i;
  39. for (i = 1; i < argc; i++) {
  40. if (strcmp(argv[i], "-iter") == 0) {
  41. char *argptr;
  42. niter = strtol(argv[++i], &argptr, 10);
  43. }
  44. if (strcmp(argv[i], "-display") == 0) {
  45. display = 1;
  46. }
  47. }
  48. }
  49. static float my_rand (void)
  50. {
  51. return (float) rand () / (float) RAND_MAX;
  52. }
  53. int main(int argc, char **argv)
  54. {
  55. int x, y;
  56. float mean=0;
  57. float matrix[X][Y];
  58. parse_args(argc, argv);
  59. srand (time (NULL));
  60. for(x = 0; x < X; x++) {
  61. for (y = 0; y < Y; y++) {
  62. matrix[x][y] = my_rand () * 100;
  63. mean += matrix[x][y];
  64. }
  65. }
  66. mean /= (x*y);
  67. if (display) {
  68. fprintf(stdout, "mean=%f\n", mean);
  69. for(x = 0; x < X; x++) {
  70. for (y = 0; y < Y; y++) {
  71. fprintf(stdout, "%3f ", matrix[x][y]);
  72. }
  73. fprintf(stdout, "\n");
  74. }
  75. }
  76. #pragma starpu initialize
  77. for(x = 0; x < X; x++) {
  78. for (y = 0; y < Y; y++) {
  79. #pragma starpu register &matrix[x][y] 1
  80. }
  81. }
  82. while(niter--) {
  83. for (x = 1; x < X-1; x++) {
  84. for (y = 1; y < Y-1; y++) {
  85. stencil5(&matrix[x][y], &matrix[x-1][y], &matrix[x+1][y],
  86. &matrix[x][y-1], &matrix[x][y+1]);
  87. }
  88. }
  89. }
  90. #pragma starpu wait
  91. for(x = 0; x < X; x++) {
  92. for (y = 0; y < Y; y++) {
  93. #pragma starpu unregister &matrix[x][y]
  94. }
  95. }
  96. #pragma starpu shutdown
  97. if (display) {
  98. fprintf(stdout, "mean=%f\n", mean);
  99. for(x = 0; x < X; x++) {
  100. for (y = 0; y < Y; y++) {
  101. fprintf(stdout, "%3f ", matrix[x][y]);
  102. }
  103. fprintf(stdout, "\n");
  104. }
  105. }
  106. return EXIT_SUCCESS;
  107. }