Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
constify MPI_Group*
[simgrid.git] / src / smpi / mpi / smpi_datatype_derived.cpp
1 /* smpi_datatype.cpp -- MPI primitives to handle datatypes                  */
2 /* Copyright (c) 2009-2019. 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(void* noncontiguous_buf, void* contiguous_buf, int count)
28 {
29   char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
30   char* noncontiguous_buf_char = static_cast<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(void* contiguous_buf, void* noncontiguous_buf, int count, MPI_Op op)
35 {
36   char* contiguous_buf_char = static_cast<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( void* noncontiguous_buf, void *contiguous_buf,
51                     int count){
52   char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
53   char* noncontiguous_buf_char = static_cast<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( void* contiguous_buf, void *noncontiguous_buf,
70                               int count, MPI_Op op){
71   char* contiguous_buf_char = static_cast<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, int* block_lengths,
95                              MPI_Aint* block_indices, MPI_Datatype old_type)
96     : Datatype(size, lb, ub, flags), block_count_(count), old_type_(old_type)
97 {
98   old_type_->ref();
99   block_lengths_ = new int[count];
100   block_indices_ = new MPI_Aint[count];
101   for (int i = 0; i < count; i++) {
102     block_lengths_[i] = block_lengths[i];
103     block_indices_[i] = block_indices[i];
104   }
105 }
106
107 Type_Hindexed::Type_Hindexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, int* block_lengths,
108                              int* block_indices, MPI_Datatype old_type, MPI_Aint factor)
109     : Datatype(size, lb, ub, flags), block_count_(count), old_type_(old_type)
110 {
111   old_type_->ref();
112   block_lengths_ = new int[count];
113   block_indices_ = new MPI_Aint[count];
114   for (int i = 0; i < count; i++) {
115     block_lengths_[i] = block_lengths[i];
116     block_indices_[i] = block_indices[i] * factor;
117   }
118 }
119
120 Type_Hindexed::~Type_Hindexed()
121 {
122   Datatype::unref(old_type_);
123   if(refcount()==0){
124     delete[] block_lengths_;
125     delete[] block_indices_;
126   }
127 }
128
129 void Type_Hindexed::serialize( void* noncontiguous_buf, void *contiguous_buf,
130                 int count){
131   char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
132   char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+ block_indices_[0];
133   for (int j = 0; j < count; j++) {
134     for (int i = 0; i < block_count_; i++) {
135       if (not(old_type_->flags() & DT_FLAG_DERIVED))
136         memcpy(contiguous_buf_char, noncontiguous_buf_char, block_lengths_[i] * old_type_->size());
137       else
138         old_type_->serialize(noncontiguous_buf_char, contiguous_buf_char,block_lengths_[i]);
139
140       contiguous_buf_char += block_lengths_[i]*old_type_->size();
141       if (i<block_count_-1)
142         noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
143       else
144         noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
145     }
146     noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
147   }
148 }
149
150 void Type_Hindexed::unserialize( void* contiguous_buf, void *noncontiguous_buf,
151                           int count, MPI_Op op){
152   char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
153   char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+ block_indices_[0];
154   for (int j = 0; j < count; j++) {
155     for (int i = 0; i < block_count_; i++) {
156       if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
157         if(op!=MPI_OP_NULL)
158           op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i],
159                             old_type_);
160       }else
161         old_type_->unserialize( contiguous_buf_char,noncontiguous_buf_char,block_lengths_[i], op);
162
163       contiguous_buf_char += block_lengths_[i]*old_type_->size();
164       if (i<block_count_-1)
165         noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
166       else
167         noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
168     }
169     noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
170   }
171 }
172
173 Type_Indexed::Type_Indexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, int* block_lengths,
174                            int* block_indices, MPI_Datatype old_type)
175     : Type_Hindexed(size, lb, ub, flags, count, block_lengths, block_indices, old_type, old_type->get_extent())
176 {
177 }
178
179 Type_Struct::Type_Struct(int size,MPI_Aint lb, MPI_Aint ub, int flags, int count, int* block_lengths, MPI_Aint* block_indices, MPI_Datatype* old_types): Datatype(size, lb, ub, flags), block_count_(count), block_lengths_(block_lengths), block_indices_(block_indices), old_types_(old_types){
180   block_lengths_= new int[count];
181   block_indices_= new MPI_Aint[count];
182   old_types_=  new MPI_Datatype[count];
183   for (int i = 0; i < count; i++) {
184     block_lengths_[i]=block_lengths[i];
185     block_indices_[i]=block_indices[i];
186     old_types_[i]=old_types[i];
187     old_types_[i]->ref();
188   }
189 }
190
191 Type_Struct::~Type_Struct(){
192   for (int i = 0; i < block_count_; i++) {
193     Datatype::unref(old_types_[i]);
194   }
195   if(refcount()==0){
196     delete[] block_lengths_;
197     delete[] block_indices_;
198     delete[] old_types_;
199   }
200 }
201
202
203 void Type_Struct::serialize( void* noncontiguous_buf, void *contiguous_buf,
204                         int count){
205   char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
206   char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+ block_indices_[0];
207   for (int j = 0; j < count; j++) {
208     for (int i = 0; i < block_count_; i++) {
209       if (not(old_types_[i]->flags() & DT_FLAG_DERIVED))
210         memcpy(contiguous_buf_char, noncontiguous_buf_char, block_lengths_[i] * old_types_[i]->size());
211       else
212         old_types_[i]->serialize( noncontiguous_buf_char,contiguous_buf_char,block_lengths_[i]);
213
214
215       contiguous_buf_char += block_lengths_[i]*old_types_[i]->size();
216       if (i<block_count_-1)
217         noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
218       else //let's hope this is MPI_UB ?
219         noncontiguous_buf_char += block_lengths_[i]*old_types_[i]->get_extent();
220     }
221     noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
222   }
223 }
224
225 void Type_Struct::unserialize( void* contiguous_buf, void *noncontiguous_buf,
226                               int count, MPI_Op op){
227   char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
228   char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+ block_indices_[0];
229   for (int j = 0; j < count; j++) {
230     for (int i = 0; i < block_count_; i++) {
231       if (not(old_types_[i]->flags() & DT_FLAG_DERIVED)) {
232         if(op!=MPI_OP_NULL)
233           op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i], old_types_[i]);
234       }else
235         old_types_[i]->unserialize( contiguous_buf_char, noncontiguous_buf_char,block_lengths_[i], op);
236
237       contiguous_buf_char += block_lengths_[i]*old_types_[i]->size();
238       if (i<block_count_-1)
239         noncontiguous_buf_char =  static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
240       else
241         noncontiguous_buf_char += block_lengths_[i]*old_types_[i]->get_extent();
242     }
243     noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
244   }
245 }
246
247 }
248 }