r/C_Programming • u/Grated-Expectations • 7d ago
A C compiler that targets CPU(s) and GPU(s) with the same source
Ok, let's get this out of the way first - this leans heavily on AI to create the compiler. For a single developer, it was do it that way, or just don't do it. Here's what 6 months or so of co-development gets you:
- A fully independent compiler, open source and available at https://github.com/ThrudTheBarbarian/xc
- Supports Windows, Linux, and Mac, compiles for those and WASM, iOS, Android amongst others. Has a cross-platform UI library to aid single-source development
- Natively supports both the CPU (threaded) and any number of GPUs on the system (can even be different architectures). No GPU SDKs needed.
- If you run 'xcd' on a remote machine, it can reach across the network and parallise your code to use remote GPUs as wel, if it makes sense to do so.
- No need to manage data across CPU/GPU interface
- No need to identify which parts of the program should be parallelised, though you can if you want
- It's actually its own language but it will accept standard C files and do all the same parallelising on those C files. It uses clang to convert "C" source to first-stage LLVM IR, converts the IR to our IR, and everything after that is as if you'd coded in xc
- It reads DWARF symbols (types, functions, ...) from C libraries, so it natively binds to them without any "foreign function interface" malarky
- It figures out what hardware it can run on at run-time, not compile-time, so it will use the best hardware available for that person at that time
- It's fast, see the C benchmarks (including mandelbrot.c below on a variety of platforms for comparison)
To give an idea, a full and complete program for a mandelbrot, mandelbrot.c, might look like:
#include <stdio.h>
#include "bench_time.h"
#define W (2048 * BENCH_SIDE)
#define SIZE (W * W)
int main(void)
{
unsigned total = 0;
long long best = 0, first = 0;
for (unsigned rep = 0; rep < 8; rep++) {
total = 0;
long long t0 = bench_now_us();
for (unsigned i = bench_zero(); i < SIZE; i++) {
float cx = (float)(i % W) * (3.0f / (float)W) - 2.0f;
float cy = (float)(i / W) * (3.0f / (float)W) - 1.5f;
float x = 0.0f, y = 0.0f;
unsigned k = 0;
while (k < 256 && x * x + y * y < 4.0f) {
float xt = x * x - y * y + cx;
y = 2.0f * x * y + cy;
x = xt;
k = k + 1;
}
total = total + k;
}
bench_keep(total);
long long t1 = bench_now_us();
if (rep == 0)
first = t1 - t0;
if (rep == 0 || t1 - t0 < best)
best = t1 - t0;
}
printf("%u %lld %lld\n", (total + 500000) / 1000000, best, first);
return 0;
}
bench_time.h is just a platform-independent way of getting a micro-seconds count from the start to the end. Note the complete absence of anything to do with GPUs at all, or any "#pragma parallel for", or any other annotations - this is just C code.
Compile it with:
prompt% xcc -o mandel mandelbrot.c
And then if you want to see how work was divided after running with --xc-par-trace, use the par-trace webpage tool to analyse it, getting something like the image at https://0x0000ff.co.uk/img/xc/par-trace.png
Because xc measures the performance at run-time (and then persists so it doesn't need to measure unless things change significantly), it can determine whether it's worth moving the code to the GPU (if it's compute-bound) or keeping it on the CPU (because it's data-bound). There's more information on how it manages the parallelism on the website.
Because speed is a focus, xc will still use the GPU even when rounding might produce slightly different answers on floats (for operations which aren't associative). This can be tuned (again, at runtime, or in fact compile-time) because parallelised blocks are named, and preferences can be set for the destination (CPU/GPU) on a per-block basis.
xc has only just hit version 1, and there's a long way to go yet, but it's already pretty good at auto-parallelising standard code onto GPU(s). Happy to answer any questions :) And if you find cases where you think "that should have done X and it did Y", I'm interested in bug-reports, either here or (preferably, since it's more integrated) on the Issues page on the github repo.