This example shows the use of the rocBLAS Level 1 swap operation, which exchanges elements between two HIP vectors. The Level 1 API defines operations between vectors.
- Read in command-line parameters.
- Allocate and initialize host vectors.
- Compute CPU reference result.
- Create a rocBLAS handle.
- Allocate and initialize device vectors.
- Invoke the rocBLAS swap function.
- Copy the result from device to host.
- Destroy the rocBLAS handle, release device memory.
- Validate the output by comparing it to the CPU reference result.
The application provides the following optional command line arguments:
-
-x
or--incx
. The stride between consecutive values in the data array that makes up vector$x$ , which must be greater than 0. Its default value is 1. -
-y
or--incy
. The stride between consecutive values in the data array that makes up vector$y$ , which must be greater than 0. Its default value is 1. -
-n
or--n
. The number of elements in vectors$x$ and$y$ , which must be greater than 0. Its default value is 5.
- rocBLAS is initialized by calling
rocblas_create_handle(rocblas_handle*)
and it is terminated by callingrocblas_destroy_handle(rocblas_handle)
. rocblas_set_vector(n, elem_size, *x, incx, *y, incy)
is used to copy vectors from host to device memory.n
is the total number of elements that should be copied, andelem_size
is the size of a single element in bytes. The elements are copied fromx
toy
, where the step size between consecutive elements ofx
andy
is given respectively byincx
andincy
. Note that the increment is given in elements, not bytes. Additionally, the step size of eitherx
,y
, or both may also be negative. In this case care must be taken that the correct pointer is passed torocblas_set_vector
, as it is not automatically adjusted to the end of the input vector. Whenincx
andincy
are 1, calling this function is equivalent tohipMemcpy(y, x, n * elem_size, hipMemcpyHostToDevice)
. See the following diagram , which illustratesrocblas_set_vector(3, sizeof(T), x, incx, y, incy)
:
rocblas_get_vector(n, elem_size, *x, incx, *y, incy)
is used to copy vectors from device to host memory. Its arguments are similar torocblas_set_vector
. Elements are also copied fromx
toy
.rocblas_Xswap(handle, n, *x, incx, *y, incy)
exchanges elements between vectorsx
andy
. The two vectors are respectively indexed according to the step incrementsincx
andincy
which are each indexed according to step incrementsincx
andincy
similar torocblas_set_vector
androcblas_get_vector
.n
gives the amount of elements that should be exchanged.X
specifies the data type of the operation, and can be one ofs
(single-precision:rocblas_float
),d
(double-precision:rocblas_double
),h
(half-precision:rocblas_half
),c
(single-precision complex:rocblas_complex
), orz
(double-precision complex:rocblas_double_complex
).
rocblas_create_handle
rocblas_destroy_handle
rocblas_get_vector
rocblas_handle
rocblas_int
rocblas_set_vector
rocblas_sswap
rocblas_status
rocblas_status_success
rocblas_status_to_string
hipFree
hipMalloc