1 /****************************************************************/
2 /* Parallel Combinatorial BLAS Library (for Graph Computations) */
3 /* version 1.6 -------------------------------------------------*/
4 /* date: 6/15/2017 ---------------------------------------------*/
5 /* authors: Ariful Azad, Aydin Buluc  --------------------------*/
6 /****************************************************************/
7 /*
8  Copyright (c) 2010-2017, The Regents of the University of California
9 
10  Permission is hereby granted, free of charge, to any person obtaining a copy
11  of this software and associated documentation files (the "Software"), to deal
12  in the Software without restriction, including without limitation the rights
13  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14  copies of the Software, and to permit persons to whom the Software is
15  furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included in
18  all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26  THE SOFTWARE.
27  */
28 
29 #ifndef _DENSE_PAR_MAT_H_
30 #define _DENSE_PAR_MAT_H_
31 
32 #include <iostream>
33 #include <fstream>
34 #include <cmath>
35 #include <mpi.h>
36 #include <vector>
37 
38 #include "CombBLAS.h"
39 #include "Operations.h"
40 #include "MPIOp.h"
41 #include "FullyDistVec.h"
42 
43 namespace combblas {
44 
45 template <class IU, class NU, class DER>
46 class SpParMat;
47 
48 template <class IT, class NT>
49 class DenseParMat
50 {
51 public:
52 	// Constructors
DenseParMat()53 	DenseParMat (): array(NULL), m(0), n(0)
54 	{
55 		commGrid.reset(new CommGrid(MPI_COMM_WORLD, 0, 0));
56 	}
DenseParMat(NT value,std::shared_ptr<CommGrid> grid,IT rows,IT cols)57 	DenseParMat (NT value, std::shared_ptr<CommGrid> grid, IT rows, IT cols): m(rows), n(cols)
58 	{
59 		array = SpHelper::allocate2D<double>(rows, cols);
60 		for(int i=0; i< rows; ++i)
61 		{
62 			std::fill_n(array[i], cols, value);		// fill array[i][0] ... array[i][cols] with "value"
63 		}
64 		commGrid.reset(new CommGrid(*grid));
65 	}
DenseParMat(NT ** seqarr,std::shared_ptr<CommGrid> grid,IT rows,IT cols)66 	DenseParMat (NT ** seqarr, std::shared_ptr<CommGrid> grid, IT rows, IT cols): array(seqarr), m(rows), n(cols)
67 	{
68 		commGrid.reset(new CommGrid(*grid));
69 	}
70 
DenseParMat(const DenseParMat<IT,NT> & rhs)71 	DenseParMat (const DenseParMat< IT,NT > & rhs): m(rhs.m), n(rhs.n)		// copy constructor
72 	{
73 		if(rhs.array != NULL)
74 		{
75 			array = SpHelper::allocate2D<NT>(m, n);
76 			for(int i=0; i< m; ++i)
77 			{
78         std::copy(array[i], array[i]+n, rhs.array[i]);
79 			}
80 		}
81 		commGrid.reset(new CommGrid(*(rhs.commGrid)));
82 	}
83 
84 	DenseParMat< IT,NT > &  operator=(const DenseParMat< IT,NT > & rhs);
85 
86 	template <typename DER>
87 	DenseParMat< IT,NT > & operator+=(const SpParMat< IT,NT,DER > & rhs);		// add a sparse matrix
88 
89 	template <typename _BinaryOperation>
90 	FullyDistVec< IT,NT > Reduce(Dim dim, _BinaryOperation __binary_op, NT identity) const;
91 
~DenseParMat()92 	~DenseParMat ()
93 	{
94 		if(array != NULL)
95 			SpHelper::deallocate2D(array, m);
96 	}
97 
getcommgrid()98 	std::shared_ptr<CommGrid> getcommgrid () { return commGrid; }
99 
grows()100    IT grows() const
101     {
102         IT glrows;
103         MPI_Allreduce(&m, &glrows, 1, MPIType<IT>(), MPI_SUM, commGrid->GetColWorld());
104         return glrows;
105     }
106 
gcols()107     IT gcols() const
108     {
109         IT glcols;
110         MPI_Allreduce(&n, &glcols, 1, MPIType<IT>(), MPI_SUM, commGrid->GetRowWorld());
111         return glcols;
112     }
113 
114 private:
115 	std::shared_ptr<CommGrid> commGrid;
116 	NT ** array;
117 	IT m, n;	// Local row and columns
118 
119 	template <class IU, class NU, class DER>
120 	friend class SpParMat;
121 };
122 
123 }
124 
125 #include "DenseParMat.cpp"
126 
127 #endif
128 
129