cuda编程---cuda硬件信息与错误处置

2023-11-24 10:58

本文主要是介绍cuda编程---cuda硬件信息与错误处置,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、硬件信息查询:
#include <stdio.h>int main() {int nDevices;cudaGetDeviceCount(&nDevices);for (int i=0; i < nDevices; i++) {cudaDeviceProp prop;cudaGetDeviceProperties(&prop, i);printf("Device Number: %d\n", i);printf("  Device name: %s\n", prop.name);printf("  Memory Clock Rate (KHz): %d\n",prop.memoryClockRate);printf("  Memory Bus Width (bits): %d\n",prop.memoryBusWidth);printf("  Peak Memory Bandwidth (GB/s): %f\n\n",2.0*prop.memoryClockRate*(prop.memoryBusWidth/8)/1.0e6);}return 0;
}
二、错误处置:

1、代码段一:

#include <stdio.h>int main() {int nDevices;cudaError_t err = cudaGetDeviceCount(&nDevices);if (err != cudaSuccess) printf("%s\n", cudaGetErrorString(err));for (int i=0; i < nDevices; i++) {cudaDeviceProp prop;cudaGetDeviceProperties(&prop, i);printf("Device Number: %d\n", i);printf("  Device name: %s\n", prop.name);printf("  Memory Clock Rate (KHz): %d\n",prop.memoryClockRate);printf("  Memory Bus Width (bits): %d\n",prop.memoryBusWidth);printf("  Peak Memory Bandwidth (GB/s): %f\n\n",2.0*prop.memoryClockRate*(prop.memoryBusWidth/8)/1.0e6);}return 0;
}

这段代码在下面处有改变:
cudaError_t err = cudaGetDeviceCount(&nDevices);
if (err != cudaSuccess)
printf("%s\n", cudaGetErrorString(err));

2、代码段二:

#include <iostream>
#include <math.h>
#include <stdio.h>__global__
void saxpy(int n,float a,float *x,float *y)
{int i = blockIdx.x*blockDim.x +threadIdx.x;if (i < n) y[i] = a*x[i] + y[i];
}int main(void){int N = 1 << 20; //1M element.//float *x=new float[N];//float *y=new float[N];//Allocate Unified Memory -- accessible from CPU or GPUfloat *x, *y, *d_x, *d_y;x = (float*)malloc(N*sizeof(float));y = (float*)malloc(N*sizeof(float));cudaMalloc(&d_x, N*sizeof(float));cudaMalloc(&d_y, N*sizeof(float));//initialize x and y arrays on the host.for (int i=0;i<N;i++){x[i]=1.0f;y[i]=2.0f;}cudaEvent_t start, stop;cudaEventCreate(&start);cudaEventCreate(&stop);cudaMemcpy(d_x, x, N*sizeof(float), cudaMemcpyHostToDevice);cudaMemcpy(d_y, y, N*sizeof(float), cudaMemcpyHostToDevice);cudaEventRecord(start);saxpy<<< (N+255)/256, 256>>>(N, 2.0, d_x, d_y);cudaError_t errSync = cudaGetLastError();cudaError_t errAsync = cudaDeviceSynchronize();if (errSync != cudaSuccess)printf("Sync kernel error: %s\n", cudaGetErrorString(errSync));if (errAsync != cudaSuccess)printf("Async kernel error: %s\n",cudaGetErrorString(errAsync));cudaEventRecord(stop);cudaMemcpy(y, d_y, N*sizeof(float), cudaMemcpyDeviceToHost);cudaEventSynchronize(stop);float milliseconds = 0;cudaEventElapsedTime(&milliseconds, start, stop);// Check for errors (all values should be 3.0f)float maxError=0.0f;for (int i=0;i<N;i++)maxError=max(maxError,fabs(y[i]-4.0f));printf("Max error: %f . \n", maxError);printf("Effective Bandwidth (GB/s): %f .\n", N*4*3/milliseconds/1e6);cudaFree(x);cudaFree(y);cudaFree(d_x);cudaFree(d_y);cudaEventDestroy(start);cudaEventDestroy(stop);return 0;
}

这篇关于cuda编程---cuda硬件信息与错误处置的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/421800

相关文章

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

Linux如何快速检查服务器的硬件配置和性能指标

《Linux如何快速检查服务器的硬件配置和性能指标》在运维和开发工作中,我们经常需要快速检查Linux服务器的硬件配置和性能指标,本文将以CentOS为例,介绍如何通过命令行快速获取这些关键信息,... 目录引言一、查询CPU核心数编程(几C?)1. 使用 nproc(最简单)2. 使用 lscpu(详细信

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

如何解决Druid线程池Cause:java.sql.SQLRecoverableException:IO错误:Socket read timed out的问题

《如何解决Druid线程池Cause:java.sql.SQLRecoverableException:IO错误:Socketreadtimedout的问题》:本文主要介绍解决Druid线程... 目录异常信息触发场景找到版本发布更新的说明从版本更新信息可以看到该默认逻辑已经去除总结异常信息触发场景复

Python struct.unpack() 用法及常见错误详解

《Pythonstruct.unpack()用法及常见错误详解》struct.unpack()是Python中用于将二进制数据(字节序列)解析为Python数据类型的函数,通常与struct.pa... 目录一、函数语法二、格式字符串详解三、使用示例示例 1:解析整数和浮点数示例 2:解析字符串示例 3:解

CentOS 7 YUM源配置错误的解决方法

《CentOS7YUM源配置错误的解决方法》在使用虚拟机安装CentOS7系统时,我们可能会遇到YUM源配置错误的问题,导致无法正常下载软件包,为了解决这个问题,我们可以替换YUM源... 目录一、备份原有的 YUM 源配置文件二、选择并配置新的 YUM 源三、清理旧的缓存并重建新的缓存四、验证 YUM 源

Linux查看系统盘和SSD盘的容量、型号及挂载信息的方法

《Linux查看系统盘和SSD盘的容量、型号及挂载信息的方法》在Linux系统中,管理磁盘设备和分区是日常运维工作的重要部分,而lsblk命令是一个强大的工具,它用于列出系统中的块设备(blockde... 目录1. 查看所有磁盘的物理信息方法 1:使用 lsblk(推荐)方法 2:使用 fdisk -l(

SpringBoot如何对密码等敏感信息进行脱敏处理

《SpringBoot如何对密码等敏感信息进行脱敏处理》这篇文章主要为大家详细介绍了SpringBoot对密码等敏感信息进行脱敏处理的几个常用方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录​1. 配置文件敏感信息脱敏​​2. 日志脱敏​​3. API响应脱敏​​4. 其他注意事项​​总结