/[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 751 by bcumming, Mon Jun 26 01:46:34 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 2006 by ACcESS MNRF                   *  
  *                                                          *  
  *              http://www.access.edu.au                    *  
  *       Primary Business: Queensland, Australia            *  
  *  Licensed under the Open Software License version 3.0    *  
  *     http://www.opensource.org/licenses/osl-3.0.php       *  
  *                                                          *  
  ************************************************************  
 */  
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 71  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    
 void Finley_Util_AddScatter(dim_t len,index_t* index,dim_t numData,double* in,double * out){  
    dim_t i,s;  
    for (s=0;s<len;s++) {  
        for(i=0;i<numData;i++) {  
           #pragma omp atomic  
           out[INDEX2(i,index[s],numData)]+=in[INDEX2(i,s,numData)];  
        }  
    }  
 }  
73    
74  #ifdef PASO_MPI  void Finley_Util_AddScatter(dim_t len,index_t* index,dim_t numData,double* in,double * out, index_t upperBound){
 /* same as AddScatter(), but checks that value index[] is below an upper bound upperBound before    
    addition. This is used to ensure that only the influence of local DOF is added */  
 /*        out(1:numData,index[p])+=in(1:numData,p)  
         where p = {k=1...len , index[k]<upperBound}*/  
 void Finley_Util_AddScatter_upperBound(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 ) {
           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  }    }
   
   
 #endif  
84    
85  /*    multiplies two matrices */  /*    multiplies two matrices */
86    
# Line 108  void Finley_Util_AddScatter_upperBound(d Line 88  void Finley_Util_AddScatter_upperBound(d
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 124  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 371  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 424  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 453  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 479  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 488  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  void Finley_copyDouble(dim_t n,double* source, double* target) {           }
565    dim_t i;           #ifdef PASO_MPI
566    for (i=0;i<n;i++) target[i]=source[i];           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  #ifdef PASO_MPI  #ifdef PASO_MPI
591  void Finley_printDoubleArray( FILE *fid, dim_t n, double *array, char *name  )  void Finley_printDoubleArray( FILE *fid, dim_t n, double *array, char *name  )
592  {  {
# Line 503  void Finley_printDoubleArray( FILE *fid, Line 596  void Finley_printDoubleArray( FILE *fid,
596      fprintf( fid, "%s [ ", name );      fprintf( fid, "%s [ ", name );
597    else    else
598      fprintf( fid, "[ " );        fprintf( fid, "[ " );  
599    for( i=0; i<(n<30 ? n : 30); i++ )    for( i=0; i<(n<60 ? n : 60); i++ )
600      fprintf( fid, "%g ", array[i] );      fprintf( fid, "%g ", array[i] );
601    if( n>=30 )    if( n>=30 )
602      fprintf( fid, "... " );      fprintf( fid, "... " );
# Line 517  void Finley_printIntArray( FILE *fid, di Line 610  void Finley_printIntArray( FILE *fid, di
610      fprintf( fid, "%s [ ", name );      fprintf( fid, "%s [ ", name );
611    else    else
612      fprintf( fid, "[ " );        fprintf( fid, "[ " );  
613    for( i=0; i<(n<30 ? n : 30); i++ )    for( i=0; i<(n<60 ? n : 60); i++ )
614      fprintf( fid, "%d ", array[i] );      fprintf( fid, "%d ", array[i] );
615    if( n>=30 )    if( n>=30 )
616      fprintf( fid, "... " );      fprintf( fid, "... " );
# Line 531  void Finley_printMaskArray( FILE *fid, d Line 624  void Finley_printMaskArray( FILE *fid, d
624      fprintf( fid, "%s [ ", name );      fprintf( fid, "%s [ ", name );
625    else    else
626      fprintf( fid, "[ " );        fprintf( fid, "[ " );  
627    for( i=0; i<(n<30 ? n : 30); i++ )    for( i=0; i<(n<60 ? n : 60); i++ )
628      if( array[i]!=-1 )      if( array[i]!=-1 )
629        fprintf( fid, "%d ", array[i] );        fprintf( fid, "%3d ", array[i] );
630      else      else
631        fprintf( fid, "* " );        fprintf( fid, "  * " );
632    if( n>=30 )    if( n>=30 )
633      fprintf( fid, "... " );      fprintf( fid, "... " );
634    fprintf( fid, "]\n" );    fprintf( fid, "]\n" );
635  }  }
636  #endif  #endif
   
 /*  
  * Revision 1.8  2005/08/12 01:45:43  jgs  
  *  
  * Revision 1.7.2.2  2005/09/07 06:26:22  gross  
  * the solver from finley are put into the standalone package paso now  
  *  
  * Revision 1.7.2.1  2005/08/04 22:41:11  gross  
  * some extra routines for finley that might speed-up RHS assembling in some cases (not actived right now)  
  *  
  * Revision 1.7  2005/07/08 04:07:59  jgs  
  * Merge of development branch back to main trunk on 2005-07-08  
  *  
  * Revision 1.1.1.1.2.4  2005/06/29 02:34:57  gross  
  * some changes towards 64 integers in finley  
  *  
  * Revision 1.1.1.1.2.3  2005/03/02 23:35:06  gross  
  * reimplementation of the ILU in Finley. block size>1 still needs some testing  
  *  
  * Revision 1.1.1.1.2.2  2005/02/18 02:27:31  gross  
  * two function that will be used for a reimplementation of the ILU preconditioner  
  *  
  * Revision 1.1.1.1.2.1  2004/11/12 06:58:19  gross  
  * 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  
  *  
  * Revision 1.1.1.1  2004/10/26 06:53:57  jgs  
  * initial import of project esys2  
  *  
  * Revision 1.3  2004/08/26 12:03:52  gross  
  * Some other bug in Finley_Assemble_gradient fixed.  
  *  
  * Revision 1.2  2004/07/02 04:21:13  gross  
  * Finley C code has been included  
  *  
  * Revision 1.1.1.1  2004/06/24 04:00:40  johng  
  * Initial version of eys using boost-python.  
  *  
  *  
  */  

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

  ViewVC Help
Powered by ViewVC 1.1.26