Added DeviceType switch, removed copy script from run configs (rider bug prevents execution), added CUDA and OpenCL run configs.

This commit is contained in:
Samuele Lorefice
2025-04-08 18:53:52 +02:00
parent 8992a6c33d
commit 964eb02adb
7 changed files with 79 additions and 34 deletions

View File

@@ -1,7 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Run SDF Tool [8 pic]" type="DotNetProject" factoryName=".NET Project" focusToolWindowBeforeRun="true">
<configuration default="false" name="Run SDF Tool [8 pic CUDA]" type="DotNetProject" factoryName=".NET Project" focusToolWindowBeforeRun="true">
<option name="EXE_PATH" value="$PROJECT_DIR$/SDFMapCreator/bin/Debug/net8.0/SDFMapTool.exe" />
<option name="PROGRAM_PARAMETERS" value="-D ./images -o &quot;./sdf.png&quot; -i &quot;01.png;02.png;03.png;04.png;05.png;06.png;07.png;08.png&quot;" />
<option name="PROGRAM_PARAMETERS" value="-D ./images -o &quot;./sdf.png&quot; -i &quot;01.png;02.png;03.png;04.png;05.png;06.png;07.png;08.png&quot; --device CUDA" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/SDFMapCreator/bin/Debug/net8.0" />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
@@ -15,8 +15,6 @@
<option name="PROJECT_TFM" value="net8.0" />
<method v="2">
<option name="Build" default="false" projectName="SDFMapCreator" projectPath="$PROJECT_DIR$/SDFMapCreator/SDFMapCreator.csproj" />
<option name="RunConfigurationTask" enabled="false" run_configuration_name="Copy Images To TestDirs" run_configuration_type="ShConfigurationType" />
<option name="RunConfigurationTask" enabled="true" run_configuration_name="Copy Images Psh" run_configuration_type="PowerShellRunType" />
</method>
</configuration>
</component>

View File

@@ -0,0 +1,20 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Run SDF Tool [8 pic OpenCL]" type="DotNetProject" factoryName=".NET Project" focusToolWindowBeforeRun="true">
<option name="EXE_PATH" value="$PROJECT_DIR$/SDFMapCreator/bin/Debug/net8.0/SDFMapTool.exe" />
<option name="PROGRAM_PARAMETERS" value="-D ./images -o &quot;./sdf.png&quot; -i &quot;01.png;02.png;03.png;04.png;05.png;06.png;07.png;08.png&quot; --device OpenCL" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/SDFMapCreator/bin/Debug/net8.0" />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
<option name="USE_MONO" value="0" />
<option name="RUNTIME_ARGUMENTS" value="" />
<option name="PROJECT_PATH" value="$PROJECT_DIR$/SDFMapCreator/SDFMapCreator.csproj" />
<option name="PROJECT_EXE_PATH_TRACKING" value="1" />
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="1" />
<option name="PROJECT_KIND" value="DotNetCore" />
<option name="PROJECT_TFM" value="net8.0" />
<method v="2">
<option name="Build" default="false" projectName="SDFMapCreator" projectPath="$PROJECT_DIR$/SDFMapCreator/SDFMapCreator.csproj" />
</method>
</configuration>
</component>

View File

@@ -15,8 +15,6 @@
<option name="PROJECT_TFM" value="net8.0" />
<method v="2">
<option name="Build" default="false" projectName="SDFMapCreator" projectPath="$PROJECT_DIR$/SDFMapCreator/SDFMapCreator.csproj" />
<option name="RunConfigurationTask" enabled="true" run_configuration_name="Copy Images To TestDirs" run_configuration_type="ShConfigurationType" />
<option name="RunConfigurationTask" enabled="true" run_configuration_name="Copy Images Psh" run_configuration_type="PowerShellRunType" />
</method>
</configuration>
</component>

View File

@@ -8,7 +8,8 @@
- Automatic CUDA/OpenCL GPU Acceleration for most of the process
- *nix style command line interface
## How to use:
## How to use
From command line: `SDFMapTool -D <directory_with_images> -i "<image1.png;image2.png;...>" -o <output_file_path.png> ...`
Available options:
@@ -31,8 +32,10 @@ Available options:
--sigma Blur sigma value (weighting).
--device Device to use for computation. (CPU, OpenCL, CUDA)
--help Display this help screen.
--version Display version information.
```
```

View File

@@ -5,6 +5,12 @@ using CommandLine;
namespace SDFMapCreator;
public enum DeviceType {
CPU,
OpenCL,
CUDA
}
public class Options {
[Option('d', "debug", Required = false, HelpText = "Enable debug mode.")]
public bool Debug { get; set; } = false;
@@ -22,6 +28,8 @@ public class Options {
public float BlurStep { get; set; } = 0.5f;
[Option("sigma", Required = false, HelpText = "Blur sigma value (weighting).")]
public float BlurSigma { get; set; } = 1f;
[Option("device", Required = false, HelpText = "Device to use for computation. (CPU, OpenCL, CUDA)")]
public DeviceType? Device { get; set; } = null;
}
public static class Program {
@@ -40,7 +48,7 @@ public static class Program {
static List<TransitionMaskData> TransitionMasks = new();
static List<SDFData> SDFs = new();
static List<Vector3[,]> Gradients = new();
static readonly SdfKernels kernels = new();
static SdfKernels kernels;
static uint width;
static uint height;
@@ -51,9 +59,11 @@ public static class Program {
parser.ParseArguments<Options>(args)
.WithParsed(o => options = o)
.WithNotParsed(o => {
.WithNotParsed(_ => {
Environment.Exit(1);
});
if(options.Device != null) kernels = new (options.Device.Value);
else kernels = new();
string debugPath = $"{Environment.CurrentDirectory}{PSep}Debug";
if (options.Debug) {

View File

@@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Numerics;
using ILGPU;
using ILGPU.Runtime;
@@ -9,12 +10,12 @@ namespace SDFMapCreator;
public partial class SdfKernels {
Context gpuContext;
Accelerator accelerator;
public SdfKernels() {
// Initialize the GPU context
gpuContext = Context.Create(builder => builder
.OpenCL()
.Cuda()
.OpenCL()
.CPU()
.Math(MathMode.Fast32BitOnly)
.EnableAlgorithms()
@@ -22,35 +23,52 @@ public partial class SdfKernels {
Console.WriteLine("Reading available accelerators (CUDA only)...");
foreach (var device in gpuContext.GetCudaDevices()) {
using var accelerator = device.CreateAccelerator(gpuContext);
accelerator = device.CreateAccelerator(gpuContext);
Console.WriteLine($"Found accelerator: {accelerator.Name} ({accelerator.AcceleratorType})");
}
Console.WriteLine("Reading available accelerators (OpenCL only)...");
foreach (var device in gpuContext.GetCLDevices()) {
using var accelerator = device.CreateAccelerator(gpuContext);
accelerator = device.CreateAccelerator(gpuContext);
Console.WriteLine($"Found accelerator: {accelerator.Name} ({accelerator.AcceleratorType})");
}
Console.WriteLine("Reading available accelerators (CPU only)...");
foreach (var device in gpuContext.GetCPUDevices()) {
using var accelerator = device.CreateAccelerator(gpuContext);
accelerator = device.CreateAccelerator(gpuContext);
Console.WriteLine($"Found accelerator: {accelerator.Name} ({accelerator.AcceleratorType})");
}
accelerator = gpuContext.GetPreferredDevice(false).CreateAccelerator(gpuContext);
}
public SdfKernels(DeviceType device) {
Console.WriteLine($"Looking up for {device.ToString()} accelerators...");
var builder = Context.Create();
switch (device) {
case DeviceType.CPU: builder.CPU(); break;
case DeviceType.CUDA: builder.Cuda(); break;
case DeviceType.OpenCL: builder.OpenCL(); break;
}
gpuContext = builder
.Math(MathMode.Fast32BitOnly)
.EnableAlgorithms()
.ToContext();
accelerator = gpuContext.GetPreferredDevice(false).CreateAccelerator(gpuContext);
}
public void SelfMask(Vector3[,] input, out Vector3[,] mask) {
var dev = gpuContext.GetPreferredDevice(false);
var width = input.GetLength(0);
var height = input.GetLength(1);
mask = new Vector3[width, height];
using var accelerator = dev.CreateAccelerator(gpuContext);
using var buffer = accelerator.Allocate2DDenseX<Vector3>(new(width, height));
buffer.CopyFromCPU(input);
using var maskBuffer = accelerator.Allocate2DDenseX<Vector3>(new(width, height));
var selfMaskKernel = accelerator.LoadAutoGroupedStreamKernel<Index2D, ArrayView2D<Vector3, Stride2D.DenseX>,
ArrayView2D<Vector3, Stride2D.DenseX>>(SelfMaskKernel);
@@ -62,11 +80,9 @@ public partial class SdfKernels {
}
public void ABMask(Vector3[,] A, Vector3[,] B, out Vector3[,] mask) {
var dev = gpuContext.GetPreferredDevice(false);
var width = A.GetLength(0);
var height = A.GetLength(1);
mask = new Vector3[width, height];
using var accelerator = dev.CreateAccelerator(gpuContext);
using var bufferA = accelerator.Allocate2DDenseX<Vector3>(new(width, height));
bufferA.CopyFromCPU(A);
@@ -89,11 +105,9 @@ public partial class SdfKernels {
}
public void EdgeDetect(Vector3[,] mask, out Vector3[,] edge) {
var dev = gpuContext.GetPreferredDevice(false);
var width = mask.GetLength(0);
var height = mask.GetLength(1);
edge = new Vector3[width, height];
using var accelerator = dev.CreateAccelerator(gpuContext);
using var buffer = accelerator.Allocate2DDenseX<Vector3>(new(width, height));
buffer.CopyFromCPU(mask);
@@ -109,9 +123,6 @@ public partial class SdfKernels {
}
public void Sdf(Vector2[] edges, int width, int height, out Vector3[,] sdf) {
var dev = gpuContext.GetPreferredDevice(false);
using var accelerator = dev.CreateAccelerator(gpuContext);
using var buffer = accelerator.Allocate2DDenseX<Vector3>(new(width, height));
using var edgeBuffer = accelerator.Allocate1D<Vector2>(edges.Length);
@@ -130,11 +141,9 @@ public partial class SdfKernels {
}
public void Gradient(Vector3[,] mask, Vector3[,] sdfa, Vector3[,] sdfb, out Vector3[,] gradient) {
var dev = gpuContext.GetPreferredDevice(false);
var width = mask.GetLength(0);
var height = mask.GetLength(1);
gradient = new Vector3[width, height];
using var accelerator = dev.CreateAccelerator(gpuContext);
using var bufferMask = accelerator.Allocate2DDenseX<Vector3>(new(width, height));
bufferMask.CopyFromCPU(mask);
@@ -162,11 +171,9 @@ public partial class SdfKernels {
public void DirectionalBlur(Vector3[,] image, Vector3[,] mask, out Vector3[,] output, float radius = 3f,
float step = .5f, float sigma = 1f) {
var dev = gpuContext.GetPreferredDevice(false);
var width = image.GetLength(0);
var height = image.GetLength(1);
output = new Vector3[width, height];
using var accelerator = dev.CreateAccelerator(gpuContext);
using var imageBuffer = accelerator.Allocate2DDenseX<Vector3>(new(width, height));
imageBuffer.CopyFromCPU(image);
@@ -195,4 +202,9 @@ public partial class SdfKernels {
a.PrintInformation(infoString);
return infoString.ToString();
}
~SdfKernels() {
accelerator?.Dispose();
gpuContext?.Dispose();
}
}

View File

@@ -1,11 +1,15 @@
$imagesPath = "./images/*"
if (Test-Path "./bin/Debug/net8.0/")
{
echo "Copying images to Debug folder"
copy ./images/* "./bin/Debug/net8.0/"
Write-Output "Copying images to Debug folder"
Copy-Item -Path $imagesPath -Destination "./bin/Debug/net8.0/images/"
}
if(Test-Path "./bin/Release/net8.0/")
{
echo "Copying images to Release folder"
copy ./images/* "./bin/Release/net8.0/"
}
Write-Output "Copying images to Release folder"
Copy-Item -Path $imagesPath -Destination "./bin/Release/net8.0/images/"
}
return 0