fmatrix.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011, 2012, 2013, 2015 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. /*
  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(int argc, char **argv)
  42. {
  43. unsigned j, n=1;
  44. int matrix[NX*NY];
  45. int ret, i;
  46. int factor = 12;
  47. FPRINTF(stderr,"IN Matrix: \n");
  48. for(j=0 ; j<NY ; j++)
  49. {
  50. for(i=0 ; i<NX ; i++)
  51. {
  52. matrix[(j*NX)+i] = n++;
  53. FPRINTF(stderr, "%4d ", matrix[(j*NX)+i]);
  54. }
  55. FPRINTF(stderr,"\n");
  56. }
  57. FPRINTF(stderr,"\n");
  58. starpu_data_handle_t handle;
  59. struct starpu_codelet cl =
  60. {
  61. .cpu_funcs = {cpu_func},
  62. .cpu_funcs_name = {"cpu_func"},
  63. .nbuffers = 1,
  64. .modes = {STARPU_RW},
  65. .name = "matrix_scal"
  66. };
  67. ret = starpu_init(NULL);
  68. if (ret == -ENODEV)
  69. return 77;
  70. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  71. /* Declare data to StarPU */
  72. starpu_matrix_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)matrix, NX, NX, NY, sizeof(matrix[0]));
  73. /* Partition the matrix in PARTS sub-matrices */
  74. struct starpu_data_filter f =
  75. {
  76. .filter_func = starpu_matrix_filter_block,
  77. .nchildren = PARTS
  78. };
  79. starpu_data_partition(handle, &f);
  80. /* Submit a task on each sub-vector */
  81. for (i=0; i<starpu_data_get_nb_children(handle); i++)
  82. {
  83. struct starpu_task *task = starpu_task_create();
  84. task->handles[0] = starpu_data_get_sub_data(handle, 1, i);
  85. task->cl = &cl;
  86. task->synchronous = 1;
  87. task->cl_arg = &factor;
  88. task->cl_arg_size = sizeof(factor);
  89. ret = starpu_task_submit(task);
  90. if (ret == -ENODEV) goto enodev;
  91. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  92. }
  93. /* Unpartition the data, unregister it from StarPU and shutdown */
  94. starpu_data_unpartition(handle, STARPU_MAIN_RAM);
  95. starpu_data_unregister(handle);
  96. starpu_shutdown();
  97. /* Print result matrix */
  98. n=1;
  99. FPRINTF(stderr,"OUT Matrix: \n");
  100. for(j=0 ; j<NY ; j++)
  101. {
  102. for(i=0 ; i<NX ; i++)
  103. {
  104. FPRINTF(stderr, "%4d ", matrix[(j*NX)+i]);
  105. if (matrix[(j*NX)+i] != (int) n*12)
  106. {
  107. FPRINTF(stderr, "Incorrect result %4d != %4d", matrix[(j*NX)+i], n*12);
  108. ret=1;
  109. }
  110. n++;
  111. }
  112. FPRINTF(stderr,"\n");
  113. }
  114. FPRINTF(stderr,"\n");
  115. return ret;
  116. enodev:
  117. starpu_shutdown();
  118. return 77;
  119. }