SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DenseMatrixMultiplication.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2009 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 ///@brief
30 ///@file DenseMatrixMultiplication.h
31 
32 #ifndef CORE_DATATYPES_DENSEMATRIXMULTIPLICATION_H
33 #define CORE_DATATYPES_DENSEMATRIXMULTIPLICATION_H
34 
35 #if defined(HAVE_CBLAS)
36 #if defined(__APPLE__)
37 #include <vecLib/cblas.h>
38 #else
39 extern "C"{
40 #include <cblas.h>
41 }
42 #endif
43 #endif
44 
45 namespace SCIRun {
46 
47 template <typename T>
48 void
50  index_type beg, index_type end,
51  int spVec) const
52 {
53  ASSERTEQ(x.nrows(), this->ncols_);
54  ASSERTEQ(b.nrows(), this->nrows_);
55 
56  if (beg == -1) beg = 0;
57  if (end == -1) end = this->nrows_;
58 
59 #if defined(HAVE_CBLAS)
60  double ALPHA = 1.0;
61  double BETA = 0.0;
62  cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, (end-beg),
63  1, this->ncols_, ALPHA, dataptr_+(beg*this->ncols_), this->ncols_,
64  x.get_data_pointer(), 1, BETA,
65  b.get_data_pointer()+beg, 1);
66 #else
67 
68  double* xdata = x.get_data_pointer();
69  double* bdata = b.get_data_pointer();
70 
71  size_type m8 = (this->ncols_)/8;
72  size_type m = (this->ncols_)-m8*8;
73 
74  index_type i, j;
75  if(!spVec)
76  {
77  for (i=beg; i<end; i++)
78  {
79  double sum=0;
80  double* row=data[i];
81  double* xd=xdata;
82 
83  for (j=0; j<m8; j++)
84  {
85  sum+=(*row)*(*xd);
86  row++; xd++;
87  sum+=(*row)*(*xd);
88  row++; xd++;
89  sum+=(*row)*(*xd);
90  row++; xd++;
91  sum+=(*row)*(*xd);
92  row++; xd++;
93  sum+=(*row)*(*xd);
94  row++; xd++;
95  sum+=(*row)*(*xd);
96  row++; xd++;
97  sum+=(*row)*(*xd);
98  row++; xd++;
99  sum+=(*row)*(*xd);
100  row++; xd++;
101  }
102 
103  for (j=0; j<m; j++)
104  {
105  sum+=(*row)*(*xd);
106  row++; xd++;
107  }
108  (*bdata)=sum;
109  bdata++;
110  }
111  }
112  else
113  {
114  for (i=beg; i<end; i++) b[i]=0;
115  for (j=0; j<this->ncols_; j++)
116  {
117  if (x[j])
118  for (i=beg; i<end; i++)
119  {
120  b[i]+=data[i][j]*x[j];
121  }
122  }
123  }
124 
125 #endif
126 }
127 
128 template <typename T>
129 void
131 {
132  index_type i, j;
133 
134  double* xdata = x.get_data_pointer();
135  double* bdata = b.get_data_pointer();
136 
137  size_type m8 = (this->ncols_)/8;
138  size_type m = (this->ncols_)-m8*8;
139 
140  for (i=0; i<this->nrows_; i++)
141  {
142  double sum=0;
143  double* row = data[i];
144  double* xd = xdata;
145  for (j=0; j<m8; j++)
146  {
147  sum+=(*row)*(*xd);
148  row++; xd++;
149  sum+=(*row)*(*xd);
150  row++; xd++;
151  sum+=(*row)*(*xd);
152  row++; xd++;
153  sum+=(*row)*(*xd);
154  row++; xd++;
155  sum+=(*row)*(*xd);
156  row++; xd++;
157  sum+=(*row)*(*xd);
158  row++; xd++;
159  sum+=(*row)*(*xd);
160  row++; xd++;
161  sum+=(*row)*(*xd);
162  row++; xd++;
163  }
164 
165  for (j=0; j<m; j++)
166  {
167  sum+=(*row)*(*xd);
168  row++; xd++;
169  }
170 
171  *bdata=sum;
172  bdata++;
173  }
174 }
175 
176 template <typename T>
177 void
179  index_type beg, index_type end,
180  int spVec) const
181 {
182  // Compute At*x=b
183  ASSERT(x.nrows() == this->nrows_);
184  ASSERT(b.nrows() == this->ncols_);
185  if (beg == -1) beg = 0;
186  if (end == -1) end = this->ncols_;
187  index_type i, j;
188  if (!spVec)
189  {
190  for (i=beg; i<end; i++)
191  {
192  double sum=0;
193  for (j=0; j<this->nrows_; j++)
194  {
195  sum+=data[j][i]*x[j];
196  }
197  b[i]=sum;
198  }
199  }
200  else
201  {
202  for (i=beg; i<end; i++) b[i]=0;
203  for (j=0; j<this->nrows_; j++)
204  if (x[j])
205  {
206  double *row=data[j];
207  for (i=beg; i<end; i++)
208  b[i]+=row[i]*x[j];
209  }
210  }
211 }
212 
213 template <typename T>
216 {
217  DenseMatrix* result = zero_matrix(rows, cols);
218  for (size_type i = 0; i < column.nrows(); ++i)
219  (*result)[i][i] = column[i];
220 
221  return result;
222 }
223 
224 } // End namespace SCIRun
225 
226 #endif
Definition: DenseMatrix.h:68
#define ASSERTEQ(c1, c2)
Definition: Assert.h:98
virtual void mult(const ColumnMatrix &x, ColumnMatrix &b, index_type beg=-1, index_type end=-1, int spVec=0) const
Definition: DenseMatrixMultiplication.h:49
static DenseMatrix * make_diagonal_from_column(const ColumnMatrix &column, size_type rows, size_type cols)
Definition: DenseMatrixMultiplication.h:215
#define ASSERT(condition)
Definition: Assert.h:110
long long size_type
Definition: Types.h:40
dictionary data
Definition: eabLatVolData.py:11
Definition: ColumnMatrix.h:55
virtual T * get_data_pointer() const
Definition: ColumnMatrix.h:200
virtual void mult_transpose(const ColumnMatrix &x, ColumnMatrix &b, index_type beg=-1, index_type end=-1, int spVec=0) const
Definition: DenseMatrixMultiplication.h:178
long long index_type
Definition: Types.h:39
size_type nrows() const
Definition: Matrix.h:132
void multiply(ColumnMatrix &x, ColumnMatrix &b) const
Definition: DenseMatrixMultiplication.h:130