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);
147 virtual void set(
const vector<T>& idx,
const vector<S>& vals,
const bool additive =
false,
const bool local =
false) = 0;
157 virtual void set(
const vector<T>& idx,
const S val,
const bool additive =
false,
const bool local =
false) = 0;
164 virtual void set(
const S val) = 0;
174 virtual void set(
const T idx,
const S val) = 0;
183 virtual void get(
const vector<T> & idx, S *out) = 0;
192 virtual S
get(
const T idx) = 0;
295 virtual T
lsize()
const = 0;
302 virtual T
gsize()
const = 0;
319 virtual S*
ptr() = 0;
373 virtual S
mag()
const = 0;
380 virtual S
sum()
const = 0;
387 virtual S
min()
const = 0;
403 virtual bool is_init()
const = 0;
410 virtual std::string
to_string()
const = 0;
461 MPI_Comm comm = this->mesh != NULL ? this->mesh->
comm : PETSC_COMM_WORLD;
462 MPI_Comm_rank(comm, &rank);
463 MPI_Comm_size(comm, &size);
465 int glb_size = this->
gsize();
466 int loc_size = this->
lsize();
473 fd = fopen(file,
"w");
477 MPI_Allreduce(MPI_IN_PLACE, &err, 1, MPI_INT, MPI_MAX, comm);
487 fprintf(fd,
"%d\n", glb_size);
489 for(
int i=0; i<loc_size/this->
dpn; i++) {
490 for(
int j=0; j<this->
dpn; j++)
491 nwr += fprintf(fd,
"%f ", p[i*this->dpn+j]);
492 nwr += fprintf(fd,
"\n");
496 for(
int pid=1; pid < size; pid++)
501 MPI_Recv(&rsize, 1, MPI_INT, pid,
SF_MPITAG, comm, &stat);
503 MPI_Recv(wbuff.
data(), rsize*
sizeof(S), MPI_BYTE, pid,
SF_MPITAG, comm, &stat);
505 for(
int i=0; i<rsize/this->
dpn; i++) {
506 for(
int j=0; j<this->
dpn; j++)
507 nwr += fprintf(fd,
"%f ", wbuff[i*this->dpn+j]);
508 nwr += fprintf(fd,
"\n");
514 MPI_Send(&loc_size, 1, MPI_INT, 0,
SF_MPITAG, comm);
515 MPI_Send(p, loc_size*
sizeof(S), MPI_BYTE, 0,
SF_MPITAG, comm);
519 MPI_Bcast(&nwr, 1, MPI_LONG, 0, comm);
535 MPI_Comm comm = this->mesh != NULL ? this->mesh->
comm : PETSC_COMM_WORLD;
536 MPI_Comm_rank(comm, &rank); MPI_Comm_size(comm, &size);
538 long int loc_size = this->
lsize();
542 for(
long int i=0; i<loc_size; i++) buff[i] = p[i];
555 MPI_Comm comm = this->mesh != NULL ? this->mesh->
comm : PETSC_COMM_WORLD;
556 int rank; MPI_Comm_rank(comm, &rank);
562 fd = fopen(file.c_str(),
"w");
567 MPI_Allreduce(MPI_IN_PLACE, &error, 1, MPI_INT, MPI_SUM, comm);
570 nwr = this->write_binary<V>(fd);
583 MPI_Comm comm = this->mesh != NULL ? this->mesh->
comm : PETSC_COMM_WORLD;
585 size_t loc_size = this->
lsize();
591 for(
size_t i=0; i<loc_size; i++)
602 MPI_Comm comm = mesh != NULL ? mesh->
comm : PETSC_COMM_WORLD;
605 int rank; MPI_Comm_rank(comm, &rank);
611 fd = fopen(file.c_str(),
"r");
616 MPI_Allreduce(MPI_IN_PLACE, &error, 1, MPI_INT, MPI_SUM, comm);
619 nrd = read_binary<V>(fd);
631 MPI_Comm comm = this->mesh != NULL ? this->mesh->
comm : PETSC_COMM_WORLD;
633 size_t loc_size = this->
lsize();
643 MPI_Comm comm = this->mesh != NULL ? this->mesh->
comm : PETSC_COMM_WORLD;
644 int rank; MPI_Comm_rank(comm, &rank);
652 fd = fopen(file.c_str(),
"r");
657 MPI_Allreduce(MPI_IN_PLACE, &error, 1, MPI_INT, MPI_SUM, comm);
674 template<
class T,
class S>
707 if(b_buff) VecDestroy(&b_buff);
708 if(vec_sc) VecScatterDestroy(&vec_sc);
716 template<
class T,
class S>
728 template<
class T,
class S>
740 template<
class T,
class S>
778 const size_t gsize_a,
779 const size_t gsize_b,
782 assert(_registry.
count(spec) == 0);
785 _registry[spec] = sc;
794 for(
size_t i=0; i<nbr_a.
size(); i++)
795 for(
short j=0; j<
dpn; j++) idx_a[i*dpn+j] = nbr_a[i]*dpn+j;
797 for(
size_t i=0; i<nbr_b.
size(); i++)
798 for(
short j=0; j<
dpn; j++) idx_b[i*dpn+j] = nbr_b[i]*dpn+j;
800 err += ISCreateGeneral(PETSC_COMM_WORLD, idx_a.size(), idx_a.data(), PETSC_COPY_VALUES, &is_a);
801 err += ISCreateGeneral(PETSC_COMM_WORLD, idx_b.size(), idx_b.data(), PETSC_COPY_VALUES, &is_b);
803 err += ISSetPermutation(is_b);
806 err += VecCreateMPI(PETSC_COMM_WORLD, idx_a.size(), gsize_a*
dpn, &a);
807 err += VecCreateMPI(PETSC_COMM_WORLD, idx_b.size(), gsize_b*
dpn, &sc->b_buff);
809 err += VecScatterCreate(a, is_a, sc->b_buff, is_b, &sc->vec_sc);
811 err += VecDestroy(&a);
812 err += ISDestroy (&is_a);
813 err += ISDestroy (&is_b);
816 fprintf(stderr,
"%s : an error has occurred! Scattering spec: Mesh ID %d, permutation ID %d, numbering %d, dpn %d!\n",
817 __func__,
int(spec.
v1),
int(spec.
v2),
int(spec.
v3),
int(spec.
v4));
819 sc->idx_a.assign(idx_a.begin(), idx_a.end());
820 sc->idx_b.assign(idx_b.begin(), idx_b.end());
841 assert(_registry.
count(spec) == 0);
844 assert(idx_a.
size() == idx_b.
size());
847 _registry[spec] = sc;
855 T lsize_a = layout_a[rank+1] - layout_a[rank];
856 T lsize_b = layout_b[rank+1] - layout_b[rank];
857 T gsize_a = layout_a[layout_a.size()-1];
858 T gsize_b = layout_b[layout_a.size()-1];
862 for(
size_t i=0; i<idx_a.
size(); i++)
863 for(
short j=0; j<
dpn; j++) sidx_a[i*dpn+j] = idx_a[i]*dpn+j;
865 for(
size_t i=0; i<idx_b.
size(); i++)
866 for(
short j=0; j<
dpn; j++) sidx_b[i*dpn+j] = idx_b[i]*dpn+j;
868 err += ISCreateGeneral(PETSC_COMM_WORLD, sidx_a.size(), sidx_a.data(), PETSC_COPY_VALUES, &is_a);
869 err += ISCreateGeneral(PETSC_COMM_WORLD, sidx_b.size(), sidx_b.data(), PETSC_COPY_VALUES, &is_b);
871 err += VecCreateMPI(PETSC_COMM_WORLD, lsize_a*dpn, gsize_a*dpn, &a);
872 err += VecCreateMPI(PETSC_COMM_WORLD, lsize_b*dpn, gsize_b*dpn, &sc->b_buff);
874 err += VecScatterCreate(a, is_a, sc->b_buff, is_b, &sc->vec_sc);
876 err += VecDestroy(&a);
877 err += ISDestroy (&is_a);
878 err += ISDestroy (&is_b);
881 fprintf(stderr,
"%s : an error has occurred! Scattering spec: To ID %d, From ID %d, numbering %d, dpn %d!\n",
882 __func__,
int(spec.
v1),
int(spec.
v2),
int(spec.
v3),
int(spec.
v4));
884 sc->idx_a.assign(idx_a.
begin(), idx_a.
end());
885 sc->idx_b.assign(idx_b.
begin(), idx_b.
end());
905 if(_registry.
count(spec))
906 ret = _registry[spec];
917 for(it = _registry.
begin(); it != _registry.
end(); ++it) {
930 #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
virtual S * device_ptr()=0
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 release_device_ptr(S *&p)=0
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.