Skip to content

Commit 83147b0

Browse files
committed
feat: add sprux_begin_capture/sprux_end_capture to C API
Expose MetalContext::beginCapture/endCapture through the C API so embedders (JAX FFI, Python) can bracket GPU trace captures around specific code sections, avoiding capturing JIT warmup noise. Co-developed-by: Claude Code v2.1.81 (claude-opus-4-6)
1 parent d2d53a6 commit 83147b0

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

sprux/sprux/sprux_c_api.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
#include "sprux/sprux/Solver.h"
1616
#include "sprux/sprux/SpruxFFISolver.h"
1717

18+
#ifdef SPRUX_USE_METAL
19+
#include "sprux/sprux/MetalDefs.h"
20+
#endif
21+
1822
using namespace Sprux;
1923

2024
struct sprux_solver {
@@ -193,4 +197,26 @@ int sprux_ffi_dot(sprux_ffi_solver_t h, const double* csr_data, const double* x,
193197
}
194198
}
195199

200+
int sprux_begin_capture(const char* output_path) {
201+
#ifdef SPRUX_USE_METAL
202+
try {
203+
return MetalContext::instance().beginCapture(output_path) ? 1 : 0;
204+
} catch (...) {
205+
return 0;
206+
}
207+
#else
208+
(void)output_path;
209+
return 0;
210+
#endif
211+
}
212+
213+
void sprux_end_capture(void) {
214+
#ifdef SPRUX_USE_METAL
215+
try {
216+
MetalContext::instance().endCapture();
217+
} catch (...) {
218+
}
219+
#endif
220+
}
221+
196222
} // extern "C"

sprux/sprux/sprux_c_api.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,20 @@ int sprux_ffi_solve(sprux_ffi_solver_t h, const double* csr_data, const double*
197197
*/
198198
int sprux_ffi_dot(sprux_ffi_solver_t h, const double* csr_data, const double* x, double* b_out);
199199

200+
/**
201+
* Begin a GPU trace capture. Writes a .gputrace file for analysis in
202+
* Xcode or apple-profiler tools.
203+
*
204+
* @param output_path Path for .gputrace output (e.g. "/tmp/sprux.gputrace")
205+
* @return 1 if capture started, 0 if not (Metal not available or already capturing)
206+
*/
207+
int sprux_begin_capture(const char* output_path);
208+
209+
/**
210+
* End a GPU trace capture started with sprux_begin_capture.
211+
*/
212+
void sprux_end_capture(void);
213+
200214
#ifdef __cplusplus
201215
}
202216
#endif

0 commit comments

Comments
 (0)