mpi.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #include "mpiP.h"
  2. /****************************************************************************/
  3. static int initialized=0;
  4. /****************************************************************************/
  5. /*
  6. * INIT/FINALIZE
  7. *
  8. */
  9. FC_FUNC( mpi_init_fort , MPI_INIT_FORT)
  10. (int *f_MPI_COMM_WORLD,
  11. int *f_MPI_ANY_SOURCE, int *f_MPI_ANY_TAG,
  12. int *f_MPI_PROC_NULL, int *f_MPI_ROOT,
  13. int *f_MPI_COMM_NULL, int *f_MPI_REQUEST_NULL,
  14. int *f_MPI_GROUP_NULL, int *f_MPI_GROUP_EMPTY,
  15. int *f_MPI_UNDEFINED,
  16. int *f_MPI_MAX_ERROR_STRING,
  17. int *f_MPI_MAX_PROCESSOR_NAME,
  18. int *f_MPI_STATUS_SIZE,
  19. int *f_MPI_SOURCE, int *f_MPI_TAG, int *f_MPI_ERROR,
  20. int *f_status,
  21. int *fsource, int *ftag, int *ferror,
  22. int *f_MPI_INTEGER, void *fint1, void *fint2,
  23. int *f_MPI_LOGICAL, void *flog1, void *flog2,
  24. int *f_MPI_REAL, void *freal1, void *freal2,
  25. int *f_MPI_DOUBLE_PRECISION,
  26. void *fdub1, void *fdub2,
  27. int *f_MPI_COMPLEX, void *fcomp1, void *fcomp2,
  28. int *ierror)
  29. {
  30. int err;
  31. int size;
  32. int offset;
  33. *ierror=MPI_Init(NULL,NULL);
  34. err=0;
  35. /*
  36. * These 3 macros compare things from mpif.h (as passed in by the f_
  37. * arguments) to the values in C (from #including mpi.h).
  38. *
  39. * Unfortunately, this kind of thing is done most easily in a nasty
  40. * looking macto.
  41. *
  42. */
  43. /*
  44. * verify_eq
  45. * compare value of constants in C and fortran
  46. * i.e. compare *f_<name> to <name>
  47. */
  48. #define verify_eq(name) \
  49. if (*f_##name != name) \
  50. { fprintf(stderr,"mpi-serial: mpi_init_fort: %s not consistant " \
  51. "between mpif.h (%d) and mpi.h (%d)\n",\
  52. #name,*f_##name,name); \
  53. err=1; }
  54. #define verify_eq_warn(name) \
  55. if (*f_##name != name) \
  56. { fprintf(stderr,"mpi-serial: mpi_init_fort: warning: %s not consistant " \
  57. "between mpif.h (%d) and mpi.h (%d)\n",\
  58. #name,*f_##name,name); \
  59. }
  60. /*
  61. * verify_size
  62. * verify that the type name in fortran has the correct
  63. * value (i.e. the size of that data type).
  64. * Determine size by subtracting the pointer values of two
  65. * consecutive array locations.
  66. */
  67. #define verify_size(name,p1,p2) \
  68. if ( (size=((char *)(p2) - (char *)(p1))) != *f_##name ) \
  69. { fprintf(stderr,"mpi-serial: mpi_init_fort: mpif.h %s (%d) " \
  70. "does not match actual fortran size (%d)\n", \
  71. #name,*f_##name,size); \
  72. err=1; }
  73. /*
  74. * verify_field
  75. * check the struct member offsets for MPI_Status vs. the
  76. * fortan integer array offsets. E.g. the location of
  77. * status->MPI_SOURCE should be the same as STATUS(MPI_SOURCE)
  78. */
  79. #define verify_field(name) \
  80. { offset= (char *)&((MPI_Status *)f_status)->name - (char *)f_status; \
  81. if ( offset != (*f_##name-1)*sizeof(int) ) \
  82. { fprintf(stderr,"mpi-serial: mpi_init_fort: mpif.h %s (%d) (%d bytes) " \
  83. "is inconsistant w/offset in MPI_Status (%d bytes)\n", \
  84. #name,*f_##name,(*f_##name-1)*sizeof(int),offset); \
  85. err=1; }}
  86. verify_eq(MPI_COMM_WORLD);
  87. verify_eq(MPI_ANY_SOURCE);
  88. verify_eq(MPI_ANY_TAG);
  89. verify_eq(MPI_PROC_NULL);
  90. verify_eq(MPI_ROOT);
  91. verify_eq(MPI_COMM_NULL);
  92. verify_eq(MPI_REQUEST_NULL);
  93. verify_eq(MPI_GROUP_NULL);
  94. verify_eq(MPI_GROUP_EMPTY);
  95. verify_eq(MPI_UNDEFINED);
  96. verify_eq(MPI_MAX_ERROR_STRING);
  97. verify_eq(MPI_MAX_PROCESSOR_NAME);
  98. verify_eq(MPI_STATUS_SIZE);
  99. verify_field(MPI_SOURCE);
  100. verify_field(MPI_TAG);
  101. verify_field(MPI_ERROR);
  102. verify_eq(MPI_INTEGER);
  103. verify_size(MPI_INTEGER,fint1,fint2);
  104. verify_size(MPI_LOGICAL,flog1,flog2);
  105. verify_eq_warn(MPI_REAL);
  106. verify_size(MPI_REAL,freal1,freal2);
  107. verify_eq(MPI_DOUBLE_PRECISION);
  108. verify_size(MPI_DOUBLE_PRECISION,fdub1,fdub2);
  109. verify_size(MPI_COMPLEX,fcomp1,fcomp2);
  110. if (err)
  111. abort();
  112. }
  113. int MPI_Init(int *argc, char **argv[])
  114. {
  115. MPI_Comm my_comm_world;
  116. if (sizeof(MPI_Aint) < sizeof(void *))
  117. {
  118. fprintf(stderr, "mpi-serial: MPI_Init: "
  119. "MPI_Aint is not large enough for void *\n");
  120. abort();
  121. }
  122. my_comm_world=mpi_comm_new();
  123. if (my_comm_world != MPI_COMM_WORLD)
  124. {
  125. fprintf(stderr,"MPI_Init: conflicting MPI_COMM_WORLD\n");
  126. abort();
  127. }
  128. initialized=1;
  129. return(MPI_SUCCESS);
  130. }
  131. /*********/
  132. FC_FUNC( mpi_finalize, MPI_FINALIZE )(int *ierror)
  133. {
  134. *ierror=MPI_Finalize();
  135. }
  136. /*
  137. * MPI_Finalize()
  138. *
  139. * this library doesn't support re-initializing MPI, so
  140. * the finalize will just leave everythign as it is...
  141. *
  142. */
  143. int MPI_Finalize(void)
  144. {
  145. initialized=0;
  146. mpi_destroy_handles();
  147. return(MPI_SUCCESS);
  148. }
  149. /*********/
  150. FC_FUNC( mpi_abort , MPI_ABORT )(int *comm, int *errorcode, int *ierror)
  151. {
  152. *ierror=MPI_Abort( *comm, *errorcode);
  153. }
  154. int MPI_Abort(MPI_Comm comm, int errorcode)
  155. {
  156. fprintf(stderr,"MPI_Abort: error code = %d\n",errorcode);
  157. exit(errorcode);
  158. }
  159. /*********/
  160. FC_FUNC( mpi_error_string , MPI_ERROR_STRING)
  161. (int *errorcode, char *string,
  162. int *resultlen, int *ierror)
  163. {
  164. *ierror=MPI_Error_string(*errorcode, string, resultlen);
  165. }
  166. int MPI_Error_string(int errorcode, char *string, int *resultlen)
  167. {
  168. sprintf(string,"MPI Error: code %d\n",errorcode);
  169. *resultlen=strlen(string);
  170. return(MPI_SUCCESS);
  171. }
  172. /*********/
  173. FC_FUNC( mpi_get_processor_name , MPI_GET_PROCESSOR_NAME )
  174. (char *name, int *resultlen, int *ierror)
  175. {
  176. *ierror=MPI_Get_processor_name(name,resultlen);
  177. }
  178. int MPI_Get_processor_name(char *name, int *resultlen)
  179. {
  180. int ret;
  181. ret=gethostname(name,MPI_MAX_PROCESSOR_NAME);
  182. if (ret!=0)
  183. strncpy(name,"unknown host name",MPI_MAX_PROCESSOR_NAME);
  184. name[MPI_MAX_PROCESSOR_NAME-1]='\0'; /* make sure NULL terminated */
  185. *resultlen=strlen(name);
  186. return(MPI_SUCCESS);
  187. }
  188. /*********/
  189. FC_FUNC( mpi_initialized , MPI_INITIALIZED )(int *flag, int *ierror)
  190. {
  191. *ierror=MPI_Initialized(flag);
  192. }
  193. int MPI_Initialized(int *flag)
  194. {
  195. *flag= initialized;
  196. return(MPI_SUCCESS);
  197. }