I have two questions, the first is:
I am having the most strange behaviour when implementing an inner System.Threading.Tasks.Parallel.For loop, I have a nested loop and I am parallelizing the inner loop.
Single threaded approach:
for( int i = 0; i < numberOfFeatures; ++i )
{
path = new GH_Path(i);
feature = layer.GetFeature(i);
bool pattern = false;
if( feature != null )
{
geo = feature.GetGeometryRef();
if( geo != null )
{
ring = geo.GetGeometryRef( 0 );
int pointCount = ring.GetPointCount();
Point3d[] tempPointArray = new Point3d[pointCount];
for (int j = 0; j < pointCount; ++j)
{
ring.GetPoint( j, pointList );
tempPointArray[j] = new Point3d( pointList[0], pointList[1], pointList[2] );
}
if( tempPointArray.Length > 1 )
{
pattern = true;
Polyline polyOut = new Polyline( tempPointArray );
IGH_GeometricGoo geoGoo = GH_Convert.ToGeometricGoo( polyOut );
geoOutput.Append( geoGoo, path );
}
}
}
IGH_Goo gooPattern = GH_Convert.ToGoo( pattern );
cullPattern.Append( gooPattern, path );
}
Multithreaded:
for( int i = 0; i < numberOfFeatures; ++i )
{
path = new GH_Path(i);
feature = layer.GetFeature(i);
bool pattern = false;
if( feature != null )
{
geo = feature.GetGeometryRef();
if( geo != null )
{
ring = geo.GetGeometryRef( 0 );
int pointCount = ring.GetPointCount();
double[][] points = new double[pointCount][];
Point3d[] tempPointArray = new Point3d[pointCount];
System.Threading.Tasks.Parallel.For( 0, pointCount, j =>
{
points[j] = new double[3];
ring.GetPoint( j, points[j]);
tempPointArray[j] = new Point3d( points[j][0], points[j][1], points[j][2] );
});
if( tempPointArray.Length > 1 )
{
pattern = true;
Polyline polyOut = new Polyline( tempPointArray );
IGH_GeometricGoo geoGoo = GH_Convert.ToGeometricGoo( polyOut );
geoOutput.Append( geoGoo, path );
}
}
}
IGH_Goo gooPattern = GH_Convert.ToGoo( pattern );
cullPattern.Append( gooPattern, path );
}
*cullPattern is a GH_Structure<IGH_Goo> and geoOutput a GH_Structure<IGH_GeometricGoo>
The purpose of the code is to extract GIS vector data through the C# GDAL’s bindings. The question is why does the multithreaded approach take longer to run than the single-threaded one (even for large enough files) when running as a GH plugin. The second question is, for debugging purposes I have a Console App that does basically the same, I have profiled both approaches with the console app and the multithreaded one is much faster, but I can’t really profile the same code as I can’t get RhinoCommon or the GH kernel to run on the Console App, the app can’t find the references, is it intended behaviour for I don’t know intelectual property stuff? To profile I would need to reference RhinoCommon.dll, Grasshopper.dll and GH_IO.dll I think.
Thanks Felipe