Browse Source

KiHa unit test expanded

pull/2/head
Trevor Irons 6 years ago
parent
commit
6673e45307

+ 7
- 1
Modules/FDEM1D/CMakeLists.txt View File

@@ -15,7 +15,13 @@ target_link_libraries(fdem1d "lemmacore")
15 15
 
16 16
 if ( KIHA_EM1D )
17 17
 	target_link_libraries(fdem1d "kihaem1d")
18
-	target_link_libraries(fdem1d "gfortran")
18
+	if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
19
+  		# using GCC
20
+		target_link_libraries(fdem1d "gfortran")
21
+	elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
22
+  		# using Intel C++
23
+		target_link_libraries(fdem1d "ifcore" "imf" "svml" "intlc")
24
+	endif()
19 25
 endif()
20 26
 
21 27
 # Testing

+ 50
- 0
Modules/FDEM1D/examples/Bench.sh View File

@@ -0,0 +1,50 @@
1
+#!/bin/bash
2
+
3
+# this is a simple benchmarking script for Hantenna 
4
+
5
+for i in {1..12}
6
+do
7
+   echo "Welcome $i times"
8
+   export OMP_NUM_THREADS=$i
9
+   ./Hantenna inp/trans.inp inp/cond.inp inp/points.inp inp/config.inp
10
+done
11
+
12
+sed -i -- 's/FHTKEY51/FHTKEY101/g' inp/config.inp 
13
+for i in {1..12}
14
+do
15
+   echo "Welcome $i times"
16
+   export OMP_NUM_THREADS=$i
17
+   ./Hantenna inp/trans.inp inp/cond.inp inp/points.inp inp/config.inp
18
+done
19
+
20
+sed -i -- 's/FHTKEY101/FHTKEY201/g' inp/config.inp 
21
+for i in {1..12}
22
+do
23
+   echo "Welcome $i times"
24
+   export OMP_NUM_THREADS=$i
25
+   ./Hantenna inp/trans.inp inp/cond.inp inp/points.inp inp/config.inp
26
+done
27
+
28
+sed -i -- 's/FHTKEY201/ANDERSON801/g' inp/config.inp 
29
+for i in {1..12}
30
+do
31
+   echo "Welcome $i times"
32
+   export OMP_NUM_THREADS=$i
33
+   ./Hantenna inp/trans.inp inp/cond.inp inp/points.inp inp/config.inp
34
+done
35
+
36
+sed -i -- 's/ANDERSON801/QWEKEY/g' inp/config.inp 
37
+for i in {1..12}
38
+do
39
+   echo "Welcome $i times"
40
+   export OMP_NUM_THREADS=$i
41
+   ./Hantenna inp/trans.inp inp/cond.inp inp/points.inp inp/config.inp
42
+done
43
+
44
+sed -i -- 's/QWEKEY/CHAVE/g' inp/config.inp 
45
+for i in {1..12}
46
+do
47
+   echo "Welcome $i times"
48
+   export OMP_NUM_THREADS=$i
49
+   ./Hantenna inp/trans.inp inp/cond.inp inp/points.inp inp/config.inp
50
+done

+ 1
- 0
Modules/FDEM1D/examples/CMakeLists.txt View File

@@ -41,3 +41,4 @@ INSTALL_TARGETS( "/share/FDEM1D/"
41 41
 )
42 42
 
43 43
 install (DIRECTORY inp  DESTINATION "${CMAKE_INSTALL_PREFIX}/share/FDEM1D/" )
44
+install (FILES  Bench.sh plottimings.py  DESTINATION "${CMAKE_INSTALL_PREFIX}/share/FDEM1D/" )

+ 8
- 0
Modules/FDEM1D/examples/Hantenna.cpp View File

@@ -213,6 +213,14 @@ const char *buildString = __DATE__ ", " __TIME__;
213 213
     }
214 214
     hreal.close();
215 215
     // Clean up
216
+
217
+    // report timings
218
+    #ifdef LEMMAUSEOMP
219
+    std::ofstream outfile;
220
+    outfile.open("timings.csv", std::ios_base::app);
221
+    outfile << compiler << "," << ver << "," <<  buildString << "," << config[0] << "," <<  omp_get_max_threads() << "," << paTime/60. << "\n";
222
+    #endif
223
+
216 224
 }
217 225
 
218 226
 std::vector<Real>  readinpfile(const std::string& fname) {

+ 2
- 2
Modules/FDEM1D/examples/inp/config.inp View File

@@ -1,9 +1,9 @@
1 1
 // Hankel Transform type uncomment desired
2
-//FHTKEY51       // Key's   51 point FHT
2
+FHTKEY51       // Key's   51 point FHT
3 3
 //FHTKEY101      // Key's  101 point FHT
4 4
 //FHTKEY201      // Key's  201 point FHT
5 5
 //FHTKONG61      // Kong's  61 point FHT
6
-FHTKONG121     // Kong's 121 point FHT
6
+//FHTKONG121     // Kong's 121 point FHT
7 7
 //FHTKONG241	 // Kong's 241 point FHT
8 8
 //ANDERSON801    // Anderson's 801 Point FHT, with lagged convolution
9 9
 //CHAVE          // Chave's Gaussian Quadrature

+ 69
- 0
Modules/FDEM1D/examples/plottimings.py View File

@@ -0,0 +1,69 @@
1
+import matplotlib.pyplot as plt 
2
+import numpy as np 
3
+from collections import defaultdict
4
+import cpuinfo # pip install py-cpuinfo
5
+
6
+light_grey = np.array([float(225)/float(255)]*3)
7
+
8
+def fixLeg(legend):
9
+    rect = legend.get_frame()
10
+    #rect.set_color('None')
11
+    rect.set_facecolor(light_grey)
12
+    rect.set_linewidth(0.0)
13
+    rect.set_alpha(1.0)
14
+
15
+def deSpine(ax1):
16
+    spines_to_remove = ['top', 'right']
17
+    for spine in spines_to_remove:
18
+        ax1.spines[spine].set_visible(False)
19
+    #ax1.xaxis.set_ticks_position('none')
20
+    #ax1.yaxis.set_ticks_position('none')
21
+    ax1.get_xaxis().tick_bottom()
22
+    ax1.get_yaxis().tick_left()
23
+
24
+timings = np.recfromtxt(r"timings.csv", delimiter=r",", encoding=None)
25
+BENCH = {} # defaultdict(str)
26
+
27
+for i,line in enumerate(timings): 
28
+
29
+    # check if compiler has been added already 
30
+    if line[0].strip() in BENCH:
31
+        pass
32
+    else:
33
+        BENCH[line[0].strip()] = {}
34
+        print(line[1])
35
+        BENCH[line[0].strip()]["version"] = line[1].strip()
36
+
37
+    # check to see if Hankel has been added already 
38
+    if line[4].strip() in BENCH[line[0].strip()]:
39
+        pass
40
+    else: 
41
+        BENCH[line[0].strip()][line[4].strip()] = {}
42
+        BENCH[line[0].strip()][line[4].strip()]["NP"] = []
43
+        BENCH[line[0].strip()][line[4].strip()]["Time"] = []
44
+        
45
+    BENCH[line[0].strip()][line[4].strip()]["NP"].append( line[5] )
46
+    BENCH[line[0].strip()][line[4].strip()]["Time"].append( line[6] )
47
+
48
+#print (BENCH)
49
+fig = plt.figure()
50
+
51
+for compiler in BENCH:    
52
+    print (compiler)
53
+    for hankel in BENCH[compiler]:   
54
+        if hankel != "version": 
55
+            plt.plot(BENCH[compiler][hankel]["NP"], BENCH[compiler][hankel]["Time"], ".-", label=str(hankel))
56
+
57
+plt.gca().set_yscale('log')    
58
+plt.gca().set_title( str(compiler) + " " + str(BENCH[compiler]["version"]) ) 
59
+plt.suptitle( cpuinfo.get_cpu_info()['brand'] ) 
60
+
61
+
62
+
63
+leg = plt.legend()
64
+
65
+deSpine(plt.gca())
66
+fixLeg(leg)
67
+
68
+plt.savefig("timings.png")
69
+plt.show()

+ 251
- 0
Modules/FDEM1D/testing/BenchKiHa.h View File

@@ -0,0 +1,251 @@
1
+/* This file is part of Lemma, a geophysical modelling and inversion API.
2
+ * More information is available at http://lemmasoftware.org
3
+ */
4
+
5
+/* This Source Code Form is subject to the terms of the Mozilla Public
6
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
7
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
+ */
9
+
10
+/**
11
+ * @file
12
+ * @date      02/01/19 22:34:11
13
+ * @version   $Id$
14
+ * @author    Trevor Irons (ti)
15
+ * @email     Trevor.Irons@utah.edu
16
+ * @copyright Copyright (c) 2019, University of Utah
17
+ * @copyright Copyright (c) 2019, Lemma Software, LLC
18
+ */
19
+
20
+#include <cxxtest/TestSuite.h>
21
+#include <FDEM1D>
22
+#include <random>
23
+#include "timer.h"
24
+
25
+#define EPSILON 1e-10
26
+using namespace Lemma;
27
+
28
+class MyAlgorithmTest : public CxxTest::TestSuite {
29
+   // Put in your usual, quick unit tests here
30
+};
31
+
32
+class MyAlgorithmTestPerformance : public CxxTest::TestSuite {
33
+
34
+public:
35
+
36
+    std::shared_ptr< DipoleSource > dipole;
37
+    std::shared_ptr< LayeredEarthEM > earth;
38
+    std::shared_ptr< FieldPoints > receivers;
39
+    std::shared_ptr< EMEarth1D > EmEarth;
40
+
41
+	jsw_timer timer;
42
+
43
+    Real Delta;
44
+
45
+    void setUp() {
46
+        // Put in any code needed to set up your test,
47
+        // but that should NOT be counted in the execution time.
48
+	    dipole = DipoleSource::NewSP();
49
+
50
+        //dipole->SetType(GROUNDEDELECTRICDIPOLE);
51
+		//dipole->SetPolarisation(XPOLARISATION);
52
+
53
+
54
+		dipole->SetNumberOfFrequencies(1);
55
+		dipole->SetMoment(1);
56
+		dipole->SetFrequency(0, 4400.1000);
57
+		//dipole->SetPhase(0);
58
+		//dipole->SetLocation( (VectorXr(3) << 49, 49, -1e-4).finished() );
59
+		dipole->SetLocation( 0, 0, -1e-4  );
60
+
61
+	    // Define model
62
+	    VectorXcr sigma(2);
63
+		sigma << 0., 1e-3;//, .1;//, .01, .001;
64
+	    VectorXr  thick(1);
65
+		thick << 10;//, 10, 10;
66
+
67
+	    earth = LayeredEarthEM::NewSP();
68
+        earth->SetNumberOfLayers(2);
69
+		earth->SetLayerConductivity(sigma);
70
+		//earth->SetLayerThickness(thick);
71
+
72
+	    receivers = FieldPoints::NewSP();
73
+		Vector3r loc;
74
+
75
+        Real ox = 2.;
76
+		Real oy = 2.;
77
+		Real oz = 2;
78
+
79
+		Real dx = 0.62;
80
+		Real dy = 0.62;
81
+		Real dz = 0.62;
82
+
83
+		int  nx = 20;
84
+		int  ny = 20;
85
+		int  nz = 20;
86
+        Delta = nx*ny*nz*1e-10;
87
+
88
+		receivers->SetNumberOfPoints(nx*ny*nz);
89
+		int ir = 0;
90
+		for (int ix=0; ix<nx; ++ix) {
91
+		for (int iy=0; iy<ny; ++iy) {
92
+		for (int iz=0; iz<nz; ++iz) {
93
+			loc << ox+ix*dx, oy+iy*dy, oz+iz*dz;
94
+            //std::cout << "Receiver location " << loc.transpose() << std::endl;
95
+			receivers->SetLocation(ir, loc);
96
+			//oz += dz;
97
+			++ ir;
98
+		}
99
+        }
100
+        }
101
+
102
+        EmEarth = EMEarth1D::NewSP();
103
+        EmEarth->SetHankelTransformMethod(CHAVE);
104
+        EmEarth->SetFieldsToCalculate(BOTH); // Fortran needs this
105
+		EmEarth->AttachDipoleSource(dipole);
106
+		EmEarth->AttachLayeredEarthEM(earth);
107
+		EmEarth->AttachFieldPoints(receivers);
108
+
109
+   }
110
+
111
+   void tearDown() {
112
+      // Clean-up code, also NOT counted in execution time.
113
+   }
114
+
115
+   void test_Hz() {
116
+
117
+        dipole->SetType(MAGNETICDIPOLE);
118
+		dipole->SetPolarisation(ZPOLARISATION);
119
+
120
+        // Put in a unit test that will be slow.
121
+	    std::cout << "C++\n";
122
+
123
+  	    timer.begin();
124
+	    EmEarth->MakeCalc3();
125
+	    Real lemmaTime = timer.end();
126
+
127
+        auto lc = receivers->GetEfield( 0 );
128
+
129
+        #ifdef KIHALEE_EM1D
130
+	    receivers->ClearFields();
131
+        std::cout << "\nFORTRAN KiHa\n";
132
+  	    timer.begin();
133
+ 	    EmEarth->MakeCalc();
134
+	    Real kihaTime = timer.end();
135
+
136
+        auto fc = receivers->GetEfield( 0 ); //0,0);
137
+
138
+        std::cout << "Lemma time:" << lemmaTime << "\tKiHa time:" << kihaTime << std::endl;
139
+
140
+        std::cout.precision(16);
141
+        std::cout << "Lemma norm |" << (lc).norm() << "|" << std::endl;
142
+        std::cout << "KiHa norm  |" << (fc).norm() << "|" << std::endl;
143
+        std::cout << "Difference norm |" << (lc - fc).norm() << "|" << std::endl;
144
+
145
+        TS_ASSERT_DELTA((lc-fc).norm(), 0.0, Delta);
146
+        #endif
147
+   }
148
+
149
+   void test_Hx() {
150
+
151
+        dipole->SetType(MAGNETICDIPOLE);
152
+		dipole->SetPolarisation(XPOLARISATION);
153
+
154
+        // Put in a unit test that will be slow.
155
+	    std::cout << "C++\n";
156
+
157
+  	    timer.begin();
158
+	    EmEarth->MakeCalc3();
159
+	    Real lemmaTime = timer.end();
160
+
161
+        auto lc = receivers->GetEfield( 0 );
162
+
163
+        #ifdef KIHALEE_EM1D
164
+	    receivers->ClearFields();
165
+        std::cout << "\nFORTRAN KiHa\n";
166
+  	    timer.begin();
167
+ 	    EmEarth->MakeCalc();
168
+	    Real kihaTime = timer.end();
169
+
170
+        auto fc = receivers->GetEfield( 0 ); //0,0);
171
+
172
+        std::cout << "Lemma time:" << lemmaTime << "\tKiHa time:" << kihaTime << std::endl;
173
+
174
+        std::cout.precision(16);
175
+        std::cout << "Lemma norm |" << (lc).norm() << "|" << std::endl;
176
+        std::cout << "KiHa norm  |" << (fc).norm() << "|" << std::endl;
177
+        std::cout << "Difference norm |" << (lc - fc).norm() << "|" << std::endl;
178
+
179
+        TS_ASSERT_DELTA((lc-fc).norm(), 0.0, Delta);
180
+        #endif
181
+   }
182
+
183
+   void test_Hy() {
184
+
185
+        dipole->SetType(MAGNETICDIPOLE);
186
+		dipole->SetPolarisation(YPOLARISATION);
187
+
188
+        // Put in a unit test that will be slow.
189
+	    std::cout << "C++\n";
190
+
191
+  	    timer.begin();
192
+	    EmEarth->MakeCalc3();
193
+	    Real lemmaTime = timer.end();
194
+
195
+        auto lc = receivers->GetEfield( 0 );
196
+
197
+        #ifdef KIHALEE_EM1D
198
+	    receivers->ClearFields();
199
+        std::cout << "\nFORTRAN KiHa\n";
200
+  	    timer.begin();
201
+ 	    EmEarth->MakeCalc();
202
+	    Real kihaTime = timer.end();
203
+
204
+        auto fc = receivers->GetEfield( 0 ); //0,0);
205
+
206
+        std::cout << "Lemma time:" << lemmaTime << "\tKiHa time:" << kihaTime << std::endl;
207
+
208
+        std::cout.precision(16);
209
+        std::cout << "Lemma norm |" << (lc).norm() << "|" << std::endl;
210
+        std::cout << "KiHa norm  |" << (fc).norm() << "|" << std::endl;
211
+        std::cout << "Difference norm |" << (lc - fc).norm() << "|" << std::endl;
212
+
213
+        TS_ASSERT_DELTA((lc-fc).norm(), 0.0, Delta);
214
+        #endif
215
+   }
216
+
217
+   void test_Ex() {
218
+
219
+        dipole->SetType(GROUNDEDELECTRICDIPOLE);
220
+		dipole->SetPolarisation(XPOLARISATION);
221
+
222
+        // Put in a unit test that will be slow.
223
+	    std::cout << "C++\n";
224
+
225
+  	    timer.begin();
226
+	    EmEarth->MakeCalc3();
227
+	    Real lemmaTime = timer.end();
228
+
229
+        auto lc = receivers->GetEfield( 0 );
230
+
231
+        #ifdef KIHALEE_EM1D
232
+	    receivers->ClearFields();
233
+        std::cout << "\nFORTRAN KiHa\n";
234
+  	    timer.begin();
235
+ 	    EmEarth->MakeCalc();
236
+	    Real kihaTime = timer.end();
237
+
238
+        auto fc = receivers->GetEfield( 0 ); //0,0);
239
+
240
+        std::cout << "Lemma time:" << lemmaTime << "\tKiHa time:" << kihaTime << std::endl;
241
+
242
+        std::cout.precision(16);
243
+        std::cout << "Lemma norm |" << (lc).norm() << "|" << std::endl;
244
+        std::cout << "KiHa norm  |" << (fc).norm() << "|" << std::endl;
245
+        std::cout << "Difference norm |" << (lc - fc).norm() << "|" << std::endl;
246
+
247
+        TS_ASSERT_DELTA((lc-fc).norm(), 0.0, Delta);
248
+        #endif
249
+   }
250
+
251
+};

+ 7
- 3
Modules/FDEM1D/testing/CMakeLists.txt View File

@@ -6,9 +6,13 @@ target_link_libraries(unittest_FEM1D_GetNameCheck "lemmacore" "fdem1d" "yaml-cpp
6 6
 CXXTEST_ADD_TEST(unittest_FEM1D_SerializeCheck SerializeCheck.cc ${CMAKE_CURRENT_SOURCE_DIR}/SerializeCheck.h)
7 7
 target_link_libraries(unittest_FEM1D_SerializeCheck "lemmacore" "fdem1d" "yaml-cpp")
8 8
 
9
+CXXTEST_ADD_TEST(benchKiHa BenchKiHa.cc ${CMAKE_CURRENT_SOURCE_DIR}/BenchKiHa.h)
10
+target_link_libraries(benchKiHa "lemmacore" "fdem1d" "yaml-cpp")
11
+
9 12
 set_target_properties(unittest_FEM1D_GetNameCheck
10
-	              unittest_FEM1D_SerializeCheck 
11
-       	PROPERTIES 
13
+	                  unittest_FEM1D_SerializeCheck 
14
+                      benchKiHa
15
+    PROPERTIES 
12 16
 	CXX_STANDARD 14
13 17
 	CXX_STANDARD_REQUIRED ON
14
-	)
18
+)

Loading…
Cancel
Save