/[escript]/trunk/esysUtils/src/Esys_MPI.c
ViewVC logotype

Contents of /trunk/esysUtils/src/Esys_MPI.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3792 - (show annotations)
Wed Feb 1 06:16:25 2012 UTC (6 years, 11 months ago) by caltinay
File MIME type: text/plain
File size: 4128 byte(s)
Merged ripley rectangular domain into trunk.

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
9 * Licensed under the Open Software License version 3.0
10 * http://www.opensource.org/licenses/osl-3.0.php
11 *
12 *******************************************************/
13
14
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18
19
20 #include "Esys_MPI.h"
21 #include "index.h"
22 #include "mem.h"
23 #include "error.h"
24
25
26 /* allocate memory for an mpi_comm, and find the communicator details */
27 Esys_MPIInfo* Esys_MPIInfo_alloc( MPI_Comm comm )
28 {
29 #ifdef ESYS_MPI
30 int error;
31 #endif
32
33 Esys_MPIInfo *out=NULL;
34
35 out = MEMALLOC( 1, Esys_MPIInfo );
36
37 out->reference_counter = 0;
38 out->msg_tag_counter = 0;
39 #ifdef ESYS_MPI
40 error = MPI_Comm_rank( comm, &out->rank )==MPI_SUCCESS && MPI_Comm_size( comm, &out->size )==MPI_SUCCESS;
41 if( !error ) {
42 Esys_setError( ESYS_MPI_ERROR, "Esys_MPIInfo_alloc : error finding comm rank/size" );
43 }
44
45 out->comm = comm;
46 #else
47 out->rank=0;
48 out->size=1;
49 out->comm=-1;
50 #endif
51 out->reference_counter++;
52
53 return out;
54 }
55
56 /* free memory for an mpi_comm */
57 void Esys_MPIInfo_free( Esys_MPIInfo *in )
58 {
59 if( in && !(--in->reference_counter) )
60 MEMFREE( in );
61 }
62
63 Esys_MPIInfo *Esys_MPIInfo_getReference( Esys_MPIInfo* in )
64 {
65 if (in!=NULL)
66 ++(in->reference_counter);
67
68 return in;
69 }
70 /* N = #CPUs, k is a CPU number but out of range or even negative. Return a CPU number in 0...n-1. */
71 index_t Esys_MPIInfo_mod(index_t n, index_t k)
72 {
73 index_t q, out=0;
74 if (n>1) {
75 q=k/n;
76 if (k>0) {
77 out=k-n*q;
78 } else if (k<0) {
79 out=k-n*(q-1);
80 }
81 }
82 return out;
83 }
84
85 void Esys_MPIInfo_Split( Esys_MPIInfo *mpi_info, dim_t N, dim_t* local_N,index_t* offset)
86 {
87 int rest=0;
88 int s=mpi_info->size;
89 int r=mpi_info->rank;
90 *local_N=N/s;
91 rest=N-(*local_N)*s;
92 if (r<rest) {
93 (*local_N)++;
94 (*offset)=(*local_N)*r;
95 } else {
96 (*offset)=(*local_N)*r+rest;
97 }
98 }
99
100
101 dim_t Esys_MPIInfo_setDistribution(Esys_MPIInfo* mpi_info ,index_t min_id,index_t max_id,index_t* distribution) {
102 int rest=0, p;
103 int s=mpi_info->size;
104 dim_t N=max_id-min_id+1;
105 if (N>0) {
106 int local_N=N/s;
107 rest=N-local_N*s;
108 for (p=0; p<s; ++p) {
109 if (p<rest) {
110 distribution[p]=min_id+(local_N+1)*p;
111 } else {
112 distribution[p]=min_id+rest+local_N*p;
113 }
114 }
115 distribution[s]=max_id+1;
116 if (rest==0) {
117 return local_N;
118 } else {
119 return local_N+1;
120 }
121 } else {
122 for (p=0; p<s+1; ++p) distribution[p]=min_id;
123 return 0;
124 }
125 }
126
127 /* checks that there is no error accross all processes in a communicator */
128 /* NOTE : does not make guarentee consistency of error string on each process */
129 bool_t Esys_MPIInfo_noError( Esys_MPIInfo *mpi_info )
130 {
131 int errorLocal = Esys_noError() ? 0 : 1;
132 int errorGlobal = errorLocal;
133
134 return (errorGlobal==0);
135 }
136
137 /**************************************************
138 WRAPPERS
139 **************************************************/
140
141 int Esys_MPIInfo_initialized( void )
142 {
143 #ifdef ESYS_MPI
144 int error=0, initialised=0;
145 error = MPI_Initialized( &initialised );
146 if( error!=MPI_SUCCESS )
147 Esys_setError( ESYS_MPI_ERROR, "mpi_initialised : MPI error" );
148 return initialised;
149 #else
150 return TRUE;
151 #endif
152 }
153
154 /* Append MPI rank to file name if multiple MPI processes */
155 char *Esys_MPI_appendRankToFileName(const char *fileName, int mpi_size, int mpi_rank) {
156 /* Make plenty of room for the mpi_rank number and terminating '\0' */
157 char *newFileName = TMPMEMALLOC(strlen(fileName)+20,char);
158 strncpy(newFileName, fileName, strlen(fileName)+1);
159 if (mpi_size>1) sprintf(newFileName+strlen(newFileName), ".%04d", mpi_rank);
160 return(newFileName);
161 }
162
163 #ifndef _OPENMP
164 int serial_get_max_threads(void) {
165 return 1;
166 }
167 int serial_get_thread_num(void) {
168 return 0;
169 }
170 #endif
171

Properties

Name Value
svn:mergeinfo /branches/lapack2681/paso/src/Paso_MPI.c:2682-2741 /branches/pasowrap/esysUtils/src/Esys_MPI.c:3661-3674 /branches/restext/paso/src/Paso_MPI.c:2610-2624 /branches/ripleygmg_from_3668/esysUtils/src/Esys_MPI.c:3669-3791 /branches/stage3.0/paso/src/Paso_MPI.c:2569-2590 /release/3.0/paso/src/Paso_MPI.c:2591-2601

  ViewVC Help
Powered by ViewVC 1.1.26