Actual source code: taomat_ga.c

  1: //File: taomat_ga.c

  3: /**************************************************************

  5: Author: Limin Zhang, Ph.D.
  6:         Mathematics Department
  7:         Columbia Basin College
  8:         Pasco, WA 99301
  9:         Limin.Zhang@cbc2.org

 11: Mentor: Jarek Naplocha, Ph.D.
 12:         Environmental Molecular Science Laboratory
 13:         Pacific Northwest National Laboratory
 14:         Richland, WA 99352

 16: Date: 7/11/2002

 18: Purpose:
 19:       to design and implement some interfaces between TAO and
 20:       global arrays.

 22: Revise History:

 24: 8/8/02
 25:         To clean Petsc function calls and marcos.
 26: **************************************************************/



 30: #include "taomat_ga.h"
 31: #include "taovec_ga.h"
 32: #include <string>
 33: /*
 34: int MatD_Fischer(Mat, Vec, Vec, Vec, Vec, Vec, Vec, Vec, Vec);
 35: int MatCreateADA(Mat,Vec,Vec,Mat*);
 36: int MatSMFResetRowColumn(Mat,IS,IS);
 37: */

 39: /*@C
 40:    TaoWrapGaMat - Creates a new TaoMat object using a GA matrix.

 42:    Input Parameter:
 43: .  M -  a GA matrix

 45:    Output Parameter:
 46: .  MM - new TaoMat

 48:    Level: advanced

 50: .seealso TaoMatGetGaMat(), TaoMatDestroy()
 51: @*/
 52: int TaoWrapGaMat (GAMat M, TaoMatGa ** MM)
 53: {
 54:   TaoFunctionBegin;
 55:   *MM = new TaoMatGa (M);
 56:   TaoFunctionReturn (0);
 57: }

 59: /*@C
 60:    TaoMatGetGaMat - If the TaoMat is of the TaoMatGa type, this routine
 61:    gets the underlying GA matrix.

 63:    Input Parameter:
 64: .  MM - the TaoMat 

 66:    Output Parameter:
 67: .  M -  the GA mat

 69:    Note:
 70:    The function TaoMatGa::GetMat() will also return the Mat M.

 72:    Level: advanced
 73: @*/
 74: int TaoMatGetGaMat (TaoMat * MM, GAMat * M)
 75: {
 76:   TaoFunctionBegin;
 77:   if (MM)
 78:     {
 79:       *M = ((TaoMatGa *) MM)->GetMat ();
 80:     }
 81:   else
 82:     {
 83:       *M = 0;
 84:     }
 85:   TaoFunctionReturn (0);
 86: }

 88: TaoMatGa::TaoMatGa (GAMat MM):TaoMat ()
 89: {
 90:   this->pm = MM;
 91:   //  this->MatObject = (void *) (&MM);
 92:   this->pm_pre = MM;
 93:   return;
 94: }

 96: int TaoMatGa::Compatible (TaoVec * xx, TaoVec * yy, TaoTruth *flag)
 97: {
 98:   int
 99:     nx,
100:     ny,
101:     m,
102:     n;
103:   GAVec
104:     x,
105:     y;
106:   if (xx == 0 || yy == 0)
107:     { *flag=TAO_FALSE; return 0; }

109:   x = ((TaoVecGa *) xx)->GetVec ();
110:   y = ((TaoVecGa *) yy)->GetVec ();
111:   if (x == 0 || y == 0 || this->pm == 0)
112:     { *flag=TAO_FALSE; return 0; }

114:   int
115:     xtype,
116:     ytype,
117:     type,
118:     ndim,
119:     dims[2];

121:   NGA_Inquire (x, &xtype, &ndim, &nx);
122:   if (ndim != 1)
123:     GA_Error ((char*)"TaoMatGa::Compatible: Wrong dimension", ndim);

125:   NGA_Inquire (y, &ytype, &ndim, &ny);
126:   if (ndim != 1)
127:     GA_Error ((char*)"TaoMatGa::Compatible:Wrong dimension", ndim);

129:   NGA_Inquire (this->pm, &type, &ndim, dims);
130:   if (ndim != 2)
131:     GA_Error ((char*)"TaoMatGa::Compatible:Wrong dimension", ndim);
132:   n = dims[0];
133:   m = dims[1];

135:   if (xtype != type || ytype != type)
136:     GA_Error ((char*)"TaoMatGa::Compatible:Wrong data type", type);

138:   if (n != nx || m != ny)
139:     { *flag=TAO_FALSE; return 0; }

141:   *flag=TAO_TRUE; 
142:   return 0;

144: }

146: int
147: TaoMatGa::Clone (TaoMat ** ntm)
148: {
149:   TaoMatGa *nptm;
150:   GAMat npm;
151:   TaoFunctionBegin;
152:   npm = GA_Duplicate (this->pm, (char*)"Cloned");        
153:   if (!npm)
154:     GA_Error ((char*)"TaoMatGa::Clone:duplicate failed: ", npm);
155:   TaoWrapGaMat (npm, &nptm);
156:   *ntm = nptm;
157:   TaoFunctionReturn (0);
158: }

160: int
161: TaoMatGa::CopyFrom (TaoMat * tm)
162: {
163:   TaoFunctionBegin;
164: #if 0 //try a different way to get GAMat
165:   TaoMatGa *temp2 = dynamic_cast < TaoMatGa * >(tm);
166:   GAMat pm2 = temp2->GetMat ();
167: #endif
168:   GAMat pm2 = ((TaoMatGa *)tm)->GetMat ();
169:   GA_Copy (pm2, this->pm);
170:   TaoFunctionReturn (0);
171: }

173: int
174: TaoMatGa::GetDimensions (int *m, int *n)
175: {
176:   TaoFunctionBegin;
177:   int type, ndim, dims[2];
178:   NGA_Inquire (this->pm, &type, &ndim, dims);
179:   if (ndim != 2)
180:     GA_Error ((char*)"TaoMatGa::GetDimension: wrong dimension", ndim);
181:   else
182:     {
183:       *n = dims[0];
184:       *m = dims[1];
185:     }
186:   TaoFunctionReturn (0);
187: }

189: int
190: TaoMatGa::Multiply (TaoVec * tv, TaoVec * tw)
191: {
192:   double alpha = 1.0;
193:   double beta = 0.0;
194:   TaoTruth flag;
195:   TaoFunctionBegin;
196:   GAVec vv = ((TaoVecGa *) tv)->GetVec ();
197:   GAVec ww = ((TaoVecGa *) tw)->GetVec ();
198:   int m, n = 1, k;                //n = 1 due to vecotor ww that is k by 1 array 
199:   this->GetDimensions (&m, &k);
200:   this->Compatible (tv, tw,&flag);
201:   if (flag)
202:     GA_Dgemm ('N', 'N', m, n, k, alpha, this->pm, vv, beta, ww);
203:   else
204:     TaoFunctionReturn (1);
205:   TaoFunctionReturn (0);
206: }

208: int
209: TaoMatGa::MultiplyAdd (TaoVec * tv, TaoVec * tw, TaoVec * ty)
210: {
211:   TaoTruth flag1,flag2;
212:   TaoFunctionBegin;
213:   GAVec xx = ((TaoVecGa *) tv)->GetVec ();
214:   GAVec ww = ((TaoVecGa *) tw)->GetVec ();
215:   GAVec yy = ((TaoVecGa *) ty)->GetVec ();
216:   GAVec temp_ww = GA_Duplicate (ww, (char*)"temp_WW");
217:   if (!temp_ww)
218:     GA_Error ((char*)"TaoMatGa::MultiplyAdd:Duplicate:", temp_ww);
219:   GA_Copy (ww, temp_ww);
220:   double alpha = 1.0;
221:   double beta = 1.0;
222:   int m, n = 1, k;                //n = 1 due to vecotor ww that is k by 1 array
223:   this->GetDimensions (&m, &k);
224:   this->Compatible (tv, ty, &flag1);
225:   this->Compatible (tv, tw, &flag2);
226:   if (flag1 && flag2)
227:     {
228:       GA_Dgemm ('N', 'N', m, n, k, alpha, this->pm, xx, beta, temp_ww);
229:       //copy from temp_ww to yy. Doing this way, the data in ww is not dirty!!!!!
230:       GA_Copy (temp_ww, yy);
231:       //free the temporary memory
232:       GA_Destroy(temp_ww);
233:     }
234:   else
235:     TaoFunctionReturn (1);
236:   TaoFunctionReturn (0);
237: }

239: int
240: TaoMatGa::MultiplyTranspose (TaoVec * tv, TaoVec * tw)
241: {
242:   double alpha = 1.0;
243:   double beta = 0.0;
244:   int m, n = 1, k;                /*n = 1 due to vecotor ww that is k */
245:   TaoFunctionBegin;
246:   GAVec vv = ((TaoVecGa *) tv)->GetVec ();
247:   GAVec ww = ((TaoVecGa *) tw)->GetVec ();
248:   this->GetDimensions (&m, &k);
249:   GA_Dgemm ('T', 'N', k, n, m, alpha, this->pm, vv, beta, ww);
250:   TaoFunctionReturn (0);
251: }

253: int
254: TaoMatGa::MultiplyTransposeAdd (TaoVec * tv, TaoVec * tw, TaoVec * ty)
255: {
256:   GAVec xx = ((TaoVecGa *) tv)->GetVec ();
257:   GAVec ww = ((TaoVecGa *) tw)->GetVec ();
258:   GAVec yy = ((TaoVecGa *) ty)->GetVec ();
259:   TaoFunctionBegin;
260:   TaoFunctionBegin;
261:   GAVec temp_ww = GA_Duplicate (ww, (char*)"temp_WW");
262:   if (!temp_ww)
263:     GA_Error ((char*)"TaoMatGa::MultiplyAdd:Dupli cate:", temp_ww);
264:   GA_Copy (ww, temp_ww);
265:   double alpha = 1.0;
266:   double beta = 1.0;
267:   int m, n = 1, k;
268:   //n = 1 due to vecotor ww that is k by 1 array
269:   this->GetDimensions (&m, &k);
270:   GA_Dgemm ('T', 'N', k, n, m, alpha, this->pm, xx, beta, temp_ww);
271:   //copy from temp_ww to yy. Doing this way, the data in ww is not dirty!!!!!
272:   GA_Copy (temp_ww, yy);
273:   //free the memory held by the temporary GA array temp_ww
274:   GA_Destroy(temp_ww);
275:   TaoFunctionReturn (0);
276: }

278: int
279: TaoMatGa::SetDiagonal (TaoVec * tv)
280: {
281:   GAVec vv = ((TaoVecGa *) tv)->GetVec ();
282:   TaoFunctionBegin;
283:   GA_Set_diagonal (this->pm, vv);
284:   TaoFunctionReturn (0);
285: }

287: int
288: TaoMatGa::AddDiagonal (TaoVec * tv)
289: {
290:   GAVec vv = ((TaoVecGa *) tv)->GetVec ();
291:   TaoFunctionBegin;
292:   GA_Add_diagonal (this->pm, vv);
293:   TaoFunctionReturn (0);
294: }

296: int
297: TaoMatGa::GetDiagonal (TaoVec * tv)
298: {
299:   GAVec vv = ((TaoVecGa *) tv)->GetVec ();
300:   TaoFunctionBegin;
301:   GA_Get_diag (this->pm, vv);
302:   TaoFunctionReturn (0);
303: }

305: int
306: TaoMatGa::ShiftDiagonal (TaoScalar c)
307: {
308:   TaoFunctionBegin;
309:   GA_Shift_diagonal (this->pm, &c);
310:   TaoFunctionReturn (0);
311: }

313: int
314: TaoMatGa::View ()
315: {
316:   TaoFunctionBegin;
317:   GA_Print (this->pm);
318:   TaoFunctionReturn (0);
319: }


322: int
323: TaoMatGa::Presolve ()
324: {
325:   TaoFunctionBegin;
326:   TaoFunctionReturn (0);
327: }

329: int
330: TaoMatGa::Solve (TaoVec * tv, TaoVec * tw, TaoTruth * tt)
331: {
332:   TaoFunctionBegin;
333:   GA_Error ((char*)"not yet implemented.", 0);
334:   TaoFunctionReturn (1);
335: }

337: int
338: TaoMatGa::RowScale (TaoVec * tv)
339: {
340:   GAVec vv = ((TaoVecGa *) tv)->GetVec ();
341:   TaoFunctionBegin;
342:   GA_Scale_rows (this->pm, vv);
343:   TaoFunctionReturn (0);
344: }

346: int
347: TaoMatGa::ColScale (TaoVec * tv)
348: {
349:   GAVec vv = ((TaoVecGa *) tv)->GetVec ();
350:   TaoFunctionBegin;
351:   GA_Scale_cols (this->pm, vv);
352:   TaoFunctionReturn (0);
353: }

355: int
356: TaoMatGa::CreateReducedMatrix (TaoIndexSet * S1,
357:                                TaoIndexSet * S2, TaoMat ** MM)
358: {
359:   int info;
360:   TaoMatGa *M;
361:   GAMat B = 0;
362:   TaoFunctionBegin;
363:   info = TaoWrapGaMat (B, &M);
364:   CHKERRQ (info);
365:   *MM = M;
366:   TaoFunctionReturn (0);
367: }

369: int
370: TaoMatGa::SetReducedMatrix (TaoMat * M, TaoIndexSet * S1, TaoIndexSet * S2)
371: {
372:   TaoFunctionBegin;
373:   GA_Error ((char*)"Not yet implemented.", 0);
374:   TaoFunctionReturn (1);
375: }



379: int
380: TaoMatGa::D_Fischer (TaoVec * tx, TaoVec * tf, TaoVec * tl,
381:                      TaoVec * tu, TaoVec * tda,
382:                      TaoVec * tdb, TaoVec * tt1, TaoVec * tt2)
383: {
384:   TaoFunctionBegin;
385:   GA_Error ((char*)"Not yet implemented.", 0);
386:   TaoFunctionReturn (1);
387: }

389: int
390: TaoMatGa::Norm1 (double *nm)
391: {
392:   TaoFunctionBegin;
393:   GA_Norm1 (this->pm, nm);
394:   TaoFunctionReturn (0);
395: }


398: int
399: TaoMatGa::Fill (double c)
400: {

402:   TaoFunctionBegin;
403:   GA_Fill (this->pm, &c);
404:   TaoFunctionReturn (0);
405: }


408: int
409: TaoMatGa::Zero ()
410: {

412:   TaoFunctionBegin;
413:   GA_Zero (this->pm);
414:   TaoFunctionReturn (0);

416: }