starpu_mpi_datatype.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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 <starpu_mpi_datatype.h>
  17. /*
  18. * MPI_* functions usually requires both a pointer to the first element of
  19. * a datatype and the datatype itself, so we need to provide both.
  20. */
  21. typedef int (*handle_to_datatype_func)(starpu_data_handle, MPI_Datatype *);
  22. typedef void *(*handle_to_ptr_func)(starpu_data_handle);
  23. /*
  24. * Matrix
  25. */
  26. static int handle_to_datatype_matrix(starpu_data_handle data_handle, MPI_Datatype *datatype)
  27. {
  28. int ret;
  29. unsigned nx = starpu_matrix_get_nx(data_handle);
  30. unsigned ny = starpu_matrix_get_ny(data_handle);
  31. unsigned ld = starpu_matrix_get_local_ld(data_handle);
  32. size_t elemsize = starpu_matrix_get_elemsize(data_handle);
  33. ret = MPI_Type_vector(ny, nx*elemsize, ld*elemsize, MPI_BYTE, datatype);
  34. STARPU_ASSERT(ret == MPI_SUCCESS);
  35. ret = MPI_Type_commit(datatype);
  36. STARPU_ASSERT(ret == MPI_SUCCESS);
  37. return 0;
  38. }
  39. static void *handle_to_ptr_matrix(starpu_data_handle data_handle)
  40. {
  41. return (void *)starpu_matrix_get_local_ptr(data_handle);
  42. }
  43. /*
  44. * Block
  45. */
  46. static int handle_to_datatype_block(starpu_data_handle data_handle, MPI_Datatype *datatype)
  47. {
  48. int ret;
  49. unsigned nx = starpu_block_get_nx(data_handle);
  50. unsigned ny = starpu_block_get_ny(data_handle);
  51. unsigned nz = starpu_block_get_nz(data_handle);
  52. unsigned ldy = starpu_block_get_local_ldy(data_handle);
  53. unsigned ldz = starpu_block_get_local_ldz(data_handle);
  54. size_t elemsize = starpu_block_get_elemsize(data_handle);
  55. MPI_Datatype datatype_2dlayer;
  56. ret = MPI_Type_vector(ny, nx*elemsize, ldy*elemsize, MPI_BYTE, &datatype_2dlayer);
  57. STARPU_ASSERT(ret == MPI_SUCCESS);
  58. ret = MPI_Type_commit(&datatype_2dlayer);
  59. STARPU_ASSERT(ret == MPI_SUCCESS);
  60. ret = MPI_Type_hvector(nz, 1, ldz*elemsize, datatype_2dlayer, datatype);
  61. STARPU_ASSERT(ret == MPI_SUCCESS);
  62. ret = MPI_Type_commit(datatype);
  63. STARPU_ASSERT(ret == MPI_SUCCESS);
  64. return 0;
  65. }
  66. static void *handle_to_ptr_block(starpu_data_handle data_handle)
  67. {
  68. return (void *)starpu_block_get_local_ptr(data_handle);
  69. }
  70. /*
  71. * Vector
  72. */
  73. static int handle_to_datatype_vector(starpu_data_handle data_handle, MPI_Datatype *datatype)
  74. {
  75. int ret;
  76. unsigned nx = starpu_vector_get_nx(data_handle);
  77. size_t elemsize = starpu_vector_get_elemsize(data_handle);
  78. ret = MPI_Type_contiguous(nx*elemsize, MPI_BYTE, datatype);
  79. STARPU_ASSERT(ret == MPI_SUCCESS);
  80. ret = MPI_Type_commit(datatype);
  81. STARPU_ASSERT(ret == MPI_SUCCESS);
  82. return 0;
  83. }
  84. static void *handle_to_ptr_vector(starpu_data_handle data_handle)
  85. {
  86. return (void *)starpu_vector_get_local_ptr(data_handle);
  87. }
  88. /*
  89. * Generic
  90. */
  91. static handle_to_datatype_func handle_to_datatype_funcs[STARPU_NINTERFACES_ID] = {
  92. [STARPU_MATRIX_INTERFACE_ID] = handle_to_datatype_matrix,
  93. [STARPU_BLOCK_INTERFACE_ID] = handle_to_datatype_block,
  94. [STARPU_VECTOR_INTERFACE_ID] = handle_to_datatype_vector,
  95. [STARPU_CSR_INTERFACE_ID] = NULL,
  96. [STARPU_BCSR_INTERFACE_ID] = NULL
  97. };
  98. static handle_to_ptr_func handle_to_ptr_funcs[STARPU_NINTERFACES_ID] = {
  99. [STARPU_MATRIX_INTERFACE_ID] = handle_to_ptr_matrix,
  100. [STARPU_BLOCK_INTERFACE_ID] = handle_to_ptr_block,
  101. [STARPU_VECTOR_INTERFACE_ID] = handle_to_ptr_vector,
  102. [STARPU_CSR_INTERFACE_ID] = NULL,
  103. [STARPU_BCSR_INTERFACE_ID] = NULL
  104. };
  105. int starpu_mpi_handle_to_datatype(starpu_data_handle data_handle, MPI_Datatype *datatype)
  106. {
  107. unsigned id = starpu_get_handle_interface_id(data_handle);
  108. handle_to_datatype_func func = handle_to_datatype_funcs[id];
  109. STARPU_ASSERT(func);
  110. return func(data_handle, datatype);
  111. }
  112. void *starpu_mpi_handle_to_ptr(starpu_data_handle data_handle)
  113. {
  114. unsigned id = starpu_get_handle_interface_id(data_handle);
  115. handle_to_ptr_func func = handle_to_ptr_funcs[id];
  116. STARPU_ASSERT(func);
  117. return func(data_handle);
  118. }