/// <summary>Returns the matrix product M1*M2</summary> /// <param name="M1">First matrix</param> /// <param name="M2">Second matrix</param> public float[,] MultiplyLocals(float[,] M1, float[,] M2) { //M pxq, N qxr int p = M1.GetLength(0); int q = M1.GetLength(1); int r = M2.GetLength(1); if (q != M2.GetLength(0)) throw new Exception("Matrix dimensions do not match for multiplication"); float[] vecM1 = MatrixToVector(M1, ref p, ref q); float[] vecM2 = MatrixToVector(M2, ref q, ref r); float[] vecResp = new float[p * r]; CLCalc.Program.Variable varResp = new CLCalc.Program.Variable(vecResp); CLCalc.Program.Variable varM1 = new CLCalc.Program.Variable(vecM1); CLCalc.Program.Variable varM2 = new CLCalc.Program.Variable(vecM2); //Finaliza a soma dos elementos int[] vecQ = new int[1] { q }; CLCalc.Program.Variable varQ = new CLCalc.Program.Variable(vecQ); CLCalc.Program.Variable[] args = new CLCalc.Program.Variable[4] { varResp, varM1, varM2, varQ }; int[] max = new int[2] { p, r }; floatMatrixMultLocals.Execute(args, max, new int[] { 8, 8 }); varResp.ReadFromDeviceTo(vecResp); varResp.Dispose(); return VectorToMatrix(vecResp, ref p, ref r); }
static void Main() { //Initializes OpenCL Platforms and Devices and sets everything up CLCalc.InitCL(); //Create vectors with 2000 numbers float[] v1 = new float[n], v2 = new float[n]; var vResult = new float[n]; //Creates population for v1 and v2 for (int i = 0; i < n; i++) { v1[i] = (float)i / 10; v2[i] = -(float)i / 9; } //var prog = new ComputeProgram(CLCalc.Program.Context, ""); //Compiles the source codes. The source is a string array because the user may want //to split the source into many strings. CLCalc.Program.Compile(new string[] { vecSum }); //Gets host access to the OpenCL floatVectorSum kernel CLCalc.Program.Kernel VectorSum = new CLCalc.Program.Kernel("floatVectorSum"); //Creates vectors v1 and v2 in the device memory CLCalc.Program.Variable varV1 = new CLCalc.Program.Variable(v1); CLCalc.Program.Variable varV2 = new CLCalc.Program.Variable(v2); //Arguments of VectorSum kernel CLCalc.Program.Variable[] args = new CLCalc.Program.Variable[] { varV1, varV2 }; //How many workers will there be? We need "n", one for each element int[] workers = new int[1] { n }; sw.Start(); //Execute the kernel for (int i = 0; i < count; i++) DoOCL(VectorSum, args, workers); sw.Stop(); //Read device memory varV1 to host memory vResult varV1.ReadFromDeviceTo(vResult); Console.WriteLine("OpenCL: {0}", sw.ElapsedTicks); sw.Restart(); for (int i = 0; i < count; i++) DoCPU(v1, v2, vResult); sw.Stop(); Console.WriteLine("CPU: {0}", sw.ElapsedTicks); PressAny(); }
/// <summary>Computes the Discrete Fourier Transform of a double2 vector x whose length is a power of 16. /// x = { Re[x0] Im[x0] Re[x1] Im[x1] ... Re[xn] Im[xn] }, n = power of 16 (Length = 2*pow(16,n))</summary> public static double[] FFT16(double[] x) { if (CLx == null || CLx.OriginalVarLength != x.Length) { CLx = new CLCalc.Program.Variable(x); CLy = new CLCalc.Program.Variable(x); } //Writes original content CLx.WriteToDevice(x); CLy = FFT16(ref CLx); double[] y = new double[x.Length]; CLy.ReadFromDeviceTo(y); return y; }
/// <summary>Computes the inverse Discrete Fourier Transform of a float2 vector x whose length is a power of 4. /// x = { Re[x0] Im[x0] Re[x1] Im[x1] ... Re[xn] Im[xn] }, n = power of 4 (Length = 2*pow(4,n))</summary> public static float[] iFFT4(float[] x) { if (CLx == null || CLx.OriginalVarLength != x.Length) { CLx = new CLCalc.Program.Variable(x); CLy = new CLCalc.Program.Variable(x); } //Writes original content CLx.WriteToDevice(x); CLy = iFFT4(CLx); float[] y = new float[x.Length]; CLy.ReadFromDeviceTo(y); return y; }
private void button3_Click(object sender, EventArgs e) { float[] x = new float[] { 1, 2, 3, 0.123f }; float[] y = new float[] { 1, 2, 1, 1 }; string s = @" kernel void sum (global float4 * x, global float4 * y) { x[0] = x[0] + y[0]; } "; CLCalc.Program.Compile(new string[] { s }); CLCalc.Program.Kernel sum = new CLCalc.Program.Kernel("sum"); CLCalc.Program.Variable varx=new CLCalc.Program.Variable(x); CLCalc.Program.Variable vary=new CLCalc.Program.Variable(y); CLCalc.Program.Variable[] args = { varx, vary }; int[] max = new int[] { 1 }; sum.Execute(args, max); varx.ReadFromDeviceTo(x); //float[] t = new float[1] { 0 }; //float[] pos = new float[] { 1, 2, 3, 4, 5, 6 }; //float[] vel = new float[] { 1, 0, 0, 0, 0, 0 }; //float[] forces = new float[] { 0, 1, 0, 0, 0, 0 }; //float[] masses = new float[] { 1, 1 }; //float[] colSizes = new float[] { 0.1f, 0.1f }; //CLCalc.InitCL(); //CLCalc.CLPrograms.floatBodyPhysics phys = new CLCalc.CLPrograms.floatBodyPhysics(10); //CLCalc.CLPrograms.floatBodyPhysics phys2 = new CLCalc.CLPrograms.floatBodyPhysics(20); //CLCalc.FinishCL(); }
private void button1_Click(object sender, EventArgs e) { CLCalc.InitCL(); double[] a = new double[] { 2, 147483647, 2, 7 }; double[] b = new double[] { 1, 2, 7, 4 }; double[] c = new double[4]; CLCalc.Program.Variable v1 = new CLCalc.Program.Variable(a); CLCalc.Program.Variable v2 = new CLCalc.Program.Variable(b); CLCalc.Program.Variable v3 = new CLCalc.Program.Variable(c); CLCalc.CLPrograms.VectorSum VecSum = new CLCalc.CLPrograms.VectorSum(); CLCalc.CLPrograms.MinDifs Mdifs = new CLCalc.CLPrograms.MinDifs(); //string[] s = new string[] { VecSum.intVectorSum, VecSum.floatVectorSum }; string[] s = new string[] { VecSum.doubleVectorSum }; CLCalc.Program.Compile(s); CLCalc.Program.Kernel k = new CLCalc.Program.Kernel("doubleVectorSum"); //CLCalc.Program.Kernel k2 = new CLCalc.Program.Kernel("intVectorSum"); //CLCalc.Program.Kernel k = new CLCalc.Program.Kernel("floatMinDifs"); CLCalc.Program.Variable[] vv = new CLCalc.Program.Variable[3] { v1, v2, v3 }; int[] max=new int[1] {a.Length}; k.Execute(vv, max); CLCalc.Program.Sync(); v3.ReadFromDeviceTo(c); CLCalc.FinishCL(); }
public float[] Add(float[] B, int startx, int starty, int startz, int Bx, int By) { float[] start = new float[3] { startx, starty, startz }; float[] size = new float[6] { X, Y, Z, B.GetLength(0), Bx, By }; float[] csResult = new float[X * Y * Z]; /*In order to avoid OOM, will start at the startz value, and pass in one 2D matrix at a time. * */ if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.Unknown) CLCalc.InitCL(); if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.UsingCL) { CLSource source = new CLSource(); CLCalc.Program.Compile(new string[] { source.AddSubset_3string }); Matrix = new CLCalc.Program.Kernel("AddSubset3"); } CLCalc.Program.Variable dev_A = new CLCalc.Program.Variable(M); CLCalc.Program.Variable dev_B = new CLCalc.Program.Variable(B); CLCalc.Program.Variable dev_start = new CLCalc.Program.Variable(start); CLCalc.Program.Variable dev_size = new CLCalc.Program.Variable(size); CLCalc.Program.Variable dev_Result = new CLCalc.Program.Variable(csResult); CLCalc.Program.Variable[] args = new CLCalc.Program.Variable[5] { dev_A, dev_B, dev_start, dev_size, dev_Result }; Matrix.Execute(args, new int[] { X * Y * Z }); dev_Result.ReadFromDeviceTo(csResult); M = csResult; return csResult; }