Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
axpy.C
Go to the documentation of this file.
1 /* *******************************************************************
2  * Rocstar Simulation Suite *
3  * Copyright@2015, Illinois Rocstar LLC. All rights reserved. *
4  * *
5  * Illinois Rocstar LLC *
6  * Champaign, IL *
7  * www.illinoisrocstar.com *
8  * sales@illinoisrocstar.com *
9  * *
10  * License: See LICENSE file in top level of distribution package or *
11  * http://opensource.org/licenses/NCSA *
12  *********************************************************************/
13 /* *******************************************************************
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES *
16  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *
17  * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR *
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
21  * USE OR OTHER DEALINGS WITH THE SOFTWARE. *
22  *********************************************************************/
23 #include "Rocblas.h"
24 
25 // Performs the operation: z = a*x op y
26 template <class data_type, int atype>
27 void Rocblas::axpy_gen( const void *ain, const Attribute *x,
28  const Attribute *y, Attribute *z) {
29  COM_assertion_msg(!x->is_windowed() && !y->is_windowed() && !z->is_windowed(),
30  "Unsupported attribute type");
32  (std::string("Incompatible data types between ")+
33  x->fullname()+" and "+y->fullname()).c_str());
35  (std::string("Incompatible data types between ")+
36  x->fullname()+" and "+z->fullname()).c_str());
37 
38  int num_dims = z->size_of_components();
39  COM_assertion_msg(num_dims == x->size_of_components() &&
40  num_dims == y->size_of_components(),
41  (std::string("Numbers of components do not match between ")+
42  x->fullname()+" and "+y->fullname()).c_str());
43 
44  std::vector<const Pane*> apanes, xpanes, ypanes;
45  std::vector<Pane*> zpanes;
46 
47  x->window()->panes(xpanes);
48  y->window()->panes(ypanes);
49  z->window()->panes(zpanes);
50 
51  COM_assertion_msg( xpanes.size() == ypanes.size(),
52  (std::string("Numbers of panes do not match between ")+
53  x->window()->name()+" and "+y->window()->name()).c_str());
54  COM_assertion_msg( xpanes.size() == zpanes.size(),
55  (std::string("Numbers of panes do not match between ")+
56  x->window()->name()+" and "+z->window()->name()).c_str());
57 
58  std::vector<Pane*>::iterator zit, zend;
59  std::vector<const Pane*>::const_iterator xit, yit;
60 
61  const Pane** ait=NULL;
62  const Attribute *a=NULL;
63  const data_type *aval=NULL;
64 
65  if ( atype == BLAS_VOID)
66  aval = reinterpret_cast<const data_type *>( ain);
67  else {
68  a = reinterpret_cast<const Attribute*>(ain);
69 
70  if ( !a->is_windowed()) {
71  a->window()->panes( apanes);
72  COM_assertion_msg(xpanes.size() == ypanes.size(),
73  (std::string("Numbers of panes do not match between ")+
74  x->window()->name()+" and "+y->window()->name()).c_str());
75  ait = &apanes[0];
76  }
77  else {
78  COM_assertion_msg( a->size_of_items()==1, "Size of items do not match");
79  aval = reinterpret_cast<const data_type *>( a->pointer());
80 
81  COM_assertion_msg( aval, (std::string("Caught NULL pointer in ")+
82  a->fullname()).c_str());
83  }
84  }
85 
86  const int anum_dims = atype!=BLAS_VOID ? a->size_of_components() : 0;
87  COM_assertion_msg( atype==BLAS_VOID || (anum_dims==1 || anum_dims==num_dims),
88  (std::string("Numbers of components do not match between ")+
89  a->fullname()+" and "+z->fullname()).c_str());
90 
91  for(zit=zpanes.begin(),zend=zpanes.end(),xit=xpanes.begin(),yit=ypanes.begin();
92  zit != zend; ++zit, ait+=(atype!=BLAS_VOID&&ait), ++xit, ++yit) {
93  Attribute *pz = (*zit)->attribute(z->id());
94  const int length = pz->size_of_items();
95  int zstrd=get_stride<BLAS_VEC2D>(pz);
96  const bool zstg = num_dims!=zstrd;
97 
98  const Attribute *px = (*xit)->attribute(x->id());
99  int xstrd = get_stride<BLAS_VEC2D>(px);
100  COM_assertion_msg(length == px->size_of_items() || xstrd==0,
101  (std::string("Numbers of items do not match between ")+
102  x->fullname()+" and "+z->fullname()+
103  " on pane "+to_str((*zit)->id())).c_str());
104  const bool xstg = num_dims!=xstrd || xstrd==0;
105 
106  const Attribute *py = (*yit)->attribute(y->id());
107  int ystrd = get_stride<BLAS_VEC2D>(py);
108  COM_assertion_msg(length == py->size_of_items() || ystrd==0,
109  (std::string("Numbers of items do not match between ")+
110  y->fullname()+" and "+z->fullname()+
111  " on pane "+to_str((*zit)->id())).c_str());
112 
113  const bool ystg = num_dims!=ystrd || ystrd==0;
114 
115  const Attribute *pa = (atype!=BLAS_VOID&&ait)?(*ait)->attribute(a->id()):a;
116  int astrd = get_stride<atype>(pa);
117  const bool astg = pa && (anum_dims!=num_dims || anum_dims!=astrd);
118  COM_assertion_msg( atype!=BLAS_SCNE && atype!=BLAS_VEC2D ||
119  length == int(pa->size_of_items()) || astrd==0,
120  (std::string("Numbers of items do not match between ")+
121  a->fullname()+" and "+z->fullname()+
122  " on pane "+to_str((*zit)->id())).c_str());
123 
124  // Optimized version for contiguous attributes
125  if ( !xstg && !zstg && !ystg && !astg && atype != BLAS_VEC
126  && (atype != BLAS_SCNE || num_dims==1)) {
127  const data_type *xval = (const data_type *)px->pointer();
128  const data_type *yval = (const data_type *)py->pointer();
129  data_type *zval = (data_type *)pz->pointer();
130 
131  // Get address for a if a is not window attribute
132  if ( atype != BLAS_VOID && ait)
133  aval = reinterpret_cast<const data_type *>(pa->pointer());
134 
135  //Loop for each element/node and for each dimension
136  for(Size i = 0, s = length*num_dims; i<s; ++i, ++zval, ++xval, ++yval)
137  *zval = getref<data_type,atype,0>(aval,i,0,1)* *xval+ *yval;
138  }
139  else { // General version
140  //Loop for each dimension.
141  for(int i = 0; i < num_dims; ++i) {
142  Attribute *pz_i = num_dims==1?pz:(*zit)->attribute(z->id()+i+1);
143  data_type *zval = (data_type *)pz_i->pointer();
144  zstrd=get_stride<BLAS_VEC2D>(pz_i);
145 
146  const Attribute *px_i = num_dims==1?px:(*xit)->attribute(x->id()+i+1);
147  const data_type *xval = (const data_type *)px_i->pointer();
148  xstrd=get_stride<BLAS_VEC2D>(px_i);
149 
150  const Attribute *py_i = num_dims==1?py:(*yit)->attribute(y->id()+i+1);
151  const data_type *yval = (const data_type *)py_i->pointer();
152  ystrd=get_stride<BLAS_VEC2D>(py_i);
153 
154  if ( atype != BLAS_VOID && ait) {
155  const Attribute *pa_i=anum_dims==1?pa:(*ait)->attribute(a->id()+i+1);
156  aval = reinterpret_cast<const data_type *>( pa_i->pointer());
157  astrd = get_stride<atype>( pa_i);
158  }
159 
160  //Loop for each element/node.
161  for(int j=0; j<length; ++j, xval+=xstrd, zval+=zstrd, yval+=ystrd)
162  *zval = getref<data_type,atype,1>(aval,j,i,astrd) * *xval + *yval;
163  }
164  } // end if
165  } // end for
166 }
167 
168 
169 //Operation wrapper for z = ax + y.
170 void Rocblas::axpy( const Attribute *a, const Attribute *x, const Attribute *y,
171  Attribute *z) {
172  COM_Type att_type = z->data_type();
173  const int ancomp = a->size_of_components();
174 
175  if(att_type == COM_INT || att_type == COM_INTEGER) {
176  if ( a->is_windowed()) {
177  if ( ancomp == 1)
178  axpy_gen<int,BLAS_SCALAR>(a, x, y, z);
179  else
180  axpy_gen<int,BLAS_VEC>(a, x, y, z);
181  }
182  else {
183  if ( ancomp == 1)
184  axpy_gen<int,BLAS_SCNE>(a, x, y, z);
185  else
186  axpy_gen<int,BLAS_VEC2D>(a, x, y, z);
187  }
188  }
189  else {
190  COM_assertion_msg( att_type==COM_DOUBLE || att_type==COM_DOUBLE_PRECISION,
191  (std::string("Unsupported data type in ")+
192  z->fullname()).c_str());
193 
194  if ( a->is_windowed()) {
195  if ( ancomp == 1)
196  axpy_gen<double,BLAS_SCALAR>(a, x, y, z);
197  else
198  axpy_gen<double,BLAS_VEC>(a, x, y, z);
199  }
200  else {
201  if ( ancomp == 1)
202  axpy_gen<double,BLAS_SCNE>(a, x, y, z);
203  else
204  axpy_gen<double,BLAS_VEC2D>(a, x, y, z);
205  }
206  }
207 }
208 
209 //Operation wrapper for z = ax + y (a is a scalar pointer).
210 void Rocblas::axpy_scalar( const void *a, const Attribute *x, const Attribute *y,
211  Attribute *z) {
212  COM_Type att_type = z->data_type();
213 
214  if(att_type == COM_INT || att_type == COM_INTEGER)
215  axpy_gen<int,BLAS_VOID>(a, x, y, z);
216  else if (att_type == COM_DOUBLE || att_type == COM_DOUBLE_PRECISION)
217  axpy_gen<double,BLAS_VOID>(a, x, y, z);
218 }
219 
220 
221 
222 
223 
224 
int COM_Type
Indices for derived data types.
Definition: roccom_basic.h:122
A Pane object contains a mesh, pane attribute, and field variables.
Definition: Pane.h:43
An Attribute object is a data member of a window.
Definition: Attribute.h:51
void int int REAL REAL * y
Definition: read.cpp:74
#define COM_assertion_msg(EX, msg)
double s
Definition: blastest.C:80
bool is_windowed() const
Checks whether the attribute is associated with the window.
Definition: Attribute.h:188
const std::string & name() const
Obtain the window&#39;s name.
Definition: Window.h:92
const Window * window() const
Obtain a constant pointer to the parent window of the attribute.
Definition: Attribute.C:80
double length(Vector3D *const v, int n)
static void axpy(const Attribute *a, const Attribute *x, const Attribute *y, Attribute *z)
Operation wrapper for z = a * x + y.
Definition: axpy.C:170
const void * pointer() const
Obtain a constant pointer to the physical address.
Definition: Attribute.h:150
void int int int REAL REAL REAL * z
Definition: write.cpp:76
static void axpy_scalar(const void *a, const Attribute *x, const Attribute *y, Attribute *z)
Operation wrapper for z = a * x + y (a is a scalar pointer).
Definition: axpy.C:210
int COM_compatible_types(COM_Type type1, COM_Type type2)
Definition: roccom_c++.h:563
blockLoc i
Definition: read.cpp:79
void int int REAL * x
Definition: read.cpp:74
Definition for Rocblas API.
static std::string to_str(int i)
Definition: Rocblas.h:283
unsigned int Size
Definition: Rocblas.h:38
j indices j
Definition: Indexing.h:6
static void axpy_gen(const void *a, const Attribute *x, const Attribute *y, Attribute *z)
Performs the operation: z = a*x + y.
Definition: axpy.C:27
COM_Type data_type() const
Obtain the data type of each component of the attribute.
Definition: Attribute.h:197
int id() const
Obtain the id (or index) of the attribute.
Definition: Attribute.h:120
void panes(std::vector< int > &ps, int rank=-2)
Obtain all the local panes of the window.
Definition: Window.C:809
int size_of_items() const
Obtain the number of items in the attribute.
Definition: Attribute.C:111
std::string fullname() const
Obtain the full name of the attribute including window name suitable for printing out error messages...
Definition: Attribute.C:82
int size_of_components() const
Obtain the number of components in the attribute.
Definition: Attribute.h:203