/[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 3793 - (show annotations)
Wed Feb 1 07:39:43 2012 UTC (7 years, 6 months ago) by gross
File MIME type: text/plain
File size: 4142 byte(s)
new implementation of FCT solver with some modifications to the python interface
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!=NULL) {
60 if (!(--in->reference_counter) ) MEMFREE( in );
61 }
62 }
63
64 Esys_MPIInfo *Esys_MPIInfo_getReference( Esys_MPIInfo* in )
65 {
66 if (in!=NULL)
67 ++(in->reference_counter);
68
69 return in;
70 }
71 /* N = #CPUs, k is a CPU number but out of range or even negative. Return a CPU number in 0...n-1. */
72 index_t Esys_MPIInfo_mod(index_t n, index_t k)
73 {
74 index_t q, out=0;
75 if (n>1) {
76 q=k/n;
77 if (k>0) {
78 out=k-n*q;
79 } else if (k<0) {
80 out=k-n*(q-1);
81 }
82 }
83 return out;
84 }
85
86 void Esys_MPIInfo_Split( Esys_MPIInfo *mpi_info, dim_t N, dim_t* local_N,index_t* offset)
87 {
88 int rest=0;
89 int s=mpi_info->size;
90 int r=mpi_info->rank;
91 *local_N=N/s;
92 rest=N-(*local_N)*s;
93 if (r<rest) {
94 (*local_N)++;
95 (*offset)=(*local_N)*r;
96 } else {
97 (*offset)=(*local_N)*r+rest;
98 }
99 }
100
101
102 dim_t Esys_MPIInfo_setDistribution(Esys_MPIInfo* mpi_info ,index_t min_id,index_t max_id,index_t* distribution) {
103 int rest=0, p;
104 int s=mpi_info->size;
105 dim_t N=max_id-min_id+1;
106 if (N>0) {
107 int local_N=N/s;
108 rest=N-local_N*s;
109 for (p=0; p<s; ++p) {
110 if (p<rest) {
111 distribution[p]=min_id+(local_N+1)*p;
112 } else {
113 distribution[p]=min_id+rest+local_N*p;
114 }
115 }
116 distribution[s]=max_id+1;
117 if (rest==0) {
118 return local_N;
119 } else {
120 return local_N+1;
121 }
122 } else {
123 for (p=0; p<s+1; ++p) distribution[p]=min_id;
124 return 0;
125 }
126 }
127
128 /* checks that there is no error accross all processes in a communicator */
129 /* NOTE : does not make guarentee consistency of error string on each process */
130 bool_t Esys_MPIInfo_noError( Esys_MPIInfo *mpi_info )
131 {
132 int errorLocal = Esys_noError() ? 0 : 1;
133 int errorGlobal = errorLocal;
134
135 return (errorGlobal==0);
136 }
137
138 /**************************************************
139 WRAPPERS
140 **************************************************/
141
142 int Esys_MPIInfo_initialized( void )
143 {
144 #ifdef ESYS_MPI
145 int error=0, initialised=0;
146 error = MPI_Initialized( &initialised );
147 if( error!=MPI_SUCCESS )
148 Esys_setError( ESYS_MPI_ERROR, "mpi_initialised : MPI error" );
149 return initialised;
150 #else
151 return TRUE;
152 #endif
153 }
154
155 /* Append MPI rank to file name if multiple MPI processes */
156 char *Esys_MPI_appendRankToFileName(const char *fileName, int mpi_size, int mpi_rank) {
157 /* Make plenty of room for the mpi_rank number and terminating '\0' */
158 char *newFileName = TMPMEMALLOC(strlen(fileName)+20,char);
159 strncpy(newFileName, fileName, strlen(fileName)+1);
160 if (mpi_size>1) sprintf(newFileName+strlen(newFileName), ".%04d", mpi_rank);
161 return(newFileName);
162 }
163
164 #ifndef _OPENMP
165 int serial_get_max_threads(void) {
166 return 1;
167 }
168 int serial_get_thread_num(void) {
169 return 0;
170 }
171 #endif
172

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