xref: /petsc/src/sys/mpiuni/mpi.c (revision 1f6cc5b2a846dea79a785bf5d9c3705a05a876f0)
1 /*
2       This provides a few of the MPI-uni functions that cannot be implemented
3     with C macros
4 */
5 #include "include/mpiuni/mpi.h"
6 
7 #if defined(PETSC_HAVE_STDLIB_H)
8 #include <stdlib.h>
9 #endif
10 
11 #define MPI_SUCCESS 0
12 #define MPI_FAILURE 1
13 void    *MPIUNI_TMP        = 0;
14 int     MPIUNI_DATASIZE[5] = { sizeof(int),sizeof(float),sizeof(double),2*sizeof(double),sizeof(char)};
15 /*
16        With MPI Uni there is only one communicator, which is called 1.
17 */
18 #define MAX_ATTR 128
19 
20 typedef struct {
21   void                *extra_state;
22   void                *attribute_val;
23   int                 active;
24   MPI_Delete_function *del;
25 } MPI_Attr;
26 
27 static MPI_Attr attr[MAX_ATTR];
28 static int      num_attr = 1,mpi_tag_ub = 100000000;
29 
30 #if defined(__cplusplus)
31 extern "C" {
32 #endif
33 
34 /*
35    To avoid problems with prototypes to the system memcpy() it is duplicated here
36 */
37 int MPIUNI_Memcpy(void *a,const void* b,int n) {
38   int  i;
39   char *aa= (char*)a;
40   char *bb= (char*)b;
41 
42   for (i=0; i<n; i++) aa[i] = bb[i];
43   return 0;
44 }
45 
46 /*
47    Used to set the built-in MPI_TAG_UB attribute
48 */
49 static int Keyval_setup(void)
50 {
51   attr[0].active        = 1;
52   attr[0].attribute_val = &mpi_tag_ub;
53   return 0;
54 }
55 
56 /*
57          These functions are mapped to the Petsc_ name by ./mpi.h
58 */
59 int Petsc_MPI_Keyval_create(MPI_Copy_function *copy_fn,MPI_Delete_function *delete_fn,int *keyval,void *extra_state)
60 {
61   if (num_attr >= MAX_ATTR) MPI_Abort(MPI_COMM_WORLD,1);
62 
63   attr[num_attr].extra_state = extra_state;
64   attr[num_attr].del         = delete_fn;
65   *keyval                    = num_attr++;
66   return 0;
67 }
68 
69 int Petsc_MPI_Keyval_free(int *keyval)
70 {
71   attr[*keyval].active = 0;
72   return MPI_SUCCESS;
73 }
74 
75 int Petsc_MPI_Attr_put(MPI_Comm comm,int keyval,void *attribute_val)
76 {
77   attr[keyval].active        = 1;
78   attr[keyval].attribute_val = attribute_val;
79   return MPI_SUCCESS;
80 }
81 
82 int Petsc_MPI_Attr_delete(MPI_Comm comm,int keyval)
83 {
84   if (attr[keyval].active && attr[keyval].del) {
85     (*(attr[keyval].del))(comm,keyval,attr[keyval].attribute_val,attr[keyval].extra_state);
86   }
87   attr[keyval].active        = 0;
88   attr[keyval].attribute_val = 0;
89   return MPI_SUCCESS;
90 }
91 
92 int Petsc_MPI_Attr_get(MPI_Comm comm,int keyval,void *attribute_val,int *flag)
93 {
94   if (!keyval) Keyval_setup();
95   *flag                   = attr[keyval].active;
96   *(void **)attribute_val = attr[keyval].attribute_val;
97   return MPI_SUCCESS;
98 }
99 
100 static int dups = 0;
101 int Petsc_MPI_Comm_dup(MPI_Comm comm,MPI_Comm *out)
102 {
103   *out = comm;
104   dups++;
105   return 0;
106 }
107 
108 int Petsc_MPI_Comm_free(MPI_Comm *comm)
109 {
110   int i;
111 
112   if (--dups) return MPI_SUCCESS;
113   for (i=0; i<num_attr; i++) {
114     if (attr[i].active && attr[i].del) {
115       (*attr[i].del)(*comm,i,attr[i].attribute_val,attr[i].extra_state);
116     }
117     attr[i].active = 0;
118   }
119   return MPI_SUCCESS;
120 }
121 
122 /* --------------------------------------------------------------------------*/
123 
124 int Petsc_MPI_Abort(MPI_Comm comm,int errorcode)
125 {
126   abort();
127   return MPI_SUCCESS;
128 }
129 
130 static int MPI_was_initialized = 0;
131 
132 int Petsc_MPI_Initialized(int *flag)
133 {
134   *flag = MPI_was_initialized;
135   return 0;
136 }
137 
138 int Petsc_MPI_Finalize(void)
139 {
140   MPI_was_initialized = 0;
141   return 0;
142 }
143 
144 /* -------------------     Fortran versions of several routines ------------------ */
145 
146 #if defined(PETSC_HAVE_FORTRAN_CAPS)
147 #define mpi_init_             MPI_INIT
148 #define mpi_finalize_         MPI_FINALIZE
149 #define mpi_comm_size_        MPI_COMM_SIZE
150 #define mpi_comm_rank_        MPI_COMM_RANK
151 #define mpi_abort_            MPI_ABORT
152 #define mpi_allreduce_        MPI_ALLREDUCE
153 #define mpi_barrier_          MPI_BARRIER
154 #define mpi_bcast_            MPI_BCAST
155 #define mpi_gather_           MPI_GATHER
156 #define mpi_allgather_        MPI_ALLGATHER
157 #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
158 #define mpi_init_             mpi_init
159 #define mpi_finalize_         mpi_finalize
160 #define mpi_comm_size_        mpi_comm_size
161 #define mpi_comm_rank_        mpi_comm_rank
162 #define mpi_abort_            mpi_abort
163 #define mpi_allreduce_        mpi_allreduce
164 #define mpi_barrier_          mpi_barrier
165 #define mpi_bcast_            mpi_bcast
166 #define mpi_gather_           mpi_gather
167 #define mpi_allgather_        mpi_allgather
168 #endif
169 
170 #if defined(PETSC_HAVE_FORTRAN_UNDERSCORE_UNDERSCORE)
171 #define mpi_init_             mpi_init__
172 #define mpi_finalize_         mpi_finalize__
173 #define mpi_comm_size_        mpi_comm_size__
174 #define mpi_comm_rank_        mpi_comm_rank__
175 #define mpi_abort_            mpi_abort__
176 #define mpi_allreduce_        mpi_allreduce__
177 #define mpi_barrier_          mpi_barrier__
178 #define mpi_bcast_            mpi_bcast__
179 #define mpi_gather_           mpi_gather__
180 #define mpi_allgather_        mpi_allgather__
181 #endif
182 
183 void PETSC_STDCALL  mpi_init_(int *ierr)
184 {
185   MPI_was_initialized = 1;
186   *ierr = MPI_SUCCESS;
187 }
188 
189 void PETSC_STDCALL  mpi_finalize_(int *ierr)
190 {
191   *ierr = MPI_SUCCESS;
192 }
193 
194 void PETSC_STDCALL mpi_comm_size_(MPI_Comm *comm,int *size,int *ierr)
195 {
196   *size = 1;
197   *ierr = 0;
198 }
199 
200 void PETSC_STDCALL mpi_comm_rank_(MPI_Comm *comm,int *rank,int *ierr)
201 {
202   *rank=0;
203   *ierr=MPI_SUCCESS;
204 }
205 
206 void PETSC_STDCALL mpi_comm_split(MPI_Comm *comm,int *color,int *key, MPI_Comm *newcomm, int *ierr)
207 {
208   *newcomm = *comm;
209   *ierr=MPI_SUCCESS;
210 }
211 
212 void PETSC_STDCALL mpi_abort_(MPI_Comm *comm,int *errorcode,int *ierr)
213 {
214   abort();
215   *ierr = MPI_SUCCESS;
216 }
217 
218 void PETSC_STDCALL mpi_allreduce_(void *sendbuf,void *recvbuf,int *count,int *datatype,int *op,int *comm,int *ierr)
219 {
220   MPIUNI_Memcpy(recvbuf,sendbuf,(*count)*MPIUNI_DATASIZE[*datatype]);
221   *ierr = MPI_SUCCESS;
222 }
223 
224 void PETSC_STDCALL mpi_barrier_(MPI_Comm *comm,int *ierr)
225 {
226   *ierr = MPI_SUCCESS;
227 }
228 
229 void PETSC_STDCALL mpi_bcast_(void *buf,int *count,int *datatype,int *root,int *comm,int *ierr)
230 {
231   *ierr = MPI_SUCCESS;
232 }
233 
234 
235 void PETSC_STDCALL mpi_gather_(void *sendbuf,int *scount,int *sdatatype, void* recvbuf, int* rcount, int* rdatatype, int *root,int *comm,int *ierr)
236 {
237   MPIUNI_Memcpy(recvbuf,sendbuf,(*scount)*MPIUNI_DATASIZE[*sdatatype]);
238   *ierr = MPI_SUCCESS;
239 }
240 
241 
242 void PETSC_STDCALL mpi_allgather_(void *sendbuf,int *scount,int *sdatatype, void* recvbuf, int* rcount, int* rdatatype,int *comm,int *ierr)
243 {
244   MPIUNI_Memcpy(recvbuf,sendbuf,(*scount)*MPIUNI_DATASIZE[*sdatatype]);
245   *ierr = MPI_SUCCESS;
246 }
247 
248 #if defined(__cplusplus)
249 }
250 #endif
251 
252 
253