starpu_mpi_datatype.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. * Vector
  42. */
  43. static int handle_to_datatype_vector(starpu_data_handle data_handle, MPI_Datatype *datatype)
  44. {
  45. unsigned nx = starpu_get_vector_nx(data_handle);
  46. size_t elemsize = starpu_get_vector_elemsize(data_handle);
  47. MPI_Type_contiguous(nx*elemsize, MPI_BYTE, datatype);
  48. MPI_Type_commit(datatype);
  49. return 0;
  50. }
  51. static void *handle_to_ptr_vector(starpu_data_handle data_handle)
  52. {
  53. return (void *)starpu_get_vector_local_ptr(data_handle);
  54. }
  55. /*
  56. * Generic
  57. */
  58. static handle_to_datatype_func handle_to_datatype_funcs[STARPU_NINTERFACES_ID] = {
  59. [STARPU_BLAS_INTERFACE_ID] = handle_to_datatype_blas,
  60. [STARPU_BLOCK_INTERFACE_ID] = NULL,
  61. [STARPU_VECTOR_INTERFACE_ID] = handle_to_datatype_vector,
  62. [STARPU_CSR_INTERFACE_ID] = NULL,
  63. [STARPU_CSC_INTERFACE_ID] = NULL,
  64. [STARPU_BCSCR_INTERFACE_ID] = NULL
  65. };
  66. static handle_to_ptr_func handle_to_ptr_funcs[STARPU_NINTERFACES_ID] = {
  67. [STARPU_BLAS_INTERFACE_ID] = handle_to_ptr_blas,
  68. [STARPU_BLOCK_INTERFACE_ID] = NULL,
  69. [STARPU_VECTOR_INTERFACE_ID] = handle_to_ptr_vector,
  70. [STARPU_CSR_INTERFACE_ID] = NULL,
  71. [STARPU_CSC_INTERFACE_ID] = NULL,
  72. [STARPU_BCSCR_INTERFACE_ID] = NULL
  73. };
  74. int starpu_mpi_handle_to_datatype(starpu_data_handle data_handle, MPI_Datatype *datatype)
  75. {
  76. unsigned id = starpu_get_handle_interface_id(data_handle);
  77. handle_to_datatype_func func = handle_to_datatype_funcs[id];
  78. STARPU_ASSERT(func);
  79. return func(data_handle, datatype);
  80. }
  81. void *starpu_mpi_handle_to_ptr(starpu_data_handle data_handle)
  82. {
  83. unsigned id = starpu_get_handle_interface_id(data_handle);
  84. handle_to_ptr_func func = handle_to_ptr_funcs[id];
  85. STARPU_ASSERT(func);
  86. return func(data_handle);
  87. }