starpu_mpi_datatype.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2011 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <starpu_mpi_datatype.h>
  18. /*
  19. * MPI_* functions usually requires both a pointer to the first element of
  20. * a datatype and the datatype itself, so we need to provide both.
  21. */
  22. typedef int (*handle_to_datatype_func)(starpu_data_handle, MPI_Datatype *);
  23. typedef void *(*handle_to_ptr_func)(starpu_data_handle);
  24. /*
  25. * Matrix
  26. */
  27. static int handle_to_datatype_matrix(starpu_data_handle data_handle, MPI_Datatype *datatype)
  28. {
  29. int ret;
  30. unsigned nx = starpu_matrix_get_nx(data_handle);
  31. unsigned ny = starpu_matrix_get_ny(data_handle);
  32. unsigned ld = starpu_matrix_get_local_ld(data_handle);
  33. size_t elemsize = starpu_matrix_get_elemsize(data_handle);
  34. ret = MPI_Type_vector(ny, nx*elemsize, ld*elemsize, MPI_BYTE, datatype);
  35. STARPU_ASSERT(ret == MPI_SUCCESS);
  36. ret = MPI_Type_commit(datatype);
  37. STARPU_ASSERT(ret == MPI_SUCCESS);
  38. return 0;
  39. }
  40. /*
  41. * Block
  42. */
  43. static int handle_to_datatype_block(starpu_data_handle data_handle, MPI_Datatype *datatype)
  44. {
  45. int ret;
  46. unsigned nx = starpu_block_get_nx(data_handle);
  47. unsigned ny = starpu_block_get_ny(data_handle);
  48. unsigned nz = starpu_block_get_nz(data_handle);
  49. unsigned ldy = starpu_block_get_local_ldy(data_handle);
  50. unsigned ldz = starpu_block_get_local_ldz(data_handle);
  51. size_t elemsize = starpu_block_get_elemsize(data_handle);
  52. MPI_Datatype datatype_2dlayer;
  53. ret = MPI_Type_vector(ny, nx*elemsize, ldy*elemsize, MPI_BYTE, &datatype_2dlayer);
  54. STARPU_ASSERT(ret == MPI_SUCCESS);
  55. ret = MPI_Type_commit(&datatype_2dlayer);
  56. STARPU_ASSERT(ret == MPI_SUCCESS);
  57. ret = MPI_Type_hvector(nz, 1, ldz*elemsize, datatype_2dlayer, datatype);
  58. STARPU_ASSERT(ret == MPI_SUCCESS);
  59. ret = MPI_Type_commit(datatype);
  60. STARPU_ASSERT(ret == MPI_SUCCESS);
  61. return 0;
  62. }
  63. /*
  64. * Vector
  65. */
  66. static int handle_to_datatype_vector(starpu_data_handle data_handle, MPI_Datatype *datatype)
  67. {
  68. int ret;
  69. unsigned nx = starpu_vector_get_nx(data_handle);
  70. size_t elemsize = starpu_vector_get_elemsize(data_handle);
  71. ret = MPI_Type_contiguous(nx*elemsize, MPI_BYTE, datatype);
  72. STARPU_ASSERT(ret == MPI_SUCCESS);
  73. ret = MPI_Type_commit(datatype);
  74. STARPU_ASSERT(ret == MPI_SUCCESS);
  75. return 0;
  76. }
  77. /*
  78. * Variable
  79. */
  80. static int handle_to_datatype_variable(starpu_data_handle data_handle, MPI_Datatype *datatype)
  81. {
  82. int ret;
  83. size_t elemsize = starpu_variable_get_elemsize(data_handle);
  84. ret = MPI_Type_contiguous(elemsize, MPI_BYTE, datatype);
  85. STARPU_ASSERT(ret == MPI_SUCCESS);
  86. ret = MPI_Type_commit(datatype);
  87. STARPU_ASSERT(ret == MPI_SUCCESS);
  88. return 0;
  89. }
  90. /*
  91. * Generic
  92. */
  93. static handle_to_datatype_func handle_to_datatype_funcs[STARPU_NINTERFACES_ID] = {
  94. [STARPU_MATRIX_INTERFACE_ID] = handle_to_datatype_matrix,
  95. [STARPU_BLOCK_INTERFACE_ID] = handle_to_datatype_block,
  96. [STARPU_VECTOR_INTERFACE_ID] = handle_to_datatype_vector,
  97. [STARPU_CSR_INTERFACE_ID] = NULL,
  98. [STARPU_BCSR_INTERFACE_ID] = NULL,
  99. [STARPU_VARIABLE_INTERFACE_ID] = handle_to_datatype_variable,
  100. };
  101. int starpu_mpi_handle_to_datatype(starpu_data_handle data_handle, MPI_Datatype *datatype)
  102. {
  103. unsigned id = starpu_get_handle_interface_id(data_handle);
  104. handle_to_datatype_func func = handle_to_datatype_funcs[id];
  105. STARPU_ASSERT(func);
  106. return func(data_handle, datatype);
  107. }
  108. void *starpu_mpi_handle_to_ptr(starpu_data_handle data_handle)
  109. {
  110. return (void*) starpu_handle_get_local_ptr(data_handle);
  111. }