/[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

revision 471 by jgs, Fri Jan 27 01:33:02 2006 UTC revision 2425 by gross, Thu May 14 09:06:35 2009 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-2008 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    
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 73  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    
74  void Finley_Util_AddScatter(dim_t len,index_t* index,dim_t numData,double* in,double * out){  
75    void Finley_Util_AddScatter(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 ) {
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  }  }
# Line 354  int Finley_Util_ValueAndIndex_compar(con Line 352  int Finley_Util_ValueAndIndex_compar(con
352     e2=(Finley_Util_ValueAndIndex*) arg2;     e2=(Finley_Util_ValueAndIndex*) arg2;
353     if (e1->value < e2->value) return -1;     if (e1->value < e2->value) return -1;
354     if (e1->value > e2->value) return  1;     if (e1->value > e2->value) return  1;
355       if (e1->index < e2->index) return -1;
356       if (e1->index > e2->index) return  1;
357     return 0;     return 0;
358  }  }
359    
360  void Finley_Util_sortValueAndIndex(dim_t n,Finley_Util_ValueAndIndex* array) {  void Finley_Util_sortValueAndIndex(dim_t n,Finley_Util_ValueAndIndex* array) {
361       /* OMP : needs parallelization !*/       /* OMP : needs parallelization !*/
362       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 408  index_t Finley_Util_getMaxInt(dim_t dim,
408     }     }
409     return out;     return out;
410  }  }
411    /**************************************************************/
412    
413    /* calculates the minimum value from a dim X N integer array */
414    
415    index_t Finley_Util_getFlaggedMinInt(dim_t dim,dim_t N,index_t* values, index_t ignore) {
416       dim_t i,j;
417       index_t out,out_local;
418       out=INDEX_T_MAX;
419       if (values!=NULL && dim*N>0 ) {
420         out=values[0];
421         #pragma omp parallel private(out_local)
422         {
423             out_local=out;
424             #pragma omp for private(i,j) schedule(static)
425             for (j=0;j<N;j++) {
426               for (i=0;i<dim;i++) if (values[INDEX2(i,j,dim)]!=ignore) out_local=MIN(out_local,values[INDEX2(i,j,dim)]);
427             }
428             #pragma omp critical
429             out=MIN(out_local,out);
430         }
431       }
432       return out;
433    }
434                                                                                                                                                      
435    /* calculates the maximum value from a dim X N integer array */
436    
437    index_t Finley_Util_getFlaggedMaxInt(dim_t dim,dim_t N,index_t* values, index_t ignore) {
438       dim_t i,j;
439       index_t out,out_local;
440       out=-INDEX_T_MAX;
441       if (values!=NULL && dim*N>0 ) {
442         out=values[0];
443         #pragma omp parallel private(out_local)
444         {
445             out_local=out;
446             #pragma omp for private(i,j) schedule(static)
447             for (j=0;j<N;j++) {
448                 for (i=0;i<dim;i++) if (values[INDEX2(i,j,dim)]!=ignore) out_local=MAX(out_local,values[INDEX2(i,j,dim)]);
449             }
450             #pragma omp critical
451             out=MAX(out_local,out);
452          }
453       }
454       return out;
455    }
456    
457  /* 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. */
458    
# Line 436  index_t Finley_Util_cumsum(dim_t N,index Line 482  index_t Finley_Util_cumsum(dim_t N,index
482     index_t out=0,tmp;     index_t out=0,tmp;
483     dim_t i;     dim_t i;
484     #ifdef _OPENMP     #ifdef _OPENMP
485        index_t partial_sums[omp_get_max_threads()],sum;        index_t *partial_sums=NULL, sum;
486          partial_sums=TMPMEMALLOC(omp_get_max_threads(),index_t);
487        #pragma omp parallel private(sum,i,tmp)        #pragma omp parallel private(sum,i,tmp)
488        {        {
489          sum=0;          sum=0;
# Line 462  index_t Finley_Util_cumsum(dim_t N,index Line 509  index_t Finley_Util_cumsum(dim_t N,index
509            array[i]=tmp;            array[i]=tmp;
510          }          }
511        }        }
512          TMPMEMFREE(partial_sums);
513     #else     #else
514        for (i=0;i<N;++i) {        for (i=0;i<N;++i) {
515           tmp=out;           tmp=out;
# Line 471  index_t Finley_Util_cumsum(dim_t N,index Line 519  index_t Finley_Util_cumsum(dim_t N,index
519     #endif     #endif
520     return out;     return out;
521  }  }
522    void Finley_Util_setValuesInUse(const index_t *values, const dim_t numValues, dim_t *numValuesInUse, index_t **valuesInUse, Paso_MPIInfo* mpiinfo)
523    {
524       dim_t i;
525       index_t lastFoundValue=INDEX_T_MIN, minFoundValue, local_minFoundValue, *newValuesInUse=NULL;
526       register index_t itmp;
527       bool_t allFound=FALSE;
528       dim_t nv=0;
529    
530       while (! allFound) {
531           /*
532            *  find smallest value bigger than lastFoundValue
533            */
534            minFoundValue=INDEX_T_MAX;
535            #pragma omp parallel private(local_minFoundValue)
536            {
537                local_minFoundValue=minFoundValue;
538                #pragma omp for private(i,itmp) schedule(static)
539                for (i=0;i< numValues;i++) {
540                   itmp=values[i];
541                   if ((itmp>lastFoundValue) && (itmp<local_minFoundValue)) local_minFoundValue=itmp;
542                }
543                #pragma omp critical
544                {
545                   if (local_minFoundValue<minFoundValue) minFoundValue=local_minFoundValue;
546                }
547    
548  void Finley_copyDouble(dim_t n,double* source, double* target) {           }
549    dim_t i;           #ifdef PASO_MPI
550    for (i=0;i<n;i++) target[i]=source[i];           local_minFoundValue=minFoundValue;
551  }           MPI_Allreduce(&local_minFoundValue,&minFoundValue, 1, MPI_INT, MPI_MIN, mpiinfo->comm );
552             #endif
553  /*           /* if we found a new tag we need to add this too the valuesInUseList */
554   * Revision 1.8  2005/08/12 01:45:43  jgs  
555   * erge of development branch dev-02 back to main trunk on 2005-08-12           if (minFoundValue < INDEX_T_MAX) {
556   *               newValuesInUse=MEMALLOC(nv+1,index_t);
557   * Revision 1.7.2.2  2005/09/07 06:26:22  gross               if (*valuesInUse!=NULL) {
558   * the solver from finley are put into the standalone package paso now                   memcpy(newValuesInUse,*valuesInUse,sizeof(index_t)*nv);
559   *                   MEMFREE(*valuesInUse);
560   * Revision 1.7.2.1  2005/08/04 22:41:11  gross               }
561   * some extra routines for finley that might speed-up RHS assembling in some cases (not actived right now)               newValuesInUse[nv]=minFoundValue;
562   *               *valuesInUse=newValuesInUse;
563   * Revision 1.7  2005/07/08 04:07:59  jgs               newValuesInUse=NULL;
564   * Merge of development branch back to main trunk on 2005-07-08               nv++;
565   *               lastFoundValue=minFoundValue;
566   * Revision 1.1.1.1.2.4  2005/06/29 02:34:57  gross           } else {
567   * some changes towards 64 integers in finley               allFound=TRUE;
568   *           }
569   * Revision 1.1.1.1.2.3  2005/03/02 23:35:06  gross     }
570   * reimplementation of the ILU in Finley. block size>1 still needs some testing     *numValuesInUse=nv;
571   *  }
572   * Revision 1.1.1.1.2.2  2005/02/18 02:27:31  gross  
573   * two function that will be used for a reimplementation of the ILU preconditioner  
574   *  #ifdef PASO_MPI
575   * Revision 1.1.1.1.2.1  2004/11/12 06:58:19  gross  void Finley_printDoubleArray( FILE *fid, dim_t n, double *array, char *name  )
576   * 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  {
577   *    index_t i;
578   * Revision 1.1.1.1  2004/10/26 06:53:57  jgs    
579   * initial import of project esys2    if( name )
580   *      fprintf( fid, "%s [ ", name );
581   * Revision 1.3  2004/08/26 12:03:52  gross    else
582   * Some other bug in Finley_Assemble_gradient fixed.      fprintf( fid, "[ " );  
583   *    for( i=0; i<(n<60 ? n : 60); i++ )
584   * Revision 1.2  2004/07/02 04:21:13  gross      fprintf( fid, "%g ", array[i] );
585   * Finley C code has been included    if( n>=30 )
586   *      fprintf( fid, "... " );
587   * Revision 1.1.1.1  2004/06/24 04:00:40  johng    fprintf( fid, "]\n" );
588   * Initial version of eys using boost-python.  }
589   *  void Finley_printIntArray( FILE *fid, dim_t n, int *array, char *name  )
590   *  {
591   */    index_t i;
592      
593      if( name )
594        fprintf( fid, "%s [ ", name );
595      else
596        fprintf( fid, "[ " );  
597      for( i=0; i<(n<60 ? n : 60); i++ )
598        fprintf( fid, "%d ", array[i] );
599      if( n>=30 )
600        fprintf( fid, "... " );
601      fprintf( fid, "]\n" );
602    }
603    void Finley_printMaskArray( FILE *fid, dim_t n, int *array, char *name  )
604    {
605      index_t i;
606      
607      if( name )
608        fprintf( fid, "%s [ ", name );
609      else
610        fprintf( fid, "[ " );  
611      for( i=0; i<(n<60 ? n : 60); i++ )
612        if( array[i]!=-1 )
613          fprintf( fid, "%3d ", array[i] );
614        else
615          fprintf( fid, "  * " );
616      if( n>=30 )
617        fprintf( fid, "... " );
618      fprintf( fid, "]\n" );
619    }
620    #endif

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

  ViewVC Help
Powered by ViewVC 1.1.26