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

相关文章

MySQL的JDBC编程详解

《MySQL的JDBC编程详解》:本文主要介绍MySQL的JDBC编程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、前置知识1. 引入依赖2. 认识 url二、JDBC 操作流程1. JDBC 的写操作2. JDBC 的读操作总结前言本文介绍了mysq

深度解析Java @Serial 注解及常见错误案例

《深度解析Java@Serial注解及常见错误案例》Java14引入@Serial注解,用于编译时校验序列化成员,替代传统方式解决运行时错误,适用于Serializable类的方法/字段,需注意签... 目录Java @Serial 注解深度解析1. 注解本质2. 核心作用(1) 主要用途(2) 适用位置3

Debian 13升级后网络转发等功能异常怎么办? 并非错误而是管理机制变更

《Debian13升级后网络转发等功能异常怎么办?并非错误而是管理机制变更》很多朋友反馈,更新到Debian13后网络转发等功能异常,这并非BUG而是Debian13Trixie调整... 日前 Debian 13 Trixie 发布后已经有众多网友升级到新版本,只不过升级后发现某些功能存在异常,例如网络转

Python异步编程之await与asyncio基本用法详解

《Python异步编程之await与asyncio基本用法详解》在Python中,await和asyncio是异步编程的核心工具,用于高效处理I/O密集型任务(如网络请求、文件读写、数据库操作等),接... 目录一、核心概念二、使用场景三、基本用法1. 定义协程2. 运行协程3. 并发执行多个任务四、关键

AOP编程的基本概念与idea编辑器的配合体验过程

《AOP编程的基本概念与idea编辑器的配合体验过程》文章简要介绍了AOP基础概念,包括Before/Around通知、PointCut切入点、Advice通知体、JoinPoint连接点等,说明它们... 目录BeforeAroundAdvise — 通知PointCut — 切入点Acpect — 切面

SpringBoot3匹配Mybatis3的错误与解决方案

《SpringBoot3匹配Mybatis3的错误与解决方案》文章指出SpringBoot3与MyBatis3兼容性问题,因未更新MyBatis-Plus依赖至SpringBoot3专用坐标,导致类冲... 目录SpringBoot3匹配MyBATis3的错误与解决mybatis在SpringBoot3如果

C#异步编程ConfigureAwait的使用小结

《C#异步编程ConfigureAwait的使用小结》本文介绍了异步编程在GUI和服务器端应用的优势,详细的介绍了async和await的关键作用,通过实例解析了在UI线程正确使用await.Conf... 异步编程是并发的一种形式,它有两大好处:对于面向终端用户的GUI程序,提高了响应能力对于服务器端应

nginx配置错误日志的实现步骤

《nginx配置错误日志的实现步骤》配置nginx代理过程中,如果出现错误,需要看日志,可以把nginx日志配置出来,以便快速定位日志问题,下面就来介绍一下nginx配置错误日志的实现步骤,感兴趣的可... 目录前言nginx配置错误日志总结前言在配置nginx代理过程中,如果出现错误,需要看日志,可以把

C# async await 异步编程实现机制详解

《C#asyncawait异步编程实现机制详解》async/await是C#5.0引入的语法糖,它基于**状态机(StateMachine)**模式实现,将异步方法转换为编译器生成的状态机类,本... 目录一、async/await 异步编程实现机制1.1 核心概念1.2 编译器转换过程1.3 关键组件解析

SQL Server跟踪自动统计信息更新实战指南

《SQLServer跟踪自动统计信息更新实战指南》本文详解SQLServer自动统计信息更新的跟踪方法,推荐使用扩展事件实时捕获更新操作及详细信息,同时结合系统视图快速检查统计信息状态,重点强调修... 目录SQL Server 如何跟踪自动统计信息更新:深入解析与实战指南 核心跟踪方法1️⃣ 利用系统目录