fmatrix.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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. /*
  17. * This examplifies how to use partitioning filters. We here just split a 2D
  18. * matrix into 2D slices (along the X axis), and run a dumb kernel on them.
  19. */
  20. #include <starpu.h>
  21. #define NX 5
  22. #define NY 4
  23. #define PARTS 2
  24. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  25. void cpu_func(void *buffers[], void *cl_arg)
  26. {
  27. unsigned i, j;
  28. int *factor = (int *) cl_arg;
  29. /* length of the matrix */
  30. unsigned nx = STARPU_MATRIX_GET_NX(buffers[0]);
  31. unsigned ny = STARPU_MATRIX_GET_NY(buffers[0]);
  32. unsigned ld = STARPU_MATRIX_GET_LD(buffers[0]);
  33. /* local copy of the matrix pointer */
  34. int *val = (int *)STARPU_MATRIX_GET_PTR(buffers[0]);
  35. for(j=0; j<ny ; j++)
  36. {
  37. for(i=0; i<nx ; i++)
  38. val[(j*ld)+i] *= *factor;
  39. }
  40. }
  41. int main(void)
  42. {
  43. unsigned j;
  44. int n=1;
  45. int matrix[NX*NY];
  46. int ret, i;
  47. int factor = 12;
  48. FPRINTF(stderr,"IN Matrix: \n");
  49. for(j=0 ; j<NY ; j++)
  50. {
  51. for(i=0 ; i<NX ; i++)
  52. {
  53. matrix[(j*NX)+i] = n++;
  54. FPRINTF(stderr, "%4d ", matrix[(j*NX)+i]);
  55. }
  56. FPRINTF(stderr,"\n");
  57. }
  58. FPRINTF(stderr,"\n");
  59. starpu_data_handle_t handle;
  60. struct starpu_codelet cl =
  61. {
  62. .cpu_funcs = {cpu_func},
  63. .cpu_funcs_name = {"cpu_func"},
  64. .nbuffers = 1,
  65. .modes = {STARPU_RW},
  66. .name = "matrix_scal"
  67. };
  68. ret = starpu_init(NULL);
  69. if (ret == -ENODEV)
  70. return 77;
  71. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  72. /* Declare data to StarPU */
  73. starpu_matrix_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)matrix, NX, NX, NY, sizeof(matrix[0]));
  74. /* Partition the matrix in PARTS sub-matrices */
  75. struct starpu_data_filter f =
  76. {
  77. .filter_func = starpu_matrix_filter_block,
  78. .nchildren = PARTS
  79. };
  80. starpu_data_partition(handle, &f);
  81. /* Submit a task on each sub-vector */
  82. for (i=0; i<starpu_data_get_nb_children(handle); i++)
  83. {
  84. struct starpu_task *task = starpu_task_create();
  85. task->handles[0] = starpu_data_get_sub_data(handle, 1, i);
  86. task->cl = &cl;
  87. task->synchronous = 1;
  88. task->cl_arg = &factor;
  89. task->cl_arg_size = sizeof(factor);
  90. ret = starpu_task_submit(task);
  91. if (ret == -ENODEV) goto enodev;
  92. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  93. }
  94. /* Unpartition the data, unregister it from StarPU and shutdown */
  95. starpu_data_unpartition(handle, STARPU_MAIN_RAM);
  96. starpu_data_unregister(handle);
  97. starpu_shutdown();
  98. /* Print result matrix */
  99. n=1;
  100. FPRINTF(stderr,"OUT Matrix: \n");
  101. for(j=0 ; j<NY ; j++)
  102. {
  103. for(i=0 ; i<NX ; i++)
  104. {
  105. FPRINTF(stderr, "%4d ", matrix[(j*NX)+i]);
  106. if (matrix[(j*NX)+i] != (int) n*12)
  107. {
  108. FPRINTF(stderr, "Incorrect result %4d != %4d", matrix[(j*NX)+i], n*12);
  109. ret=1;
  110. }
  111. n++;
  112. }
  113. FPRINTF(stderr,"\n");
  114. }
  115. FPRINTF(stderr,"\n");
  116. return ret;
  117. enodev:
  118. starpu_shutdown();
  119. return 77;
  120. }