30#ifndef _FULLY_DIST_SP_VEC_H_
31#define _FULLY_DIST_SP_VEC_H_
46template <
class IT,
class NT,
class DER>
52template <
class IU,
class NU>
55template <
class IU,
class NU>
72template <
class IT,
class NT>
73class FullyDistSpVec:
public FullyDist<IT,NT,typename combblas::disable_if< combblas::is_boolean<NT>::value, NT >::type>
77 explicit FullyDistSpVec (
IT glen );
78 FullyDistSpVec ( std::shared_ptr<CommGrid> grid);
79 FullyDistSpVec ( std::shared_ptr<CommGrid> grid,
IT glen);
81 template <
typename _UnaryOperation>
82 FullyDistSpVec (
const FullyDistVec<IT,NT> & rhs, _UnaryOperation unop);
83 FullyDistSpVec (
const FullyDistVec<IT,NT> & rhs);
84 FullyDistSpVec (
IT globalsize,
const FullyDistVec<IT,IT> & inds,
const FullyDistVec<IT,NT> & vals,
bool SumDuplicates =
false);
85 FullyDistSpVec (std::shared_ptr<CommGrid> grid,
IT globallen,
const std::vector<IT>& indvec,
const std::vector<NT> & numvec,
bool SumDuplicates =
false,
bool sorted=
false);
89 FullyDistSpVec<IT,NT> Invert (
IT globallen);
90 template <
typename _BinaryOperationIdx,
typename _BinaryOperationVal,
typename _BinaryOperationDuplicate>
91 FullyDistSpVec<IT,NT> Invert (
IT globallen, _BinaryOperationIdx __binopIdx, _BinaryOperationVal __binopVal, _BinaryOperationDuplicate __binopDuplicate);
92 template <
typename _BinaryOperationIdx,
typename _BinaryOperationVal>
93 FullyDistSpVec<IT,NT> InvertRMA (
IT globallen, _BinaryOperationIdx __binopIdx, _BinaryOperationVal __binopVal);
96 template <
typename NT1,
typename _UnaryOperation>
97 void Select (
const FullyDistVec<IT,NT1> & denseVec, _UnaryOperation unop);
98 template <
typename _UnaryOperation>
99 void FilterByVal (FullyDistSpVec<IT,IT> Selector, _UnaryOperation __unop,
bool filterByIndex);
100 template <
typename NT1>
101 void Setminus (
const FullyDistSpVec<IT,NT1> & other);
106 template <
typename NT1,
typename _UnaryOperation,
typename _BinaryOperation>
107 void SelectApply (
const FullyDistVec<IT,NT1> & denseVec, _UnaryOperation __unop, _BinaryOperation __binop);
113 void stealFrom(FullyDistSpVec<IT,NT> & victim);
114 FullyDistSpVec<IT,NT> & operator=(
const FullyDistSpVec< IT,NT > & rhs);
115 FullyDistSpVec<IT,NT> & operator=(
const FullyDistVec< IT,NT > & rhs);
116 FullyDistSpVec<IT,NT> & operator=(
NT fixedval)
119#pragma omp parallel for
121 for(
size_t i=0; i < ind.size(); ++i)
125 FullyDistSpVec<IT,NT> & operator+=(
const FullyDistSpVec<IT,NT> & rhs);
126 FullyDistSpVec<IT,NT> & operator-=(
const FullyDistSpVec<IT,NT> & rhs);
128 class ScalarReadSaveHandler
131 NT getNoNum(
IT index) {
return static_cast<NT>(1); }
133 template <
typename c,
typename t>
134 NT read(std::basic_istream<c,t>& is,
IT index)
141 template <
typename c,
typename t>
142 void save(std::basic_ostream<c,t>& os,
const NT& v,
IT index)
148 template <
class HANDLER>
149 void ParallelWrite(
const std::string & filename,
bool onebased, HANDLER handler,
bool includeindices =
true,
bool includeheader =
false);
150 void ParallelWrite(
const std::string & filename,
bool onebased,
bool includeindices =
true) { ParallelWrite(filename, onebased, ScalarReadSaveHandler(), includeindices); };
153 template <
typename _BinaryOperation>
154 void ParallelRead (
const std::string & filename,
bool onebased, _BinaryOperation BinOp);
158 template <
class HANDLER>
159 std::ifstream& ReadDistribute (std::ifstream& infile,
int master, HANDLER handler);
160 std::ifstream& ReadDistribute (std::ifstream& infile,
int master) {
return ReadDistribute(infile, master, ScalarReadSaveHandler()); }
162 template <
class HANDLER>
163 void SaveGathered(std::ofstream& outfile,
int master, HANDLER handler,
bool printProcSplits =
false);
164 void SaveGathered(std::ofstream& outfile,
int master) { SaveGathered(outfile, master, ScalarReadSaveHandler()); }
167 template <
typename NNT>
operator FullyDistSpVec< IT,NNT > () const
169 FullyDistSpVec<IT,NNT> CVT(commGrid);
170 CVT.ind = std::vector<IT>(ind.begin(), ind.end());
171 CVT.num = std::vector<NNT>(num.begin(), num.end());
176 bool operator==(
const FullyDistSpVec<IT,NT> & rhs)
const
178 FullyDistVec<IT,NT> v = *
this;
179 FullyDistVec<IT,NT> w = rhs;
183 void PrintInfo(std::string vecname)
const;
185 void nziota(
NT first);
186 FullyDistVec<IT,NT> operator() (
const FullyDistVec<IT,IT> & ri)
const;
187 void SetElement (
IT indx,
NT numx);
188 void DelElement (
IT indx);
189 NT operator[](
IT indx);
190 bool WasFound()
const {
return wasFound; }
193 FullyDistSpVec<IT, IT> sort();
195#if __cplusplus > 199711L
196 template <
typename _BinaryOperation = minimum<NT> >
197 FullyDistSpVec<IT, NT> Uniq(_BinaryOperation __binary_op = _BinaryOperation(), MPI_Op mympiop = MPI_MIN);
199 template <
typename _BinaryOperation >
200 FullyDistSpVec<IT, NT> Uniq(_BinaryOperation __binary_op, MPI_Op mympiop);
204 template <
typename _UnaryOperation>
205 FullyDistSpVec<IT,NT> Prune(_UnaryOperation __unary_op,
bool inPlace =
true)
207 FullyDistSpVec<IT,NT> temp(commGrid);
208 IT spsize = ind.size();
209 for(
IT i=0; i< spsize; ++i)
211 if(!(__unary_op(num[i])))
213 temp.ind.push_back(ind[i]);
214 temp.num.push_back(num[i]);
223 return FullyDistSpVec<IT,NT>(commGrid);
239 IT locnnz = ind.size();
240 MPI_Allreduce( &locnnz, &totnnz, 1,
MPIType<IT>(), MPI_SUM, commGrid->GetWorld());
243 using FullyDist<
IT,
NT,
typename combblas::disable_if< combblas::is_boolean<NT>::value,
NT >::type>::LengthUntil;
244 using FullyDist<
IT,
NT,
typename combblas::disable_if< combblas::is_boolean<NT>::value,
NT >::type>::MyLocLength;
245 using FullyDist<
IT,
NT,
typename combblas::disable_if< combblas::is_boolean<NT>::value,
NT >::type>::MyRowLength;
246 using FullyDist<
IT,
NT,
typename combblas::disable_if< combblas::is_boolean<NT>::value,
NT >::type>::TotalLength;
247 using FullyDist<
IT,
NT,
typename combblas::disable_if< combblas::is_boolean<NT>::value,
NT >::type>::Owner;
248 using FullyDist<
IT,
NT,
typename combblas::disable_if< combblas::is_boolean<NT>::value,
NT >::type>::RowLenUntil;
252 IT offset = LengthUntil();
253 IT spsize = ind.size();
255 #pragma omp parallel for
257 for(
IT i=0; i< spsize; ++i)
258 num[i] = ind[i] + offset;
261 template <
typename _Predicate>
262 IT Count(_Predicate pred)
const;
264 template <
typename _UnaryOperation>
265 void Apply(_UnaryOperation __unary_op)
268 IT spsize = num.size();
270#pragma omp parallel for
272 for(
IT i=0; i < spsize; ++i)
273 num[i] = __unary_op(num[i]);
276 template <
typename _BinaryOperation>
277 void ApplyInd(_BinaryOperation __binary_op)
279 IT offset = LengthUntil();
280 IT spsize = ind.size();
282 #pragma omp parallel for
284 for(
IT i=0; i < spsize; ++i)
285 num[i] = __binary_op(num[i], ind[i] + offset);
290 template <
typename _BinaryOperation>
291 NT Reduce(_BinaryOperation __binary_op,
NT init)
const;
293 template <
typename OUT,
typename _BinaryOperation,
typename _UnaryOperation>
294 OUT Reduce(_BinaryOperation __binary_op, OUT default_val, _UnaryOperation __unary_op)
const;
297 std::shared_ptr<CommGrid> getcommgrid()
const {
return commGrid; }
300 NT GetLocalElement(
IT indx);
301 void BulkSet(
IT inds[],
int count);
302 std::vector<IT> GetLocalInd (){std::vector<IT> rind = ind;
return rind;};
303 std::vector<NT> GetLocalNum (){std::vector<NT> rnum = num;
return rnum;};
305 template <
typename _Predicate>
306 FullyDistVec<IT,IT> FindInds(_Predicate pred)
const;
307 template <
typename _Predicate>
308 FullyDistVec<IT,NT> FindVals(_Predicate pred)
const;
312 using FullyDist<
IT,
NT,
typename combblas::disable_if< combblas::is_boolean<NT>::value,
NT >::type>::glen;
313 using FullyDist<
IT,
NT,
typename combblas::disable_if< combblas::is_boolean<NT>::value,
NT >::type>::commGrid;
316 std::vector< IT > ind;
317 std::vector< NT > num;
321 template <
typename _BinaryOperation>
322 void SparseCommon(std::vector< std::vector < std::pair<IT,NT> > > & data, _BinaryOperation BinOp);
325#if __cplusplus > 199711L
326 template <
typename _BinaryOperation = minimum<NT> >
327 FullyDistSpVec<IT, NT> UniqAll2All(_BinaryOperation __binary_op = _BinaryOperation(), MPI_Op mympiop = MPI_MIN);
329 template <
typename _BinaryOperation >
330 FullyDistSpVec<IT, NT> UniqAll2All(_BinaryOperation __binary_op, MPI_Op mympiop);
334 template <
class IU,
class NU>
335 friend class FullyDistSpVec;
337 template <
class IU,
class NU>
338 friend class FullyDistVec;
340 template <
class IU,
class NU,
class UDER>
341 friend class SpParMat;
343 template <
class IU,
class NU>
344 friend class SparseVectorLocalIterator;
346 template <
typename SR,
typename IU,
typename NUM,
typename NUV,
typename UDER>
347 friend FullyDistSpVec<IU,typename promote_trait<NUM,NUV>::T_promote>
348 SpMV (
const SpParMat<IU,NUM,UDER> &
A,
const FullyDistSpVec<IU,NUV> & x );
350 template <
typename SR,
typename IU,
typename NUM,
typename UDER>
351 friend FullyDistSpVec<IU,typename promote_trait<NUM,IU>::T_promote>
352 SpMV (
const SpParMat<IU,NUM,UDER> &
A,
const FullyDistSpVec<IU,IU> & x,
bool indexisvalue);
354 template <
typename VT,
typename IU,
typename UDER>
355 friend FullyDistSpVec<IU,VT>
SpMV (
const SpParMat<IU,bool,UDER> &
A,
const FullyDistSpVec<IU,VT> & x, OptBuf<int32_t, VT > & optbuf);
357 template <
typename SR,
typename IVT,
typename OVT,
typename IU,
typename NUM,
typename UDER>
358 friend void SpMV (
const SpParMat<IU,NUM,UDER> &
A,
const FullyDistSpVec<IU,IVT> & x, FullyDistSpVec<IU,OVT> & y,
bool indexisvalue, OptBuf<int32_t, OVT > & optbuf);
360 template <
typename SR,
typename IVT,
typename OVT,
typename IU,
typename NUM,
typename UDER>
361 friend void SpMV (
const SpParMat<IU,NUM,UDER> &
A,
const FullyDistSpVec<IU,IVT> & x, FullyDistSpVec<IU,OVT> & y,
bool indexisvalue, OptBuf<int32_t, OVT > & optbuf, PreAllocatedSPA<OVT> & SPA);
363 template <
typename IU,
typename NU1,
typename NU2>
364 friend FullyDistSpVec<IU,typename promote_trait<NU1,NU2>::T_promote>
365 EWiseMult (
const FullyDistSpVec<IU,NU1> & V,
const FullyDistVec<IU,NU2> & W ,
bool exclude, NU2 zero);
367 template <
typename RET,
typename IU,
typename NU1,
typename NU2,
typename _BinaryOperation,
typename _BinaryPredicate>
368 friend FullyDistSpVec<IU,RET>
369 EWiseApply (
const FullyDistSpVec<IU,NU1> & V,
const FullyDistVec<IU,NU2> & W , _BinaryOperation _binary_op, _BinaryPredicate _doOp,
bool allowVNulls, NU1 Vzero,
const bool useExtendedBinOp);
371 template <
typename RET,
typename IU,
typename NU1,
typename NU2,
typename _BinaryOperation,
typename _BinaryPredicate>
372 friend FullyDistSpVec<IU,RET>
373 EWiseApply_threaded (
const FullyDistSpVec<IU,NU1> & V,
const FullyDistVec<IU,NU2> & W , _BinaryOperation _binary_op, _BinaryPredicate _doOp,
bool allowVNulls, NU1 Vzero,
const bool useExtendedBinOp);
376 template <
typename RET,
typename IU,
typename NU1,
typename NU2,
typename _BinaryOperation,
typename _BinaryPredicate>
377 friend FullyDistSpVec<IU,RET>
378 EWiseApply (
const FullyDistSpVec<IU,NU1> & V,
const FullyDistSpVec<IU,NU2> & W , _BinaryOperation _binary_op, _BinaryPredicate _doOp,
bool allowVNulls,
bool allowWNulls, NU1 Vzero, NU2 Wzero,
const bool allowIntersect,
const bool useExtendedBinOp);
380 template <
typename IU>
381 friend void RandPerm(FullyDistSpVec<IU,IU> & V);
383 template <
typename IU>
391 template <
typename IU,
typename VT>
392 friend void MergeContributions(FullyDistSpVec<IU,VT> & y,
int * & recvcnt,
int * & rdispls,
int32_t * & recvindbuf, VT * & recvnumbuf,
int rowneighs);
394 template<
typename IU,
typename NV>
395 friend void TransposeVector(MPI_Comm & World,
const FullyDistSpVec<IU,NV> & x,
int32_t & trxlocnz, IU & lenuntil,
int32_t * & trxinds, NV * & trxnums,
bool indexisvalue);
397 template <
class IU,
class NU,
class DER,
typename _UnaryOperation>
398 friend SpParMat<IU, bool, DER> PermMat1 (
const FullyDistSpVec<IU,NU> & ri,
const IU ncol, _UnaryOperation __unop);
void iota(_ForwardIter __first, _ForwardIter __last, T __value)
void MergeContributions(FullyDistSpVec< IU, VT > &y, int *&recvcnt, int *&rdispls, int32_t *&recvindbuf, VT *&recvnumbuf, int rowneighs)
void RenameVertices(DistEdgeList< IU > &DEL)
FullyDistSpVec< IU, RET > EWiseApply_threaded(const FullyDistSpVec< IU, NU1 > &V, const FullyDistVec< IU, NU2 > &W, _BinaryOperation _binary_op, _BinaryPredicate _doOp, bool allowVNulls, NU1 Vzero, const bool useExtendedBinOp)
MPI_Datatype MPIType(void)
Dcsc< IU, N_promote > EWiseApply(const Dcsc< IU, NU1 > &A, const Dcsc< IU, NU2 > *B, _BinaryOperation __binary_op, bool notB, const NU2 &defaultBVal)
void TransposeVector(MPI_Comm &World, const FullyDistSpVec< IU, NV > &x, int32_t &trxlocnz, IU &lenuntil, int32_t *&trxinds, NV *&trxnums, bool indexisvalue)
Dcsc< IU, typename promote_trait< NU1, NU2 >::T_promote > EWiseMult(const Dcsc< IU, NU1 > &A, const Dcsc< IU, NU2 > *B, bool exclude)