Skip to content

Commit

Permalink
feat: add the common example python csharp
Browse files Browse the repository at this point in the history
  • Loading branch information
ereali-aneo committed Jun 5, 2024
1 parent 804186b commit 1d56c26
Show file tree
Hide file tree
Showing 9 changed files with 554 additions and 213 deletions.
31 changes: 19 additions & 12 deletions csharp/native/Demo.sln
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ArmoniK.Samples.SubTasking.
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DynamicSubmission", "DynamicSubmission", "{E828F881-9FE4-4705-9E3F-11E5F28920B4}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ArmoniK.Samples.DynamicSubmission.Client", "DynamicSubmission\Client\ArmoniK.Samples.DynamicSubmission.Client.csproj", "{BB7CE522-FD6A-4A57-8800-FC38784B3086}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ArmoniK.Samples.DynamicSubmission.Client", "DynamicSubmission\Client\ArmoniK.Samples.DynamicSubmission.Client.csproj", "{E5F3F277-9469-4D89-B704-B91EFF90823B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ArmoniK.Samples.DynamicSubmission.Worker", "DynamicSubmission\Worker\ArmoniK.Samples.DynamicSubmission.Worker.csproj", "{E4D6823B-A818-4467-B754-35080A4A604A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ArmoniK.Samples.DynamicSubmission.Worker", "DynamicSubmission\Worker\ArmoniK.Samples.DynamicSubmission.Worker.csproj", "{3CD70D2C-7FF3-4EC0-893B-F0D0405EA364}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ArmoniK.Samples.DynamicSubmission.Common", "DynamicSubmission\Common\ArmoniK.Samples.DynamicSubmission.Common.csproj", "{CC42EA27-F59B-4DD6-8F7D-8077F77B56D5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -70,14 +72,18 @@ Global
{52337672-4D34-43CC-A329-A1D4BFED75A1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{52337672-4D34-43CC-A329-A1D4BFED75A1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{52337672-4D34-43CC-A329-A1D4BFED75A1}.Release|Any CPU.Build.0 = Release|Any CPU
{BB7CE522-FD6A-4A57-8800-FC38784B3086}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BB7CE522-FD6A-4A57-8800-FC38784B3086}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BB7CE522-FD6A-4A57-8800-FC38784B3086}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BB7CE522-FD6A-4A57-8800-FC38784B3086}.Release|Any CPU.Build.0 = Release|Any CPU
{E4D6823B-A818-4467-B754-35080A4A604A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E4D6823B-A818-4467-B754-35080A4A604A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E4D6823B-A818-4467-B754-35080A4A604A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E4D6823B-A818-4467-B754-35080A4A604A}.Release|Any CPU.Build.0 = Release|Any CPU
{E5F3F277-9469-4D89-B704-B91EFF90823B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E5F3F277-9469-4D89-B704-B91EFF90823B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E5F3F277-9469-4D89-B704-B91EFF90823B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E5F3F277-9469-4D89-B704-B91EFF90823B}.Release|Any CPU.Build.0 = Release|Any CPU
{3CD70D2C-7FF3-4EC0-893B-F0D0405EA364}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3CD70D2C-7FF3-4EC0-893B-F0D0405EA364}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3CD70D2C-7FF3-4EC0-893B-F0D0405EA364}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3CD70D2C-7FF3-4EC0-893B-F0D0405EA364}.Release|Any CPU.Build.0 = Release|Any CPU
{CC42EA27-F59B-4DD6-8F7D-8077F77B56D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CC42EA27-F59B-4DD6-8F7D-8077F77B56D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CC42EA27-F59B-4DD6-8F7D-8077F77B56D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CC42EA27-F59B-4DD6-8F7D-8077F77B56D5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -91,8 +97,9 @@ Global
{0C8F34CE-B4E6-4088-BD54-CFCE4E05C3D5} = {E156D1A1-D6C0-48F6-9251-68D839061DD0}
{4239FDD7-FD9C-4A45-A6D2-5539EDEF0A3A} = {9DAFB1A5-261F-4FAF-BFBC-6D6CC40745A7}
{52337672-4D34-43CC-A329-A1D4BFED75A1} = {9DAFB1A5-261F-4FAF-BFBC-6D6CC40745A7}
{BB7CE522-FD6A-4A57-8800-FC38784B3086} = {E828F881-9FE4-4705-9E3F-11E5F28920B4}
{E4D6823B-A818-4467-B754-35080A4A604A} = {E828F881-9FE4-4705-9E3F-11E5F28920B4}
{E5F3F277-9469-4D89-B704-B91EFF90823B} = {E828F881-9FE4-4705-9E3F-11E5F28920B4}
{3CD70D2C-7FF3-4EC0-893B-F0D0405EA364} = {E828F881-9FE4-4705-9E3F-11E5F28920B4}
{CC42EA27-F59B-4DD6-8F7D-8077F77B56D5} = {E828F881-9FE4-4705-9E3F-11E5F28920B4}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {45373606-CD20-466E-9453-F54C280DDCA2}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
Expand All @@ -17,4 +16,7 @@
<PackageReference Include="System.Threading.Tasks" Version="4.3.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Common\ArmoniK.Samples.DynamicSubmission.Common.csproj" />
</ItemGroup>
</Project>
16 changes: 6 additions & 10 deletions csharp/native/DynamicSubmission/Client/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.


FROM mcr.microsoft.com/dotnet/runtime:6.0 AS base
#ARG TNAME="ArmoniK.Samples.SubTasking.Client" TPATH="SubTasking/Client/"

WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["SubTasking/Client/ArmoniK.Samples.SubTasking.Client.csproj", "SubTasking/Client/"]
RUN dotnet restore "SubTasking/Client/ArmoniK.Samples.SubTasking.Client.csproj"
COPY ["DynamicSubmission/Client/ArmoniK.Samples.DynamicSubmission.Client.csproj", "DynamicSubmission/Client/"]
RUN dotnet restore "DynamicSubmission/Client/ArmoniK.Samples.DynamicSubmission.Client.csproj"
COPY . .
WORKDIR "/src/SubTasking/Client/"
RUN dotnet build "ArmoniK.Samples.SubTasking.Client.csproj" -c Release -o /app/build
WORKDIR "/src/DynamicSubmission/Client/"
RUN dotnet build "ArmoniK.Samples.DynamicSubmission.Client.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "ArmoniK.Samples.SubTasking.Client.csproj" -c Release -o /app/publish /p:UseAppHost=false
RUN dotnet publish "ArmoniK.Samples.DynamicSubmission.Client.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ArmoniK.Samples.SubTasking.Client.dll"]
ENTRYPOINT ["dotnet", "ArmoniK.Samples.DynamicSubmission.Client.dll"]
110 changes: 47 additions & 63 deletions csharp/native/DynamicSubmission/Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.CommandLine;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;

using ArmoniK.Api.Client;
using ArmoniK.Api.Client.Options;
Expand All @@ -35,35 +40,14 @@
using ArmoniK.Api.gRPC.V1.Results;
using ArmoniK.Api.gRPC.V1.Sessions;
using ArmoniK.Api.gRPC.V1.Tasks;

using ArmoniK.Samples.DynamicSubmission.Common;
using Google.Protobuf;
using Google.Protobuf.WellKnownTypes;



namespace ArmoniK.Samples.DynamicSubmission.Client
{
public record Table
{
public uint Size { get; init; }
public uint Threshold { get; init; }
public uint[] Values { get; set; }

public Table(uint size, uint threshold)
{
Size = size;
Threshold = threshold;
Values = new uint[size];
for (uint i = 0; i < size; i++)
{
Values[i] = (i + 1);
}
}
}

internal static class Program
{

/// <summary>
/// Method for sending task and retrieving their results from ArmoniK
/// </summary>
Expand All @@ -77,15 +61,18 @@ internal static class Program
/// <exception cref="Exception">Issues with results from tasks</exception>
/// <exception cref="ArgumentOutOfRangeException">Unknown response type from control plane</exception>
internal static async Task Run(string endpoint,
string partition, uint n, uint threshold)
string partition,
uint n,
uint threshold)
{
// Create gRPC channel to connect with ArmoniK control plane
Console.WriteLine();
var input = new Table(n, threshold);
var input = new Table(n,
threshold);
var channel = GrpcChannelFactory.CreateChannel(new GrpcClient
{
Endpoint = endpoint,
});
{
Endpoint = endpoint,
});

// Create client for task submission
var taskClient = new Tasks.TasksClient(channel);
Expand All @@ -101,61 +88,61 @@ internal static async Task Run(string endpoint,

// Default task options that will be used by each task if not overwritten when submitting tasks
var taskOptions = new TaskOptions
{
MaxDuration = Duration.FromTimeSpan(TimeSpan.FromHours(1)),
MaxRetries = 2,
Priority = 1,
PartitionId = partition,
};
{
MaxDuration = Duration.FromTimeSpan(TimeSpan.FromHours(1)),
MaxRetries = 2,
Priority = 1,
PartitionId = partition,
};

// Request for session creation with default task options and allowed partitions for the session
var createSessionReply = sessionClient.CreateSession(new CreateSessionRequest
{
DefaultTaskOption = taskOptions,
PartitionIds =
{
DefaultTaskOption = taskOptions,
PartitionIds =
{
partition,
},
});
});

Console.WriteLine($"sessionId: {createSessionReply.SessionId}");

// Create the result metadata and keep the id for task submission
var resultId = resultClient.CreateResultsMetaData(new CreateResultsMetaDataRequest
{
SessionId = createSessionReply.SessionId,
Results =
{
SessionId = createSessionReply.SessionId,
Results =
{
new CreateResultsMetaDataRequest.Types.ResultCreate
{
Name = "Result",
},
},
})
})
.Results.Single()
.ResultId;

// Create the payload metadata (a result) and upload data at the same time
var payloadId = resultClient.CreateResults(new CreateResultsRequest
{
SessionId = createSessionReply.SessionId,
Results =
{
SessionId = createSessionReply.SessionId,
Results =
{
new CreateResultsRequest.Types.ResultCreate
{
Data = UnsafeByteOperations.UnsafeWrap(Encoding.ASCII.GetBytes(JsonSerializer.Serialize(input))),
Name = "Payload",
},
},
})
})
.Results.Single()
.ResultId;

// Submit task with payload and result ids
var submitTasksResponse = taskClient.SubmitTasks(new SubmitTasksRequest
{
SessionId = createSessionReply.SessionId,
TaskCreations =
{
SessionId = createSessionReply.SessionId,
TaskCreations =
{
new SubmitTasksRequest.Types.TaskCreation
{
Expand All @@ -166,7 +153,7 @@ internal static async Task Run(string endpoint,
},
},
},
});
});

Console.WriteLine($"Task id {submitTasksResponse.TaskInfos.Single().TaskId}");

Expand All @@ -182,7 +169,7 @@ await eventClient.WaitForResultsAsync(createSessionReply.SessionId,
var result = await resultClient.DownloadResultData(createSessionReply.SessionId,
resultId,
CancellationToken.None);
// Test if the result is equal to input % 2
// Test if the result is equal to the sum of 0 to n
var res = uint.Parse(Encoding.ASCII.GetString(result));
var expectedResult = n * (n + 1) / 2;

Expand All @@ -205,16 +192,16 @@ public static async Task<int> Main(string[] args)
description: "Name of the partition to which submit tasks.",
getDefaultValue: () => "default");
var n = new Option<uint>("--n",
description: "unsigned int n to calculate the sum of each number from 0 to n.",
getDefaultValue: () => 0);
description: "unsigned int n to calculate the sum of each number from 0 to n.",
getDefaultValue: () => 0);
var threshold = new Option<uint>("--threshold",
description: "threshold to subdivide the addition in table of threshold size.",
getDefaultValue: () => 0);
description: "threshold to subdivide the addition in table of threshold size.",
getDefaultValue: () => 0);

// Describe the application and its purpose
var rootCommand =
new
RootCommand($"Calculate the sum of a table with subtask and agregation example for ArmoniK.\nIt sends a task to ArmoniK with a table conataining 0, 1, 2..., n subdivided it in threshold sized table and create a new task , agregate their result and addition them to get the final result");
new
RootCommand($"Calculate the sum of a table with subtask and aggregation example for ArmoniK.\nIt sends a task to ArmoniK with a table containing 0, 1, 2..., n subdivided it in threshold sized table and create a new task , aggregate their result and addition them to get the final result");
// Add the options to the parser
rootCommand.AddOption(endpoint);
rootCommand.AddOption(partition);
Expand All @@ -223,15 +210,12 @@ public static async Task<int> Main(string[] args)

// Configure the handler to call the function that will do the work
rootCommand.SetHandler(Run,
endpoint,
partition,
n,
threshold);
endpoint,
partition,
n,
threshold);
// Parse the command line parameters and call the function that represents the application
return await rootCommand.InvokeAsync(args);
}
}
}


// difficulter class serialize deserialize+ check nbr of dependancy and put as a dependancy + send result to first task
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ArmoniK.Api.Client" Version="3.17.1" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="System.Linq" Version="4.3.0" />
<PackageReference Include="System.Threading" Version="4.3.0" />
<PackageReference Include="System.Threading.Tasks" Version="4.3.0" />
</ItemGroup>

</Project>
Loading

0 comments on commit 1d56c26

Please sign in to comment.