4

What is the best way to operate with complex numbers using jCuda? Should I use cuComplex format or is there any other solution (like an array with real and imaginary parts going one after another)? I would really appreciate examples of java code with this type of calculations.

As my purpose is to solve big systems of linear equations with complex numbers using GPU, I would not like to attach to jCuda only. What are the alternative ways to conduct such calculations with GPU?

4

1 回答 1

3

首先,关于您关于在 GPU 上使用 Java 进行计算的一般问题,我在这里写了几句话。

您的应用案例似乎非常具体。您可能应该更详细地描述您的实际意图,因为这将支配所有设计决策。到目前为止,我只能给出一些基本的提示。决定哪个是最合适的解决方案取决于您。


在 Java 世界和 GPU 世界之间架桥时的主要困难之一是内存处理根本不同。

C/C++ 中的内存布局

CUDA 中的cuComplex结构定义为

typedef float2 cuFloatComplex
typedef cuFloatComplex cuComplex;

wherefloat2基本上是类似的东西

struct float2 {
    float x; 
    float y; 
};

(带有一些额外的对齐说明符等)

现在,当您在 C/C++ 程序中分配值数组时cuComplex,您只需编写类似

cuComplex *c = new cuComplex[100];

在这种情况下,可以保证所有这些cuComplex值的内存将是单个连续的内存块。这个内存块只包含复数的所有x和值,一个接一个:y

      _____________________________
c -> | x0 | y0 | x1 | y1 | x2 | y2 |... 
     |____|____|____|____|____|____|

这个连续的内存块可以很容易地复制到设备中:一个人获取指针,然后调用类似的调用

cudaMemcpy(device, c, sizeof(cuComplex)*n, cudaMemcpyHostToDevice);

Java中的内存布局

考虑创建一个在结构上与结构相同的 Java 类cuComplex并分配以下数组的情况:

 class cuComplex {
     public float x;
     public float y;
 }

 cuComplex c[] = new cuComplex[100];

那么你就没有一个连续的float值的内存块。相反,您有一个对对象的引用数组cuComplex,并且各自的xy值分散在各处:

      ____________________
c -> |  c0  |  c1  |  c2  |... 
     |______|______|______|
        |       |      |
        v       v      v
      [x,y]   [x,y]  [x,y]

这里的关键点是:

您不能将(Java)cuComplex对象数组复制到设备!.


这有几个含义。在评论中,您已经提到了cublasSetVector将数组作为参数的方法,cuComplex我试图强调这不是最有效的解决方案,只是为了方便起见。事实上,这种方法通过在内部创建一个新ByteBuffer的以获得连续的内存块,ByteBuffer用数组中的值填充它cuComplex[],然后将其复制ByteBuffer到设备来工作。

当然,这会带来开销,您很可能希望在性能关键型应用程序中避免这种开销。


有几个选项可以解决这个问题。幸运的是,对于复数,解决方案相对简单:

不要使用cuComplex结构来表示复数数组

相反,您应该将复数数组表示为单个连续的内存块,其中复数的实部和虚部交错,每个分别是单个floatdouble值。这将允许不同后端之间最大的互操作性(省略某些细节,如对齐要求)。

不幸的是,这可能会造成一些不便并提出一些问题,并且对此没有万能的解决方案。

如果试图概括这一点,不仅指复数,而且指一般的“结构”,那么可以应用一种“模式”:可以为结构创建接口,并创建这些结构的集合这是实现此接口的类的实例列表,它们都由一个连续的内存块支持。这可能适用于某些情况。但是对于复数,每个复数都有一个 Java 对象的内存开销可能非常大。

另一个极端,仅处理原始float[]double[]数组也可能不是最佳解决方案。例如:如果您有一个float表示复数的值数组,那么如何将这些复数中的一个与另一个相乘?

一种“中间”解决方案可以创建一个接口,允许访问复数的实部和虚部。在实现中,这些复数存储在单个数组中,如上所述。


我在这里画了一个这样的实现。

笔记:

这只是作为一个例子,展示基本思想,并展示它如何与 JCublas 之类的东西一起工作。对您而言,不同的策略可能更合适,具体取决于您的实际目标:应该有哪些其他后端(除了 JCuda)?在 Java 端处理复数应该有多“方便”?在 Java 端处理复数的结构(类/接口)应该是什么样的?

简而言之:在继续实施之前,您应该非常清楚您的应用程序/库应该能够做什么。

import static jcuda.jcublas.JCublas2.*;
import static jcuda.jcublas.cublasOperation.CUBLAS_OP_N;
import static jcuda.runtime.JCuda.*;

import java.util.Random;

import jcuda.*;
import jcuda.jcublas.cublasHandle;
import jcuda.runtime.cudaMemcpyKind;

// An interface describing an array of complex numbers, residing
// on the host, with methods for accessing the real and imaginary
// parts of the complex numbers, as well as methods for copying
// the underlying data from and to the device
interface cuComplexHostArray
{
    int size();

    float getReal(int i);
    float getImag(int i);

    void setReal(int i, float real);
    void setImag(int i, float imag);

    void set(int i, cuComplex c);
    void set(int i, float real, float imag);

    cuComplex get(int i, cuComplex c);

    void copyToDevice(Pointer devicePointer);
    void copyFromDevice(Pointer devicePointer);
}

// A default implementation of a cuComplexHostArray, backed
// by a single float[] array
class DefaultCuComplexHostArray implements cuComplexHostArray
{
    private final int size;
    private final float data[];

    DefaultCuComplexHostArray(int size)
    {
        this.size = size;
        this.data = new float[size * 2];
    }

    @Override
    public int size()
    {
        return size;
    }

    @Override
    public float getReal(int i)
    {
        return data[i+i];
    }

    @Override
    public float getImag(int i)
    {
        return data[i+i+1];
    }

    @Override
    public void setReal(int i, float real)
    {
        data[i+i] = real;
    }

    @Override
    public void setImag(int i, float imag)
    {
        data[i+i+1] = imag;
    }

    @Override
    public void set(int i, cuComplex c)
    {
        data[i+i+0] = c.x;
        data[i+i+1] = c.y;
    }

    @Override
    public void set(int i, float real, float imag)
    {
        data[i+i+0] = real;
        data[i+i+1] = imag;
    }

    @Override
    public cuComplex get(int i, cuComplex c)
    {
        float real = getReal(i);
        float imag = getImag(i);
        if (c != null)
        {
            c.x = real;
            c.y = imag;
            return c;
        }
        return cuComplex.cuCmplx(real, imag);
    }

    @Override
    public void copyToDevice(Pointer devicePointer)
    {
        cudaMemcpy(devicePointer, Pointer.to(data),
            size * Sizeof.FLOAT * 2,
            cudaMemcpyKind.cudaMemcpyHostToDevice);
    }

    @Override
    public void copyFromDevice(Pointer devicePointer)
    {
        cudaMemcpy(Pointer.to(data), devicePointer,
            size * Sizeof.FLOAT * 2,
            cudaMemcpyKind.cudaMemcpyDeviceToHost);
    }
}

// An example that performs a "gemm" with complex numbers, once
// in Java and once in JCublas2, and verifies the result
public class JCublas2ComplexSample
{
    public static void main(String args[])
    {
        testCgemm(500);
    }

    public static void testCgemm(int n)
    {
        cuComplex alpha = cuComplex.cuCmplx(0.3f, 0.2f);
        cuComplex beta  = cuComplex.cuCmplx(0.1f, 0.7f);
        int nn = n * n;

        System.out.println("Creating input data...");
        Random random = new Random(0);
        cuComplex[] rhA = createRandomComplexRawArray(nn, random);
        cuComplex[] rhB = createRandomComplexRawArray(nn, random);
        cuComplex[] rhC = createRandomComplexRawArray(nn, random);

        random = new Random(0);
        cuComplexHostArray hA = createRandomComplexHostArray(nn, random);
        cuComplexHostArray hB = createRandomComplexHostArray(nn, random);
        cuComplexHostArray hC = createRandomComplexHostArray(nn, random);

        System.out.println("Performing Cgemm with Java...");
        cgemmJava(n, alpha, rhA, rhB, beta, rhC);

        System.out.println("Performing Cgemm with JCublas...");
        cgemmJCublas(n, alpha, hA, hB, beta, hC);

        boolean passed = isCorrectResult(hC, rhC);
        System.out.println("testCgemm "+(passed?"PASSED":"FAILED"));
    }

    private static void cgemmJCublas(
        int n,
        cuComplex alpha,
        cuComplexHostArray A,
        cuComplexHostArray B,
        cuComplex beta,
        cuComplexHostArray C)
    {
        int nn = n * n;

        // Create a CUBLAS handle
        cublasHandle handle = new cublasHandle();
        cublasCreate(handle);

        // Allocate memory on the device
        Pointer dA = new Pointer();
        Pointer dB = new Pointer();
        Pointer dC = new Pointer();
        cudaMalloc(dA, nn * Sizeof.FLOAT * 2);
        cudaMalloc(dB, nn * Sizeof.FLOAT * 2);
        cudaMalloc(dC, nn * Sizeof.FLOAT * 2);

        // Copy the memory from the host to the device
        A.copyToDevice(dA);
        B.copyToDevice(dB);
        C.copyToDevice(dC);

        // Execute cgemm
        Pointer pAlpha = Pointer.to(new float[]{alpha.x, alpha.y});
        Pointer pBeta = Pointer.to(new float[]{beta.x, beta.y});
        cublasCgemm(handle, CUBLAS_OP_N, CUBLAS_OP_N, n, n, n,
            pAlpha, dA, n, dB, n, pBeta, dC, n);

        // Copy the result from the device to the host
        C.copyFromDevice(dC);

        // Clean up
        cudaFree(dA);
        cudaFree(dB);
        cudaFree(dC);
        cublasDestroy(handle);
    }

    private static void cgemmJava(
        int n,
        cuComplex alpha,
        cuComplex A[],
        cuComplex B[],
        cuComplex beta,
        cuComplex C[])
    {
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                cuComplex prod = cuComplex.cuCmplx(0, 0);
                for (int k = 0; k < n; ++k)
                {
                    cuComplex ab =
                        cuComplex.cuCmul(A[k * n + i], B[j * n + k]);
                    prod = cuComplex.cuCadd(prod, ab);
                }
                cuComplex ap = cuComplex.cuCmul(alpha, prod);
                cuComplex bc = cuComplex.cuCmul(beta, C[j * n + i]);
                C[j * n + i] = cuComplex.cuCadd(ap, bc);
            }
        }
    }

    private static cuComplex[] createRandomComplexRawArray(
        int n, Random random)
    {
        cuComplex c[] = new cuComplex[n];
        for (int i = 0; i < n; i++)
        {
            float real = random.nextFloat();
            float imag = random.nextFloat();
            c[i] = cuComplex.cuCmplx(real, imag);
        }
        return c;
    }

    private static cuComplexHostArray createRandomComplexHostArray(
        int n, Random random)
    {
        cuComplexHostArray c = new DefaultCuComplexHostArray(n);
        for (int i = 0; i < n; i++)
        {
            float real = random.nextFloat();
            float imag = random.nextFloat();
            c.setReal(i, real);
            c.setImag(i, imag);
        }
        return c;
    }

    private static boolean isCorrectResult(
        cuComplexHostArray result, cuComplex reference[])
    {
        float errorNormX = 0;
        float errorNormY = 0;
        float refNormX = 0;
        float refNormY = 0;
        for (int i = 0; i < result.size(); i++)
        {
            float diffX = reference[i].x - result.getReal(i);
            float diffY = reference[i].y - result.getImag(i);
            errorNormX += diffX * diffX;
            errorNormY += diffY * diffY;
            refNormX += reference[i].x * result.getReal(i);
            refNormY += reference[i].y * result.getImag(i);
        }
        errorNormX = (float) Math.sqrt(errorNormX);
        errorNormY = (float) Math.sqrt(errorNormY);
        refNormX = (float) Math.sqrt(refNormX);
        refNormY = (float) Math.sqrt(refNormY);
        if (Math.abs(refNormX) < 1e-6)
        {
            return false;
        }
        if (Math.abs(refNormY) < 1e-6)
        {
            return false;
        }
        return
            (errorNormX / refNormX < 1e-6f) &&
            (errorNormY / refNormY < 1e-6f);
    }
}

(顺便说一句:我可能会采取这个答案的一部分并将它们扩展为JCuda的样本和/或“如何...”页面。提供一些这样的信息的任务已经在我的“待办事项”上列出了很长一段时间)。

于 2015-04-26T13:54:37.013 回答