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:
-
-xor--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. -
-yor--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. -
-nor--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.nis the total number of elements that should be copied, andelem_sizeis the size of a single element in bytes. The elements are copied fromxtoy, where the step size between consecutive elements ofxandyis given respectively byincxandincy. 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. Whenincxandincyare 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 fromxtoy.rocblas_Xswap(handle, n, *x, incx, *y, incy)exchanges elements between vectorsxandy. The two vectors are respectively indexed according to the step incrementsincxandincywhich are each indexed according to step incrementsincxandincysimilar torocblas_set_vectorandrocblas_get_vector.ngives the amount of elements that should be exchanged.Xspecifies 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_handlerocblas_destroy_handlerocblas_get_vectorrocblas_handlerocblas_introcblas_set_vectorrocblas_sswaprocblas_statusrocblas_status_successrocblas_status_to_string
hipFreehipMalloc