本方法采用简单的单线程计算每组行和列乘加运算

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <cuda_runtime.h> __global__ void matrixMulKernel(float *C, float *A, float *B, int width, int height){
int tx = blockIdx.x * blockDim.x + threadIdx.x;
int ty = blockIdx.y * blockDim.y + threadIdx.y;
if(tx >= width || ty >= height)
return; float sum = ;
for(int i=; i<width; ++i){
sum += A[ty * width + i] * B[i * width + tx];
} C[ty * width + tx] = sum;
} void constantInit(float *data, int size, float val){
for (int i = ; i < size; ++i){
data[i] = val;
}
} void matrixMul(){
unsigned int width = ;
unsigned int height = ;
unsigned int size = width * height * sizeof(float);
float *h_A = (float*)malloc(size);
float *h_B = (float*)malloc(size);
float *h_C = (float*)malloc(size);
// Initialize host memory
const float valB = 0.01f;
constantInit(h_A, width*height, 1.0f);
constantInit(h_B, width*height, valB); float *d_A, *d_B, *d_C;
cudaMalloc((void**)&d_A, size);
cudaMalloc((void**)&d_B, size);
cudaMalloc((void**)&d_C, size); //copy host memory to device
cudaMemcpy(d_A, h_A, size, cudaMemcpyHostToDevice);
cudaMemcpy(d_B, h_B, size, cudaMemcpyHostToDevice); //config dims
dim3 block(, );
dim3 grid(width / block.x, height / block.y); // Excute the kernel
matrixMulKernel<<<grid, block>>>(d_C, d_A, d_B, width, height); // Copy the memory from device to host
cudaMemcpy(h_C, d_C, size, cudaMemcpyDeviceToHost); printf("Checking computed result for correctness: ");
bool correct = true;
// test relative error by the formula
// |<x, y>_cpu - <x,y>_gpu|/<|x|, |y|> < eps
double eps = .e- ; // machine zero for (int i = ; i < width*height; i++){
double abs_err = fabs(h_C[i] - (width * valB));
double dot_length = width;
double abs_val = fabs(h_C[i]);
double rel_err = abs_err/abs_val/dot_length ;
if (rel_err > eps)
{
printf("Error! Matrix[%05d]=%.8f, ref=%.8f error term is > %E\n", i, h_C[i], (float)(width*height), eps);
correct = false;
}
}
printf("%s\n", correct ? "Result = PASS" : "Result = FAIL"); // Free
free(h_A);
free(h_B);
free(h_C);
cudaFree(d_A);
cudaFree(d_B);
cudaFree(d_C); } int main(){
matrixMul();
}

cuda编程-矩阵乘法(1)的更多相关文章

  1. cuda编程-矩阵乘法(2)

    采用shared memory加速 代码 #include <stdio.h> #include <stdlib.h> #include <math.h> #inc ...

  2. cuda(2) 矩阵乘法优化过程

    Created on 2013-8-5URL : http://blog.sina.com.cn/s/blog_a502f1a30101mjch.html@author: zhxfl转载请说明出处 # ...

  3. CUDA编程之快速入门

    CUDA(Compute Unified Device Architecture)的中文全称为计算统一设备架构.做图像视觉领域的同学多多少少都会接触到CUDA,毕竟要做性能速度优化,CUDA是个很重要 ...

  4. CUDA编程之快速入门【转】

    https://www.cnblogs.com/skyfsm/p/9673960.html CUDA(Compute Unified Device Architecture)的中文全称为计算统一设备架 ...

  5. 详解CUDA编程

    CUDA 是 NVIDIA 的 GPGPU 模型,它使用 C 语言为基础,可以直接以大多数人熟悉的 C 语言,写出在显示芯片上执行的程序,而不需要去学习特定的显示芯片的指令或是特殊的结构.” 编者注: ...

  6. CUDA 矩阵乘法终极优化指南

    作者:马骏 | 旷视 MegEngine 架构师 前言 单精度矩阵乘法(SGEMM)几乎是每一位学习 CUDA 的同学绕不开的案例,这个经典的计算密集型案例可以很好地展示 GPU 编程中常用的优化技巧 ...

  7. OpenCL 矩阵乘法

    ▶ 矩阵乘法,按照书里的内容进行了几方面的优化,包括局部内存,矢量数据类型,寄存器,流水线等. ● 最直接的乘法.调用时 main.c 中使用 size_t globalSize[] = { rowA ...

  8. 【Cuda编程】加法归约

    目录 cuda编程并行归约 AtomicAdd调用出错 gpu cpu下时间计算 加法的归约 矩阵乘法 矩阵转置 统计数目 平方和求和 分块处理 线程相邻 多block计算 cuda编程并行归约 At ...

  9. CUDA编程(十)使用Kahan&#39;s Summation Formula提高精度

    CUDA编程(十) 使用Kahan's Summation Formula提高精度 上一次我们准备去并行一个矩阵乘法.然后我们在GPU上完毕了这个程序,当然是非常单纯的把任务分配给各个线程.也没有经过 ...

随机推荐

  1. 深入理解Proxy 及 使用Proxy实现vue数据双向绑定

    阅读目录 1.什么是Proxy?它的作用是? 2.get(target, propKey, receiver) 3.set(target, propKey, value, receiver) 4.ha ...

  2. linux安装jdk1.8(rpm方式)

    在Oracle官网下载64位的jdk1.8版本 jdk1.8: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloa ...

  3. __attribute__ 机制详解(一)

    GNU C 的一大特色就是__attribute__ 机制.__attribute__ 可以设置函数属性(Function Attribute).变量属性(Variable Attribute)和类型 ...

  4. Vue-插槽学习

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. odoo 11 配置nginx反向代理

    第一步 安装nginx 和 certbot    具体步骤可以参考这篇文章的第6,7步. 第二步 配置nginx映射到odoo网站的文件,主要有2个,文件中的cloudapp.top是我们自己的域名, ...

  6. 使用keras的LSTM进行预测----实战练习

    代码 import numpy as np from keras.models import Sequential from keras.layers import Dense from keras. ...

  7. 简单使用redis实现sso单点登录

    前面几篇分享了nosql只mongodb,今天简单分享另一个nosql神兵redis. 主要模仿sso单点登录,将登录人信息写入redis.话不多说,直接上马,驾. /// <summary&g ...

  8. mariadb(第一章)

      数据库介绍 1.什么是数据库? 简单的说,数据库就是一个存放数据的仓库,这个仓库是按照一定的数据结构(数据结构是指数据的组织形式或数据之间的联系)来组织,存储的,我们可以通过数据库提供的多种方法来 ...

  9. mysqldump 和mysqlbinlog

    一.mysqldump 1.备份test库 #mysqldump -uroot -p' test >test.sql 2.备份 -B参数 ' -B test >test_B.sql --B ...

  10. Python_练习题_49

    # 3.用map来处理字符串列表,把列表中所有人都变成sb,比方alex_sb name=['alex','wupeiqi','yuanhao','nezha'] def func(item): re ...