当前位置:网站首页>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 .
边栏推荐
- 微信公众号第三方平台开发,零基础入门。想学我教你啊
- 从0开始构建一个瀚高数据库Docker镜像
- TiDB v6.0.0 (DMR) :缓存表初试丨TiDB Book Rush
- A troubleshooting of CPU bottom falling
- Flink 数据偶尔数据积压导致checkpoint失败
- Do you know the IP protocol?
- 增强for循环的增删操作 & 迭代器删除集合元素
- 【NVMe2.0b 14】NVMe Admin Command Set
- Flink sql -- No factory implements ‘org.apache.flink.table.delegation.ExecutorFactory‘.
- layer.open 当传值为数组或值太长时处理方法
猜你喜欢

Redis design and Implementation (III) | interaction between server and client (event IO model)

Wechat applet reports errors using vant web app

Flink Exception -- No ExecutorFactory found to execute the application

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

vim 从嫌弃到依赖(21)——跨文件搜索

Cesium learning notes (III) creating instances

codeforces每日5题(均1700)-第三天

Self made GIF dynamic graph -gifcam

Graffiti Wi Fi & ble SoC development slide strip
![[kotlin collaboration process] complete the advanced kotlin collaboration process](/img/43/9c4b337caf406537e317dea2ed5f17.png)
[kotlin collaboration process] complete the advanced kotlin collaboration process
随机推荐
Introduction to opencv (II): image color space conversion and image saving
C# ListBox如何获取选中的内容(搜了很多无效的文章)
Pycharm Dlib library installation
Niuke Xiaobai month race 52
1162 Postfix Expression
Interference source current spectrum test of current probe
2021-05-17
Cesium learning notes (I)
Unit Test
Gilbert Strang's course notes on linear algebra - Lesson 2
国债逆回购绝对安全吗 网上怎么开户
【NVMe2.0b 14-8】Set Features(下篇)
【NVMe2.0b 14-5】Firmware Download/Commit command
vite項目require語法兼容問題解决require is not defined
An example of a single service in a cloud project driven by a domain
Sword finger offer II 076 The kth largest number in the array (use heap to solve TOPK problem)
2021-02-18
挖财开户安全吗?怎么有人说不靠谱。
Flink 数据偶尔数据积压导致checkpoint失败
codeforces每日5题(均1700)-第三天