addMatrixGPU function

GPUTensor<Matrix> addMatrixGPU(
  1. GPUTensor<Matrix> a,
  2. GPUTensor<Matrix> b,
  3. CommandBuffer tape, {
  4. GPUTensor<Matrix>? outTensor,
})

Adds the command for addition of two matrices a and b to the tape. Creates and allocates a Tensor to store the results.

Implementation

GPUTensor<Matrix> addMatrixGPU(
    GPUTensor<Matrix> a, GPUTensor<Matrix> b, CommandBuffer tape,
    {GPUTensor<Matrix>? outTensor}) {
  int numRows = a.shape[0];
  int numCols = a.shape[1];

  // Initialize with an empty structure to set the shape
  GPUTensor<Matrix> out =
      outTensor ?? GPUTensor<Matrix>.empty(<int>[numRows, numCols]);

  tape.putInt(OP_ADD);
  tape.putString(a.id);
  tape.putString(b.id);
  tape.putString(out.id);

  out.creator = GPUNode(
    [a, b],
    (CommandBuffer bTape) {
      bTape.putInt(OP_ADD_INTO);
      bTape.putString(out.gradId);
      bTape.putString(a.gradId);

      bTape.putInt(OP_ADD_INTO);
      bTape.putString(out.gradId);
      bTape.putString(b.gradId);
    },
    opName: 'add_matrixGPU',
    cost: numRows * numCols,
  );

  return out;
}