@@ -2884,36 +2884,47 @@ size_t ggml_set_scratch(struct ggml_context * ctx, struct ggml_scratch scratch)
2884
2884
return result ;
2885
2885
}
2886
2886
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
+
2887
2896
bool ggml_mlock_supported (void ) {
2888
2897
return GGML_MLOCK_SUPPORT ;
2889
2898
}
2890
2899
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
2891
2906
#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 ) {
2899
2907
if (ctx -> mem_buffer_mlocked ) {
2900
2908
return true;
2901
2909
}
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
+ }
2906
2919
return false;
2907
2920
}
2908
2921
ctx -> mem_buffer_mlocked = true;
2909
2922
return true;
2910
- }
2911
2923
#else // GGML_MLOCK_SUPPORT
2912
- bool ggml_mlock (struct ggml_context * ctx , char * * err_p ) {
2913
2924
* err_p = strdup ("can't mlock because it's not supported on this system" );
2914
2925
return false;
2915
- }
2916
2926
#endif // GGML_MLOCK_SUPPORT
2927
+ }
2917
2928
2918
2929
////////////////////////////////////////////////////////////////////////////////
2919
2930
0 commit comments