/[escript]/temp/finley/src/Util.c
ViewVC logotype

Annotation of /temp/finley/src/Util.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1387 - (hide annotations)
Fri Jan 11 07:45:26 2008 UTC (11 years, 9 months ago) by trankine
File MIME type: text/plain
File size: 16842 byte(s)
Restore the trunk that existed before the windows changes were committed to the (now moved to branches) old trunk.
1 jgs 82
2 ksteube 1312 /* $Id$ */
3 jgs 82
4 ksteube 1312 /*******************************************************
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 jgs 82
16     /**************************************************************/
17    
18 ksteube 1312 /* Some utility routines: */
19 jgs 82
20     /**************************************************************/
21    
22     #include "Finley.h"
23     #include "Util.h"
24 woo409 757
25 jgs 113 #ifdef _OPENMP
26     #include <omp.h>
27     #endif
28 jgs 82
29     /**************************************************************/
30    
31 jgs 147 /* returns true if any of the values in the short array values is not equalt to Zero */
32    
33     bool_t Finley_Util_anyNonZeroDouble(dim_t N, double* values) {
34     dim_t q;
35     for (q=0;q<N;++q) if (ABS(values[q])>0) return TRUE;
36     return FALSE;
37     }
38     /**************************************************************/
39    
40 jgs 82 /* gathers double values out from in by index: */
41    
42     /* out(1:numData,1:len)=in(1:numData,index(1:len)) */
43    
44 jgs 123 void Finley_Util_Gather_double(dim_t len,index_t* index,dim_t numData,double* in, double * out){
45     dim_t s,i;
46 jgs 82 for (s=0;s<len;s++) {
47     for (i=0;i<numData;i++) {
48     out[INDEX2(i,s,numData)]=in[INDEX2(i,index[s],numData)];
49     }
50     }
51     }
52    
53     /**************************************************************/
54    
55    
56     /* gathers maybelong values out from in by index: */
57    
58     /* out(1:numData,1:len)=in(1:numData,index(1:len)) */
59    
60 jgs 123 void Finley_Util_Gather_int(dim_t len,index_t* index,dim_t numData, index_t* in, index_t * out){
61     dim_t s,i;
62 jgs 82 for (s=0;s<len;s++) {
63     for (i=0;i<numData;i++) {
64     out[INDEX2(i,s,numData)]=in[INDEX2(i,index[s],numData)];
65     }
66     }
67     }
68    
69     /**************************************************************/
70    
71     /* adds a vector in into out using and index. */
72    
73 gross 798 /* out(1:numData,index[p])+=in(1:numData,p) where p = {k=1...len , index[k]<upperBound}*/
74 jgs 82
75 gross 798
76     void Finley_Util_AddScatter(dim_t len,index_t* index,dim_t numData,double* in,double * out, index_t upperBound){
77 jgs 123 dim_t i,s;
78 jgs 82 for (s=0;s<len;s++) {
79     for(i=0;i<numData;i++) {
80 ksteube 799 if( index[s]<upperBound ) {
81 bcumming 751 out[INDEX2(i,index[s],numData)]+=in[INDEX2(i,s,numData)];
82 ksteube 799 }
83 bcumming 751 }
84     }
85 gross 798 }
86 bcumming 751
87 jgs 82 /* multiplies two matrices */
88    
89     /* A(1:A1,1:A2)=B(1:A1,1:B2)*C(1:B2,1:A2) */
90    
91 jgs 123 void Finley_Util_SmallMatMult(dim_t A1,dim_t A2, double* A, dim_t B2, double*B, double* C) {
92     dim_t i,j,s;
93 jgs 82 for (i=0;i<A1*A2;i++) A[i]=0;
94     for (i=0;i<A1;i++) {
95     for (j=0;j<A2;j++) {
96     for (s=0;s<B2;s++) {
97     A[INDEX2(i,j,A1)]+=B[INDEX2(i,s,A1)]*C[INDEX2(s,j,B2)];
98     }
99     }
100     }
101     }
102    
103     /* multiplies a two sets of matries: */
104    
105     /* A(1:A1,1:A2,i)=B(1:A1,1:B2,i)*C(1:B2,1:A2,i) i=1,len */
106    
107 jgs 123 void Finley_Util_SmallMatSetMult(dim_t len,dim_t A1,dim_t A2, double* A, dim_t B2, double*B, double* C) {
108     dim_t q,i,j,s;
109 jgs 82 for (i=0;i<A1*A2*len;i++) A[i]=0;
110     for (q=0;q<len;q++) {
111     for (i=0;i<A1;i++) {
112     for (j=0;j<A2;j++) {
113     for (s=0;s<B2;s++) {
114     A[INDEX3(i,j,q,A1,A2)]+=B[INDEX3(i,s,q,A1,B2)]*C[INDEX3(s,j,q,B2,A2)];
115     }
116     }
117     }
118     }
119     }
120     /* inverts the set of dim x dim matrices A(:,:,1:len) with dim=1,2,3 */
121     /* the determinante is returned. */
122    
123 jgs 123 void Finley_Util_InvertSmallMat(dim_t len,dim_t dim,double* A,double *invA, double* det){
124     dim_t q;
125     register double D,A11,A12,A13,A21,A22,A23,A31,A32,A33;
126 jgs 82
127     switch(dim) {
128     case 1:
129     for (q=0;q<len;q++) {
130 jgs 115 D=A[q];
131 jgs 102 if (ABS(D) > 0 ){
132     det[q]=D;
133     D=1./D;
134 jgs 115 invA[q]=D;
135 jgs 102 } else {
136 jgs 150 Finley_setError(ZERO_DIVISION_ERROR,"__FILE__: Non-regular matrix");
137 jgs 82 return;
138     }
139     }
140     break;
141    
142     case 2:
143     for (q=0;q<len;q++) {
144 jgs 115 A11=A[INDEX3(0,0,q,2,2)];
145     A12=A[INDEX3(0,1,q,2,2)];
146     A21=A[INDEX3(1,0,q,2,2)];
147     A22=A[INDEX3(1,1,q,2,2)];
148 jgs 82
149     D = A11*A22-A12*A21;
150 jgs 102 if (ABS(D) > 0 ){
151     det[q]=D;
152     D=1./D;
153 jgs 115 invA[INDEX3(0,0,q,2,2)]= A22*D;
154     invA[INDEX3(1,0,q,2,2)]=-A21*D;
155     invA[INDEX3(0,1,q,2,2)]=-A12*D;
156     invA[INDEX3(1,1,q,2,2)]= A11*D;
157 jgs 102 } else {
158 jgs 150 Finley_setError(ZERO_DIVISION_ERROR,"__FILE__: Non-regular matrix");
159 jgs 82 return;
160     }
161     }
162     break;
163    
164     case 3:
165     for (q=0;q<len;q++) {
166 jgs 115 A11=A[INDEX3(0,0,q,3,3)];
167     A21=A[INDEX3(1,0,q,3,3)];
168     A31=A[INDEX3(2,0,q,3,3)];
169     A12=A[INDEX3(0,1,q,3,3)];
170     A22=A[INDEX3(1,1,q,3,3)];
171     A32=A[INDEX3(2,1,q,3,3)];
172     A13=A[INDEX3(0,2,q,3,3)];
173     A23=A[INDEX3(1,2,q,3,3)];
174     A33=A[INDEX3(2,2,q,3,3)];
175 jgs 82
176     D = A11*(A22*A33-A23*A32)+ A12*(A31*A23-A21*A33)+A13*(A21*A32-A31*A22);
177 jgs 102 if (ABS(D) > 0 ){
178     det[q] =D;
179     D=1./D;
180 jgs 115 invA[INDEX3(0,0,q,3,3)]=(A22*A33-A23*A32)*D;
181     invA[INDEX3(1,0,q,3,3)]=(A31*A23-A21*A33)*D;
182     invA[INDEX3(2,0,q,3,3)]=(A21*A32-A31*A22)*D;
183     invA[INDEX3(0,1,q,3,3)]=(A13*A32-A12*A33)*D;
184     invA[INDEX3(1,1,q,3,3)]=(A11*A33-A31*A13)*D;
185     invA[INDEX3(2,1,q,3,3)]=(A12*A31-A11*A32)*D;
186     invA[INDEX3(0,2,q,3,3)]=(A12*A23-A13*A22)*D;
187     invA[INDEX3(1,2,q,3,3)]=(A13*A21-A11*A23)*D;
188     invA[INDEX3(2,2,q,3,3)]=(A11*A22-A12*A21)*D;
189 jgs 102 } else {
190 jgs 150 Finley_setError(ZERO_DIVISION_ERROR,"__FILE__: Non-regular matrix");
191 jgs 82 return;
192     }
193     }
194     break;
195    
196     }
197     return;
198     }
199    
200     /* sets the derterminate of a set of dim x dim matrices A(:,:,1:len) with dim=1,2,3 */
201    
202 jgs 123 void Finley_Util_DetOfSmallMat(dim_t len,dim_t dim,double* A, double* det){
203     dim_t q;
204     register double A11,A12,A13,A21,A22,A23,A31,A32,A33;
205 jgs 82
206     switch(dim) {
207     case 1:
208     for (q=0;q<len;q++) {
209 jgs 115 det[q]=A[q];
210 jgs 82 }
211     break;
212    
213     case 2:
214     for (q=0;q<len;q++) {
215 jgs 115 A11=A[INDEX3(0,0,q,2,2)];
216     A12=A[INDEX3(0,1,q,2,2)];
217     A21=A[INDEX3(1,0,q,2,2)];
218     A22=A[INDEX3(1,1,q,2,2)];
219 jgs 82
220     det[q] = A11*A22-A12*A21;
221     }
222     break;
223    
224     case 3:
225     for (q=0;q<len;q++) {
226 jgs 115 A11=A[INDEX3(0,0,q,3,3)];
227     A21=A[INDEX3(1,0,q,3,3)];
228     A31=A[INDEX3(2,0,q,3,3)];
229     A12=A[INDEX3(0,1,q,3,3)];
230     A22=A[INDEX3(1,1,q,3,3)];
231     A32=A[INDEX3(2,1,q,3,3)];
232     A13=A[INDEX3(0,2,q,3,3)];
233     A23=A[INDEX3(1,2,q,3,3)];
234     A33=A[INDEX3(2,2,q,3,3)];
235 jgs 82
236     det[q] = A11*(A22*A33-A23*A32)+ A12*(A31*A23-A21*A33)+A13*(A21*A32-A31*A22);
237     }
238     break;
239    
240     }
241     return;
242     }
243     /* returns the normalized vector Normal[dim,len] orthogonal to A(:,0,q) and A(:,1,q) in the case of dim=3 */
244     /* or the vector A(:,0,q) in the case of dim=2 */
245    
246 jgs 123 void Finley_NormalVector(dim_t len, dim_t dim, dim_t dim1, double* A,double* Normal) {
247     dim_t q;
248     register double A11,A12,CO_A13,A21,A22,CO_A23,A31,A32,CO_A33,length,invlength;
249 jgs 82
250     switch(dim) {
251     case 1:
252 jgs 115 for (q=0;q<len;q++) Normal[q] =1;
253 jgs 82 break;
254     case 2:
255     for (q=0;q<len;q++) {
256 jgs 115 A11=A[INDEX3(0,0,q,2,dim1)];
257     A21=A[INDEX3(1,0,q,2,dim1)];
258 jgs 82 length = sqrt(A11*A11+A21*A21);
259     if (! length>0) {
260 jgs 150 Finley_setError(ZERO_DIVISION_ERROR,"__FILE__: area equals zero.");
261 jgs 82 return;
262     } else {
263     invlength=1./length;
264 jgs 115 Normal[INDEX2(0,q,2)]=A21*invlength;
265     Normal[INDEX2(1,q,2)]=-A11*invlength;
266 jgs 82 }
267     }
268     break;
269     case 3:
270     for (q=0;q<len;q++) {
271 jgs 115 A11=A[INDEX3(0,0,q,3,dim1)];
272     A21=A[INDEX3(1,0,q,3,dim1)];
273     A31=A[INDEX3(2,0,q,3,dim1)];
274     A12=A[INDEX3(0,1,q,3,dim1)];
275     A22=A[INDEX3(1,1,q,3,dim1)];
276     A32=A[INDEX3(2,1,q,3,dim1)];
277 jgs 82 CO_A13=A21*A32-A31*A22;
278     CO_A23=A31*A12-A11*A32;
279     CO_A33=A11*A22-A21*A12;
280     length=sqrt(CO_A13*CO_A13+CO_A23*CO_A23+CO_A33*CO_A33);
281     if (! length>0) {
282 jgs 150 Finley_setError(ZERO_DIVISION_ERROR,"__FILE__: area equals zero.");
283 jgs 82 return;
284     } else {
285     invlength=1./length;
286 jgs 115 Normal[INDEX2(0,q,3)]=CO_A13*invlength;
287     Normal[INDEX2(1,q,3)]=CO_A23*invlength;
288     Normal[INDEX2(2,q,3)]=CO_A33*invlength;
289 jgs 82 }
290    
291     }
292     break;
293    
294     }
295     return;
296     }
297    
298     /* return the length of the vector which is orthogonal to the vectors A(:,0,q) and A(:,1,q) in the case of dim=3 */
299     /* or the vector A(:,0,q) in the case of dim=2 */
300    
301 jgs 123 void Finley_LengthOfNormalVector(dim_t len, dim_t dim, dim_t dim1, double* A,double* length) {
302     dim_t q;
303 jgs 82 double A11,A12,CO_A13,A21,A22,CO_A23,A31,A32,CO_A33;
304    
305     switch(dim) {
306     case 1:
307 jgs 115 for (q=0;q<len;q++) length[q] =1;
308 jgs 82 break;
309     case 2:
310     for (q=0;q<len;q++) {
311 jgs 115 A11=A[INDEX3(0,0,q,2,dim1)];
312     A21=A[INDEX3(1,0,q,2,dim1)];
313 jgs 82 length[q] = sqrt(A11*A11+A21*A21);
314     }
315     break;
316     case 3:
317     for (q=0;q<len;q++) {
318 jgs 115 A11=A[INDEX3(0,0,q,3,dim1)];
319     A21=A[INDEX3(1,0,q,3,dim1)];
320     A31=A[INDEX3(2,0,q,3,dim1)];
321     A12=A[INDEX3(0,1,q,3,dim1)];
322     A22=A[INDEX3(1,1,q,3,dim1)];
323     A32=A[INDEX3(2,1,q,3,dim1)];
324 jgs 82 CO_A13=A21*A32-A31*A22;
325     CO_A23=A31*A12-A11*A32;
326     CO_A33=A11*A22-A21*A12;
327     length[q]=sqrt(CO_A13*CO_A13+CO_A23*CO_A23+CO_A33*CO_A33);
328     }
329     break;
330    
331     }
332     return;
333     }
334    
335     /* inverts the map map of length len */
336     /* there is no range checking! */
337     /* at output Map[invMap[i]]=i for i=0:lenInvMap */
338    
339 jgs 123 void Finley_Util_InvertMap(dim_t lenInvMap, index_t* invMap,dim_t lenMap, index_t* Map) {
340     dim_t i;
341 jgs 82 for (i=0;i<lenInvMap;i++) invMap[i]=0;
342     for (i=0;i<lenMap;i++) {
343     if (Map[i]>=0) invMap[Map[i]]=i;
344     }
345     }
346    
347     /* orders a Finley_Util_ValueAndIndex array by value */
348     /* it is assumed that n is large */
349    
350     int Finley_Util_ValueAndIndex_compar(const void *arg1 , const void *arg2 ) {
351     Finley_Util_ValueAndIndex *e1,*e2;
352     e1=(Finley_Util_ValueAndIndex*) arg1;
353     e2=(Finley_Util_ValueAndIndex*) arg2;
354     if (e1->value < e2->value) return -1;
355     if (e1->value > e2->value) return 1;
356 gross 763 if (e1->index < e2->index) return -1;
357     if (e1->index > e2->index) return 1;
358 jgs 82 return 0;
359     }
360 woo409 757
361 jgs 123 void Finley_Util_sortValueAndIndex(dim_t n,Finley_Util_ValueAndIndex* array) {
362 jgs 82 /* OMP : needs parallelization !*/
363     qsort(array,n,sizeof(Finley_Util_ValueAndIndex),Finley_Util_ValueAndIndex_compar);
364     }
365    
366    
367     /**************************************************************/
368    
369     /* calculates the minimum value from a dim X N integer array */
370    
371 jgs 123 index_t Finley_Util_getMinInt(dim_t dim,dim_t N,index_t* values) {
372     dim_t i,j;
373     index_t out,out_local;
374     out=INDEX_T_MAX;
375 jgs 82 if (values!=NULL && dim*N>0 ) {
376     out=values[0];
377 jgs 115 #pragma omp parallel private(out_local)
378     {
379     out_local=out;
380     #pragma omp for private(i,j) schedule(static)
381     for (j=0;j<N;j++) {
382     for (i=0;i<dim;i++) out_local=MIN(out_local,values[INDEX2(i,j,dim)]);
383     }
384     #pragma omp critical
385     out=MIN(out_local,out);
386 jgs 82 }
387     }
388     return out;
389     }
390    
391     /* calculates the maximum value from a dim X N integer array */
392    
393 jgs 123 index_t Finley_Util_getMaxInt(dim_t dim,dim_t N,index_t* values) {
394     dim_t i,j;
395     index_t out,out_local;
396     out=-INDEX_T_MAX;
397 jgs 82 if (values!=NULL && dim*N>0 ) {
398     out=values[0];
399 jgs 115 #pragma omp parallel private(out_local)
400     {
401     out_local=out;
402     #pragma omp for private(i,j) schedule(static)
403     for (j=0;j<N;j++) {
404     for (i=0;i<dim;i++) out_local=MAX(out_local,values[INDEX2(i,j,dim)]);
405     }
406     #pragma omp critical
407     out=MAX(out_local,out);
408     }
409 jgs 82 }
410     return out;
411     }
412 ksteube 1312 /**************************************************************/
413 jgs 82
414 ksteube 1312 /* 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 jgs 82 /* set the index of the positive entries in mask. The length of index is returned. */
459    
460 jgs 123 dim_t Finley_Util_packMask(dim_t N,index_t* mask,index_t* index) {
461     dim_t out,k;
462 jgs 82 out=0;
463     /*OMP */
464     for (k=0;k<N;k++) {
465     if (mask[k]>=0) {
466     index[out]=k;
467     out++;
468     }
469     }
470     return out;
471     }
472    
473     /* returns true if array contains value */
474 jgs 123 bool_t Finley_Util_isAny(dim_t N,index_t* array,index_t value) {
475     bool_t out=FALSE;
476     dim_t i;
477 jgs 82 #pragma omp parallel for private(i) schedule(static) reduction(||:out)
478 jgs 115 for (i=0;i<N;i++) out = out || (array[i]==value);
479 jgs 82 return out;
480     }
481 jgs 113 /* calculates the cummultative sum in array and returns the total sum */
482 jgs 123 index_t Finley_Util_cumsum(dim_t N,index_t* array) {
483     index_t out=0,tmp;
484     dim_t i;
485 jgs 113 #ifdef _OPENMP
486 jgs 123 index_t partial_sums[omp_get_max_threads()],sum;
487 jgs 113 #pragma omp parallel private(sum,i,tmp)
488     {
489     sum=0;
490 jgs 115 #pragma omp for schedule(static)
491     for (i=0;i<N;++i) sum+=array[i];
492 jgs 113 partial_sums[omp_get_thread_num()]=sum;
493 jgs 115 #pragma omp barrier
494 jgs 113 #pragma omp master
495     {
496     out=0;
497     for (i=0;i<omp_get_max_threads();++i) {
498     tmp=out;
499     out+=partial_sums[i];
500     partial_sums[i]=tmp;
501     }
502     }
503 jgs 115 #pragma omp barrier
504 jgs 113 sum=partial_sums[omp_get_thread_num()];
505 jgs 115 #pragma omp for schedule(static)
506     for (i=0;i<N;++i) {
507     tmp=sum;
508     sum+=array[i];
509     array[i]=tmp;
510     }
511 jgs 113 }
512     #else
513     for (i=0;i<N;++i) {
514     tmp=out;
515     out+=array[i];
516     array[i]=tmp;
517     }
518     #endif
519     return out;
520     }
521 jgs 82
522 jgs 123
523 bcumming 751 #ifdef PASO_MPI
524     void Finley_printDoubleArray( FILE *fid, dim_t n, double *array, char *name )
525     {
526     index_t i;
527    
528     if( name )
529     fprintf( fid, "%s [ ", name );
530     else
531     fprintf( fid, "[ " );
532 bcumming 782 for( i=0; i<(n<60 ? n : 60); i++ )
533 bcumming 751 fprintf( fid, "%g ", array[i] );
534     if( n>=30 )
535     fprintf( fid, "... " );
536     fprintf( fid, "]\n" );
537     }
538     void Finley_printIntArray( FILE *fid, dim_t n, int *array, char *name )
539     {
540     index_t i;
541    
542     if( name )
543     fprintf( fid, "%s [ ", name );
544     else
545     fprintf( fid, "[ " );
546 bcumming 782 for( i=0; i<(n<60 ? n : 60); i++ )
547 bcumming 751 fprintf( fid, "%d ", array[i] );
548     if( n>=30 )
549     fprintf( fid, "... " );
550     fprintf( fid, "]\n" );
551     }
552     void Finley_printMaskArray( FILE *fid, dim_t n, int *array, char *name )
553     {
554     index_t i;
555    
556     if( name )
557     fprintf( fid, "%s [ ", name );
558     else
559     fprintf( fid, "[ " );
560 bcumming 782 for( i=0; i<(n<60 ? n : 60); i++ )
561 bcumming 751 if( array[i]!=-1 )
562 bcumming 782 fprintf( fid, "%3d ", array[i] );
563 bcumming 751 else
564 bcumming 782 fprintf( fid, " * " );
565 bcumming 751 if( n>=30 )
566     fprintf( fid, "... " );
567     fprintf( fid, "]\n" );
568     }
569     #endif

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26