MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / main

Function main

paddle/phi/kernels/xpu/plugin/example/example.cc:24–71  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

22namespace xdnn = baidu::xpu::api;
23
24int main() {
25 int num = 5;
26 int errcode = 0;
27 auto ctx = xdnn::create_context();
28 float* A = nullptr;
29 errcode = xpu_malloc(reinterpret_cast<void**>(&A), num * sizeof(float));
30 assert(errcode == 0);
31 float* B = nullptr;
32 errcode = xpu_malloc(reinterpret_cast<void**>(&B), num * sizeof(float));
33 assert(errcode == 0);
34
35 std::vector<float> A_cpu = {1, 2, 3, 4, 5};
36 std::vector<float> B_cpu(num, 0.0f);
37 std::vector<float> B_ref = {3, 4, 5, 6, 7};
38 xpu_memcpy(reinterpret_cast<void*>(A),
39 reinterpret_cast<void*>(&(A_cpu[0])),
40 num * sizeof(float),
41 XPUMemcpyKind::XPU_HOST_TO_DEVICE);
42 errcode = xdnn::plugin::add2(ctx, A, B, num);
43 assert(errcode == 0);
44 xpu_memcpy(reinterpret_cast<void*>(&(B_cpu[0])),
45 reinterpret_cast<void*>(B),
46 num * sizeof(float),
47 XPUMemcpyKind::XPU_DEVICE_TO_HOST);
48 printf("A(%p):\n", A);
49 for (size_t i = 0; i < num; i++) {
50 printf("%f ", A_cpu[i]);
51 }
52 printf("\nB(%p):\n", B);
53 for (size_t i = 0; i < num; i++) {
54 printf("%f ", B_cpu[i]);
55 }
56 bool pass = true;
57 for (size_t i = 0; i < num; i++) {
58 if (fabs(B_cpu[i] - B_ref[i]) > 1e-5f) {
59 pass = false;
60 break;
61 }
62 }
63 printf("\nCheck %s! \n", pass ? "pass" : "fail");
64
65 destroy_context(ctx);
66 errcode = xpu_free(A);
67 assert(errcode == 0);
68 errcode = xpu_free(B);
69 assert(errcode == 0);
70 return 0;
71}

Callers

nothing calls this directly

Calls 2

add2Function · 0.85
printfFunction · 0.85

Tested by

no test coverage detected