Skip to content

Instantly share code, notes, and snippets.

@Marenz
Created December 2, 2017 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Marenz/9f734331c01f65c19510fef93796830a to your computer and use it in GitHub Desktop.
Save Marenz/9f734331c01f65c19510fef93796830a to your computer and use it in GitHub Desktop.
struct ResizingRegionAllocator
{
private void[] memory;
private size_t position;
public uint alignment = 1;
public void[] allocate ( size_t size )
{
if (memory.length - position < size)
{
memory.length = size + position;
memory.assumeSafeAppend();
}
scope(exit)
position += size;
return memory[position .. position + size];
}
public void freeAll ( )
{
position = 0;
}
}
auto mtimes ( Mat1, Mat2, Alloc ) ( Mat1 m1, Mat2 m2, ref Alloc alloc )
{
import mir.ndslice.allocation;
import mir.ndslice.topology;
import glas.ndslice;
auto toSlice ( Mat ) ( Mat mat )
{
static if (is(typeof(mat.iterator) == float*))
return mat;
else
{
auto new_mat = makeUninitSlice!float(alloc, mat.shape);
new_mat[] = mat;
return new_mat;
}
}
assert (m1.shape.length == m2.shape.length);
assert (m1.shape[$-1] == m2.shape[0]);
auto tmp_result = makeUninitSlice!float(alloc, m1.shape[0], m2.shape[$-1]);
gemm(1.0f, toSlice(m1).universal, toSlice(m2).universal, 0.0f, tmp_result.universal);
return tmp_result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment