starpu_mpi_datatype.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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_hvector(nz, 1, ldz*elemsize, datatype_2dlayer, datatype);
  54. MPI_Type_commit(datatype);
  55. return 0;
  56. }
  57. static void *handle_to_ptr_block(starpu_data_handle data_handle)
  58. {
  59. return (void *)starpu_get_block_local_ptr(data_handle);
  60. }
  61. /*
  62. * Vector
  63. */
  64. static int handle_to_datatype_vector(starpu_data_handle data_handle, MPI_Datatype *datatype)
  65. {
  66. unsigned nx = starpu_get_vector_nx(data_handle);
  67. size_t elemsize = starpu_get_vector_elemsize(data_handle);
  68. MPI_Type_contiguous(nx*elemsize, MPI_BYTE, datatype);
  69. MPI_Type_commit(datatype);
  70. return 0;
  71. }
  72. static void *handle_to_ptr_vector(starpu_data_handle data_handle)
  73. {
  74. return (void *)starpu_get_vector_local_ptr(data_handle);
  75. }
  76. /*
  77. * Generic
  78. */
  79. static handle_to_datatype_func handle_to_datatype_funcs[STARPU_NINTERFACES_ID] = {
  80. [STARPU_BLAS_INTERFACE_ID] = handle_to_datatype_blas,
  81. [STARPU_BLOCK_INTERFACE_ID] = handle_to_datatype_block,
  82. [STARPU_VECTOR_INTERFACE_ID] = handle_to_datatype_vector,
  83. [STARPU_CSR_INTERFACE_ID] = NULL,
  84. [STARPU_CSC_INTERFACE_ID] = NULL,
  85. [STARPU_BCSCR_INTERFACE_ID] = NULL
  86. };
  87. static handle_to_ptr_func handle_to_ptr_funcs[STARPU_NINTERFACES_ID] = {
  88. [STARPU_BLAS_INTERFACE_ID] = handle_to_ptr_blas,
  89. [STARPU_BLOCK_INTERFACE_ID] = handle_to_ptr_block,
  90. [STARPU_VECTOR_INTERFACE_ID] = handle_to_ptr_vector,
  91. [STARPU_CSR_INTERFACE_ID] = NULL,
  92. [STARPU_CSC_INTERFACE_ID] = NULL,
  93. [STARPU_BCSCR_INTERFACE_ID] = NULL
  94. };
  95. int starpu_mpi_handle_to_datatype(starpu_data_handle data_handle, MPI_Datatype *datatype)
  96. {
  97. unsigned id = starpu_get_handle_interface_id(data_handle);
  98. handle_to_datatype_func func = handle_to_datatype_funcs[id];
  99. STARPU_ASSERT(func);
  100. return func(data_handle, datatype);
  101. }
  102. void *starpu_mpi_handle_to_ptr(starpu_data_handle data_handle)
  103. {
  104. unsigned id = starpu_get_handle_interface_id(data_handle);
  105. handle_to_ptr_func func = handle_to_ptr_funcs[id];
  106. STARPU_ASSERT(func);
  107. return func(data_handle);
  108. }