Page MenuHomec4science

grid.hh
No OneTemporary

File Metadata

Created
Tue, Jun 18, 11:59
#include <algorithm>
#include <cstddef>
#include <iostream>
#include <memory>
#include <mpi.h>
#include <vector>
#include <random>
#ifndef GRID_H
#define GRID_H
class Grid {
typedef char T;
public:
Grid(const long int ldims[2], const long int gdims[2],
const long int offsets[2], int ghost)
: _dims{ldims[0], ldims[1]}, _gdims{gdims[0], gdims[1]},
_offsets{offsets[0], offsets[1]}, _ghosts(ghost),
_grid(new T[(ldims[0] + 2 * _ghosts) * (ldims[1] + 2 * _ghosts)]),
_requests(NULL), _nb_requests(0) {
clear();
}
Grid(const Grid & other)
: Grid(other._dims, other._gdims, other._offsets, other._ghosts) {
std::copy_n(other._grid, fullSize(), _grid);
}
~Grid();
T & operator()(long int i, long int j) {
return *(_grid + (i + _ghosts) * (_dims[1] + 2 * _ghosts) + (j + _ghosts));
}
void swap(Grid & other) {
T * tmp = _grid;
_grid = other._grid;
other._grid = tmp;
MPI_Request * tmp_reqs = _requests;
_requests = other._requests;
other._requests = tmp_reqs;
}
void setOffsets(long int offsets[2]) {
_offsets[0] = offsets[0];
_offsets[1] = offsets[1];
}
void clear() {
std::uninitialized_fill_n(_grid, fullSize() * sizeof(T), T());
}
void randomize() {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, 4);
for (long int i = 0; i < _dims[0]; i++) {
for (long int j = 0; j < _dims[1]; j++) {
operator()(i, j) = dis(gen) > 3 ? 1 : 0;
}
}
}
void initalizeCommunicationRequests(MPI_Comm & comm, int coords[2]);
void commAll() { MPI_Startall(2 * _nb_requests, _requests); }
void waitRecv() {
MPI_Waitall(_nb_requests, _requests + _nb_requests, MPI_STATUS_IGNORE);
}
void waitSend() { MPI_Waitall(_nb_requests, _requests, MPI_STATUS_IGNORE); }
long int size(long int i) const { return _dims[i]; }
long int size() const { return _dims[0] * _dims[1]; }
long int globalSize(long int i) const { return _gdims[i]; }
long int globalSize() const { return _gdims[0] * _gdims[1]; }
long int fullSize(long int i) const { return (_dims[i] + 2 * _ghosts); }
long int fullSize() const {
return (_dims[0] + 2 * _ghosts) * (_dims[1] + 2 * _ghosts);
}
long int offset(long int i) const { return _offsets[i]; }
long int ghost() const { return _ghosts; }
const T * data() const { return _grid; }
private:
long int _dims[2];
long int _gdims[2];
long int _offsets[2];
long int _ghosts;
T * _grid;
MPI_Request * _requests;
MPI_Datatype _column_t, _line_t, _corner_t;
int _nb_requests;
};
#endif /* GRID_H */

Event Timeline