I haven't tried it, but one of my friends sent me this sample of a 2D Convolver.

(Note; Some glyph substitutions have appeared in the code below - the lightbulbs are for

. [ i ]

and an EntryPoint attribute is missing at the top. What's happened to the "F# code" options?)

open System

open Microsoft.ParallelArrays

[<EntryPoint>]

let main(args) =

let inputSize = 4000

// Create Random generator

let random = Random(42)

// Declare an input data array

let inputData = Array2D.init inputSize inputSize (fun i j -> float32 (random.NextDouble() * float (random.Next(1, 100))))

// Declare filter kernel for the convolution

let (kernel : float32 []) = Array.map float32 [| 2; 5; 7; 4; 3 |] ;

let kernelSize = kernel.Length

// Create an Accelerator float parallel array for the F# input array

use inputArray = new FloatParallelArray(inputData)

// Build the expression to convolve in the X or Y direction

let rec convolve (shifts : int -> int []) i (a : FloatParallelArray)

= let e = kernel.[i] * ParallelArrays.Shift(a, shifts i)

if i = 0 then

e

else

e + convolve shifts (i-1) a

let convolveX = convolve (fun i -> [| -i; 0 |]) (kernelSize-1) inputArray

let convolveXY = convolve (fun i -> [| 0; -i |]) (kernelSize-1) convolveX

// Create DX9 target and compute result

use dx9Target = new DX9Target()

let resDXY = dx9Target.ToArray2D(convolveXY)

printfn "DX9 --> \r\n%A" resDX

using System;

using Microsoft.ParallelArrays;

using A = Microsoft.ParallelArrays.ParallelArrays;

namespace AcceleratorSamples

{

partial class Convolver

{

public static float[,] Convolver2D(Target ComputeTarget, float[] a, float[,] x)

{

FloatParallelArray xpar = new FloatParallelArray(x);

int n = x.GetLength(0);

int m = x.GetLength(1);

FloatParallelArray zpar = new FloatParallelArray(0.0f, new int[2] { n, m });

int[] shiftBy = new int[2] { 0, 0 };

for (int i = 0; i < a.Length; i++)

{

shiftBy[1] = -i;

zpar += a[i] * A.Shift(xpar, shiftBy);

}

shiftBy[1] = 0;

FloatParallelArray ypar = new FloatParallelArray(0.0f, new int[2] { n, m });

for (int i = 0; i < a.Length; i++)

{

shiftBy[0] = -i;

ypar += a[i] * A.Shift(zpar, shiftBy);

}

float[,] result = ComputeTarget.ToArray2D(ypar as FloatParallelArray);

return result;

}

}

}

namespace AcceleratorSamples

{

partial class Convolver

{

static void Main(string[] args)

{

float[,] x = new float[6, 8] {{ 7, 2, 5, 9, 3, 8, 6, 4 },

{ 2, 8, 7, 4, 8, 9, 3, 5 },

{ 9, 4, 6, 1, 8, 9, 1, 2 },

{ 8, 2, 7, 3, 9, 2, 5, 8 },

{ 4, 7, 3, 8, 3, 9, 5, 3 },

{ 6, 7, 1, 6, 3, 7, 3, 9}};

float[] a = new float[5] { 2, 5, 7, 4, 3 };

Target target = new Microsoft.ParallelArrays.DX9Target();

float[,] y = Convolver2D(target, a, x);

for (int row = 0; row < y.GetLength(0); row++)

{

for (int col = 0; col < y.GetLength(1); col++)

{

Console.Write(y[row, col] + " ");

}

Console.WriteLine();

}

}

}

}

By on 12/11/2009 4:52 PM ()

Thats perfect, thanks. Is it your blog? [link:blogs.msdn.com]

By on 12/15/2009 9:04 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper