Commit fb9a6f07 authored by BORNON Théophile's avatar BORNON Théophile

Bench scripts and minor changes

parent a79faff5
...@@ -9,7 +9,8 @@ namespace S04_Projet ...@@ -9,7 +9,8 @@ namespace S04_Projet
public enum Operation public enum Operation
{ {
Load, Load,
Filter Filter,
Save
} }
private enum RGB private enum RGB
...@@ -60,7 +61,7 @@ namespace S04_Projet ...@@ -60,7 +61,7 @@ namespace S04_Projet
this.bPixelPtr = bPixelPtr; this.bPixelPtr = bPixelPtr;
nbPixel = opt.width * opt.height; nbPixel = opt.width * opt.height;
size = filter.GetLength(0); size = filter.GetLength(0);
nbProcessors = 4; nbProcessors = Math.Min(Environment.ProcessorCount, 8);
} }
else throw new Exception("Impossible d'utiliser ce constructeur pour ce type d'opération"); else throw new Exception("Impossible d'utiliser ce constructeur pour ce type d'opération");
} }
...@@ -76,7 +77,7 @@ namespace S04_Projet ...@@ -76,7 +77,7 @@ namespace S04_Projet
/// <param name="filePtr"></param> /// <param name="filePtr"></param>
public unsafe MultiThreadedTask(Operation ToDo, Options opt, byte* rPixelPtr, byte* gPixelPtr, byte* bPixelPtr, byte* filePtr) public unsafe MultiThreadedTask(Operation ToDo, Options opt, byte* rPixelPtr, byte* gPixelPtr, byte* bPixelPtr, byte* filePtr)
{ {
if (ToDo == Operation.Load) if (ToDo == Operation.Load || ToDo == Operation.Save)
{ {
this.rPixelPtr = rPixelPtr; this.rPixelPtr = rPixelPtr;
this.gPixelPtr = gPixelPtr; this.gPixelPtr = gPixelPtr;
...@@ -85,20 +86,18 @@ namespace S04_Projet ...@@ -85,20 +86,18 @@ namespace S04_Projet
this.opt = opt; this.opt = opt;
this.filePtr = filePtr; this.filePtr = filePtr;
nbPixel = opt.width * opt.height; nbPixel = opt.width * opt.height;
nbProcessors = Environment.ProcessorCount; nbProcessors = Math.Min(Environment.ProcessorCount, 8);
} }
else throw new Exception("Impossible d'utiliser ce constructeur pour ce type d'opération"); else throw new Exception("Impossible d'utiliser ce constructeur pour ce type d'opération");
} }
#region LoadImage #region Load
public unsafe void LoadImage() public unsafe void LoadImage()
{ {
Console.WriteLine("Load Image");
Thread[] threads = new Thread[nbProcessors]; Thread[] threads = new Thread[nbProcessors];
for (int i = 0; i < nbProcessors; i++) for (int i = 0; i < nbProcessors; i++)
{ {
Console.WriteLine("Creating and starting thread {0}", i);
threads[i] = new Thread(new ParameterizedThreadStart(LoadImageTask)); threads[i] = new Thread(new ParameterizedThreadStart(LoadImageTask));
threads[i].Start(i); threads[i].Start(i);
} }
...@@ -109,22 +108,19 @@ namespace S04_Projet ...@@ -109,22 +108,19 @@ namespace S04_Projet
private unsafe void LoadImageTask(object i) private unsafe void LoadImageTask(object i)
{ {
if (filePtr != null) int start = nbPixel / nbProcessors * (int)i;
{ int end = nbPixel / nbProcessors * ((int)i + 1);
int start = nbPixel / nbProcessors * (int)i;
int end = nbPixel / nbProcessors * ((int)i + 1);
int x, y; int x, y;
for (int j = start; j < end; j++) for (int j = start; j < end; j++)
{ {
x = j % opt.width; x = j % opt.width;
y = j / opt.width; y = j / opt.width;
*(bPixelPtr + j) = *(filePtr + opt.offset + opt.padding * y + j * 3); *(bPixelPtr + j) = *(filePtr + opt.offset + opt.padding * y + j * 3);
*(gPixelPtr + j) = *(filePtr + opt.offset + opt.padding * y + j * 3 + 1); *(gPixelPtr + j) = *(filePtr + opt.offset + opt.padding * y + j * 3 + 1);
*(rPixelPtr + j) = *(filePtr + opt.offset + opt.padding * y + j * 3 + 2); *(rPixelPtr + j) = *(filePtr + opt.offset + opt.padding * y + j * 3 + 2);
}
} }
} }
#endregion #endregion
...@@ -253,5 +249,39 @@ namespace S04_Projet ...@@ -253,5 +249,39 @@ namespace S04_Projet
else return (byte)r; else return (byte)r;
} }
#endregion #endregion
#region Save
public unsafe void SaveImage()
{
Thread[] threads = new Thread[nbProcessors];
for (int i = 0; i < nbProcessors; i++)
{
threads[i] = new Thread(new ParameterizedThreadStart(SaveImageTask));
threads[i].Start(i);
}
for (int i = 0; i < nbProcessors; i++)
threads[i].Join();
}
private unsafe void SaveImageTask(object i)
{
int start = nbPixel / nbProcessors * (int)i;
int end = nbPixel / nbProcessors * ((int)i + 1);
int x, y;
for (int j = start; j < end; j++)
{
x = j % opt.width;
y = j / opt.width;
*(filePtr + opt.offset + opt.padding * y + j * 3) = *(bPixelPtr + j);
*(filePtr + opt.offset + opt.padding * y + j * 3 + 1) = *(gPixelPtr + j);
*(filePtr + opt.offset + opt.padding * y + j * 3 + 2) = *(rPixelPtr + j);
}
}
#endregion
} }
} }
...@@ -112,7 +112,7 @@ namespace S04_Projet ...@@ -112,7 +112,7 @@ namespace S04_Projet
#region Mono #region Mono
if (!Program.MULTITHREADING_LOAD) if (!Program.MULTITHREADING)
{ {
int y; int y;
fixed (byte* filePtr = file, rPtr = rPixels, gPtr = gPixels, bPtr = bPixels) fixed (byte* filePtr = file, rPtr = rPixels, gPtr = gPixels, bPtr = bPixels)
...@@ -181,18 +181,30 @@ namespace S04_Projet ...@@ -181,18 +181,30 @@ namespace S04_Projet
#endregion #endregion
#region Pixel array #region Pixel array
int x; if (!Program.MULTITHREADING)
int y;
fixed (byte* filePtr = file)
{ {
for (int i = 0; i < nbPixel; i++) int x;
int y;
fixed (byte* filePtr = file)
{ {
x = i % opt.width; for (int i = 0; i < nbPixel; i++)
y = i / opt.width; {
x = i % opt.width;
y = i / opt.width;
*(filePtr + i * 3 + opt.padding * y + opt.offset) = bPixels[i];
*(filePtr + i * 3 + opt.padding * y + opt.offset + 1) = gPixels[i];
*(filePtr + i * 3 + opt.padding * y + opt.offset + 2) = rPixels[i];
}
}
}
*(filePtr + i * 3 + opt.padding * y + opt.offset) = bPixels[i]; else
*(filePtr + i * 3 + opt.padding * y + opt.offset + 1) = gPixels[i]; {
*(filePtr + i * 3 + opt.padding * y + opt.offset + 2) = rPixels[i]; fixed (byte* filePtr = file, rPtr = rPixels, gPtr = gPixels, bPtr = bPixels)
{
MultiThreadedTask SaveImageTask = new MultiThreadedTask(MultiThreadedTask.Operation.Save, opt, rPtr, gPtr, bPtr, filePtr);
SaveImageTask.SaveImage();
} }
} }
#endregion #endregion
......
...@@ -6,7 +6,7 @@ namespace S04_Projet ...@@ -6,7 +6,7 @@ namespace S04_Projet
{ {
class Program class Program
{ {
public static bool MULTITHREADING_LOAD = false; //public static bool MULTITHREADING_LOAD = false;
public static bool MULTITHREADING = false; public static bool MULTITHREADING = false;
public static bool DEBUG = false; public static bool DEBUG = false;
...@@ -83,8 +83,8 @@ namespace S04_Projet ...@@ -83,8 +83,8 @@ namespace S04_Projet
DEBUG = true; DEBUG = true;
else if (args[i] == "--multithreading" || args[i] == "-MT") else if (args[i] == "--multithreading" || args[i] == "-MT")
MULTITHREADING = true; MULTITHREADING = true;
else if (args[i] == "--multithreadingLoad" || args[i] == "-MTL") //else if (args[i] == "--multithreadingLoad" || args[i] == "-MTL")
MULTITHREADING_LOAD = true; // MULTITHREADING_LOAD = true;
else if (args[i] == "--") ; else if (args[i] == "--") ;
if (DEBUG) if (DEBUG)
......
@echo off @echo off
FOR /L %%i IN (1,1,100) DO ( FOR /L %%i IN (1,1,100) DO (
ECHO %%i ECHO %%i
ImageProcessing.exe -d -MTL -i img\flocon.bmp ImageProcessing.exe -d -MT -i img\flocon.bmp
) )
\ No newline at end of file
@echo off
FOR /L %%i IN (1,1,100) DO (
ECHO %%i
ImageProcessing.exe -d -MT -i img\flocon.bmp -o output.bmp
)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment