/[escript]/branches/domexper/dudley/src/Util.c
ViewVC logotype

Diff of /branches/domexper/dudley/src/Util.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/finley/src/Util.c revision 471 by jgs, Fri Jan 27 01:33:02 2006 UTC branches/domexper/dudley/src/Util.c revision 3082 by jfenwick, Tue Aug 3 06:04:05 2010 UTC
# Line 1  Line 1 
 /*  
  ******************************************************************************  
  *                                                                            *  
  *       COPYRIGHT  ACcESS 2003,2004,2005 -  All Rights Reserved              *  
  *                                                                            *  
  * This software is the property of ACcESS. No part of this code              *  
  * may be copied in any form or by any means without the expressed written    *  
  * consent of ACcESS.  Copying, use or modification of this software          *  
  * by any unauthorised person is illegal unless that person has a software    *  
  * license agreement with ACcESS.                                             *  
  *                                                                            *  
  ******************************************************************************  
 */  
1    
2  /**************************************************************/  /*******************************************************
3    *
4    * Copyright (c) 2003-2010 by University of Queensland
5    * Earth Systems Science Computational Center (ESSCC)
6    * http://www.uq.edu.au/esscc
7    *
8    * Primary Business: Queensland, Australia
9    * Licensed under the Open Software License version 3.0
10    * http://www.opensource.org/licenses/osl-3.0.php
11    *
12    *******************************************************/
13    
 /*   Some utility routines: */  
14    
15  /**************************************************************/  /**************************************************************/
16    
17  /*   author: gross@access.edu.au */  /*   Some utility routines: */
 /*   Version: $Id$ */  
18    
19  /**************************************************************/  /**************************************************************/
20    
 #include "Finley.h"  
21  #include "Util.h"  #include "Util.h"
22    
23  #ifdef _OPENMP  #ifdef _OPENMP
24  #include <omp.h>  #include <omp.h>
25  #endif  #endif
# Line 73  void Finley_Util_Gather_int(dim_t len,in Line 68  void Finley_Util_Gather_int(dim_t len,in
68    
69  /*   adds a vector in into out using and index. */  /*   adds a vector in into out using and index. */
70    
71  /*        out(1:numData,index(1:len))+=in(1:numData,1:len) */  /*        out(1:numData,index[p])+=in(1:numData,p) where p = {k=1...len , index[k]<upperBound}*/
72    
73    
74  void Finley_Util_AddScatter(dim_t len,index_t* index,dim_t numData,double* in,double * out){  void Finley_Util_AddScatter(dim_t len,index_t* index,dim_t numData,double* in,double * out, index_t upperBound){
75     dim_t i,s;     dim_t i,s;
76     for (s=0;s<len;s++) {     for (s=0;s<len;s++) {
77         for(i=0;i<numData;i++) {         for(i=0;i<numData;i++) {
78            #pragma omp atomic            if( index[s]<upperBound ) {
79            out[INDEX2(i,index[s],numData)]+=in[INDEX2(i,s,numData)];              out[INDEX2(i,index[s],numData)]+=in[INDEX2(i,s,numData)];
80          }
81         }         }
82     }     }
83  }  }
# Line 91  void Finley_Util_AddScatter(dim_t len,in Line 88  void Finley_Util_AddScatter(dim_t len,in
88    
89  void Finley_Util_SmallMatMult(dim_t A1,dim_t A2, double* A, dim_t B2, double*B, double* C) {  void Finley_Util_SmallMatMult(dim_t A1,dim_t A2, double* A, dim_t B2, double*B, double* C) {
90      dim_t i,j,s;      dim_t i,j,s;
91      for (i=0;i<A1*A2;i++) A[i]=0;      register double rtmp;
92         for (i=0;i<A1;i++) {         for (i=0;i<A1;i++) {
93            for (j=0;j<A2;j++) {            for (j=0;j<A2;j++) {
94               for (s=0;s<B2;s++) {               rtmp=0;
95                  A[INDEX2(i,j,A1)]+=B[INDEX2(i,s,A1)]*C[INDEX2(s,j,B2)];               for (s=0;s<B2;s++) rtmp+=B[INDEX2(i,s,A1)]*C[INDEX2(s,j,B2)];
96               }               A[INDEX2(i,j,A1)]=rtmp;
97            }            }
98         }         }
99  }  }
# Line 107  void Finley_Util_SmallMatMult(dim_t A1,d Line 104  void Finley_Util_SmallMatMult(dim_t A1,d
104    
105  void Finley_Util_SmallMatSetMult(dim_t len,dim_t A1,dim_t A2, double* A, dim_t B2, double*B, double* C) {  void Finley_Util_SmallMatSetMult(dim_t len,dim_t A1,dim_t A2, double* A, dim_t B2, double*B, double* C) {
106      dim_t q,i,j,s;      dim_t q,i,j,s;
107      for (i=0;i<A1*A2*len;i++) A[i]=0;      register double rtmp;
108      for (q=0;q<len;q++) {      for (q=0;q<len;q++) {
109         for (i=0;i<A1;i++) {         for (i=0;i<A1;i++) {
110            for (j=0;j<A2;j++) {            for (j=0;j<A2;j++) {
111               for (s=0;s<B2;s++) {               rtmp=0;
112                  A[INDEX3(i,j,q,A1,A2)]+=B[INDEX3(i,s,q,A1,B2)]*C[INDEX3(s,j,q,B2,A2)];               for (s=0;s<B2;s++) rtmp+=B[INDEX3(i,s,q,A1,B2)]*C[INDEX3(s,j,q,B2,A2)];
113               }               A[INDEX3(i,j,q, A1,A2)]=rtmp;
114              }
115           }
116        }
117    }
118    /*    multiplies a set of matries with a single matrix: */
119    
120    /*        A(1:A1,1:A2,i)=B(1:A1,1:B2,i)*C(1:B2,1:A2) i=1,len */
121    
122    void Finley_Util_SmallMatSetMult1(dim_t len,dim_t A1,dim_t A2, double* A, dim_t B2, double*B, double* C) {
123        dim_t q,i,j,s;
124        register double rtmp;
125        for (q=0;q<len;q++) {
126           for (i=0;i<A1;i++) {
127              for (j=0;j<A2;j++) {
128                 rtmp=0;
129                 for (s=0;s<B2;s++) rtmp+=B[INDEX3(i,s,q, A1,B2)]*C[INDEX2(s,j,B2)];
130                 A[INDEX3(i,j,q,A1,A2)]=rtmp;
131            }            }
132         }         }
133      }      }
# Line 354  int Finley_Util_ValueAndIndex_compar(con Line 368  int Finley_Util_ValueAndIndex_compar(con
368     e2=(Finley_Util_ValueAndIndex*) arg2;     e2=(Finley_Util_ValueAndIndex*) arg2;
369     if (e1->value < e2->value) return -1;     if (e1->value < e2->value) return -1;
370     if (e1->value > e2->value) return  1;     if (e1->value > e2->value) return  1;
371       if (e1->index < e2->index) return -1;
372       if (e1->index > e2->index) return  1;
373     return 0;     return 0;
374  }  }
375    
376  void Finley_Util_sortValueAndIndex(dim_t n,Finley_Util_ValueAndIndex* array) {  void Finley_Util_sortValueAndIndex(dim_t n,Finley_Util_ValueAndIndex* array) {
377       /* OMP : needs parallelization !*/       /* OMP : needs parallelization !*/
378       qsort(array,n,sizeof(Finley_Util_ValueAndIndex),Finley_Util_ValueAndIndex_compar);       qsort(array,n,sizeof(Finley_Util_ValueAndIndex),Finley_Util_ValueAndIndex_compar);
# Line 407  index_t Finley_Util_getMaxInt(dim_t dim, Line 424  index_t Finley_Util_getMaxInt(dim_t dim,
424     }     }
425     return out;     return out;
426  }  }
427    /**************************************************************/
428    
429    /* calculates the minimum value from a dim X N integer array */
430    
431    index_t Finley_Util_getFlaggedMinInt(dim_t dim,dim_t N,index_t* values, index_t ignore) {
432       dim_t i,j;
433       index_t out,out_local;
434       out=INDEX_T_MAX;
435       if (values!=NULL && dim*N>0 ) {
436         out=values[0];
437         #pragma omp parallel private(out_local)
438         {
439             out_local=out;
440             #pragma omp for private(i,j) schedule(static)
441             for (j=0;j<N;j++) {
442               for (i=0;i<dim;i++) if (values[INDEX2(i,j,dim)]!=ignore) out_local=MIN(out_local,values[INDEX2(i,j,dim)]);
443             }
444             #pragma omp critical
445             out=MIN(out_local,out);
446         }
447       }
448       return out;
449    }
450                                                                                                                                                      
451    /* calculates the maximum value from a dim X N integer array */
452    
453    index_t Finley_Util_getFlaggedMaxInt(dim_t dim,dim_t N,index_t* values, index_t ignore) {
454       dim_t i,j;
455       index_t out,out_local;
456       out=-INDEX_T_MAX;
457       if (values!=NULL && dim*N>0 ) {
458         out=values[0];
459         #pragma omp parallel private(out_local)
460         {
461             out_local=out;
462             #pragma omp for private(i,j) schedule(static)
463             for (j=0;j<N;j++) {
464                 for (i=0;i<dim;i++) if (values[INDEX2(i,j,dim)]!=ignore) out_local=MAX(out_local,values[INDEX2(i,j,dim)]);
465             }
466             #pragma omp critical
467             out=MAX(out_local,out);
468          }
469       }
470       return out;
471    }
472    
473  /* set the index of the positive entries in mask. The length of index is returned. */  /* set the index of the positive entries in mask. The length of index is returned. */
474    
# Line 436  index_t Finley_Util_cumsum(dim_t N,index Line 498  index_t Finley_Util_cumsum(dim_t N,index
498     index_t out=0,tmp;     index_t out=0,tmp;
499     dim_t i;     dim_t i;
500     #ifdef _OPENMP     #ifdef _OPENMP
501        index_t partial_sums[omp_get_max_threads()],sum;        index_t *partial_sums=NULL, sum;
502          partial_sums=TMPMEMALLOC(omp_get_max_threads(),index_t);
503        #pragma omp parallel private(sum,i,tmp)        #pragma omp parallel private(sum,i,tmp)
504        {        {
505          sum=0;          sum=0;
# Line 462  index_t Finley_Util_cumsum(dim_t N,index Line 525  index_t Finley_Util_cumsum(dim_t N,index
525            array[i]=tmp;            array[i]=tmp;
526          }          }
527        }        }
528          TMPMEMFREE(partial_sums);
529     #else     #else
530        for (i=0;i<N;++i) {        for (i=0;i<N;++i) {
531           tmp=out;           tmp=out;
# Line 471  index_t Finley_Util_cumsum(dim_t N,index Line 535  index_t Finley_Util_cumsum(dim_t N,index
535     #endif     #endif
536     return out;     return out;
537  }  }
538    void Finley_Util_setValuesInUse(const index_t *values, const dim_t numValues, dim_t *numValuesInUse, index_t **valuesInUse, Paso_MPIInfo* mpiinfo)
539    {
540       dim_t i;
541       index_t lastFoundValue=INDEX_T_MIN, minFoundValue, local_minFoundValue, *newValuesInUse=NULL;
542       register index_t itmp;
543       bool_t allFound=FALSE;
544       dim_t nv=0;
545    
546       while (! allFound) {
547           /*
548            *  find smallest value bigger than lastFoundValue
549            */
550            minFoundValue=INDEX_T_MAX;
551            #pragma omp parallel private(local_minFoundValue)
552            {
553                local_minFoundValue=minFoundValue;
554                #pragma omp for private(i,itmp) schedule(static)
555                for (i=0;i< numValues;i++) {
556                   itmp=values[i];
557                   if ((itmp>lastFoundValue) && (itmp<local_minFoundValue)) local_minFoundValue=itmp;
558                }
559                #pragma omp critical
560                {
561                   if (local_minFoundValue<minFoundValue) minFoundValue=local_minFoundValue;
562                }
563    
564             }
565             #ifdef PASO_MPI
566             local_minFoundValue=minFoundValue;
567             MPI_Allreduce(&local_minFoundValue,&minFoundValue, 1, MPI_INT, MPI_MIN, mpiinfo->comm );
568             #endif
569             /* if we found a new tag we need to add this too the valuesInUseList */
570    
571             if (minFoundValue < INDEX_T_MAX) {
572                 newValuesInUse=MEMALLOC(nv+1,index_t);
573                 if (*valuesInUse!=NULL) {
574                     memcpy(newValuesInUse,*valuesInUse,sizeof(index_t)*nv);
575                     MEMFREE(*valuesInUse);
576                 }
577                 newValuesInUse[nv]=minFoundValue;
578                 *valuesInUse=newValuesInUse;
579                 newValuesInUse=NULL;
580                 nv++;
581                 lastFoundValue=minFoundValue;
582             } else {
583                 allFound=TRUE;
584             }
585       }
586       *numValuesInUse=nv;
587    }
588    
589    
590  void Finley_copyDouble(dim_t n,double* source, double* target) {  #ifdef PASO_MPI
591    dim_t i;  void Finley_printDoubleArray( FILE *fid, dim_t n, double *array, char *name  )
592    for (i=0;i<n;i++) target[i]=source[i];  {
593  }    index_t i;
594      
595  /*    if( name )
596   * Revision 1.8  2005/08/12 01:45:43  jgs      fprintf( fid, "%s [ ", name );
597   * erge of development branch dev-02 back to main trunk on 2005-08-12    else
598   *      fprintf( fid, "[ " );  
599   * Revision 1.7.2.2  2005/09/07 06:26:22  gross    for( i=0; i<(n<60 ? n : 60); i++ )
600   * the solver from finley are put into the standalone package paso now      fprintf( fid, "%g ", array[i] );
601   *    if( n>=30 )
602   * Revision 1.7.2.1  2005/08/04 22:41:11  gross      fprintf( fid, "... " );
603   * some extra routines for finley that might speed-up RHS assembling in some cases (not actived right now)    fprintf( fid, "]\n" );
604   *  }
605   * Revision 1.7  2005/07/08 04:07:59  jgs  void Finley_printIntArray( FILE *fid, dim_t n, int *array, char *name  )
606   * Merge of development branch back to main trunk on 2005-07-08  {
607   *    index_t i;
608   * Revision 1.1.1.1.2.4  2005/06/29 02:34:57  gross    
609   * some changes towards 64 integers in finley    if( name )
610   *      fprintf( fid, "%s [ ", name );
611   * Revision 1.1.1.1.2.3  2005/03/02 23:35:06  gross    else
612   * reimplementation of the ILU in Finley. block size>1 still needs some testing      fprintf( fid, "[ " );  
613   *    for( i=0; i<(n<60 ? n : 60); i++ )
614   * Revision 1.1.1.1.2.2  2005/02/18 02:27:31  gross      fprintf( fid, "%d ", array[i] );
615   * two function that will be used for a reimplementation of the ILU preconditioner    if( n>=30 )
616   *      fprintf( fid, "... " );
617   * Revision 1.1.1.1.2.1  2004/11/12 06:58:19  gross    fprintf( fid, "]\n" );
618   * a lot of changes to get the linearPDE class running: most important change is that there is no matrix format exposed to the user anymore. the format is chosen by the Domain according to the solver and symmetry  }
619   *  void Finley_printMaskArray( FILE *fid, dim_t n, int *array, char *name  )
620   * Revision 1.1.1.1  2004/10/26 06:53:57  jgs  {
621   * initial import of project esys2    index_t i;
622   *    
623   * Revision 1.3  2004/08/26 12:03:52  gross    if( name )
624   * Some other bug in Finley_Assemble_gradient fixed.      fprintf( fid, "%s [ ", name );
625   *    else
626   * Revision 1.2  2004/07/02 04:21:13  gross      fprintf( fid, "[ " );  
627   * Finley C code has been included    for( i=0; i<(n<60 ? n : 60); i++ )
628   *      if( array[i]!=-1 )
629   * Revision 1.1.1.1  2004/06/24 04:00:40  johng        fprintf( fid, "%3d ", array[i] );
630   * Initial version of eys using boost-python.      else
631   *        fprintf( fid, "  * " );
632   *    if( n>=30 )
633   */      fprintf( fid, "... " );
634      fprintf( fid, "]\n" );
635    }
636    #endif

Legend:
Removed from v.471  
changed lines
  Added in v.3082

  ViewVC Help
Powered by ViewVC 1.1.26