有返回值的函数
error_code = cudaDeviceReset();
#include<stdio.h>int main() {cudaError_t error_code = cudaDeviceReset();printf("returned error code:%d \n", error_code); // a cudaError_t variable can be regarded as a integerprintf("cudaSuccess:%d, error_code==cudaSuccess:%d \n", cudaSuccess, cudaSuccess==error_code);if(error_code==cudaSuccess){printf("CUDA API successed!\n");}else{printf("Error needs to be handled! code:%d \n", error_code);}return 0;}
获取对应行数和文件名
#include<stdio.h>int main() {cudaDeviceProp prop;int device_id = 0;printf("\nGet properties from device #%d:\n", device_id);cudaError_t error_code = cudaGetDeviceProperties(&prop, device_id);if(error_code==cudaSuccess){printf("CUDA API successed!\n");}else if(error_code==cudaErrorInvalidDevice){printf("Invalid Device! code:%d \n", error_code);}device_id = 1;printf("\nGet properties from device #%d:\n", device_id);error_code = cudaGetDeviceProperties(&prop, device_id); // 这一行有错,没有足够GPUif(error_code==cudaSuccess){printf("CUDA API successed!\n");}else if(error_code==cudaErrorInvalidDevice){printf("Invalid Device! code:%d \n", error_code);printf("line:%d in %s\n", __LINE__, __FILE__);// 打印行数和文件名}device_id = 2;printf("\nGet properties from device #%d:\n", device_id);error_code = cudaGetDeviceProperties(&prop, device_id);if(error_code==cudaSuccess){printf("CUDA API successed!\n");}else if(error_code==cudaErrorInvalidDevice){printf("Invalid Device! code:%d \n", error_code);printf("line:%d in %s\n", __LINE__, __FILE__);}return 0;}
Kernel 调用无返回值,获取最近一次错误,并且打印提示字符
error_code = cudaGetLastError();cudaGetErrorString(error_code)
#include<stdio.h>__global__ void hello(){printf("*");}int main() {cudaError_t error_code;hello<<<-1, 1>>>(); // 不可调用-1error_code = cudaGetLastError();if(error_code!=cudaSuccess){printf("\n");printf("line:%d in %s\n", __LINE__, __FILE__);printf("Error needs to be handled!\n");printf("Error code:%d \n", error_code);printf("Error string:%s \n", cudaGetErrorString(error_code));}hello<<<1, 1025>>>(); // 线程数越界error_code = cudaGetLastError();if(error_code!=cudaSuccess){printf("\n");printf("line:%d in %s\n", __LINE__, __FILE__);printf("Error needs to be handled!\n");printf("Error code:%d \n", error_code);printf("Error string:%s \n", cudaGetErrorString(error_code));}error_code = cudaDeviceSynchronize();if(error_code!=cudaSuccess){printf("\n");printf("line:%d in %s\n", __LINE__, __FILE__);printf("Error needs to be handled!\n");printf("Error code:%d \n", error_code);printf("Error string:%s \n", cudaGetErrorString(error_code));}error_code = cudaDeviceReset();if(error_code!=cudaSuccess){printf("\n");printf("line:%d in %s\n", __LINE__, __FILE__);printf("Error needs to be handled!\n");printf("Error code:%d \n", error_code);printf("Error string:%s \n", cudaGetErrorString(error_code));}return 0;}
s
#include<stdio.h>#define CHECK(X);{ \cudaError_t error_code=X;; \error_code = cudaGetLastError(); \if(error_code!=cudaSuccess){ \printf("\n"); \printf("line:%d in %s\n", __LINE__, __FILE__); \printf("Error needs to be handled!\n"); \printf("Error code:%d \n", error_code); \printf("Error string:%s \n", cudaGetErrorString(error_code)); \} \}
心得:
注意观察,不是直接调用的CHECK kernel 函数,经过查找资料,充分了解,才能正确的实现功能,不能执拗地违背规律。
总之需要详细地调研,才能更好地分析,巧妇难为无米之炊。

