dparallel.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /** \file
  2. This file has the parallel I/O functions.
  3. Copyright 2010 University Corporation for Atmospheric
  4. Research/Unidata. See COPYRIGHT file for more info.
  5. */
  6. #include <config.h>
  7. #include <netcdf_f.h>
  8. #include "ncdispatch.h"
  9. /* This function creates a file for use with parallel I/O. */
  10. int
  11. nc_create_par(const char *path, int cmode, MPI_Comm comm,
  12. MPI_Info info, int *ncidp)
  13. {
  14. #ifndef USE_PARALLEL
  15. return NC_ENOPAR;
  16. #else
  17. NC_MPI_INFO data;
  18. MPI_Comm comm_c = 0;
  19. MPI_Info info_c = 0;
  20. /* One of these two parallel IO modes must be chosen by the user,
  21. * or else pnetcdf must be in use. */
  22. if (!(cmode & NC_MPIIO || cmode & NC_MPIPOSIX) &&
  23. !(cmode & NC_PNETCDF))
  24. return NC_EINVAL;
  25. comm_c = (MPI_Comm)comm;
  26. info_c = (MPI_Info)info;
  27. data.comm = comm_c;
  28. data.info = info_c;
  29. return NC_create(path, cmode, 0, 0, NULL, 1, &data, ncidp);
  30. #endif /* USE_PARALLEL */
  31. }
  32. /* This function opens a file for parallel I/O. */
  33. int
  34. nc_open_par(const char *path, int mode, MPI_Comm comm,
  35. MPI_Info info, int *ncidp)
  36. {
  37. #ifndef USE_PARALLEL
  38. return NC_ENOPAR;
  39. #else
  40. NC_MPI_INFO mpi_data;
  41. /* One of these two parallel IO modes must be chosen by the user,
  42. * or else pnetcdf must be in use. */
  43. if (!(mode & NC_MPIIO || mode & NC_MPIPOSIX) &&
  44. !(mode & NC_PNETCDF))
  45. return NC_EINVAL;
  46. mpi_data.comm = comm;
  47. mpi_data.info = info;
  48. return NC_open(path, mode, 0, NULL, 1, &mpi_data, ncidp);
  49. #endif /* USE_PARALLEL */
  50. }
  51. /* Fortran needs to pass MPI comm/info as integers. */
  52. int
  53. nc_open_par_fortran(const char *path, int mode, int comm,
  54. int info, int *ncidp)
  55. {
  56. #ifndef USE_PARALLEL
  57. return NC_ENOPAR;
  58. #else
  59. MPI_Comm comm_c = 0;
  60. MPI_Info info_c = 0;
  61. /* Convert fortran comm and info to C comm and info, if there is a
  62. * function to do so. Otherwise just pass them. */
  63. #ifdef HAVE_MPI_COMM_F2C
  64. comm_c = MPI_Comm_f2c(comm);
  65. info_c = MPI_Info_f2c(info);
  66. #else
  67. comm_c = (MPI_Comm)comm;
  68. info_c = (MPI_Info)info;
  69. #endif
  70. return nc_open_par(path, mode, comm_c, info_c, ncidp);
  71. #endif
  72. }
  73. /* This function will change the parallel access of a variable from
  74. * independent to collective. */
  75. int
  76. nc_var_par_access(int ncid, int varid, int par_access)
  77. {
  78. NC* ncp;
  79. int stat = NC_NOERR;
  80. if ((stat = NC_check_id(ncid, &ncp)))
  81. return stat;
  82. #ifndef USE_PARALLEL
  83. return NC_ENOPAR;
  84. #else
  85. return ncp->dispatch->var_par_access(ncid,varid,par_access);
  86. #endif
  87. }
  88. /* when calling from fortran: convert MPI_Comm and MPI_Info to C */
  89. int
  90. nc_create_par_fortran(const char *path, int cmode, int comm,
  91. int info, int *ncidp)
  92. {
  93. #ifndef USE_PARALLEL
  94. return NC_ENOPAR;
  95. #else
  96. MPI_Comm comm_c = 0;
  97. MPI_Info info_c = 0;
  98. #ifdef USE_PARALLEL
  99. #ifdef HAVE_MPI_COMM_F2C
  100. comm_c = MPI_Comm_f2c(comm);
  101. info_c = MPI_Info_f2c(info);
  102. #else
  103. comm_c = (MPI_Comm)comm;
  104. info_c = (MPI_Info)info;
  105. #endif
  106. #endif
  107. return nc_create_par(path, cmode, comm_c, info_c, ncidp);
  108. #endif
  109. }