aboutsummaryrefslogtreecommitdiff
path: root/src/partition.c
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-10 08:29:58 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-10 08:33:56 +0000
commit776276d5c7e67356a880f3e31076d32cdc1c1882 (patch)
treecbde131f3d46338adee9c67ac0f988295736bdd4 /src/partition.c
downloadcoursework-776276d5c7e67356a880f3e31076d32cdc1c1882.tar.gz
publishHEADmaster
Diffstat (limited to 'src/partition.c')
-rw-r--r--src/partition.c134
1 files changed, 134 insertions, 0 deletions
diff --git a/src/partition.c b/src/partition.c
new file mode 100644
index 0000000..529f1ac
--- /dev/null
+++ b/src/partition.c
@@ -0,0 +1,134 @@
1#include <stdlib.h>
2#include <stdio.h>
3
4#define RATIO (66/33.)
5
6struct sizes {
7 size_t cpu;
8 size_t gpu;
9 size_t tail;
10};
11
12struct partition {
13 size_t offset;
14 size_t size;
15};
16
17struct sizes
18get_sizes (size_t size, int node_count__cpu, int node_count__gpu, int thread_count)
19{
20 int thread_count__cpu = thread_count*node_count__cpu + (thread_count - 1)*node_count__gpu;
21 int thread_count__gpu = node_count__gpu;
22
23 double weight__cpu = thread_count__cpu*RATIO;
24 double weight__gpu = thread_count__gpu;
25
26 double ratio__cpu = weight__cpu/(weight__cpu + weight__gpu);
27
28 struct sizes sizes;
29
30 size_t size__cpus = size * ratio__cpu;
31 size_t size__gpus = size - size__cpus;
32 sizes.cpu = (thread_count__cpu > 0) ? size__cpus/thread_count__cpu : 0;
33 sizes.gpu = (thread_count__gpu > 0) ? size__gpus/thread_count__gpu : 0;
34 sizes.tail = size - (thread_count__cpu*sizes.cpu + thread_count__gpu*sizes.gpu);
35
36 return sizes;
37}
38
39struct partition
40partition_node (struct sizes sizes, int node_count__cpu, int node_count__gpu,
41 int thread_count, int node_rank)
42{
43 struct partition partition;
44
45 if (node_rank < node_count__cpu) { // CPU NODE
46 partition.size = thread_count*sizes.cpu;
47 partition.offset = node_rank*partition.size;
48 } else { // GPU NODE
49 partition.size = (thread_count - 1)*sizes.cpu + sizes.gpu;
50 partition.offset = node_count__cpu*thread_count*sizes.cpu + (node_rank - node_count__cpu)*partition.size;
51 }
52
53 if (node_rank + 1 == node_count__cpu + node_count__gpu)
54 partition.size += sizes.tail;
55
56 return partition;
57}
58
59struct partition
60partition_thread (struct partition partition, struct sizes sizes, bool cpu, bool tail,
61 int thread_count, int thread)
62{
63 if (cpu) {
64 partition.size = sizes.cpu;
65 partition.offset += thread*partition.size;
66 } else {
67 if (thread == 0) {
68 partition.size = sizes.gpu;
69 } else {
70 partition.size = sizes.cpu;
71 partition.offset += sizes.gpu + (thread - 1)*partition.size;
72 }
73 }
74
75 if (tail)
76 partition.size += sizes.tail;
77
78 return partition;
79}
80
81void
82test (size_t size, int node_count__cpu, int node_count__gpu, int thread_count)
83{
84 printf ("%lu %d %d %d\n", size, node_count__cpu, node_count__gpu, thread_count);
85
86 int node_count = node_count__cpu + node_count__gpu;
87 for (int n = 0; n < node_count; ++n) {
88 struct sizes sizes = get_sizes (size, node_count__cpu, node_count__gpu, thread_count);
89 struct partition pn = partition_node (sizes, node_count__cpu, node_count__gpu, thread_count, n);
90 printf ("[%d/%d] %10lu %10lu %10lu\n", n + 1, node_count,
91 pn.offset, pn.offset + pn.size, pn.size);
92 for (int t = 0; t < thread_count; ++t) {
93 struct partition pt = partition_thread (pn, sizes, n < node_count__cpu,
94 n + 1 == node_count__cpu + node_count__gpu &&
95 t + 1 == thread_count, thread_count, t);
96 printf ("[%d/%d] [%d/%d] %10lu %10lu %10lu\n", n + 1, node_count, t + 1, thread_count,
97 pt.offset, pt.offset + pt.size, pt.size);
98 }
99 }
100}
101
102int
103main (int argc, char *argv[])
104{
105 test (1024, 2, 0, 4); putchar ('\n');
106 test (1024, 0, 2, 4); putchar ('\n');
107
108 test (1024, 1, 1, 1); putchar ('\n');
109 test (1024, 1, 1, 2); putchar ('\n');
110 test (1024, 1, 1, 8); putchar ('\n');
111
112 test (1024, 2, 2, 1); putchar ('\n');
113 test (1024, 2, 2, 2); putchar ('\n');
114 test (1024, 2, 2, 3); putchar ('\n');
115 test (1024, 2, 2, 8); putchar ('\n');
116
117 test (1024, 4, 1, 8); putchar ('\n');
118 test (1024, 1, 4, 8); putchar ('\n');
119
120 test (1000, 2, 2, 3); putchar ('\n');
121 test (1000, 3, 1, 4); putchar ('\n');
122 test (1000, 1, 3, 4); putchar ('\n');
123
124 test (7, 2, 2, 4); putchar ('\n');
125 test (3, 2, 2, 4); putchar ('\n');
126 test (1, 2, 2, 4); putchar ('\n');
127
128 test (0, 2, 2, 4); putchar ('\n');
129
130 test (1ULL<<20, 2, 2, 2); putchar ('\n');
131 test (1ULL<<32, 2, 2, 4); putchar ('\n');
132
133 return EXIT_SUCCESS;
134}