RVV Application#
Introduction#
RVV (RISC-V Vector) is the vector extension instruction set of the RISC-V architecture. The K230 chip supports the RVV extension, which can utilize vector instructions for parallel computation, significantly improving data processing performance. This section describes how to use the RVV extension on the K230 platform.
Feature Description#
RVV Features#
RVV provides powerful vector computation capabilities:
SIMD Computation: Single Instruction Multiple Data parallel computation
Variable Vector Length: Dynamically adjusted vector length based on hardware support
Rich Vector Instructions: Support arithmetic, logical, load/store operations, etc.
Flexible Data Types: Support integer, floating-point, and other data types
K230 RVV Support#
K230 chip’s RVV support:
Vector Length: 256-bit or 512-bit (depending on the specific model)
Vector Registers: 32 vector registers (v0-v31)
Data Width: Support 8, 16, 32, 64-bit data
Scalar Types: Integer and floating-point
Main Advantages#
Using RVV can provide the following advantages:
Performance Improvement: Improve performance through parallel computation
Concise Code: Vector instructions can accomplish the same functionality with less code
High Energy Efficiency: Compared to scalar computation, higher performance per unit power consumption
Application Scenarios#
RVV is suitable for the following scenarios:
Image processing: pixel-level parallel operations
Audio processing: audio signal parallel processing
Matrix operations: matrix multiplication, addition, etc.
Data copying: batch data copying
Encryption algorithms: parallel encryption/decryption
DSP applications: digital signal processing
Compilation Instructions#
Enabling RVV Support#
RVV compilation options need to be added at compile time:
# Makefile example
CFLAGS += -march=rv64gcv -mabi=lp64d
# CMake example
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=rv64gcv -mabi=lp64d")
Inline Functions#
To use RVV inline functions in code, the corresponding header file needs to be included:
#include <riscv_vector.h>
Usage Instructions#
RVV Basic Usage#
Vector Configuration and Loading#
#include <riscv_vector.h>
void vector_add_example(float* a, float* b, float* c, int n) {
// Set vector length
size_t vl = vsetvl_e32m4(n);
// Load vectors
vfloat32m4_t va = vle32_v_f32m4(a, vl);
vfloat32m4_t vb = vle32_v_f32m4(b, vl);
// Vector addition
vfloat32m4_t vc = vfadd_vv_f32m4(va, vb, vl);
// Store result
vse32_v_f32m4(c, vc, vl);
}
Vector Width Configuration#
RVV supports different vector widths (LMUL):
// Use 1/2/4/8 times the vector length
vfloat32m1_t v1 = ...; // 1x vector length
vfloat32m2_t v2 = ...; // 2x vector length
vfloat32m4_t v4 = ...; // 4x vector length
vfloat32m8_t v8 = ...; // 8x vector length
Conditional Processing#
Use masks for conditional processing:
void vector_conditional_example(float* a, float* b, float* c, int n) {
size_t vl = vsetvl_e32m4(n);
vfloat32m4_t va = vle32_v_f32m4(a, vl);
vfloat32m4_t vb = vle32_v_f32m4(b, vl);
// Create mask (positions where a > b are 1)
vbool32_t mask = vmfgt_vf_f32m4(va, vb, vl);
// Conditional selection
vfloat32m4_t vc = vfmerge_vfm_f32m4(vb, va, mask, vl);
vse32_v_f32m4(c, vc, vl);
}
Reduction Operations#
Use reduction operations for accumulation:
float vector_sum_example(float* a, int n) {
size_t vl = vsetvl_e32m4(n);
vfloat32m4_t va = vle32_v_f32m4(a, vl);
// Horizontal reduction sum
float sum = vfredosum_vs_f32m4_f32m4(va, vfmv_s_f_f32m4(0.0f, vl), vl);
return sum;
}
Performance Optimization Suggestions#
Vector Length: Use as large a vector length (LMUL) as possible to improve performance
Memory Alignment: Ensure data alignment to improve load/store efficiency
Loop Unrolling: Combine with loop unrolling to further improve performance
Avoid Scalar Code: Use vector instructions to replace scalar loops whenever possible
Tip
RVV programming requires a certain learning cost. It is recommended to start with simple examples and gradually master the various instructions and usage of RVV. For detailed documentation on RVV, please refer to the RISC-V Vector Extension Specification.
Tip
Using RVV on the K230 platform can significantly improve data processing performance, especially in scenarios requiring large amounts of parallel computation such as image processing and audio processing. It is recommended to combine the hardware features of K230 (such as DMA, cache) for comprehensive optimization.
