From 776276d5c7e67356a880f3e31076d32cdc1c1882 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Tue, 10 Nov 2026 11:29:58 +0300 Subject: publish --- src/partition.c | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 src/partition.c (limited to 'src/partition.c') 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 @@ +#include +#include + +#define RATIO (66/33.) + +struct sizes { + size_t cpu; + size_t gpu; + size_t tail; +}; + +struct partition { + size_t offset; + size_t size; +}; + +struct sizes +get_sizes (size_t size, int node_count__cpu, int node_count__gpu, int thread_count) +{ + int thread_count__cpu = thread_count*node_count__cpu + (thread_count - 1)*node_count__gpu; + int thread_count__gpu = node_count__gpu; + + double weight__cpu = thread_count__cpu*RATIO; + double weight__gpu = thread_count__gpu; + + double ratio__cpu = weight__cpu/(weight__cpu + weight__gpu); + + struct sizes sizes; + + size_t size__cpus = size * ratio__cpu; + size_t size__gpus = size - size__cpus; + sizes.cpu = (thread_count__cpu > 0) ? size__cpus/thread_count__cpu : 0; + sizes.gpu = (thread_count__gpu > 0) ? size__gpus/thread_count__gpu : 0; + sizes.tail = size - (thread_count__cpu*sizes.cpu + thread_count__gpu*sizes.gpu); + + return sizes; +} + +struct partition +partition_node (struct sizes sizes, int node_count__cpu, int node_count__gpu, + int thread_count, int node_rank) +{ + struct partition partition; + + if (node_rank < node_count__cpu) { // CPU NODE + partition.size = thread_count*sizes.cpu; + partition.offset = node_rank*partition.size; + } else { // GPU NODE + partition.size = (thread_count - 1)*sizes.cpu + sizes.gpu; + partition.offset = node_count__cpu*thread_count*sizes.cpu + (node_rank - node_count__cpu)*partition.size; + } + + if (node_rank + 1 == node_count__cpu + node_count__gpu) + partition.size += sizes.tail; + + return partition; +} + +struct partition +partition_thread (struct partition partition, struct sizes sizes, bool cpu, bool tail, + int thread_count, int thread) +{ + if (cpu) { + partition.size = sizes.cpu; + partition.offset += thread*partition.size; + } else { + if (thread == 0) { + partition.size = sizes.gpu; + } else { + partition.size = sizes.cpu; + partition.offset += sizes.gpu + (thread - 1)*partition.size; + } + } + + if (tail) + partition.size += sizes.tail; + + return partition; +} + +void +test (size_t size, int node_count__cpu, int node_count__gpu, int thread_count) +{ + printf ("%lu %d %d %d\n", size, node_count__cpu, node_count__gpu, thread_count); + + int node_count = node_count__cpu + node_count__gpu; + for (int n = 0; n < node_count; ++n) { + struct sizes sizes = get_sizes (size, node_count__cpu, node_count__gpu, thread_count); + struct partition pn = partition_node (sizes, node_count__cpu, node_count__gpu, thread_count, n); + printf ("[%d/%d] %10lu %10lu %10lu\n", n + 1, node_count, + pn.offset, pn.offset + pn.size, pn.size); + for (int t = 0; t < thread_count; ++t) { + struct partition pt = partition_thread (pn, sizes, n < node_count__cpu, + n + 1 == node_count__cpu + node_count__gpu && + t + 1 == thread_count, thread_count, t); + printf ("[%d/%d] [%d/%d] %10lu %10lu %10lu\n", n + 1, node_count, t + 1, thread_count, + pt.offset, pt.offset + pt.size, pt.size); + } + } +} + +int +main (int argc, char *argv[]) +{ + test (1024, 2, 0, 4); putchar ('\n'); + test (1024, 0, 2, 4); putchar ('\n'); + + test (1024, 1, 1, 1); putchar ('\n'); + test (1024, 1, 1, 2); putchar ('\n'); + test (1024, 1, 1, 8); putchar ('\n'); + + test (1024, 2, 2, 1); putchar ('\n'); + test (1024, 2, 2, 2); putchar ('\n'); + test (1024, 2, 2, 3); putchar ('\n'); + test (1024, 2, 2, 8); putchar ('\n'); + + test (1024, 4, 1, 8); putchar ('\n'); + test (1024, 1, 4, 8); putchar ('\n'); + + test (1000, 2, 2, 3); putchar ('\n'); + test (1000, 3, 1, 4); putchar ('\n'); + test (1000, 1, 3, 4); putchar ('\n'); + + test (7, 2, 2, 4); putchar ('\n'); + test (3, 2, 2, 4); putchar ('\n'); + test (1, 2, 2, 4); putchar ('\n'); + + test (0, 2, 2, 4); putchar ('\n'); + + test (1ULL<<20, 2, 2, 2); putchar ('\n'); + test (1ULL<<32, 2, 2, 4); putchar ('\n'); + + return EXIT_SUCCESS; +} -- cgit v1.3.1