Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/MeshKernelNET/Api/DisposableContacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public int NumContacts

protected override void SetNativeObject(ref ContactsNative nativeObject)
{
nativeObject.mesh1d_indices = GetPinnedObjectPointer(Mesh1dIndices);
nativeObject.mesh2d_indices = GetPinnedObjectPointer(Mesh2dIndices);
nativeObject.mesh1d_indices = GetPinnedObjectPointer(0);
nativeObject.mesh2d_indices = GetPinnedObjectPointer(1);
nativeObject.num_contacts = NumContacts;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/MeshKernelNET/Api/DisposableCurvilinearGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ public double GetNodeY(int nodeIndex)

protected override void SetNativeObject(ref CurvilinearGridNative nativeObject)
{
nativeObject.node_x = GetPinnedObjectPointer(NodeX);
nativeObject.node_y = GetPinnedObjectPointer(NodeY);
nativeObject.node_x = GetPinnedObjectPointer(0);
nativeObject.node_y = GetPinnedObjectPointer(1);
nativeObject.num_m = numM;
nativeObject.num_n = numN;
}
Expand Down
6 changes: 3 additions & 3 deletions src/MeshKernelNET/Api/DisposableGeometryList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ public double[] Values

protected override void SetNativeObject(ref GeometryListNative nativeObject)
{
nativeObject.xCoordinates = GetPinnedObjectPointer(XCoordinates);
nativeObject.yCoordinates = GetPinnedObjectPointer(YCoordinates);
nativeObject.zCoordinates = GetPinnedObjectPointer(Values);
nativeObject.xCoordinates = GetPinnedObjectPointer(0);
nativeObject.yCoordinates = GetPinnedObjectPointer(1);
nativeObject.zCoordinates = GetPinnedObjectPointer(2);
nativeObject.numberOfCoordinates = NumberOfCoordinates;
nativeObject.geometrySeperator = GeometrySeparator;
nativeObject.innerOuterSeperator = InnerOuterSeparator;
Expand Down
6 changes: 3 additions & 3 deletions src/MeshKernelNET/Api/DisposableGriddedSamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ protected override void SetNativeObject(ref GriddedSamplesNative nativeObject)
nativeObject.origin_x = originX;
nativeObject.origin_y = originY;
nativeObject.cell_size = cellSize;
nativeObject.coordinates_x = GetPinnedObjectPointer(coordinatesX);
nativeObject.coordinates_y = GetPinnedObjectPointer(coordinatesY);
nativeObject.values = GetPinnedObjectPointer(values);
nativeObject.coordinates_x = GetPinnedObjectPointer(0);
nativeObject.coordinates_y = GetPinnedObjectPointer(1);
nativeObject.values = GetPinnedObjectPointer(2);
nativeObject.value_type = valueType;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/MeshKernelNET/Api/DisposableMesh1D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ public int NumValidEdges

protected override void SetNativeObject(ref Mesh1DNative nativeObject)
{
nativeObject.edge_nodes = GetPinnedObjectPointer(EdgeNodes);
nativeObject.node_x = GetPinnedObjectPointer(NodeX);
nativeObject.node_y = GetPinnedObjectPointer(NodeY);
nativeObject.edge_nodes = GetPinnedObjectPointer(0);
nativeObject.node_x = GetPinnedObjectPointer(1);
nativeObject.node_y = GetPinnedObjectPointer(2);
nativeObject.num_valid_nodes = numValidNodes;
nativeObject.num_nodes = NumNodes;
nativeObject.num_edges = NumEdges;
Expand Down
176 changes: 164 additions & 12 deletions src/MeshKernelNET/Api/DisposableMesh2D.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using MeshKernelNET.Native;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using MeshKernelNET.Native;
using ProtoBuf;

namespace MeshKernelNET.Api
Expand Down Expand Up @@ -59,6 +63,33 @@ public sealed class DisposableMesh2D : DisposableNativeObject<Mesh2DNative>, IRe

public DisposableMesh2D()
{
NumNodes = 0;
NumEdges = 0;
NumFaces = 0;
NumFaceNodes = 0;
}

public DisposableMesh2D(DisposableMesh2D source)
{
NumNodes = source.NumNodes;
NumEdges = source.NumEdges;
NumFaces = source.NumFaces;
NumFaceNodes = source.NumFaceNodes;


edgeFaces = (int[])source.edgeFaces?.Clone();
edgeNodes = (int[])source.edgeNodes?.Clone();
faceEdges = (int[])source.faceEdges?.Clone();
faceNodes = (int[])source.faceNodes?.Clone();
nodesPerFace = (int[])source.nodesPerFace?.Clone();
nodeX = (double[])source.nodeX?.Clone();
nodeY = (double[])source.nodeY?.Clone();
edgeX = (double[])source.edgeX?.Clone();
edgeY = (double[])source.edgeY?.Clone();
faceX = (double[])source.faceX?.Clone();
faceY = (double[])source.faceY?.Clone();
numValidNodes = source.numValidNodes;
numValidEdges = source.numValidEdges;
}

public DisposableMesh2D(int nNodes, int nEdges, int nFaces, int nFaceNodes)
Expand All @@ -81,6 +112,126 @@ public DisposableMesh2D(int nNodes, int nEdges, int nFaces, int nFaceNodes)
FaceY = new double[NumFaces];
}

private bool HasChanged(int nNodes, int nEdges, int nFaces)
{
if (nNodes > NumNodes)
{
return true;
}
if (nEdges > NumEdges)
{
return true;
}
if (nFaces > NumFaces)
{
return true;
}

return false;
}


public void Resize(int nNodes, int nEdges, int nFaces, int nFaceNodes)
{
bool hasChanged = HasChanged(nNodes, nEdges, nFaces);
if (hasChanged)
{
UnPinMemory();
}

if (nNodes > NumNodes)
{
double[] newNodeX = new double[nNodes];
double[] newNodeY = new double[nNodes];
if (NodeX != null)
{
Array.Copy(NodeY, newNodeY, NumNodes);
}
if (NodeY != null)
{
Array.Copy(NodeY, newNodeY, NumNodes);
}
NodeX = newNodeX;
NodeY = newNodeY;
NumNodes = nNodes;
}

if (nEdges > NumEdges)
{
double[] newEdgeX = new double[nEdges];
double[] newEdgeY = new double[nEdges];
int[] newEdgeFaces = new int[nEdges * 2];
int[] newEdgeNodes = new int[nEdges * 2];
if (EdgeX != null)
{
Array.Copy(EdgeX, newEdgeX, NumEdges);
}
if (EdgeY != null)
{
Array.Copy(EdgeY, newEdgeY, NumEdges);
}
if (EdgeFaces != null)
{
Array.Copy(EdgeFaces, newEdgeFaces, NumEdges * 2);
}
if (EdgeNodes != null)
{
Array.Copy(EdgeNodes, newEdgeNodes, NumEdges * 2);
}
EdgeX = newEdgeX;
EdgeY = newEdgeY;
EdgeFaces = newEdgeFaces;
EdgeNodes = newEdgeNodes;
NumEdges = nEdges;
}

if (nFaces > NumFaces)
{
double[] newFaceX = new double[nFaces];
double[] newFaceY = new double[nFaces];
int[] newNodesPerFace = new int[nFaces];
if (FaceX != null)
{
Array.Copy(FaceX, newFaceX, NumFaces);
}
if (FaceY != null)
{
Array.Copy(FaceY, newFaceY, NumFaces);
}
if (NodesPerFace != null)
{
Array.Copy(NodesPerFace, newNodesPerFace, NumFaces);
}
FaceX = newFaceX;
FaceY = newFaceY;
NodesPerFace = newNodesPerFace;
NumFaces = nFaces;
}

if (nFaceNodes > NumFaceNodes)
{
int[] newFaceEdges = new int[nFaceNodes];
int[] newFaceNodes = new int[nFaceNodes];
if (FaceEdges != null)
{
Array.Copy(FaceEdges, newFaceEdges, NumFaceNodes);
}
if (FaceNodes != null)
{
Array.Copy(FaceNodes, newFaceNodes, NumFaceNodes);
}

FaceEdges = newFaceEdges;
FaceNodes = newFaceNodes;
NumFaceNodes = nFaceNodes;
}

if (hasChanged)
{
PinMemory();
}
}

~DisposableMesh2D()
{
Dispose(false);
Expand Down Expand Up @@ -240,17 +391,18 @@ public double GetNodeY(int nodeIndex)

protected override void SetNativeObject(ref Mesh2DNative nativeObject)
{
nativeObject.edge_faces = GetPinnedObjectPointer(EdgeFaces);
nativeObject.edge_nodes = GetPinnedObjectPointer(EdgeNodes);
nativeObject.face_edges = GetPinnedObjectPointer(FaceEdges);
nativeObject.face_nodes = GetPinnedObjectPointer(FaceNodes);
nativeObject.nodes_per_face = GetPinnedObjectPointer(NodesPerFace);
nativeObject.node_x = GetPinnedObjectPointer(NodeX);
nativeObject.node_y = GetPinnedObjectPointer(NodeY);
nativeObject.edge_x = GetPinnedObjectPointer(EdgeX);
nativeObject.edge_y = GetPinnedObjectPointer(EdgeY);
nativeObject.face_x = GetPinnedObjectPointer(FaceX);
nativeObject.face_y = GetPinnedObjectPointer(FaceY);
nativeObject.edge_faces = GetPinnedObjectPointer(0);
nativeObject.edge_nodes = GetPinnedObjectPointer(1);
nativeObject.face_edges = GetPinnedObjectPointer(2);
nativeObject.face_nodes = GetPinnedObjectPointer(3);
nativeObject.nodes_per_face = GetPinnedObjectPointer(4);
nativeObject.node_x = GetPinnedObjectPointer(5);
nativeObject.node_y = GetPinnedObjectPointer(6);
nativeObject.edge_x = GetPinnedObjectPointer(7);
nativeObject.edge_y = GetPinnedObjectPointer(8);
nativeObject.face_x = GetPinnedObjectPointer(9);
nativeObject.face_y = GetPinnedObjectPointer(10);

nativeObject.num_nodes = NumNodes;
nativeObject.num_valid_nodes = NumValidNodes;
nativeObject.num_edges = NumEdges;
Expand Down
30 changes: 19 additions & 11 deletions src/MeshKernelNET/Api/DisposableNativeObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace MeshKernelNET.Api
/// </summary>
public abstract class DisposableNativeObject<TNative> : IDisposable where TNative : new()
{
private readonly Dictionary<object, GCHandle> objectGarbageCollectHandles = new Dictionary<object, GCHandle>();
private readonly Dictionary<int, GCHandle> objectGarbageCollectHandles = new Dictionary<int, GCHandle>();
private bool disposed;

/// <summary>
Expand Down Expand Up @@ -63,11 +63,12 @@ public TNative CreateNativeObject()
/// <summary>
/// Get the pointer to the pinned object
/// </summary>
/// <param name="objectToLookUp">Object to get </param>
/// <param name="objectId">Object to get </param>
/// <returns></returns>
protected IntPtr GetPinnedObjectPointer(object objectToLookUp)
protected IntPtr GetPinnedObjectPointer(int objectId)
{
return objectGarbageCollectHandles[objectToLookUp].AddrOfPinnedObject();
var addressPinnedObject = objectGarbageCollectHandles[objectId].AddrOfPinnedObject();
return addressPinnedObject;
}

/// <summary>
Expand All @@ -92,11 +93,12 @@ protected virtual void Dispose(bool disposing)
/// <summary>
/// Pins the arrays in memory (no garbage collect until unpinned (done in dispose))
/// </summary>
private void PinMemory()
public void PinMemory()
{
IEnumerable<PropertyInfo> arrayProperties = GetType().GetProperties().Where(f => f.PropertyType.IsArray);

// force initialization
int keyCounter = 0;
foreach (PropertyInfo arrayProperty in arrayProperties)
{
Type elementType = arrayProperty.PropertyType.GetElementType();
Expand All @@ -117,28 +119,34 @@ private void PinMemory()
}

byte[] bytes = ((string[])objectToPin).GetFlattenedAsciiCodedStringArray(bufferSize);
AddObjectToPin(bytes, objectToPin);
AddObjectToPin(bytes, keyCounter);
}
else
{
AddObjectToPin(objectToPin);
AddObjectToPin(objectToPin, keyCounter);
}

keyCounter++;
}
}

private void UnPinMemory()
public void UnPinMemory()
{
foreach (KeyValuePair<object, GCHandle> valuePair in objectGarbageCollectHandles)
if (objectGarbageCollectHandles.Count <= 0)
{
return;
}

foreach (KeyValuePair<int, GCHandle> valuePair in objectGarbageCollectHandles)
{
valuePair.Value.Free();
}

objectGarbageCollectHandles.Clear();
}

private void AddObjectToPin(object objectToPin, object lookupObject = null)
private void AddObjectToPin(object objectToPin, int key)
{
object key = lookupObject ?? objectToPin;
objectGarbageCollectHandles.Add(key, GCHandle.Alloc(objectToPin, GCHandleType.Pinned));
}

Expand Down
Loading