Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'pikachuyann/simgrid-xbt_random'
[simgrid.git] / src / smpi / mpi / smpi_datatype_derived.cpp
1 /* smpi_datatype.cpp -- MPI primitives to handle datatypes                  */
2 /* Copyright (c) 2009-2020. The SimGrid Team. All rights reserved.          */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "smpi_datatype_derived.hpp"
8 #include "smpi_op.hpp"
9 #include <xbt/log.h>
10
11 #include <cstring>
12
13 namespace simgrid{
14 namespace smpi{
15
16 Type_Contiguous::Type_Contiguous(int size, MPI_Aint lb, MPI_Aint ub, int flags, int block_count, MPI_Datatype old_type)
17     : Datatype(size, lb, ub, flags), block_count_(block_count), old_type_(old_type)
18 {
19   old_type_->ref();
20 }
21
22 Type_Contiguous::~Type_Contiguous()
23 {
24   Datatype::unref(old_type_);
25 }
26
27 void Type_Contiguous::serialize(const void* noncontiguous_buf, void* contiguous_buf, int count)
28 {
29   char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
30   const char* noncontiguous_buf_char = static_cast<const char*>(noncontiguous_buf)+lb();
31   memcpy(contiguous_buf_char, noncontiguous_buf_char, count * block_count_ * old_type_->size());
32 }
33
34 void Type_Contiguous::unserialize(const void* contiguous_buf, void* noncontiguous_buf, int count, MPI_Op op)
35 {
36   const char* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
37   char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+lb();
38   int n= count*block_count_;
39   if(op!=MPI_OP_NULL)
40     op->apply( contiguous_buf_char, noncontiguous_buf_char, &n, old_type_);
41 }
42
43 Type_Hvector::Type_Hvector(int size,MPI_Aint lb, MPI_Aint ub, int flags, int count, int block_length, MPI_Aint stride, MPI_Datatype old_type): Datatype(size, lb, ub, flags), block_count_(count), block_length_(block_length), block_stride_(stride), old_type_(old_type){
44   old_type->ref();
45 }
46 Type_Hvector::~Type_Hvector(){
47   Datatype::unref(old_type_);
48 }
49
50 void Type_Hvector::serialize(const void* noncontiguous_buf, void *contiguous_buf,
51                     int count){
52   char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
53   const char* noncontiguous_buf_char = static_cast<const char*>(noncontiguous_buf);
54
55   for (int i = 0; i < block_count_ * count; i++) {
56     if (not(old_type_->flags() & DT_FLAG_DERIVED))
57       memcpy(contiguous_buf_char, noncontiguous_buf_char, block_length_ * old_type_->size());
58     else
59       old_type_->serialize( noncontiguous_buf_char, contiguous_buf_char, block_length_);
60
61     contiguous_buf_char += block_length_*old_type_->size();
62     if((i+1)%block_count_ ==0)
63       noncontiguous_buf_char += block_length_*old_type_->size();
64     else
65       noncontiguous_buf_char += block_stride_;
66   }
67 }
68
69 void Type_Hvector::unserialize(const void* contiguous_buf, void *noncontiguous_buf,
70                               int count, MPI_Op op){
71   const char* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
72   char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf);
73
74   for (int i = 0; i < block_count_ * count; i++) {
75     if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
76       if(op!=MPI_OP_NULL)
77         op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_length_, old_type_);
78     }else
79       old_type_->unserialize( contiguous_buf_char, noncontiguous_buf_char, block_length_, op);
80     contiguous_buf_char += block_length_*old_type_->size();
81     if((i+1)%block_count_ ==0)
82       noncontiguous_buf_char += block_length_*old_type_->size();
83     else
84       noncontiguous_buf_char += block_stride_;
85   }
86 }
87
88 Type_Vector::Type_Vector(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, int block_length, int stride,
89                          MPI_Datatype old_type)
90     : Type_Hvector(size, lb, ub, flags, count, block_length, stride * old_type->get_extent(), old_type)
91 {
92 }
93
94 Type_Hindexed::Type_Hindexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
95                              const MPI_Aint* block_indices, MPI_Datatype old_type)
96     : Datatype(size, lb, ub, flags)
97     , block_count_(count)
98     , block_lengths_(new int[count])
99     , block_indices_(new MPI_Aint[count])
100     , old_type_(old_type)
101 {
102   old_type_->ref();
103   for (int i = 0; i < count; i++) {
104     block_lengths_[i] = block_lengths[i];
105     block_indices_[i] = block_indices[i];
106   }
107 }
108
109 Type_Hindexed::Type_Hindexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
110                              const int* block_indices, MPI_Datatype old_type, MPI_Aint factor)
111     : Datatype(size, lb, ub, flags)
112     , block_count_(count)
113     , block_lengths_(new int[count])
114     , block_indices_(new MPI_Aint[count])
115     , old_type_(old_type)
116 {
117   old_type_->ref();
118   for (int i = 0; i < count; i++) {
119     block_lengths_[i] = block_lengths[i];
120     block_indices_[i] = block_indices[i] * factor;
121   }
122 }
123
124 Type_Hindexed::~Type_Hindexed()
125 {
126   Datatype::unref(old_type_);
127   if(refcount()==0){
128     delete[] block_lengths_;
129     delete[] block_indices_;
130   }
131 }
132
133 void Type_Hindexed::serialize(const void* noncontiguous_buf, void *contiguous_buf,
134                 int count){
135   char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
136   const char* noncontiguous_buf_iter = static_cast<const char*>(noncontiguous_buf);
137   const char* noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[0];
138   for (int j = 0; j < count; j++) {
139     for (int i = 0; i < block_count_; i++) {
140       if (not(old_type_->flags() & DT_FLAG_DERIVED))
141         memcpy(contiguous_buf_char, noncontiguous_buf_char, block_lengths_[i] * old_type_->size());
142       else
143         old_type_->serialize(noncontiguous_buf_char, contiguous_buf_char,block_lengths_[i]);
144
145       contiguous_buf_char += block_lengths_[i]*old_type_->size();
146       if (i<block_count_-1)
147         noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[i+1];
148       else
149         noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
150     }
151     noncontiguous_buf_iter=noncontiguous_buf_char;
152   }
153 }
154
155 void Type_Hindexed::unserialize(const void* contiguous_buf, void *noncontiguous_buf,
156                           int count, MPI_Op op){
157   const char* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
158   char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+ block_indices_[0];
159   for (int j = 0; j < count; j++) {
160     for (int i = 0; i < block_count_; i++) {
161       if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
162         if(op!=MPI_OP_NULL)
163           op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i],
164                             old_type_);
165       }else
166         old_type_->unserialize( contiguous_buf_char,noncontiguous_buf_char,block_lengths_[i], op);
167
168       contiguous_buf_char += block_lengths_[i]*old_type_->size();
169       if (i<block_count_-1)
170         noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
171       else
172         noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
173     }
174     noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
175   }
176 }
177
178 Type_Indexed::Type_Indexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
179                            const int* block_indices, MPI_Datatype old_type)
180     : Type_Hindexed(size, lb, ub, flags, count, block_lengths, block_indices, old_type, old_type->get_extent())
181 {
182 }
183
184 Type_Struct::Type_Struct(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
185                          const MPI_Aint* block_indices, const MPI_Datatype* old_types)
186     : Datatype(size, lb, ub, flags)
187     , block_count_(count)
188     , block_lengths_(new int[count])
189     , block_indices_(new MPI_Aint[count])
190     , old_types_(new MPI_Datatype[count])
191 {
192   for (int i = 0; i < count; i++) {
193     block_lengths_[i]=block_lengths[i];
194     block_indices_[i]=block_indices[i];
195     old_types_[i]=old_types[i];
196     old_types_[i]->ref();
197   }
198 }
199
200 Type_Struct::~Type_Struct(){
201   for (int i = 0; i < block_count_; i++) {
202     Datatype::unref(old_types_[i]);
203   }
204   if(refcount()==0){
205     delete[] block_lengths_;
206     delete[] block_indices_;
207     delete[] old_types_;
208   }
209 }
210
211
212 void Type_Struct::serialize(const void* noncontiguous_buf, void *contiguous_buf,
213                         int count){
214   char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
215   const char* noncontiguous_buf_iter = static_cast<const char*>(noncontiguous_buf);
216   const char* noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[0];
217   for (int j = 0; j < count; j++) {
218     for (int i = 0; i < block_count_; i++) {
219       if (not(old_types_[i]->flags() & DT_FLAG_DERIVED))
220         memcpy(contiguous_buf_char, noncontiguous_buf_char, block_lengths_[i] * old_types_[i]->size());
221       else
222         old_types_[i]->serialize( noncontiguous_buf_char,contiguous_buf_char,block_lengths_[i]);
223
224
225       contiguous_buf_char += block_lengths_[i]*old_types_[i]->size();
226       if (i<block_count_-1)
227         noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[i+1];
228       else //let's hope this is MPI_UB ?
229         noncontiguous_buf_char += block_lengths_[i]*old_types_[i]->get_extent();
230     }
231     noncontiguous_buf_iter=noncontiguous_buf_char;
232   }
233 }
234
235 void Type_Struct::unserialize(const void* contiguous_buf, void *noncontiguous_buf,
236                               int count, MPI_Op op){
237   const char* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
238   char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+ block_indices_[0];
239   for (int j = 0; j < count; j++) {
240     for (int i = 0; i < block_count_; i++) {
241       if (not(old_types_[i]->flags() & DT_FLAG_DERIVED)) {
242         if(op!=MPI_OP_NULL)
243           op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i], old_types_[i]);
244       }else
245         old_types_[i]->unserialize( contiguous_buf_char, noncontiguous_buf_char,block_lengths_[i], op);
246
247       contiguous_buf_char += block_lengths_[i]*old_types_[i]->size();
248       if (i<block_count_-1)
249         noncontiguous_buf_char =  static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
250       else
251         noncontiguous_buf_char += block_lengths_[i]*old_types_[i]->get_extent();
252     }
253     noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
254   }
255 }
256
257 }
258 }