cudamatrix-multiplicationdotcublasaccumulate

CUBLAS accumulate output


This is a very simple question about Cublas library which I strangely couldn't find answer in documentation or elsewhere.

I am using rather old version of CUBLAS (10.2) but it should not matter. I use cublasSgemm to multiply two 32-bit floats matrices A * B and put the result in matrix C:

stat = cublasSgemm(handle, CUBLAS_OP_N, CUBLAS_OP_T, nRows, k, nCols, alpha, A, nRows, B, k, beta, C, nRows);

Is it possible to make CUBLAS to accumulate the result in C? This means that if C contains some data it would not be erased but accumulated with the multiplication result?

This can be used for example when memory is limited and one need to shrink sizes of input matrices if are too big and multiply several times. I however couldn't see such option in cublasSgemm?


Solution

  • Is it possible to make CUBLAS to accumulate the result in C? This means that if C contains some data it would not be erased but accumulated with the multiplication result?

    Yes, cublasSgemm does exactly that. Referring to the documentation:

    This function performs the matrix-matrix multiplication

    C=αop(A)op(B)+βC
                 ^^^
                 This is the accumulation part of the formula.
    

    If you set beta to zero, then the previous contents of C will not be accumulated.

    If you set beta to 1, then the previous contents of C will be added to the multiplication (AxB) result.

    If you set beta to some other value, a scaled (multiplied) version of the previous contents of C will be added.

    Note that as far as this description and function are concerned, all of this functionality was defined/specified as part of the netlib BLAS description, and should be similar to other BLAS libraries, and is not unique or specific to CUBLAS.