starpu_mpi_datatype.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (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. * Blas
  25. */
  26. static int handle_to_datatype_blas(starpu_data_handle data_handle, MPI_Datatype *datatype)
  27. {
  28. unsigned nx = starpu_get_blas_nx(data_handle);
  29. unsigned ny = starpu_get_blas_ny(data_handle);
  30. unsigned ld = starpu_get_blas_local_ld(data_handle);
  31. size_t elemsize = starpu_get_blas_elemsize(data_handle);
  32. MPI_Type_vector(ny, nx*elemsize, ld*elemsize, MPI_BYTE, datatype);
  33. MPI_Type_commit(datatype);
  34. return 0;
  35. }
  36. static void *handle_to_ptr_blas(starpu_data_handle data_handle)
  37. {
  38. return (void *)starpu_get_blas_local_ptr(data_handle);
  39. }
  40. /*
  41. * Block
  42. */
  43. static int handle_to_datatype_block(starpu_data_handle data_handle, MPI_Datatype *datatype)
  44. {
  45. unsigned nx = starpu_get_block_nx(data_handle);
  46. unsigned ny = starpu_get_block_ny(data_handle);
  47. unsigned nz = starpu_get_block_nz(data_handle);
  48. unsigned ldy = starpu_get_block_local_ldy(data_handle);
  49. unsigned ldz = starpu_get_block_local_ldz(data_handle);
  50. size_t elemsize = starpu_get_block_elemsize(data_handle);
  51. MPI_Datatype datatype_2dlayer;
  52. MPI_Type_vector(ny, nx*elemsize, ldy*elemsize, MPI_BYTE, &datatype_2dlayer);
  53. MPI_Type_commit(&datatype_2dlayer);
  54. MPI_Type_hvector(nz, 1, ldz*elemsize, datatype_2dlayer, datatype);
  55. MPI_Type_commit(datatype);
  56. return 0;
  57. }
  58. static void *handle_to_ptr_block(starpu_data_handle data_handle)
  59. {
  60. return (void *)starpu_get_block_local_ptr(data_handle);
  61. }
  62. /*
  63. * Vector
  64. */
  65. static int handle_to_datatype_vector(starpu_data_handle data_handle, MPI_Datatype *datatype)
  66. {
  67. unsigned nx = starpu_get_vector_nx(data_handle);
  68. size_t elemsize = starpu_get_vector_elemsize(data_handle);
  69. MPI_Type_contiguous(nx*elemsize, MPI_BYTE, datatype);
  70. MPI_Type_commit(datatype);
  71. return 0;
  72. }
  73. static void *handle_to_ptr_vector(starpu_data_handle data_handle)
  74. {
  75. return (void *)starpu_get_vector_local_ptr(data_handle);
  76. }
  77. /*
  78. * Generic
  79. */
  80. static handle_to_datatype_func handle_to_datatype_funcs[STARPU_NINTERFACES_ID] = {
  81. [STARPU_BLAS_INTERFACE_ID] = handle_to_datatype_blas,
  82. [STARPU_BLOCK_INTERFACE_ID] = handle_to_datatype_block,
  83. [STARPU_VECTOR_INTERFACE_ID] = handle_to_datatype_vector,
  84. [STARPU_CSR_INTERFACE_ID] = NULL,
  85. [STARPU_CSC_INTERFACE_ID] = NULL,
  86. [STARPU_BCSCR_INTERFACE_ID] = NULL
  87. };
  88. static handle_to_ptr_func handle_to_ptr_funcs[STARPU_NINTERFACES_ID] = {
  89. [STARPU_BLAS_INTERFACE_ID] = handle_to_ptr_blas,
  90. [STARPU_BLOCK_INTERFACE_ID] = handle_to_ptr_block,
  91. [STARPU_VECTOR_INTERFACE_ID] = handle_to_ptr_vector,
  92. [STARPU_CSR_INTERFACE_ID] = NULL,
  93. [STARPU_CSC_INTERFACE_ID] = NULL,
  94. [STARPU_BCSCR_INTERFACE_ID] = NULL
  95. };
  96. int starpu_mpi_handle_to_datatype(starpu_data_handle data_handle, MPI_Datatype *datatype)
  97. {
  98. unsigned id = starpu_get_handle_interface_id(data_handle);
  99. handle_to_datatype_func func = handle_to_datatype_funcs[id];
  100. STARPU_ASSERT(func);
  101. return func(data_handle, datatype);
  102. }
  103. void *starpu_mpi_handle_to_ptr(starpu_data_handle data_handle)
  104. {
  105. unsigned id = starpu_get_handle_interface_id(data_handle);
  106. handle_to_ptr_func func = handle_to_ptr_funcs[id];
  107. STARPU_ASSERT(func);
  108. return func(data_handle);
  109. }