/[escript]/trunk/escript/src/Utils.cpp
ViewVC logotype

Annotation of /trunk/escript/src/Utils.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2607 - (hide annotations)
Tue Aug 18 01:02:56 2009 UTC (10 years, 7 months ago) by jfenwick
File size: 3259 byte(s)
Added getMPIWorldSum function to the esys.escript module.
This function takes an integer from each member of the MPIWorld.
This will hopefully address mantis issue 359

Added unit tests for most of the c++ free functions in the module.



1 gross 391
2 ksteube 1312 /*******************************************************
3 ksteube 1811 *
4 jfenwick 2548 * Copyright (c) 2003-2009 by University of Queensland
5 ksteube 1811 * 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 ksteube 1312
14 ksteube 1811
15 ksteube 1806 #include <string.h>
16    
17 jgs 474 #include "Utils.h"
18 gross 797 #include "DataVector.h"
19 gross 391
20 jgs 478 #ifdef _OPENMP
21     #include <omp.h>
22     #endif
23    
24 ksteube 1561 #ifdef PASO_MPI
25     #include <mpi.h>
26     #endif
27    
28 phornby 1628 #ifdef _WIN32
29     #include <WinSock2.h>
30 phornby 1835 #else
31     #include <unistd.h>
32 phornby 1628 #endif
33    
34 gross 391 namespace escript {
35    
36 ksteube 1247 int getSvnVersion()
37     {
38     #ifdef SVN_VERSION
39     return SVN_VERSION;
40     #else
41     return 0;
42     #endif
43     }
44    
45 ksteube 1620 /* This is probably not very robust, but it works on Savanna today and is useful for performance analysis */
46     int get_core_id() {
47     int processor_num=-1;
48     #ifdef CORE_ID1
49     FILE *fp;
50     int i, count_spaces=0;
51     char fname[100];
52     char buf[1000];
53    
54     sprintf(fname, "/proc/%d/stat", getpid());
55     fp = fopen(fname, "r");
56     if (fp == NULL) return(-1);
57     fgets(buf, 1000, fp);
58     fclose(fp);
59    
60     for (i=strlen(buf)-1; i>=0; i--) {
61     if (buf[i] == ' ') count_spaces++;
62     if (count_spaces == 4) break;
63     }
64     processor_num = atoi(&buf[i+1]);
65     #endif
66     return(processor_num);
67     }
68    
69    
70 ksteube 1561 void printParallelThreadCnt()
71     {
72     int mpi_iam=0, mpi_num=1;
73 ksteube 1568 char hname[64];
74 ksteube 1561
75 ksteube 1705 #ifdef HAVE_GETHOSTNAME
76 ksteube 1568 gethostname(hname, 64);
77 ksteube 1806 hname[63] = '\0';
78 ksteube 1705 #else
79     strcpy(hname, "unknown host");
80     #endif
81 ksteube 1567
82 ksteube 1561 #ifdef PASO_MPI
83     MPI_Comm_rank(MPI_COMM_WORLD, &mpi_iam);
84     MPI_Comm_size(MPI_COMM_WORLD, &mpi_num);
85     #endif
86    
87     #pragma omp parallel
88     {
89     int omp_iam=0, omp_num=1;
90     #ifdef _OPENMP
91     omp_iam = omp_get_thread_num(); /* Call in a parallel region */
92     omp_num = omp_get_num_threads();
93     #endif
94 jfenwick 2607 #pragma omp critical (printthrdcount)
95 ksteube 1620 printf("printParallelThreadCounts: MPI=%03d/%03d OpenMP=%03d/%03d running on %s core %d\n",
96     mpi_iam, mpi_num, omp_iam, omp_num, hname, get_core_id());
97 ksteube 1561 }
98     }
99    
100 gross 391 void setNumberOfThreads(const int num_threads)
101     {
102    
103     #ifdef _OPENMP
104     omp_set_num_threads(num_threads);
105     #endif
106    
107     }
108    
109     int getNumberOfThreads()
110     {
111     #ifdef _OPENMP
112     return omp_get_max_threads();
113     #else
114     return 1;
115     #endif
116    
117     }
118    
119 gross 2313 ESCRIPT_DLL_API int getMPISizeWorld() {
120 ksteube 1805 int mpi_num = 1;
121     #ifdef PASO_MPI
122     MPI_Comm_size(MPI_COMM_WORLD, &mpi_num);
123     #endif
124     return mpi_num;
125     }
126    
127 gross 2313 ESCRIPT_DLL_API int getMPIRankWorld() {
128 ksteube 1805 int mpi_iam = 0;
129     #ifdef PASO_MPI
130     MPI_Comm_rank(MPI_COMM_WORLD, &mpi_iam);
131     #endif
132     return mpi_iam;
133     }
134    
135 gross 2308 ESCRIPT_DLL_API int getMPIWorldMax(const int val) {
136     #ifdef PASO_MPI
137     int val2 = val;
138     int out = val;
139     MPI_Allreduce( &val2, &out, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD );
140     #else
141     int out = val;
142     #endif
143     return out;
144     }
145    
146 jfenwick 2607 ESCRIPT_DLL_API int getMPIWorldSum(const int val) {
147     #ifdef PASO_MPI
148     int val2 = val;
149     int out = 0;
150     MPI_Allreduce( &val2, &out, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
151     #else
152     int out = val;
153     #endif
154     return out;
155     }
156 gross 2308
157 gross 2313 ESCRIPT_DLL_API double getMachinePrecision() {
158 gross 2100 return DBL_EPSILON;
159     }
160 gross 2313 ESCRIPT_DLL_API double getMaxFloat() {
161 gross 2100 return DBL_MAX;
162     }
163 gross 2313 ESCRIPT_DLL_API void MPIBarrierWorld() {
164     #ifdef PASO_MPI
165     MPI_Barrier(MPI_COMM_WORLD );
166     #endif
167     }
168 gross 2100
169    
170 gross 391 } // end of namespace

  ViewVC Help
Powered by ViewVC 1.1.26