/[escript]/trunk-mpi-branch/paso/src/Paso_MPI.c
ViewVC logotype

Contents of /trunk-mpi-branch/paso/src/Paso_MPI.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1249 - (show annotations)
Thu Aug 16 00:47:44 2007 UTC (11 years, 8 months ago) by ksteube
File MIME type: text/plain
File size: 3482 byte(s)
Fixed Paso_MPIInfo_mod for n=1 (single CPU).
Removed four unit tests relating to Interval() object.
Removed unit test file escript/test/DataProfTestCase.cpp.

1 #include <stdlib.h>
2 #include <stdio.h>
3
4
5 #include "Paso_MPI.h"
6
7
8 /* allocate memory for an mpi_comm, and find the communicator details */
9 Paso_MPIInfo* Paso_MPIInfo_alloc( MPI_Comm comm )
10 {
11 int error;
12 Paso_MPIInfo *out=NULL;
13
14 out = MEMALLOC( 1, Paso_MPIInfo );
15
16 out->reference_counter = 0;
17 out->msg_tag_counter = 0;
18 #ifdef PASO_MPI
19 error = MPI_Comm_rank( comm, &out->rank )==MPI_SUCCESS && MPI_Comm_size( comm, &out->size )==MPI_SUCCESS;
20 if( !error ) {
21 Paso_setError( PASO_MPI_ERROR, "Paso_MPIInfo_alloc : error finding comm rank/size" );
22 }
23
24 out->comm = comm;
25 #else
26 out->rank=0;
27 out->size=1;
28 out->comm=-1;
29 #endif
30 out->reference_counter++;
31
32 return out;
33 }
34
35 /* free memory for an mpi_comm */
36 void Paso_MPIInfo_free( Paso_MPIInfo *in )
37 {
38 if( in && !(--in->reference_counter) )
39 MEMFREE( in );
40 }
41
42 Paso_MPIInfo *Paso_MPIInfo_getReference( Paso_MPIInfo* in )
43 {
44 if (in!=NULL)
45 ++(in->reference_counter);
46
47 return in;
48 }
49 /* N = #CPUs, k is a CPU number but out of range or even negative. Return a CPU number in 0...n-1. */
50 index_t Paso_MPIInfo_mod(index_t n, index_t k)
51 {
52 index_t q, out=0;
53 if (n>1) {
54 q=k/n;
55 if (k>0) {
56 out=k-n*q;
57 } else if (k<0) {
58 out=k-n*(q-1);
59 }
60 }
61 return out;
62 }
63
64 void Paso_MPIInfo_Split( Paso_MPIInfo *mpi_info, dim_t N, dim_t* local_N,index_t* offset)
65 {
66 int rest=0;
67 int s=mpi_info->size;
68 int r=mpi_info->rank;
69 *local_N=N/s;
70 rest=N-(*local_N)*s;
71 if (r<rest) {
72 (*local_N)++;
73 (*offset)=(*local_N)*r;
74 } else {
75 (*offset)=(*local_N)*r+rest;
76 }
77 }
78
79
80 dim_t Paso_MPIInfo_setDistribution(Paso_MPIInfo* mpi_info ,index_t min_id,index_t max_id,index_t* distribution) {
81 int rest=0, p;
82 dim_t out;
83 int s=mpi_info->size;
84 dim_t N=max_id-min_id+1;
85 int local_N=N/s;
86 rest=N-local_N*s;
87 for (p=0; p<s; ++p) {
88 if (p<rest) {
89 distribution[p]=min_id+(local_N+1)*p;
90 out=local_N+1;
91 } else {
92 distribution[p]=min_id+rest+local_N*p;
93 }
94 }
95 distribution[s]=max_id+1;
96 if (rest==0) {
97 return local_N;
98 } else {
99 return local_N+1;
100 }
101 }
102
103 /* checks that there is no error accross all processes in a communicator */
104 /* NOTE : does not make guarentee consistency of error string on each process */
105 bool_t Paso_MPIInfo_noError( Paso_MPIInfo *mpi_info )
106 {
107 int errorLocal = (int)Paso_noError();
108 int errorGlobal=errorLocal;
109 if (mpi_info->size>1) {
110 #ifdef PASO_MPI
111 #if 0 /* ksteube disable error checking during benchmarking activities */
112 MPI_Allreduce( &errorLocal, &errorGlobal, 1, MPI_INT, MPI_LAND, mpi_info->comm );
113 #else
114 errorGlobal=errorLocal;
115 #endif
116 #else
117 errorGlobal=errorLocal;
118 #endif
119 /* take care of the case where the error was on another processor */
120 if( errorLocal && !errorGlobal )
121 Paso_setError( PASO_MPI_ERROR, "Paso_MPI_noError() : there was an error on another MPI process" );
122 }
123 return (bool_t) errorGlobal;
124 }
125
126
127 /**************************************************
128 WRAPPERS
129 **************************************************/
130
131 int Paso_MPIInfo_initialized( void )
132 {
133 int error=0, initialised=0;
134
135 #ifdef PASO_MPI
136 error = MPI_Initialized( &initialised );
137 if( error!=MPI_SUCCESS )
138 Paso_setError( PASO_MPI_ERROR, "mpi_initialised : MPI error" );
139 return initialised;
140 #else
141 return TRUE;
142 #endif
143 }

  ViewVC Help
Powered by ViewVC 1.1.26