首页 > 其他分享 >cuda 编 程(9)Error checking in CUDA programs demo

cuda 编 程(9)Error checking in CUDA programs demo

时间:2023-01-16 11:33:25浏览次数:51  
标签:__ const programs checking demo void int double CHECK


error.cuh

#pragma once
#include <stdio.h>

#define CHECK(call) \
do \
{ \
const cudaError_t error_code = call; \
if (error_code != cudaSuccess) \
{ \
printf("CUDA Error:\n"); \
printf(" File: %s\n", __FILE__); \
printf(" Line: %d\n", __LINE__); \
printf(" Error code: %d\n", error_code); \
printf(" Error text: %s\n", \
cudaGetErrorString(error_code)); \
exit(1); \
} \
} while (0)

check1api.cu

#include "error.cuh"
#include <math.h>
#include <stdio.h>

const double EPSILON = 1.0e-15;
const double a = 1.23;
const double b = 2.34;
const double c = 3.57;
void __global__ add(const double *x, const double *y, double *z, const int N);
void check(const double *z, const int N);

int main(void)
{
const int N = 100000000;
const int M = sizeof(double) * N;
double *h_x = (double*) malloc(M);
double *h_y = (double*) malloc(M);
double *h_z = (double*) malloc(M);

for (int n = 0; n < N; ++n)
{
h_x[n] = a;
h_y[n] = b;
}

double *d_x, *d_y, *d_z;
CHECK(cudaMalloc((void **)&d_x, M));
CHECK(cudaMalloc((void **)&d_y, M));
CHECK(cudaMalloc((void **)&d_z, M));
CHECK(cudaMemcpy(d_x, h_x, M, cudaMemcpyDeviceToHost));
CHECK(cudaMemcpy(d_y, h_y, M, cudaMemcpyDeviceToHost));

const int block_size = 128;
const int grid_size = (N + block_size - 1) / block_size;
add<<<grid_size, block_size>>>(d_x, d_y, d_z, N);

CHECK(cudaMemcpy(h_z, d_z, M, cudaMemcpyDeviceToHost));
check(h_z, N);

free(h_x);
free(h_y);
free(h_z);
CHECK(cudaFree(d_x));
CHECK(cudaFree(d_y));
CHECK(cudaFree(d_z));
return 0;
}

void __global__ add(const double *x, const double *y, double *z, const int N)
{
const int n = blockDim.x * blockIdx.x + threadIdx.x;
if (n < N)
{
z[n] = x[n] + y[n];
}
}

void check(const double *z, const int N)
{
bool has_error = false;
for (int n = 0; n < N; ++n)
{
if (fabs(z[n] - c) > EPSILON)
{
has_error = true;
}
}
printf("%s\n", has_error ? "Has errors" : "No errors");
}
nvcc check1api.cu -o check
./check
CUDA Error:
File: check1api.cu
Line: 30
Error code: 1
Error text: invalid argument


标签:__,const,programs,checking,demo,void,int,double,CHECK
From: https://blog.51cto.com/u_15202985/6010278

相关文章

  • mysql mgr demo
    dockercompose配置services:db:container_name:mgr-db-0image:mysql:8.0restart:alwaysenvironment:MYSQL_ROOT_PASSWORD:'123456'......
  • nodemon
         ......
  • 纯css闪烁效果demo
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"conten......
  • LOFAR & DEMON
    LOFAR(Lowfrequencyanalysisandrecording)谱可反映信号的非平稳特性,进而可提取信号中的宽带线谱分布特征。但是轴频及其倍频却因为基本上都淹没在低频宽带噪声中而无......
  • Demo演示:ARM+FPGA主流嵌入式架构板卡-HDMI显示摄像画面
    ​各位工程师小伙伴们,大家好,ARM+FPGA作为一种主流的嵌入式系统的处理架构。相对于单纯的的ARM开发或单纯的FPGA开发,ARM加FPGA能够带来功耗、性能、成本等组合优势。米尔新......
  • 安卓笔记 0 加载模板和设置事件的DEMO
    在onCreate的方法中加载模板2种主要方式:1:setContentView(R.layout.activity_main);2:LinearLayoutmainLinearLayout=(LinearLayout)getLayoutInflate......
  • angular的DEMO(用来练习和顺便看看)
    inflector(辅助) 将用户输入的字符串转化成驼峰或者空格或者底线的小插件;这个是一个小的过滤器,平常也是用不到的,合格是过滤器的代码:app.filter("inflector",......
  • springboot 自动配置 自动监控demo
    1、注解定义@Target({java.lang.annotation.ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic@interfaceUmp{publicabstractSt......
  • 从一个 Demo 说起 Dubbo3
    简介2017年的9月份,阿里宣布重启Dubbo的开发维护,并且后续又将Dubbo捐献给了Apache,经过多年的发展已经发布到3.X版本了,Dubbo重启维护之后是否有值得我们期待的功能呢,下面就来......
  • openssl生成服务器证书时报unable to load number from ./demoCA/serial错误
    1、输入以下命令生成服务器证书时:sudoopensslca-inserver.csr-certca-cert.pem-keyfileca.key-outserver-cert.pem报了如下错误:Usingconfigurationfrom/usr......