C++ 如何在CUDA中执行多重矩阵乘法?
我有一个方阵数组C++ 如何在CUDA中执行多重矩阵乘法?,c++,c++11,matrix,parallel-processing,cuda,C++,C++11,Matrix,Parallel Processing,Cuda,我有一个方阵数组int*M[10]以便M[i]定位i-矩阵的第一个元素。我想将所有矩阵M[I]乘以另一个矩阵N,这样我就可以收到一个方阵int*P[10]作为输出 我看到了不同的可能性: 将M[i]的不同元素的计算分配给不同的线程;例如,我有10矩阵,4x4大小,因此涉及的线程数将是160;如何使用CUDA实现这种方法 在上述示例的框架中,创建复合矩阵大小40x40(即,收集10,4x4大小的矩阵)并使用40x40线程;但这种方法似乎需要更多的时间;我尝试使用矩阵数组,但我认为我做错了什么;如何
int*M[10]编码>以便M[i]
定位i
-矩阵的第一个元素。我想将所有矩阵M[I]
乘以另一个矩阵N
,这样我就可以收到一个方阵int*P[10]
作为输出
我看到了不同的可能性:
将M[i]
的不同元素的计算分配给不同的线程;例如,我有10
矩阵,4x4
大小,因此涉及的线程数将是160
;如何使用CUDA实现这种方法李>
在上述示例的框架中,创建复合矩阵大小40x40
(即,收集10
,4x4
大小的矩阵)并使用40x40
线程;但这种方法似乎需要更多的时间;我尝试使用矩阵数组,但我认为我做错了什么;如何将此方法用于10
矩阵?如何在内核函数中编写它李>
这就是我正在尝试的
void GPU_Multi(int *M[2], int *N, int *P[2], size_t width)
{
int *devM[2];
int *devN[2];
int *devP[2];
size_t allocasize =sizeof(int) *width*width;
for(int i = 0 ; i < 10 ; i ++ )
{
cudaMalloc((void**)&devM[ i ], allocasize );
cudaMalloc((void**)&devP[ i ], allocasize );
}
cudaMalloc((void**)&devN, allocasize );
for(int i = 0 ; i < 10 ; i ++ ) {
cudaMemcpy(devM[ i ],M[ i ], allocasize , cudaMemcpyHostToDevice);
cudaMemcpy(devN, N, allocasize , cudaMemcpyHostToDevice);
dim3 block(width*2, width*2);
dim3 grid(1,1,1);
Kernel_Function<<<grid, block>>> (devM[2], devN, devP[2],width);
for(int i = 0 ; i < 10 ; i ++ )
{
cudaMemcpy(P[ i ], P[ i ], allocatesize, cudaMemcpyDeviceToHost);
cudaFree(devM[ i ]);
cudaFree(devP[ i ]);
}
}
void GPU\u Multi(int*M[2],int*N,int*P[2],size\u t width)
{
int*devM[2];
int*devN[2];
int*devP[2];
尺寸=尺寸(整数)*宽度*宽度;
对于(int i=0;i<10;i++)
{
cudamaloc((void**)和devM[i],allocasize);
cudamaloc((void**)和devP[i],allocasize);
}
cudamaloc((void**)和devN,allocasize);
对于(int i=0;i<10;i++){
cudaMemcpy(devM[i],M[i],allocasize,cudamemcpyhostodevice);
cudaMemcpy(devN,N,allocasize,cudamemcpyhostodevice);
dim3块(宽度*2,宽度*2);
dim3网格(1,1,1);
核函数(devM[2],devN,devP[2],宽度);
对于(int i=0;i<10;i++)
{
cudaMemcpy(P[i],P[i],allocatesize,cudaMemcpyDeviceToHost);
cudaFree(devM[i]);
cudaFree(devP[i]);
}
}
我认为,通过使用专门为此目的设计的(执行大量“相对较小”的矩阵乘法运算),可能会获得最快的性能
即使您希望将矩阵数组(M[]
)乘以单个矩阵(N
),批处理gemm函数也要求您传递N
(即N[]
)的矩阵数组,这在您的情况下都是相同的
编辑:现在我已经完成了一个示例,对下面的示例进行修改后,我似乎很清楚,我们可以传递一个N
矩阵,让GPU Multi
函数只需将单个N
矩阵发送到设备,同时传递N
的指针数组,即d_Narray
在下面的示例中,所有指针都指向设备上相同的N
矩阵
下面是一个完全工作的批处理GEMM示例:
#include <stdio.h>
#include <cuda_runtime.h>
#include <cublas_v2.h>
#include <assert.h>
#define ROWM 4
#define COLM 3
#define COLN 5
#define cudaCheckErrors(msg) \
do { \
cudaError_t __err = cudaGetLastError(); \
if (__err != cudaSuccess) { \
fprintf(stderr, "Fatal error: %s (%s at %s:%d)\n", \
msg, cudaGetErrorString(__err), \
__FILE__, __LINE__); \
fprintf(stderr, "*** FAILED - ABORTING\n"); \
exit(1); \
} \
} while (0)
typedef float mytype;
// Pi = Mi x Ni
// pr = P rows = M rows
// pc = P cols = N cols
// mc = M cols = N rows
void GPU_Multi(mytype **M, mytype **N, mytype **P
, size_t pr, size_t pc, size_t mc
, size_t num_mat, mytype alpha, mytype beta)
{
mytype *devM[num_mat];
mytype *devN[num_mat];
mytype *devP[num_mat];
size_t p_size =sizeof(mytype) *pr*pc;
size_t m_size =sizeof(mytype) *pr*mc;
size_t n_size =sizeof(mytype) *mc*pc;
const mytype **d_Marray, **d_Narray;
mytype **d_Parray;
cublasHandle_t myhandle;
cublasStatus_t cublas_result;
for(int i = 0 ; i < num_mat; i ++ )
{
cudaMalloc((void**)&devM[ i ], m_size );
cudaMalloc((void**)&devN[ i ], n_size );
cudaMalloc((void**)&devP[ i ], p_size );
}
cudaMalloc((void**)&d_Marray, num_mat*sizeof(mytype *));
cudaMalloc((void**)&d_Narray, num_mat*sizeof(mytype *));
cudaMalloc((void**)&d_Parray, num_mat*sizeof(mytype *));
cudaCheckErrors("cudaMalloc fail");
for(int i = 0 ; i < num_mat; i ++ ) {
cudaMemcpy(devM[i], M[i], m_size , cudaMemcpyHostToDevice);
cudaMemcpy(devN[i], N[i], n_size , cudaMemcpyHostToDevice);
cudaMemcpy(devP[i], P[i], p_size , cudaMemcpyHostToDevice);
}
cudaMemcpy(d_Marray, devM, num_mat*sizeof(mytype *), cudaMemcpyHostToDevice);
cudaMemcpy(d_Narray, devN, num_mat*sizeof(mytype *), cudaMemcpyHostToDevice);
cudaMemcpy(d_Parray, devP, num_mat*sizeof(mytype *), cudaMemcpyHostToDevice);
cudaCheckErrors("cudaMemcpy H2D fail");
cublas_result = cublasCreate(&myhandle);
assert(cublas_result == CUBLAS_STATUS_SUCCESS);
// change to cublasDgemmBatched for double
cublas_result = cublasSgemmBatched(myhandle, CUBLAS_OP_N, CUBLAS_OP_N
, pr, pc, mc
, &alpha, d_Marray, pr, d_Narray, mc
, &beta, d_Parray, pr
, num_mat);
assert(cublas_result == CUBLAS_STATUS_SUCCESS);
for(int i = 0 ; i < num_mat ; i ++ )
{
cudaMemcpy(P[i], devP[i], p_size, cudaMemcpyDeviceToHost);
cudaFree(devM[i]);
cudaFree(devN[i]);
cudaFree(devP[i]);
}
cudaFree(d_Marray);
cudaFree(d_Narray);
cudaFree(d_Parray);
cudaCheckErrors("cudaMemcpy D2H fail");
}
int main(){
mytype h_M1[ROWM][COLM], h_M2[ROWM][COLM];
mytype h_N1[COLM][COLN], h_N2[COLM][COLN];
mytype h_P1[ROWM][COLN], h_P2[ROWM][COLN];
mytype *h_Marray[2], *h_Narray[2], *h_Parray[2];
for (int i = 0; i < ROWM; i++)
for (int j = 0; j < COLM; j++){
h_M1[i][j] = 1.0f; h_M2[i][j] = 2.0f;}
for (int i = 0; i < COLM; i++)
for (int j = 0; j < COLN; j++){
h_N1[i][j] = 1.0f; h_N2[i][j] = 1.0f;}
for (int i = 0; i < ROWM; i++)
for (int j = 0; j < COLN; j++){
h_P1[i][j] = 0.0f; h_P2[i][j] = 0.0f;}
h_Marray[0] = &(h_M1[0][0]);
h_Marray[1] = &(h_M2[0][0]);
h_Narray[0] = &(h_N1[0][0]);
h_Narray[1] = &(h_N2[0][0]);
h_Parray[0] = &(h_P1[0][0]);
h_Parray[1] = &(h_P2[0][0]);
GPU_Multi(h_Marray, h_Narray, h_Parray, ROWM, COLN, COLM, 2, 1.0f, 0.0f);
for (int i = 0; i < ROWM; i++)
for (int j = 0; j < COLN; j++){
if (h_P1[i][j] != COLM*1.0f)
{
printf("h_P1 mismatch at %d,%d was: %f should be: %f\n"
, i, j, h_P1[i][j], COLM*1.0f); return 1;
}
if (h_P2[i][j] != COLM*2.0f)
{
printf("h_P2 mismatch at %d,%d was: %f should be: %f\n"
, i, j, h_P2[i][j], COLM*2.0f); return 1;
}
}
printf("Success!\n");
return 0;
}
#包括
#包括
#包括
#包括
#定义第4行
#定义COLM 3
#定义COLN 5
#定义cudaCheckErrors(msg)\
做{\
cudaError\u t\u err=cudaGetLastError()\
如果(_err!=cudaSuccess){\
fprintf(标准,“致命错误:%s(%s位于%s:%d)\n”\
msg,cudaGetErrorString(_err)\
__文件(行)\
fprintf(stderr,“***失败-中止\n”)\
出口(1)\
} \
}而(0)
typedef-float-mytype;
//Pi=Mi x Ni
//pr=P行=M行
//pc=P列=N列
//mc=M列=N行
无效GPU多(mytype**M,mytype**N,mytype**P
,尺码为pr,尺码为pc,尺码为mc
、大小、数量、mytype alpha、mytype beta)
{
mytype*devM[num_mat];
mytype*devN[num_mat];
mytype*devP[num_mat];
大小p大小=大小F(mytype)*pr*pc;
尺寸=尺寸(mytype)*pr*mc;
大小n大小=大小f(mytype)*mc*pc;
const mytype**d_Marray,**d_Narray;
mytype**d_Parray;
cublasHandle_t myhandle;
cublasStatus_t cublas_结果;
对于(int i=0;ih_M1
0.000000 1.000000 2.000000
1.000000 2.000000 3.000000
2.000000 3.000000 4.000000
3.000000 4.000000 5.000000
h_N1
0.000000 1.000000 2.000000 3.000000 4.000000
1.000000 2.000000 3.000000 4.000000 5.000000
2.000000 3.000000 4.000000 5.000000 6.000000
h_M2
0.000000 -2.000000 -4.000000
2.000000 0.000000 -2.000000
4.000000 2.000000 0.000000
6.000000 4.000000 2.000000
h_N2
0.000000 -1.000000 -2.000000 -3.000000 -4.000000
1.000000 0.000000 -1.000000 -2.000000 -3.000000
2.000000 1.000000 0.000000 -1.000000 -2.000000
h_P1
5.000000 8.000000 11.000000 14.000000 17.000000
8.000000 14.000000 20.000000 26.000000 32.000000
11.000000 20.000000 29.000000 38.000000 47.000000
14.000000 26.000000 38.000000 50.000000 62.000000
h_P2
-10.000000 -4.000000 2.000000 8.000000 14.000000
-4.000000 -4.000000 -4.000000 -4.000000 -4.000000
2.000000 -4.000000 -10.000000 -16.000000 -22.000000
8.000000 -4.000000 -16.000000 -28.000000 -40.000000