Skip to content

Commit c56d424

Browse files
committed
Update 1d_HeatTransfer code sample according to the new guideline.
1 parent d80d94a commit c56d424

File tree

1 file changed

+36
-39
lines changed

1 file changed

+36
-39
lines changed

DirectProgramming/DPC++/StructuredGrids/1d_HeatTransfer/src/1d_HeatTransfer.cpp

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@
3434
//
3535
//******************************************************************************
3636
#include <CL/sycl.hpp>
37-
#include <dpc_common.hpp>
3837
#include <fstream>
3938
#include <iomanip>
4039
#include <iostream>
40+
#include "dpc_common.hpp"
4141

4242
using namespace sycl;
43+
using namespace std;
4344

4445
constexpr float dt = 0.002f;
4546
constexpr float dx = 0.01f;
@@ -49,12 +50,12 @@ constexpr float temp = 100.0f; // Initial temperature.
4950
//************************************
5051
// Function description: display input parameters used for this sample.
5152
//************************************
52-
void Usage(std::string programName) {
53-
std::cout << " Incorrect parameters \n";
54-
std::cout << " Usage: ";
55-
std::cout << programName << " <n> <i>\n\n";
56-
std::cout << " n : Number of points to simulate \n";
57-
std::cout << " i : Number of timesteps \n";
53+
void Usage(string programName) {
54+
cout << " Incorrect parameters \n";
55+
cout << " Usage: ";
56+
cout << programName << " <n> <i>\n\n";
57+
cout << " n : Number of points to simulate \n";
58+
cout << " i : Number of timesteps \n";
5859
}
5960

6061
//************************************
@@ -75,43 +76,40 @@ float* ComputeHeatDeviceParallel(float* arr, float* arr_next, float C,
7576
try {
7677
// Define the device queue
7778
queue q = default_selector{};
78-
std::cout << "Kernel runs on "
79-
<< q.get_device().get_info<info::device::name>() << "\n";
79+
cout << "Kernel runs on " << q.get_device().get_info<info::device::name>()
80+
<< "\n";
8081

8182
// Set boundary condition at one end.
8283
arr[0] = arr_next[0] = temp;
8384

8485
float* current_data_ptr = arr;
8586
float* next_data_ptr = arr_next;
86-
// current_data_ptr = arr;
87-
// next_data_ptr = arr_next;
8887

8988
// Buffer scope
9089
{
91-
buffer<float, 1> arr_buf(current_data_ptr, range<1>{num_p + 2});
92-
buffer<float, 1> arr_next_buf(next_data_ptr, range<1>{num_p + 2});
90+
buffer temperature_buf(current_data_ptr, range(num_p + 2));
91+
buffer temperature_next_buf(next_data_ptr, range(num_p + 2));
9392

9493
// Iterate over timesteps
9594
for (i = 1; i <= num_iter; i++) {
9695
if (i % 2 != 0) {
97-
q.submit([&](handler& h) {
96+
q.submit([&](auto& h) {
9897
// The size of memory amount that will be given to the buffer.
9998
range<1> num_items{num_p + 2};
10099

101-
auto arr_acc = arr_buf.get_access<access::mode::read_write>(h);
102-
auto arr_next_acc =
103-
arr_next_buf.get_access<access::mode::read_write>(h);
100+
accessor temperature(temperature_buf, h);
101+
accessor temperature_next(temperature_next_buf, h);
104102

105103
h.parallel_for(num_items, [=](id<1> k) {
106104
size_t gid = k.get(0);
107105

108106
if (gid == 0) {
109107
} else if (gid == num_p + 1) {
110-
arr_next_acc[k] = arr_acc[k - 1];
108+
temperature_next[k] = temperature[k - 1];
111109
} else {
112-
arr_next_acc[k] =
113-
C * (arr_acc[k + 1] - 2 * arr_acc[k] + arr_acc[k - 1]) +
114-
arr_acc[k];
110+
temperature_next[k] =
111+
C * (temperature[k + 1] - 2 * temperature[k] + temperature[k - 1]) +
112+
temperature[k];
115113
}
116114
}); // end parallel for loop in kernel1
117115
}); // end device queue
@@ -121,20 +119,19 @@ float* ComputeHeatDeviceParallel(float* arr, float* arr_next, float C,
121119
// The size of memory amount that will be given to the buffer.
122120
range<1> num_items{num_p + 2};
123121

124-
auto arr_acc = arr_buf.get_access<access::mode::read_write>(h);
125-
auto arr_next_acc =
126-
arr_next_buf.get_access<access::mode::read_write>(h);
122+
accessor temperature(temperature_buf, h);
123+
accessor temperature_next(temperature_next_buf, h);
127124

128125
h.parallel_for(num_items, [=](id<1> k) {
129126
size_t gid = k.get(0);
130127

131128
if (gid == 0) {
132129
} else if (gid == num_p + 1) {
133-
arr_acc[k] = arr_next_acc[k - 1];
130+
temperature[k] = temperature_next[k - 1];
134131
} else {
135-
arr_acc[k] = C * (arr_next_acc[k + 1] - 2 * arr_next_acc[k] +
136-
arr_next_acc[k - 1]) +
137-
arr_next_acc[k];
132+
temperature[k] = C * (temperature_next[k + 1] - 2 * temperature_next[k] +
133+
temperature_next[k - 1]) +
134+
temperature_next[k];
138135
}
139136
}); // end parallel for loop in kernel2
140137
}); // end device queue
@@ -144,8 +141,8 @@ float* ComputeHeatDeviceParallel(float* arr, float* arr_next, float C,
144141

145142
q.wait_and_throw();
146143

147-
} catch (cl::sycl::exception e) {
148-
std::cout << "SYCL exception caught: " << e.what() << "\n";
144+
} catch (sycl::exception e) {
145+
cout << "SYCL exception caught: " << e.what() << "\n";
149146
}
150147

151148
if (i % 2 != 0)
@@ -197,7 +194,7 @@ bool CompareResults(float* device_results, float* host_results,
197194
double norm2 = 0;
198195
bool err = false;
199196

200-
std::ofstream err_file;
197+
ofstream err_file;
201198
err_file.open("error_diff.txt");
202199

203200
err_file << " \t idx\theat[i]\t\theat_CPU[i] \n";
@@ -225,16 +222,16 @@ int main(int argc, char* argv[]) {
225222

226223
// Read input parameters
227224
try {
228-
n_point = std::stoi(argv[1]);
229-
n_iteration = std::stoi(argv[2]);
225+
n_point = stoi(argv[1]);
226+
n_iteration = stoi(argv[2]);
230227

231228
} catch (...) {
232229
Usage(argv[0]);
233230
return (-1);
234231
}
235232

236-
std::cout << "Number of points: " << n_point << "\n";
237-
std::cout << "Number of iterations: " << n_iteration << "\n";
233+
cout << "Number of points: " << n_point << "\n";
234+
cout << "Number of iterations: " << n_iteration << "\n";
238235

239236
// Array heat and heat_next arrays store temperatures of the current and next
240237
// iteration of n_point (calculated in kernel)
@@ -260,7 +257,7 @@ int main(int argc, char* argv[]) {
260257
ComputeHeatDeviceParallel(heat, heat_next, C, n_point, n_iteration, temp);
261258

262259
// Display time used by device
263-
std::cout << "Kernel time: " << t_par.Elapsed() << " sec\n";
260+
cout << "Elapsed time: " << t_par.Elapsed() << " sec\n";
264261

265262
// Compute heat in CPU in (for comparision)
266263
float* final_CPU = NULL;
@@ -273,10 +270,10 @@ int main(int argc, char* argv[]) {
273270
bool err = CompareResults(final_device, final_CPU, n_point, C);
274271

275272
if (err == true)
276-
std::cout << "Please check the error_diff.txt file ...\n";
273+
cout << "Please check the error_diff.txt file ...\n";
277274
else
278-
std::cout << "PASSED! There is no difference between the results computed "
279-
"in host and in kernel.\n";
275+
cout << "PASSED! There is no difference between the results computed "
276+
"in host and in kernel.\n";
280277

281278
// Cleanup
282279
delete[] heat;

0 commit comments

Comments
 (0)