Logo AND Algorithmique Numérique Distribuée

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