Newer
Older
/***************************************************************************
DistributedArrayView_impl.h - description
-------------------
begin : Sep 20, 2018
copyright : (C) 2018 by Tomas Oberhuber et al.
email : tomas.oberhuber@fjfi.cvut.cz
***************************************************************************/
/* See Copyright Notice in tnl/Copyright */
// Implemented by: Jakub Klinkovský
#pragma once
#include "DistributedArrayView.h"
namespace TNL {
namespace Containers {
template< typename Value,
typename Device,
typename Index,
typename Communicator >
template< typename Value_ >
__cuda_callable__
DistributedArrayView< Value, Device, Index, Communicator >::
DistributedArrayView( const DistributedArrayView< Value_, Device, Index, Communicator >& view )
: localRange( view.getLocalRange() ),
globalSize( view.getSize() ),
group( view.getCommunicationGroup() ),
localData( view.getConstLocalView() )
{}
template< typename Value,
typename Device,
typename Index,
typename Communicator >
Jakub Klinkovský
committed
__cuda_callable__
void
DistributedArrayView< Value, Device, Index, Communicator >::
Jakub Klinkovský
committed
bind( DistributedArrayView view )
{
localRange = view.getLocalRange();
globalSize = view.getSize();
group = view.getCommunicationGroup();
localData.bind( view.getLocalView() );
Jakub Klinkovský
committed
}
template< typename Value,
typename Device,
typename Index,
typename Communicator >
template< typename Value_ >
Jakub Klinkovský
committed
void
DistributedArrayView< Value, Device, Index, Communicator >::
Jakub Klinkovský
committed
bind( Value_* data, IndexType localSize )
{
TNL_ASSERT_EQ( localSize, localRange.getSize(),
"The local array size does not match the local range of the distributed array." );
localData.bind( data, localSize );
}
template< typename Value,
typename Device,
typename Index,
typename Communicator >
__cuda_callable__
Jakub Klinkovský
committed
typename DistributedArrayView< Value, Device, Index, Communicator >::ViewType
DistributedArrayView< Value, Device, Index, Communicator >::
Jakub Klinkovský
committed
getView()
Jakub Klinkovský
committed
return *this;
}
template< typename Value,
typename Device,
typename Index,
typename Communicator >
Jakub Klinkovský
committed
__cuda_callable__
typename DistributedArrayView< Value, Device, Index, Communicator >::ConstViewType
DistributedArrayView< Value, Device, Index, Communicator >::
Jakub Klinkovský
committed
getConstView() const
Jakub Klinkovský
committed
return *this;
}
template< typename Value,
typename Device,
typename Index,
typename Communicator >
DistributedArrayView< Value, Device, Index, Communicator >&
DistributedArrayView< Value, Device, Index, Communicator >::
operator=( const DistributedArrayView& view )
{
TNL_ASSERT_EQ( getSize(), view.getSize(), "The sizes of the array views must be equal, views are not resizable." );
TNL_ASSERT_EQ( getLocalRange(), view.getLocalRange(), "The local ranges must be equal, views are not resizable." );
TNL_ASSERT_EQ( getCommunicationGroup(), view.getCommunicationGroup(), "The communication groups of the array views must be equal." );
localData = view.getConstLocalView();
return *this;
}
template< typename Value,
typename Device,
typename Index,
typename Communicator >
template< typename Array >
DistributedArrayView< Value, Device, Index, Communicator >&
DistributedArrayView< Value, Device, Index, Communicator >::
operator=( const Array& array )
{
TNL_ASSERT_EQ( getSize(), array.getSize(), "The global sizes must be equal, views are not resizable." );
TNL_ASSERT_EQ( getLocalRange(), array.getLocalRange(), "The local ranges must be equal, views are not resizable." );
TNL_ASSERT_EQ( getCommunicationGroup(), array.getCommunicationGroup(), "The communication groups must be equal." );
localData = array.getConstLocalView();
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
return *this;
}
template< typename Value,
typename Device,
typename Index,
typename Communicator >
const Subrange< Index >&
DistributedArrayView< Value, Device, Index, Communicator >::
getLocalRange() const
{
return localRange;
}
template< typename Value,
typename Device,
typename Index,
typename Communicator >
typename Communicator::CommunicationGroup
DistributedArrayView< Value, Device, Index, Communicator >::
getCommunicationGroup() const
{
return group;
}
template< typename Value,
typename Device,
typename Index,
typename Communicator >
typename DistributedArrayView< Value, Device, Index, Communicator >::LocalViewType
DistributedArrayView< Value, Device, Index, Communicator >::
getLocalView()
{
return localData;
}
template< typename Value,
typename Device,
typename Index,
typename Communicator >
typename DistributedArrayView< Value, Device, Index, Communicator >::ConstLocalViewType
DistributedArrayView< Value, Device, Index, Communicator >::
getConstLocalView() const
{
return localData;
}
template< typename Value,
typename Device,
typename Index,
typename Communicator >
void
DistributedArrayView< Value, Device, Index, Communicator >::
copyFromGlobal( ConstLocalViewType globalArray )
{
TNL_ASSERT_EQ( getSize(), globalArray.getSize(),
"given global array has different size than the distributed array view" );
LocalViewType localView( localData );
const LocalRangeType localRange = getLocalRange();
auto kernel = [=] __cuda_callable__ ( IndexType i ) mutable
{
localView[ i ] = globalArray[ localRange.getGlobalIndex( i ) ];
};
ParallelFor< DeviceType >::exec( (IndexType) 0, localRange.getSize(), kernel );
}
template< typename Value,
typename Device,
typename Index,
typename Communicator >
String
DistributedArrayView< Value, Device, Index, Communicator >::
getType()
{
return String( "Containers::DistributedArrayView< " ) +
TNL::getType< Value >() + ", " +
Device::getDeviceType() + ", " +
TNL::getType< Index >() + ", " +
// TODO: communicators don't have a getType method
"<Communicator> >";
}
template< typename Value,
typename Device,
typename Index,
typename Communicator >
void
DistributedArrayView< Value, Device, Index, Communicator >::
reset()
{
localRange.reset();
globalSize = 0;
group = Communicator::NullGroup;
localData.reset();
}
template< typename Value,
typename Device,
typename Index,
typename Communicator >
bool
DistributedArrayView< Value, Device, Index, Communicator >::
empty() const
{
return getSize() == 0;
}
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// TODO: swap
template< typename Value,
typename Device,
typename Index,
typename Communicator >
Index
DistributedArrayView< Value, Device, Index, Communicator >::
getSize() const
{
return globalSize;
}
template< typename Value,
typename Device,
typename Index,
typename Communicator >
void
DistributedArrayView< Value, Device, Index, Communicator >::
setValue( ValueType value )
{
localData.setValue( value );
}
template< typename Value,
typename Device,
typename Index,
typename Communicator >
void
DistributedArrayView< Value, Device, Index, Communicator >::
setElement( IndexType i, ValueType value )
{
const IndexType li = localRange.getLocalIndex( i );
localData.setElement( li, value );
}
template< typename Value,
typename Device,
typename Index,
typename Communicator >
Value
DistributedArrayView< Value, Device, Index, Communicator >::
getElement( IndexType i ) const
{
const IndexType li = localRange.getLocalIndex( i );
return localData.getElement( li );
}
template< typename Value,
typename Device,
typename Index,
typename Communicator >
__cuda_callable__
Value&
DistributedArrayView< Value, Device, Index, Communicator >::
operator[]( IndexType i )
{
const IndexType li = localRange.getLocalIndex( i );
return localData[ li ];
}
template< typename Value,
typename Device,
typename Index,
typename Communicator >
__cuda_callable__
const Value&
DistributedArrayView< Value, Device, Index, Communicator >::
operator[]( IndexType i ) const
{
const IndexType li = localRange.getLocalIndex( i );
return localData[ li ];
}
template< typename Value,
typename Device,
typename Index,
typename Communicator >
template< typename Array >
bool
DistributedArrayView< Value, Device, Index, Communicator >::
operator==( const Array& array ) const
{
// we can't run allreduce if the communication groups are different
if( group != array.getCommunicationGroup() )
return false;
const bool localResult =
localRange == array.getLocalRange() &&
globalSize == array.getSize() &&
localData == array.getConstLocalView();
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
bool result = true;
if( group != CommunicatorType::NullGroup )
CommunicatorType::Allreduce( &localResult, &result, 1, MPI_LAND, group );
return result;
}
template< typename Value,
typename Device,
typename Index,
typename Communicator >
template< typename Array >
bool
DistributedArrayView< Value, Device, Index, Communicator >::
operator!=( const Array& array ) const
{
return ! (*this == array);
}
template< typename Value,
typename Device,
typename Index,
typename Communicator >
bool
DistributedArrayView< Value, Device, Index, Communicator >::
containsValue( ValueType value ) const
{
bool result = false;
if( group != CommunicatorType::NullGroup ) {
const bool localResult = localData.containsValue( value );
CommunicatorType::Allreduce( &localResult, &result, 1, MPI_LOR, group );
}
return result;
}
template< typename Value,
typename Device,
typename Index,
typename Communicator >
bool
DistributedArrayView< Value, Device, Index, Communicator >::
containsOnlyValue( ValueType value ) const
{
bool result = true;
if( group != CommunicatorType::NullGroup ) {
const bool localResult = localData.containsOnlyValue( value );
CommunicatorType::Allreduce( &localResult, &result, 1, MPI_LAND, group );
}
return result;
}
} // namespace Containers
} // namespace TNL