/usr/include/dune/grid/uggrid/uglbgatherscatter.hh is in libdune-grid-dev 2.5.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_UGLBGATHERSCATTER_HH
#define DUNE_UGLBGATHERSCATTER_HH
namespace Dune {
/** \brief Gather/scatter methods for dynamic loadbalancing with UGGrid
*/
class UGLBGatherScatter
{
class LBMessageBuffer
{
public:
template <class DataType>
void read(DataType& x)
{
count_--;
memcpy(&x, data_ + count_*sizeof(DataType), sizeof(DataType));
if (!count_)
free(data_);
}
template <class DataType>
void write(const DataType& x)
{
count_++;
char* moreData_ = (char*)realloc(data_, count_*sizeof(DataType));
if (moreData_)
data_ = moreData_;
memcpy(data_ + (count_-1)*sizeof(DataType), &x, sizeof(DataType));
}
LBMessageBuffer()
: count_(0), data_(nullptr)
{}
private:
int count_;
char* data_;
};
public:
/** \brief Gather data before load balancing
*/
template <int codim, class GridView, class DataHandle>
static void gather(const GridView& gridView, DataHandle& dataHandle)
{
const int dim = GridView::dimension;
// do nothing if nothing has to be done
if (!dataHandle.contains(dim, codim))
return;
// write the data into a global vector on process 0
// write the macrogrid index of each entity into the corresponding UG vector
for (const auto entity : entities(gridView, Codim<codim>()))
{
int numberOfParams = dataHandle.size(entity);
if (!numberOfParams)
continue;
// obtain data from DUNE handle and write it into the UG message buffer
LBMessageBuffer lbMessageBuffer;
dataHandle.gather(lbMessageBuffer, entity);
char*& buffer = gridView.grid().getRealImplementation(entity).getTarget()->message_buffer;
assert(not buffer);
typedef typename DataHandle::DataType DataType;
buffer = (char*)malloc(sizeof(int) + numberOfParams*sizeof(DataType));
*((int*)buffer) = numberOfParams*sizeof(DataType); // Size of the actual payload
for (int paramIdx = 0; paramIdx < numberOfParams; paramIdx++)
{
DataType *dataPointer = (DataType*)(buffer + sizeof(int) + paramIdx*sizeof(DataType));
lbMessageBuffer.read(*dataPointer);
}
}
}
/** \brief Scatter data after load balancing
*/
template <int codim, class GridView, class DataHandle>
static void scatter(const GridView& gridView, DataHandle& dataHandle)
{
const int dim = GridView::dimension;
// do nothing if nothing has to be done
if (!dataHandle.contains(dim, codim))
return;
// obtain the data from the global vector with help of
// the macro index and scatter it
for (const auto entity : entities(gridView, Codim<codim>()))
{
int numberOfParams = dataHandle.size(entity);
if (!numberOfParams)
continue;
// get data from UG message buffer and write to DUNE message buffer
char*& buffer = gridView.grid().getRealImplementation(entity).getTarget()->message_buffer;
assert(buffer);
LBMessageBuffer lbMessageBuffer;
for(int paramIdx = 0; paramIdx < numberOfParams; paramIdx++)
{
typedef typename DataHandle::DataType DataType;
DataType *dataPointer = (DataType*)(buffer + sizeof(int) + paramIdx*sizeof(DataType));
lbMessageBuffer.write(*dataPointer);
}
// call the data handle with the message buffer
dataHandle.scatter(lbMessageBuffer, entity, numberOfParams);
// free object's local message buffer
free (buffer);
buffer = nullptr;
}
}
};
} // namespace Dune
#endif
|