gpu_alloc/config.rs
1/// Configuration for [`GpuAllocator`]
2///
3/// [`GpuAllocator`]: type.GpuAllocator
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub struct Config {
7 /// Size in bytes of request that will be served by dedicated memory object.
8 /// This value should be large enough to not exhaust memory object limit
9 /// and not use slow memory object allocation when it is not necessary.
10 pub dedicated_threshold: u64,
11
12 /// Size in bytes of request that will be served by dedicated memory object if preferred.
13 /// This value should be large enough to not exhaust memory object limit
14 /// and not use slow memory object allocation when it is not necessary.
15 ///
16 /// This won't make much sense if this value is larger than `dedicated_threshold`.
17 pub preferred_dedicated_threshold: u64,
18
19 /// Size in bytes of transient memory request that will be served by dedicated memory object.
20 /// This value should be large enough to not exhaust memory object limit
21 /// and not use slow memory object allocation when it is not necessary.
22 ///
23 /// This won't make much sense if this value is lesser than `dedicated_threshold`.
24 pub transient_dedicated_threshold: u64,
25
26 /// Size in bytes of first chunk in free-list allocator.
27 pub starting_free_list_chunk: u64,
28
29 /// Upper limit for size in bytes of chunks in free-list allocator.
30 pub final_free_list_chunk: u64,
31
32 /// Minimal size for buddy allocator.
33 pub minimal_buddy_size: u64,
34
35 /// Initial memory object size for buddy allocator.
36 /// If less than `minimal_buddy_size` then `minimal_buddy_size` is used instead.
37 pub initial_buddy_dedicated_size: u64,
38}
39
40impl Config {
41 /// Returns default configuration.
42 ///
43 /// This is not `Default` implementation to discourage usage outside of
44 /// prototyping.
45 ///
46 /// Proper configuration should depend on hardware and intended usage.\
47 /// But those values can be used as starting point.\
48 /// Note that they can simply not work for some platforms with lesser
49 /// memory capacity than today's "modern" GPU (year 2020).
50 pub fn i_am_prototyping() -> Self {
51 // Assume that today's modern GPU is made of 1024 potatoes.
52 let potato = Config::i_am_potato();
53
54 Config {
55 dedicated_threshold: potato.dedicated_threshold * 1024,
56 preferred_dedicated_threshold: potato.preferred_dedicated_threshold * 1024,
57 transient_dedicated_threshold: potato.transient_dedicated_threshold * 1024,
58 starting_free_list_chunk: potato.starting_free_list_chunk * 1024,
59 final_free_list_chunk: potato.final_free_list_chunk * 1024,
60 minimal_buddy_size: potato.minimal_buddy_size * 1024,
61 initial_buddy_dedicated_size: potato.initial_buddy_dedicated_size * 1024,
62 }
63 }
64
65 /// Returns default configuration for average sized potato.
66 pub fn i_am_potato() -> Self {
67 Config {
68 dedicated_threshold: 32 * 1024,
69 preferred_dedicated_threshold: 1024,
70 transient_dedicated_threshold: 128 * 1024,
71 starting_free_list_chunk: 8 * 1024,
72 final_free_list_chunk: 128 * 1024,
73 minimal_buddy_size: 1,
74 initial_buddy_dedicated_size: 8 * 1024,
75 }
76 }
77}