working on capter3

This commit is contained in:
qinzhaoyu 2021-11-15 09:33:47 +08:00
parent ae3602a1cf
commit 57465f51ad
3 changed files with 160 additions and 0 deletions

32
capter3/ReadMe.md Normal file
View File

@ -0,0 +1,32 @@
# 简单 CUDA 程序的基本框架
------
## 单源文件 CUDA 程序基本框架
对于单源文件的 cuda 程序,基本框架为:
包含头文件
定义常量或宏
声明 c++ 自定义函数和 cuda 核函数的原型
int main()
{
分配主机和设备内存
初始化主机中数据
将某些数据从主机复制到设备
调用核函数在设备中计算
将某些数据从设备复制到主机
释放主机和设备内存
}
c++ 自定义函数和 cuda 核函数的定义

64
capter3/add.cpp Normal file
View File

@ -0,0 +1,64 @@
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
const double EPSILON = 1.0e-10;
const double a = 1.23;
const double b = 2.34;
const double c = 3.57;
void add(const double *x, const double *y, double *z, const int N);
void check(const double *z, const int N);
int main()
{
const int N = 1e4;
const int M = sizeof(double) * N;
// 申请内存。
double *x = (double*) malloc(M);
double *y = (double*) malloc(M);
double *z = (double*) malloc(M);
for (int i = 0; i < N; ++i)
{
x[i] = a;
y[i] = b;
}
add(x, y, z, N);
check(z, N);
// 释放内存。
free(x);
free(y);
free(z);
return 0;
}
void add(const double *x, const double *y, double *z, const int N)
{
for (int i = 0; i < N; ++i)
{
z[i] = x[i] + y[i];
}
}
void check(const double *z, const int N)
{
bool has_error = false;
for (int i = 0; i < N ;++i)
{
if (fabs(z[i] - c) > EPSILON)
{
has_error = true;
}
}
printf("%s\n", has_error ? "has error" : "no error");
}

64
capter3/add.cu Normal file
View File

@ -0,0 +1,64 @@
#include <math.h>
//#include <stdlib.h>
#include <stdio.h>
const double EPSILON = 1.0e-10;
const double a = 1.23;
const double b = 2.34;
const double c = 3.57;
__global__ void add(const double *x, const double *y, double *z, const int N);
void check(const double *z, const int N);
int main()
{
const int N = 1e4;
const int M = sizeof(double) * N;
// 申请内存。
double *x = (double*) malloc(M);
double *y = (double*) malloc(M);
double *z = (double*) malloc(M);
for (int i = 0; i < N; ++i)
{
x[i] = a;
y[i] = b;
}
add(x, y, z, N);
check(z, N);
// 释放内存。
free(x);
free(y);
free(z);
return 0;
}
void add(const double *x, const double *y, double *z, const int N)
{
for (int i = 0; i < N; ++i)
{
z[i] = x[i] + y[i];
}
}
void check(const double *z, const int N)
{
bool has_error = false;
for (int i = 0; i < N ;++i)
{
if (fabs(z[i] - c) > EPSILON)
{
has_error = true;
}
}
printf("%s\n", has_error ? "has error" : "no error");
}