@@ -2884,36 +2884,47 @@ size_t ggml_set_scratch(struct ggml_context * ctx, struct ggml_scratch scratch)
28842884 return result ;
28852885}
28862886
2887+ #ifdef __APPLE__
2888+ #define MLOCK_SUGGESTION \
2889+ "Try increasing the sysctl values 'vm.user_wire_limit' and 'vm.global_user_wire_limit' and/or " \
2890+ "decreasing 'vm.global_no_user_wire_amount'. Also try increasing RLIMIT_MLOCK (ulimit -l).\n"
2891+ #else
2892+ #define MLOCK_SUGGESTION \
2893+ "Try increasing RLIMIT_MLOCK ('ulimit -l' as root).\n"
2894+ #endif
2895+
28872896bool ggml_mlock_supported (void ) {
28882897 return GGML_MLOCK_SUPPORT ;
28892898}
28902899
2900+ bool ggml_mlock (
2901+ struct ggml_context * ctx ,
2902+ const void * opt_extra_addr ,
2903+ size_t opt_extra_len ,
2904+ char * * err_p ) {
2905+ // TODO: Use SetProcessWorkingSetSize() + VirtualLock() on WIN32
28912906#if GGML_MLOCK_SUPPORT
2892- #ifdef __APPLE__
2893- #define MLOCK_SUGGESTION "Try increasing the sysctl values 'vm.user_wire_limit' and 'vm.global_user_wire_limit' and/or\n" \
2894- "decreasing 'vm.global_no_user_wire_amount'. Also try increasing RLIMIT_MLOCK (ulimit -l)."
2895- #else
2896- #define MLOCK_SUGGESTION "Try increasing RLIMIT_MLOCK (ulimit -l)."
2897- #endif
2898- bool ggml_mlock (struct ggml_context * ctx , char * * err_p ) {
28992907 if (ctx -> mem_buffer_mlocked ) {
29002908 return true;
29012909 }
2902- if (mlock (ctx -> mem_buffer , ctx -> mem_size )) {
2903- int ret = asprintf (err_p , "failed to mlock %zu-byte buffer: %s\n" MLOCK_SUGGESTION ,
2904- ctx -> mem_size , strerror (errno ));
2905- GGML_ASSERT (ret >= 0 );
2910+ if (mlock (ctx -> mem_buffer , ctx -> mem_size ) ||
2911+ (opt_extra_len &&
2912+ mlock (opt_extra_addr , opt_extra_len ))) {
2913+ if ((* err_p = malloc (1024 ))) {
2914+ snprintf (* err_p , 1024 ,
2915+ "failed to mlock %zu-byte buffer: %s\n" MLOCK_SUGGESTION ,
2916+ ctx -> mem_size + opt_extra_len ,
2917+ strerror (errno ));
2918+ }
29062919 return false;
29072920 }
29082921 ctx -> mem_buffer_mlocked = true;
29092922 return true;
2910- }
29112923#else // GGML_MLOCK_SUPPORT
2912- bool ggml_mlock (struct ggml_context * ctx , char * * err_p ) {
29132924 * err_p = strdup ("can't mlock because it's not supported on this system" );
29142925 return false;
2915- }
29162926#endif // GGML_MLOCK_SUPPORT
2927+ }
29172928
29182929////////////////////////////////////////////////////////////////////////////////
29192930
0 commit comments