@@ -902,3 +902,53 @@ void blas_set_parameter(void)
902902}
903903
904904#endif
905+
906+ #if defined(ARCH_RISCV64 )
907+
908+ #include <stdio.h>
909+
910+ /* RISC-V has no architectural cache-size query (cf. x86 CPUID / LoongArch
911+ CPUCFG), so read the L2 (level 2, unified) size from Linux sysfs and fall
912+ back to 512 KB. Returns the L2 size in kilobytes. */
913+ int get_L2_size (void ) {
914+ int size = 0 ;
915+ #if defined(OS_LINUX ) || defined(OS_ANDROID )
916+ int idx ;
917+ for (idx = 0 ; idx <= 4 ; idx ++ ) {
918+ char path [80 ]; FILE * fp ; int level = 0 ; long val = 0 ; char unit = 0 ;
919+ snprintf (path , sizeof (path ), "/sys/devices/system/cpu/cpu0/cache/index%d/level" , idx );
920+ fp = fopen (path , "r" ); if (fp == NULL ) continue ;
921+ if (fscanf (fp , "%d" , & level ) != 1 ) { fclose (fp ); continue ; }
922+ fclose (fp ); if (level != 2 ) continue ;
923+ snprintf (path , sizeof (path ), "/sys/devices/system/cpu/cpu0/cache/index%d/size" , idx );
924+ fp = fopen (path , "r" ); if (fp == NULL ) continue ;
925+ if (fscanf (fp , "%ld%c" , & val , & unit ) >= 1 ) {
926+ if (unit == 'M' || unit == 'm' ) val *= 1024 ;
927+ if (unit == 'G' || unit == 'g' ) val *= 1024 * 1024 ;
928+ size = (int )val ;
929+ }
930+ fclose (fp ); if (size > 0 ) break ;
931+ }
932+ #endif
933+ if (size <= 0 ) size = 512 ;
934+ return size ;
935+ }
936+
937+ void blas_set_parameter (void ) {
938+ #if defined(SGEMM_DEFAULT_P_BASE )
939+ /* Scale each precision's packed-A dimension P from the detected L2, relative
940+ to the size the active core's base blocking targets (RISCV_L2_REFERENCE_KB).
941+ The bases come from the core's own param.h block, so this is not tied to any
942+ single core; Q and R keep their param.h defaults. */
943+ int l2 = get_L2_size (); /* KB */
944+ int scale = l2 / RISCV_L2_REFERENCE_KB ;
945+ if (scale < 1 ) scale = 1 ;
946+ if (scale > 4 ) scale = 4 ;
947+ sgemm_p = SGEMM_DEFAULT_P_BASE * scale ;
948+ dgemm_p = DGEMM_DEFAULT_P_BASE * scale ;
949+ cgemm_p = CGEMM_DEFAULT_P_BASE * scale ;
950+ zgemm_p = ZGEMM_DEFAULT_P_BASE * scale ;
951+ #endif
952+ }
953+
954+ #endif
0 commit comments