private static extern CUBLASStatusv2 cublasCrot_v2(cublasHandle handle, int n, IntPtr x, int incx, IntPtr y, int incy, ref float c, ref ComplexF s);
public CUBLASStatusv2 cublasCaxpy(cublasHandle handle, int n, ref ComplexF alpha, IntPtr x, int incx, IntPtr y, int incy) { return cublasCaxpy_v2(handle, n, ref alpha, x, incx, y, incy); }
/// <summary> /// Subtracts value y from value x. /// </summary> /// <param name="x">Value one.</param> /// <param name="y">Value to be subtracted.</param> /// <returns>New value.</returns> public static ComplexF Subtract(ComplexF x, ComplexF y) { x.x = x.x - y.x; x.y = x.y - y.y; return(x); }
private static extern CUBLASStatusv2 cublasCaxpy_v2(cublasHandle handle, int n, ref ComplexF alpha, IntPtr x, int incx, IntPtr y, int incy);
/// <summary> /// ROTGs the specified a. /// </summary> /// <param name="a">A.</param> /// <param name="b">The b.</param> /// <param name="c">The c.</param> /// <param name="s">The s.</param> public abstract void ROTG(ComplexF[] a, ComplexF[] b, float[] c, ComplexF[] s);
/// <summary> /// Conjugates the specified value. /// </summary> /// <param name="x">The value.</param> /// <returns>Conjugated value.</returns> public static ComplexF Conj(ComplexF x) { return(new ComplexF(x.x, -1 * x.y)); }
/// <summary> /// Adds value y to value x. /// </summary> /// <param name="x">Value one.</param> /// <param name="y">Value to be added.</param> /// <returns>New value.</returns> public static ComplexF Add(ComplexF x, ComplexF y) { x.x = x.x + y.x; x.y = x.y + y.y; return x; }
/// <summary> /// ROTs the specified vectorx. /// </summary> /// <param name="vectorx">The vectorx.</param> /// <param name="vectory">The vectory.</param> /// <param name="c">The c.</param> /// <param name="s">The s.</param> /// <param name="n">The n.</param> /// <param name="rowx">The rowx.</param> /// <param name="incx">The incx.</param> /// <param name="rowy">The rowy.</param> /// <param name="incy">The incy.</param> public void ROT(ComplexF[] vectorx, ComplexF[] vectory, float c, float s, int n = 0, int rowx = 0, int incx = 1, int rowy = 0, int incy = 1) { ROT(vectorx, vectory, new[] { c }, new[] { s }, n, rowx, incx, rowy, incy); }
/// <summary> /// Gets the absolute of the specified value. /// </summary> /// <param name="x">The value.</param> /// <returns>Absolute.</returns> public static float Abs(ComplexF x) { float a = x.x; float b = x.y; float v, w, t; a = Math.Abs(a); b = Math.Abs(b); if (a > b) { v = a; w = b; } else { v = b; w = a; } t = w / v; t = 1.0f + t * t; t = v * GMath.Sqrt(t); if ((v == 0.0f) || (v > 3.402823466e38f) || (w > 3.402823466e38f)) { t = v + w; } return t; }
/// <summary> /// Conjugates the specified value. /// </summary> /// <param name="x">The value.</param> /// <returns>Conjugated value.</returns> public static ComplexF Conj(ComplexF x) { return new ComplexF(x.x, -1 * x.y); }
/// <summary> /// Divides value x by y. /// </summary> /// <param name="x">Value one.</param> /// <param name="y">Value two.</param> /// <returns>New value.</returns> public static ComplexF Divide(ComplexF x, ComplexF y) { float s = Math.Abs(y.x) + Math.Abs(y.y); float oos = 1.0f / s; float ars = x.x * oos; float ais = x.y * oos; float brs = y.x * oos; float bis = y.y * oos; s = (brs * brs) + (bis * bis); oos = 1.0f / s; ComplexF quot = new ComplexF(((ars * brs) + (ais * bis)) * oos, ((ais * brs) - (ars * bis)) * oos); return quot; }
public static void Execute() { CudafyModule km = CudafyTranslator.Cudafy(); GPGPU gpu = CudafyHost.GetDevice(CudafyModes.Target); gpu.LoadModule(km); // 2D Console.WriteLine("kernel"); ComplexF[,] host_A = new ComplexF[XSIZE, YSIZE]; ComplexF[,] host_B = new ComplexF[XSIZE, YSIZE]; ComplexF[,] host_C = new ComplexF[XSIZE, YSIZE]; int i = 0; for (int x = 0; x < XSIZE; x++) for (int y = 0; y < YSIZE; y++) { host_A[x, y] = new ComplexF(i, i); host_B[x, y] = new ComplexF(2, 0); i++; } ComplexF[,] dev_A = gpu.CopyToDevice(host_A); ComplexF[,] dev_B = gpu.CopyToDevice(host_B); ComplexF[,] dev_C = gpu.Allocate<ComplexF>(XSIZE, YSIZE); Console.WriteLine("complexAdd"); gpu.Launch(XSIZE, 1, "complexAdd", dev_A, dev_B, dev_C); gpu.CopyFromDevice(dev_C, host_C); i = 0; bool pass = true; for (int x = 0; x < XSIZE; x++) { for (int y = 0; y < YSIZE && pass; y++) { ComplexF expected = ComplexF.Add(host_A[x, y], host_B[x, y]); pass = host_C[x, y].x == expected.x && host_C[x, y].y == expected.y; } } Console.WriteLine(pass ? "Pass" : "Fail"); Console.WriteLine("complexSub"); gpu.Launch(XSIZE, 1, "complexSub", dev_A, dev_B, dev_C); gpu.CopyFromDevice(dev_C, host_C); i = 0; pass = true; for (int x = 0; x < XSIZE; x++) { for (int y = 0; y < YSIZE && pass; y++) { ComplexF expected = ComplexF.Subtract(host_A[x, y], host_B[x, y]); pass = host_C[x, y].x == expected.x && host_C[x, y].y == expected.y; } } Console.WriteLine(pass ? "Pass" : "Fail"); Console.WriteLine("complexMpy"); gpu.Launch(XSIZE, 1, "complexMpy", dev_A, dev_B, dev_C); gpu.CopyFromDevice(dev_C, host_C); i = 0; pass = true; for (int x = 0; x < XSIZE; x++) { for (int y = 0; y < YSIZE && pass; y++) { ComplexF expected = ComplexF.Multiply(host_A[x, y], host_B[x, y]); //Console.WriteLine("{0} {1} : {2} {3}", host_C[x, y].R, host_C[x, y].I, expected.R, expected.I); pass = Verify(host_C[x, y], expected, 1e-14F); i++; } } Console.WriteLine(pass ? "Pass" : "Fail"); Console.WriteLine("complexDiv"); gpu.Launch(XSIZE, 1, "complexDiv", dev_A, dev_B, dev_C); gpu.CopyFromDevice(dev_C, host_C); i = 0; pass = true; for (int x = 0; x < XSIZE; x++) { for (int y = 0; y < YSIZE && pass; y++) { ComplexF expected = ComplexF.Divide(host_A[x, y], host_B[x, y]); //Console.WriteLine("{0} {1} : {2} {3}", host_C[x, y].R, host_C[x, y].I, expected.R, expected.I); if (i > 0) pass = Verify(host_C[x, y], expected, 1e-13F); i++; } } Console.WriteLine(pass ? "Pass" : "Fail"); Console.WriteLine("complexAbs"); gpu.Launch(XSIZE, 1, "complexAbs", dev_A, dev_C); gpu.CopyFromDevice(dev_C, host_C); i = 0; pass = true; for (int x = 0; x < XSIZE; x++) { for (int y = 0; y < YSIZE && pass; y++) { float expected = ComplexF.Abs(host_A[x, y]); pass = Verify(host_C[x, y].x, expected, 1e-2F); //Console.WriteLine("{0} {1} : {2}", host_C[x, y].x, host_C[x, y].y, expected); i++; } } Console.WriteLine(pass ? "Pass" : "Fail"); gpu.FreeAll(); }
public static void complexAbs(GThread thread, ComplexF[,] a, ComplexF[,] c) { int x = thread.blockIdx.x; int y = 0; float j = 1.0F; float k = 2.0F; ComplexF cf = new ComplexF(3 + j, 7 + k); while (y < YSIZE) { c[x, y].x = ComplexF.Abs(a[x, y]); c[x, y].y = cf.y;// 0.0F; y++; } }
public CUBLASStatusv2 cublasCrot(cublasHandle handle, int n, IntPtr x, int incx, IntPtr y, int incy, ref float c, ref ComplexF s) { return cublasCrot_v2(handle, n, x, incx, y, incy, ref c, ref s); }
/// <summary> /// Subtracts value y from value x. /// </summary> /// <param name="x">Value one.</param> /// <param name="y">Value to be subtracted.</param> /// <returns>New value.</returns> public static ComplexF Subtract(ComplexF x, ComplexF y) { x.x = x.x - y.x; x.y = x.y - y.y; return x; }
public static void complexDiv(GThread thread, ComplexF[,] a, ComplexF[,] b, ComplexF[,] c) { int x = thread.blockIdx.x; int y = 0; while (y < YSIZE) { c[x, y] = ComplexF.Divide(a[x, y], b[x, y]); y++; } }
/// <summary> /// Multiplies value x and y. /// </summary> /// <param name="x">Value one.</param> /// <param name="y">Value two.</param> /// <returns>New value.</returns> public static ComplexF Multiply(ComplexF x, ComplexF y) { ComplexF c = new ComplexF(); c.x = x.x * y.x - x.y * y.y; c.y = x.x * y.y + x.y * y.x; return c; }
/// <summary> /// ROTs the specified vectorx. /// </summary> /// <param name="vectorx">The vectorx.</param> /// <param name="vectory">The vectory.</param> /// <param name="c">The c.</param> /// <param name="s">The s.</param> /// <param name="n">The n.</param> /// <param name="rowx">The rowx.</param> /// <param name="incx">The incx.</param> /// <param name="rowy">The rowy.</param> /// <param name="incy">The incy.</param> public abstract void ROT(ComplexF[] vectorx, ComplexF[] vectory, float[] c, float[] s, int n = 0, int rowx = 0, int incx = 1, int rowy = 0, int incy = 1);
/// <summary> /// SCALs the specified alpha. /// </summary> /// <param name="alpha">The alpha.</param> /// <param name="vectorx">The vectorx.</param> /// <param name="n">The n.</param> /// <param name="rowx">The rowx.</param> /// <param name="incx">The incx.</param> public abstract void SCAL(float[] alpha, ComplexF[] vectorx, int n = 0, int rowx = 0, int incx = 1);
/// <summary> /// SCALs the specified alpha. /// </summary> /// <param name="alpha">The alpha.</param> /// <param name="vectorx">The vectorx.</param> /// <param name="n">The n.</param> /// <param name="rowx">The rowx.</param> /// <param name="incx">The incx.</param> public void SCAL(float alpha, ComplexF[] vectorx, int n = 0, int rowx = 0, int incx = 1) { SCAL(new []{ alpha }, vectorx, n, rowx, incx); }
/// <summary> /// IAMINs the specified vectorx. /// </summary> /// <param name="vectorx">The vectorx.</param> /// <param name="n">The n.</param> /// <param name="rowx">The rowx.</param> /// <param name="incx">The incx.</param> /// <returns></returns> public abstract int IAMIN(ComplexF[] vectorx, int n = 0, int rowx = 0, int incx = 1);
/// <summary> /// Adds value y to value x. /// </summary> /// <param name="x">Value one.</param> /// <param name="y">Value to be added.</param> /// <returns>New value.</returns> public static ComplexF Add(ComplexF x, ComplexF y) { x.x = x.x + y.x; x.y = x.y + y.y; return(x); }
/// <summary> /// AXPYs the specified alpha. /// </summary> /// <param name="alpha">The alpha.</param> /// <param name="vectorx">The vectorx.</param> /// <param name="vectory">The vectory.</param> /// <param name="n">The n.</param> /// <param name="rowx">The rowx.</param> /// <param name="incx">The incx.</param> /// <param name="rowy">The rowy.</param> /// <param name="incy">The incy.</param> public void AXPY(ComplexF alpha, ComplexD[] vectorx, ComplexD[] vectory, int n = 0, int rowx = 0, int incx = 1, int rowy = 0, int incy = 1) { AXPY(new [] { alpha }, vectorx, vectory, n, rowx, incx, rowy, incy); }
public void SetUp() { _gpu = CudafyHost.CreateDevice(CudafyModes.Target); Console.WriteLine("CUDA driver version={0}", _gpu.GetDriverVersion()); _fft = GPGPUFFT.Create(_gpu); _hostInput = new float[N * BATCH]; _hostInputCplx = new ComplexF[N * BATCH]; _hostOutput = new float[N * BATCH]; _hostOutputCplx = new ComplexF[N * BATCH]; _devInput = _gpu.Allocate(_hostInput); _devInputCplx = _gpu.Allocate(_hostInputCplx); _devInter = _gpu.Allocate<float>(N * 2 * BATCH); _devInterCplx = _gpu.Allocate<ComplexF>(N * BATCH); _devOutput = _gpu.Allocate(_hostOutput); _devOutputCplx = _gpu.Allocate(_hostOutputCplx); Console.WriteLine("CUFFT version={0}", _fft.GetVersion()); for (int b = 0; b < BATCH; b++) { for (int i = 0; i < N; i++) { ComplexF cf = new ComplexF(); cf.x = (float)((10.0F * Math.Sin(100 * 2 * Math.PI * i / N * Math.PI / 180))); cf.y = (float)((10.0F * Math.Sin(200 * 2 * Math.PI * i / N * Math.PI / 180))); _hostInput[i + b * N] = cf.x; _hostInputCplx[i + b * N] = cf; } } }
/// <summary> /// AXPYs the specified alpha. /// </summary> /// <param name="alpha">The alpha.</param> /// <param name="vectorx">The vectorx.</param> /// <param name="vectory">The vectory.</param> /// <param name="n">The n.</param> /// <param name="rowx">The rowx.</param> /// <param name="incx">The incx.</param> /// <param name="rowy">The rowy.</param> /// <param name="incy">The incy.</param> protected abstract void AXPY(ComplexF[] alpha, ComplexD[] vectorx, ComplexD[] vectory, int n = 0, int rowx = 0, int incx = 1, int rowy = 0, int incy = 1);
private static extern CUBLASStatusv2 cublasCdotc_v2(cublasHandle handle, int n, IntPtr x, int incx, IntPtr y, int incy, ref ComplexF result);
/// <summary> /// COPYs the specified vectorx. /// </summary> /// <param name="vectorx">The vectorx.</param> /// <param name="vectory">The vectory.</param> /// <param name="n">The n.</param> /// <param name="rowx">The rowx.</param> /// <param name="incx">The incx.</param> /// <param name="rowy">The rowy.</param> /// <param name="incy">The incy.</param> public abstract void COPY(ComplexF[] vectorx, ComplexF[] vectory, int n = 0, int rowx = 0, int incx = 1, int rowy = 0, int incy = 1);
private static extern CUBLASStatusv2 cublasCrotg_v2(cublasHandle handle, ref ComplexF a, ref ComplexF b, ref float c, ref ComplexF s);
/// <summary> /// DOTCs the specified vectorx. /// </summary> /// <param name="vectorx">The vectorx.</param> /// <param name="vectory">The vectory.</param> /// <param name="n">The n.</param> /// <param name="rowx">The rowx.</param> /// <param name="incx">The incx.</param> /// <param name="rowy">The rowy.</param> /// <param name="incy">The incy.</param> /// <returns></returns> public abstract ComplexF DOTC(ComplexF[] vectorx, ComplexF[] vectory, int n = 0, int rowx = 0, int incx = 1, int rowy = 0, int incy = 1);
public CUBLASStatusv2 cublasCdotc(cublasHandle handle, int n, IntPtr x, int incx, IntPtr y, int incy, ref ComplexF result) { return cublasCdotc_v2(handle, n, x, incx, y, incy, ref result); }
/// <summary> /// NRs the m2. /// </summary> /// <param name="vectorx">The vectorx.</param> /// <param name="n">The n.</param> /// <param name="rowx">The rowx.</param> /// <param name="incx">The incx.</param> /// <returns></returns> public abstract float NRM2(ComplexF[] vectorx, int n = 0, int rowx = 0, int incx = 1);
public CUBLASStatusv2 cublasCrotg(cublasHandle handle, ref ComplexF a, ref ComplexF b, ref float c, ref ComplexF s) { return cublasCrotg_v2(handle, ref a, ref b, ref c, ref s); }
private static bool Verify(ComplexF x, ComplexF y, float delta) { if (Math.Abs(x.x - y.x) > delta || Math.Abs(x.y - y.y) > delta) return false; return true; }