已合并
docs: add full HelloWorld sample code and build/run steps #6487
docs: add full HelloWorld sample code and build/run steps #6487
已合并
liuliu876创建于 9月29日
共 2 个文件变更+27-14
@@ -27,11 +27,6 @@
27 int main(int argc, char const* argv[])27 int main(int argc, char const* argv[])
28 {28 {
29 ...29 ...
30- constexpr uint32_t numBlocks = 8;
31- ...
32-
33- aclrtStream stream = nullptr;
34- aclrtCreateStream(&stream);
35 // Launch kernel <<<numBlocks, dynUBufSize, stream>>>30 // Launch kernel <<<numBlocks, dynUBufSize, stream>>>
36 // numBlocks : Number of blocks. Default to 8 in this example.31 // numBlocks : Number of blocks. Default to 8 in this example.
37 // dynUBufSize : Dynamic unified buffer size. Default to 0 in this example.32 // dynUBufSize : Dynamic unified buffer size. Default to 0 in this example.
@@ -41,8 +36,27 @@
41 }36 }
42 ```37 ```
43 38 
44-- **算子编译与运行**:39+- **算子编译与运行**:
45- 40+ 
41+ 本样例完整代码(hello_world.asc)如下:
42+ ```cpp
43+ #include "utils/debug/asc_printf.h"
44+ #include "acl/acl.h"
45+ 
46+ __global__ __vector__ void hello_world() { printf("Hello World!!!\n"); }
47+ 
48+ int main(int argc, char const* argv[])
49+ {
50+ aclInit(nullptr);
51+ aclrtSetDevice(0); // Acquire runtime management resources.
52+ hello_world<<<8, 0, nullptr>>>();
53+ aclrtSynchronizeDevice();
54+ aclrtResetDevice(0); // Release runtime resources.
55+ aclFinalize();
56+ return 0;
57+ }
58+ ```
59+ 运行如下命令进行编译运行:
46 ```bash60 ```bash
47 bisheng hello_world.asc --npu-arch=dav-2201 -o demo61 bisheng hello_world.asc --npu-arch=dav-2201 -o demo
48 ./demo62 ./demo
@@ -20,12 +20,11 @@ __global__ __vector__ void hello_world() { printf("Hello World!!!\n"); }
20 20 
21int main(int argc, char const* argv[])21int main(int argc, char const* argv[])
22{22{
23- aclrtSetDevice(0); // 运行管理资源申请。23+ aclInit(nullptr);
24- aclrtStream stream = nullptr;24+ aclrtSetDevice(0); // Acquire runtime management resources.
25- aclrtCreateStream(&stream);25+ hello_world<<<8, 0, nullptr>>>();
26- hello_world<<<8, 0, stream>>>();26+ aclrtSynchronizeDevice();
27- aclrtSynchronizeStream(stream);27+ aclrtResetDevice(0); // Release runtime resources.
28- aclrtDestroyStream(stream);28+ aclFinalize();
29- aclrtResetDevice(0); // 销毁运行资源。
30 return 0;29 return 0;
31}30}