-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkokkos_sample.cpp
More file actions
187 lines (160 loc) · 6.65 KB
/
Copy pathkokkos_sample.cpp
File metadata and controls
187 lines (160 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <iostream>
#include <iomanip>
#include <fstream>
#include <filesystem>
#include <cmath>
#include <chrono>
#include <Kokkos_Core.hpp>
#include <cstdlib>
#include "FDTD_kokkos.h"
#include "FDTD_PML_kokkos.h"
#define __PML_TEST__
using namespace FDTD_kokkos;
void spherical_wave(int n, int it, const std::string base_path = "../../PlotScript/") {
CurrentParameters cur_param {
8,
4,
0.2
};
double T = cur_param.period;
double Tx = cur_param.period_x;
double Ty = cur_param.period_y;
double Tz = cur_param.period_z;
cur_param.iterations = static_cast<int>(static_cast<double>(cur_param.period) / cur_param.dt);
std::function<double(double, double, double, double)> cur_func =
[T, Tx, Ty, Tz](double x, double y, double z, double t) {
return sin(2.0 * FDTD_const::PI * t / T)
* pow(cos(2.0 * FDTD_const::PI * x / Tx), 2.0)
* pow(cos(2.0 * FDTD_const::PI * y / Ty), 2.0)
* pow(cos(2.0 * FDTD_const::PI * z / Tz), 2.0);
};
double d = FDTD_const::C;
double boundary = static_cast<double>(n) / 2.0 * d;
Parameters params {
n, // Ni
n, // Nj
n, // Nk
-boundary, // x_min
boundary, // x_max
-boundary, // y_min
boundary, // y_max
-boundary, // z_min
boundary, // z_max
d, // dx
d, // dy
d // dz
};
FDTD_kokkos::FDTD method(params, cur_param.dt);
int cur_time = std::min(cur_param.iterations, it);
int start_i = static_cast<int>(floor((-Tx / 4.0 - params.ax) / params.dx));
int start_j = static_cast<int>(floor((-Ty / 4.0 - params.ay) / params.dy));
int start_k = static_cast<int>(floor((-Tz / 4.0 - params.az) / params.dz));
int max_i = static_cast<int>(floor((Tx / 4.0 - params.ax) / params.dx));
int max_j = static_cast<int>(floor((Ty / 4.0 - params.ay) / params.dy));
int max_k = static_cast<int>(floor((Tz / 4.0 - params.az) / params.dz));
auto start = std::chrono::high_resolution_clock::now();
for (int t = 0; t < cur_time; t++) {
for (int i = start_i; i < max_i; ++i) {
for (int j = start_j; j < max_j; ++j) {
for (int k = start_k; k < max_k; ++k) {
int index = i + j * params.Ni + k * params.Ni * params.Nj;
double value = cur_func(static_cast<double>(i) * params.dx,
static_cast<double>(j) * params.dy,
static_cast<double>(k) * params.dz,
static_cast<double>(t + 1) * cur_param.dt);
method.get_field(Component::JX)[index] = value;
method.get_field(Component::JY)[index] = value;
method.get_field(Component::JZ)[index] = value;
}
}
}
method.update_fields();
}
method.zeroed_currents();
for (int t = cur_time; t < it; t++) {
method.update_fields();
}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = end - start;
std::cout << "Execution time: " << elapsed.count() << " s" << std::endl;
#ifdef __PML_TEST__
FDTD_kokkos::FDTD_PML pml_method(params, cur_param.dt, 0.2);
auto start_pml = std::chrono::high_resolution_clock::now();
for (int t = 0; t < cur_time; t++) {
for (int i = start_i; i < max_i; ++i) {
for (int j = start_j; j < max_j; ++j) {
for (int k = start_k; k < max_k; ++k) {
int index = i + j * params.Ni + k * params.Ni * params.Nj;
double value = cur_func(static_cast<double>(i) * params.dx,
static_cast<double>(j) * params.dy,
static_cast<double>(k) * params.dz,
static_cast<double>(t + 1) * cur_param.dt);
pml_method.get_field(Component::JX)[index] = value;
pml_method.get_field(Component::JY)[index] = value;
pml_method.get_field(Component::JZ)[index] = value;
}
}
}
pml_method.update_fields();
}
pml_method.zeroed_currents();
for (int t = cur_time; t < it; t++) {
pml_method.update_fields();
}
auto end_pml = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed_pml = end_pml - start_pml;
std::cout << "Execution time (PML): " << elapsed_pml.count() << " s" << std::endl;
#endif //__PML_TEST__
int k = params.Nk/2;
for (int j = params.Nj/2 - 5; j < params.Nj/2 + 5; j++) {
for (int i = params.Ni/2 - 5; i < params.Ni/2 + 5; i++) {
int index = i + j * params.Ni + k * params.Ni * params.Nj;
std::cout << std::setw(12) << std::fixed << std::setprecision(5)
<< method.get_field(Component::EX)[index];
}
std::cout << std::endl;
}
std::cout << std::endl;
#ifdef __PML_TEST__
std::cout << "PML: \n" << std::endl;
for (int j = params.Nj/2 - 5; j < params.Nj/2 + 5; j++) {
for (int i = params.Ni/2 - 5; i < params.Ni/2 + 5; i++) {
int index = i + j * params.Ni + k * params.Ni * params.Nj;
std::cout << std::setw(12) << std::fixed << std::setprecision(5)
<< pml_method.get_field(Component::EX)[index];
}
std::cout << std::endl;
}
std::cout << std::endl;
#endif //__PML_TEST__
}
int main(int argc, char* argv[]) {
Kokkos::initialize(argc, argv); {
if (Kokkos::hwloc::available())
std::cout << "hwloc available" << std::endl;
std::ostringstream msg;
msg << "hwloc( NUMA[" << Kokkos::hwloc::get_available_numa_count() << "] x CORE["
<< Kokkos::hwloc::get_available_cores_per_numa() << "] x HT["
<< Kokkos::hwloc::get_available_threads_per_core() << "] )" << std::endl;
Kokkos::print_configuration(msg);
std::cout << msg.str();
std::ifstream source_fin;
std::vector<char*> arguments(argv, argv + argc);
if (argc == 1) {
int N = 32;
int Iterations = 100;
spherical_wave(N, Iterations, "../../");
}
else if (argc == 3) {
int N = std::atoi(arguments[1]);
int Iterations = std::atoi(arguments[2]);
spherical_wave(N, Iterations, "");
}
else {
std::cout << "ERROR: Incorrect number of parameters" << std::endl;
exit(1);
}
}
Kokkos::finalize();
return 0;
}