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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1811 - (hide annotations)
Thu Sep 25 23:11:13 2008 UTC (11 years, 5 months ago) by ksteube
File size: 2495 byte(s)
Copyright updated in all files

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

  ViewVC Help
Powered by ViewVC 1.1.26