Add new multi-GPU multi-func test in gdb.rocm.
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.rocm / multi-func.cpp
1 #include <stdio.h>
2 #include <iostream>
3 #include "hip/hip_runtime.h"
4
5 #define CHECK(cmd) \
6 { \
7 hipError_t error = cmd; \
8 if (error != hipSuccess) { \
9 fprintf(stderr, "error: '%s'(%d) at %s:%d\n", hipGetErrorString(error), error, \
10 __FILE__, __LINE__); \
11 exit(EXIT_FAILURE); \
12 } \
13 }
14
15 // Defining number of elements in Array
16 #define N 5
17
18 __device__ int add_one(int no) {
19 return no + 1;
20 }
21
22 __device__ int multiply_two(int no) {
23 no = no*2;
24 return no;
25 }
26
27 __device__ int multiply_three(int no) {
28 no = no*3;
29 return no;
30 }
31
32 //c = a*2 + b*3 + 1;
33 __device__ int gpu_operations(int a,int b) {
34 int c;
35 c = multiply_two(a) + multiply_three(b);
36 c = add_one(c);
37 return c;
38 }
39
40 __global__ void gpu_kernel_operations(int *d_a, int *d_b, int *d_c) {
41 // Getting block index of current kernel
42 int tid = blockIdx.x; // handle the data at this index
43 if (tid < N)
44 d_c[tid] = gpu_operations(d_a[tid],d_b[tid]);
45 }
46
47 int main(void) {
48 // Defining host arrays
49 int h_a[N], h_b[N], h_c[N];
50 // Defining device pointers
51 int *d_a, *d_b, *d_c;
52 // allocate the memory
53 CHECK(hipMalloc((void**)&d_a, N * sizeof(int)));
54 CHECK(hipMalloc((void**)&d_b, N * sizeof(int)));
55 CHECK(hipMalloc((void**)&d_c, N * sizeof(int)));
56
57 // Initializing Arrays
58 for (int i = 0; i < N; i++) {
59 h_a[i] = i;
60 h_b[i] = i + 1;
61 }
62
63 // Copy input arrays from host to device memory
64 CHECK(hipMemcpy(d_a, h_a, N * sizeof(int), hipMemcpyHostToDevice));
65 CHECK(hipMemcpy(d_b, h_b, N * sizeof(int), hipMemcpyHostToDevice));
66
67 // Calling kernels with N blocks and one thread per block, passing
68 // device pointers as parameters
69 hipLaunchKernelGGL(gpu_kernel_operations, dim3(N), dim3(1 ), 0, 0, d_a, d_b, d_c);
70
71 // Copy result back to host memory from device memory
72 CHECK(hipMemcpy(h_c, d_c, N * sizeof(int), hipMemcpyDeviceToHost));
73
74 // Printing result on console
75 for (int i = 0; i < N; i++) {
76 if (h_c[i] != h_a[i]*2 + h_b[i]*3 + 1) {
77 fprintf(stderr, "ERROR: wrong result of %d element is %d*2 + %d*3 + 1 = %d\n",
78 i, h_a[i], h_b[i],h_c[i]);
79 exit(EXIT_FAILURE);
80 }
81 }
82 // Free up memory
83 CHECK(hipFree(d_a));
84 CHECK(hipFree(d_b));
85 CHECK(hipFree(d_c));
86 return 0;
87 }
88
This page took 0.033057 seconds and 4 git commands to generate.