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

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

  ViewVC Help
Powered by ViewVC 1.1.26