<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>How-to</title>
<meta name="description" content="Embed minimage from C, C++, and Python. pkg-config." />
<meta name="generator" content="Org Mode" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" href="assets/minimage-icon.svg" type="image/svg+xml" />
<link rel="stylesheet" href="css/site.css" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link href="https://fonts.googleapis.com/css2?family=Atkinson+Hyperlegible:wght@400;700&family=JetBrains+Mono:wght@500;700&display=swap" rel="stylesheet" />
</head>
<body>
<div id="org-div-home-and-up">
<a accesskey="h" href="index.html"> UP </a>
|
<a accesskey="H" href="index.html"> HOME </a>
</div><div id="content" class="content">
<nav class="top" aria-label="minimage">
<a class="brand" href="index.html">minimage</a>
<a href="getting-started.html">Install</a>
<a href="tutorial.html">Tutorial</a>
<a href="howto.html">How-to</a>
<a href="explanation.html">Explain</a>
<a href="reference.html">Reference</a>
<a href="https://lode-org.github.io/">LODE</a>
</nav>
<div class="rule" aria-hidden="true"></div>
<section id="outline-container-org47d7a02" class="outline-2">
<h2 id="org47d7a02">Embed from C</h2>
<div class="outline-text-2" id="text-org47d7a02">
<div class="org-src-container">
<pre class="src src-c">#include "minimage.h"
mi_cell box = mi_cell_ortho(10.0, 10.0, 10.0);
const double p[3] = {0.2, 0.0, 0.0};
const double q[3] = {9.4, 0.0, 0.0};
double d2 = 0.0;
if (mi_dist2(&box, p, q, &d2) != 0) {
fprintf(stderr, "%s\n", mi_last_error());
}
</pre>
</div>
<p>
Link <code>libminimage</code> and add <code>include/</code> to the include path. Meson
exposes <code>minimage_dep</code>. CMake exposes <code>minimage::minimage</code>.
</p>
</div>
</section>
<section id="outline-container-orgfb381bc" class="outline-2">
<h2 id="orgfb381bc">Embed from C++</h2>
<div class="outline-text-2" id="text-orgfb381bc">
<div class="org-src-container">
<pre class="src src-cpp">#include "minimage.hpp"
const minimage::Cell box = minimage::Cell::ortho(10.0, 10.0, 10.0);
const double d2 = box.dist2({0.2, 0.0, 0.0}, {9.4, 0.0, 0.0});
</pre>
</div>
<p>
The header is a RAII wrap of <code>minimage.h</code>. Failures throw
<code>minimage::Error</code>.
</p>
</div>
</section>
<section id="outline-container-org7f76993" class="outline-2">
<h2 id="org7f76993">Embed from Python</h2>
<div class="outline-text-2" id="text-org7f76993">
<div class="org-src-container">
<pre class="src src-python">import minimage
cell = minimage.Cell.ortho(10.0, 10.0, 10.0)
print(cell.dist2([0.2, 0.0, 0.0], [9.4, 0.0, 0.0]))
d2 = cell.dist2_pairs(ps, qs) # (n, 3) arrays in, numpy out
f = cell.fixed_many(positions) # uint64 (n, 3)
d2 = cell.dist2_pairs_fixed(f[i], f[j])
</pre>
</div>
<p>
Array arguments go through DLPack: numpy, PyTorch, JAX, and CuPy host
arrays are read in place, and the result is a numpy array that owns
its buffer. A list in gives a list out. A device array raises; move it
to the host, or run the wrap on the device with <code>minimage-burn</code>.
</p>
</div>
</section>
<section id="outline-container-orgafe3f95" class="outline-2">
<h2 id="orgafe3f95">Run on a GPU with Burn</h2>
<div class="outline-text-2" id="text-orgafe3f95">
<p>
<code>burn/</code> is the <code>minimage-burn</code> crate, outside the workspace because
Burn needs Rust 1.95. Enable the backend features you need (<code>wgpu</code>,
<code>vulkan</code>, <code>metal</code>, <code>cuda</code>, <code>rocm</code>, <code>cpu</code>, <code>flex</code>, <code>fusion</code>).
</p>
<div class="org-src-container">
<pre class="src src-rust">use burn_tensor::{DType, Device, DeviceKind};
use minimage_burn::{to_vec, Lattice};
let device = Device::wgpu(DeviceKind::DefaultDevice);
let lat = Lattice::new(&cell, &device, DType::F32);
let sp = lat.fractions(&cell, &ps);
let sq = lat.fractions(&cell, &qs);
let d2 = to_vec(lat.dist2_pairs_fractional(sp, sq));
</pre>
</div>
<p>
<code>fractions</code> folds on the CPU in double precision and uploads; the wrap
on the device is then exact in single or half precision.
<code>dist2_pairs</code>, <code>dist2_many</code>, and <code>wrap</code> take Cartesian rows instead.
</p>
</div>
</section>
<section id="outline-container-org512ef66" class="outline-2">
<h2 id="org512ef66">pkg-config</h2>
<div class="outline-text-2" id="text-org512ef66">
<div class="org-src-container">
<pre class="src src-text">pkg-config --cflags --libs minimage
</pre>
</div>
<p>
Meson and CMake both write <code>minimage.pc</code>. <code>Libs</code> includes <code>-L${libdir}</code>
so a wrap consumer can find <code>-lminimage</code>.
</p>
</div>
</section>
</div>
</body>
</html>