Skip to content

Commit 0b1328e

Browse files
authored
Merge pull request #17 from cherubrock-seb/fix/example-runtime-correctness-20261110
Fix vector add example runtime validation
2 parents 67c6589 + 1b1feaa commit 0b1328e

2 files changed

Lines changed: 10 additions & 8 deletions

File tree

‎examples/vector_add/main.cu‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ template<int items_per_thread>
3030
void run_kernel(int n) {
3131
double constant = 1.0;
3232
std::vector<half> input(n);
33-
std::vector<float> output_expected;
34-
std::vector<float> output_result;
33+
std::vector<float> output_expected(n);
34+
std::vector<float> output_result(n);
3535

3636
// Generate input data
3737
for (int i = 0; i < n; i++) {
3838
input[i] = half(i);
39-
output_expected[i] = float(i + constant);
39+
output_expected[i] =
40+
float(half(float(input[i]) * float(input[i]) + float(half(constant))));
4041
}
4142

4243
// Allocate device memory
@@ -59,7 +60,7 @@ void run_kernel(int n) {
5960
kf::make_vec_ptr(output_dev));
6061

6162
// Copy results back
62-
cuda_check(cudaMemcpy(output_dev, output_result.data(), sizeof(float) * n, cudaMemcpyDefault));
63+
cuda_check(cudaMemcpy(output_result.data(), output_dev, sizeof(float) * n, cudaMemcpyDefault));
6364

6465
// Check results
6566
for (int i = 0; i < n; i++) {

‎examples/vector_add_tiling/main.cu‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ template<int items_per_thread, int block_size = 256>
3333
void run_kernel(int n) {
3434
double constant = 1.0;
3535
std::vector<half> input(n);
36-
std::vector<float> output_expected;
37-
std::vector<float> output_result;
36+
std::vector<float> output_expected(n);
37+
std::vector<float> output_result(n);
3838

3939
// Generate input data
4040
for (int i = 0; i < n; i++) {
4141
input[i] = half(i);
42-
output_expected[i] = float(i + constant);
42+
output_expected[i] =
43+
float(half(float(input[i]) * float(input[i]))) * float(constant);
4344
}
4445

4546
// Allocate device memory
@@ -58,7 +59,7 @@ void run_kernel(int n) {
5859
<<<grid_size, block_size>>>(n, input_dev, constant, output_dev);
5960

6061
// Copy results back
61-
cuda_check(cudaMemcpy(output_dev, output_result.data(), sizeof(float) * n, cudaMemcpyDefault));
62+
cuda_check(cudaMemcpy(output_result.data(), output_dev, sizeof(float) * n, cudaMemcpyDefault));
6263

6364
// Check results
6465
for (int i = 0; i < n; i++) {

0 commit comments

Comments
 (0)