Skip to content

Commit

Permalink
clang-format applied per book needs (Apress#66)
Browse files Browse the repository at this point in the history
* Formatted using find . -name "*cpp" -exec clang-format -i --style="{BasedOnStyle: Google, ColumnLimit: 60}" {} \;

* fix include ordering issue caused by clang-format, and disable clang-format in that region
  • Loading branch information
jamesreinders authored May 3, 2023
1 parent ef79a65 commit 68cdcc9
Show file tree
Hide file tree
Showing 150 changed files with 2,333 additions and 1,934 deletions.
6 changes: 4 additions & 2 deletions Contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ Copyright for Apress source code belongs to the author(s). However, under fair u
2. Fork the repository for the relevant book.
3. Create a new branch on which to make your change, e.g.
`git checkout -b my_code_contribution`
4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted.
5. Submit a pull request.
4. Keep formating: clang-format -i --style="{BasedOnStyle: Google, ColumnLimit: 60}" <file>.cpp
[we used find . -name "*cpp" -exec clang-format -i --style="{BasedOnStyle: Google, ColumnLimit: 60}" {} \; originally]
5. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted.
6. Submit a pull request.

Thank you for your contribution!
20 changes: 9 additions & 11 deletions samples/Ch01_intro/fig_1_1_hello.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,27 @@

// SPDX-License-Identifier: MIT

#include <sycl/sycl.hpp>
#include <iostream>
#include <sycl/sycl.hpp>
using namespace sycl;

const std::string secret {
"Ifmmp-!xpsme\"\012J(n!tpssz-!Ebwf/!"
"J(n!bgsbje!J!dbo(u!ep!uibu/!.!IBM\01" };
const std::string secret{
"Ifmmp-!xpsme\"\012J(n!tpssz-!Ebwf/!"
"J(n!bgsbje!J!dbo(u!ep!uibu/!.!IBM\01"};

const auto sz = secret.size();

int main() {
queue Q;

char *result = malloc_shared<char>(sz, Q);
std::memcpy(result,secret.data(),sz);
char* result = malloc_shared<char>(sz, Q);
std::memcpy(result, secret.data(), sz);

Q.parallel_for(sz,[=](auto& i) {
result[i] -= 1;
}).wait();
Q.parallel_for(sz, [=](auto& i) {
result[i] -= 1;
}).wait();

std::cout << result << "\n";
free(result, Q);
return 0;
}


25 changes: 12 additions & 13 deletions samples/Ch01_intro/fig_1_3_race.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,31 @@

// SPDX-License-Identifier: MIT

#include <sycl/sycl.hpp>
#include <iostream>
#include <sycl/sycl.hpp>
using namespace sycl;

const std::string secret {
"Ifmmp-!xpsme\"\012J(n!tpssz-!Ebwf/!"
"J(n!bgsbje!J!dbo(u!ep!uibu/!.!IBM\01" };
const std::string secret{
"Ifmmp-!xpsme\"\012J(n!tpssz-!Ebwf/!"
"J(n!bgsbje!J!dbo(u!ep!uibu/!.!IBM\01"};

const auto sz = secret.size();

int main() {
queue Q;

char *result = malloc_shared<char>(sz, Q);
char* result = malloc_shared<char>(sz, Q);

// Introduce potential data race! We don't define a dependence
// to ensure correct ordering with later operations.
Q.memcpy(result,secret.data(),sz);
// Introduce potential data race! We don't define a
// dependence to ensure correct ordering with later
// operations.
Q.memcpy(result, secret.data(), sz);

Q.parallel_for(sz,[=](auto& i) {
result[i] -= 1;
}).wait();
Q.parallel_for(sz, [=](auto& i) {
result[i] -= 1;
}).wait();

std::cout << result << "\n";
free(result, Q);
return 0;
}


24 changes: 12 additions & 12 deletions samples/Ch01_intro/fig_1_4_lambda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,33 @@

#include <iostream>

void print_values( const int& i, const int& j, const int& k, const int& l ) {
void print_values(const int& i, const int& j, const int& k,
const int& l) {
std::cout << "i == " << i << "\n";
std::cout << "j == " << j << "\n";
std::cout << "k == " << k << "\n";
std::cout << "l == " << l << "\n";
}

int main() {

// START BOOK SNIP
// START BOOK SNIP
int i = 1, j = 10, k = 100, l = 1000;

auto lambda = [i, &j] (int k0, int &l0) -> int {
auto lambda = [i, &j](int k0, int& l0) -> int {
j = 2 * j;
k0 = 2 * k0;
l0 = 2 * l0;
return i + j + k0 + l0;
};

print_values( i, j, k, l );
std::cout << "First call returned " << lambda( k, l ) << "\n";
print_values( i, j, k, l );
std::cout << "Second call returned " << lambda( k, l ) << "\n";
print_values( i, j, k, l );
// END BOOK SNIP
print_values(i, j, k, l);
std::cout << "First call returned " << lambda(k, l)
<< "\n";
print_values(i, j, k, l);
std::cout << "Second call returned " << lambda(k, l)
<< "\n";
print_values(i, j, k, l);
// END BOOK SNIP

return 0;
}


32 changes: 15 additions & 17 deletions samples/Ch01_intro/fig_1_6_functor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@

// START BOOK SNIP
class Functor {
public:
Functor(int i, int &j) : my_i{i}, my_jRef{j} { }

int operator()(int k0, int &l0) {
my_jRef = 2 * my_jRef;
k0 = 2 * k0;
l0 = 2 * l0;
return my_i + my_jRef + k0 + l0;
}

private:
int my_i;
int &my_jRef;
public:
Functor(int i, int &j) : my_i{i}, my_jRef{j} {}

int operator()(int k0, int &l0) {
my_jRef = 2 * my_jRef;
k0 = 2 * k0;
l0 = 2 * l0;
return my_i + my_jRef + k0 + l0;
}

private:
int my_i;
int &my_jRef;
};
// END BOOK SNIP

Expand All @@ -27,10 +27,8 @@ int main() {

Functor F{i, j};

std::cout << "First call returned " << F( k, l ) << "\n";
std::cout << "Second call returned " << F( k, l ) << "\n";
std::cout << "First call returned " << F(k, l) << "\n";
std::cout << "Second call returned " << F(k, l) << "\n";

return 0;
}


16 changes: 9 additions & 7 deletions samples/Ch02_where_code_runs/fig_2_10_gpu_selector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

// SPDX-License-Identifier: MIT

#include <sycl/sycl.hpp>
#include <iostream>
#include <sycl/sycl.hpp>
using namespace sycl;

int main() {
// Create queue bound to an available GPU device
queue Q{ gpu_selector_v };
queue Q{gpu_selector_v};

std::cout << "Selected device: " <<
Q.get_device().get_info<info::device::name>() << "\n";
std::cout << " -> Device vendor: " <<
Q.get_device().get_info<info::device::vendor>() << "\n";
std::cout << "Selected device: "
<< Q.get_device().get_info<info::device::name>()
<< "\n";
std::cout
<< " -> Device vendor: "
<< Q.get_device().get_info<info::device::vendor>()
<< "\n";

return 0;
}

29 changes: 16 additions & 13 deletions samples/Ch02_where_code_runs/fig_2_12_multiple_selectors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@

// SPDX-License-Identifier: MIT

#include <sycl/sycl.hpp>
#include <sycl/ext/intel/fpga_extensions.hpp> // For fpga_selector_v
#include <iostream>
#include <string>
#include <sycl/ext/intel/fpga_extensions.hpp> // For fpga_selector_v
#include <sycl/sycl.hpp>
using namespace sycl;

void output_dev_info( const device& dev, const std::string& selector_name) {
std::cout << selector_name << ": Selected device: " <<
dev.get_info<info::device::name>() << "\n";
std::cout << " -> Device vendor: " <<
dev.get_info<info::device::vendor>() << "\n";
void output_dev_info(const device& dev,
const std::string& selector_name) {
std::cout << selector_name << ": Selected device: "
<< dev.get_info<info::device::name>() << "\n";
std::cout << " -> Device vendor: "
<< dev.get_info<info::device::vendor>() << "\n";
}

int main() {
output_dev_info( device{ default_selector_v}, "default_selector_v" );
output_dev_info( device{ cpu_selector_v}, "cpu_selector_v" );
output_dev_info( device{ gpu_selector_v}, "gpu_selector_v" );
output_dev_info( device{ accelerator_selector_v}, "accelerator_selector_v" );
output_dev_info( device{ ext::intel::fpga_selector_v}, "fpga_selector_v" );
output_dev_info(device{default_selector_v},
"default_selector_v");
output_dev_info(device{cpu_selector_v}, "cpu_selector_v");
output_dev_info(device{gpu_selector_v}, "gpu_selector_v");
output_dev_info(device{accelerator_selector_v},
"accelerator_selector_v");
output_dev_info(device{ext::intel::fpga_selector_v},
"fpga_selector_v");

return 0;
}

21 changes: 12 additions & 9 deletions samples/Ch02_where_code_runs/fig_2_13_gpu_plus_fpga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@

// SPDX-License-Identifier: MIT

#include <sycl/sycl.hpp>
#include <sycl/ext/intel/fpga_extensions.hpp> // For fpga_selector_v
#include <iostream>
#include <sycl/ext/intel/fpga_extensions.hpp> // For fpga_selector_v
#include <sycl/sycl.hpp>
using namespace sycl;

int main() {
queue my_gpu_queue( gpu_selector_v );
queue my_fpga_queue( ext::intel::fpga_selector_v );
queue my_gpu_queue(gpu_selector_v);
queue my_fpga_queue(ext::intel::fpga_selector_v);

std::cout << "Selected device 1: " <<
my_gpu_queue.get_device().get_info<info::device::name>() << "\n";
std::cout << "Selected device 1: "
<< my_gpu_queue.get_device()
.get_info<info::device::name>()
<< "\n";

std::cout << "Selected device 2: " <<
my_fpga_queue.get_device().get_info<info::device::name>() << "\n";
std::cout << "Selected device 2: "
<< my_fpga_queue.get_device()
.get_info<info::device::name>()
<< "\n";

return 0;
}

41 changes: 23 additions & 18 deletions samples/Ch02_where_code_runs/fig_2_15_aspect_selector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,33 @@

// SPDX-License-Identifier: MIT

#include <sycl/sycl.hpp>
#include <iostream>
#include <sycl/sycl.hpp>
using namespace sycl;

int main() {
// In the aspect_selector form taking a comma seperated group of
// aspects, all aspects must be present for a device to be selected.
queue Q1{ aspect_selector(aspect::fp16, aspect::gpu) };

// In the aspect_selector form that takes two vectors, the first
// vector contains aspects that a device must exhibit, and the
// second contains aspects that must NOT be exhibited.
queue Q2{ aspect_selector(
std::vector{aspect::fp64, aspect::fp16},
std::vector{aspect::gpu, aspect::accelerator}) };

std::cout << "First selected device is: " <<
Q1.get_device().get_info<info::device::name>() << "\n";

std::cout << "Second selected device is: " <<
Q2.get_device().get_info<info::device::name>() << "\n";
// In the aspect_selector form taking a comma seperated
// group of aspects, all aspects must be present for a
// device to be selected.
queue Q1{aspect_selector(aspect::fp16, aspect::gpu)};

// In the aspect_selector form that takes two vectors, the
// first vector contains aspects that a device must
// exhibit, and the second contains aspects that must NOT
// be exhibited.
queue Q2{aspect_selector(
std::vector{aspect::fp64, aspect::fp16},
std::vector{aspect::gpu, aspect::accelerator})};

std::cout
<< "First selected device is: "
<< Q1.get_device().get_info<info::device::name>()
<< "\n";

std::cout
<< "Second selected device is: "
<< Q2.get_device().get_info<info::device::name>()
<< "\n";

return 0;
}

17 changes: 9 additions & 8 deletions samples/Ch02_where_code_runs/fig_2_16_custom_selector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,30 @@

// SPDX-License-Identifier: MIT

#include <sycl/sycl.hpp>
#include <iostream>
#include <sycl/sycl.hpp>
using namespace sycl;

// START CODE SNIP

int my_selector(const device &dev) {
if (dev.get_info<info::device::name>().find("pac_a10") != std::string::npos &&
dev.get_info<info::device::vendor>().find("Intel") != std::string::npos) {
if (dev.get_info<info::device::name>().find("pac_a10") !=
std::string::npos &&
dev.get_info<info::device::vendor>().find("Intel") !=
std::string::npos) {
return 1;
}
return -1;
}

// END CODE SNIP


int main() {
queue Q( my_selector );
queue Q(my_selector);

std::cout << "Selected device is: " <<
Q.get_device().get_info<info::device::name>() << "\n";
std::cout << "Selected device is: "
<< Q.get_device().get_info<info::device::name>()
<< "\n";

return 0;
}

Loading

0 comments on commit 68cdcc9

Please sign in to comment.