Skip to content

Commit

Permalink
It appears intentionally clearing lists and setting variables to null…
Browse files Browse the repository at this point in the history
… as soon as they're no more needed has an impact on memory footprint.
  • Loading branch information
smbadiwe committed Nov 10, 2017
1 parent 368f314 commit bdfdc39
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 41 deletions.
16 changes: 8 additions & 8 deletions MODA.Impl/ModaAlgorithms.1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static Dictionary<QueryGraph, string> Algorithm1_C(UndirectedGraph<int> i
{
qGraph = GetNextNode()?.QueryGraph;
if (qGraph == null) break;
IList<Mapping> mappings;
ICollection<Mapping> mappings;
if (qGraph.EdgeCount == (subgraphSize - 1)) // i.e. if qGraph is a tree
{
var inputGraphClone = inputGraph.Clone();
Expand Down Expand Up @@ -98,7 +98,7 @@ public static Dictionary<QueryGraph, string> Algorithm1_C(UndirectedGraph<int> i
}
else
{
List<Mapping> mappings;
ICollection<Mapping> mappings;
if (UseModifiedGrochow)
{
// Modified Mapping module - MODA and Grockow & Kellis
Expand Down Expand Up @@ -127,23 +127,23 @@ public static Dictionary<QueryGraph, string> Algorithm1_C(UndirectedGraph<int> i
/// <param name="subgraphSize"></param>
/// <param name="thresholdValue">Frequency value, above which we can comsider the subgraph a "frequent subgraph"</param>
/// <returns></returns>
public static Dictionary<QueryGraph, IList<Mapping>> Algorithm1(UndirectedGraph<int> inputGraph, QueryGraph qGraph, int subgraphSize = -1, int thresholdValue = 0)
public static Dictionary<QueryGraph, ICollection<Mapping>> Algorithm1(UndirectedGraph<int> inputGraph, QueryGraph qGraph, int subgraphSize = -1, int thresholdValue = 0)
{
// The enumeration module (Algo 3) needs the mappings generated from the previous run(s)
Dictionary<QueryGraph, IList<Mapping>> allMappings;
Dictionary<QueryGraph, ICollection<Mapping>> allMappings;
int numIterations = -1;
if (inputGraph.VertexCount < 121) numIterations = inputGraph.VertexCount;

if (qGraph == null) // Use MODA's expansion tree
{
#region Use MODA's expansion tree
var treatedNodes = new HashSet<QueryGraph>();
allMappings = new Dictionary<QueryGraph, IList<Mapping>>(_builder.NumberOfQueryGraphs);
allMappings = new Dictionary<QueryGraph, ICollection<Mapping>>(_builder.NumberOfQueryGraphs);
do
{
qGraph = GetNextNode()?.QueryGraph;
if (qGraph == null) break;
IList<Mapping> mappings;
ICollection<Mapping> mappings;
if (qGraph.IsTree(subgraphSize))
{
var inputGraphClone = inputGraph.Clone();
Expand Down Expand Up @@ -200,7 +200,7 @@ public static Dictionary<QueryGraph, IList<Mapping>> Algorithm1(UndirectedGraph<
}
else
{
List<Mapping> mappings;
ICollection<Mapping> mappings;
if (UseModifiedGrochow)
{
// Modified Mapping module - MODA and Grockow & Kellis
Expand All @@ -213,7 +213,7 @@ public static Dictionary<QueryGraph, IList<Mapping>> Algorithm1(UndirectedGraph<
}

qGraph.RemoveNonApplicableMappings(mappings, inputGraph);
allMappings = new Dictionary<QueryGraph, IList<Mapping>>(1) { { qGraph, mappings } };
allMappings = new Dictionary<QueryGraph, ICollection<Mapping>>(1) { { qGraph, mappings } };
}

return allMappings;
Expand Down
25 changes: 14 additions & 11 deletions MODA.Impl/ModaAlgorithms.2.Modified.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
//This is the one that has gone bad
using QuickGraph;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MODA.Impl
{
Expand All @@ -25,17 +21,17 @@ public partial class ModaAlgorithms
/// <param name="queryGraph">H</param>
/// <param name="inputGraph">G</param>
/// <param name="numberOfSamples">To be decided. If not set, we use the <paramref name="inputGraph"/> size / 3</param>
private static List<Mapping> Algorithm2_Modified(QueryGraph queryGraph, UndirectedGraph<int> inputGraph, int numberOfSamples, bool getInducedMappingsOnly)
private static ICollection<Mapping> Algorithm2_Modified(QueryGraph queryGraph, UndirectedGraph<int> inputGraph, int numberOfSamples, bool getInducedMappingsOnly)
{
if (numberOfSamples <= 0) numberOfSamples = inputGraph.VertexCount / 3;

var theMappings = new Dictionary<IList<int>, List<Mapping>>(MappingNodesComparer);
var inputGraphDegSeq = inputGraph.GetNodesSortedByDegree(numberOfSamples);

var threadName = Thread.CurrentThread.ManagedThreadId;
Console.WriteLine("Thread {0}:\tCalling Algo 2-Modified:\n", threadName);

var queryGraphEdges = queryGraph.Edges.ToList();
var queryGraphEdges = queryGraph.Edges.ToArray();
var h = queryGraph.Vertices.ElementAt(0);
var f = new Dictionary<int, int>(1);
for (int i = 0; i < inputGraphDegSeq.Count; i++)
Expand All @@ -62,18 +58,25 @@ private static List<Mapping> Algorithm2_Modified(QueryGraph queryGraph, Undirect
theMappings[item.Key] = item.Value;
}
}
mappings = null;
}
mappings.Clear();
mappings = null;
#endregion
}
}

f.Clear();
f = null;
Array.Clear(queryGraphEdges, 0, queryGraphEdges.Length);
queryGraphEdges = null;
inputGraphDegSeq.Clear();
inputGraphDegSeq = null;
var toReturn = new HashSet<Mapping>(theMappings.Values.SelectMany(s => s));
theMappings.Clear();
theMappings = null;
inputGraphDegSeq = null;


Console.WriteLine("\nThread {0}:\tAlgorithm 2: All iteration tasks completed. Number of mappings found: {1}.\n", threadName, toReturn.Count);
return toReturn.ToList();
return toReturn;
}

}
Expand Down
20 changes: 15 additions & 5 deletions MODA.Impl/ModaAlgorithms.2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public partial class ModaAlgorithms
/// <param name="queryGraph">H</param>
/// <param name="inputGraphClone">G</param>
/// <param name="numberOfSamples">To be decided. If not set, we use the <paramref name="inputGraphClone"/> size / 3</param>
internal static List<Mapping> Algorithm2(QueryGraph queryGraph, UndirectedGraph<int> inputGraphClone, int numberOfSamples, bool getInducedMappingsOnly)
internal static ICollection<Mapping> Algorithm2(QueryGraph queryGraph, UndirectedGraph<int> inputGraphClone, int numberOfSamples, bool getInducedMappingsOnly)
{
if (numberOfSamples <= 0) numberOfSamples = inputGraphClone.VertexCount / 3;

Expand All @@ -22,7 +22,7 @@ internal static List<Mapping> Algorithm2(QueryGraph queryGraph, UndirectedGraph<
var theMappings = new Dictionary<IList<int>, List<Mapping>>(MappingNodesComparer);
var inputGraphDegSeq = inputGraphClone.GetNodesSortedByDegree(numberOfSamples);
var queryGraphVertices = queryGraph.Vertices.ToArray();
var queryGraphEdges = queryGraph.Edges.ToList();
var queryGraphEdges = queryGraph.Edges.ToArray();
var subgraphSize = queryGraphVertices.Length;
var threadName = System.Threading.Thread.CurrentThread.ManagedThreadId;
Console.WriteLine("Thread {0}:\tCallingu Algo 2:\n", threadName);
Expand All @@ -39,7 +39,8 @@ internal static List<Mapping> Algorithm2(QueryGraph queryGraph, UndirectedGraph<
var f = new Dictionary<int, int>(1);
f[h] = g;
var mappings = Utils.IsomorphicExtension(f, queryGraph, queryGraphEdges, inputGraphClone, getInducedMappingsOnly);

f.Clear();
f = null;
if (mappings.Count > 0)
{
foreach (var item in mappings)
Expand All @@ -56,17 +57,26 @@ internal static List<Mapping> Algorithm2(QueryGraph queryGraph, UndirectedGraph<
}
}
}
mappings.Clear();
mappings = null;
#endregion
}
}

//Remove g
inputGraphClone.RemoveVertex(g);
}
Array.Clear(queryGraphEdges, 0, queryGraphEdges.Length);
queryGraphEdges = null;
inputGraphDegSeq.Clear();
inputGraphDegSeq = null;

var toReturn = new HashSet<Mapping>(theMappings.Values.SelectMany(s => s));

theMappings.Clear();
theMappings = null;

Console.WriteLine("Thread {0}:\tAlgorithm 2: All tasks completed. Number of mappings found: {1}.", threadName, toReturn.Count);
return toReturn.ToList();
return toReturn;
}
}
}
10 changes: 5 additions & 5 deletions MODA.Impl/ModaAlgorithms.3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public partial class ModaAlgorithms
/// <param name="parentQueryGraph"></param>
/// <param name="fileName"></param>
/// <param name="parentGraphMappings">NB: This param is still used even outside this method is call. So, be careful how you set/clear its values.</param>
private static IList<Mapping> Algorithm3(Dictionary<QueryGraph, IList<Mapping>> allMappings, UndirectedGraph<int> inputGraph, QueryGraph queryGraph,
private static IList<Mapping> Algorithm3(Dictionary<QueryGraph, ICollection<Mapping>> allMappings, UndirectedGraph<int> inputGraph, QueryGraph queryGraph,
AdjacencyGraph<ExpansionTreeNode> expansionTree,
QueryGraph parentQueryGraph, out string newFileName, string fileName = null)
{
newFileName = null;
IList<Mapping> parentGraphMappings;
ICollection<Mapping> parentGraphMappings;
if (string.IsNullOrWhiteSpace(fileName))
{
if (!allMappings.TryGetValue(parentQueryGraph, out parentGraphMappings))
Expand Down Expand Up @@ -53,7 +53,7 @@ private static IList<Mapping> Algorithm3(Dictionary<QueryGraph, IList<Mapping>>

var list = new List<Mapping>();
int oldCount = parentGraphMappings.Count, id = 0, queryGraphEdgeCount = queryGraph.EdgeCount;
var queryGraphEdges = queryGraph.Edges.ToList();
var queryGraphEdges = queryGraph.Edges.ToArray();

var groupByGNodes = parentGraphMappings.GroupBy(x => x.Function.Values, MappingNodesComparer); //.ToDictionary(x => x.Key, x => x.ToArray(), MappingNodesComparer);
foreach (var set in groupByGNodes)
Expand Down Expand Up @@ -87,7 +87,7 @@ private static IList<Mapping> Algorithm3(Dictionary<QueryGraph, IList<Mapping>>
}
}
}

queryGraphEdges = null;
var threadName = System.Threading.Thread.CurrentThread.ManagedThreadId;

// Remove mappings from the parent qGraph that are found in this qGraph
Expand All @@ -99,7 +99,7 @@ private static IList<Mapping> Algorithm3(Dictionary<QueryGraph, IList<Mapping>>
{
parentGraphMappings.Add(item);
}

theRest = null;
// Now, remove duplicates
queryGraph.RemoveNonApplicableMappings(list, inputGraph);
if (!string.IsNullOrWhiteSpace(fileName) && oldCount > parentGraphMappings.Count)
Expand Down
6 changes: 3 additions & 3 deletions MODA.Impl/QueryGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,22 @@ public IList<Mapping> ReadMappingsFromFile(string filename)
/// </summary>
/// <param name="mappings"></param>
/// <returns>filename where it is written</returns>
public string WriteMappingsToFile(IList<Mapping> mappings)
public string WriteMappingsToFile(ICollection<Mapping> mappings)
{
var fileName = $"{mappings.Count}#{Identifier}.ser";
System.IO.File.WriteAllText(fileName, Extensions.CompressString(Newtonsoft.Json.JsonConvert.SerializeObject(mappings)));
return fileName;
}

public void RemoveNonApplicableMappings(IList<Mapping> mappings, UndirectedGraph<int> inputGraph)
public void RemoveNonApplicableMappings(ICollection<Mapping> mappings, UndirectedGraph<int> inputGraph)
{
if (mappings.Count == 0) return;

int subgraphSize = VertexCount;
var mapGroups = mappings.GroupBy(x => x.Function.Values, ModaAlgorithms.MappingNodesComparer); //.ToDictionary(x => x.Key, x => x.ToArray());

var toAdd = new List<Mapping>();
var queryGraphEdges = Edges.ToList();
var queryGraphEdges = Edges.ToArray();
foreach (var group in mapGroups)
{
var g_nodes = group.Key; // Remember, f(h) = g, so .Values is for g's
Expand Down
Loading

0 comments on commit bdfdc39

Please sign in to comment.