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

revision 751 by bcumming, Mon Jun 26 01:46:34 2006 UTC revision 2881 by jfenwick, Thu Jan 28 02:03:15 2010 UTC
# Line 1  Line 1
/*
************************************************************
*          Copyright 2006 by ACcESS MNRF                   *
*                                                          *
*              http://www.access.edu.au                    *
*       Primary Business: Queensland, Australia            *
*                                                          *
************************************************************
*/
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
11    *
12    *******************************************************/
13
/*   Some utility routines: */
14
15  /**************************************************************/  /**************************************************************/
16
17  /*   author: gross@access.edu.au */  /*   Some utility routines: */
/*   Version: \$Id\$ */
18
19  /**************************************************************/  /**************************************************************/
20
21  #include "Finley.h"  #include "Finley.h"
22  #include "Util.h"  #include "Util.h"
23
24  #ifdef _OPENMP  #ifdef _OPENMP
25  #include <omp.h>  #include <omp.h>
26  #endif  #endif
# Line 71  void Finley_Util_Gather_int(dim_t len,in Line 69  void Finley_Util_Gather_int(dim_t len,in
69
70  /*   adds a vector in into out using and index. */  /*   adds a vector in into out using and index. */
71
72  /*        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}*/
73
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)];
}
}
}
74
75  #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){
76     dim_t i,s;     dim_t i,s;
77     for (s=0;s<len;s++) {     for (s=0;s<len;s++) {
78         for(i=0;i<numData;i++) {         for(i=0;i<numData;i++) {
79            //#pragma omp atomic            if( index[s]<upperBound ) {
if( index[s]<upperBound )
80              out[INDEX2(i,index[s],numData)]+=in[INDEX2(i,s,numData)];              out[INDEX2(i,index[s],numData)]+=in[INDEX2(i,s,numData)];
81          }
82         }         }
83     }     }
84  }    }

#endif
85
86  /*    multiplies two matrices */  /*    multiplies two matrices */
87
89
90  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) {
91      dim_t i,j,s;      dim_t i,j,s;
92      for (i=0;i<A1*A2;i++) A[i]=0;      register double rtmp;
93         for (i=0;i<A1;i++) {         for (i=0;i<A1;i++) {
94            for (j=0;j<A2;j++) {            for (j=0;j<A2;j++) {
95               for (s=0;s<B2;s++) {               rtmp=0;
96                  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)];
97               }               A[INDEX2(i,j,A1)]=rtmp;
98            }            }
99         }         }
100  }  }
# Line 124  void Finley_Util_SmallMatMult(dim_t A1,d Line 105  void Finley_Util_SmallMatMult(dim_t A1,d
105
106  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) {
107      dim_t q,i,j,s;      dim_t q,i,j,s;
108      for (i=0;i<A1*A2*len;i++) A[i]=0;      register double rtmp;
109      for (q=0;q<len;q++) {      for (q=0;q<len;q++) {
110         for (i=0;i<A1;i++) {         for (i=0;i<A1;i++) {
111            for (j=0;j<A2;j++) {            for (j=0;j<A2;j++) {
112               for (s=0;s<B2;s++) {               rtmp=0;
113                  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)];
114               }               A[INDEX3(i,j,q, A1,A2)]=rtmp;
115              }
116           }
117        }
118    }
119    /*    multiplies a set of matries with a single matrix: */
120
121    /*        A(1:A1,1:A2,i)=B(1:A1,1:B2,i)*C(1:B2,1:A2) i=1,len */
122
123    void Finley_Util_SmallMatSetMult1(dim_t len,dim_t A1,dim_t A2, double* A, dim_t B2, double*B, double* C) {
124        dim_t q,i,j,s;
125        register double rtmp;
126        for (q=0;q<len;q++) {
127           for (i=0;i<A1;i++) {
128              for (j=0;j<A2;j++) {
129                 rtmp=0;
130                 for (s=0;s<B2;s++) rtmp+=B[INDEX3(i,s,q, A1,B2)]*C[INDEX2(s,j,B2)];
131                 A[INDEX3(i,j,q,A1,A2)]=rtmp;
132            }            }
133         }         }
134      }      }
# Line 371  int Finley_Util_ValueAndIndex_compar(con Line 369  int Finley_Util_ValueAndIndex_compar(con
369     e2=(Finley_Util_ValueAndIndex*) arg2;     e2=(Finley_Util_ValueAndIndex*) arg2;
370     if (e1->value < e2->value) return -1;     if (e1->value < e2->value) return -1;
371     if (e1->value > e2->value) return  1;     if (e1->value > e2->value) return  1;
372       if (e1->index < e2->index) return -1;
373       if (e1->index > e2->index) return  1;
374     return 0;     return 0;
375  }  }
376
377  void Finley_Util_sortValueAndIndex(dim_t n,Finley_Util_ValueAndIndex* array) {  void Finley_Util_sortValueAndIndex(dim_t n,Finley_Util_ValueAndIndex* array) {
378       /* OMP : needs parallelization !*/       /* OMP : needs parallelization !*/
379       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 425  index_t Finley_Util_getMaxInt(dim_t dim,
425     }     }
426     return out;     return out;
427  }  }
428    /**************************************************************/
429
430    /* calculates the minimum value from a dim X N integer array */
431
432    index_t Finley_Util_getFlaggedMinInt(dim_t dim,dim_t N,index_t* values, index_t ignore) {
433       dim_t i,j;
434       index_t out,out_local;
435       out=INDEX_T_MAX;
436       if (values!=NULL && dim*N>0 ) {
437         out=values[0];
438         #pragma omp parallel private(out_local)
439         {
440             out_local=out;
441             #pragma omp for private(i,j) schedule(static)
442             for (j=0;j<N;j++) {
443               for (i=0;i<dim;i++) if (values[INDEX2(i,j,dim)]!=ignore) out_local=MIN(out_local,values[INDEX2(i,j,dim)]);
444             }
445             #pragma omp critical
446             out=MIN(out_local,out);
447         }
448       }
449       return out;
450    }
451
452    /* calculates the maximum value from a dim X N integer array */
453
454    index_t Finley_Util_getFlaggedMaxInt(dim_t dim,dim_t N,index_t* values, index_t ignore) {
455       dim_t i,j;
456       index_t out,out_local;
457       out=-INDEX_T_MAX;
458       if (values!=NULL && dim*N>0 ) {
459         out=values[0];
460         #pragma omp parallel private(out_local)
461         {
462             out_local=out;
463             #pragma omp for private(i,j) schedule(static)
464             for (j=0;j<N;j++) {
465                 for (i=0;i<dim;i++) if (values[INDEX2(i,j,dim)]!=ignore) out_local=MAX(out_local,values[INDEX2(i,j,dim)]);
466             }
467             #pragma omp critical
468             out=MAX(out_local,out);
469          }
470       }
471       return out;
472    }
473
474  /* 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. */
475
# Line 453  index_t Finley_Util_cumsum(dim_t N,index Line 499  index_t Finley_Util_cumsum(dim_t N,index
499     index_t out=0,tmp;     index_t out=0,tmp;
500     dim_t i;     dim_t i;
501     #ifdef _OPENMP     #ifdef _OPENMP
502        index_t partial_sums[omp_get_max_threads()],sum;        index_t *partial_sums=NULL, sum;
504        #pragma omp parallel private(sum,i,tmp)        #pragma omp parallel private(sum,i,tmp)
505        {        {
506          sum=0;          sum=0;
# Line 479  index_t Finley_Util_cumsum(dim_t N,index Line 526  index_t Finley_Util_cumsum(dim_t N,index
526            array[i]=tmp;            array[i]=tmp;
527          }          }
528        }        }
529          TMPMEMFREE(partial_sums);
530     #else     #else
531        for (i=0;i<N;++i) {        for (i=0;i<N;++i) {
532           tmp=out;           tmp=out;
# Line 488  index_t Finley_Util_cumsum(dim_t N,index Line 536  index_t Finley_Util_cumsum(dim_t N,index
536     #endif     #endif
537     return out;     return out;
538  }  }
539    void Finley_Util_setValuesInUse(const index_t *values, const dim_t numValues, dim_t *numValuesInUse, index_t **valuesInUse, Paso_MPIInfo* mpiinfo)
540    {
541       dim_t i;
542       index_t lastFoundValue=INDEX_T_MIN, minFoundValue, local_minFoundValue, *newValuesInUse=NULL;
543       register index_t itmp;
544       bool_t allFound=FALSE;
545       dim_t nv=0;
546
547       while (! allFound) {
548           /*
549            *  find smallest value bigger than lastFoundValue
550            */
551            minFoundValue=INDEX_T_MAX;
552            #pragma omp parallel private(local_minFoundValue)
553            {
554                local_minFoundValue=minFoundValue;
555                #pragma omp for private(i,itmp) schedule(static)
556                for (i=0;i< numValues;i++) {
557                   itmp=values[i];
558                   if ((itmp>lastFoundValue) && (itmp<local_minFoundValue)) local_minFoundValue=itmp;
559                }
560                #pragma omp critical
561                {
562                   if (local_minFoundValue<minFoundValue) minFoundValue=local_minFoundValue;
563                }
564
565  void Finley_copyDouble(dim_t n,double* source, double* target) {           }
566    dim_t i;           #ifdef PASO_MPI
567    for (i=0;i<n;i++) target[i]=source[i];           local_minFoundValue=minFoundValue;
568             MPI_Allreduce(&local_minFoundValue,&minFoundValue, 1, MPI_INT, MPI_MIN, mpiinfo->comm );
569             #endif
570             /* if we found a new tag we need to add this too the valuesInUseList */
571
572             if (minFoundValue < INDEX_T_MAX) {
573                 newValuesInUse=MEMALLOC(nv+1,index_t);
574                 if (*valuesInUse!=NULL) {
575                     memcpy(newValuesInUse,*valuesInUse,sizeof(index_t)*nv);
576                     MEMFREE(*valuesInUse);
577                 }
578                 newValuesInUse[nv]=minFoundValue;
579                 *valuesInUse=newValuesInUse;
580                 newValuesInUse=NULL;
581                 nv++;
582                 lastFoundValue=minFoundValue;
583             } else {
584                 allFound=TRUE;
585             }
586       }
587       *numValuesInUse=nv;
588  }  }
589
590
591  #ifdef PASO_MPI  #ifdef PASO_MPI
592  void Finley_printDoubleArray( FILE *fid, dim_t n, double *array, char *name  )  void Finley_printDoubleArray( FILE *fid, dim_t n, double *array, char *name  )
593  {  {
# Line 503  void Finley_printDoubleArray( FILE *fid, Line 597  void Finley_printDoubleArray( FILE *fid,
597      fprintf( fid, "%s [ ", name );      fprintf( fid, "%s [ ", name );
598    else    else
599      fprintf( fid, "[ " );        fprintf( fid, "[ " );
600    for( i=0; i<(n<30 ? n : 30); i++ )    for( i=0; i<(n<60 ? n : 60); i++ )
601      fprintf( fid, "%g ", array[i] );      fprintf( fid, "%g ", array[i] );
602    if( n>=30 )    if( n>=30 )
603      fprintf( fid, "... " );      fprintf( fid, "... " );
# Line 517  void Finley_printIntArray( FILE *fid, di Line 611  void Finley_printIntArray( FILE *fid, di
611      fprintf( fid, "%s [ ", name );      fprintf( fid, "%s [ ", name );
612    else    else
613      fprintf( fid, "[ " );        fprintf( fid, "[ " );
614    for( i=0; i<(n<30 ? n : 30); i++ )    for( i=0; i<(n<60 ? n : 60); i++ )
615      fprintf( fid, "%d ", array[i] );      fprintf( fid, "%d ", array[i] );
616    if( n>=30 )    if( n>=30 )
617      fprintf( fid, "... " );      fprintf( fid, "... " );
# Line 531  void Finley_printMaskArray( FILE *fid, d Line 625  void Finley_printMaskArray( FILE *fid, d
625      fprintf( fid, "%s [ ", name );      fprintf( fid, "%s [ ", name );
626    else    else
627      fprintf( fid, "[ " );        fprintf( fid, "[ " );
628    for( i=0; i<(n<30 ? n : 30); i++ )    for( i=0; i<(n<60 ? n : 60); i++ )
629      if( array[i]!=-1 )      if( array[i]!=-1 )
630        fprintf( fid, "%d ", array[i] );        fprintf( fid, "%3d ", array[i] );
631      else      else
632        fprintf( fid, "* " );        fprintf( fid, "  * " );
633    if( n>=30 )    if( n>=30 )
634      fprintf( fid, "... " );      fprintf( fid, "... " );
635    fprintf( fid, "]\n" );    fprintf( fid, "]\n" );
636  }  }
637  #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.2881