当前位置:网站首页>CUDA realizes matrix multiplication
CUDA realizes matrix multiplication
2022-06-30 08:31:00 【Wu lele~】
List of articles
Preface
This paper mainly uses CUDA Realize matrix multiplication .
1、 Simple ideas
#include <stdio.h>
#define BLOCK_NUM 8
#define THREAD_NUM 32
#define R_SIZE BLOCK_NUM * THREAD_NUM
#define M_SIZE R_SIZE*R_SIZE
void __global__ matmul1(int *da, int *db, int *dres);
void __global__ matmul1(int *da, int *db, int *dres)
{
// Get the absolute number of each thread , in total 256 strip
int tid = blockDim.x * blockIdx.x + threadIdx.x;
// Each thread calculates a row of data in the result matrix
// With tid = 0 For example , Need to add up
for(int c=0; c<R_SIZE; ++c)
{
for(int r=0; r<R_SIZE; ++r)
dres[tid*R_SIZE + c] += da[tid*R_SIZE+r] * db[r*R_SIZE+c];
}
}
int main(int argc, char *argv[])
{
// Allocate host memory
int *ha, *hb, *hres;
ha = (int *) malloc (sizeof(int) * M_SIZE);
hb = (int *) malloc (sizeof(int) * M_SIZE);
hres = (int *) malloc(sizeof(int) * M_SIZE);
// assignment
for(int i=0; i<R_SIZE; ++i)
{
for(int j=0; j<R_SIZE; ++j)
{
ha[i*R_SIZE+j] = 1;
hb[i*R_SIZE+j] = 1;
hres[i*R_SIZE+j] = 0;
}
}
// Allocate equipment internal lubrication
int *da, *db, *dres;
cudaMalloc((void**)&da, sizeof(int)*M_SIZE);
cudaMalloc((void**)&db, sizeof(int)*M_SIZE);
cudaMalloc((void**)&dres, sizeof(int)*M_SIZE);
// Copy the data
cudaMemcpy(da,ha, sizeof(int)*M_SIZE, cudaMemcpyHostToDevice);
cudaMemcpy(db,hb, sizeof(int)*M_SIZE, cudaMemcpyHostToDevice);
cudaMemcpy(dres, hres, sizeof(int)*M_SIZE, cudaMemcpyHostToDevice);
// Call the kernel function
matmul1<<<BLOCK_NUM,THREAD_NUM>>>(da,db,dres);
// Copy the data
cudaMemcpy(hres, dres, sizeof(int)*M_SIZE, cudaMemcpyDeviceToHost);
// Print to see
printf("%d\n",hres[0]);
// Free memory
free(ha);
free(hb);
free(hres);
cudaFree(da);
cudaFree(db);
cudaFree(dres);
return 0;
}
analysis
First defined 256 Threads , The number of threads is equal to the number of rows in the matrix . In kernel function , Variable tid Get the of each thread ID. namely [0~255]. Corresponding to the final matrix 256 That's ok . That is, a thread needs to calculate the result matrix of one row . hypothesis tid =0, Then we analyze the double loop in the kernel function , Get separately da The row elements of a matrix and db The column elements of the matrix are multiplied and accumulated to obtain the final solution of the corresponding position .
Matrix multiplication optimization will be introduced later , Remove one layer according to reasonable thread arrangement for loop .
2、 Optimize
#include <stdio.h>
#define BLOCK_NUM 8
#define THREAD_NUM 32
#define R_SIZE BLOCK_NUM * THREAD_NUM
#define M_SIZE R_SIZE*R_SIZE
void __global__ matmul2(int *da, int *db, int *dres);
void __global__ matmul2(int *da, int *db, int *dres)
{
// Get the of each thread ID, Number ID:(row,col). Corresponding to the result matrix That's ok and Column
int row = blockDim.y * blockIdx.y + threadIdx.y;
int col = blockDim.x * blockIdx.x + threadIdx.x;
// The result of each thread , A thread corresponds to an element of a result matrix
for(int i=0; i<R_SIZE; ++i)
{
dres[row*R_SIZE + col] += da[row*R_SIZE+i] * db[i*row+col];
}
}
int main(int argc, char *argv[])
{
// Allocate host memory
int *ha, *hb, *hres;
ha = (int *) malloc (sizeof(int) * M_SIZE);
hb = (int *) malloc (sizeof(int) * M_SIZE);
hres = (int *) malloc(sizeof(int) * M_SIZE);
// assignment
for(int i=0; i<R_SIZE; ++i)
{
for(int j=0; j<R_SIZE; ++j)
{
ha[i*R_SIZE+j] = 1;
hb[i*R_SIZE+j] = 1;
hres[i*R_SIZE+j] = 0;
}
}
// Allocate equipment internal lubrication
int *da, *db, *dres;
cudaMalloc((void**)&da, sizeof(int)*M_SIZE);
cudaMalloc((void**)&db, sizeof(int)*M_SIZE);
cudaMalloc((void**)&dres, sizeof(int)*M_SIZE);
// Copy the data
cudaMemcpy(da,ha, sizeof(int)*M_SIZE, cudaMemcpyHostToDevice);
cudaMemcpy(db,hb, sizeof(int)*M_SIZE, cudaMemcpyHostToDevice);
cudaMemcpy(dres, hres, sizeof(int)*M_SIZE, cudaMemcpyHostToDevice);
// Call the kernel function
// Assign threads
const dim3 grid_size(BLOCK_NUM, BLOCK_NUM);
const dim3 block_size(THREAD_NUM, THREAD_NUM);
matmul2<<<grid_size, block_size>>>(da,db,dres);
// Copy the data
cudaMemcpy(hres, dres, sizeof(int)*M_SIZE, cudaMemcpyDeviceToHost);
// Print to see
printf("%d\n",hres[0]);
// Free memory
free(ha);
free(hb);
free(hres);
cudaFree(da);
cudaFree(db);
cudaFree(dres);
return 0;
}
summary
How to understand , Thread is const dim3 block_size(8,8); Formal definition .
边栏推荐
- 增强for循环的增删操作 & 迭代器删除集合元素
- Cesium learning notes (V) custom geometry and appearance
- 牛客小白月賽52
- End-to-end 3D Point Cloud Instance Segmentation without Detection
- MySQL quotation sentence is unlocked: algorithm=insert, lock=none
- Unity basic lighting model
- 【NVMe2.0b 14-4】Directive Send/Receive command
- Experiment 2 LED button PWM 2021/11/22
- Using typera+picgo to realize automatic uploading of markdown document pictures
- 1163 Dijkstra Sequence
猜你喜欢

Using typera+picgo to realize automatic uploading of markdown document pictures

What are the Amazon evaluation terms?

【NVMe2.0b 14-6】Format NVM、Keep Alive、Lockdown command

【NVMe2.0b 14-6】Format NVM、Keep Alive、Lockdown command

Self made GIF dynamic graph -gifcam

【NVMe2.0b 14-1】Abort、Asynchronous Event Request、Capacity Management command

Axure make menu bar effect

Redis设计与实现(三)| 服务器与客户端的交互(事件IO模型)

An example of a single service in a cloud project driven by a domain

【NVMe2.0b 14-5】Firmware Download/Commit command
随机推荐
1162 Postfix Expression
Axure制作菜单栏效果
Introduction to opencv (II): image color space conversion and image saving
文件上传 upload 组件 on-success 事件,添加自定义参数
Experiment 2 LED button PWM 2021/11/22
Unity simple shader
QT event cycle
【NVMe2.0b 14-4】Directive Send/Receive command
【NVMe2.0b 14-5】Firmware Download/Commit command
Experiment 4 QT
【NVMe2.0b 14-2】Create/Delete Queue
Camera
【NVMe2.0b 14-6】Format NVM、Keep Alive、Lockdown command
Opencv video
Unity basic lighting model
Wechat official account third-party platform development, zero foundation entry. I want to teach you
Deploy the cow like customer network project on the ECS
Dlib library blink
Redis设计与实现(七)| 发布 & 订阅
TiDB 6.0:让 TSO 更高效丨TiDB Book Rush