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
16 changes: 10 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
project.lock.json
DNWS260.code-workspace
Dockerfile
bin/
obj/
Migrations/
project.lock.json
DNWS260.code-workspace
Dockerfile
bin/
obj/
out/
Migrations/
*.db
client/node_modules/
client/app/bower_components/
Binary file added .vs/slnx.sqlite
Binary file not shown.
Binary file added .vs/slnx.sqlite-journal
Binary file not shown.
10 changes: 5 additions & 5 deletions ClientInfoPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@ public HTTPResponse GetResponse(HTTPRequest request)
{
StringBuilder sb = new StringBuilder();

string[] client_endpoint = request.getPropertyByKey("RemoteEndPoint").Split(':');
string[] client_endpoint = request.GetPropertyByKey("RemoteEndPoint").Split(':');
string val;
sb.Append("<html><body>");
sb.Append("Client Port: ").Append(client_endpoint[1]).Append("<br />\n");
if ((val = request.getPropertyByKey("user-agent")) != null)
if ((val = request.GetPropertyByKey("user-agent")) != null)
{
sb.Append("Browser Information: ").Append(val).Append("<br />\n");
}
if ((val = request.getPropertyByKey("accept-language")) != null)
if ((val = request.GetPropertyByKey("accept-language")) != null)
{
sb.Append("Accept Language: ").Append(val).Append("<br />\n");
}
if ((val = request.getPropertyByKey("accept-encoding")) != null)
if ((val = request.GetPropertyByKey("accept-encoding")) != null)
{
sb.Append("Accept Encoding: ").Append(val).Append("<br />\n");
}
sb.Append("</body></html>");
HTTPResponse response = new HTTPResponse(200);
response.body = Encoding.UTF8.GetBytes(sb.ToString());
response.Body = Encoding.UTF8.GetBytes(sb.ToString());
return response;
}

Expand Down
15 changes: 12 additions & 3 deletions DNWS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
</ItemGroup>
</Project>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
<PackageReference Include="newtonsoft.json" Version="11.0.1" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="OpenCL.NET" Version="2.2.9.0" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
</ItemGroup>
</Project>
4 changes: 2 additions & 2 deletions DelayPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public HTTPResponse GetResponse(HTTPRequest request)
delay = Convert.ToInt32(parts[1]);
} catch (Exception ex) {
response = new HTTPResponse(400);
response.body = Encoding.UTF8.GetBytes(ex.ToString());
response.Body = Encoding.UTF8.GetBytes(ex.ToString());
return response;
}

Expand All @@ -38,7 +38,7 @@ public HTTPResponse GetResponse(HTTPRequest request)
sb.Append("<html><body>");
sb.Append("<h1>Sleep for " + delay + " millisecond. </h1>");
sb.Append("</body></html>");
response.body = Encoding.UTF8.GetBytes(sb.ToString());
response.Body = Encoding.UTF8.GetBytes(sb.ToString());
return response;
}

Expand Down
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM microsoft/dotnet:2.0-sdk AS build-env
WORKDIR /app

COPY *.csproj ./
RUN dotnet restore
COPY . ./
RUN dotnet publish -c Release -r linux-x64 -o out
#RUN dotnet publish -c Release -o out

FROM microsoft/dotnet:2.0-runtime-deps
WORKDIR /app
COPY --from=build-env /app/out ./
#COPY /app/out ./
COPY ./config.json /app/out
COPY ./index.html /app/out
ENTRYPOINT [ "/app/DNWS" ]
200 changes: 200 additions & 0 deletions GPUPlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;

using OpenCL.Net.Extensions;
using OpenCL.Net;
using System.Linq;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace DNWS
{
class GPUPlugin : IPluginWithParameters
{
private Context _context;
private Device _device;
private Dictionary<string, string> _parameters;
private bool _isInit = false;

public GPUPlugin()
{
}

private void init()
{
ErrorCode error;

// Get platform info
Platform[] platforms = Cl.GetPlatformIDs(out error);
List<Device> devicesList = new List<Device>();

LogError (error, "Cl.GetPlaformIDs");

DeviceType deviceType = DeviceType.Default;
switch(_parameters["DeviceType"]) {
case "Gpu":
deviceType = DeviceType.Gpu;
break;
case "Cpu":
deviceType = DeviceType.Cpu;
break;
case "All":
deviceType = DeviceType.All;
break;
case "Accelerator":
deviceType = DeviceType.Accelerator;
break;
}

// Get available devices
foreach (Platform platform in platforms) {
string platformName = Cl.GetPlatformInfo(platform, PlatformInfo.Name, out error).ToString();
Console.WriteLine("Platform: " + platformName);
LogError (error, "Cl.GetPlatformInfo");
foreach (Device device in Cl.GetDeviceIDs(platform, deviceType, out error)) {
LogError(error, "Cl.GetDeviceIDs");
Console.WriteLine("Device:" + device.ToString() );
devicesList.Add(device);
}
}

if(devicesList.Count <= 0) {
Console.WriteLine("No devices found.");
return;
}

_device = devicesList[0];
if(Cl.GetDeviceInfo(_device, DeviceInfo.ImageSupport, out error).CastTo<OpenCL.Net.Bool>() == OpenCL.Net.Bool.False)
{
Console.WriteLine("No image support.");
return;
}
_context = Cl.CreateContext(null, 1, new[] {_device}, ContextNotify, IntPtr.Zero, out error);
LogError(error, "Cl.CreateContext");


}

private void ContextNotify(string errInfo, byte[] data, IntPtr cb, IntPtr userData) {
Console.WriteLine("OpenCL Notification: " + errInfo);
}

public void PreProcessing(HTTPRequest request)
{
throw new NotImplementedException();
}

private void LogError(ErrorCode err, string name)
{
if (err != ErrorCode.Success) {
Console.WriteLine("ERROR: " + name + " (" + err.ToString() + ")");
}
}

private StringBuilder GenUploadForm()
{
StringBuilder sb = new StringBuilder();
sb.Append("<form method=\"post\">");
sb.Append("Image URL:");
sb.Append("<input type=\"text\" name=\"imageUploadUrl\" id=\"imageUplaodUrl\" />");
sb.Append("<input type=\"submit\" value=\"Retrieve Image\" name=\"submit\" />");
sb.Append("</form>");
return sb;
}

public HTTPResponse GetResponse(HTTPRequest request)
{
HTTPResponse response = new HTTPResponse(200);
StringBuilder sb = new StringBuilder();
ErrorCode error;

if(!_isInit) {
init();
_isInit = true;
}

if (request.Method == HTTPRequest.METHOD_GET) {
sb.Append("<html><body>");
sb.Append(GenUploadForm());
sb.Append("</body></html>");
response.Body = Encoding.UTF8.GetBytes(sb.ToString());
return response;
} else if (request.Method == HTTPRequest.METHOD_POST) {
sb.Append(request.Body);
response.Body = Encoding.UTF8.GetBytes(sb.ToString());
string programPath = System.Environment.CurrentDirectory + "/Kernel.cl";
if(!System.IO.File.Exists(programPath)) {
Console.WriteLine("Program doesn't exist at path " + programPath);
return new HTTPResponse(404);
}

sb.Append("<html><body>");
string programSource = System.IO.File.ReadAllText(programPath);
using (OpenCL.Net.Program program = Cl.CreateProgramWithSource(_context, 1, new[] {programSource}, null, out error)) {
LogError(error, "Cl.CreateProgramWithSource");
error = Cl.BuildProgram(program, 1, new[] {_device}, string.Empty, null, IntPtr.Zero);
LogError(error, "Cl.BuildProgram");
if (Cl.GetProgramBuildInfo (program, _device, ProgramBuildInfo.Status, out error).CastTo<OpenCL.Net.BuildStatus>()
!= BuildStatus.Success) {
LogError(error, "Cl.GetProgramBuildInfo");
Console.WriteLine("Cl.GetProgramBuildInfo != Success");
Console.WriteLine(Cl.GetProgramBuildInfo(program, _device, ProgramBuildInfo.Log, out error));
return new HTTPResponse(404);
}
Kernel kernel = Cl.CreateKernel(program, "answer", out error);
LogError(error, "Cl.CreateKernel");

Random rand = new Random();
int[] input = (from i in Enumerable.Range(0, 100) select (int)rand.Next()).ToArray();
int[] output = new int[100];

var buffIn = _context.CreateBuffer(input, MemFlags.ReadOnly);
var buffOut = _context.CreateBuffer(output, MemFlags.WriteOnly);
int IntPtrSize = Marshal.SizeOf(typeof(IntPtr));
error = Cl.SetKernelArg(kernel, 0, (IntPtr)IntPtrSize, buffIn);
error |= Cl.SetKernelArg(kernel, 1, (IntPtr)IntPtrSize, buffOut);
LogError(error, "Cl.SetKernelArg");
CommandQueue cmdQueue = Cl.CreateCommandQueue(_context, _device, (CommandQueueProperties)0, out error);
LogError(error, "Cl.CreateCommandQueue");
Event clevent;
error = Cl.EnqueueNDRangeKernel(cmdQueue, kernel, 2, null, new[]{(IntPtr)100,(IntPtr)1}, null, 0, null, out clevent);
LogError(error, "Cl.EnqueueNDRangeKernel");
error = Cl.Finish(cmdQueue);
LogError(error, "Cl.Finih");
error = Cl.EnqueueReadBuffer(cmdQueue, buffOut, OpenCL.Net.Bool.True, 0, 100, output, 0, null, out clevent);
LogError(error, "Cl.EnqueueReadBuffer");
error = Cl.Finish(cmdQueue);
LogError(error, "Cl.Finih");


Cl.ReleaseKernel(kernel);
Cl.ReleaseCommandQueue(cmdQueue);
Cl.ReleaseMemObject(buffIn);
Cl.ReleaseMemObject(buffOut);
sb.Append("<pre>");
for(int i = 0; i != 100; i++) {
sb.Append(input[i] + " % 42 = " + output[i] + "<br />");
}
sb.Append("</pre>");
}
sb.Append("</body></html>");
response.Body = Encoding.UTF8.GetBytes(sb.ToString());
return response;
}
return new HTTPResponse(501);
}

public HTTPResponse PostProcessing(HTTPResponse response)
{
throw new NotImplementedException();
}

public void SetParameters(Dictionary<string, string> parameters)
{
_parameters = parameters;
}
}
}
Loading