@@ -2885,36 +2885,47 @@ size_t ggml_set_scratch(struct ggml_context * ctx, struct ggml_scratch scratch)
2885
2885
return result ;
2886
2886
}
2887
2887
2888
+ #ifdef __APPLE__
2889
+ #define MLOCK_SUGGESTION \
2890
+ "Try increasing the sysctl values 'vm.user_wire_limit' and 'vm.global_user_wire_limit' and/or " \
2891
+ "decreasing 'vm.global_no_user_wire_amount'. Also try increasing RLIMIT_MLOCK (ulimit -l).\n"
2892
+ #else
2893
+ #define MLOCK_SUGGESTION \
2894
+ "Try increasing RLIMIT_MLOCK ('ulimit -l' as root).\n"
2895
+ #endif
2896
+
2888
2897
bool ggml_mlock_supported (void ) {
2889
2898
return GGML_MLOCK_SUPPORT ;
2890
2899
}
2891
2900
2901
+ bool ggml_mlock (
2902
+ struct ggml_context * ctx ,
2903
+ const void * opt_extra_addr ,
2904
+ size_t opt_extra_len ,
2905
+ char * * err_p ) {
2906
+ // TODO: Use SetProcessWorkingSetSize() + VirtualLock() on WIN32
2892
2907
#if GGML_MLOCK_SUPPORT
2893
- #ifdef __APPLE__
2894
- #define MLOCK_SUGGESTION "Try increasing the sysctl values 'vm.user_wire_limit' and 'vm.global_user_wire_limit' and/or\n" \
2895
- "decreasing 'vm.global_no_user_wire_amount'. Also try increasing RLIMIT_MLOCK (ulimit -l)."
2896
- #else
2897
- #define MLOCK_SUGGESTION "Try increasing RLIMIT_MLOCK (ulimit -l)."
2898
- #endif
2899
- bool ggml_mlock (struct ggml_context * ctx , char * * err_p ) {
2900
2908
if (ctx -> mem_buffer_mlocked ) {
2901
2909
return true;
2902
2910
}
2903
- if (mlock (ctx -> mem_buffer , ctx -> mem_size )) {
2904
- int ret = asprintf (err_p , "failed to mlock %zu-byte buffer: %s\n" MLOCK_SUGGESTION ,
2905
- ctx -> mem_size , strerror (errno ));
2906
- GGML_ASSERT (ret >= 0 );
2911
+ if (mlock (ctx -> mem_buffer , ctx -> mem_size ) ||
2912
+ (opt_extra_len &&
2913
+ mlock (opt_extra_addr , opt_extra_len ))) {
2914
+ if ((* err_p = malloc (1024 ))) {
2915
+ snprintf (* err_p , 1024 ,
2916
+ "failed to mlock %zu-byte buffer: %s\n" MLOCK_SUGGESTION ,
2917
+ ctx -> mem_size + opt_extra_len ,
2918
+ strerror (errno ));
2919
+ }
2907
2920
return false;
2908
2921
}
2909
2922
ctx -> mem_buffer_mlocked = true;
2910
2923
return true;
2911
- }
2912
2924
#else // GGML_MLOCK_SUPPORT
2913
- bool ggml_mlock (struct ggml_context * ctx , char * * err_p ) {
2914
2925
* err_p = strdup ("can't mlock because it's not supported on this system" );
2915
2926
return false;
2916
- }
2917
2927
#endif // GGML_MLOCK_SUPPORT
2928
+ }
2918
2929
2919
2930
////////////////////////////////////////////////////////////////////////////////
2920
2931
0 commit comments