source: CONFIG_DEVT/IPSLCM6.5_work_ENSEMBLES/oasis3-mct/lib/mct/mpi-serial/pack.c

Last change on this file was 5725, checked in by aclsce, 3 years ago

Added new oasis3-MCT version to be used to handle ensembles simulations with XIOS.

File size: 3.4 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include "mpiP.h"
5#include "type.h"
6
7/*
8 *
9 */
10
11
12FC_FUNC( mpi_pack , MPI_PACK )
13     ( void *inbuf, int *incount, int *datatype,
14       void *outbuf, int *outsize, int *position, int *comm, int *ierror)
15{
16  *ierror=MPI_Pack(inbuf, *incount,* datatype,
17                   outbuf, *outsize, position, *comm);
18}
19
20
21
22int MPI_Pack( void *inbuf, int incount, MPI_Datatype datatype,
23              void *outbuf, int outsize, int *position, MPI_Comm comm)
24{
25  int ret;
26
27  Datatype type_ptr = *(Datatype*) mpi_handle_to_datatype(datatype);
28  Comm* comm_ptr = mpi_handle_to_ptr(comm);
29
30  ret = Pack(inbuf, incount, type_ptr, outbuf, outsize, position, comm_ptr);
31
32  return ret;
33}
34
35
36
37int Pack(void *inbuf, int incount, Datatype type,
38              void *outbuf, int outsize, int *position, Comm * comm)
39{
40  int i, j;
41  MPI_Aint extent;
42  //check that buffer is large enough
43  Type_extent(type, &extent);
44  for (i = 0; i < incount; i++)
45  {
46    for (j = 0; j < type->count; j++)
47    {
48      if ((*position) + Simpletype_length(type->pairs[j].type) > outsize)
49      {
50        printf("MPI_Pack: data exceeds buffer size\n");
51        exit(1);
52      }
53      memcpy(((char*) outbuf)+(*position), inbuf+type->pairs[j].disp + (extent*i),
54             Simpletype_length(type->pairs[j].type));
55      *position += Simpletype_length(type->pairs[j].type);
56    }
57  }
58}
59
60FC_FUNC( mpi_pack_size, MPI_PACK_SIZE )(int * incount, int * datatype,
61                                          int * comm, long * size, int *ierr)
62{
63  *ierr = MPI_Pack_size(*incount, *datatype, *comm, size);
64}
65
66int MPI_Pack_size(int incount, MPI_Datatype datatype,
67                  MPI_Comm comm, MPI_Aint * size)
68{
69  int ret;
70  Datatype type_ptr = *(Datatype*) mpi_handle_to_datatype(datatype);
71  Comm * comm_ptr = mpi_handle_to_ptr(comm);
72
73  ret = Pack_size(incount, type_ptr, comm_ptr, size);
74
75  return ret;
76}
77
78
79int Pack_size(int incount, Datatype datatype,
80                   Comm * comm, MPI_Aint * size)
81{
82  int i;
83  *size = 0;
84  //sum up all sizes
85  for(i = 0; i < datatype->count; i++)
86  {
87    *size += Simpletype_length(datatype->pairs[i].type);
88  }
89  *size *= incount;
90  printf("Size = %d\n", *size);
91}
92
93
94/*
95 *
96 */
97
98
99FC_FUNC( mpi_unpack , MPI_UNPACK )
100     ( void *inbuf, int *insize, int *position,
101       void *outbuf, int *outcount, int *datatype,
102       int *comm, int *ierror )
103{
104  *ierror=MPI_Unpack( inbuf, *insize, position,
105                      outbuf, *outcount, *datatype, *comm);
106}
107
108
109int MPI_Unpack(void * inbuf, int insize, int * position, void * outbuf,
110               int outcount, MPI_Datatype type, MPI_Comm comm)
111{
112  int ret;
113  Datatype type_ptr = *(Datatype*) mpi_handle_to_datatype(type);
114  Comm * comm_ptr = mpi_handle_to_ptr(comm);
115
116  ret = Unpack(inbuf, insize, position, outbuf, outcount, type_ptr, comm_ptr);
117
118  return ret;
119}
120
121int Unpack(void * inbuf, int insize, int * position, void *outbuf,
122                int outcount, Datatype type, Comm* comm)
123{
124  int i, j;
125  MPI_Aint extent;
126
127  Type_extent(type, &extent);
128
129  for (i = 0; i < outcount; i++)
130  {
131    for (j = 0; j < type->count; j++)
132    {
133      if ((*position) + Simpletype_length(type->pairs[j].type) > insize)
134      {
135        printf("MPI_Unpack: Data exceeds buffer size\n");
136        exit(1);
137      }
138      memcpy(outbuf+type->pairs[j].disp + (extent*i), ((char*) inbuf)+(*position) ,
139             Simpletype_length(type->pairs[j].type));
140      *position += Simpletype_length(type->pairs[j].type);
141    }
142  }
143}
144
145
Note: See TracBrowser for help on using the repository browser.