19 #ifndef _SF_ABSTRACT_VECTOR_H 20 #define _SF_ABSTRACT_VECTOR_H 53 template<
class T,
class S>
77 ltype inp_layout) = 0;
94 virtual void init(
int igsize,
int ilsize,
int idpn = 1,
ltype ilayout = unset) = 0;
122 MPI_Allreduce(MPI_IN_PLACE, &N, 1, MPI_INT, MPI_SUM, mesh->
comm);
136 return std::tuple<int, int>(N, n);
146 virtual void set(
const vector<T>& idx,
const vector<S>& vals,
const bool additive =
false) = 0;
154 virtual void set(
const vector<T>& idx,
const S val) = 0;
161 virtual void set(
const S val) = 0;
171 virtual void set(
const T idx,
const S val) = 0;
180 virtual void get(
const vector<T> & idx, S *out) = 0;
189 virtual S
get(
const T idx) = 0;
292 virtual T
lsize()
const = 0;
299 virtual T
gsize()
const = 0;
316 virtual S*
ptr() = 0;
351 virtual S
mag()
const = 0;
358 virtual S
sum()
const = 0;
365 virtual S
min()
const = 0;
381 virtual bool is_init()
const = 0;
388 virtual std::string
to_string()
const = 0;
439 MPI_Comm comm = this->mesh != NULL ? this->mesh->
comm : PETSC_COMM_WORLD;
440 MPI_Comm_rank(comm, &rank);
441 MPI_Comm_size(comm, &size);
443 int glb_size = this->
gsize();
444 int loc_size = this->
lsize();
451 fd = fopen(file,
"w");
455 MPI_Allreduce(MPI_IN_PLACE, &err, 1, MPI_INT, MPI_MAX, comm);
465 fprintf(fd,
"%d\n", glb_size);
467 for(
int i=0; i<loc_size/this->
dpn; i++) {
468 for(
int j=0; j<this->
dpn; j++)
469 nwr += fprintf(fd,
"%f ", p[i*this->dpn+j]);
470 nwr += fprintf(fd,
"\n");
474 for(
int pid=1; pid < size; pid++)
479 MPI_Recv(&rsize, 1, MPI_INT, pid,
SF_MPITAG, comm, &stat);
481 MPI_Recv(wbuff.
data(), rsize*
sizeof(S), MPI_BYTE, pid,
SF_MPITAG, comm, &stat);
483 for(
int i=0; i<rsize/this->
dpn; i++) {
484 for(
int j=0; j<this->
dpn; j++)
485 nwr += fprintf(fd,
"%f ", wbuff[i*this->dpn+j]);
486 nwr += fprintf(fd,
"\n");
492 MPI_Send(&loc_size, 1, MPI_INT, 0,
SF_MPITAG, comm);
493 MPI_Send(p, loc_size*
sizeof(S), MPI_BYTE, 0,
SF_MPITAG, comm);
497 MPI_Bcast(&nwr, 1, MPI_LONG, 0, comm);
513 MPI_Comm comm = this->mesh != NULL ? this->mesh->
comm : PETSC_COMM_WORLD;
514 MPI_Comm_rank(comm, &rank); MPI_Comm_size(comm, &size);
516 long int loc_size = this->
lsize();
520 for(
long int i=0; i<loc_size; i++) buff[i] = p[i];
533 MPI_Comm comm = this->mesh != NULL ? this->mesh->
comm : PETSC_COMM_WORLD;
534 int rank; MPI_Comm_rank(comm, &rank);
540 fd = fopen(file.c_str(),
"w");
545 MPI_Allreduce(MPI_IN_PLACE, &error, 1, MPI_INT, MPI_SUM, comm);
548 nwr = this->write_binary<V>(fd);
561 MPI_Comm comm = this->mesh != NULL ? this->mesh->
comm : PETSC_COMM_WORLD;
563 size_t loc_size = this->
lsize();
569 for(
size_t i=0; i<loc_size; i++)
580 MPI_Comm comm = mesh != NULL ? mesh->
comm : PETSC_COMM_WORLD;
583 int rank; MPI_Comm_rank(comm, &rank);
589 fd = fopen(file.c_str(),
"r");
594 MPI_Allreduce(MPI_IN_PLACE, &error, 1, MPI_INT, MPI_SUM, comm);
597 nrd = read_binary<V>(fd);
609 MPI_Comm comm = this->mesh != NULL ? this->mesh->
comm : PETSC_COMM_WORLD;
611 size_t loc_size = this->
lsize();
621 MPI_Comm comm = this->mesh != NULL ? this->mesh->
comm : PETSC_COMM_WORLD;
622 int rank; MPI_Comm_rank(comm, &rank);
630 fd = fopen(file.c_str(),
"r");
635 MPI_Allreduce(MPI_IN_PLACE, &error, 1, MPI_INT, MPI_SUM, comm);
652 template<
class T,
class S>
685 if(b_buff) VecDestroy(&b_buff);
686 if(vec_sc) VecScatterDestroy(&vec_sc);
694 template<
class T,
class S>
706 template<
class T,
class S>
718 template<
class T,
class S>
756 const size_t gsize_a,
757 const size_t gsize_b,
760 assert(_registry.
count(spec) == 0);
763 _registry[spec] = sc;
772 for(
size_t i=0; i<nbr_a.
size(); i++)
773 for(
short j=0; j<
dpn; j++) idx_a[i*dpn+j] = nbr_a[i]*dpn+j;
775 for(
size_t i=0; i<nbr_b.
size(); i++)
776 for(
short j=0; j<
dpn; j++) idx_b[i*dpn+j] = nbr_b[i]*dpn+j;
778 err += ISCreateGeneral(PETSC_COMM_WORLD, idx_a.size(), idx_a.data(), PETSC_COPY_VALUES, &is_a);
779 err += ISCreateGeneral(PETSC_COMM_WORLD, idx_b.size(), idx_b.data(), PETSC_COPY_VALUES, &is_b);
781 err += ISSetPermutation(is_b);
784 err += VecCreateMPI(PETSC_COMM_WORLD, idx_a.size(), gsize_a*
dpn, &a);
785 err += VecCreateMPI(PETSC_COMM_WORLD, idx_b.size(), gsize_b*
dpn, &sc->b_buff);
787 err += VecScatterCreate(a, is_a, sc->b_buff, is_b, &sc->vec_sc);
789 err += VecDestroy(&a);
790 err += ISDestroy (&is_a);
791 err += ISDestroy (&is_b);
794 fprintf(stderr,
"%s : an error has occurred! Scattering spec: Mesh ID %d, permutation ID %d, numbering %d, dpn %d!\n",
795 __func__,
int(spec.
v1),
int(spec.
v2),
int(spec.
v3),
int(spec.
v4));
797 sc->idx_a.assign(idx_a.begin(), idx_a.end());
798 sc->idx_b.assign(idx_b.begin(), idx_b.end());
819 assert(_registry.
count(spec) == 0);
822 assert(idx_a.
size() == idx_b.
size());
825 _registry[spec] = sc;
833 T lsize_a = layout_a[rank+1] - layout_a[rank];
834 T lsize_b = layout_b[rank+1] - layout_b[rank];
835 T gsize_a = layout_a[layout_a.size()-1];
836 T gsize_b = layout_b[layout_a.size()-1];
840 for(
size_t i=0; i<idx_a.
size(); i++)
841 for(
short j=0; j<
dpn; j++) sidx_a[i*dpn+j] = idx_a[i]*dpn+j;
843 for(
size_t i=0; i<idx_b.
size(); i++)
844 for(
short j=0; j<
dpn; j++) sidx_b[i*dpn+j] = idx_b[i]*dpn+j;
846 err += ISCreateGeneral(PETSC_COMM_WORLD, sidx_a.size(), sidx_a.data(), PETSC_COPY_VALUES, &is_a);
847 err += ISCreateGeneral(PETSC_COMM_WORLD, sidx_b.size(), sidx_b.data(), PETSC_COPY_VALUES, &is_b);
849 err += VecCreateMPI(PETSC_COMM_WORLD, lsize_a*dpn, gsize_a*dpn, &a);
850 err += VecCreateMPI(PETSC_COMM_WORLD, lsize_b*dpn, gsize_b*dpn, &sc->b_buff);
852 err += VecScatterCreate(a, is_a, sc->b_buff, is_b, &sc->vec_sc);
854 err += VecDestroy(&a);
855 err += ISDestroy (&is_a);
856 err += ISDestroy (&is_b);
859 fprintf(stderr,
"%s : an error has occurred! Scattering spec: To ID %d, From ID %d, numbering %d, dpn %d!\n",
860 __func__,
int(spec.
v1),
int(spec.
v2),
int(spec.
v3),
int(spec.
v4));
862 sc->idx_a.assign(idx_a.
begin(), idx_a.
end());
863 sc->idx_b.assign(idx_b.
begin(), idx_b.
end());
883 if(_registry.
count(spec))
884 ret = _registry[spec];
895 for(it = _registry.
begin(); it != _registry.
end(); ++it) {
908 #endif // _SF_ABSTRACT_VECTOR_H
virtual void overshadow(const abstract_vector< T, S > &sub, bool member, int offset, int sz, bool share)=0
void operator()(abstract_vector< T, S > &v, bool fwd)
Apply the scattering on a data vector.
virtual void operator-=(const abstract_vector< T, S > &vec)=0
virtual void release_ptr(S *&p)=0
int dpn
d.o.f. per mesh vertex.
virtual void add_scaled(const abstract_vector< T, S > &vec, S k)=0
virtual void finish_assembly()=0
MPI_Comm comm
the parallel mesh is defined on a MPI world
size_t root_read_ascii(FILE *fd, vector< V > &vec, MPI_Comm comm, bool int_data)
Read binary data into a vector.
virtual void forward(abstract_vector< T, S > &out, scattering &sc, bool add=false)=0
scattering * register_permutation(const quadruple< int > spec, const vector< T > &nbr_a, const vector< T > &nbr_b, const size_t gsize_a, const size_t gsize_b, const short dpn)
Register a permutation scattering.
VecScatter vec_sc
The scatterer.
scattering * get_scattering(const quadruple< int > spec)
Access an previously registered scattering.
overlapping_layout< T > pl
nodal parallel layout
void free_scatterings()
Free the registered scatterings.
virtual T lsize() const =0
virtual ~abstract_vector()=default
virtual void operator*=(const S sca)=0
#define SF_MPITAG
the MPI tag when communicating
virtual void const_release_ptr(const S *&p) const =0
size_t read_ascii(FILE *fd)
Simple utility functions for parallel data.
ltype layout
used vector layout (nodal, algebraic, unset)
T * data()
Pointer to the vector's start.
size_t write_ascii(const char *file, bool write_header)
virtual const S * const_ptr() const =0
const T * end() const
Pointer to the vector's end.
size_t root_read(FILE *fd, vector< V > &vec, MPI_Comm comm)
Read binary data into a vector.
scattering * register_scattering(const quadruple< int > spec, const vector< T > &layout_a, const vector< T > &layout_b, const vector< T > &idx_a, const vector< T > &idx_b, const int rank, const int dpn)
Register a scattering.
virtual void operator+=(const abstract_vector< T, S > &vec)=0
virtual std::string to_string() const =0
virtual void init(const meshdata< mesh_int_t, mesh_real_t > &imesh, int idpn, ltype inp_layout)=0
void forward(abstract_vector< T, S > &in, abstract_vector< T, S > &out, bool add=false)
Forward scattering.
virtual S dot(const abstract_vector< T, S > &v) const =0
virtual bool equals(const abstract_vector< T, S > &rhs) const =0
std::tuple< int, int > init_common(const meshdata< mesh_int_t, mesh_real_t > &imesh, int idpn, ltype inp_layout)
size_t write_binary(FILE *fd)
Write a vector to HD in binary. File descriptor is already set up.
size_t read_binary(FILE *fd)
size_t read_ascii(std::string file)
virtual void apply_scattering(scattering &sc, bool fwd)=0
virtual bool is_init() const =0
size_t write_binary(std::string file)
write binary. Open file descriptor myself.
hm_int count(const K &key) const
Check if key exists.
size_t size() const
The current size of the vector.
size_t num_global_idx() const
Retrieve the global number of indices.
virtual void shallow_copy(const abstract_vector< T, S > &v)=0
size_t l_numpts
local number of points
virtual void operator=(const vector< S > &rhs)=0
A vector storing arbitrary data.
virtual void get_ownership_range(T &start, T &stop) const =0
size_t l_numelem
local number of elements
size_t read_binary(std::string file)
const T * begin() const
Pointer to the vector's start.
Classes and algorithms related to the layout of distributed meshes.
const meshdata< mesh_int_t, mesh_real_t > * mesh
the connected mesh
virtual void backward(abstract_vector< T, S > &out, scattering &sc, bool add=false)=0
size_t num_algebraic_idx() const
Retrieve the number of local algebraic indices.
size_t g_numelem
global number of elements
void treat_file_open_error(const char *file, const char *caller, const int errnum, const bool do_exit, int rank)
treat a file open error by displaying the errnum string interpretation and the caller ...
size_t root_write(FILE *fd, const vector< V > &vec, MPI_Comm comm)
Write vector data binary to disk.
virtual void deep_copy(const abstract_vector< T, S > &v)=0
void resize(size_t n)
Resize a vector.
virtual void operator/=(const S sca)=0
Classes similar to unordered_set and unordered_map, but with better performance.
void backward(abstract_vector< T, S > &in, abstract_vector< T, S > &out, bool add=false)
Backward scattering.
The scatterer registry class.
virtual T gsize() const =0
Vec b_buff
A buffer vector which also defines the parallel layout of the "b" side.
Container for a PETSc VecScatter.