Skip to content

Commit ebe7a33

Browse files
committed
Complete saxpy
1 parent 92c55bc commit ebe7a33

7 files changed

Lines changed: 128 additions & 11 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "Linux",
5+
"includePath": [
6+
"${workspaceFolder}/source/**"
7+
],
8+
"defines": [],
9+
"compilerPath": "/usr/local/cuda/bin/nvcc",
10+
"cStandard": "gnu17",
11+
"cppStandard": "gnu++17",
12+
"intelliSenseMode": "linux-gcc-x64",
13+
"configurationProvider": "ms-vscode.makefile-tools"
14+
}
15+
],
16+
"version": 4
17+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"recommendations": [
3+
"nvidia.nsight-vscode-edition",
4+
"ms-vscode.cpptools",
5+
"ms-vscode.makefile-tools"
6+
]
7+
}

cuda-introduction/.vscode/launch.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
"configurations": [
44
{
55
"name": "CUDA C++: Launch",
6+
"preLaunchTask": "CMake: build",
67
"type": "cuda-gdb",
78
"request": "launch",
8-
"program": "${workspaceFolder}/build/bin/saxpy",
9+
"program": "${command:cmake.launchTargetPath}",
910
}
1011
]
1112
}
Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,63 @@
11
{
22
"files.associations": {
33
"cpp": "cuda-cpp",
4-
"hpp": "cuda-cpp"
4+
"hpp": "cuda-cpp",
5+
"array": "cpp",
6+
"atomic": "cpp",
7+
"bit": "cpp",
8+
"cctype": "cpp",
9+
"charconv": "cpp",
10+
"chrono": "cpp",
11+
"clocale": "cpp",
12+
"cmath": "cpp",
13+
"compare": "cpp",
14+
"concepts": "cpp",
15+
"cstdarg": "cpp",
16+
"cstddef": "cpp",
17+
"cstdint": "cpp",
18+
"cstdio": "cpp",
19+
"cstdlib": "cpp",
20+
"cstring": "cpp",
21+
"ctime": "cpp",
22+
"cwchar": "cpp",
23+
"cwctype": "cpp",
24+
"deque": "cpp",
25+
"string": "cpp",
26+
"unordered_map": "cpp",
27+
"vector": "cpp",
28+
"exception": "cpp",
29+
"algorithm": "cpp",
30+
"functional": "cpp",
31+
"iterator": "cpp",
32+
"memory": "cpp",
33+
"memory_resource": "cpp",
34+
"numeric": "cpp",
35+
"optional": "cpp",
36+
"random": "cpp",
37+
"ratio": "cpp",
38+
"string_view": "cpp",
39+
"system_error": "cpp",
40+
"tuple": "cpp",
41+
"type_traits": "cpp",
42+
"utility": "cpp",
43+
"format": "cpp",
44+
"initializer_list": "cpp",
45+
"iomanip": "cpp",
46+
"iosfwd": "cpp",
47+
"iostream": "cpp",
48+
"istream": "cpp",
49+
"limits": "cpp",
50+
"new": "cpp",
51+
"numbers": "cpp",
52+
"ostream": "cpp",
53+
"queue": "cpp",
54+
"ranges": "cpp",
55+
"span": "cpp",
56+
"sstream": "cpp",
57+
"stdexcept": "cpp",
58+
"streambuf": "cpp",
59+
"text_encoding": "cpp",
60+
"typeinfo": "cpp",
61+
"variant": "cpp"
562
}
663
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"type": "cmake",
6+
"label": "CMake: build",
7+
"command": "build",
8+
"targets": [
9+
"${command:cmake.buildTargetName}"
10+
],
11+
"group": "build",
12+
"problemMatcher": [],
13+
"detail": "CMake template build task"
14+
},
15+
]
16+
}

cuda-introduction/source/common.cu

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ unsigned divup(unsigned size, unsigned div)
99
{
1010
// TODO: implement a 1 line function to return the divup operation.
1111
// Note: You only need to use addition, subtraction, and division operations.
12-
return 0;
12+
return (size + div - 1) / div;
1313
}
1414

1515
void clearHostAndDeviceArray(float *res, float *dev_res, unsigned size, const int value)
@@ -26,6 +26,11 @@ bool compareReferenceAndResult(const float *ref, const float *res, unsigned size
2626
bool passed = true;
2727
for (unsigned i = 0; i < size; i++)
2828
{
29+
#if 0
30+
// print thread info regardless of fail or not
31+
std::cout << "ID: " << i << " \t Res: " << res[i] << " \t Ref: " << ref[i] << std::endl;
32+
#endif
33+
2934
// LOOK: Check if floating point values are equal within an epsilon as returns can vary slightly between CPU and GPU
3035
if (std::fabs(res[i] - ref[i]) > epsilon)
3136
{

cuda-introduction/source/saxpy.cu

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,22 @@
99
__global__ void saxpy(float* const z, const float* const x, const float* const y, const float a, const unsigned size)
1010
{
1111
// TODO 9: Compute the global index for each thread.
12-
unsigned idx = 0;
12+
unsigned idx = threadIdx.x;
1313

1414
// TODO 10: Check if idx is out of bounds. If yes, return.
15-
if (idx >= 0)
16-
return;
15+
if (idx >= size)
16+
{return;}
1717

1818
// TODO 11: Perform the SAXPY operation: z = a * x + y.
19+
20+
z[idx] = a * x[idx] + y[idx];
1921
}
2022

2123
int main(int argc, char *argv[])
2224
{
2325
// TODO 1: Set the size. Start with something simple like 64.
2426
// TODO Optional: Try out these sizes: 256, 1024, 2048, 14, 103, 1025, 3127
25-
const unsigned size = 0;
27+
const unsigned size = 64;
2628

2729
// Host arrays.
2830
float* x = new float[size];
@@ -51,11 +53,16 @@ int main(int argc, char *argv[])
5153
// Device arrays
5254
float *d_x, *d_y, *d_z;
5355

56+
int sizeInBytes = size * sizeof(float);
57+
5458
// TODO 2: Allocate memory on the device. Fill in the blanks for d_x, then do the same commands for d_y and d_z.
55-
// CUDA(cudaMalloc((void **)& pointer, size in bytes)));
59+
CUDA(cudaMalloc((void **)& d_x, sizeInBytes));
60+
CUDA(cudaMalloc((void **)& d_y, sizeInBytes));
61+
CUDA(cudaMalloc((void **)& d_z, sizeInBytes));
5662

5763
// TODO 3: Copy array contents of X and Y from the host (CPU) to the device (GPU). Follow what you did for 2,
58-
// CUDA(cudaMemcpy(dest ptr, source ptr, size in bytes, direction enum));
64+
CUDA(cudaMemcpy(d_x, x, sizeInBytes, cudaMemcpyHostToDevice));
65+
CUDA(cudaMemcpy(d_y, y, sizeInBytes, cudaMemcpyHostToDevice));
5966

6067
CUDA(cudaDeviceSynchronize());
6168

@@ -69,17 +76,20 @@ int main(int argc, char *argv[])
6976
// TODO 4: Setup threads and blocks.
7077
// Start threadPerBlock as 128, then try out differnt configurations: 32, 64, 256, 512, 1024
7178
// Use divup to get the number of blocks to launch.
72-
const unsigned threadsPerBlock = 0;
79+
const unsigned threadsPerBlock = 128;
7380

7481
// TODO 5: Implement the divup function in common.cpp
7582
const unsigned blocks = divup(size, threadsPerBlock);
7683

7784
// TODO 6: Launch the GPU kernel with blocks and threadPerBlock as launch configuration
78-
// saxpy<<< >>> (....);
85+
saxpy<<<blocks, threadsPerBlock>>> (d_z, d_x, d_y, a, size);
7986

8087
// TODO 7: Copy the answer back to the host (CPU) from the device (GPU).
8188
// Copy what you did in 3, except for d_z -> z.
8289

90+
cudaMemcpy(z, d_z, sizeInBytes, cudaMemcpyDeviceToHost);
91+
92+
8393
// LOOK: Use postprocess to check the result
8494
compareReferenceAndResult(z_gold, z, size, 1e-6);
8595
std::cout << "****************************************************" << std::endl << std::endl;
@@ -88,6 +98,10 @@ int main(int argc, char *argv[])
8898
// TODO 8: free device memory using cudaFree
8999
// CUDA(cudaFree(device pointer));
90100

101+
CUDA(cudaFree(d_x));
102+
CUDA(cudaFree(d_y));
103+
CUDA(cudaFree(d_z));
104+
91105
// free host memory
92106
delete[] x;
93107
delete[] y;

0 commit comments

Comments
 (0)