并行转置不同矩阵

时间:2019-05-06 08:55:32

标签: c++ cuda

我喜欢3种不同大小的矩阵,并希望将它们并行转置。

首先,我使用malloc将它们放入2D数组中,然后使用cudaMalloc将数组从主机(h_B)传输到设备(d_B)。

使用threadIdx查找数组中矩阵的每个地址。使用cublas函数。 这是我的代码。

该代码可以编译,但无法得到结果。在全局函数float *A = new float[m*n]中似乎不是一个好方法。

有人对此有想法吗? 非常感谢!

/* Includes, system */
#include <stdio.h>
#include <stdlib.h>
#include<iostream>

/* Includes, cuda */
#include <cuda_runtime.h>
#include <cublas_v2.h>

/* Includes, cuda helper functions */
#include <helper_cuda.h>

__global__ void transposeCublasSgeam(int *M_A, int *N_A, float *ptrA, float *ptrC, const int N, int *address)
{
    cublasHandle_t cnpHandle;
    cublasStatus_t status = cublasCreate(&cnpHandle);

    if (status != CUBLAS_STATUS_SUCCESS)
    {
        return;
    }

    const float d_alpha = 1.0f;
    const float d_beta = 0.0f;
    int idx = threadIdx.x;
    if(idx<N){
      int m = M_A[idx]; //A_row
      int n = N_A[idx]; //A_col
      float *A = new float[m*n]; 
      float *C = new float[m*n];
      A = ptrA+address[idx];
      C = ptrC+address[idx];
      cublasSgeam(cnpHandle, CUBLAS_OP_T, CUBLAS_OP_T, m, n, &d_alpha, (const float*)A, n, &d_beta, (const float *)A, n, C, m);
      delete[] A;
      delete[] C;
    }
     cublasDestroy(cnpHandle);

}

int main()
{

    const int N = 3;
    int M_B[N] = { 2,3,2 }; //row number of matrices 
    int N_B[N] = { 3,2,4 }; //col number of matrices 

    float a[6] = { 1,2,3,
                  4,5,6 };
    float b[6] = { 1,2,
                 3,4,
                 5,6};
    float c[8] = { 1,2,3,1,
                  2,3,4,5 };

    float **h_B = (float**)malloc(N * sizeof(float*));
    float **h_BT = (float**)malloc(N * sizeof(float*));

    h_B[0] = a, h_BT[0] = a;
    h_B[1] = b, h_BT[1] = b;
    h_B[2] = c, h_BT[2] = c;

    int NUM_B = 20; // total number of elements
    int address[] = {0,6,12}; 

    float *d_B, *d_BT;
    checkCudaErrors(cudaMalloc((void **)&d_B, NUM_B * sizeof(float)));
    checkCudaErrors(cudaMalloc((void **)&d_BT, NUM_B * sizeof(float)));
    checkCudaErrors(cudaMemcpy(d_B, h_B, NUM_B * sizeof(float), cudaMemcpyHostToDevice));
    checkCudaErrors(cudaMemcpy(d_BT, h_BT, NUM_B * sizeof(float), cudaMemcpyHostToDevice));

    transposeCublasSgeam<<<1,N>>>(M_B, N_B, d_B,d_BT, N,address); 

    checkCudaErrors(cudaMemcpy(h_BT, d_BT, NUM_B * sizeof(float), cudaMemcpyDeviceToHost));

    cudaFree(d_B);
    cudaFree(d_BT);
    delete[] h_B;
    delete[] h_BT;

    return 0;
}

1 个答案:

答案 0 :(得分:1)

您的代码中存在许多错误。我可能会错过一些说明。

  1. 请注意,此cublas-in-device-code功能已在较新的CUDA版本中不再可用。
  2. 传递给设备代码的每个指针都需要分配cudaMalloc。您完成了cudaMalloc的几个指针,但并非全部。
  3. 您对指针和指针数组感到困惑。我将无法为您解决所有这些问题。您的内核设计确实不需要使用指针数组的复杂性。所以我删除了所有这些内容。
  4. 在CUDA动态并行(CDP)中,无法将指向本地地址空间的指针传递给子内核。您不能在本地地址空间中使用alpha和beta,而不能将指向它们的指针传递给CDP中的CUBLAS。
  5. 要进行纯转置,请研究the CUBLAS Sgeam documentation以使用推荐的参数。

我相信我还修复了其他问题。请研究以下示例:

$ cat t1433.cu
/* Includes, system */
#include <stdio.h>
#include <stdlib.h>
#include<iostream>

/* Includes, cuda */
#include <cuda_runtime.h>
#include <cublas_v2.h>

/* Includes, cuda helper functions */
#include <helper_cuda.h>

__global__ void transposeCublasSgeam(int *M_A, int *N_A, float *ptrA, float *ptrC, const int N, int *address)
{
    cublasHandle_t cnpHandle;
    cublasStatus_t status = cublasCreate(&cnpHandle);

    if (status != CUBLAS_STATUS_SUCCESS)
    {
        printf("thread: %d, error1: %d\n", threadIdx.x, (int)status);
        return;
    }

    float *d_alpha =  new float; // a pointer to device-heap, not local memory
    *d_alpha = 1.0f;
    float *d_beta  = new float;
    *d_beta = 0.0f;
    int idx = threadIdx.x;
    if(idx<N){
      int m = M_A[idx]; //A_row
      int n = N_A[idx]; //A_col
      status = cublasSgeam(cnpHandle, CUBLAS_OP_T, CUBLAS_OP_N, m, n, d_alpha, ptrA+address[idx], n, d_beta, ptrC+address[idx], m, ptrC+address[idx], m);
      if (status != CUBLAS_STATUS_SUCCESS)
      {
        printf("thread: %d, error2: %d\n", threadIdx.x, (int)status);
        return;
      }
    }
     cublasDestroy(cnpHandle);

}

int main()
{

    const int N = 3;
    int M_B[N] = { 2,3,2 }; //row number of matrices
    int N_B[N] = { 3,2,4 }; //col number of matrices

    float a[6] = { 1,2,3,
                  4,5,6 };
    float b[6] = { 1,2,
                 3,4,
                 5,6};
    float c[8] = { 1,2,3,1,
                  2,3,4,5 };
    float *h_Bdata  = (float *)malloc(sizeof(a)+sizeof(b)+sizeof(c));
    float *h_BTdata = (float *)malloc(sizeof(a)+sizeof(b)+sizeof(c));
    memcpy(h_Bdata, a, sizeof(a));
    memcpy(h_Bdata+(sizeof(a)/sizeof(a[0])), b, sizeof(b));
    memcpy(h_Bdata+(sizeof(a)/sizeof(a[0]))+(sizeof(b)/sizeof(b[0])), c, sizeof(c));

    int NUM_B = 20; // total number of elements
    int address[] = {0,6,12};
    int *d_address;
    cudaMalloc(&d_address, sizeof(address));
    cudaMemcpy(d_address, address, sizeof(address), cudaMemcpyHostToDevice);
    int *d_M_B, *d_N_B;
    cudaMalloc(&d_M_B, sizeof(M_B));
    cudaMalloc(&d_N_B, sizeof(N_B));
    cudaMemcpy(d_M_B, M_B, sizeof(M_B), cudaMemcpyHostToDevice);
    cudaMemcpy(d_N_B, N_B, sizeof(N_B), cudaMemcpyHostToDevice);
    float *d_B, *d_BT;
    checkCudaErrors(cudaMalloc((void **)&d_B, NUM_B * sizeof(float)));
    checkCudaErrors(cudaMalloc((void **)&d_BT, NUM_B * sizeof(float)));
    checkCudaErrors(cudaMemcpy(d_B, h_Bdata, NUM_B * sizeof(float), cudaMemcpyHostToDevice));

    transposeCublasSgeam<<<1,N>>>(d_M_B, d_N_B, d_B,d_BT, N,d_address);

    checkCudaErrors(cudaMemcpy(h_BTdata, d_BT, NUM_B * sizeof(float), cudaMemcpyDeviceToHost));
    std::cout << "B , BT" << std::endl;
    for (int i = 0; i < NUM_B; i++){
      std::cout << h_Bdata[i]  << " , " << h_BTdata[i] << std::endl;}
    cudaFree(d_B);
    cudaFree(d_BT);

    return 0;
}
$ /usr/local/cuda-8.0/bin/nvcc -I/usr/local/cuda-8.0/samples/common/inc  t1433.cu -rdc=true -lcublas_device -lcudadevrt -arch=sm_35 -o t1433
$ LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64 CUDA_VISIBLE_DEVICES="3" cuda-memcheck ./t1433
========= CUDA-MEMCHECK
B , BT
1 , 1
2 , 4
3 , 2
4 , 5
5 , 3
6 , 6
1 , 1
2 , 3
3 , 5
4 , 2
5 , 4
6 , 6
1 , 1
2 , 2
3 , 2
1 , 3
2 , 3
3 , 4
4 , 1
5 , 5
========= ERROR SUMMARY: 0 errors
$